Sign In

Trouble In Digital Clock Using Six 7 Segment

ahmad shahnawaz.inst@gmail.com India

Hardware: Atmega16 With Frequency MHz Software: C Using Avr Studio 4

Hello, i am doing a project of digital clock with six 7segment decoder using ATMEGA 16 controller , but i am facing problem in segment blinking. all are always blinking they are not changing the digit without blinking. i do a lot but unable to fix it. please help me..

Images

Program


#define F_CPU	4000000UL
/*#include <avr/delay.h>*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>


#define SegDataPort		PORTB
#define SegDataPin		PINB
#define SegDataDDR		DDRB
 
#define SegCntrlPort	PORTC
#define SegCntrlPin		PINC
#define SegCntrlDDR		DDRC
volatile uint8_t digits[6];

void delay_ms(unsigned int time)		// Time delay function
{
	unsigned int i;
 	for(i=0;i<time;i++)
	_delay_loop_1(0);
}

 
 
/*Global Variables Declarations*/
unsigned char hours = 0;
unsigned char minutes = 0;
unsigned char seconds = 0;
unsigned char SegVal=0x00;
 
 
/*Function Declarations*/
/*****************************************************************************/
/*Decimal Digit (0-9) to Seven Segment Values Encoder*/
/*unsigned char SegVal;*/
unsigned char DigitTo7SegEncoder(unsigned char digit, unsigned char common);
 
/*Timer Counter 1 Compare Match A Interrupt Service Routine/Interrupt Handler*/
ISR(TIMER1_COMPA_vect);
 
 
/*Main Program*/
/*****************************************************************************/
int main(void)
{
    //uint16_t j;
	DDRB=0xFF;
	DDRC=0xFF;
    SegDataDDR = 0xFF;
	SegCntrlDDR = 0x3F;
	SegCntrlPort = 0xFF;
 
	TCCR1B = (1<<CS10|1<<CS11|1<<WGM12);
	OCR1A = 15625-1;
	TIMSK = 1<<OCIE1A;
	sei();
 
 
	while(1)
    {
        /* Set Minutes when SegCntrl Pin 6 Switch is Pressed*/
		if((SegCntrlPin & 0x40) == 0 )
		{	
			delay_ms(500);
			if(minutes < 59)
				minutes++;
			else
				minutes = 0;
		}
        /* Set Hours when SegCntrl Pin 7 Switch is Pressed*/			
		if((SegCntrlPin & 0x80) == 0 )
		{	
			delay_ms(500);
			if(hours < 23)
				hours++;
			else
				hours = 0;
		}			
 
		SegDataPort = DigitTo7SegEncoder(seconds%10,1);
		SegCntrlPort = 0x01;
		
		SegDataPort = DigitTo7SegEncoder(seconds/10,1); 
		SegCntrlPort = 0x02;
		                         
		SegDataPort = DigitTo7SegEncoder(minutes%10,1);
		SegCntrlPort = 0x04;
		
		SegDataPort = DigitTo7SegEncoder(minutes/10,1); 
		SegCntrlPort = 0x08;
		
		SegDataPort = DigitTo7SegEncoder(hours%10,1); 
		SegCntrlPort = 0x10;
		
		SegDataPort = DigitTo7SegEncoder(hours/10,1);
		SegCntrlPort = 0x20; 
		}

	return 0;
}
 
/*
* Function Description:
* Encode a Decimal Digit 0-9 to its Seven Segment Equivalent.
*
* Function Arguments:
* digit - Decimal Digit to be Encoded
* common - Common Anode (0), Common Cathode(1)
* SegVal - Encoded Seven Segment Value 
*
* Connections:
* Encoded SegVal is return in the other G-F-E-D-C-B-A that is A is the least
* significant bit (bit 0) and G bit 6.
*/
unsigned char DigitTo7SegEncoder(unsigned char digit, unsigned char common)
{
	unsigned char SegVal;
 {
 	switch(digit)	
	{	
		case 0:	if(common == 1)	{SegVal = 0b00111111;PORTB=SegVal;delay_ms(50);}
				else			{SegVal = ~0b00111111;PORTB=SegVal;delay_ms(50);}
				break;
		case 1:	if(common == 1)	{SegVal = 0b00000110;PORTB=SegVal;delay_ms(50);}
				else			{SegVal = ~0b00000110;PORTB=SegVal;delay_ms(50);}
				break;
		case 2:	if(common == 1)	{SegVal = 0b01011011;PORTB=SegVal;delay_ms(50);}
				else			{SegVal = ~0b01011011;PORTB=SegVal;delay_ms(50);}
				break;
		case 3:	if(common == 1)	{SegVal = 0b01001111;PORTB=SegVal;delay_ms(50);}
				else			{SegVal = ~0b01001111;PORTB=SegVal;delay_ms(50);}
				break;
		case 4:	if(common == 1)	{SegVal = 0b01100110;PORTB=SegVal;delay_ms(50);}
				else			{SegVal = ~0b01100110;PORTB=SegVal;delay_ms(50);}
				break;
		case 5:	if(common == 1)	{SegVal = 0b01101101;PORTB=SegVal;delay_ms(50);}
				else			{SegVal = ~0b01101101;PORTB=SegVal;delay_ms(50);}
				break;
		case 6:	if(common == 1)	{SegVal = 0b01111101;PORTB=SegVal;delay_ms(50);}
				else			{SegVal = ~0b01111101;PORTB=SegVal;delay_ms(50);}
				break;
		case 7:	if(common == 1)	{SegVal = 0b00000111;PORTB=SegVal;delay_ms(50);}
				else			{SegVal = ~0b00000111;PORTB=SegVal;delay_ms(50);}
				break;
		case 8:	if(common == 1)	{SegVal = 0b01111111;PORTB=SegVal;delay_ms(50);}
				else			{SegVal = ~0b01111111;PORTB=SegVal;delay_ms(50);}
				break;
		case 9:	if(common == 1)	{SegVal = 0b01101111;PORTB=SegVal;delay_ms(50);}
				else			{SegVal = ~0b01101111;PORTB=SegVal;delay_ms(50);}		
	}		
	return SegVal;
}
 }



/*Timer Counter 1 Compare Match A Interrupt Service Routine/Interrupt Handler*/
ISR(TIMER1_COMPA_vect)
{
	 seconds++;          
    {
	 if(seconds == 60)                           
	 {
	  seconds = 0;
     minutes=minutes+1;
	}
	if(minutes == 60)
	{
		minutes = 0;
		hours=hours+1;		
	}
	if(hours > 23)
     hours = 0;
	}
  

}

Post a reply to ahmad

Think you can help ahmad ? Then post your thoughts that might help ahmad . You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.