#include <avr/io.h> #define F_CPU 8000000 #define BAUD 9600 #define RATE ((F_CPU/16/BAUD)-1) #include <util/delay.h> #include <avr/interrupt.h> #define LED_ON PORTD |= (1<<PIND6); #define LED_OFF6 PORTD &= ~(1<<PIND6); #define LED_OFF5 PORTD &= ~(1<<PIND5); #define toggel_led6 PORTD ^=(1<<PD6); #define toggel_led5 PORTD ^=(1<<PD5); volatile int a=0,b=0,c=0,m=0,n=0; // #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) // #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) #define RX_BUFFER_SIZE 128 char rxBuffer[RX_BUFFER_SIZE]; uint8_t rxReadPos=0; uint8_t rxWritePos=0; char getChar(void); char peekChar(void); void T2delay() { while(TIFR&0x01==0); TCCR2=0; } void T2_init() { TCCR2 = (1<<CS22)|(1<<CS21)|(1<<CS20); TCNT2 = 0xB2; } int main(void) { DDRD |=(1<<PIND6)|(1<<PIND5); TIMSK=(1<<TOIE2); T2_init(); UBRRH |= (RATE>>8); UBRRL |= RATE; UCSRB |= (1<<RXEN)|(1<<RXCIE); UCSRC |= (1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1); sei(); while (1) { char x = getChar(); } } char peekChar(void) { char ret='\0'; if (rxReadPos != rxWritePos) { ret=rxBuffer[rxReadPos]; } return ret; } char getChar(void) { char ret='\0'; if (rxReadPos != rxWritePos) { ret=rxBuffer[rxReadPos]; rxReadPos++; if (rxReadPos>=RX_BUFFER_SIZE) { rxReadPos=0; } } return ret; } ISR(USART_RXC_vect) { rxBuffer[rxWritePos]=UDR; rxWritePos++; if (rxWritePos>=RX_BUFFER_SIZE) { rxWritePos=0; } }
i have made this code for uart receive but i want to receive a string through uart and store in a variable then compare the string to execute any function.if it possible to you then please help me how do i make this work i want.