Understanding AVR

Posted on at


Here are a few problems with their solutions, basically the purpose of this code is to understand the Embedded system coding. This coding is in C language but we can also do it in machine language or assembly language.

basically AVR stands for Advanced Virtual RISC and RISC stand for the Reduced Instruction Set computing and this risc was designed to have more powerful computing and advanced computers use this risc. as we can see that today the computers are much more effecient than they were before. the main reason is the simple and effecient structure of the computer. as we can see that in early times the computers were very large and had very much weight but you can see nowadays the computers are just not more than grams of their weight.

Q#1: Write a Program to count events at T1 for 2 seconds convert it into frequency and Show the frequency of incoming events at PORTA and PORTC and after each 2 seconds continuously. Assume XTAL = 1MHz.

Code:

#include <avr /io. h>

Int main ( )

{   PORT B = 0x01 ;

DDRC = 0xFF ;

DDRA = 0xFF ;

TCCR1A = 0x00 ;

TCCR1B = 0x06 ;

TCNTH = 0x00 ;

TCN1L = 0x00 ;

While (1)

{

PORT A = TCNT1L;

PORT C = TCNT1H;

T1 delay (2) ; } }

Void T1 delay ( unsigned int I )

{

 int k, l, m

For (l=0 ; l<i+1 ; l++ )

{

For (k=0 ;  k<<11 ; k++)

{

Delay 100ms ( ) ; } } }

Void delay (void )

{unsigned int f ;

for (f=0; f < 42150; f++ ) ; }

Q#2: The 7-Segment Driver MAX7221 is connected to the microcontroller using SPI Protocol and it is also connected with two 7-Segments as shown below. Write a C program using SPI Protocol to Display 7h on two 7-Segments.                                                                                                          

Code:

 

#define F_CPU 1000000

 

#include <avr /io. h>

#include<util\ delay. h>

 

#define MOSI 5                             //BIT MASKING

//#define MISO 6

#define SCK  7

#define SS   4

 

 

void execute( unsigned char cmd , unsigned char data)

{

     PORTB& = ~ (1 << SS) ;

     SPDR = cmd ;

     While (!(SPSR&(1 << SPIF) ) ) ;

     SPDR = data ;

     while (!(SPSR & (1 << SPIF ) ) ) ;

     PORTB| = ( 1 << SS) ;

}

 

int main (void)

{ DDRB = (1 << MOSI)|(1 << SCK)|(1 << SS) ;  //MOSI and SCK are output

     SPSR = (1 << SPI2X);

     SPCR = (1 << SPE)|(1 << MSTR)|(1 << SPR0) ;//enable SPI as master

     Execute (0x09, 0b00000010) ;

     Execute (0x0B, 0x02) ;

     Execute (0x0C, 0x01);

     Execute (0x01,0b0010111) ;

     Execute (0x02, 0x07) ;

     While (1) ;

     Return 0 ;

}

 

 

Simulation :

 

 

Q#3 : Write a program to communicate with DS1307 Real Time Clock (RTC) using I2C protocol. Assume XTAL = 8MHz, SCL frequency should be 100KHz. First connect to RTC and set date and time as 18/12/2013 11:22:05 PM. Now read the current date and time and show on BCD 7-Segments alternatively.

Code:

#define F_CPU 1000000

 

#include <avr /io. h>

#include <util \delay. h>

void i2c_init (void)

{  TWSR = 0x00; //set prescaler bits  to zero

     TWBR = 0x47;    //SCL frequency is 50K for XTAL = 8M

     TWCR = 0x04;   //enable TWI module

 

}

void i2c_start (void)

{

     TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);

      while ( ! (TWCR & (1 << TWINT) ) ) ;

    

}

 

void   i2c_write (unsigned char data)

{

     TWDR =  data   ;

     TWCR = (1 << TWINT) | (1 << TWEN) ;

     while    (!(TWCR & (1 << TWINT) ) ) ;

     }

    

     unsigned  char  i2c_read (unsigned  char  ackVal)

     {  TWCR = (1 << TWINT) | (1 << TWEN)| (ackVal << TWEA) ;

           while    (!(TWCR&(1 << TWINT) ) ) ;

           return   TWDR      ;

           }

     void i2c_stop()

     { TWCR  =    (1<<TWINT) | (1<<TWEN) | (1<<TWSTO) ;

     for   (   int  k = 0   ;   k<100   ;   k++)    ;   //wait   for  a   short  time

     }

 void   rtc_init (void)

 {i2c_init ( ) ;   // initialize   I2C  module

 i2c_start ( )  ;  // transmit   START   condition

 i2c_write (0xD0) ;   // address   DS1307   for  write

 i2c_write ( 0x07) ;    // set  register  pointer  to  7

 i2c_write (0x00) ;    // set  value  of   location  7   to   0

 i2c_stop ( );    // transmit   STOP   condition

 }

 void  rtc_setTime (unsigned  char  h, unsigned  char  m, unsigned  char s)

 {   i2c_start ( ) ;        // transmit START condition

      i2c_write ( 0xD0 ) ;

      i2c_write ( 0 ) ;

      i2c_write ( s ) ;

      i2c_write (m) ;

      i2c_write (h) ;

      i2c_stop ( ) ;

      

}

 \\ this part is for setting the date

     void rtc_setDate (unsigned char y, unsigned char m, unsigned char d)

     {   

            i2c_start ( ) ;  //transmit START condition

          i2c_write (0xD0) ;    //address DS1307 for write

          i2c_write (0x04) ;    //set register pointer to 4

          i2c_write (d) ;   //set day

          i2c_write (m);   //set month

          i2c_write (y);   //set year

           i2c_stop ( ) ;    //transmit STOP condition

     }

 

     void rtc_getTime ( unsigned char *h, unsigned char *m, unsigned char *s)

     {i2c_start ( ) ; // transmit START condition

     i2c_write (0xD0) ;     // address DS1307 for write

     i2c_write (0) ;   // set register pointer to 0

     i2c_stop ( ) ;    // transmit STOP condition

     i2c_start ( ) ;    // transmit START condition

     i2c_write (0xD1) ;     // address DS1307 for read

     *s = i2c_read (1) ;   // read second, return ACK

     *m = i2c_read (1) ;   //read minute, return ACK

     *h = i2c_read (0) ;   //read hour, return NACK

     i2c_stop ( ) ;    //transmit STOP condition

      }

      void rtc_getDate(unsigned char *y,unsigned char *m,unsigned char *d)

      { i2c_start ( ) ;     //transmit START condition

      i2c_write (0xD0) ;    //address DS1307 for write

      i2c_write (0x04) ;    //set register pointer to 4

      i2c_stop ( ) ;   // transmit STOP condition

      i2c_start ( ) ;   // transmit START condition

      i2c_write (0xD1) ;    // address DS1307 for read

      *d = i2c_read (1) ;  // read day, return ACK

      *m = i2c_read (1) ;  // read month, return ACK

      *y = i2c_read (0) ;  // read year, return NACK

      i2c_stop ( ) ;  // transmit STOP condition

      }

int main (void)

{  DDRA = 0xFF ;

     DDRB = 0xFF ;

     DDRD = 0xFF ;

     unsigned char i, j, k, n ;

     n = 0 ;

     rtc_init ( ) ;

           rtc_setTime (0x12,0x00,0x01)  ;     //  19 : 45 : 30 (hh  : mm : ss)

           rtc_setDate (0x13,0x12,0x25) ;    // 13 : 12 : 25 (yy : mm : dd)

           while(1)

           { if (n % 2 == 0)

           { _delay_ms ( 1000 ) ;

                rtc_getTime (&i, &j, &k) ;

           PORTA = k ;

           PORTB = j ;

           PORTD = i ;

           n++ ; }

           else

           {  _delay_ms (1000) ;

           rtc_getDate (&i, &j, &k) ;

           PORTA = k ;

           PORTB = j ;

           PORTD = i ;

           n++ ; }

           }

           // while(1) ;     //stop here

           return 0 ;

     }

 

Simulation :

                                                              


 



About the author

Azmat_Ali

i am working at filmannex.

Subscribe 0
160