Buy an Axon, Axon II, or Axon Mote and build a great robot, while helping to support SoR.
0 Members and 1 Guest are viewing this topic.
void analogWrite(uint8_t pin, int val){// connect pwm to pin on timer 0, channel A sbi(TCCR0A, COM0A1); // set pwm duty OCR0A = val;
sbi(TCCR0A, WGM01);sbi(TCCR0A, WGM00);sbi(TCCR0B, CS01);sbi(TCCR0B, CS00);sbi(TIMSK, TOIE0);
SIGNAL(TIM0_OVF_vect){ // copy these to local variables so they can be stored in registers // (volatile variables must be read from memory on every access) unsigned long m = timer0_millis; unsigned char f = timer0_fract; m += MILLIS_INC; f += FRACT_INC; if (f >= FRACT_MAX) { f -= FRACT_MAX; m += 1; } timer0_fract = f; timer0_millis = m; timer0_overflow_count++;}
if(!bitRead(TCCR0A, COM0A1)){ cbi(DDRB, 0); sbi(TCCR0A, COM0A1); TCNT0 = 0; delay(10); sbi(DDRB, 0); }// set pwm dutyOCR0A = val;
It may well be that the timer overflow interrupt request (in TIFR) is already set so as soon as you enable the interrupt then the interrupt happens.Try clearing the TIFR bit (by writing a 1 to it) before enabling the interrupt.
The prescaler is free running, i.e. it operates independently of the Clock Select logic ofTimer/Counter0. Since the prescaler is not affected by the timer/counter’s clock select, the state of the prescaler will have implications for situations where a prescaled clock is used. One exampleof a prescaling artifact is when the timer/counter is enabled and clocked by the prescaler (6 >CS0[2:0] > 1). The number of system clock cycles from when the timer is enabled to the firstcount occurs can be from 1 to N+1 system clock cycles, where N equals the prescaler divisor (8,64, 256, or 1024).It is possible to use the Prescaler Reset for synchronizing the Timer/Counter to programexecution.
if(!bitRead(TCCR0A, COM0A1)){ sbi(GTCCR, TSM); sbi(GTCCR, PSR0); sbi(TCCR0A, COM0A1); OCR0A = val; cbi(GTCCR, TSM); } // set pwm duty else OCR0A = val;