On 5/26/05, Dave Turner wrote: > Hi again guys, > > I am currently working on a software PWM. I got it to work fine with > 1 pin, and now I'm extending it to 8 pins. I am using a regularly > incremented PWMtimer, and 8 GPRs, called PWMlevel 1 to 8. Inside the > PWMupdate function, I use a loop, and indirect addressing to access > each pwmlevel, and check if it's higher or lower than pwmtimer, to > decide when to turn it off. Therefore, the pwmlevel decides on > average, how high the pin is. > > All the loop code works just fine, in checking each pwmlevel, but then > I come to set or clear each bit on portb. I need a way to set or > clear a particular bit, depending on a variable, i.e. if the variable > is 4, I want to set/clear bit 4. I cannot think of a way to access a > bit, specified on the fly. The only method I could think of is to use > the variable as a loop counter, and in each loop, bit shift d'1' to > the left, and then move this to portb. Unfortunately, this would > disturb all the other bits in porta. > > Thanks in advance. Something like this? (untested code!) ; Be careful where this goes in the program space -- the addwf must clear the carry bit... ; Uses one scratch file, named scratch set_bit macro ; bit is in 'w' 0..7 clrf scratch bsf scratch,0 addwf PCL,f rrf scratch,f ; 0100 0000 rrf scratch,f ; 0010 0000 rrf scratch,f ; 0001 0000 rrf scratch,f ; 0000 1000 rrf scratch,f ; 0000 0100 rrf scratch,f ; 0000 0010 rrf scratch,f ; 0000 0001 movfw scratch iorwf PORTA,f endm set_bit macro ; bit is in 'w' 0..7 clrf scratch bsf scratch,0 addwf PCL,f rrf scratch,f ; 0100 0000 rrf scratch,f ; 0010 0000 rrf scratch,f ; 0001 0000 rrf scratch,f ; 0000 1000 rrf scratch,f ; 0000 0100 rrf scratch,f ; 0000 0010 rrf scratch,f ; 0000 0001 comf scratch,w andwf PORTA,f endm -- You think that it is a secret, but it never has been one. - fortune cookie -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist