void setup(void)
{
...
TCCR1A = 0×00; // sets timer control bits to PWM Phase and Frequency Correct mode
TCCR1B = 0×12; // sets timer control bits to Prescaler N = 8
ICR1 = 0×07d0; // Upper Timer Limit = 2000 (in hex) equals 2ms
}
//after which you can analogWrite to pins 9 and 10
3 Responses to “The missing 3 lines of code”
Leave a Reply
paul
Why would you use phase & freq correct mode but not enable waveforms on OC1A or OC1B, which connect to pins 9 & 10, that presumably you’re low pass filtering to get simulated analog outputs, or using to switch mosfets to drive power into something? The upper 4 bits of TCCR1A are all zero, which disconnects OC1A & 0C1B.
feurig
The majority of the arduino sample code for driving servos uses manual timing which is extremely sensitive to the logic and the other things you are trying to do. The hardware based timing makes it the other code simpler. On sunday I did not bring the laptop that I did the origional code on and could not find this code int time to have the bot work.
feurig
Paul,
To answer your initial question the pin is actually connected by the analogWrite(function). The point of hiding all of this of course is so that most people don’t have to dig that deeply. That was the origional point of my presentation.
When the arduino works you should be able to bring in hardware and wire and program it just before showtime.
But as I said sometimes we fail.
Don.
———————————— Snippet from wiring_analog.c —————————————–
// Right now, PWM output only works on the pins with // hardware support. These are defined in the appropriate // pins_*.c file. For the rest of the pins, we default // to digital output. void analogWrite(uint8_t pin, int val) { // We need to make sure the PWM output is enabled for those pins // that support it, as we turn it off when digitally reading or // writing with them. Also, make sure the pin is in output mode // for consistenty with Wiring, which doesn't require a pinMode // call for the analog output pins. pinMode(pin, OUTPUT); if (digitalPinToTimer(pin) == TIMER1A) { // connect pwm to pin on timer 1, channel A sbi(TCCR1A, COM1A1); // set pwm duty OCR1A = val; } else if (digitalPinToTimer(pin) == TIMER1B) { // connect pwm to pin on timer 1, channel B sbi(TCCR1A, COM1B1); // set pwm duty OCR1B = val; #if defined(__AVR_ATmega168__) } else if (digitalPinToTimer(pin) == TIMER0A) { // connect pwm to pin on timer 0, channel A sbi(TCCR0A, COM0A1); // set pwm duty OCR0A = val; } else if (digitalPinToTimer(pin) == TIMER0B) { // connect pwm to pin on timer 0, channel B sbi(TCCR0A, COM0B1); // set pwm duty OCR0B = val; } else if (digitalPinToTimer(pin) == TIMER2A) { // connect pwm to pin on timer 2, channel A sbi(TCCR2A, COM2A1); // set pwm duty OCR2A = val; } else if (digitalPinToTimer(pin) == TIMER2B) { // connect pwm to pin on timer 2, channel B sbi(TCCR2A, COM2B1); // set pwm duty OCR2B = val; #else } else if (digitalPinToTimer(pin) == TIMER2) { // connect pwm to pin on timer 2, channel B sbi(TCCR2, COM21); // set pwm duty OCR2 = val; #endif } else if (val < 128) digitalWrite(pin, LOW); else digitalWrite(pin, HIGH); }