Hello All,
I am trying to write a code for UART interrupt.
SO first i am write a code on polling bases. this will work fine if i did not use setup or loop function.
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)
//Declaration of our functions
void USART_init(void);
unsigned char USART_receive(void);
void USART_send( unsigned char data);
void USART_Print(char* StringPtr);
char String[]="";
char String1[]="hello";
unsigned char c = 0;
int main(void){
USART_init();
while(1){
USART_receive();
USART_Print(String);
}
return 0;
}
void USART_init(void){
UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);
UBRR0L = (uint8_t)(BAUD_PRESCALLER);
UCSR0B = (1<<RXEN0)|(1<<TXEN0)| (1<<RXCIE0);
//UCSR0C = (3<<UCSZ00);
UCSR0C = ((1<<UCPOL0) | (1<<UCSZ01) | (1<<UCSZ00));
}
unsigned char USART_receive(void){
int i = 0;
while(!(UCSR0A & (1<<RXC0)));
String[i] = UDR0;
i++;
return UDR0;
}
void USART_send( unsigned char data){
while(!(UCSR0A & (1<<UDRE0)));
UDR0 = data;
}
void USART_Print(char* StringPtr){
while(*StringPtr != 0x00){
USART_send(*StringPtr);
StringPtr++;
}
}
/*ISR(USART0_RX_vect)
{
USART_Print(String1);
}
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)
char String[]="";
void setup()
{
UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);
UBRR0L = (uint8_t)(BAUD_PRESCALLER);
UCSR0B = (1<<RXEN0)|(1<<TXEN0)| (1<<RXCIE0);
//UCSR0C = (3<<UCSZ00);
UCSR0C = ((1<<UCPOL0) | (1<<UCSZ01) | (1<<UCSZ00));
}
void loop()
{
USART_receive();
USART_Print(String);
}
unsigned char USART_receive(void){
int i = 0;
while(!(UCSR0A & (1<<RXC0)));
String[i] = UDR0;
i++;
return UDR0;
}
void USART_send( unsigned char data){
while(!(UCSR0A & (1<<UDRE0)));
UDR0 = data;
}
void USART_Print(char* StringPtr){
while(*StringPtr != 0x00){
USART_send(*StringPtr);
StringPtr++;
}
}
anyone tell me why this second code is not working? any specific reasons ?
And how to make this code workable? (with interrupt or with ISR)