Atmega16 clock with 7segment

Status
Not open for further replies.

rezaf

Advanced Member level 4
Joined
Apr 11, 2009
Messages
106
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Location
Montagram
Activity points
2,147
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 .

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:

I changed place of #asm sei but the problem not resolved. please see the attached picture.
 

You should also reset the counter at the start of each interrupt because if you don't it will continue to count up to 65535

Code:
// Timer 1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{                              
  [COLOR="#FF0000"]TCNT1=0;[/COLOR]  // reset counter 1
  if(Sec==59)
    {
     Sec=0;
     Min++;
...........

This part is also wrong
Code:
if(Sec==59)
    {
     Sec=0;
     Min++;
    } 
  else
     Sec++; 
    
  if(Min==59)
    { 
     Min=0;
     H++;
    } 
  else
     Min++;
    
  if(H==23)
     H=0;
  else             
     H++;

it incenses all three variable is each interrupt.
Think about it, when the sec is 1 then Min==59 is false and H==23 is false too so they both increase.
You have to write a different condition, for example

Code:
if(Sec==59)
{
    Sec=0;
    if(Min==59)
    {
        Min=0;
        if(H==23)
            H=0;
        else
            H++;

    }
    else
        Min++;
}
else
    Sec++;

Alex

---------- Post added at 10:33 ---------- Previous post was at 10:28 ----------

Also note that when you have an array like unsigned char second[16];
then the data is second[0] to second[15] and not second[1] to second[16]

so you should change your code to read the first and second byte instead of the second and third byte

Alex
 

Your digit decode method will also fail for two additional reasons:
- itoa() doesn't generate leading zeros
- you're comparing ASCII strings with 0 - 9 instead of '0' to '9'
 

Very Thanks Friends. with your helps I change the code and problems resolved. the Final Code is below :

Code:
#include <mega16.h>
#include <stdlib.h>
#include <stdio.h>
#include <delay.h>
#define xtal 8000000;
// Declare your global variables here 

unsigned int com1 = 0b11111110;
unsigned int com2 = 0b11111101;
unsigned int com3 = 0b11111011;
unsigned int com4 = 0b11110111;
unsigned int empty = 0b11111111;

char second[16];
char minute[16];
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;

char select ;

int Sec=0,Min=0,H=0;

// Timer 1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{                              
TCNT1=0;  // reset counter 1
if(Sec==59)
{
    Sec=0;
    if(Min==59)
    {
        Min=0;
        if(H==23)
            H=0;
        else
            H++;

    }
    else
        Min++;
}
else
    Sec++;
     
   itoa(H,hour);
   itoa(Min,minute);
   itoa(Sec,second); 
   
   if (Min <= 9)
    {
     minute[1] = minute[0];
     minute[0] = 0x30;
    } 
   if (H <= 9)
    {
     hour[1] = hour[0];
     hour[0] = 0x30;
    } 
     
//Digit 1

       switch (hour[0])
        {
         case 0x30:
          {(PORTC = zero);  break;}
         case 0x31:
          {(PORTC = one);   break;}
         case 0x32:
          {(PORTC = two);   break;}
         case 0x33:
          {(PORTC = three); break;}
         case 0x34:
          {(PORTC = four);   break;}
         case 0x35:
          {(PORTC = five);   break;}
         case 0x36:
          {(PORTC = six);    break;}
         case 0x37:
          {(PORTC = seven); break;}
         case 0x38:
          {(PORTC = eight); break;}
         case 0x39:
          {(PORTC = nine);  break;}
       }  
    PORTD=com1;
    delay_ms(50);
    PORTD=empty;
    PORTC = 0b00000000;

//Digit 2
       switch (hour[1])
        {
         case 0x30:
          {(PORTC = zero);  break;}
         case 0x31:
          {(PORTC = one);   break;}
         case 0x32:
          {(PORTC = two);   break;}
         case 0x33:
          {(PORTC = three); break;}
         case 0x34:
          {(PORTC = four);   break;}
         case 0x35:
          {(PORTC = five);   break;}
         case 0x36:
          {(PORTC = six);    break;}
         case 0x37:
          {(PORTC = seven); break;}
         case 0x38:
          {(PORTC = eight); break;}
         case 0x39:
          {(PORTC = nine);  break;}
       }  
    PORTD=com2;
    PORTB.0 = 1;
    delay_ms(50);
    PORTD=empty;
    PORTB.0 = 0;
    PORTC = 0b00000000;

//Digit 3
       switch (minute[0])
        {
         case 0x30:
          {(PORTC = zero);  break;}
         case 0x31:
          {(PORTC = one);   break;}
         case 0x32:
          {(PORTC = two);   break;}
         case 0x33:
          {(PORTC = three); break;}
         case 0x34:
          {(PORTC = four);   break;}
         case 0x35:
          {(PORTC = five);   break;}
         case 0x36:
          {(PORTC = six);    break;}
         case 0x37:
          {(PORTC = seven); break;}
         case 0x38:
          {(PORTC = eight); break;}
         case 0x39:
          {(PORTC = nine);  break;}
       }  
    PORTD=com3;
    delay_ms(50);
    PORTD=empty;
    PORTC = 0b00000000;

//Digit 4
       switch (minute[1])
        {
         case 0x30:
          {(PORTC = zero);  break;}
         case 0x31:
          {(PORTC = one);   break;}
         case 0x32:
          {(PORTC = two);   break;}
         case 0x33:
          {(PORTC = three); break;}
         case 0x34:
          {(PORTC = four);   break;}
         case 0x35:
          {(PORTC = five);   break;}
         case 0x36:
          {(PORTC = six);    break;}
         case 0x37:
          {(PORTC = seven); break;}
         case 0x38:
          {(PORTC = eight); break;}
         case 0x39:
          {(PORTC = nine);  break;}
       }  
    PORTD=com4;
    delay_ms(50);
    PORTD=empty;
    PORTC = 0b00000000;
}

    
void main(void)
{
PORTA=0x00;
DDRA=0x00;

PORTB=0x00;
DDRB=0xFF;

PORTC=0x00;
DDRC=0xFF;

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;
// Global enable interrupts
#asm("sei")
}
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…