ATMEL STUDIO 6.0 Programming:Serial Communication via USB RS-232


Please copy and Paste

/************************************************************************************
Written by: Vinod Desai,Sachitanand Malewar NEX Robotics Pvt. Ltd.and www.sureshQ.Blogspot.in
Edited by: e-Yantra team
AVR Studio Version 6

Date: 19th October 2012
 Application example: Robot control over serial port via USB-RS232 converter 
 www.sureshQ.Blogspot.in  (located on the ATMEGA260 microcontroller adaptor board)

 Concepts covered:  serial communication

 Serial Port used: UART2

 There are two components to the motion control:
 1. Direction control using pins PORTA0 to PORTA3
 2. Velocity control by PWM on pins PL3 and PL4 using OC5A and OC5B.

 In this experiment for the simplicity PL3 and PL4 are kept at logic 1.

 Pins for PWM are kept at logic 1.
  
 Connection Details:  
 
  Motion control: L-1---->PA0; L-2---->PA1;
    R-1---->PA2; R-2---->PA3;
    PL3 (OC5A) ----> Logic 1; PL4 (OC5B) ----> Logic 1; 


  Serial Communication: PORTD 2 --> RXD1 UART1 receive for RS232 serial communication
PORTD 3 --> TXD1 UART1 transmit for RS232 serial communication

PORTH 0 --> RXD2 UART 2 receive for USB - RS232 communication
PORTH 1 --> TXD2 UART 2 transmit for USB - RS232 communication

PORTE 0 --> RXD0 UART0 receive for ZigBee wireless communication
PORTE 1 --> TXD0 UART0 transmit for ZigBee wireless communication

PORTJ 0 --> RXD3 UART3 receive available on microcontroller expansion socket
PORTJ 1 --> TXD3 UART3 transmit available on microcontroller expansion socket

Serial communication baud rate: 9600bps
To control robot use number pad of the keyboard which is located on the right hand side of the keyboard.
Make sure that NUM lock is on.

Commands:
Keyboard Key   ASCII value Action
8 0x38 Forward
2 0x32 Backward
4 0x34 Left
6 0x36 Right
5 0x35 Stop
7 0x37 Buzzer on
9 0x39 Buzzer off

 Note: 

 1. Make sure that in the configuration options following settings are 
  done for proper operation of the code www.sureshQ.Blogspot.in

  Microcontroller: atmega2560
  Frequency: 14745600
  Optimization: -O0  (For more information read section: Selecting proper optimization 
  options below figure 2.22 in the Software Manual)

 2. Difference between the codes for RS232 serial, USB and wireless communication is only in the serial port number.
  Rest of the things are the same. 

 3. For USB communication check the Jumper 1 position on the ATMEGA2560 microcontroller adaptor board

 4. Auxiliary power can supply current up to 1 Ampere while Battery can supply current up to 
  2 Ampere. When both motors of the robot changes direction suddenly without stopping, 
it produces large current surge. When robot is powered by Auxiliary power which can supply
only 1 Ampere of current, sudden direction change in both the motors will cause current  www.sureshQ.Blogspot.in
surge which can reset the microcontroller because of sudden fall in voltage. 
It is a good practice to stop the motors for at least 0.5seconds before changing 
the direction. This will also increase the useable time of the fully charged battery.
the life of the motor.

*********************************************************************************/

/********************************************************************************


********************************************************************************/

#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 UART2
// desired baud rate:9600
// actual baud rate:9600 (error 0.0%)
// char size: 8 bit
// parity: Disabled
void uart2_init(void)
{
 UCSR2B = 0x00; //disable while setting baud rate
 UCSR2A = 0x00;
 UCSR2C = 0x06;
 UBRR2L = 0x5F; //set baud rate lo
 UBRR2H = 0x00; //set baud rate hi
 UCSR2B = 0x98;
}


SIGNAL(SIG_USART2_RECV) // ISR for receive complete interrupt
{
data = UDR2; //making copy of data  www.sureshQ.Blogspot.in from UDR2 in 'data' variable 

UDR2 = data; //echo data back to PC

if(data == 0x38) //ASCII value of 8
{
PORTA=0x06;  //forward
}

if(data == 0x32) //ASCII value of 2
{
PORTA=0x09; //back
}

if(data == 0x34) //ASCII value of 4
{
PORTA=0x05;  //left
}

if(data == 0x36) //ASCII value of 6
{
PORTA=0x0A; //right
}

if(data == 0x35) //ASCII value of 5
{
PORTA=0x00; //stop
}

if(data == 0x37) //ASCII value of 7
{
buzzer_on();
}

if(data == 0x39) //ASCII value of 9
{
buzzer_off();
}

}


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

//Main Function
int main(void)
{
init_devices();
while(1);
}


ATMEL STUDIO 6.0 Programming:Serial Communication via USB RS-232 ATMEL STUDIO 6.0 Programming:Serial Communication via USB RS-232 Reviewed by Suresh Bojja on 7/07/2016 05:47:00 AM Rating: 5

Subjects & Labs

AC (6) ADS (2) ATMEL (6) AWP (5) C- Language (3) CAO (5) CDAC (1) Cloud Computing (1) CMC (6) CN (10) CONSERVATION & MANAGEMENT Complete Notes (1) Control Sys (2) CVSM (2) Data Analytics (1) DC (5) Digital System Design (3) DIP (17) DLD (10) DLD Lab Manual (1) DS (4) DSD&DICA (9) DSP (6) EDC (45) Electonic Components (2) Embedded (7) EMWTL (6) ENERGY AUDIT (1) Engg. Mechanics (2) Engg.Chemistry (11) Faculty Files (6) Faculty Labs (1) Fuzzy (3) II MID (1) Job Exam Papers (4) LICA (1) Linux OS (1) Materials (1) MATLAB (27) MCU IDE 8051 (1) MEMS (4) MPMC (57) MPMC LAB Manual (3) MTECH (6) MWE (2) OC (1) PDC (9) Projects (8) RADAR (1) Research (9) RVSP (3) SC (1) Softwares (1) SS (1) STLD (18) TCSS (1) TSSN (1) TV Engg (1) VHDL (4) VLSI (8) VLSI Lab manual (1) WCN (1) Wireless Commu. (1) WSN (2)

Contact Form :

Name

Email *

Message *

Theme images by enjoynz. Powered by Blogger.