On Thu, 27 Aug 2015, Josh Koffman wrote: > Hi all, > > Amongst all the other things on my plate right now, I'd like to make a > small library to do Bit Angle Modulation on some of my projects. I > made one years ago in assembly, but now I'd like to do it in XC8. > > For reference, there are some great posts on BAM on the PicBasic forums h= ere: > > http://www.picbasic.co.uk/forum/showthread.php?p=3D45597 > http://www.picbasic.co.uk/forum/showthread.php?t=3D10564 > > Because the PIC will be doing many things, I am considering using a > high priority interrupt for the BAM, but keeping my high priority ISR > as small as possible (which is probably a good idea regardless). To > accomplish this, I want to create the 8 "slices" that I will output in > my main line, so that all the BAM ISR has to do is take that variable > and put it on the port. > > My question is, what would be the best way to handle this in C? When I > did it in assembly I used a rotate (rotate right if I remember > correctly), then read the carry bit. But then I was also doing it at > the moment of outputting, so there was no intermediate stored > variable. > > My current thought is to define a typedef that lets me access the > individual bits, then a bunch of if/else statements that check the bit > of the source value, and if it's a 1 set the bit of the corresponding > "slice" variable. This would result in a lot of if statements though, > so I'm wondering if there's a slicker way to do this. > > I'm trying to avoid dropping into ASM to do this for a few reasons. > > Any thoughts? > > Thanks! > > Josh >From what I've read from the description of BAM, if I were doing this in C= =20 I would do it like this: // globals unsigned char bit_cnt, bit_mask, value_out; void set_BAM(unsigned char val) { // disable interrupts - how do you do this in XC8? bit_cnt =3D 1; bit_mask =3D 0x80; value_out =3D val; // enable interrupts } void service_BAM() { bit_cnt--; if (bit_cnt =3D=3D 0) { if ((bit_mask & 0x80) =3D=3D 0) { bit_mask <<=3D 1; } else { bit_mask =3D 1; } bit_cnt =3D bit_mask; // I'm assuming the output pin is defined at compile time if ((value_out & bit_mask) =3D=3D 0) { // reset output pin } else { // set output pin } } } 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 .