I've been trying to drive a servo off the ATtiny85 using interrupts and I'm not having much luck. I've distilled the code down the the absolute minimum, I believe that the code should produce an asymmetric waveform but it doesn't, something strange is happening and the OCR0A registry doesn't seem to react to new values that I assign in the switch cases.... Help!
int state =1;
void setup() {
pinMode(0, OUTPUT);
// Enable Output Compare Match Interrupt
TIMSK |= (1 << OCIE0A);
//set the compare value to any number larger than 0
OCR0A = 255;
//reset the Timer Counter Control Register to its reset value
TCCR0B = 0;
//set counter0 prescaler to 64
//our FCLK is 8mhz so this makes each timer tick be 8 microseconds long
TCCR0B &= ~(1<< CS02); //clear
TCCR0B |= (1<< CS01); //set
TCCR0B |= (1<< CS00); //set
}
void loop() {
//nada
}
ISR(TIMER0_COMPA_vect)
{
switch (state)
{
case 1:
PORTB|= B00000001; //set pin high
TCNT0 = 0;//reset the counter to 0
OCR0A = 200;
//update our state
state = 2;
break;
case 2:
PORTB &= ~B00000001; //set pin low
//set the compare value to the additional amount of timer ticks the pulse should last
//update our state
OCR0A = 50;
TCNT0 = 0;//reset the counter to 0
state = 1;
break;
}//end switch
}
avrdude: verification error, first mismatch at byte 0x0180
0x1f != 0x0f
avrdude: verification error; content mismatch
Also, I'm trying to change the compare register each time I enter the ISR so that I can replicate a servo drive waveform, my original clip of code has 200 and 50 assigned alternatively to the OCR0A register but the pin doesn't seem to be spending the prescribed time in those states.
I do have a O'Scope to see how long the pin spends up and down, that's how I go to the point where I know the output wave form isn't asymmetric as I'd expect, its completely symmetric, transitions are on a time that lines up with the smallest value I put in for the OCR0A registry leading me to believe that I'm not changing the OCR0A in the ISR.
int state =1;
void setup() {
pinMode(0, OUTPUT);
// Enable Output Compare Match Interrupt
TIMSK |= (1 << OCIE0A);
TCCR0A |=(1<<WGM01); //Start timer 1 in CTC mode Table 11.5
//set the compare value to any number larger than 0
OCR0A = 255;
//reset the Timer Counter Control Register to its reset value
TCCR0B = 0;
//set counter0 prescaler to 64
//our FCLK is 8mhz so this makes each timer tick be 8 microseconds long
TCCR0B &= ~(1<< CS02); //clear
TCCR0B |= (1<< CS01); //set
TCCR0B |= (1<< CS00); //set
}
void loop() {
}
ISR(TIMER0_COMPA_vect)
{
switch (state)
{
case 1:
PORTB|= B00000001; //set pin high
TCNT0 = 0;//reset the counter to 0
OCR0A = 200;
//update our state
state = 2;
break;
case 2:
PORTB &= ~B00000001; //set pin low
//set the compare value to the additional amount of timer ticks the pulse should last
//update our state
OCR0A = 50;
TCNT0 = 0;//reset the counter to 0
state = 1;
break;
}//end switch
}
That is strange.
When I try your latest sketch I get a assymetric waveform (about 1.04 High and 2.04 low).
The only explanation I can think of is that the tiny is autoresetting.
The usual advise to solve this is to connect a 10kOhm resistor from reset to Vcc, and a 0.1 uF capacitor from ground to Vcc as close to the chip as possible.
BTW if it is a servo signal to control a standard rc servo that you are trying to make, then
there are servo libraris for the t85.
So you're not seeing the pulse change duration as the main loop changes the servopos value either?
I've tried to get the servo library referred to below to work but had no luck, so I pulled apart the library and tried to reverse engineer it... and that's where I am now.
OK, I tried to get that library to work and I get this error:
In file included from TinyServoExample.ino:39:0:
C:\Users\Bill\Documents\Arduino\libraries\TinyServo/TinyServo.h:77:4: error: #error TinyServo library does not support this processor.
#error TinyServo library does not support this processor.
I can't say I understand why, it's pretty straightforward that the ATtiny85 is supported, I look through the logic tree in TinyServo.h and I think that the 85 should be covered.
I'm running at 8 MHz, in which case it's recommended that I force the timer to timer 1 but I'm not really sure how to do that, I found the file core_build_options.h but wasn't sure what to change. I tried 1 MHz but that didn't change the error...
I got the same "processor not supported error" with your code. I'm guessing that there must be something in the 1.63 version on the IDE I'm using that is difference in the naming of processors.