rezaf
Advanced Member level 4
Hi Friends.
my project is digital clock with 7-segment(only hour and minutes) with atmega16 and it's timer1 compare mode.
Please review my code and say to me if anything is wrong. when I simulate it, always show 0 in four 7-segments. I written this with CodeVisionAVR .
my project is digital clock with 7-segment(only hour and minutes) with atmega16 and it's timer1 compare mode.
Please review my code and say to me if anything is wrong. when I simulate it, always show 0 in four 7-segments. I written this with CodeVisionAVR .
Code:
#include <mega16.h>
#include <stdlib.h>
#include <stdio.h>
#include <delay.h>
#define xtal 8000000;
// Declare your global variables here
unsigned int Sec=0,Min=0,H=0;
int com1 = 0b11111110; //common cathode of segment 1 on PD0
int com2 = 0b11111101;
int com3 = 0b11111011;
int com4 = 0b11110111;
unsigned char second[16];
unsigned char minute[16];
unsigned char hour[16];
unsigned char zero = 0x3F; //declare decimal digits for show on 7segment
unsigned char one = 0x06;
unsigned char two = 0x5B; //common cathode
unsigned char three = 0x4F;
unsigned char four = 0x66;
unsigned char five = 0x6D;
unsigned char six = 0x7D;
unsigned char seven = 0x07;
unsigned char eight = 0x7F;
unsigned char nine = 0x6F;
unsigned char select ;
// Timer 1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
if(Sec==59)
{
Sec=0;
Min++;
}
else
Sec++;
if(Min==59)
{
Min=0;
H++;
}
else
Min++;
if(H==23)
H=0;
else
H++;
PORTB.0 = 1; //dot point (must blink every 1 second)
}
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func0=Out Func1=Out Func2=Out Func3=Out Func4=Out Func5=Out Func6=Out Func7=Out
// State0=0 State1=0 State2=0 State3=0 State4=0 State5=0 State6=0 State7=0
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func0=Out Func1=Out Func2=Out Func3=Out Func4=Out Func5=In Func6=In Func7=In
// State0=0 State1=0 State2=0 State3=0 State4=0 State5=T State6=T State7=T
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
PORTC=0x00;
DDRC=0xFF;
// Port D initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
PORTD=0b11111111;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 7.813 kHz
// Mode: CTC top=OCR1A
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x0D;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x1E;
OCR1AL=0xC2;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
GICR|=0x00;
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x10;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
// Analog Comparator Output: Off
ACSR=0x80;
SFIOR=0x00;
while(1)
{
PORTB.0 = 0; //dot point off
itoa(Sec,second);
itoa(Min,minute);
itoa(H,hour);
//Digit 1
select = hour[1];
switch (select)
{
case 0:
{(PORTC = zero); break;}
case 1:
{(PORTC = one); break;}
case 2:
{(PORTC = two); break;}
case 3:
{(PORTC = three); break;}
case 4:
{(PORTC = four); break;}
case 5:
{(PORTC = five); break;}
case 6:
{(PORTC = six); break;}
case 7:
{(PORTC = seven); break;}
case 8:
{(PORTC = eight); break;}
case 9:
{(PORTC = nine); break;}
}
PORTD=com1;
delay_ms(10);
PORTD=0x00;
//Digit 2
select = hour[2];
switch (select)
{
case 0:
{(PORTC = zero); break;}
case 1:
{(PORTC = one); break;}
case 2:
{(PORTC = two); break;}
case 3:
{(PORTC = three); break;}
case 4:
{(PORTC = four); break;}
case 5:
{(PORTC = five); break;}
case 6:
{(PORTC = six); break;}
case 7:
{(PORTC = seven); break;}
case 8:
{(PORTC = eight); break;}
case 9:
{(PORTC = nine); break;}
}
PORTD=com2;
delay_ms(10);
PORTD=0x00;
//Digit 3
select = minute[1];
switch (select)
{
case 0:
{(PORTC = zero); break;}
case 1:
{(PORTC = one); break;}
case 2:
{(PORTC = two); break;}
case 3:
{(PORTC = three); break;}
case 4:
{(PORTC = four); break;}
case 5:
{(PORTC = five); break;}
case 6:
{(PORTC = six); break;}
case 7:
{(PORTC = seven); break;}
case 8:
{(PORTC = eight); break;}
case 9:
{(PORTC = nine); break;}
}
PORTD=com3;
delay_ms(10);
PORTD=0x00;
//Digit 4
select = minute[2];
switch (select)
{
case 0:
{(PORTC = zero); break;}
case 1:
{(PORTC = one); break;}
case 2:
{(PORTC = two); break;}
case 3:
{(PORTC = three); break;}
case 4:
{(PORTC = four); break;}
case 5:
{(PORTC = five); break;}
case 6:
{(PORTC = six); break;}
case 7:
{(PORTC = seven); break;}
case 8:
{(PORTC = eight); break;}
case 9:
{(PORTC = nine); break;}
}
PORTD=com4;
delay_ms(10);
PORTD=0x00;
}
// Global enable interrupts
#asm("sei")
}
Last edited: