Hello all,
I have been looking all over this forum and the arduino website. I still cannot figure it out. I am a looking for a way to generate a PWM signal of about 20 to 30 kHz. Can anybody point me to where I might find a good explanation. Or perhaps help directly.
TCCR1B is the name of an internal register, a sort of variable that is already set up.
TCCR1B & 0b11111000 - is a bitwise operation between the value in the TCCR1B register and the binary value that follows it. Basically it sets all those bit in the TCCR1B to zero that have a zero in the binary number. In other words it sets the three least significant bits to zero.
| ; - the | is a bitwise or operation that adds in the bits in to the TCCR1B register. You replace with the number you want to use. It will be a number between 0 and 7.
void setup()
{
TCCR1B = 0x01;
TCCR1B = TCCR1B & 0b11111000 | 7;
OCR2B = 159; // I got this line from another forum, not sure what it means
pinMode(3, OUTPUT); // enable the PWM output (you now have a PWM signal on digital pin 3)
}
void loop()
{
OCR2B = 60; // set the PWM to 50% duty cycle
The code seems to work well for now. Although upon imperical testing, I noticed the frequency dropped off from what I orginally set it to. Perhaps Its my mutli meter. I will keep working on it.