Hello..I am trying to send data to usart so that when i type a character 'a' to it it should turn the led on and if anything else is typed it should turn it off..I have written the code but i think i am missing something..kindly help...
My code is:
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
// the USART circuitry within the AVR really likes clock frequencies divisible by 1.8432MHz. If your clock source is not divisible by 1.8432 you will have a percentage of error which will result in unstable communications.
//using formula for setting the ubrr value
//as long as the error rate is less 0.5% it will result in solid communication
#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_putstring(char* StringPtr);
char String[]=""; //String[] is in fact an array but when we put the text between the " " symbols the compiler threats it as a String and automatically puts the null termination character in the end of the text
unsigned char data,a;
int main(void){
//DDRD = 0b00000010;
//PORTD=0x00;
DDRB |=(1<<PORTB5);
PORTB=0x00;
USART_init(); //Call the USART initialization code
while(1){ //Infinite loop
//USART_putstring(String); //Pass the string to the USART_putstring function and sends it over the serial
//USART_send(data);
_delay_ms(3000); //Delay for 3 seconds so it will re-send the string every 5 seconds
data=getchar();
USART_send(data);
if (data=='a')
{
PORTB|=(1<<PORTB5);
_delay_ms(500);
}
else
{PORTB&=~(1<<PORTB5);
}
}
return 0;
}
void USART_init(void){
//UBRRn is a baudrate register
//UMSELn bit in USART Control and Status Register C (UCSRnC) selects
//between asynchronous and synchronous operation
UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8); // Baud Rate Register upper 4 bits
UBRR0L = (uint8_t)(BAUD_PRESCALLER); // Baud Rate Register lower 4 bits
//enable the transmission and receive functionality independently.
//Both the RXENn bit (Receive Enable) and the TXEN(TXENn) bit (Transmitter Enable)
//are both located in the B Register (UCSRnB)
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
//All of our configuration options are located in Register C
UCSR0C = (3<<UCSZ00); //Set frame
}
unsigned char USART_receive(void){
while(!(UCSR0A & (1<<RXC0)));//Wait until data is received
return UDR0; // Read the data from the RX buffer
}
void USART_send( unsigned char data) //write byte to serial port
{
while(!(UCSR0A & (1<<UDRE0)));// Wait until buffer is empty
UDR0 = data;// Send the data to the TX buffer
}
void USART_putstring(char* StringPtr){
while(*StringPtr != 0x00){
USART_send(*StringPtr);
StringPtr++;}
}
My code is:
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
// the USART circuitry within the AVR really likes clock frequencies divisible by 1.8432MHz. If your clock source is not divisible by 1.8432 you will have a percentage of error which will result in unstable communications.
//using formula for setting the ubrr value
//as long as the error rate is less 0.5% it will result in solid communication
#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_putstring(char* StringPtr);
char String[]=""; //String[] is in fact an array but when we put the text between the " " symbols the compiler threats it as a String and automatically puts the null termination character in the end of the text
unsigned char data,a;
int main(void){
//DDRD = 0b00000010;
//PORTD=0x00;
DDRB |=(1<<PORTB5);
PORTB=0x00;
USART_init(); //Call the USART initialization code
while(1){ //Infinite loop
//USART_putstring(String); //Pass the string to the USART_putstring function and sends it over the serial
//USART_send(data);
_delay_ms(3000); //Delay for 3 seconds so it will re-send the string every 5 seconds
data=getchar();
USART_send(data);
if (data=='a')
{
PORTB|=(1<<PORTB5);
_delay_ms(500);
}
else
{PORTB&=~(1<<PORTB5);
}
}
return 0;
}
void USART_init(void){
//UBRRn is a baudrate register
//UMSELn bit in USART Control and Status Register C (UCSRnC) selects
//between asynchronous and synchronous operation
UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8); // Baud Rate Register upper 4 bits
UBRR0L = (uint8_t)(BAUD_PRESCALLER); // Baud Rate Register lower 4 bits
//enable the transmission and receive functionality independently.
//Both the RXENn bit (Receive Enable) and the TXEN(TXENn) bit (Transmitter Enable)
//are both located in the B Register (UCSRnB)
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
//All of our configuration options are located in Register C
UCSR0C = (3<<UCSZ00); //Set frame
}
unsigned char USART_receive(void){
while(!(UCSR0A & (1<<RXC0)));//Wait until data is received
return UDR0; // Read the data from the RX buffer
}
void USART_send( unsigned char data) //write byte to serial port
{
while(!(UCSR0A & (1<<UDRE0)));// Wait until buffer is empty
UDR0 = data;// Send the data to the TX buffer
}
void USART_putstring(char* StringPtr){
while(*StringPtr != 0x00){
USART_send(*StringPtr);
StringPtr++;}
}