Thursday, February 28, 2019

USART Serial Communication


This is a procedure for serial communication between laptop/pc and avr (atmega2560)
using python code information will be transmitted from the pc and will be received by atmega2560.
Connect atmega2560 with pc via USB cable. Open device manager and check which COM port is working in your pc it will be COM3, COM4, like this. So edit in your python code with your com port.

Python code for pc(Transmission source)


import serial
#Can be Downloaded from this Link
#https://pypi.python.org/pypi/pyserial

#Global Variables
ser = 0

#Function to Initialize the Serial Port
def init_serial():        #Enter Your COM Port Number Here.
    global ser          #Must be declared in Each Function
    ser = serial.Serial()
    ser.baudrate = 9600
    ser.port = 'COM6'   #COM Port Name Start from 0
    
    #ser.port = '/dev/ttyUSB0' #If Using Linux

    #Specify the TimeOut in seconds, so that SerialPort
    #Doesn't hangs
    ser.timeout = 10
    ser.open()          #Opens SerialPort

    # print port open or closed
    if ser.isOpen():
        print 'Open: ' + ser.portstr
#Function Ends Here6  
        

#Call the Serial Initilization Function, Main Program Starts from here
init_serial()
temp = raw_input('Type what you want to send, hit enter:\r\n')
for p in temp:
    ser.write(p)        

   
   # bytes = ser.readline()  #Read from Serial Port
   # print 'You sent: ' + bytes      #Print What is Read from Port


* numpy should be imported. 
*COM port should be set.

Receiving transmitted information (atmega2560)


#define F_CPU 14745600
#include<avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>

unsigned char data; //to store received data from UDR1

void buzzer_pin_config (void)
{
 DDRC = DDRC | 0x08;  //Setting PORTC 3 as outpt
 PORTC = PORTC & 0xF7;  //Setting PORTC 3 logic low to turnoff buzzer
}

void motion_pin_config (void)
{
 DDRA = DDRA | 0x0F;
 PORTA = PORTA & 0xF0;
 DDRL = DDRL | 0x18;   //Setting PL3 and PL4 pins as output for PWM generation
 PORTL = PORTL | 0x18; //PL3 and PL4 pins are for velocity control using PWM.
}

//Function to initialize ports
void port_init()
{
 motion_pin_config();
 buzzer_pin_config();
}

void buzzer_on (void)
{
 unsigned char port_restore = 0;
 port_restore = PINC;
 port_restore = port_restore | 0x08;
 PORTC = port_restore;
}

void buzzer_off (void)
{
 unsigned char port_restore = 0;
 port_restore = PINC;
 port_restore = port_restore & 0xF7;
 PORTC = port_restore;
}

//Function To Initialize UART0
// desired baud rate:9600
// actual baud rate:9600 (error 0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
 UCSR0B = 0x00; //disable while setting baud rate
 UCSR0A = 0x00;
 UCSR0C = 0x06;
// UBRR0L = 0x47; //11059200 Hz
 UBRR0L = 0x5F; // 14745600 Hzset baud rate lo
 UBRR0H = 0x00; //set baud rate hi
 UCSR0B = 0x98;
}
unsigned char USART_Receive( void )
{
 /* Wait for data to be received */
 while ( !(UCSR0A & (1<<RXC0)) );
 char c1=UDR0;
 return c1;
}

unsigned char * UART1_Rx_Str()
{
 unsigned char string[1000], x, i = 0;

 //receive the characters until ENTER is pressed (ASCII for $ = 36)
 while((x = USART_Receive()) !=36 )
 {
  //and store the received characters into the array string[] one-by-one
  string[i++] = x;
 }

 //insert NULL to terminate the string
 string[i] = '\0';

 //return the received string
 return(string);
}
//Function To Initialize all The Devices
void init_devices()
{
 cli(); //Clears the global interrupts
 port_init();  //Initializes all the ports
 uart0_init(); //Initailize UART1 for serial communiaction
 sei();   //Enables the global interrupts
}

//Main Function
int main(void)
{
char st[100];
 init_devices();
//information recieved at avr can be print not lcd 
lcd_string(1,1,UART1_Rx_Str());
strcpy(st,UART1_Rx_Str());
//received information is stored in string "st"
}


###

*Python code will take a string as input and it will transmit serially.
*LCD functions are used so include its library else comment them.