# how to make a seven segment based count down timer for traffic signal using atmega 16

Status
Not open for further replies.

#### prashanttyagi85

dear all
i am working on traffic signal's countdown timer.
as everyone know that timer shows the remaining stop time for that road.
it starts from final value & reduce upto 0 till the signal is changed.
i have to read the red signal first of all then calculate the time then convert it into seconds then send it to 7 segments then reduce it by 1 second & change it on 7 segment.
am i write?
if yes then how to convert time into second & send it to 7 segment?

- - - Updated - - -

is nobody interested to tell me about it?

#### Er.abdulrehman

##### Member level 2
You need to load the timer register first with the HEX value of your time count.
Timer Count=1/((1/12)*Crystal Frequency)

##### Super Moderator
Staff member
This is a system consisting of several circuits. It is hard to visualize the entire layout from your verbal description.

i have to read the red signal first of all then calculate the time then convert it into seconds then send it to 7 segments then reduce it by 1 second & change it on 7 segment.

The method I'm used to seeing, is to load a desired value into the seconds timer, then start it counting down.

prashanttyagi85

### prashanttyagi85

Points: 2

#### prashanttyagi85

actually we are not having any desired value here.
we have to detect the red signal then read it in first cycle
after that wait for next cycle
in next next cycle again read the red signal
then compare the first red signals time with second one
if both time are same then display it on 7 segment in next cycle with count down
now we have to read it in each cycle that the red value is same or not

actually we are not having any desired value here.
we have to detect the red signal then read it in first cycle
after that wait for next cycle
in next next cycle again read the red signal
then compare the first red signals time with second one
if both time are same then display it on 7 segment in next cycle with count down
now we have to read it in each cycle that the red value is same or not

It as a clever approach for automatic time lapse calculation. You dont have to feed any value for time but by calculating time taken for red signal, seconds count value is updated accordingly making setup real easy.

#### prashanttyagi85

dear all after a long time i worked on this count down timer.
i am posting the code here please help me in this code as i want to use here timer interrupt.

- - - Updated - - -

Code:
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
#define F_CPU 16000000UL

char digit[] = {0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};

char count=0,sec=0,tensec=0, halfsec=0;
char i=0,j=0,one=0,ten1=0,one1=0,ten=0,hun=0,sec1=0,k=0,track=0;

//char greencount=0,greensec1=0,greentensec=0, greenhalfsec=0;

void main()
{
DDRD=0xFF;
DDRB=0x00;
DDRA=0xFF;
//	sei();	// set intrupt enable

while(1)
{
//	PORTA=0xff;
//	PORTD=0xff;
if(PINB & 0b00010000)
{

_delay_ms(5);

}
sec=sec1;
breake(sec);		//function to break sec into ones, tens
display(one, ten);	// to display on 7 segment
}
}
{
if(PINB & 0b00010000)
{

_delay_ms(20);
while(PINB & 0b00010000)
{
count++;
_delay_ms(5);
if(count==20)
{
tensec++;
count=0;
if(!(PINB & 0b00010000))
break;
}
if(tensec==5)
{
halfsec++;
tensec=0;
if(!(PINB & 0b00010000))
break;
}
if(halfsec==2)
{
sec1++;
halfsec=0;
if(!(PINB & 0b00010000))
break;
}
}
}
}

breake(sec)
{
if(sec<=99)
{
hun=0;
one=sec%10;
one1=one;
ten=sec/10;
ten1=ten;
}
else
{
hun=1;
one=sec%100;
sec=sec/10;
ten=sec%10;
}
}

display(one, ten)
{
for(i=ten;i>0;i--)
{
PORTD=~digit[i];
for(j=one;j>0;j--)
{
PORTA=~digit[j];
_delay_ms(1000);
}
if(j==0)
{
PORTA=~digit[j];
_delay_ms(1000);
one=9;
}
}
if(i==0)
{
PORTD=~digit[i];
for(j=one;j>0;j--)
{
PORTA=~digit[j];
_delay_ms(1000);
}
}
if(i==0&j==0)
{
PORTD=~digit[i];
PORTA=~digit[j];
_delay_ms(1000);
one=one1;
ten=ten1;
}

}

- - - Updated - - -

the basic funda behind my project is
i have to read a +ve signal which i am reading using PB4 pin of atmega16.
when signal on, i have to detect it, count the time upto which it remains on, & display it on 7 segment.
function breake() to convert it for display
Function display() for display it
i am doing here polling
but it is not an efficient method so please any body arrange my code with the help of timer interrupt.
thanks

#### prashanttyagi85

dear all
please send any suggestion or correction in my code for removal of my problem

#### prashanttyagi85

is nobody interested to read & correct the code?

##### Banned
When PB4 becomes high, start the timer and when PB4 becomes low then stop the timer. In the timer interrupt routine increment ms, sec, hour counters. The values of ms, sec, hour counters will give you the time for which PB4 was on. If you want more precision then incluse a timer interrupt for one us. Then increment the ms, sec, and hour counters depending upon the value of counter used for us.

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//Timer0 Prescaler = 0; Preload = 16; Actual Interrupt Time = 1 us

//Place/Copy this part in declaration section
void InitTimer0(){
SREG_I_bit = 1;
OCR0 = 16;
TCCR0 = 0x28;
TCCR0 |= 0x01;
OCIE0_bit = 1;
}

OCR0 = 16;
us++
if(us == 1000){ms++; us = 0;}
if(ms == 1000){sec++; ms = 0;}
if(sec == 60){min++; sec = 0;}
if(min == 60){hour++; min = 0;}
if(hour == 24){day++; hour = 0;}

}

//Display day:hour:min:sec:ms:us

Last edited:
prashanttyagi85

### prashanttyagi85

Points: 2

#### prashanttyagi85

thanks for reply & sorry for responding so much late.
please also tell that in your code line no. 13 said that "enter your code here" it means that should i write the code for displaying the data at 7 segment? or else.

##### Banned
You can place code which displays data on 7 Segment display.

prashanttyagi85

Points: 2