Codes for Timers,Counters and interfacing of SSD with AVR

Posted on at


C++ code for 2digit Seven Segment Displayon AVR of AT-MEGA 16 Bit:

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

// PORTA.0 = b PORTA.1 = a PORTA.2 = f
// PORTA.3 = g PORTA.4 = dot PORTA.5 = c
// PORTA.6 = d PORTA.7 = e
 // a
// _____
// f | g | b
// |_____|
// e | | c dot
// |_____| .
// d

// | No. | e d c dot g f a b |
// | 0 | 1 1 1 0 0 1 1 1 = 0xD7 |
// | 1 | 0 0 1 0 0 0 0 1 = 0x21 |
// | 2 | 1 1 0 0 1 0 1 1 = 0xCB |
// | 3 | 0 1 1 0 1 0 1 1 = 0x6B |
// | 4 | 0 0 1 0 1 1 0 1 = 0x2D |
// | 5 | 0 1 1 0 1 1 1 0 = 0x6E |
// | 6 | 1 1 1 0 1 1 1 0 = 0xEE |
// | 7 | 0 0 1 0 0 0 1 1 = 0x23 |
// | 8 | 1 1 1 0 1 1 1 1 = 0xEF |
// | 9 | 0 1 1 0 1 1 1 1 = 0x6F |
void Display(unsigned char); //Function prototype declaration
unsigned char SevenSegment[]= { 0xE7, 0x21, 0xCB, 0x6B, 0x2D, 0x6E,
0xEE, 0x23, 0xEF, 0x6F};
void main(void)
{
DDRA = 0xFF; // Port A as output
DDRB = 0xFF; // Port B as output
unsigned char i = 0;
while(1)
{
Display(i); // Displays two digit value on 7 segments
i++;
if(i > 99)
i = 0;
}
return 0;
}
void Display(unsigned char n)
{
unsigned char units, tens;
{
tens = n/10; // Separate tens from a two digit number
units = n%10; // Separate units from a two digit number
for(x = 0; x<1100; x++) // Generate delay of about 1 second
{
PORTB = 0xFE; // Select one display to show units value
PORTA = Seven_Segment[units]; // Display units
_delay_us(500);
PORTB = 0xFD; // Select second display to show tens value
PORTA = Seven_Segment[tens]; // Display tens
_delay_us(500);
}
}
Simulation:

C code for Timer duty cycle of wave:

//This prog. generates wave of 5 Hz with duty cycle of 50% on PA0. On and off time is 100ms.

#include<avr/io.h>
#include<util/delay.h>
//Macros definition
#define BitGet(p,m) ((p) & (m))
#define BitSet(p,m) ((p) |= (m))
#define BitFlip(p,m) ((p) ^= (m))
#define Bit(x) (0x01 << (x))
void TimerDelay(void); //Function prototype declaration
void main(void)
{
DDRA = 0xFF; //Make PortA output
BitSet(PORTA, Bit(0)); //Initially set PA0
while(1)
{
BitFlip(PORTA, Bit(0)); //Toggle PA0
TimerDelay(); //Generates delay of about 100ms
}
}
/* delay calculation
For a clock generation of 5Hz (200ms), timer should be overflowed twice, so:
Timer overflow @ = 200ms / 2 = 100ms (0.1s high, 0.1s low)
Crystal Clock = 1MHz
Prescaler used = 1024
Timer clock = 1MHz / 1024 = 976.5625 Hz
Timer Period = 1/976.5625 = 1024us
Timer Value = 0.1s / 1024 x10^-6s = 97.65625 = 98 (approx) */
void TimerDelay(void)
{
TCNT0 = 0x9F; // (256+1)-98 = 159 = 0x9F
TCCR0 = 0x05; //Timer0 ON, clk/1024 prescaler, Normal Mode
while(!BitGet(TIFR, Bit(0))); // Wait for timer0 overflow & TOV0 flag is raised
TCCR0 = 0x00; //Stop Timer
BitSet(TIFR, Bit(0)); //Clear TOV0
}

Simulation:

              5 Hz square wave generation through timer0

//This program counts the event occur at T1 (PB1) on every falling edge
//using 16-Bit Timer1 as event counter and shows the event count on PORTA and PORTC
//higher and lower byte respectively
#include<avr/io.h>
#include<util/delay.h>
//Start of main program
void main(void)
{
PORTB = 0x02; //Set a pull-up on PB2 (T1)

DDRA = 0xFF; //Make PORTA as output
DDRC = 0xFF; //Make PORTA as output
TCCR1A = 0x00; //Enable Counter Mode no falling adge at PB1
TCCR1B = 0x06;
while(1)
{
PORTC = TCNT1H; //Send higher byte of counter to PortC
PORTA = TCNT1L; //Send lower byte of counter to PortA
}
}

Simulation:

                                                    16-Bit Counter Using Timer1



About the author

MuhammadTayyab

Me from Pakistan and im the student of undergraduate;BS Electronics.

Subscribe 0
160