Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Please help me in displaying time using ATmega 16

Status
Not open for further replies.

NIKO BELLIC

Newbie level 6
Joined
Aug 1, 2011
Messages
14
Helped
6
Reputation
12
Reaction score
5
Trophy points
1,283
Location
Kansas City, MO
Activity points
1,389
Hi all,
Please someone help me...!! I'm facing a glitch in displaying the time on 4 seven segment LEDs using ATmega 16. Recently, I've learned how to count numbers from 00 to 99 on two 7 segment LEDs using multiplexing technique. So, to step further, I decided to write a program to display time (digital clock for max 1hr). My simple logic behind the code is that, if seconds reaches to 59, then reset seconds and increment minutes and continue counting seconds. I've faced few bugs at first and after clearing all of them, I could not see the output what I expected. After reaching 59 seconds, instead of showing 01 in minutes block, it is showing 59...!!?
My code...:
==================================
Code:
// This program starts counting the time on power up...
// Microcontroller : ATmega 16
// Compiler         : WinAVR - 20100110
// Language       : C

#include <avr/io.h>
#include <util/delay.h>

void main (void)
{
 // Initialization of ports
 DDRA=0xFF;
 DDRB=0xFF;
 unsigned int values[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; // Includes numbers from 0 to 9
 unsigned int seconds,var1,num1,num2,num3,num4,var2,minutes;
 for(seconds=0;seconds<60;seconds++)
 {
  var1=seconds;
  
  num1=var1%10;					// Extracting Units place of seconds
  var1=var1-num1;
  var1=var1/10;
  
  num2=var1%10;					// Extracting tens place of seconds
  var1=var1-num1;
  var1=var1/10;
  
  for(int i=0;i<5;i++)			// Delay between two consective counts
  { 
   PORTB=(1<<PB0)|(0<<PB1);       	// Turning ON Units place of seconds
   PORTA=values[num1];			// Writing corresponding number on Units place of seconds	
   _delay_ms(5);				// Very short delay
   
   PORTB=(0<<PB0)|(1<<PB1);	            // Turning ON Tens place and Turning OFF Units place of seconds 
   PORTA=values[num2];			// Writing corresponding number on Tens place of seconds
   _delay_ms(5);				// Very Short delay
   if (seconds==59)				// Checking condition that if seconds = 59
   {
    seconds=0;					// Resetting seconds value to 00
	for(minutes=1;minutes<60;minutes++)
	{
	 var2=minutes;				
	 
	 num3=var2%10;				// Extracting Units place of minutes
	 var2=var2-num3;
	 var2=var2/10;
	 
	 num4=var2%10;				// Extracting Tens place of minutes
	 var2=var2-num4;
	 var2=var2/10;
	}
   }
   PORTB=(1<<PB2)|(0<<PB3);	           // Turning ON Units place of minutes
   PORTA=values[num3];			// Writing corresponding number on Units place of minutes
   _delay_ms(5);				// Very short delay
	
   PORTB=(0<<PB2)|(1<<PB3);	           // Turning ON Tens place and Turning OFF Units place of minutes
   PORTA=values[num4];			// Writing corresponding number on Tens place of minutes
   _delay_ms(5);				// Very Short delay
  }
 }
}
==================================
Proteus Design file...
 

I'm facing a glitch in displaying the time on 4 seven segment LEDs using ATmega 16. Recently, I've learned how to count numbers from 00 to 99 on two 7 segment LEDs using multiplexing technique. So, to step further, I decided to write a program to display time (digital clock for max 1hr). My simple logic behind the code is that, if seconds reaches to 59, then reset seconds and increment minutes and continue counting seconds. I've faced few bugs at first and after clearing all of them, I could not see the output what I expected. After reaching 59 seconds, instead of showing 01 in minutes block, it is showing 59...!!?

The problem is due to the following section of code:

Code:
....
....			
   if (seconds==59)				// Checking condition that if seconds = 59
   {
    seconds=0;					// Resetting seconds value to 00
	[COLOR="#FF0000"]for(minutes=1;minutes<60;minutes++)
	{
	 var2=minutes;				
	 
	 num3=var2%10;				// Extracting Units place of minutes
	 var2=var2-num3;
	 var2=var2/10;
	 
	 num4=var2%10;				// Extracting Tens place of minutes
	 var2=var2-num4;
	 var2=var2/10;
	}[/COLOR]
   }
....
....
....

After the variable seconds reaches 59, the following for loop for the minutes count continues to count from 1 to 59 and then exits the for loop. I would suggest removing the for loop and just testing the minutes variable against equality with 59, clearing if true and increment you minutes variable if false.

BigDog

---------- Post added at 17:32 ---------- Previous post was at 17:22 ----------

Something along these lines:

Code:
....
....			
 if (seconds==59)				// Checking condition that if seconds = 59
 {
    seconds=0;					// Resetting seconds value to 00
	
    if(minutes==59)
        minutes=0;
    else
        minutes++;
    
     var2=minutes;				
	 
     num3=var2%10;				// Extracting Units place of minutes
     var2=var2-num3;
     var2=var2/10;
	 
     num4=var2%10;				// Extracting Tens place of minutes
     var2=var2-num4;
     var2=var2/10;

 }
....
....
....

BigDog
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top