Showing posts with label ATMEGA32L. Show all posts
Showing posts with label ATMEGA32L. Show all posts

Sunday, 22 November 2015

Digital clock using TIMER1 in ATMEGA32



#include <avr/io.h>
#include <util/delay.h>
void cmd(int);
void data(char);
void conv(int);
int main(void)
{ int i,j,k,f=0;

DDRA=0xff;
DDRB=0xff;
DDRC=0xff;
TCCR1B=0x05;
PORTC=0x00;
cmd(0x38);
cmd(0x01);
cmd(0x0e);
cmd(0x06);
cmd(0x80);

    while(1)
    {
     for(i=10;i<12;i++)
        for(j=20;j<60;j++)
    for(k=0;k<60;k++)
{
                TCNT1H=0xfa;
TCNT1L=0x00;
while((TIFR&0x04)==0);
TIFR|=0x04;
                conv(i);
data(':');
conv(j);
data(':');
conv(k);
data(' ');
if (f%2==0)
{
data('A');
data('M');
}
else
{
data('P');
data('M');
}
if (i==10&&j==20&&k==10)
PORTC=0x01;


cmd(0x80);
}
f++;
}
}
void conv(int k)
{
int c,i=0;
char o[]="00";
while (k>0)
{
c=k%10;
k=k/10;
o[i]=c+48;
 i++;   }
for(i=1;i>=0;i--)
data(o[i]);
}

void cmd(int a)
{
PORTB=a;
PORTA=0x02;
_delay_ms(100);
PORTA=0x00;
}

void data(char a)
{
PORTB=a;
PORTA=0x03;
_delay_ms(100);
PORTA=0x01;
}

Monday, 9 November 2015

Motor programming using USART in ATMEGA32



#include <avr/io.h>
#include <util/delay.h>
void cmd(int);
void data(char);
void display(const char*p)
{
while (*p!='\0')
{
data(*p);
p++;
}
}
int main(void)
{char a;
DDRA=0xff;
DDRB=0xff;
DDRC=0xff;
DDRD=0xfe;
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x06;
UBRRH=0x00;
UBRRL=0x19;
cmd(0x38);
cmd(0x01);
cmd(0x06);
cmd(0x0e);
cmd(0x80);
    while(1)
    {
        while((UCSRA&0x80)==0);
a=UDR;
if (a=='f')
{cmd(0x01);
cmd(0x80);
PORTC=0x2e;
display("forward");
}
if(a=='r')
{cmd(0x01);
cmd(0x80);
PORTC=0x1d;
display("reverse");
}
if (a=='s')
{cmd(0x01);
cmd(0x80);
PORTC=0x00;
display("stop");
}
UCSRA=0x00;

    }
}
void cmd(int o)
{
PORTB=o;
PORTA=0x02;
_delay_ms(100);
PORTA=0x00;
}
void data(char p)
{
PORTB=p;
PORTA=0x03;
_delay_ms(200);
PORTA=0x01;
}

Saturday, 7 November 2015

Motor programming in ATMEGA32



#include <avr/io.h>
#include <util/delay.h>
char keypad(void);
void cmd(int);
void data(char);
void display(const char *p)
{
while(*p!='\0')
{
data(*p);
p++;
}
}



int main(void)
{
char a;
DDRA=0xFF;
DDRB=0xFF;
DDRC=0xF0;
DDRD=0xFF;

cmd(0x38);
cmd(0x01);
cmd(0x0e);
cmd(0x06);
cmd(0x80);


    while(1)
    {

a=keypad();
switch(a)
{
case '1':
display("FORWARD");
PORTD=0x1D;
//_delay_ms(2000);
break;
case '2':
display("REVERSE");
PORTD=0x2E;
//_delay_ms(2000);
break;

case '3':
display("STOP");
PORTD=0x00;
//_delay_ms(2000);
break;

}
//PORTD=0xFF;
//_delay_ms(2000);
//cmd(0x01);
//cmd(0x80);

     
 

}
}
char keypad()
{
int e;
PORTC=0x7F;
e=PINC&0x0F;
switch (e)
{
case 0x07:
   return '1';
break;
case 0x0B:
return '2';
break;
case 0x0D:
return '3';
break;
case 0x0E:
return '4';
break;
}
PORTC=0xbf;
e=PINC&0x0f;
switch (e)
{
case 0x07:
   return '5';
break;
case 0x0b:
return '6';
break;
case 0x0d:
return '7';
break;
case 0x0e:
return '8';
break;
}
PORTC=0xdf;
e=PINC&0x0f;
switch (e)
{
case 0x07:
   return '9';
break;
case 0x0b:
return '0';
break;
case 0x0d:
return 'a';
break;
case 0x0e:
return 'b';
break;
}
PORTC=0xef;
e=PINC&0x0f;
switch (e)
{
case 0x07:
   return 'c';
break;
case 0x0b:
return 'd';
break;
case 0x0d:
return 'e';
break;
case 0x0e:
return 'f';
break;
default:
return 'z';
break;
}
    }
void cmd(int a)
{
PORTB=a;
PORTA=0x02;
_delay_ms(200);
PORTA=0x00;
}
void data(char s)
{
PORTB=s;
PORTA=0x03;
_delay_ms(200);
PORTA=0x01;
}

Thursday, 5 November 2015

ADC PROGRAMMING OF ATMEGA32

Convert the analog value to digital and display it on LCD

#include <avr/io.h>
#include <util/delay.h>
void cmd(int);
void data(char);

int main(void)
{
int i,a,c,j;
char S[3]="000";

DDRA=0x30;
DDRB=0xff;
ADMUX=0x60;
ADCSRA=0x88;
cmd(0x38);
cmd(0x01);
cmd(0x0e);
cmd(0x06);
cmd(0x80);

    while(1)
    {
ADCSRA|=0x40;

while((ADCSRA&0x10)==0);
a=ADCH;
i=0;
while(a>0)
{
c=a%10;
a=a/10;
S[i]=c+48;
i++;
}
   
  for(j=2;j>=0;j--)
  {data(S[j]);
  }
  cmd(0x01);
  cmd(0x80);
    }
}
void cmd(int a)
{
PORTB=a;
PORTA=0x20;
_delay_ms(200);
PORTA=0x00;
}

void data(char a)
{
PORTB=a;
PORTA=0x30;
_delay_ms(200);
PORTA=0x10;
}

Tuesday, 27 October 2015

TIMER0 OVERFLOW INTERRUPT OF ATMEGA32

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
ISR(TIMER0_OVF_vect)
{
PORTA=0Xff;
_delay_ms(10000);
PORTA=0X00;
_delay_ms(1000);
TIFR|=0x01;

}
int main(void)
{
DDRA=0XFF;
SREG=0X80;
TIMSK=0x01;
TCCR0=0X05;
TCNT0=0X00;
//OCR0=0xff;
    while(1);

}

TIMER2 COMPARE INTERRUPT OF ATMEGA32

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
ISR(TIMER2_COMP_vect){
       TIFR|=0X80;
PORTA=0XFF;
_delay_ms(500);
PORTA=0X00;

}
int main(void)
{
DDRA=0XFF;
SREG=0X80;
TIMSK=0X80;
TCCR2=0X07;
TCNT2=0X00;
    while(1);
    {
     _delay_ms(500);
    }
}

TIMER1 COMPARE INTERRUPT OF ATMEGA32

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
ISR(TIMER1_COMPA_vect){
PORTA=0xff;
_delay_ms(500);
PORTA=0x00;
    //TIFR|=0X10;
}
ISR(TIMER1_COMPB_vect){
PORTB=0XFF;
_delay_ms(500);
PORTB=0X00;
TIFR|=0X08;
}
int main(void)
{
DDRA=0XFF;
DDRB=0XFF;
SREG=0X80;
TIMSK=0X18;
TCCR1A=0X00;
TCCR1B=0X05;
TCNT1H=0X00;
TCNT1L=0X00;
OCR1AH=0XB0;
OCR1AL=0X00;
OCR1BH=0XF0;
OCR1BL=0X00;
    while(1);
    }

TIMER1 OVERFLOW VECTOR INTERRUPT (ATMEGA32)

Blink a port when an overflow occurs

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

ISR(TIMER1_OVF_vect){

TCNT1H=0XA0;
TCNT1L=0X00;
PORTA=0XFF;
_delay_ms(1000);
PORTA=0X00;
TIFR|=0x04;
}
int main(void)
{
    DDRA=0XFF;
SREG=0X80;
//TCCR1A=0x00;
TCCR1B=0X05;
TIMSK=0X04;

TCNT1H=0XA0;
TCNT1L=0X00;

while(1);

}

Monday, 26 October 2015

External Interrupt programming(INT0) of ATMEGA32

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

ISR(INT0_vect){
PORTA=0XFF;
_delay_ms(500);
PORTA=0X00;
_delay_ms(500);
}

int main(void)
{
//DDRD=0x00;
DDRA=0XFF;

MCUCR=0X02;
//MCUCSR=0X00;

GICR=0X40;

SREG=0X80;

TCCR0=0x05;

while(1);
}    

ATMEGA32 Interrupts

Reset and Interrupt Vectors

Saturday, 10 October 2015

PORT Blinking using TIMER2

#include <avr/io.h>
#include<util/delay.h>
int main(void)
{

 int i;
 DDRA=0xff;
PORTA=0x00;
TCCR2=0x07;

 while(1)
 {  
for(i=0;i<7;i++)
{
   TCNT2=0xff;
while((TIFR&0x40)==0);
TIFR|=0X40;
}
PORTA=PORTA^0XFF;
    }
}

Timer/Counter2 of ATMEGA32


Timer/Counter2 is a general purpose, single compare unit, 8-bit Timer/Counter module. The
main features are:
• Single Compare unit Counter
• Clear Timer on Compare Match (Auto Reload)
• Glitch-free, Phase Correct Pulse Width Modulator (PWM)
• Frequency Generator
• 10-bit Clock Prescaler
• Overflow and Compare Match Interrupt Sources (TOV2 and OCF2)
• Allows clocking from External 32 kHz Watch Crystal Independent of the I/O Clock

Registers
The Timer/Counter (TCNT2) and Output Compare Register (OCR2) are 8-bit registers. Interrupt
request (shorten as Int.Req.) signals are all visible in the Timer Interrupt Flag Register (TIFR).
All interrupts are individually masked with the Timer Interrupt Mask Register (TIMSK). TIFR and
TIMSK are not shown in the figure since these registers are shared by other timer units.
The Timer/Counter can be clocked internally, via the prescaler, or asynchronously clocked from
the TOSC1/2 pins, as detailed later in this section. The asynchronous operation is controlled by
the Asynchronous Status Register (ASSR). The Clock Select logic block controls which clock
source the Timer/Counter uses to increment (or decrement) its value. The Timer/Counter is inactive
when no clock source is selected. The output from the Clock Select logic is referred to as the
timer clock (clkT2).
The double buffered Output Compare Register (OCR2) is compared with the Timer/Counter
value at all times. The result of the compare can be used by the waveform generator to generate
a PWM or variable frequency output on the Output Compare Pin (OC2). See “Output Compare
Unit” on page 116. for details. The compare match event will also set the Compare Flag (OCF2)
which can be used to generate an output compare interrupt request.







Blinking of LED using TIMER1

#include <avr/io.h>

int main(void)
{DDRA=0XFF;
//TCCR1A=0x00;
TCCR1B=0X05;
PORTA=0x00;
    while(1)
    {TCNT1H=0Xfa;
TCNT1L=0X00;
while((TIFR&0x04)==0);
PORTA=PORTA^0xff;
TIFR|=0x04;
       
    }
}

TIMER/COUNTER1 of ATMEGA32

The 16-bit Timer/Counter unit allows accurate program execution timing (event management),
wave generation, and signal timing measurement. The main features are:
• True 16-bit Design (i.e., Allows 16-bit PWM)
• Two Independent Output Compare Units
• Double Buffered Output Compare Registers
• One Input Capture Unit
• Input Capture Noise Canceler
• Clear Timer on Compare Match (Auto Reload)
• Glitch-free, Phase Correct Pulse Width Modulator (PWM)
• Variable PWM Period
• Frequency Generator
• External Event Counter
• Four Independent Interrupt Sources (TOV1, OCF1A, OCF1B, and ICF1)

Registers

The Timer/Counter (TCNT1), Output Compare Registers (OCR1A/B), and Input Capture Register
(ICR1) are all 16-bit registers. Special procedures must be followed when accessing the 16-
bit registers. These procedures are described in the section “Accessing 16-bit Registers” on
page 89. The Timer/Counter Control Registers (TCCR1A/B) are 8-bit registers and have no CPU
access restrictions. Interrupt requests (abbreviated to Int.Req. in the figure) signals are all visible
in the Timer Interrupt Flag Register (TIFR). All interrupts are individually masked with the Timer
Interrupt Mask Register (TIMSK). TIFR and TIMSK are not shown in the figure since these registers
are shared by other timer units.
The Timer/Counter can be clocked internally, via the prescaler, or by an external clock source on
the T1 pin. The Clock Select logic block controls which clock source and edge the Timer/Counter
uses to increment (or decrement) its value. The Timer/Counter is inactive when no clock source
is selected. The output from the clock select logic is referred to as the timer clock (clkT1).
The double buffered Output Compare Registers (OCR1A/B) are compared with the Timer/Counter
value at all time. The result of the compare can be used by the Waveform Generator to
generate a PWM or variable frequency output on the Output Compare pin (OC1A/B).The compare match event will also set the Compare Match Flag (OCF1A/B) which can be used to generate an output compare interrupt request.The Input Capture Register can capture the Timer/Counter value at a given external (edge triggered) event on either the Input Capture Pin (ICP1) or on the Analog Comparator pins. The Input Capture unit includes a digital filtering unit (Noise Canceler) for reducing the chance of capturing noise spikes. The TOP value, or maximum Timer/Counter value, can in some modes of operation be defined by either the OCR1A Register, the ICR1 Register, or by a set of fixed values. When using OCR1A as TOP value in a PWM mode, the OCR1A Register can not be used for generating a PWM output. However, the TOP value will in this case be double buffered allowing the TOP value to be changed in run time. If a fixed TOP value is required, the ICR1 Register can be usedas an alternative, freeing the OCR1A to be used as PWM output.

Tuesday, 6 October 2015

Generate a 1s delay using TIMER0 in ATMEGA32

#include <avr/io.h>
void delay();
int main(void)
{  
 DDRB=0xff;
    DDRA=0XFF;
PORTA=0XFF;
TCCR0=0X05;
 

  while(1)
    {
      delay();
 PORTA=PORTA^0xff; 

    }
}
void delay()
{int i;
for(i=0;i<7;i++)
    {    TCNT0=0Xf0;  
     
     while((TIFR&0x01)==0);
 TIFR|=0x01;
 }
}

TIMER/COUNTER0 MODULE OF ATMEGA32L

Timer/Counter0 is a general purpose, single compare unit, 8-bit Timer/Counter module. The
main features are:
• Single Compare Unit Counter
• Clear Timer on Compare Match (Auto Reload)
• Glitch-free, Phase Correct Pulse Width Modulator (PWM)
• Frequency Generator
• External Event Counter
• 10-bit Clock Prescaler
• Overflow and Compare Match Interrupt Sources (TOV0 and OCF0)
The Timer/Counter (TCNT0) and Output Compare Register (OCR0) are 8-bit registers. Interrupt
request (abbreviated to Int.Req. in the figure) signals are all visible in the Timer Interrupt Flag
Register (TIFR). All interrupts are individually masked with the Timer Interrupt Mask Register
(TIMSK). TIFR and TIMSK are not shown in the figure since these registers are shared by other
timer units.
The Timer/Counter can be clocked internally, via the prescaler, or by an external clock source on
the T0 pin. The Clock Select logic block controls which clock source and edge the Timer/Counter
uses to increment (or decrement) its value. The Timer/Counter is inactive when no clock source
is selected. The output from the Clock Select logic is referred to as the timer clock (clkT0).
The double buffered Output Compare Register (OCR0) is compared with the Timer/Counter
value at all times. The result of the compare can be used by the waveform generator to generate
a PWM or variable frequency output on the Output Compare Pin (OC0).The compare match event will also set the Compare Flag (OCF0) which can be used to generate an output compare interrupt request.

Saturday, 26 September 2015

Separate the data between # and * from serial receiver and display it on LCD(LM016L) in ATMEGA32L


#include <avr/io.h>
#include <util/delay.h>
void cmd(int);
void data(char);
int main(void)
{  
       char a;
int q,d;
        DDRB=0XFF;
DDRA=0XFF;
        DDRD=0xfe;
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x06;
UBRRH=0x00;
UBRRL=0x19;
cmd(0x38);
cmd(0x01);
cmd(0x0e);
cmd(0x06);
cmd(0x80);
    while(1)
    {
        while((UCSRA&0x80)==0);

a=UDR;
if(a=='*')
q=0;
if(q==1)
data(a);
if(a=='#')
q=1;

UCSRA=0x00;
    }
}
void cmd(int a)
{
PORTB=a;
PORTA=0x00;
PORTA=0x04;
_delay_ms(200);
PORTA=0x00;

}
void data(char s)
{
PORTB=s;
PORTA=0x01;
PORTA=0x05;
_delay_ms(200);
PORTA=0x01;
}

USART ASYNCHRONOUS RECEIVER (ATMEGA32L)



#include <avr/io.h>
#include <util/delay.h>
void cmd(int);
void data(char);
int main(void)
{   DDRB=0XFF;
DDRA=0XFF;
    char a;
    DDRC=0xfe;
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x06;
UBRRH=0x00;
UBRRL=0x19;
cmd(0x38);
cmd(0x01);
cmd(0x0e);
cmd(0x06);
cmd(0x80);
    while(1)
    {
        while((UCSRA&0x80)==0);


data(UDR);
_delay_ms(1000);
UCSRA=0x00;
    }
}
void cmd(int a)
{
PORTB=a;
PORTA=0x00;
PORTA=0x04;
_delay_ms(200);
PORTA=0x00;

}
void data(char s)
{
PORTB=s;
PORTA=0x01;
PORTA=0x05;
_delay_ms(200);
PORTA=0x01;
}

USART ASYNCHRONOUS TRANSMITTER (ATMEGA32)


#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
        int i;
char S[]="hello";
        DDRD=0x02;
        UCSRA=0X00;
UCSRB=0X18;
UCSRC=0X06;

UBRRL=0X19;
UBRRH=0X00;

     for (i=0;S[i]!='\0';i++)
    {
    
UDR=S[i];
while((UCSRA&0X40)==0);
UCSRA=0X00;
_delay_ms(1000);
    }
while(1);
}
        

Friday, 25 September 2015

ATMEGA32L:USART

The Universal Synchronous and Asynchronous serial Receiver and Transmitter (USART) is a highly flexible serial communication device.
The main features are:
• Full Duplex Operation (Independent Serial Receive and Transmit Registers)
• Asynchronous or Synchronous Operation
• Master or Slave Clocked Synchronous Operation
• High Resolution Baud Rate Generator
• Supports Serial Frames with 5, 6, 7, 8, or 9 Data Bits and 1 or 2 Stop Bits
• Odd or Even Parity Generation and Parity Check Supported by Hardware
• Data OverRun Detection
• Framing Error Detection
• Noise Filtering Includes False Start Bit Detection and Digital Low Pass Filter
• Three Separate Interrupts on TX Complete, TX Data Register Empty, and RX Complete
• Multi-processor Communication Mode
• Double Speed Asynchronous Communication Mode


USART Registers

USART I/O Data Register – UDR

USART Control and Status Register A –UCSRA

USART Control and Status Register B –UCSRB

USART Control and Status Register C –UCSRC

USART Baud Rate Registers – UBRRL and UBRRH

USART Block Diagram