Происходит бесконечно долгий опрос вывода PD6, если 1 то инкрементируетя counter1, иначе counter0. Подтяжка на PD6 есть. Каждые 2 сек значение выводится через usart.
Даже если все время на PD6 стоит 1 значение counter1 периодически меняется сильно (в старших разрядах), в чем может быть проблема?
Код
#include <avr/io.h>
#include <avr/interrupt.h>
#define check_num_pin (1<<PIND6)
#define check_pin PIND
unsigned long counter1;
unsigned long counter0;
void USART_Init() // Initializing USART
{
UBRR0 = 0x33;
/* Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 1stop bit */
UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);
}
void USART_Transmit( char data )
{
/* Wait for empty transmit buffer */
while ( !(UCSR0A & (1<<UDRE0)) );
/* Put data into buffer, sends the data */
UDR0 = data;
}
void TIMER2_Init() // Initializing TIMER1
{
ASSR=(1<<AS2);
TCCR2A=(1<<WGM21);
TCCR2B=(1<<CS22)|(1<<CS21)|(1<<CS20);
OCR2A=0x40;
TIMSK2=(1<<OCIE2A);
}
int main()
{
counter1=0;
counter0=0;
TIMER2_Init();
USART_Init();
PORTD=(1<<PORTD6);
sei();
while(1)
{
if (check_num_pin & check_pin)
{
counter1++;
}
else
{
counter0++;
}
}
}
ISR(TIMER2_COMPA_vect)
{
TCCR2B=0;
USART_Transmit(counter1);
USART_Transmit(counter1>>8);
USART_Transmit(counter1>>16);
USART_Transmit(counter1>>24);
counter1=0;
counter0=0;
TCCR2B=(1<<CS22)|(1<<CS21)|(1<<CS20);
}
#include <avr/interrupt.h>
#define check_num_pin (1<<PIND6)
#define check_pin PIND
unsigned long counter1;
unsigned long counter0;
void USART_Init() // Initializing USART
{
UBRR0 = 0x33;
/* Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 1stop bit */
UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);
}
void USART_Transmit( char data )
{
/* Wait for empty transmit buffer */
while ( !(UCSR0A & (1<<UDRE0)) );
/* Put data into buffer, sends the data */
UDR0 = data;
}
void TIMER2_Init() // Initializing TIMER1
{
ASSR=(1<<AS2);
TCCR2A=(1<<WGM21);
TCCR2B=(1<<CS22)|(1<<CS21)|(1<<CS20);
OCR2A=0x40;
TIMSK2=(1<<OCIE2A);
}
int main()
{
counter1=0;
counter0=0;
TIMER2_Init();
USART_Init();
PORTD=(1<<PORTD6);
sei();
while(1)
{
if (check_num_pin & check_pin)
{
counter1++;
}
else
{
counter0++;
}
}
}
ISR(TIMER2_COMPA_vect)
{
TCCR2B=0;
USART_Transmit(counter1);
USART_Transmit(counter1>>8);
USART_Transmit(counter1>>16);
USART_Transmit(counter1>>24);
counter1=0;
counter0=0;
TCCR2B=(1<<CS22)|(1<<CS21)|(1<<CS20);
}