Maker Pro
Maker Pro

sending data to usart using atmel studio avr gcc

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++;}

}
 

Harald Kapp

Moderator
Moderator
The issue seems to be here
data=getchar();
As far as I understand you want to type on the PC's keyboard, so instead of getchar() you nee dto rad from the USART using USART_receive().
On the PC side you need to attach to the serial port e.g. via a terminal program to connect to the AVR's UART, then type 'a'.
 
The issue seems to be here

As far as I understand you want to type on the PC's keyboard, so instead of getchar() you nee dto rad from the USART using USART_receive().
On the PC side you need to attach to the serial port e.g. via a terminal program to connect to the AVR's UART, then type 'a'.
Thanks alot it worked...
But I have a confusion..I want to initialize my esp8266 wifi module...whose code I had worked on arduino but now since em New to avr so don't know why my code is giving me errors...kindly help me with that..
 
This is my code...for using my wifi module as an access point..
#include <avr/io.h>
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>

#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)
#define DEBUG true
#define String espmode
#define espmode "3"
#define SSid "KFRL-4"
#define password "RFID5678"

void Esp_init(void);
unsigned char Esp_receive(void);
void Esp_send( unsigned char data);
void Esp_putstring(char* StringPtr);
void wifiSetup(String,String,String);
void sendData(String,const,boolean);

char String[]="Hello world";
unsigned char data;

int main(void)
{
Esp_init();
while(1)
{
wifiSetup(String espmode,SSid,password);
if (Esp_receive()!=0X00)
{
_delay_ms(1000);
int ConnectionId=Esp_receive()-48;
Esp_putstring(String);
Esp_send(data);
}
}
return 0;
}
void sendData(String Command,const int timeout,boolean debug)
{
String response="";
Esp_receive(Command);
long int time=millis();
while(time+timeout)>millis;
{
while(Esp_receive()!=0X00);
{
char c=Esp_receive();
response+=c;
}
}
if(debug)
{
Esp_putstring(response);
}
return response;
}
void wifiSetup(String mode,String SSid,String pass)
{
sendData("AT\r\n",2000,DEBUG); //reset mode
String mode0= "AT+CWMODE=";
mode0 += mode;
mode0 += "\r\n";
sendData(mode0,2000,DEBUG); // configure as access point
String ssidset="AT+CWJAP=\"";
ssidset += SSid ;
ssidset += "\",\"" ;
ssidset += pass ;
ssidset += "\"" ;
ssidset += "\r\n" ;
sendData(ssidset,5000,DEBUG);
return;

}

void Esp_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 Esp_receive(void){

while(!(UCSR0A & (1<<RXC0)));//Wait until data is received
return UDR0; // Read the data from the RX buffer

}

void Esp_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 Esp_putstring(char* StringPtr){

while(*StringPtr != 0x00){
Esp_send(*StringPtr);
StringPtr++;}

}
 

Harald Kapp

Moderator
Moderator
A string is a series of charactes, so simply send one character after the other until you reach the end of the string. Wait for the UART to finish transmission of a character before you send the next one.
 

Harald Kapp

Moderator
Moderator
The routine is already there in your code:
Code:
void Esp_putstring(char* StringPtr){

while(*StringPtr != 0x00){
Esp_send(*StringPtr);
StringPtr++;}

}
 
Top