Sorry, it still the same error
static uint8_t pulse_flag = 0;
is for AVR GCC isn't it?
- - - Updated - - -
Ahaa I found it, I must add:
#include <stdint.h>
and now it works.
Thank you very much alexan
Any comments/suggestions? Which part should I fix ?
I use codevisionavr and that's why I need to include it manually, but it already works anyway, so there is no more problem in this part.I don't need to include #include <stdint.h> manually, just by using #include <avr/io.h> it gets included and should work (avr studio).
Anyway the alternative is to use unsigned char instead of uint8_t
I'm not sure with this one. I just use codevision to generate it (10-bit adc), ADC Clock frequency: 750.000 kHz, qrystal value 12MHzWhat is the rate of ADC sampling?
If you get a new adc result while you are in the middle of the pulse then you may miss the pulse return to the default state.
suppose that the adc gives a x of 40 , when you reach 40 the pin is set to low , then you may get a new reading and x equals 30 , now when you reenter the interrupt x+1 is not 41 but 31 so the pulse never returns to the default state.
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
static uint8_t pulse_flag = 0;
// Reinitializa timer
TCNT0 = 0xB5;
if(PORTC.1 && (adc1 <= 500))
{
if(x >= 1)
{
x = x - 1;
}
}
if(PORTC.1 && (adc1 >= 600))
{
if(x <= 180)
{
x = x + 1;
}
}
if(PORTC.0 && (adc2 <= 300))
{
if(y >= 1)
{
y = y - 1;
}
}
if(PORTC.0 && (adc2 >= 400))
{
if(y <= 180)
{
y = y + 1;
}
}
counter1++;
if(counter1 == x)
{
PORTC.1 = 0;
}
else
if(counter1 == x + 1)
{
PORTC.1 = 1;
pulse_flag++;
}
else
if(counter1 == y)
{
PORTC.0 = 0;
}
else
if(counter1 == y + 1)
{
PORTC.0 = 1;
pulse_flag++;
}
if(pulse_flag == 2) // only stop timer after both pulses have completed
{
pulse_flag = 0; // clear flag
TCCR0 = 0; // stop thet timer
counter1 = 0; // clear counter
}
}
void main(void)
{
while(1)
{
adc1 = read_adc(1);
adc2 = read_adc(2);
}
}
while(1)
{
adc1 = read_adc(1);
adc2 = read_adc(2);
}
volatile unsigned int adc1;
volatile unsigned int adc2;
PORTC=0xFF; // active low
DDRC=0xFF;
Could be,This can be another cause of problem , these functions can be interrupted an any point and get wrong values.
while(1)
{
adc1 =600;
adc2 = 300;
delay_ms(1000);
adc1 =400;
adc2 = 500;
delay_ms(1000);
}
if(PORTC.1 && (adc1 <= 500))
if((PORTC.1==1) && (adc1 <= 500))
if((PORTC & 0x2) && (adc1 <= 500)) // for PORTC.1
if((PORTC & 0x1) && (adc1 <= 500)) // for PORTC.0
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
static uint8_t pulse_flag = 0;
// Reinitializa timer
TCNT0 = 0xB5;
if((PORTC & 0x2) && (adc1 <= 500))
{
if(x >= 1)
{
x = x - 1;
}
}
if((PORTC & 0x2) && (adc1 >= 600))
{
if(x <= 180)
{
x = x + 1;
}
}
if((PORTC & 0x1) && (adc1 <= 300))
{
if(y >= 1)
{
y = y - 1;
}
}
if((PORTC & 0x1) && (adc1 >= 400))
{
if(y <= 180)
{
y = y + 1;
}
}
counter1++;
if(counter1 == x)
{
PORTC.1 = 0;
}
else
if(counter1 == x + 1)
{
PORTC.1 = 1;
pulse_flag++;
}
else
if(counter1 == y)
{
PORTC.0 = 0;
}
else
if(counter1 == y + 1)
{
PORTC.0 = 1;
pulse_flag++;
}
if(pulse_flag == 2) // only stop timer after both pulses have completed
{
pulse_flag = 0; // clear flag
TCCR0 = 0; // stop thet timer
counter1 = 0; // clear counter
}
}
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
static uint8_t pulse_flag = 0;
// Reinitializa timer
TCNT0 = 0xB5;
if((PORTC & 0x2) && (adc1 <= 500))
{
if(x >= 1)
{
x = x - 1;
}
}
if((PORTC & 0x2) && (adc1 >= 600))
{
if(x <= 180)
{
x = x + 1;
}
}
if((PORTC & 0x1) && (adc1 <= 300))
{
if(y >= 1)
{
b = y - 1;
}
}
if((PORTC & 0x1) && (adc1 >= 400))
{
if(y <= 180)
{
b = y + 1;
}
}
counter1++;
if(counter1 == x)
{
PORTC.1 = 0;
}
else
if(counter1 == x + 1)
{
PORTC.1 = 1;
pulse_flag++;
}
else
if(counter1 == b)
{
PORTC.0 = 0;
}
else
if(counter1 == b + 1)
{
PORTC.0 = 1;
pulse_flag++;
}
if(pulse_flag == 2) // only stop timer after both pulses have completed
{
pulse_flag = 0; // clear flag
TCCR0 = 0; // stop thet timer
counter1 = 0; // clear counter
}
}
if(counter1 == x)
{
PORTC.1 = 0;
}
else
if(counter1 == x + 1)
{
PORTC.1 = 1;
pulse_flag++;
}
else
if(counter1 == y)
{
PORTC.0 = 0;
}
else
if(counter1 == y + 1)
{
PORTC.0 = 1;
pulse_flag++;
}
if(counter1 == x)
{
PORTC.1 = 0;
}
else
if(counter1 == x + 1)
{
PORTC.1 = 1;
pulse_flag++;
}
if(counter1 == y)
{
PORTC.0 = 0;
}
else
if(counter1 == y + 1)
{
PORTC.0 = 1;
pulse_flag++;
}
if(adc1 >= 600)
{
if(x<=180)
{
x = x + 1;
}
}
if(adc2 <= 300)
{
if(y>=1)
{
y = y - 1;
}
}
if(adc2 >= 400)
{
if(y<=180)
{
y = y + 1;
}
}
// Local variables initialization
static unsigned int x = 0;
static unsigned int y = 0;
static unsigned int z = 0;
static uint8_t pulse_flag = 0;
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?