codings related to avr micro controller

82
// Program to configure watchdog timer in ATmega16 Microcontroller #include<avr/io.h> #include<util/delay.h> int main() { DDRB=0x03; if(bit_is_set(MCUCSR,WDRF)) { PORTB|=(1<<PB1); _delay_ms(1000); } PORTB&=~(1<<PB1); WDTCR=0x0F; while(1) { PORTB|=(1<<PB0); _delay_ms(400); PORTB&=~(1<<PB0); _delay_ms(400); } } LCD SCROLLING MODULE #define F_CPU 8000000 #include <avr/io.h> #include <util/delay.h> #include <avr/interrupt.h> #include <avr/eeprom.h> #include "usart.h" void switch_init ( void ); int main ( void ) { usart_init ();

description

This article is related to codings of different types of applications which is taken from engineersgarage.com

Transcript of codings related to avr micro controller

Page 1: codings related to avr micro controller

// Program to configure watchdog timer in ATmega16 Microcontroller #include<avr/io.h>#include<util/delay.h> int main(){DDRB=0x03; if(bit_is_set(MCUCSR,WDRF)){PORTB|=(1<<PB1);_delay_ms(1000); } 

PORTB&=~(1<<PB1);WDTCR=0x0F;while(1){PORTB|=(1<<PB0);_delay_ms(400);PORTB&=~(1<<PB0);_delay_ms(400);}}

 

LCD SCROLLING MODULE#define F_CPU 8000000

 

#include <avr/io.h>#include <util/delay.h>#include <avr/interrupt.h>#include <avr/eeprom.h>#include "usart.h" 

void switch_init ( void ); 

int main ( void ){usart_init ();switch_init ();while ( 1 ){PORTD &= 0x7F;_delay_ms ( 2000 );

Page 2: codings related to avr micro controller

PORTD |= 0x80;while ( 0x0F == ( PINC & 0x0F ) );   // wait till any key is pressed_delay_ms ( 50 );usart_send_string ( "This is a demonstration of single line LCD scrolling display module interfacingby Engineers Garage !!!\n" );_delay_ms ( 500 );}} 

void switch_init ( void ){cli();DDRC &= 0xE0;PORTC = 0xFF;DDRD |= 0x80;PORTD |= 0x80;} 

#define _USART_H #ifndef F_CPU #define F_CPU 8000000 #endif #define USART_BAUDRATE 9600 #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) #include<avr/io.h> #include<util/delay.h> #include <stdio.h> void usart_init(); void usart_putch(unsigned char send); unsigned int usart_getch(); void usart_send_string(const char* data); int uart_print(char c, FILE *stream); FILE uart_out = FDEV_SETUP_STREAM(uart_print, NULL, _FDEV_SETUP_WRITE); int uart_print(char c, FILE *stream) { if (c == '\n') uart_print('\r', stream); loop_until_bit_is_set(UCSRA, UDRE); UDR = c; return 0; } void usart_init () { UCSRB |= (1<<RXCIE) | (1 << RXEN) | (1 << TXEN); // Turn on the transmission reception .. // circuitry and receiver interrupt UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value.. // into the low byte of the UBRR register UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value.. // into the high byte of the UBRR register stdout = &uart_out; } void usart_putch(unsigned char send) { while ((UCSRA & (1 << UDRE)) == 0); // Do nothing until UDR is ready.. // for more data to be written to it UDR = send; // Send the byte } unsigned int usart_getch() { while ((UCSRA & (1 << RXC)) == 0); // Do nothing until data have been received and is ready to be read from UDR return(UDR); // return the byte } void usart_send_string(const char* data) { for(; *data; data ++) usart_putch(*data); } #endif

PART 2 OF LCD#ifndef _LCD_H #define _LCD_H #ifndef F_CPU #define F_CPU 8000000 #endif #include<avr/io.h> #include<util/delay.h> #include<inttypes.h> #include <stdio.h> #include <string.h> #define rs PA0 #define rw PA1 #define en PA2 void lcd_init(); void dis_cmd(char); void dis_data(char); void lcdcmd(char); void lcddata(char); void lcd_clear(void); void lcd_2nd_line(void); void lcd_1st_line(void); void lcd_string(const char *data); int lcd_print(char c, FILE *stream); int lcd_scroll(const char *data); FILE lcd_out = FDEV_SETUP_STREAM(lcd_print, NULL, _FDEV_SETUP_WRITE); char disp_beg [] = " "; int lcd_print(char c, FILE *stream) { if('\n' == c) lcd_2nd_line(); else dis_data(c); return 0; } #define F_CPU 8000000 #include <avr/io.h> #include <util/delay.h> #include <stdio.h> #include <avr/interrupt.h> #include "lcd.h" #include "usart.h" char A [ 150 ]; char B [ 150 ]; int main ( void ) { int i; usart_init (); cli(); lcd_init (); printf ( " ENGINEERS "); printf ( "\n GARAGE "); for ( i = 0; '\n' != ( A [ i ] = usart_getch () ); i ++ ); A [ i ] = '\0'; while ( 1 ) { for ( i = 0; i < 150; i ++ ) B [ i ] = A [ i ]; lcd_scroll ( B ); } } #ifndef _USART_H

Page 3: codings related to avr micro controller

#define _USART_H 

#ifndef F_CPU#define F_CPU 8000000#endif #define USART_BAUDRATE 9600#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) #include<avr/io.h>#include<util/delay.h>#include <stdio.h> 

void usart_init();void usart_putch(unsigned char send);unsigned int usart_getch();void usart_send_string(const char* data);int uart_print(char c, FILE *stream); 

FILE uart_out = FDEV_SETUP_STREAM(uart_print, NULL,                                         _FDEV_SETUP_WRITE); 

int uart_print(char c, FILE *stream){ 

  if (c == '\n')    uart_print('\r', stream);  loop_until_bit_is_set(UCSRA, UDRE);  UDR = c;  return 0;} 

void usart_init (){UCSRB |= (1<<RXCIE) | (1 << RXEN) | (1 << TXEN);   // Turn on the transmission reception ..// circuitry and receiver interruptUCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes 

UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value..// into the low byte of the UBRR registerUBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value..// into the high byte of the UBRR registerstdout = &uart_out;} void usart_putch(unsigned char send){while ((UCSRA & (1 << UDRE)) == 0); // Do nothing until UDR is ready..

Page 4: codings related to avr micro controller

// for more data to be written to itUDR = send; // Send the byte } unsigned int usart_getch(){while ((UCSRA & (1 << RXC)) == 0);// Do nothing until data have been received and is ready to be read from UDRreturn(UDR); // return the byte} 

void usart_send_string(const char* data){for(; *data; data ++)usart_putch(*data);} 

#endif// Program to get input from keypad and display it on LCD.#include<avr/io.h>#include<util/delay.h> #define pad PORTD#define r1 PD0#define r2 PD1#define r3 PD2#define r4 PD3 #define c1 PD4#define c2 PD5#define c3 PD6 void check1(void);void check2(void);  void check3(void);void check4(void); #define LCD_DATA PORTA //LCD data port #define ctrl PORTB#define en PB2 //enable signal#define rw PB1 //read/write signal#define rs PB0 //resister select signal void LCD_cmd(unsigned char cmd);void init_LCD(void);

Page 5: codings related to avr micro controller

void LCD_write(unsigned char data); unsigned int press; int main(){unsigned char value; DDRA=0xff; //LCD_DATA port as output portDDRB=0x07; //signal as out putDDRD=0x0F;pad=0xf0;init_LCD(); //initialization of LCDLCD_write_string("press a key");LCD_cmd(0xc0);  while(1){PORTD=0xF0; //set all the input to onevalue=PIND; //get the PORTD value in variable “value”if(value!=0xf0) //if any key is pressed value changed{check1();check2();check3();check4();}}return 0;} void check1(void){//DDRD = 0xf0;pad =0b11111110;//pad &= (0<<r1);_delay_us(10);if(bit_is_clear(PIND,c1))LCD_write('1');else if(bit_is_clear(PIND,c2))LCD_write('2');else if(bit_is_clear(PIND,c3))LCD_write('3');}  

Page 6: codings related to avr micro controller

void check2(void){pad=0b11111101;/pad &= (0<<r2);_delay_us(10);if(bit_is_clear(PIND,c1))LCD_write('4');else if(bit_is_clear(PIND,c2))LCD_write('5');else if(bit_is_clear(PIND,c3))LCD_write('6');} void check3(void){pad=0b11111011;//pad &= (0<<r3);_delay_us(10);if(bit_is_clear(PIND,c1))LCD_write('7');else if(bit_is_clear(PIND,c2))LCD_write('8');else if(bit_is_clear(PIND,c3))LCD_write('9');} void check4(void){pad =0b11110111;//pad &= (0<<r4);_delay_us(10);if(bit_is_clear(PIND,c1))LCD_write('#');else if(bit_is_clear(PIND,c2))LCD_write('0');else if(bit_is_clear(PIND,c3))LCD_write('*');}   void init_LCD(void){ LCD_cmd(0x38); //initializtion of 16X2 LCD in 8bit mode_delay_ms(1);

Page 7: codings related to avr micro controller

 LCD_cmd(0x01); //clear LCD_delay_ms(1); LCD_cmd(0x0E); //cursor ON_delay_ms(1); LCD_cmd(0x80); // ---8 go to first line and --0 is for 0th position_delay_ms(1);return;}  void LCD_cmd(unsigned char cmd){LCD_DATA=cmd;ctrl =(0<<rs)|(0<<rw)|(1<<en); // making RS and RW as LOW and EN as HIGH_delay_ms(1);ctrl =(0<<rs)|(0<<rw)|(0<<en); // making RS, RW , LOW and EN as LOW_delay_ms(50);return;}  void LCD_write(unsigned char data){LCD_DATA= data;ctrl = (1<<rs)|(0<<rw)|(1<<en); // making RW as LOW and RS, EN as HIGH_delay_ms(1);ctrl = (1<<rs)|(0<<rw)|(0<<en); // making EN and RW as LOW and RS HIGH_delay_ms(50); // give a 10 milli second delay to get thigs executedreturn ;} void LCD_write_string(unsigned char *str) //take address vaue of the string in pionter *str{int i=0;while(str[i]!='\0') // loop will go on till the NULL charaters is soon in string {LCD_write(str[i]); // sending data on CD byte by bytei++;}return;}

Page 8: codings related to avr micro controller

 

// Program to interface LCD in 4 bit mode with AVR microcontroller//#define F_CPU 12000000UL#include<avr/io.h>#include<util/delay.h>#include<inttypes.h> #define rs PA0 #define rw PA1#define en PA2 void lcd_init();void dis_cmd(char);void dis_data(char);void lcdcmd(char);void lcddata(char); int main(void){unsigned char data0[11]="ENGINEERS";unsigned char data1[10]="GARAGE"; int i=0;DDRA=0xFF;lcd_init();   

while(data0[i]!='\0'){dis_data(data0[i]);_delay_ms(200);i++;} dis_cmd(0xC5); i=0;while(data1[i]!='\0'){dis_data(data1[i]);_delay_ms(200);i++;} while(1);}

Page 9: codings related to avr micro controller

   void lcd_init() // fuction for intialize {dis_cmd(0x02); // to initialize LCD in 4-bit mode.dis_cmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.dis_cmd(0x0C);dis_cmd(0x06);dis_cmd(0x83);} void dis_cmd(char cmd_value){char cmd_value1; 

cmd_value1 = cmd_value & 0xF0; //mask lower nibble because PA4-PA7 pins are used. lcdcmd(cmd_value1); // send to LCD cmd_value1 = ((cmd_value<<4) & 0xF0); //shift 4-bit and masklcdcmd(cmd_value1); // send to LCD}   void dis_data(char data_value){char data_value1; 

data_value1=data_value&0xF0;lcddata(data_value1); data_value1=((data_value<<4)&0xF0);lcddata(data_value1);} void lcdcmd(char cmdout){PORTA=cmdout;PORTA&=~(1<<rs);PORTA&=~(1<<rw);PORTA|=(1<<en);_delay_ms(1);PORTA&=~(1<<en);} 

Page 10: codings related to avr micro controller

void lcddata(char dataout){PORTA=dataout;PORTA|=(1<<rs);PORTA&=~(1<<rw);PORTA|=(1<<en);_delay_ms(1);PORTA&=~(1<<en);} //Program to Display string on LCD using AVR Microcontroller (ATmega16)/*LCD DATA port----PORT Bsignal port------PORT Drs-------PD0rw-------PD1en-------PD2*/ #include<avr/io.h>#include<util/delay.h> #define LCD_DATA PORTB //LCD data port #define ctrl PORTD#define en PD2 // enable signal#define rw PD1 // read/write signal#define rs PD0 // register select signal void LCD_cmd(unsigned char cmd);void init_LCD(void);void LCD_write(unsigned char data); int main(){DDRB=0xff; DDRD=0x07; init_LCD(); // initialization of LCD_delay_ms(50); // delay of 50 mili secondsLCD_write_string("EngineersGarage"); // function to print string on LCDreturn 0;} void init_LCD(void){LCD_cmd(0x38); // initialization of 16X2 LCD in 8bit mode

Page 11: codings related to avr micro controller

_delay_ms(1); LCD_cmd(0x01); // clear LCD_delay_ms(1); LCD_cmd(0x0E); // cursor ON_delay_ms(1); LCD_cmd(0x80); // ---8 go to first line and --0 is for 0th position_delay_ms(1);return;} void LCD_cmd(unsigned char cmd){LCD_DATA=cmd;ctrl =(0<<rs)|(0<<rw)|(1<<en); _delay_ms(1);ctrl =(0<<rs)|(0<<rw)|(0<<en); _delay_ms(50);return;} void LCD_write(unsigned char data){LCD_DATA= data;ctrl = (1<<rs)|(0<<rw)|(1<<en); _delay_ms(1);ctrl = (1<<rs)|(0<<rw)|(0<<en); _delay_ms(50); return ;} void LCD_write_string(unsigned char *str) //store address value of the string in pointer *str{int i=0;while(str[i]!='\0') // loop will go on till the NULL character in the string {LCD_write(str[i]); // sending data on LCD byte by bytei++;  }return;}

REMOTE MONITORING SYSTEM

Page 12: codings related to avr micro controller

#include <avr/io.h>#include <util/delay.h>#include <string.h>unsigned int value,pre_val,upper_limit,lower_limit,upper_limit_flag=0, out_of_range_flag=0;void senddata(unsigned char data) // function to send data to LCD { _delay_ms(1); PORTD=(1<<PD0); PORTC=data; PORTD=(1<<PD0)|(1<<PD1); PORTD=(1<<PD0)|(0<<PD1); }void sendcmd(unsigned char cmd) // function to send command to LCD { //PORTD = PORTD & 0xFB; _delay_ms(1); PORTD=(0<<PD0); PORTC=cmd; PORTD=(1<<PD1); PORTD=(0<<PD1); }void printstr(unsigned char *s) // function to display string on LCD{ uint8_t l,i; l = strlen(s); // get the length of string for(i=0;i<l;i++) { senddata(*s); // write every char one by one s++; }}void transmit(unsigned int val) // transmit function { if(val<lower_limit) // if new temperature value is { // less PORTB |= 0x0C; // transmit data to make RED LED PORTB &= 0xED; // off and GREEN LED ON _delay_ms(100); PORTB |= 0x10; out_of_range_flag=1; // set out of range flag } else if(val>upper_limit) // if new temperature value is { // more

Page 13: codings related to avr micro controller

PORTB |= 0x0A; // transmit data to make RED LED PORTB &= 0xEB; // ON and GREEN LED OFF _delay_ms(100); PORTB |= 0x10; out_of_range_flag=1; } if((val>=lower_limit) && (value<=upper_limit)) // if new value is { // within range if(out_of_range_flag==1) // only one time { PORTB |= 0x06; // transmit data to ON BLUE PORTB &= 0xE7; // LED and off other two LEDs _delay_ms(100); PORTB |= 0x10; out_of_range_flag=0; } } if(val>=150) // if temperature value exceeds limit { if(upper_limit_flag==0) // only one time { PORTB |= 0x01; // transmit data to switch ON PORTB &= 0xEF; // RELAY _delay_ms(100); PORTB |= 0x10; upper_limit_flag=1; } } else if(val<=140) // if temperature decreases { if(upper_limit_flag==1) // only one time { PORTB &= 0xEE; // transmit data to switch off _delay_ms(100); // RELAY PORTB |= 0x10; upper_limit_flag=0; } } } unsigned int convert_n_display() // convert 10 bit ADC value into { // decimal and ASCII unsigned int tmp1,tmp2,tmp3,t,t1,a,temp_value; unsigned char asci[3]; tmp1 = (ADCL & 0x0F); tmp2 = ADCL >> 4; tmp3 = (ADCH & 0x0F);

Page 14: codings related to avr micro controller

tmp2 = tmp2*16; tmp3 = tmp3*256; t = tmp1+tmp2+tmp3; temp_value = t; if(t>=100) { a=2; while(t>=10) { t1=t%10; asci[a]=t1+0x30; t=t/10; a--; } asci[0]=t+0x30; } else { t1=t%10; asci[2]=t1+0x30; t=t/10; asci[1]=t+0x30; asci[0]=0x30; } sendcmd(0x80); printstr("temperature:"); sendcmd(0xC0); senddata(asci[0]); senddata(asci[1]); senddata(asci[2]); printstr(" deg Cel"); return temp_value; // return decimal value }void adc_init() // initialize built in ADC { ADMUX = (1<<REFS0); ADCSRA =(1<<ADPS2) | (1<<ADPS1) | (0<<ADPS0); ADMUX=0x00; }void lcd_init() // initialize LCD and { sendcmd(0x3E); sendcmd(0x0E); sendcmd(0x01); sendcmd(0x82); printstr("temperature"); // display message sendcmd(0xC5); printstr("sensor"); }void main()

Page 15: codings related to avr micro controller

{ DDRD=0x07; // port D 3 bits as output PORTD=0x00; DDRC=0xFF; // port C as output PORTC=0x00; DDRB = 0x1F; // port B as output PORTB = 0x10; lcd_init(); _delay_ms(2000); // wait for 2 sec adc_init(); while(1) { ADCSRA = (1<<ADEN) | (1<<ADSC); // start and enable ADC while(!(ADCSRA & (1<<ADIF))); // wait till conversion end ADCSRA = (1<<ADIF); value = convert_n_display(); // get temperature value transmit(value); // transmit to remote node pre_val = value; // save value upper_limit = pre_val+2; // set upper and lower_limit = pre_val-2; // lower limit _delay_ms(2000); // wait for 2 sec } }

 

#include <IRremote.h> // Include IR remote LibraryIRsend irsend;#define select1 // Select Buttons#define select2#define select3#define row1 4 // Operational Keypad Matrix (2x3)#define row2 5#define col1 6#define col2 7#define col3 8void setup(){ pinMode(row1,OUTPUT); pinMode(row2,OUTPUT); pinMode(col1,INPUT); pinMode(col2,INPUT); pinMode(col3,INPUT); pinMode(select1,INPUT); pinMode(select2,INPUT); pinMode(select3,INPUT);}

Page 16: codings related to avr micro controller

void loop() {if(digitalRead(select1)==HIGH) // Remote 1 (TV){ digitalWrite(row1,HIGH); digitalWrite(row2,LOW); if(digitalRead(col1)==HIGH) // key1 pressed { irsend.sendNEC(0x1CE338C7,32); //power } else if(digitalRead(col2)==HIGH) // key2 pressed { irsend.sendNEC(0x1CE3A857,32); //mute } else if(digitalRead(col3)==HIGH) // key3 pressed { irsend.sendNEC(0x1CE36897,32); //Channel Up } else { digitalWrite(3,LOW); } delay(10); digitalWrite(row2,HIGH); digitalWrite(row1,LOW); if(digitalRead(col1)==HIGH) // key4 pressed { irsend.sendNEC(0x1CE3E817,32); //Channel Down } else if(digitalRead(col2)==HIGH) // key5 pressed { irsend.sendNEC(0x1CE330CF,32); //Volume Up } else if(digitalRead(col3)==HIGH) // key6 pressed { irsend.sendNEC(0x1CE3B04F,32); //Volume Down } else { digitalWrite(3,LOW); } delay(10);}else if (digitalRead(select2)==HIGH) // --> Remote 2 (DVD/DTH/etc){ // Similar to 'if(){}' block above; copy your own remote codes;}else if (digitalRead(select2)==HIGH) // --> Remote 3 (other appliances like AC)

Page 17: codings related to avr micro controller

{ // Similar to 'if(){}' block above; copy your own remote codes;}else{ // if multiple select switches ON simultaneously --> NO operation}}

GSM BASED AC APPLIANCE

/* * gsmEG.c * * Created: 3/14/2014 1:21:32 PM * Author: GANESH SELVARAJ */ #define F_CPU 16000000UL#include <avr/io.h>#include <util/delay.h>#include <stdlib.h>#include <string.h>#include "GSM.h"#include "lcd.h"void gsm_read(){int k;clrscr();LCD_write_string("System Activated");gotoxy(1,16);UART_Transmit_string("AT+CMGR=1\r");gsm_waitfor('\r');gsm_waitfor('\n');if(UART_Receive()=='+'){gsm_waitfor('M');if(UART_Receive()=='G'){gsm_waitfor('A');gsm_waitfor(',');gsm_waitfor('"');for(k=0;k<13;k++)number[k] = UART_Receive();gsm_waitfor(',');gsm_waitfor(',');gsm_waitfor('+');gsm_waitfor('\n');for(k=0;k<9;k++)msg[k]=UART_Receive();gsm_waitfor('K');gsm_waitfor('\n');

Page 18: codings related to avr micro controller

_delay_ms(300);clrscr();LCD_write_string("Ph:");LCD_write_string(number);gotoxy(1,0);LCD_write_string("Msg:");LCD_write_string(msg);_delay_ms(2000);if(!strcmp(msg,"Motor on ")){PORTB |= (1<<PB2);}if(!strcmp(msg,"Motor off")){PORTB &= ~(1<<PB2);}gsm_delete();}}_delay_ms(1000);}int main(void){DDRB |= (1<<PB2);lcd_init();LCD_write_string("Initializing... ");gsm_init();gsm_delete();while(1){gsm_read();}return 0;}

GSM

#ifndef GSM#define GSM#include <avr/io.h>#include <util/delay.h>#include <string.h>#include <avr/wdt.h>#include <avr/interrupt.h>#include "lcd.h"

Page 19: codings related to avr micro controller

char msg[10];char number[14];int i,j;void UART_Init( unsigned int baud );void UART_Transmit_char( unsigned char data );unsigned char UART_Receive( void );void UART_Transmit_string( char *string );void UART_Init( unsigned int baud ){ /* Set baud rate */ UBRRH = (unsigned char)(baud>>8); UBRRL = (unsigned char)baud; /* Enable receiver and transmitter */ UCSRB = (1<<RXEN)|(1<<TXEN); /* Set frame format: 8data, 1stop bit */ UCSRC = (1<<URSEL)|(0<<USBS)|(3<<UCSZ0);}void UART_Transmit_char( unsigned char data ){ /* Wait for empty transmit buffer */ while ( !( UCSRA & (1<<UDRE)) ) ; /* Put data into buffer, sends the data */ UDR = data;}unsigned char UART_Receive( void ){ /* Wait for data to be received */ while ( !(UCSRA & (1<<RXC)) ) ; /* Get and return received data from buffer */ return UDR;}void UART_Transmit_string( char string[] ){ int i=0; while ( string[i] > 0) UART_Transmit_char(string[i++]);}/*************************************************************/void gsm_init(void);void gsm_read(void);void gsm_send(char *number,char *string);void gsm_delete(void);void gsm_waitfor(char c);void gsm_waitfor(char c){//enabling watchdogtimer with a time of 2.1secswdt_enable(7);//waiting for the byte to be received

Page 20: codings related to avr micro controller

while(UART_Receive()!= c);//resetting watchdogtimer and turning off the watchdogtimerwdt_reset();wdt_disable();}void gsm_init(){UART_Init(103); // baudrate=9600gotoxy(1,0);LCD_write_string(" Testing Modem ");_delay_ms(500);UART_Transmit_string("AT\r");gsm_waitfor('O');gsm_waitfor('K');gotoxy(1,0);LCD_write_string(" Modem : OK ");_delay_ms(1000);INS:gotoxy(1,0);LCD_write_string(" Checking SIM ");_delay_ms(500);UART_Transmit_string("AT+CSMINS?\r");gsm_waitfor( '\n');gsm_waitfor(',');if(UART_Receive() == '2'){ gotoxy(1,0);LCD_write_string(" SIM NOTFOUND ");_delay_ms(1000);goto INS;}else if(UART_Receive() == '1');gsm_waitfor( 'K');gsm_waitfor( '\n');gotoxy(1,0);LCD_write_string(" SIM FOUND ");_delay_ms(1000);REG:gotoxy(1,0);LCD_write_string(" Network Status ");_delay_ms(500);UART_Transmit_string("AT+CREG?\r");gsm_waitfor( '\n');gsm_waitfor(',');if(UART_Receive() == '2'){gotoxy(1,0);LCD_write_string("Network NotFound");_delay_ms(1000);goto REG;

Page 21: codings related to avr micro controller

}else if(UART_Receive() == '1');gsm_waitfor( 'K');gsm_waitfor( '\n');gotoxy(1,0);LCD_write_string(" Network Found ");_delay_ms(1000);UART_Transmit_string("AT+CMGF=1\r");gotoxy(1,0);LCD_write_string("Setting Textmode");gsm_waitfor('O');gsm_waitfor('K');gotoxy(1,0);LCD_write_string(" Textmode set ");_delay_ms(1000);}void gsm_delete(){UART_Transmit_string("AT+CMGD=1\r");gsm_waitfor('K');gsm_waitfor('\n');_delay_ms(500);}#endif

LCD

#include<avr/io.h>#include<util/delay.h>#include<inttypes.h>#include "lcd.h"void LCD_write_string(const char *str) //store address value of t he string in pointer *str{int i=0;while(str[i]!='\0') // loop will go on till the NULL character in the string{if (str[i]=='*'){i++;int8_t cc=str[i]-'0';if(cc>=0 && cc<=7){

Page 22: codings related to avr micro controller

dis_data(cc);}else{dis_data('%');dis_data(str[i]);}}else dis_data(str[i]); // sending data on LCD byte by bytei++;}return;}void lcd_init() // function for initialize{DDRA=0xFF;dis_cmd(0x02); // to initialize LCD in 4-bit mode.dis_cmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.dis_cmd(0x0C);dis_cmd(0x06);dis_cmd(0x0E);custom_char();gotoxy(0,0);}void dis_cmd(char cmd_value){char cmd_value1;cmd_value1 = ((cmd_value>>4) & 0x0F); //shift 4-bit and masklcdcmd(cmd_value1); // send to LCDcmd_value1 = cmd_value & 0x0F; //mask lower nibble because PA4-PA 7 pins are used.lcdcmd(cmd_value1); // send to LCD}void dis_data(char data_value){char data_value1;data_value1=((data_value>>4)&0x0F);lcddata(data_value1);data_value1=data_value&0x0F;lcddata(data_value1);}void lcdcmd(char cmdout){PORTA=cmdout;PORTA&=~(1<<rs);PORTA&=~(1<<rw);PORTA|=(1<<en);_delay_ms(1);PORTA&=~(1<<en);

Page 23: codings related to avr micro controller

}void lcddata(char dataout){PORTA=dataout;PORTA|=(1<<rs);PORTA&=~(1<<rw);PORTA|=(1<<en);_delay_ms(1);PORTA&=~(1<<en);}void clrscr(){_delay_ms(10);dis_cmd(0x01);_delay_ms(100);}void gotoxy(char a,char b){if(a==0) a=0b10000000;else if(a==1) a=0b11000000;else if(a==2) a=0b10010100;else if(a==3) a=0b11010100;dis_cmd(a+b); }void custom_char(){unsigned char c[]={0x04, 0x0E, 0x0E, 0x1F, 0x1F, 0x0E, 0x0E, 0x04, //Char00x1F, 0x11, 0x0A, 0x04, 0x04, 0x04, 0x04, 0x04, //Char10x01, 0x01, 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, //Char20x00, 0x00, 0x00, 0x04, 0x04, 0x14, 0x14, 0x14, //Char30x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x10, //Char40x00, 0x00, 0x0A, 0x00, 0x04, 0x1F, 0x0A, 0x04, //Char50x00, 0x04, 0x0A, 0x11, 0x1F, 0x00, 0x00, 0x00, //Char60x00, 0x00, 0x00, 0x1F, 0x11, 0x0A, 0x04, 0x00, //Char7};uint8_t a[]={72,80,88,96,104,112,120,64};uint8_t i,j;for(i=0;i<sizeof(a);i++){dis_cmd(a[i]);for(j=0;j<sizeof(c);j++){dis_data(c[j]);}}

}

Page 24: codings related to avr micro controller

lcd

#ifndef LCD_H_#define LCD_H_#define rs PA7#define rw PA6#define en PA4void lcd_init();void dis_cmd(char);void dis_data(char);void lcdcmd(char);void lcddata(char);void clrscr();void gotoxy(char,char);void LCD_write_string(const char *);void custom_char();#endif/* * gsmEG.c * * Created: 3/14/2014 1:21:32 PM * Author: GANESH SELVARAJ */ #define F_CPU 16000000UL#include <avr/io.h>#include <util/delay.h>#include <stdlib.h>#include <string.h>#include "GSM.h"#include "lcd.h"void gsm_read(){int k;clrscr();LCD_write_string("System Activated");gotoxy(1,16);UART_Transmit_string("AT+CMGR=1\r");gsm_waitfor('\r');gsm_waitfor('\n');if(UART_Receive()=='+'){gsm_waitfor('M');if(UART_Receive()=='G'){gsm_waitfor('A');

Page 25: codings related to avr micro controller

gsm_waitfor(',');gsm_waitfor('"');for(k=0;k<13;k++)number[k] = UART_Receive();gsm_waitfor(',');gsm_waitfor(',');gsm_waitfor('+');gsm_waitfor('\n');for(k=0;k<9;k++)msg[k]=UART_Receive();gsm_waitfor('K');gsm_waitfor('\n');_delay_ms(300);clrscr();LCD_write_string("Ph:");LCD_write_string(number);gotoxy(1,0);LCD_write_string("Msg:");LCD_write_string(msg);_delay_ms(2000);if(!strcmp(msg,"Motor on ")){PORTB |= (1<<PB2);}if(!strcmp(msg,"Motor off")){PORTB &= ~(1<<PB2);}gsm_delete();}}_delay_ms(1000);}int main(void){DDRB |= (1<<PB2);lcd_init();LCD_write_string("Initializing... ");gsm_init();gsm_delete();while(1){gsm_read();}return 0;}

GSM

Page 26: codings related to avr micro controller

#ifndef GSM#define GSM#include <avr/io.h>#include <util/delay.h>#include <string.h>#include <avr/wdt.h>#include <avr/interrupt.h>#include "lcd.h"char msg[10];char number[14];int i,j;void UART_Init( unsigned int baud );void UART_Transmit_char( unsigned char data );unsigned char UART_Receive( void );void UART_Transmit_string( char *string );void UART_Init( unsigned int baud ){ /* Set baud rate */ UBRRH = (unsigned char)(baud>>8); UBRRL = (unsigned char)baud; /* Enable receiver and transmitter */ UCSRB = (1<<RXEN)|(1<<TXEN); /* Set frame format: 8data, 1stop bit */ UCSRC = (1<<URSEL)|(0<<USBS)|(3<<UCSZ0);}void UART_Transmit_char( unsigned char data ){ /* Wait for empty transmit buffer */ while ( !( UCSRA & (1<<UDRE)) ) ; /* Put data into buffer, sends the data */ UDR = data;}unsigned char UART_Receive( void ){ /* Wait for data to be received */ while ( !(UCSRA & (1<<RXC)) ) ; /* Get and return received data from buffer */ return UDR;}void UART_Transmit_string( char string[] ){ int i=0; while ( string[i] > 0) UART_Transmit_char(string[i++]);

Page 27: codings related to avr micro controller

}/*************************************************************/void gsm_init(void);void gsm_read(void);void gsm_send(char *number,char *string);void gsm_delete(void);void gsm_waitfor(char c);void gsm_waitfor(char c){//enabling watchdogtimer with a time of 2.1secswdt_enable(7);//waiting for the byte to be receivedwhile(UART_Receive()!= c);//resetting watchdogtimer and turning off the watchdogtimerwdt_reset();wdt_disable();}void gsm_init(){UART_Init(103); // baudrate=9600gotoxy(1,0);LCD_write_string(" Testing Modem ");_delay_ms(500);UART_Transmit_string("AT\r");gsm_waitfor('O');gsm_waitfor('K');gotoxy(1,0);LCD_write_string(" Modem : OK ");_delay_ms(1000);INS:gotoxy(1,0);LCD_write_string(" Checking SIM ");_delay_ms(500);UART_Transmit_string("AT+CSMINS?\r");gsm_waitfor( '\n');gsm_waitfor(',');if(UART_Receive() == '2'){ gotoxy(1,0);LCD_write_string(" SIM NOTFOUND ");_delay_ms(1000);goto INS;}else if(UART_Receive() == '1');gsm_waitfor( 'K');gsm_waitfor( '\n');gotoxy(1,0);LCD_write_string(" SIM FOUND ");_delay_ms(1000);REG:

Page 28: codings related to avr micro controller

gotoxy(1,0);LCD_write_string(" Network Status ");_delay_ms(500);UART_Transmit_string("AT+CREG?\r");gsm_waitfor( '\n');gsm_waitfor(',');if(UART_Receive() == '2'){gotoxy(1,0);LCD_write_string("Network NotFound");_delay_ms(1000);goto REG;}else if(UART_Receive() == '1');gsm_waitfor( 'K');gsm_waitfor( '\n');gotoxy(1,0);LCD_write_string(" Network Found ");_delay_ms(1000);UART_Transmit_string("AT+CMGF=1\r");gotoxy(1,0);LCD_write_string("Setting Textmode");gsm_waitfor('O');gsm_waitfor('K');gotoxy(1,0);LCD_write_string(" Textmode set ");_delay_ms(1000);}void gsm_delete(){UART_Transmit_string("AT+CMGD=1\r");gsm_waitfor('K');gsm_waitfor('\n');_delay_ms(500);}#endif

LCD

#include<avr/io.h>#include<util/delay.h>#include<inttypes.h>#include "lcd.h"void LCD_write_string(const char *str) //store address value of t he string in pointer *str{

Page 29: codings related to avr micro controller

int i=0;while(str[i]!='\0') // loop will go on till the NULL character in the string{if (str[i]=='*'){i++;int8_t cc=str[i]-'0';if(cc>=0 && cc<=7){dis_data(cc);}else{dis_data('%');dis_data(str[i]);}}else dis_data(str[i]); // sending data on LCD byte by bytei++;}return;}void lcd_init() // function for initialize{DDRA=0xFF;dis_cmd(0x02); // to initialize LCD in 4-bit mode.dis_cmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.dis_cmd(0x0C);dis_cmd(0x06);dis_cmd(0x0E);custom_char();gotoxy(0,0);}void dis_cmd(char cmd_value){char cmd_value1;cmd_value1 = ((cmd_value>>4) & 0x0F); //shift 4-bit and masklcdcmd(cmd_value1); // send to LCDcmd_value1 = cmd_value & 0x0F; //mask lower nibble because PA4-PA7 pins are used.lcdcmd(cmd_value1); // send to LCD}void dis_data(char data_value){char data_value1;data_value1=((data_value>>4)&0x0F);lcddata(data_value1);data_value1=data_value&0x0F;lcddata(data_value1);

Page 30: codings related to avr micro controller

}void lcdcmd(char cmdout){PORTA=cmdout;PORTA&=~(1<<rs);PORTA&=~(1<<rw);PORTA|=(1<<en);_delay_ms(1);PORTA&=~(1<<en);}void lcddata(char dataout){PORTA=dataout;PORTA|=(1<<rs);PORTA&=~(1<<rw);PORTA|=(1<<en);_delay_ms(1);PORTA&=~(1<<en);}void clrscr(){_delay_ms(10);dis_cmd(0x01);_delay_ms(100);}void gotoxy(char a,char b){if(a==0) a=0b10000000;else if(a==1) a=0b11000000;else if(a==2) a=0b10010100;else if(a==3) a=0b11010100;dis_cmd(a+b); }void custom_char(){unsigned char c[]={0x04, 0x0E, 0x0E, 0x1F, 0x1F, 0x0E, 0x0E, 0x04, //Char00x1F, 0x11, 0x0A, 0x04, 0x04, 0x04, 0x04, 0x04, //Char10x01, 0x01, 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, //Char20x00, 0x00, 0x00, 0x04, 0x04, 0x14, 0x14, 0x14, //Char30x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x10, //Char40x00, 0x00, 0x0A, 0x00, 0x04, 0x1F, 0x0A, 0x04, //Char50x00, 0x04, 0x0A, 0x11, 0x1F, 0x00, 0x00, 0x00, //Char60x00, 0x00, 0x00, 0x1F, 0x11, 0x0A, 0x04, 0x00, //Char7};uint8_t a[]={72,80,88,96,104,112,120,64};uint8_t i,j;for(i=0;i<sizeof(a);i++){dis_cmd(a[i]);

Page 31: codings related to avr micro controller

for(j=0;j<sizeof(c);j++){dis_data(c[j]);}}}

lcd

#ifndef LCD_H_#define LCD_H_#define rs PA7#define rw PA6#define en PA4void lcd_init();void dis_cmd(char);void dis_data(char);void lcdcmd(char);void lcddata(char);void clrscr();void gotoxy(char,char);void LCD_write_string(const char *);void custom_char();#endif

LCD.H

#ifndef LCD_H_#define LCD_H_#define rs PA7#define rw PA6#define en PA4void lcd_init();void dis_cmd(char);void dis_data(char);void lcdcmd(char);void lcddata(char);void clrscr();void gotoxy(char,char);void LCD_write_string(const char *);void lcd_write_int(int,unsigned int);#endif

Page 32: codings related to avr micro controller

Variable.C

/* * VARIABLE.c * * Created: 5/29/2014 3:53:39 PM * Author: GANESH SELVARAJ */ #define F_CPU 16000000UL#include <avr/io.h>#include <util/delay.h>#include "lcd.h"void SetADC(){ADMUX|=(1<<REFS0);ADCSRA=(1<<ADEN)|(7<<ADPS0);}uint16_t ReadADC(uint8_t ch){//Select ADC Channel ch must be 0-7ch=ch&0b00000111;ADMUX&=0b11100000;ADMUX|=ch;//Start Single conversionADCSRA|=(1<<ADSC);//Wait for conversion to completewhile(!(ADCSRA & (1<<ADIF)));//Clear ADIF by writing one to itADCSRA|=(1<<ADIF);return(ADC);}void Waiting(int j) // simple delay function{uint8_t i;for(i=0;i<j;i++)_delay_ms(200);}int main(void){int v;_delay_ms(50); // delay of 50 millisecondsSetADC();lcd_init();LCD_write_string("Adj Power Supply");while(1) {gotoxy(1,1);

Page 33: codings related to avr micro controller

LCD_write_string("Voltage:");v=((ReadADC(0)/1024.00)*3000.00);lcd_write_int((v/100),2);dis_data('.');lcd_write_int((v%100),2);dis_data('V');dis_data(' ');Waiting(2); }}

LCD.H

/* * EG_LCD.c * * Created: 2/9/2014 10:49:54 PM * Author: stranger */ #include<avr/io.h>#include<util/delay.h>#include<inttypes.h>#include "lcd.h"void LCD_write_string(const char *str) //store address value of the string in pointer *str{int i=0;while(str[i]!='\0') // loop will go on till the NULL character in the string{if (str[i]=='*'){i++;int8_t cc=str[i]-'0';if(cc>=0 && cc<=7){dis_data(cc);}else{dis_data('%');dis_data(str[i]);}}else dis_data(str[i]); // sending data on LCD byte by bytei++;}

Page 34: codings related to avr micro controller

return;}void lcd_init() // function for initialize{DDRB=0xFF;dis_cmd(0x02); // to initialize LCD in 4-bit mode.dis_cmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.dis_cmd(0x0C);dis_cmd(0x06);dis_cmd(0x0E);gotoxy(0,0);}void dis_cmd(char cmd_value){char cmd_value1;cmd_value1 = ((cmd_value>>4) & 0x0F); //shift 4-bit and masklcdcmd(cmd_value1); // send to LCDcmd_value1 = cmd_value & 0x0F; //mask lower nibble because PA4-PA7 pins are used.lcdcmd(cmd_value1); // send to LCD}void dis_data(char data_value){char data_value1;data_value1=((data_value>>4)&0x0F);lcddata(data_value1);data_value1=data_value&0x0F;lcddata(data_value1);}void lcdcmd(char cmdout){PORTB=cmdout;PORTB&=~(1<<rs);PORTB&=~(1<<rw);PORTB|=(1<<en);_delay_ms(1);PORTB&=~(1<<en);}void lcddata(char dataout){PORTB=dataout;PORTB|=(1<<rs);PORTB&=~(1<<rw);PORTB|=(1<<en);_delay_ms(1);PORTB&=~(1<<en);}void clrscr(){_delay_ms(10);

Page 35: codings related to avr micro controller

dis_cmd(0x01);_delay_ms(100);}void gotoxy(char a,char b){if(a==0) a=0b10000000;else if(a==1) a=0b11000000;else if(a==2) a=0b10010100;else if(a==3) a=0b11010100;dis_cmd(a+b); }void lcd_write_int(int val,unsigned int field_length){char str[5]={0,0,0,0,0};uint8_t i=4,j=0;while(val){str[i]=val%10;val=val/10;i--;}if(field_length==-1)while(str[j]==0) j++;elsej=5-field_length;if(val<0) dis_data('-');for(i=j;i<5;i++){dis_data(48+str[i]);}} // Program to get latitude and longitude value from GPS modem and display it on LCD:

/*LCD DATA port----PORT Asignal port------PORT Brs-------PB0rw-------PB1en-------PB2*/ #define F_CPU 12000000UL #include<avr/io.h>#include<util/delay.h> #define USART_BAUDRATE 4800#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

Page 36: codings related to avr micro controller

  #define LCD_DATA PORTA //LCD data port #define ctrl PORTB#define en PB2 //enable signal#define rw PB1 //read/write signal#define rs PB0 //resister select signal void LCD_cmd(unsigned char cmd);void init_LCD(void);void LCD_write(unsigned char data);void LCD_write_string(unsigned char *str); void usart_init();unsigned int usart_getch(); unsigned char value,i,lati_value[15],lati_dir, longi_value[15], longi_dir, alti[5] ; int main(void){DDRA=0xff; //LCD_DATA port as out put portDDRB=0x07; //ctrl as out putinit_LCD(); //initialization of LCD_delay_ms(50); // delay of 50 mili secondsLCD_write_string("we at");LCD_cmd(0xC0); usart_init(); // initialization of USARTwhile(1){value=usart_getch();if(value=='$'){value=usart_getch();if(value=='G'){value=usart_getch();if(value=='P'){value=usart_getch();if(value=='G'){value=usart_getch();if(value=='G'){

Page 37: codings related to avr micro controller

value=usart_getch();if(value=='A'){value=usart_getch();if(value==','){value=usart_getch();while(value!=','){value=usart_getch();}lati_value[0]=usart_getch();value=lati_value[0];for(i=1;value!=',';i++){lati_value[i]=usart_getch();value=lati_value[i];}lati_dir=usart_getch();value=usart_getch();while(value!=','){value=usart_getch();}longi_value[0]=usart_getch();value=longi_value[0];for(i=1;value!=',';i++){longi_value[i]=usart_getch();value=longi_value[i];}longi_dir=usart_getch();LCD_cmd(0x01);_delay_ms(1);LCD_cmd(0x80);_delay_ms(1000);i=0;while(lati_value[i]!='\0'){LCD_write(lati_value[j]);j++;}LCD_write(lati_dir);LCD_cmd(0xC0);_delay_ms(1000);i=0;

Page 38: codings related to avr micro controller

while(longi_value[i]!='\0'){LCD_write(longi_value[i]);i++;}LCD_write(longi_dir);_delay_ms(1000); 

}}}}}}}}} void init_LCD(void){LCD_cmd(0x38); //initialization of 16X2 LCD in 8bit mode_delay_ms(1); LCD_cmd(0x01); //clear LCD_delay_ms(1); LCD_cmd(0x0E); //cursor ON_delay_ms(1); LCD_cmd(0x80); // ---8 go to first line and --0 is for 0th position_delay_ms(1);return;}  void LCD_cmd(unsigned char cmd){LCD_DATA=cmd;ctrl =(0<<rs)|(0<<rw)|(1<<en); _delay_us(40);ctrl =(0<<rs)|(0<<rw)|(0<<en); //_delay_ms(50);return;}  

Page 39: codings related to avr micro controller

void LCD_write(unsigned char data){LCD_DATA= data;ctrl = (1<<rs)|(0<<rw)|(1<<en); _delay_us(40);ctrl = (1<<rs)|(0<<rw)|(0<<en); //_delay_ms(50); return ;}  void usart_init(){   UCSRB |= (1<<RXCIE) | (1 << RXEN) | (1 << TXEN);   // Turn on the transmission and reception circuitryUCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR registerUBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register}  unsigned int usart_getch(){ while ((UCSRA & (1 << RXC)) == 0); // Do nothing until data have been recieved and is ready to be read from UDRreturn(UDR); // return the byte} void LCD_write_string(unsigned char *str) //take address vaue of the string in pionter *str{int i=0;while(str[i]!='\0') // loop will go on till the NULL charaters is soon in string {LCD_write(str[i]); // sending data on CD byte by bytei++;}return;}/ Program for Master Mode

Page 40: codings related to avr micro controller

// Check Code2 for Slave Mode Program#include<avr/io.h>#include<util/delay.h>#include<inttypes.h> void TWI_start(void);void TWI_repeated_start(void);void TWI_init_master(void);void TWI_write_address(unsigned char);void TWI_read_address(unsigned char);void TWI_write_data(unsigned char);void TWI_read_data(void);void TWI_stop(void); unsigned char address=0x20, read=1, write=0;unsigned char write_data=0x01, recv_data; int main(void){_delay_ms(2000);DDRB=0xff;TWI_init_master();  // Function to initialize TWIwhile(1){if(write_data==0x00) write_data=1; 

TWI_start(); // Function to send start conditionTWI_write_address(address+write); // Function to write address and data direction bit(write) on SDATWI_write_data(write_data);      // Function to write data in slaveTWI_stop(); // Function to send stop condition  _delay_ms(10); // Delay of 10 mili second TWI_start(); TWI_read_address(address+read); // Function to write address and data direction bit(read) on SDATWI_read_data(); // Function to read data from slave TWI_stop(); 

_delay_ms(1000);  

write_data = write_data * 2;}

Page 41: codings related to avr micro controller

  } void TWI_init_master(void) // Function to initialize master{TWBR=0x01; // Bit rateTWSR=(0<<TWPS1)|(0<<TWPS0); // Setting prescalar bits// SCL freq= F_CPU/(16+2(TWBR).4^TWPS)} void TWI_start(void){// Clear TWI interrupt flag, Put start condition on SDA, Enable TWITWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); while(!(TWCR & (1<<TWINT))); // Wait till start condition is transmittedwhile((TWSR & 0xF8)!= 0x08); // Check for the acknowledgement} void TWI_repeated_start(void){// Clear TWI interrupt flag, Put start condition on SDA, Enable TWITWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); while(!(TWCR & (1<<TWINT))); // wait till restart condition is transmittedwhile((TWSR & 0xF8)!= 0x10); // Check for the acknoledgement} void TWI_write_address(unsigned char data){TWDR=data; // Address and write instructionTWCR=(1<<TWINT)|(1<<TWEN);    // Clear TWI interrupt flag,Enable TWIwhile (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte transmittedwhile((TWSR & 0xF8)!= 0x18);  // Check for the acknoledgement} void TWI_read_address(unsigned char data){TWDR=data; // Address and read instructionTWCR=(1<<TWINT)|(1<<TWEN);    // Clear TWI interrupt flag,Enable TWIwhile (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte receivedwhile((TWSR & 0xF8)!= 0x40);  // Check for the acknoledgement} void TWI_write_data(unsigned char data){TWDR=data; // put data in TWDR

Page 42: codings related to avr micro controller

TWCR=(1<<TWINT)|(1<<TWEN);    // Clear TWI interrupt flag,Enable TWIwhile (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte transmittedwhile((TWSR & 0xF8) != 0x28); // Check for the acknoledgement} void TWI_read_data(void){TWCR=(1<<TWINT)|(1<<TWEN);    // Clear TWI interrupt flag,Enable TWIwhile (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte transmittedwhile((TWSR & 0xF8) != 0x58); // Check for the acknoledgementrecv_data=TWDR;PORTB=recv_data;}  void TWI_stop(void){// Clear TWI interrupt flag, Put stop condition on SDA, Enable TWITWCR= (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); while(!(TWCR & (1<<TWSTO)));  // Wait till stop condition is transmitted}// Program for Slave mode#include<avr/io.h>#include<util/delay.h> void TWI_init_slave(void);void TWI_match_read_slave(void);void TWI_read_slave(void);void TWI_match_write_slave(void);void TWI_write_slave(void); unsigned char write_data,recv_data; int main(void){DDRB=0xff;TWI_init_slave(); // Function to initilaize slavewhile(1){TWI_match_read_slave(); //Function to match the slave address and slave dirction bit(read) TWI_read_slave(); // Function to read data 

write_data=~recv_data; // Togglem the receive data 

TWI_match_write_slave(); //Function to match the slave address and slave dirction bit(write) 

Page 43: codings related to avr micro controller

TWI_write_slave();       // Function to write data}} void TWI_init_slave(void) // Function to initilaize slave{TWAR=0x20; // Fill slave address to TWAR} void TWI_write_slave(void) // Function to write data{TWDR= write_data;           // Fill TWDR register whith the data to be sent TWCR= (1<<TWEN)|(1<<TWINT);   // Enable TWI, Clear TWI interrupt flag while((TWSR & 0xF8) != 0xC0); // Wait for the acknowledgement} void TWI_match_write_slave(void) //Function to match the slave address and slave dirction bit(write) {while((TWSR & 0xF8)!= 0xA8) // Loop till correct acknoledgement have been received{// Get acknowlegement, Enable TWI, Clear TWI interrupt flagTWCR=(1<<TWEA)|(1<<TWEN)|(1<<TWINT); while (!(TWCR & (1<<TWINT)));  // Wait for TWINT flag}} void TWI_read_slave(void){// Clear TWI interrupt flag,Get acknowlegement, Enable TWITWCR= (1<<TWINT)|(1<<TWEA)|(1<<TWEN); while (!(TWCR & (1<<TWINT))); // Wait for TWINT flagwhile((TWSR & 0xF8)!=0x80); // Wait for acknowledgementrecv_data=TWDR; // Get value from TWDRPORTB=recv_data; // send the receive value on PORTB} void TWI_match_read_slave(void) //Function to match the slave address and slave dirction bit(read){while((TWSR & 0xF8)!= 0x60)  // Loop till correct acknoledgement have been received{// Get acknowlegement, Enable TWI, Clear TWI interrupt flagTWCR=(1<<TWEA)|(1<<TWEN)|(1<<TWINT);

Page 44: codings related to avr micro controller

while (!(TWCR & (1<<TWINT)));  // Wait for TWINT flag}} // Program to interface serial ADC 0831 with AVR microcontroller (ATMEGA 16)#include<avr/io.h>#include<util/delay.h>#include<inttypes.h> #define DO PD2#define CLK PD1#define CS PD0 #define lcdport PORTA#define rs PB0#define rw PB1#define en PB2 void lcd_init(void);void lcdcmd(unsigned char);void lcddata(unsigned char);void adc_conversion(unsigned char);void twi_init();unsigned char adc_read(); int main(){unsigned char data[12]= "ADC OUTPUT:"; int i=0;unsigned char bits=0; DDRA=0xFF;DDRB=0x07;DDRD=~_BV(DO); //DO pin is input pin and rest of the pins are output pinsDDRC=0xFF;PORTD=0x07;  lcd_init();while(data[i]!='\0'){lcddata(data[i]);_delay_ms(5);i++;

Page 45: codings related to avr micro controller

while(1){ PORTD|=(1<<CS); // high-to-low pulse is provided to CS pin_delay_ms(1);PORTD&=~(1<<CS);_delay_ms(1); 

while(PIND & _BV(DO)) // wait until 0 bit is received{PORTD|=(1<<CLK);_delay_ms(1);PORTD&=~(1<<CLK);_delay_ms(1);} 

PORTD|=(1<<CLK); // a clock pusle is provided_delay_ms(1);PORTD&=~(1<<CLK);_delay_ms(1);  

for(i=0;i<8;i++) {PORTD|=(1<<CLK); // data receive when pulse is high_delay_ms(1);bits=bits<<1; //"bits" is variable to store data.left shift operation is perform to make place for upcoming bit if(bit_is_set(PIND,DO)) // if 1 is received bits |=1; // bits is increment by 1 

PORTD&=~(1<<CLK); // pulse low_delay_ms(1);} 

adc_conversion(bits); //} 

 

} /* this function is written to convert interger value to their corresponding ASCII value*/ void adc_conversion(unsigned char adc_out)  

Page 46: codings related to avr micro controller

{unsigned int adc_out1;int i=0;char position=0xC2; for(i=0;i<=2;i++){adc_out1=adc_out%10;adc_out=adc_out/10;lcdcmd(position);lcddata(48+adc_out1);position--;}} void lcd_init() // fuction for LCD initialization{lcdcmd(0x38);lcdcmd(0x0C);lcdcmd(0x01);lcdcmd(0x06);lcdcmd(0x80);} void lcdcmd(unsigned char cmdout) {lcdport=cmdout;PORTB=(0<<rs)|(0<<rw)|(1<<en);_delay_ms(10);PORTB=(0<<rs)|(0<<rw)|(0<<en);}  void lcddata(unsigned char dataout){lcdport=dataout;PORTB=(1<<rs)|(0<<rw)|(1<<en);_delay_ms(10);PORTB=(1<<rs)|(0<<rw)|(0<<en);} / Program to Generate waveform using AVR Microcontroller (Atmega16) Timers#include<avr/io.h>#include<util/delay.h>#include<avr/interrupt.h>

Page 47: codings related to avr micro controller

void t0_init(void); #define FREQ 12000000 // crsytal freqeuncy#define PRECSALER 8 #define F_OUT 5000 // output frequency#define OCR0_VALUE ((((FREQ/2)/PRECSALER)/F_OUT)-1)  int main(){ t0_init(); // timer initializesei(); // enable global interruptswhile(1);} void t0_init(){// WGM0[1:0]= 10, for CTC mode// COM0[1:0]= 01, to toggle OC0 on compare match// CS0[2:0] =010. for prescaler 8 

TCCR0=(1<<WGM01)|(1<<COM00)|(1<<CS01);DDRB|=(1<<PB3); // select as output pin TIMSK|=(1<<OCIE0); //enable output compare interrupt } ISR(TIMER0_COMP_vect) // interrupt subroutine{OCR0=(uint8_t)OCR0_VALUE; //put OCR value} // Program to use Phase Correct PWM (Pulse Width Modulation) Mode of AVR microcontroller Timer#include<avr/io.h>#include<util/delay.h>#include<avr/interrupt.h> #define FREQ 12000000#define duty_cycle 75 // duty cycle require#define prescaler 1 #define OCR_value ((duty_cycle*256)/100) void t0_pwm_init(void); int main(){ 

Page 48: codings related to avr micro controller

t0_pwm_init(); sei(); // enable global interruptwhile(1); } void t0_pwm_init() //// intiatialize of timer0{// WGM0[1:0]= 01, for Phase Correct PWM mode// COM0[1:0]= 10, to select non inveting mode// CS0[2:0] =001. for no prescaler TCCR0=(1<<WGM00)|(2<<COM00)|(1<<CS00);DDRB|=(1<<PB3); // selcetOC0 as output pinTIMSK|=(1<<OCIE0); //enable Output compare interrupt}   ISR(TIMER0_COMP_vect) // interrupt subroutine{OCR0=(uint8_t)OCR_value; // put OCR value}

 

// Program to rotate servo at the step of 20 degree.#include<avr/io.h>#include<util/delay.h> #define motor PORTD#define servo PD6 void degree(unsigned int ); int main(void){unsigned int degree_value,time;DDRD=0b01000000;for(degree_value=0;degree_value<180;degree_value +=20)for(time=0;time<50;time++){degree(degree_value);}return 0;} 

Page 49: codings related to avr micro controller

void degree(unsigned int k){k=50+(k*10);motor= (1<< servo);_delay_us(k);motor = (0<<servo);_delay_ms(18);} // Program for Serial communication (USART) with different frame size using AVR microcontroller#define FREQ 12000000#include<avr/io.h>#include<util/delay.h>#include<inttypes.h> #define baud 9600#define ubrr_value (((FREQ/16)/baud)-1) void usart_init();void usart_tx(unsigned char);unsigned char usart_rx(void); int main(void){int serial_data; usart_init(); while(1){serial_data=usart_rx();usart_tx(serial_data);} }void usart_init() // USART initialization{ UBRRH=(uint16_t)(ubrr_value>>8); // define UBRR valueUBRRL=(uint16_t) ubrr_value; 

UCSRA=0x00;UCSRB=(1<<TXEN)|(1<<RXEN); //enable both transmission and receptionUCSRC=(1<<URSEL)|(1<<UCSZ0); // Asynchronous mode,frame size=6 bit,No parity, 1 stop bit. } 

Page 50: codings related to avr micro controller

 void usart_tx(unsigned char serial_data)  // funtion for transmit bit{while(!(UCSRA & (1<<UDRE))); // wait for empty UDRUDR=serial_data; // data store in UDR}  unsigned char usart_rx(void){while(!(UCSRA & (1<<RXC)));     //wait until reception completereturn UDR; // receive value from UDR }

 

// Program  to use fast PWM (Pulse Width Modulation) Mode of AVR microcontroller Timer#include<avr/io.h>#include<util/delay.h>#include<avr/interrupt.h> #define FREQ 12000000#define duty_cycle 50 // duty cycle require#define prescaler 8 #define OCR_value ((duty_cycle*256)/100)  //OCR value calculation void t0_pwm_init(void);void t2_fastpwm_init(void); int main(){ t0_pwm_init();  t2_fastpwm_init();sei();while(1);} void t0_pwm_init() // initialization for Phase Correct PWM signal using timer 0{// WGM0[1:0]= 01, for Phase Correct PWM mode// COM0[1:0]= 10, to select non inveting mode// CS0[2:0] =010. for prescaler=8 TCCR0=(1<<WGM00)|(2<<COM00)|(2<<CS00);

Page 51: codings related to avr micro controller

DDRB|=(1<<PB3); // selcet OC0 as output pinTIMSK|=(1<<OCIE0); //enable Output compare interrupt} void t2_fastpwm_init() // initialization for Phase Correct PWM signal using timer 2{ // WGM2[1:0]= 11, for Fast PWM mode// COM2[1:0]= 10, to select non inveting mode// CS2[2:0] =010. for prescaler=8 TCCR2=(1<<WGM20)|(1<<WGM21)|(2<<COM20)|(2<<CS20);DDRD|=(1<<PD7); // selcet OC2 as output pinTIMSK|=(1<<OCIE2); //enable Output compare interrupt} ISR(TIMER0_COMP_vect) // interrupt subroutine{OCR0=OCR_value; // put OCR value} ISR(TIMER2_COMP_vect) // interrupt subroutine{OCR2=OCR_value; // put OCR value} 

FOR SPEED & DIRECTION CONTROL OF AVR

#include <avr/io.h>

#include <util/delay.h>

uint16_t t=50535; // time delay value for delay loop

void keydly() // key debounce delay

{

uint8_t i;

for(i=0;i<7;i++)

_delay_loop_2(10000); // delay loop library function with fixed delay

Page 52: codings related to avr micro controller

}

void incspeed() // increase speed of motor by

{

PORTB = 0x00;

if(t>20535) t=t-2000; // decreasing time delay value if its min limit is

if(t==20535) PORTB = 0x01; // not reached. For min limit give indication on LED3

}

void decspeed() // decrease speed of motor by

{

PORTB = 0x00;

if(t<60535) t=t+2000; // increasing time delay value if its max limit is not

if(t==60535) PORTB = 0x02; // reached. For max limit give indication on LED4

Page 53: codings related to avr micro controller

}

void clockwise() // rotate motor clockwise

{

while(PINA==0xFF) // apply pulse sequence till no button is pressed

{

PORTC=0x01; // apply pulse to 1st coil

_delay_loop_2(t); // generate delay as per time delay value

PORTC=0x02; // apply pulse to next coil and like wise

_delay_loop_2(t);

PORTC=0x04;

_delay_loop_2(t);

PORTC=0x08;

Page 54: codings related to avr micro controller

_delay_loop_2(t);

}

}

void anticlockwise() // rotate motor anti clockwise

{

while(PINA==0x0F) // apply pulse sequence till no button is pressed

{

PORTC=0x08; // apply pulse to last coil

_delay_loop_2(t); // give delay as per time delay value

PORTC=0x04; // apply next pulses as per required sequence

_delay_loop_2(t);

PORTC=0x02;

Page 55: codings related to avr micro controller

_delay_loop_2(t);

PORTC=0x01;

_delay_loop_2(t);

}

}

int main(void)

{

uint8_t r=0,d; // run and direction flag

DDRC=0xFF; / / initialize ports as input and output

DDRB=0x03;

DDRD=0x03;

DDRA=0x00;

PORTC=0x00;

Page 56: codings related to avr micro controller

PORTB=0x00;

PORTD=0x00;

while(PINA==0xFF); // wait till no key is pressed

loop:switch(PINA)

{

case 0xFE: // for 1st key

keydly(); // give key debounce delay

r=1; // set run flag

d=0; // clear direction flag for CLK direction

clockwise(); // start rotating motor clock wise

break;

case 0xFD: // for 2nd key

Page 57: codings related to avr micro controller

keydly(); // key debounce delay

r=1; // set run flag

d=1; // set direction flag for ACLK direction

anticlockwise(); // start rotating motor anticlockwise

break;

case 0xFB: // for 3rd key

PORTC = 0x00; // stop motor

r=0; // clear run flag

PORTB = 0x00; // all indication off

break;

case 0xF7: // for 4th key

Page 58: codings related to avr micro controller

PORTD=0x01; // give indication on LED1

keydly();

PORTD=0x00;

incspeed(); // increase speed

if(r==1) // if motor was running

{

if(d==0) clockwise(); // check direction and

else anticlockwise(); // keep it running

}

break;

case 0xEF: // for 5th key

Page 59: codings related to avr micro controller

PORTD=0x02; // indication on LED2

keydly();

PORTD=0x00;

decspeed(); // decrease speed

if(r==1) // if motor was running

{

if(d==0) clockwise(); // check direction and

else anticlockwise(); // keep it running

}

break;

}

goto loop; // continuous loop

Page 60: codings related to avr micro controller

}

 //Program to get a serial data from RS232 (using HyperTerminal)..// and sending it back to the RS232 (to HyperTerminal).#include<avr/io.h>#include<avr/interrupt.h>

#define USART_BAUDRATE 9600#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

void usart_init();

// --------------------------------------------int main(){

usart_init(); // initialization of USART

sei(); // Enable global interruptfor (;;) // infinite loop{

// Do Nothing}

}

void usart_init(){

UCSRB |= (1<<RXCIE) | (1 << RXEN) | (1 << TXEN); // Turn on the transmission reception ..

// circuitry and receiver interrupt

UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes

UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value..

// into the low byte of the UBRR register

UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value..

// into the high byte of the UBRR register}

Page 61: codings related to avr micro controller

ISR (USART_RXC_vect){

unsigned char value;value = UDR; // Fetch the received byte value

into the variable "value"UDR = value; //Put the value to UDR

}

 

//Program to Display string on LCD using AVR Microcontroller (ATmega16)/*LCD DATA port----PORT Bsignal port------PORT Drs-------PD0rw-------PD1en-------PD2*/ #include<avr/io.h>#include<util/delay.h> #define LCD_DATA PORTB //LCD data port #define ctrl PORTD#define en PD2 // enable signal#define rw PD1 // read/write signal#define rs PD0 // register select signal void LCD_cmd(unsigned char cmd);void init_LCD(void);void LCD_write(unsigned char data); int main(){DDRB=0xff; DDRD=0x07; init_LCD(); // initialization of LCD_delay_ms(50); // delay of 50 mili secondsLCD_write_string("EngineersGarage"); // function to print string on LCDreturn 0;} void init_LCD(void){

Page 62: codings related to avr micro controller

LCD_cmd(0x38); // initialization of 16X2 LCD in 8bit mode_delay_ms(1); LCD_cmd(0x01); // clear LCD_delay_ms(1); LCD_cmd(0x0E); // cursor ON_delay_ms(1); LCD_cmd(0x80); // ---8 go to first line and --0 is for 0th position_delay_ms(1);return;} void LCD_cmd(unsigned char cmd){LCD_DATA=cmd;ctrl =(0<<rs)|(0<<rw)|(1<<en); _delay_ms(1);ctrl =(0<<rs)|(0<<rw)|(0<<en); _delay_ms(50);return;} void LCD_write(unsigned char data){LCD_DATA= data;ctrl = (1<<rs)|(0<<rw)|(1<<en); _delay_ms(1);ctrl = (1<<rs)|(0<<rw)|(0<<en); _delay_ms(50); return ;} void LCD_write_string(unsigned char *str) //store address value of the string in pointer *str{int i=0;while(str[i]!='\0') // loop will go on till the NULL character in the string {LCD_write(str[i]); // sending data on LCD byte by bytei++;  }return;} 

Page 63: codings related to avr micro controller

/ Program to get the 12 byte string and display it on LCD by Polling method:/*The RFID unique code is been displayed on LCELCD DATA port----PORT Bctrl port------PORT D

rs-------PD0rw-------PD1en-------PD2

*/

#define F_CPU 12000000UL

#define USART_BAUDRATE 9600#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

#include<avr/io.h>#include<util/delay.h>

#define LCD_DATA PORTA // LCD data port#define ctrl PORTB#define en PB2 // enable signal#define rw PB1 // read/write signal#define rs PB0 // register select signal

void LCD_cmd(unsigned char cmd);void init_LCD(void);void LCD_write(unsigned char data);

void usart_init();unsigned int usart_getch();

unsigned char i, card[12];void getcard_id(void);void LCD_display(void);

int main(void){

DDRA=0xff; //LCD_DATA port as output portDDRB=0x07; //ctrl as out putinit_LCD(); //initialization of LCDdelay_ms(50); // delay of 50 millisecondsusart_init(); // initiailztion of USARTLCD_write_string("Unique ID No."); //Function to

display string on LCDwhile(1)

Page 64: codings related to avr micro controller

{getcard_id(); // Function to get RFID card no.

from serial portLCD_cmd(0xC0); // to go in second line and

zeroth position on LCDLCD_display(); // a function to write

RFID card no. on LCD}return 0;

}

void getcard_id(void) //Function to get 12 byte ID no. from rfid card{

for(i=0;i<12;i++) {

card[i]= usart_getch(); // receive card value byte by byte

}return;

}

void LCD_display(void) //Function for displaying ID no. on LCD{

for(i=0;i<12;i++){

LCD_write(card[i]);// display card value byte by byte

}return;

}

void init_LCD(void){

LCD_cmd(0x38); //initializtion of 16x2 LCD in 8bit mode

_delay_ms(1);

LCD_cmd(0x01); //clear LCD_delay_ms(1);

LCD_cmd(0x0E); //cursor ON_delay_ms(1);

LCD_cmd(0x80); // ---8 go to first line and --0 is for 0th position

Page 65: codings related to avr micro controller

_delay_ms(1);return;

}

void LCD_cmd(unsigned char cmd){

LCD_DATA=cmd;ctrl =(0<<rs)|(0<<rw)|(1<<en);_delay_ms(1);ctrl =(0<<rs)|(0<<rw)|(0<<en);_delay_ms(50);return;

}

void LCD_write(unsigned char data){

LCD_DATA= data;ctrl = (1<<rs)|(0<<rw)|(1<<en);_delay_ms(1);ctrl = (1<<rs)|(0<<rw)|(0<<en);_delay_ms(50);return ;

}

void usart_init(){

UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry

UCSRC |= (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1);

// Use 8-bit character sizes

UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value..

// into the low byte of the UBRR register

UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value..

// into the high byte of the UBRR register}

unsigned int usart_getch(){

Page 66: codings related to avr micro controller

while ((UCSRA & (1 << RXC)) == 0); // Do nothing until data have been received..

// and is ready to be read from UDR

return(UDR); // return the byte}

void LCD_write_string(unsigned char *str) // take address value of the string in pointer *str{

int i=0;while(str[i]!='\0') // loop will go on till the NULL

characters is soon in string {

LCD_write(str[i]); // sending data on LCD byte by byte

i++;}return;

}

 

// Program to SPI (serial peripheral interface) using AVR microcontroller (ATmega16)#include<avr/io.h>#include<util/delay.h> #define MOSI PB5 void SPI_init();unsigned char SPI_RX(void); int main(){ DDRD=0xFF;PORTD=0x00; SPI_init();  while(1){PORTD=SPI_RX(); // move SPDR value to POTRD} }

Page 67: codings related to avr micro controller

 void SPI_init() //SPI initialization{DDRB=(1<<MOSI); // set MOSI as output pin, rest as inputSPCR=(1<<SPE); // Enable SPI}  unsigned char SPI_RX(){while(!(SPSR &(1<<SPIF))); //wait until SPIF get highreturn SPDR; // return SPDR value} //Program to receive a 12 byte string from RFID and display it on LCD using serial interrupt /*LCD DATA port----PORT Bctrl port------PORT Drs-------PD0rw-------PD1en-------PD2*/ #define F_CPU 12000000UL #include<avr/io.h>#include<util/delay.h>#include<avr/interrupt.h> #define USART_BAUDRATE 9600#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) #define LCD_DATA PORTA // LCD data port#define ctrl PORTB#define en PB2 // enable signal#define rw PB1 // read/write signal#define rs PB0 // register select signal void LCD_cmd(unsigned char cmd);void init_LCD(void);void LCD_write(unsigned char data);void usart_init();unsigned int usart_getch(); void LCD_write_string(unsigned char *str);

Page 68: codings related to avr micro controller

 unsigned char value, i=0; int main(void){DDRA=0xff; // LCD_DATA port as output portDDRB=0x07; // ctrl as out putcli();init_LCD(); // initialization of LCD_delay_ms(50); // delay of 50 mili secondsLCD_write_string("Unique ID No.");LCD_cmd(0xC0); usart_init(); // initialization of USARTsei();while(1);return 0;} void init_LCD(void){LCD_cmd(0x38); // initialization of 16X2 LCD in 8bit mode_delay_ms(1); LCD_cmd(0x01); // clear LCD_delay_ms(1); LCD_cmd(0x0E); // cursor ON_delay_ms(1); LCD_cmd(0x80); // ---8 go to first line and --0 is for 0th position_delay_ms(1);return;} void LCD_cmd(unsigned char cmd){LCD_DATA=cmd;ctrl =(0<<rs)|(0<<rw)|(1<<en); _delay_us(40);ctrl =(0<<rs)|(0<<rw)|(0<<en); return;} void LCD_write(unsigned char data){LCD_DATA= data;

Page 69: codings related to avr micro controller

ctrl = (1<<rs)|(0<<rw)|(1<<en);_delay_us(40);ctrl = (1<<rs)|(0<<rw)|(0<<en);_delay_us(50); //delay to get things executedreturn ;} void usart_init(){UCSRB |= (1<<RXCIE) | (1 << RXEN) | (1 << TXEN);   // Turn on the transmission and..// reception circuitryUCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizesUBRRL = BAUD_PRESCALE;  // Load lower 8-bits of the baud rate value.. // into the low byte of the UBRR registerUBRRH = (BAUD_PRESCALE >> 8);  // Load upper 8-bits of the baud rate value.. // into the high byte of the UBRR register} void LCD_write_string(unsigned char *str) //take address vaue of the string in pionter *str{int i=0;while(str[i]!='\0') // loop will go on till the NULL charaters is soon in string {LCD_write(str[i]); // sending data on CD byte by bytei++;}return;} ISR (USART_RXC_vect){i++;if(i==13){i=1;LCD_cmd(0xC0);}value = UDR;  // Put the received byte value into the variable "value"LCD_write(value);    // write receive data to LCD}  

Page 70: codings related to avr micro controller