Monday, 28 October 2013

AVR: Stopwatch With Interrupt Timer and Multiplexed 7 segment Display

Here we are connecting the 7 segment display with PORTA, i.e. Multiplexing of six-7segment display, with the control lines to PORTB. Following is the program to Interface the 7segments.

Crystal : 8MHz
Time: 0.125uSec
Prescaler: clk/1025
Timer Interrupt : 25mSec.
For 1sec , run this timmer for 40 times.

We can achieve max delay of appx. 35000uSec or 35mSec with 8MHz crystal frequency and prescaler (clk/1024).

Program: 

/*
 * two_7seg.c
 *
 * Created: 27-Oct-13 19:08:04
 *  Author: avrnarm
 */ 

/* PROGRAM to execute the counter with the multiplexed 7 segent display */
#define FUSE_CKSEL0 0
#define FUSE_CKSEL1 0
#define FUSE_CKSEL2 1
#define FUSE_CKSEL3 0

#include<avr/io.h>
#define F_CPU 8000000Ul
#include<util/delay.h>
#include<avr/interrupt.h>

unsigned int j,ss=0,mm=0,hh=0;
char ssd[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; // Hexadecimal values for the 7 Segment display generated by the look up table
char ssd_dec[]={0xbf,0x86,0xdb,0xcf,0xE6,0xEd,0xFd,0x87,0xff,0xEf}; // Hexadecimal values for the 7 Segment display generated by the look up table

int main(void)
{
DDRA=0xFF;
DDRB=0xFF;
//*********************TIMER***(8MHz crystal....clk/1024 prescaler......Obtained 25ms delay
TCNT0=61;
TCCR0=0x05;
TIMSK=(1<<TOIE0);
//*********************
sei();
while(1)
    {
// for seconds
PORTB=0xFF;
PORTA=ssd[(ss)/10];
PORTB=0b11111110;
_delay_ms(1);
PORTB=0xFF;
PORTA=ssd[(ss%10)];
               PORTB=0b11111101;
_delay_ms(1);
PORTB=0xFF;
// for minute
PORTB=0xFF;
PORTA=ssd[(mm)/10];
PORTB=0b11111011;
_delay_ms(1);
PORTB=0xFF;
PORTA=ssd_dec[(mm%10)];
PORTB=0b11110111;
_delay_ms(1);
PORTB=0xFF;
//for hour
PORTB=0xFF;
PORTA=ssd[(hh)/10];
PORTB=0b11101111;
_delay_ms(1);
PORTB=0xFF;
PORTA=ssd_dec[(hh%10)];
PORTB=0b11011111;
_delay_ms(1);
PORTB=0xFF;
if (ss==60)
{
ss=0;
mm++;
if (mm==60)
{
hh++;
mm=0;
ss=0;
}
}
    }
}
ISR(TIMER0_OVF_vect)
{
TCNT0=61;                                                          //Refill  the Timer
j++;
if(j==40)                                                                 //For delay of 1sec
{
ss++;
j=0;
}
}

0 comments:

Post a Comment

Blogroll

Copyright © 2013 RAJ PRAJAPATI (raj@electronicsrj.com)

Comments

About Projects

We also Provide support in making Electronics Projects at different level at Cheapest price. Contact Us for the further help.