On Thu, 27 Aug 2015, Josh Koffman wrote: > Hi Sergio, > > Thanks for the ideas. Just for reference, in xc8 you can enable and > disable interrupts by using ei() and di() respectively. > > For my BAM code I'm hoping to do 8 channels at once, so I'll be > preprocessing the current dimmer value of all channels to make a > single variable that holds the state of all channels for any given > time slice. Hopefully it works out! In that case: // assume all 8 channels are in a single port #define BAM_PORT PORTA // globals unsigned char bit_cnt, bit_mask, bit_pos, BAM_buff[8], chan_buff[8]; // BAM_buff[j] is the slice of chan_buff[][j] // i.e. row[j] of BAM_buff consists of col[j] of chan_buff // from what I've read, there is no advantage to storing // chan_buff[][7] as BAM_buff[0] so using left shifts in the // following code just makes things much more complicated for // no added benefit void init_BAM(void) { // disable interrupts di(); bit_cnt =3D 1; bit_mask =3D 0x80; bit_pos =3D 0; for (j=3D0; j<8; j++) { BAM_buff[j] =3D 0; chan_buff[j] =3D 0; } BAM_PORT =3D 0; // enable interrupts ei(); } void set_channel_value(unsigned char indx, unsigned char val) { unsigned char j, mask; // note for this to work col[j] of chan_buff is rearranged in row[j] of BAM= _buff chan_buff[indx] =3D val; mask =3D 1 << indx; for (j=3D0; j<8; j++) { if ((val & 1) =3D=3D 0) { // bit j of chan_buff[indx] is 0 so // clear the coressponding bit BAM_buff[j] &=3D ~mask; } else { // bit j of chan_buff[indx] is 1 so // set the coressponding bit BAM_buff[j] |=3D mask; } val >>=3D 1; } // disable interrupts di(); BAM_PORT =3D BAM_buff[bit_pos]; // enable interrupts ei(); } // this proc is called within the ISR void service_BAM(void) { // this proc processes 8 channels in parallel // each channel is only one bit wide // the value of each channel is output serially one bit at a time // the value of each channel takes 255 ticks to output competely bit_cnt--; if (bit_cnt =3D=3D 0) { // don't calculate bit_mask from bit_pos in the ISR as // as variable shifts are expensive and unnecessary in // this case if ((bit_mask & 0x80) =3D=3D 0) { bit_mask <<=3D 1; bit_pos++; } else { bit_mask =3D 1; bit_pos =3D 0; } bit_cnt =3D bit_mask; // I'm assuming the output pins are defined as a group at compile time BAM_PORT =3D BAM_buff[bit_pos]; } } Regards Sergio Masci --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .