AVR (atmega) Example and project

Solutions for AVR Examples and Projects

***************************************************
//Example: 1 LED Blink
***************************************************

#include <mega8535.h>
#include <delay.h>
void main(void)
{
PORTB=0x00;
DDRB = 0x80;
while (1)
      {
            PORTB.7 = 0x1;
            delay_ms(500);
            PORTB.7 = 0x0;
            delay_ms(500);
      };
}

***************************************************
//Example: 2 LED Blink
***************************************************

#include <mega8535.h>
#include <delay.h>


void main(void)
{

 int  j=100;
DDRB=0xC0;


while (1)
      { 
   
       
     PORTB=0x80;
     delay_ms(j);   
     PORTB=0x40;
     delay_ms(j);
     j--;

     };
}

**********************************************************
//Example: 3 LED Blink left to right & Right to Left
**********************************************************

#include<mega8535.h>
#include<delay.h>

void main (void)

 while(1)
 {
int i; 
int j;
DDRB=0Xff;
i=0xff;
PORTB=0xff;
for(j=0;j<=7;j++)
{
 PORTB=i;
 i<<=1;
 delay_ms(500);
}
for(j=0;j<=7;j++)
{
 PORTB=i;
 i>>=1;
 delay_ms(500); 
 }
}
 }

**********************************************************
//Example: 4 Read Key Press
**********************************************************

#include <mega8535.h>


void main(void)
{
DDRB=0x01;
DDRD=0x00;



while(1)

PORTB.0=PIND.2;

// PORTB=PIND>>2;

      if(PIND.4==0)
      {
      PORTB.1=1;
      }
else
      if(PIND.3==0)
      {
      PORTB.2=1;
      }
}

**********************************************************
//Example: 5 Read Key Press display array
**********************************************************

#include <mega8535.h>


void main(void)
{
int a[3]={1,1,1};
int i;
DDRB=0x0f;
DDRD=0x00;

while(1)
{

      if(PIND.2==0)
      {
     
      PORTB.0=a[0];
      }
else if
      (PIND.3==0)
      {
      PORTB.1=a[1];
      }
else if
      (PIND.4==0)
      {
      PORTB.2=a[2];
      }
  };   
}


**********************************************************
//Example: 6 Stepper motor & DC motor
**********************************************************

#include <mega8535.h>
#include <delay.h>

void main(void)
{

PORTA=0x00;
DDRA=0xff;

PORTB=0x00;
DDRB=0xff;

PORTD=0x00;
DDRD=0xff;

PORTD.7=0x1; //Enable


      PORTA.3=0x0;
      PORTA.2=0x1;      //DC Motor


while (1)
      {
      PORTB=0x01; //Step-1
      delay_ms(20);     
           
      PORTB=0x02; //Step-2
      delay_ms(20);
                   
      PORTB=0x04; //Step-3
      delay_ms(20);
     
      PORTB=0x08; //Step-4
      delay_ms(20);   
   
     
      };
}

**********************************************************
//Example: 7 LCD 16x2 Interface
**********************************************************

#asm
.equ __lcd_port=0x15 ;PORTC
#endasm

#include <mega8535.h>
#include <lcd.h>

void main(void)
{

lcd_init(16);
lcd_gotoxy(0,0);
lcd_putsf(" Dr. Y. S. Rao");

while (1);
}

**********************************************************
//Example: 8 LCD 16x2  Display right to left
**********************************************************

#asm
.equ __lcd_port=0x15 ;PORTC
#endasm

#include <mega8535.h>
#include <lcd.h>
#include<delay.h>
void main(void)
{
int i=0;
lcd_init(16);

while(1)
{
for(i=16; i>=0; i--)
{
        lcd_clear();
lcd_gotoxy(i,0);
lcd_putsf(" Dr. Y. S. Rao");
delay_ms(250);
}
};
}

**********************************************************
//Example: 9 LCD 16x2  Display 00 to 99
**********************************************************

#asm
.equ __lcd_port=0x15 ;PORTC
#endasm

#include <mega8535.h>
#include <lcd.h>
#include<delay.h>

void main(void)
{                               
int a,b; 
while(1)
{
 
lcd_init(16);

for(b=48;b<=57;b++)
{

for(a=48;a<=57;a++)
{     
               
if(b>48)
{
lcd_gotoxy(6,0);
lcd_putchar((char)b);
}

lcd_gotoxy(7,0);
lcd_putchar((char)a);
delay_ms(500);
lcd_clear(); 
}}}}

**********************************************************
//Example: 10 Triangle wave Generator
**********************************************************

#include <mega8535.h>
#include <delay.h>
#include<math.h>


void main(void)
{

 int count=0x00;
 float amp=0x0f,ap=0;
DDRD=0xFF;


while (1)
      { 
 
  while(ap<15)
  {
  PORTD=0x0f+amp*ap; 
  ap=ap+0.1;
  }

  while(ap>0)
  {
  PORTD=0x0f+amp*ap; 
  ap=ap-0.1;
  }

 
     }
}

**********************************************************
//Example: 11 Sine wave Generator
**********************************************************

#include <mega8535.h>
#include <delay.h>
#include<math.h>


void main(void)
{

 int count=0x00;
 float pi=0,amp=0x3f,amp1=0x01;
DDRD=0xFF;


while (1)
      { 
 
  while(pi<3.14)
  {
  PORTD=0x3f+amp*sin(2*pi); 
  pi=pi+0.1;
  }

  while(pi>0)
  {
  PORTD=0x3f-amp*sin(2*pi);
  pi=pi-0.1;
  }
 
 
     }

***************************************************
//Example: 12  INT-0 interrupt
***************************************************

#include <mega8535.h>
interrupt [EXT_INT0] void ext_int0_isr(void)
{
PORTB = PORTB ^ 0x01;
}
void main(void)
{
DDRB=0x01;

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: Off
// INT2: Off
GICR|=0x40;
MCUCR=0x00;
MCUCSR=0x00;
GIFR=0x40;
#asm("sei")

while(1) ;   
}

***************************************************
//Example: 13  LCD Count with INT-0 and compare
***************************************************

#include <lcd.h>
#include <delay.h>
#include <mega8535.h>

unsigned int timecount = 0;
static volatile int counter_units = 0x30;
static volatile int counter_tens = 0x30;

#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm

interrupt [EXT_INT0] void ext_int0_isr(void)
{
PORTB.0 = 0x1;

if(++timecount == 5)
{
        if(counter_tens>0x39)
        {
           counter_tens=0x30;
           counter_units=0x30;
         }
        delay_ms(100);
        timecount = 0;
        lcd_clear();
       lcd_gotoxy(0,0);
        lcd_putsf("Count =>>");
        lcd_gotoxy(1,1);
        lcd_putchar((char)counter_units);
        lcd_gotoxy(0,1);
        lcd_putchar((char)counter_tens);
        counter_units++;
        if(counter_units>0x39)
        {
           counter_units=0x30;
           counter_tens++;
        } 
}                         
}

void main(void)
{


DDRB=0x0f;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: Off
// INT2: Off
GICR|=0x40;
MCUCR=0x00; //00
MCUCSR=0x00;
GIFR=0x40;

 
lcd_init(16);
lcd_clear();
#asm("sei")

while (1)
      {
      PORTB.0 = 0x0;
      if(counter_units==0x31 && counter_tens==0x31)
      {
      PORTB.2=0x01;
       }
      };
}

***************************************************
//Example: 14  INT-0, INT-1 interrupt  Priority
***************************************************

#include <mega8535.h>
interrupt [EXT_INT1] void ext_int1_isr(void)
{
PORTB = PORTB ^ 0x01;


interrupt [EXT_INT0] void ext_int0_isr(void)
{
PORTB = PORTB ^ 0x02;
}

void main(void)
{

DDRB=0x03;

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: On
// INT1 Mode: Low level
// INT2: Off
GICR|=0xC0;
MCUCR=0x00;
MCUCSR=0x00;
GIFR=0xC0;

#asm("sei")

while(1)
   ;   
}

***********************************************************
//Example: 15  INT-0, INT-1, Timer 0  interrupt  Priority
***********************************************************

#include <mega8535.h>
unsigned int timecount = 0;
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
TCNT0=6;
if(++timecount == 1000)
{
PORTB = PORTB ^ 0x04;
timecount = 0;
}
}
interrupt [EXT_INT1] void ext_int1_isr(void)
{
PORTB = PORTB ^ 0x02;


interrupt [EXT_INT0] void ext_int0_isr(void)
{
PORTB = PORTDB ^ 0x01;
}

void main(void)
{
DDRB=0x07;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x01; // Clock value: 8000.000 kHz
TCNT0=0x00;
OCR0=0x00;

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: On
// INT1 Mode: Low level
// INT2: Off
GICR|=0xC0;
MCUCR=0x00;
MCUCSR=0x00;
GIFR=0xC0;

TIMSK=0x01; // Timer(s)/Counter(s) Interrupt(s) initialization

#asm("sei")

while(1) ; 
}

***********************************************************
//Example: 16  Timer 1   Input Capture (ICP)
***********************************************************

#include <mega8535.h>
#define pulse_out PORTB
#define icp PIND.6

unsigned char ov_counter;                       //counter for timer 1 overflow
unsigned int rising_edge,falling_edge;          // storage for times
unsigned long pulse_clocks;                     //storage for actual clock counts in the pulse

interrupt [TIM1_OVF] void timer1_ovf_isr(void)  //timer 1
{
ov_counter ++;                                  //increment counter when overflow occurs
}
interrupt[TIM1_CAPT] void tiemr1_capt_isr(void) //timer 1 input
{

if(icp)
{
rising_edge=ICR1L;                               //save start time for pulse
TCCR1B=TCCR1B & 0xBF;                           //set to trigger on falling edge next
ov_counter =0;                                  // clear overflow counter for this measurement
}
else{
falling_edge=ICR1L;                              //save falling edge time
TCCR1B=TCCR1B |0X40;                            //set rising edge
pulse_clocks=(unsigned long)falling_edge-(unsigned long)rising_edge +(unsigned long)ov_counter*0x10000;
pulse_out=pulse_clocks/500;                     //output milliseconds to PORTC
}
}


void main(void)
{
DDRB=0XFF;                                      //set port c for output
TCCR1B=0XC2;                                    //timer 1 input to clock/8
TIMSK=0X24;                                     //unmask timer overflow and capture interrupts
#asm("sei")                                     //enable global interrupt bit
while(1)
;                                               //DO NOTHING
}

****************************************************************
//Example: 17 Timer 1 output compare-A interrupt service routine
****************************************************************


#include <mega8535.h>

interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
OCR1A=OCR1A+25; //set to next match (toggle) point
}

void main(void)
{
  DDRD=0x20;         // set OC1A  bit for output

  TCCR1A=0x40;         // set prescaler to clock/8 (1 us clocks)
  TCCR1B=0x02;         //enable output compare mode to toggle OC1A pin on match
  TIMSK=0x10;         //unmask output compare match interrupt for register A
#asm("sei") //set global interrupt bit

while(1)
;// do nothing
}


****************************************************************
//Example: 18 Timer 1 Output Compare PWM
****************************************************************

#include <mega8535.h>
#define PWM_select (PINC &3)

void main(void)
{
unsigned int olddat; 
PORTC=0x03;                     //enable the internal pull-ups for in the input bits
TCCR1A=0x91;                    //compare A fdor non-inverted PWM and 8 bit resoultion
TCCR1B=0x02;                    //clock /8 prescaler
while(1)
{
 if (PWM_select !=olddat)
{
  olddat=PWM_select;            // save toggle switch value

{
  switch (PWM_select)
  {
        case 0:
                OCR1A=25;       //10 %duty cycle desired
        break;
        case 1:
                OCR1A= 51;      // 20% duty cycle desired
        break;
        case 2:
                OCR1A= 76;      //30 % duty cycle desired
        break;
       
        case 3:
                OCR1A=102;      // 40% duty cycle desired
        break;
}
} }}}


***********************************************************
//Example: 19  Watchdog Timer
***********************************************************

#include <mega8535.h>

void main(void)
{
DDRD=0x00;
DDRB=0x0F;

WDTCR = 0xB; //enable WDT and set to 120 ms timeout
while(1)
{
#asm("wdr")    //reset the watchdog timer
 
if(PIND.2==0) //if true disable the WDT
      {
        WDTCR = 0x18; //set both WDE and WDTOE
        WDTCR = 0x00; // clear WDE
        PORTB.1=1;
      }
{

PORTB.2=0x1;

}    }  }
***********************************************************
//Example: 20  UART by Polling display on PC terminal
***********************************************************

#include <mega8535.h>
#include <stdio.h>

int i;
char a;

    void main(void)
    { 
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous

UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33; // USART Baud Rate: 9600
         

       printf("Embedded Systems by Dr. Y S Rao");
       scanf("%d",&i);
       if(i==1)
       {
        printf("one is entered");
       }       

  while(1)
  {
       scanf("%c",&a);
       switch(a)
       {
        case 'a':
        printf("A is entered");
        break;
       
        case 'b':
        printf("B is entered");
        break;       
   
        }     
        }}

***********************************************************
//Example: 21-a  UART FLASH memory and PC terminal display 
***********************************************************

#include <mega8535.h>
unsigned char qcntr,sndcntr,ch;
unsigned char queue[70];
flash char * flash msg[3]={"that was a","that was b","that was c"};

interrupt [USART_TXC] void usart_tx_isr(void)
{
   if (qcntr != sndcntr) UDR=queue[sndcntr++];
 }
 void sendmsg (flash char *s)
   {
    qcntr=0;
    sndcntr=1;
    queue[qcntr++]=0x0d;
    queue[qcntr++]=0x0a;
    while (*s)queue[qcntr++]=*s++;
    UDR=queue[0];
    }
    void main(void)
    {

// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous

UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33; // USART Baud Rate: 9600
         

       #asm("sei")
       while (1)
       {
         if (UCSRA & 0x80)
         {
           ch=UDR;
           switch(ch)
           {
             case 'a':
               sendmsg(msg[0]);
               break;
             case 'b':
                sendmsg(msg[1]);
                 break; 
             case 'c':
                sendmsg(msg[2]);
                 break; 

         
               }}}               }

***********************************************************
//Example: 21-b  UART Flash/RAM and PC terminal display 
***********************************************************

#include <mega8535.h>
unsigned char qcntr,sndcntr,ch;
unsigned char queue[70];
flash char * flash msg1[15]={"that was a"};
flash char * flash msg2[15]={"that was b"};
flash char * flash msg3[15]={"that was c"};
//char msg2[]={"that was b"};
//char msg3[]={"that was neither b nor a"};

flash char * flash msg4[30]={"that was neither b nor a nor c"};
interrupt [USART_TXC] void usart_tx_isr(void)
{
   if (qcntr != sndcntr) UDR=queue[sndcntr++];
 }
 void sendmsg (flash char *s)
   {
    qcntr=0;
    sndcntr=1;
    queue[qcntr++]=0x0d;
    queue[qcntr++]=0x0a;
    while (*s)queue[qcntr++]=*s++;
    UDR=queue[0];
    }
    void main(void)
    {

// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous

UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;            // USART Baud Rate: 9600
   
   #asm("sei")
       while (1)
       {
         if (UCSRA & 0x80)
         {
           ch=UDR;
           switch(ch)
           {
             case 'a':
               sendmsg(msg1[0]);
               break;
             case 'b':
                sendmsg(msg2[0]);
                 break; 
             case 'c':
                sendmsg(msg3[0]);
                 break; 

            default:
                 sendmsg(msg4[0]);
               }}} }

***********************************************************
//Example: 22  UART polling data display string on LCD
***********************************************************

#asm
   .equ __lcd_port=0x15
#endasm           
#include <mega8535.h>
#include <lcd.h>
#include <stdio.h>
#include <string.h>
#include <DELAY.h>


char *a;
int y;   


void main(void)
{     

// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous

UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33; // USART Baud Rate: 9600
           
lcd_init(16);
lcd_clear();


while (1)
      {
       
lcd_gotoxy(0,1);
        gets(a,16); 
      y=strlen(a);
      printf("%d",y);
    lcd_puts(a);   
               
      }
}

***********************************************************
//Example: 23  UART INTO
***********************************************************

#include <mega8535.h>
flash char s[6]="Dr. Y S Rao";


interrupt [EXT_INT0] void ext_int0_isr(void)
{

PORTB=PORTB^0x1; //toggle the LSB of port B
putsf("s");
//putsf("Dr. Y S Rao");
}

#include <stdio.h>

void main(void)
{
PORTB=0x00;
DDRB=0x01;

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Falling Edge
// INT1: Off
// INT2: Off
GICR|=0x40;
MCUCR=0x02;
MCUCSR=0x00;
GIFR=0x40;

// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x08;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;


// Global enable interrupts
#asm("sei")

while (1)
      ;
}

***********************************************************
//Example: 24  ADC data on PORTB
***********************************************************

#include <mega8535.h>
#include <delay.h>

#define ADC_VREF_TYPE 0x00
 interrupt [ADC_INT] void adc_isr(void)
{

PORTB=(unsigned char) ~(ADCW>>2);

delay_ms(20);

ADCSRA|=0x40;
}

void main(void)
{

PORTB=0xFF; // all outputs
DDRB=0x00;  // all LEDs are initially off

// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AREF pin
// ADC High Speed Mode: Off
// ADC Auto Trigger Source: None
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x8B;
SFIOR&=0xEF;

ADCSRA|=0x40;

#asm("sei")


while (1);
}
***********************************************************
//Example: 25  ADC LED LCD
***********************************************************
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm

#include <90s8535.h>
#include <delay.h>
#include <lcd.h>

#define ADC_VREF_TYPE 0x00
 interrupt [ADC_INT] void adc_isr(void)
{
DDRB=0X0F;
PORTB=(unsigned char) ~(ADCW>>4);
DDRB=0x00;
lcd_init(16);
lcd_gotoxy(3,0);
 if(PINB.0==0x1)
 {
 lcd_putchar(49);
 }
 else if(PINB.0==0)
 {
  lcd_putchar(48);
  }
  lcd_gotoxy(2,0);
   if(PINB.1==0x1)
 {
 lcd_putchar(49);
 }
 else if(PINB.1==0)
 {
  lcd_putchar(48);
  }
  lcd_gotoxy(1,0);
   if(PINB.2==0x1)
 {
 lcd_putchar(49);
 }
 else if(PINB.2==0)
 {
  lcd_putchar(48);
  }
  lcd_gotoxy(0,0);
   if(PINB.3==0x1)
 {
 lcd_putchar(49);
 }
 else if(PINB.3==0)
 {
  lcd_putchar(48);
  }
 

delay_ms(2000);

ADCSR|=0x40;
}

void main(void)
{
 
 PORTB=0xFF; // all outputs
 DDRB =0xFF;  // all LEDs are initially off
ADCSR=0x8E;
#asm("sei")
ADMUX=0;
ADCSR|=0x40;
while (1);
{
}
}

***********************************************************
//Example: 26  ADC peak value  on LCD
***********************************************************

#asm
.equ __lcd_port=0x15 ;PORTC
#endasm

#include <lcd.h>
#include <mega8535.h>
#include <delay.h>
#include <stdlib.h>
#include <math.h>   
#define ADC_VREF_TYPE 0x40
 
  unsigned char s;//to pass analog input value
  float peak=0,temp,arr[50];
  int i=1; 
   char lcd_volt[7];
 

float read_adc(unsigned char adc_input)
{
delay_ms(10);
ADCSRA|=0x40;

while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}

 
void main(void)
{
 
 ADCSRA=0x85;
 
#asm("sei")
lcd_init(16);
 
  while(1){
 
lcd_clear(); 
ADMUX=0x00;
while(i<50)

arr[i++]= read_adc(s);           

}       
 
 for(i=1;i<50;i++)
{             
if(arr[i]>peak)
peak=arr[i];
}

ftoa(peak,2,lcd_volt); // Float to character conversion upto 2 decimal places..
lcd_gotoxy(0,0);
lcd_putsf("peak voltage =>>");
lcd_gotoxy(0,1);
lcd_puts(lcd_volt); //lcd_volt is array
delay_ms(200);
i=0;
peak=0;
 
    }   
   }   

***********************************************************
//Example:27   ADC data on LCD and PWM control
***********************************************************

#asm
.equ __lcd_port=0x15 ;PORTC
#endasm

#include <lcd.h>
#include <mega8535.h>
#include <delay.h>
#include <stdlib.h>
#include <math.h>   
#define ADC_VREF_TYPE 0x40
 
  unsigned char s;
  float adc_data; 
   char lcd_volt[7]; 
  float volt_disp; 


float read_adc(unsigned char adc_input)
{

ADCSRA|=0x40;

while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}



void main(void)
{
 
 ADCSRA=0x85;
 
#asm("sei")
lcd_init(16);

xx: 
lcd_clear(); 
ADMUX=0x00;             
adc_data= read_adc(s); //adc_data= 0x3ff;
 
volt_disp=adc_data; 
ftoa(volt_disp,2,lcd_volt);// Float to character conversion upto 2 decimal places..
lcd_gotoxy(0,0);
lcd_putsf("Temperature =>>");
lcd_gotoxy(0,1);
lcd_puts(lcd_volt); //lcd_volt is array
delay_ms(1000);
     
 
 if ((adc_data>(0.75*1020)/5) && (adc_data<(0.8*1020)/5))
       {OCR2 = 0xf5;}        //PWM Duty cycle

      else if ((adc_data>(0.7*1020)/5) && (adc_data<(0.75*1020)/5))//(adc_data<(3*1020)/5)
       {OCR2 = 0xe5;}        //PWM Duty cycle
       
     else if ((adc_data>(0.65*1020)/5) && (adc_data<(0.7*1020)/5))//(adc_data<(2*1020)/5)
       {OCR2 = 0xb5;}        //PWM Duty cycle
           
      else  OCR2 = 0x05;        //PWM Duty cycle
   
     TCCR2=0x69;
     delay_ms(1000); 
     lcd_clear(); 
     
goto xx;   

while (1);


}

***********************************************************
//Example: 28  ADC data on UART PC Terminal
***********************************************************

#include <mega8535.h>
#include <math.h>
#include <stdio.h>
#include <delay.h>

unsigned int snd;

#define ADC_VREF_TYPE 0x40

int read_adc(unsigned char adc_input)
{
ADMUX=adc_input;
ADCSRA|=0x40;

while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}

void main(void)
{
char ch;
unsigned char mux;

UCSRA=0x00;
UCSRB=0xD8;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
ADCSRA=0x86;
#asm("sei")

while(1)

ch=UDR;
if(ch=='a' || ch=='b') 
{
     switch(ch)
     {         
case 'a':     
mux=0;
        break;
     
        case 'b':     
        mux=1;
        break;     
     }
               
   snd=read_adc(mux);     
   printf("-%d",snd);
 
   delay_ms(2000);
  }
  else
  {   
  ch=' ';
      continue;
 
  }}}

***********************************************************
//Example: 29 analog comparator
***********************************************************

#include <mega8535.h>
 interrupt [ANA_COMP] void ana_comp_isr(void){ 
   PORTB.1=0;   //light the LED
}

void main()
{
DDRB=0x02;      //set bit 1 for output
PORTB=0x02;     //start with LED off

ACSR=0x0A;      //enable analog comp, interrupt ,falling edge
#asm("sei")     // set global interrupt enable bit
while(1)
;               //do nothing
}

***********************************************************
//Example: 30 SPI data Transmission
***********************************************************

#include <mega8535.h>
#define SPI_output_data PORTC
#define SPI_input_data PIND
 
interrupt [SPI_STC] void spi_isr()

SPI_output_data=SPDR;                   //read out new data received
SPDR=SPI_input_data;                    // load new data to start SPI transmission
                       
}

void main()
{
PORTB=0X40;                             //PULL UP ON MISO
DDRB=0XB0;                              //SCLK,MOSI , SS=OUTPUT
DDRC=0XFF;                              //ALL OP
PORTD=0XFF;                             //PULL UPS FOR DIP SWITCH
SPCR=0XD0;                              //ENABLE SPI,AND ITS INTERRUPT
 

#asm
in r30,spsr                             //SPI Interrupt enable
in r30,spdr
#endasm

#asm("sei")                             //global enable interrupts

SPDR = 0x00;                            //start SPI communication
while(1)
;
}


***********************************************************
//Example: 31 Design a library
***********************************************************

#include <mega8535.h>
#asm
   .equ __lcd_port=0x15
#endasm
#include <lcd.h>
#include <ysrlib.c>
#include <delay.h>
#include <stdlib.h>

int a, b, result;
char ch[7];

void main(void)
{
ACSR=0x80;
SFIOR=0x00;

lcd_init(16);

while (1)
      {
      lcd_clear();
     
      a = 10;
      b = 5;
     
      lcd_gotoxy(0, 0);
      result = sum(a, b);
      lcd_putsf("SUM(10, 5)");
      lcd_gotoxy(0, 1);
      itoa(result, ch);
      lcd_puts(ch);
      delay_ms(1000);
     
      lcd_clear();
      lcd_gotoxy(0, 0);
      result = minus(a, b);
      lcd_putsf("MINUS(10, 5)");
      lcd_gotoxy(0, 1);
      itoa(result, ch);
      lcd_puts(ch);
      delay_ms(1000);
     
      lcd_clear();
      lcd_gotoxy(0, 0);
      result = div(a, b);
      lcd_putsf("div(10, 5)");
      lcd_gotoxy(0, 1);
      itoa(result, ch);
      lcd_puts(ch);
      delay_ms(1000);
     
      lcd_clear();
      lcd_gotoxy(0, 0);
      result = mul(a, b);
      lcd_putsf("MUL(10, 5)");
      lcd_gotoxy(0, 1);
      itoa(result, ch);
      lcd_puts(ch);
      };
}


//----library copy it in inc folder       ysrlib.c or ysrlib.h


int sum(int a, int b) {
return a + b;
}

int minus(int a, int b) {
return a - b;
}

int div(int a, int b) {
return a / b;
}

int mul(int a, int b) {
return a * b;
}


***************************************************
//Project: 1 Bluetooth Initialization
***************************************************

 DDRD.5=0x1;

 PORTD.5=0x0;  // Hardware Initialization
 delay_ms(1000);
 PORTD.5=0x1; 
 delay_ms(1000);
 
 
printf("AT+UARTCONFIG,9600,N,1,0 \r");
printf("ATS10=0 \r");
printf("AT+BTMODE,3 \r");
printf("AT+BTSCAN \r"); 

***************************************************
// Project: 2 Bluetooth as a master
***************************************************

DDRC.3=0x1;     //Reset to Bluetooth

PORTC.3=0x0;
delay_ms(2000);   
PORTC.3=0x1;
delay_ms(2000);   
           

       
printf("AT+UARTCONFIG,9600,N,1,0 \r");
printf("ATS10=0 \r");
printf("ATD000195091DEE \r");  // Slave ESD Number
delay_ms(2000);
     
DDRA.0=0x1;
DDRA.2=0X0;
DDRA.6=0X1;
DDRA.7=0X1;

       
        lcd_clear();
        lcd_putsf("Started ");
        PORTA.6=0X1;
        PORTA.7=0X0;
        delay_ms(2000);
        if (PINA.2==0)  //Status Pin
        {
        PORTA.6=0X0;
        PORTA.7=0X1;
        lcd_clear();
        lcd_putsf("Master sending ");
        printf("b");

***************************************************
// Project: 3 Bluetooth as a slave
***************************************************

DDRC.3=0x1;      //Reset to Bluetooth
PORTC.3=0x0;
delay_ms(2000);   
PORTC.3=0x1;
delay_ms(2000);
           

printf("AT+UARTCONFIG,9600,N,1,0 \r");
printf("ATS10=0 \r");
printf("AT+BTMODE,3 \r");
printf("AT+BTSCAN \r");
delay_ms(2000);

DDRA.0=0X1;
DDRA.2=0X0;
DDRA.6=0X1;
DDRA.7=0X1;

 
 lcd_clear();
 lcd_putsf("Sl start ");
 PORTA.6=0X1;
 PORTA.7=0x0;
 delay_ms(2000);
 if(PINA.2==0)
 {
 PORTA.6=0X0;
 PORTA.7=0X1;
 lcd_clear();
 lcd_putsf("Sl Receving ");
 
 }
while (1)
      {
       scanf("%c",&a);
       switch(a)
       {
        case 'a':
        lcd_putsf("A is entered");
        break;
       
        case 'b':
        lcd_putsf("B is entered");
        break; 
       
   
        }

****************************************************
//Project: 4 Program to send a SMS using GSM Modem
****************************************************

#include <mega8535.h>
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <delay.h>

void main(void)
{
unsigned char a[50],b[50];//,c[16],d[16],e[16],temp1[100], strlength[2], temp2[16];
unsigned char str0[]="AT";
unsigned char str1[]="OK";
unsigned char str2[]={'A','T','+','C','M','G','S','=','"','9','8','2','0','9','6','2','8','7','0','"',';'};
unsigned char str3[]={'Y','S','R','A','O',0x1A};

PORTA=0x00;
DDRA=0x0F;


UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;

lcd_init(16);

puts(str0); //Cheecking if AT commands are working
putchar(0x0D); //Enter
putchar(0x0A); //Line feed

gets(a,16); //Input from UDR (carriage return)
gets(b,16); //Input from UDR (OK)
delay_ms(1000);
lcd_gotoxy(0,0);
lcd_puts(str0);
lcd_gotoxy(0,1);
lcd_puts(a); //Display the response on LCD
delay_ms(5000);
lcd_gotoxy(0,1);
lcd_puts(b); //Display the response on LCD
delay_ms(5000);
lcd_clear();

if(strncmp(b,str1,2)==0)
{
puts(str2);
putchar(0x0D); //Enter
putchar(0x0A); //Line feed
delay_ms(1000);
puts(str3);


lcd_puts(str3);
};
}


******************************************************
// Project: 5 Program to call a person using GSM
******************************************************

#include <mega8535.h>

#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <delay.h>
// Declare your global variables here

void main(void)
{
unsigned char a[50];

unsigned char str0[]="AT";

unsigned char str1[]={'a','t','d',' ','9','8','2','0','9','6','2','8','7','0',';','\0'};

PORTA=0x00;
DDRA=0x0F;

UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;

lcd_init(16);

puts(str0); //Cheecking if AT commands are working
putchar(0x0D); //Enter
putchar(0x0A); //Line feed

gets(a,16); //Input from UDR (carriage return)
gets(a,16); //Input from UDR (OK)


lcd_gotoxy(0,0);
lcd_puts(str0);
lcd_gotoxy(0,1);
lcd_puts(a); //Display the response on LCD
delay_ms(5000);
lcd_clear();


puts(str1);
putchar(0x0D); //Enter
putchar(0x0A); //Line feed

gets(a,50);
gets(a,50);

lcd_gotoxy(0,0);
lcd_puts(str1);
lcd_gotoxy(0,1);
lcd_puts(a); //Display the response on LCD
delay_ms(5000);

}


**************************************************************************
// Project: 6 Program to receive SMS through GSM and compare with string
**************************************************************************

#asm
   .equ __lcd_port=0x15
#endasm

#include <mega8535.h>
#include <lcd.h>
#include <stdio.h>
#include <string.h>
#include <delay.h>
#include <stdlib.h>

void main(void)
{     
unsigned char a[16],b[16],c[16],d[16],e[16],temp1[100], strlength[2], temp2[16];
int i,length;
unsigned char str0[] = "AT";
unsigned char str1[]="ATE0"; //Echo off
unsigned char str2[]="OK";
unsigned char str3[]="+CMTI:"; //Indication of new mesg 
unsigned char Msg[]="ysrao"; //String
unsigned char str5[]="AT+CMGR=1"; //To read the sms
unsigned char str6[]="AT+CMGF=1"; //To set text mode   
unsigned char str7[]="AT+CMGD=1";

for(i=0;i<=100;i++)
{
    a[i]=b[i]=c[i]=d[i]=temp1[i]=temp2[i]=0;
}

PORTA=0x00;
DDRA=0x01;

PORTB=0x00;
DDRB=0x00;

PORTC=0x00;
DDRC=0x00;

PORTD=0x00;
DDRD=0x00;


UCSRA=0x00;
UCSRB=0x18;                         
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;

lcd_init(16);
while(1)
{
PORTA=0x00; 
puts(str0); //Cheecking if AT commands are working
putchar(0x0D); //Enter
putchar(0x0A); //Line feed
gets(a,16); //Input from UDR (carriage return)
gets(a,16); //Input from UDR (OK)
lcd_gotoxy(0,0);
lcd_puts(str0);
lcd_gotoxy(0,1);
lcd_puts(a); //Display the response on LCD
delay_ms(10000);
lcd_clear();

if(strncmp(a,str2,2)==0)

lcd_clear();
puts(str1); //Echo off
putchar(0x0D); //Enter
putchar(0x0A); //Line feed
gets(b,16); //Input from UDR (carriage return)
gets(b,16); //Input from UDR (OK)
lcd_gotoxy(0,0);   
lcd_puts(str1);
lcd_gotoxy(0,1);
lcd_puts(b); //Display the response on LCD
delay_ms(10000);
lcd_clear();

puts(str6);             
putchar(0x0D); //Enter
putchar(0x0A); //Line feed
gets(d,16);
gets(d,16);
lcd_puts(str6);
lcd_gotoxy(0,1) ;
lcd_puts(d);
delay_ms(10000);
lcd_clear();

 while(1)
 {
    PORTA=0x00;
    lcd_clear();
    lcd_gotoxy(0,0);
    lcd_putsf("Waiting... ");
   
    gets(c,16); //Input from UDR (carriage return)
        gets(c,16); //Waiting for the sms 
     
     lcd_clear();
     lcd_gotoxy(0,1); 
     delay_ms(1000);
     lcd_puts(c);
     delay_ms(1000);
     lcd_clear();
     lcd_gotoxy(0,0);
   
     if(strncmp(c,str3,6)==0)
       {
        lcd_clear();
        puts(str5);
          putchar(0x0D); //Enter 
          gets(e,16);
          gets(temp1,100); //Input from UDR (carriage return)
          gets(temp2,16); //The Message
         lcd_puts(str5);
         delay_ms(10000);
         lcd_clear();
        lcd_putsf("Reading Message...");
           delay_ms(10000);
        lcd_clear();
        length=strlen(temp2);
        itoa(length,strlength);
        lcd_puts(strlength);
        delay_ms(10000);
        lcd_clear();
        for(i=0;i<strlen(temp2);i++)
        {
            lcd_putchar(temp2[i]);
        }
        delay_ms(5000);
        lcd_clear();
        if(strncmp(temp2,Msg,strlen(Msg))==0 && strlen(temp2)==(strlen(Msg)+1))
        {
             PORTA=0x01;
             lcd_putsf("Correct message");
             delay_ms(1000);
        }
        else
        {
              lcd_putsf("wrong message");
              delay_ms(1000);
        }
       
       
puts(str7); //Delete the sms
putchar(0x0D); //Enter
putchar(0x0A); //Line feed
gets(a,16);
gets(a,16);
lcd_clear();
delay_ms(500);
lcd_gotoxy(0,0);
lcd_puts(a);
delay_ms(1000);
  }
  }
  }
 
  };
  }
 

********************************************************** 
// Project: 7 Program to send and receive SMS
**********************************************************

#asm
   .equ __lcd_port=0x15
#endasm

#include <mega8535.h>
#include <lcd.h>
#include <stdio.h>
#include <string.h>
#include <delay.h>
#include <stdlib.h>

void main(void)
{     
unsigned char a[16],temp1[100], strlength[2], temp2[16];
int i,length;
unsigned char str0[] = "AT";
unsigned char str1[]="ATE0"; //Echo off
unsigned char str2[]="OK";
unsigned char str3[]="+CMTI:"; //Indication of new mesg 
unsigned char Msg[]="ysrao"; //String
unsigned char str5[]="AT+CMGR=1"; //To read the sms
unsigned char str6[]="AT+CMGF=1"; //To set text mode   
unsigned char str7[]="AT+CMGD=1";
unsigned char str8[]={'A','T','+','C','M','G','S','=','"','9','8','2','0','9','6','2','8','7','0','"',';'};
unsigned char str9[]="LED is on\0";
unsigned char str10[]="LED is off\0";


for(i=0;i<=100;i++)
{
   a[i]=temp1[i]=temp2[i]=0;
}

PORTA=0x00;
DDRA=0x01;

PORTB=0x00;
DDRB=0x00;

PORTC=0x00;
DDRC=0x00;

PORTD=0x00;
DDRD=0x00;


UCSRA=0x00;
UCSRB=0x18;                         
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;


lcd_init(16);

PORTA=0x00; 
puts(str0); //Cheecking if AT commands are working
putchar(0x0D); //Enter
putchar(0x0A); //Line feed
gets(a,16); //Input from UDR (carriage return)
gets(a,16); //Input from UDR (OK)
lcd_gotoxy(0,0);
lcd_puts(str0);
lcd_gotoxy(0,1);
lcd_puts(a); //Display the response on LCD
delay_ms(10000);
lcd_clear();

if(strncmp(a,str2,2)==0)

lcd_clear();
puts(str1); //Echo off
putchar(0x0D); //Enter
putchar(0x0A); //Line feed
gets(a,16); //Input from UDR (carriage return)
gets(a,16); //Input from UDR (OK)
lcd_gotoxy(0,0);   
lcd_puts(str1);
lcd_gotoxy(0,1);
lcd_puts(a); //Display the response on LCD
delay_ms(10000);
lcd_clear();

puts(str6);             
putchar(0x0D); //Enter
putchar(0x0A); //Line feed
gets(a,16);
gets(a,16);
lcd_puts(str6);
lcd_gotoxy(0,1) ;
lcd_puts(a);
delay_ms(10000);
lcd_clear();

 while(1)
 {
   
    lcd_clear();
    lcd_gotoxy(0,0);
    lcd_putsf("Waiting... ");
   
    gets(a,16); //Input from UDR (carriage return)
        gets(a,16); //Waiting for the sms 
     
     lcd_clear();
     lcd_gotoxy(0,1); 
     delay_ms(1000);
     lcd_puts(a);
     delay_ms(1000);
     lcd_clear();
     lcd_gotoxy(0,0);
   
     if(strncmp(a,str3,6)==0)
       {
        lcd_clear();
        puts(str5);
          putchar(0x0D); //Enter 
          gets(a,16);
          gets(temp1,100); //Input from UDR (carriage return)
          gets(temp2,16); //The Message
          lcd_puts(str5);
         delay_ms(10000);
         lcd_clear();
        lcd_putsf("Reading Message...");
           delay_ms(10000);
        lcd_clear();
        length=strlen(temp2);
        itoa(length,strlength);
        lcd_puts(strlength);
        delay_ms(10000);
        lcd_clear();
        for(i=0;i<strlen(temp2);i++)
        {
            lcd_putchar(temp2[i]);
        }
        delay_ms(5000);
            lcd_clear();
        if(strncmp(temp2,Msg,strlen(Msg))==0 && strlen(temp2)==(strlen(Msg)+1))
        {
             PORTA=0x01;
             lcd_putsf("Correct message");
             delay_ms(1000);
           
             puts(str8);
             putchar(0x0D); //Enter
             putchar(0x0A); //Line feed
             delay_ms(1000);
             puts(str9);
             putchar(0x1A);
             lcd_clear();
           
             lcd_gotoxy(0,0);
             lcd_puts(str8);
             lcd_gotoxy(0,1);
             lcd_puts(str9);
             lcd_clear();
        }
        else
        {
              PORTA=0x00;
              lcd_putsf("wrong message");
              delay_ms(1000);
             
              puts(str8);
              putchar(0x0D); //Enter
              putchar(0x0A); //Line feed
              delay_ms(1000);
              puts(str10);
              putchar(0x1A);
              lcd_clear();
             
              lcd_gotoxy(0,0);
             lcd_puts(str8);
             lcd_gotoxy(0,1);
             lcd_puts(str10);
             lcd_clear();
        }
       
       
   puts(str7); //Delete the sms
   putchar(0x0D); //Enter
   putchar(0x0A); //Line feed
   gets(a,16);
   gets(a,16);
   lcd_clear();
 
   lcd_gotoxy(0,0);
   lcd_puts(a);
   delay_ms(1000);
   lcd_clear();

  }  }   }   } 

Comments

Popular posts from this blog

Word Power Made Easy 5.How to Talk About Liar and Lying

Word Power Made Easy 4.How to Talk About Science and Scientists