Hello guys, I am making a binary clock with Atmega16. Here's how it works:

When it powers on, it sends you to the configureMode function with variable sec, min and hour zero. You can configure the clock with the following buttons:

PINA0 increment hours by 1

PINA1 increment minutes by 10

PINA2 increment minutes by 1

PINA3 leave conf mode and send these variables (hour, min and sec) as arguments to the displayTime_BCD function.

Now this function counts time and each second, calls the updateSec funct to update the ports in binary according to the current time in decimal. The updateX functions are working OK and so is the baseConv function (since the LEDs blink until 59 seconds is reached). Then the clock just hangs there and does nothing. The problem is in displayTime_BCD function but I don't know what it is.

P.S. Any moment pressing the PINA3 button will send the clock to configuration mode which will be enabled after 2000 milliseconds. Then you can configure the clock again and press PINA3 to pass the hours and minutes to displayTime_BCD funct.

When it powers on, it sends you to the configureMode function with variable sec, min and hour zero. You can configure the clock with the following buttons:

PINA0 increment hours by 1

PINA1 increment minutes by 10

PINA2 increment minutes by 1

PINA3 leave conf mode and send these variables (hour, min and sec) as arguments to the displayTime_BCD function.

Now this function counts time and each second, calls the updateSec funct to update the ports in binary according to the current time in decimal. The updateX functions are working OK and so is the baseConv function (since the LEDs blink until 59 seconds is reached). Then the clock just hangs there and does nothing. The problem is in displayTime_BCD function but I don't know what it is.

P.S. Any moment pressing the PINA3 button will send the clock to configuration mode which will be enabled after 2000 milliseconds. Then you can configure the clock again and press PINA3 to pass the hours and minutes to displayTime_BCD funct.

Code:

```
/*
* BinaryClock.c
*
* Created: 12/30/2011 10:50:06 PM
*/
#include <avr/io.h>
#include <util/delay.h>
int baseConv(int i){
int binaryNum = 0, multiplier = 1;
for (int n = 0; n < 4; ++n){
binaryNum += ( i % 2 ) * multiplier;
i /= 2;
multiplier *= 10;
}
return binaryNum;
}
void updateSec(int sec){
int lowerNib = sec % 10; //second column
lowerNib = baseConv(lowerNib);
int upperNib = sec / 10; //first column
upperNib = baseConv(upperNib);
PORTB = (((upperNib/1000) % 10) << 7)|(((upperNib/100) % 10) << 6)|(((upperNib/10) % 10) << 5)|((upperNib % 10) << 4)|(((lowerNib/1000) % 10) << 3)|(((lowerNib/100) % 10) << 2)|(((lowerNib/10) % 10) << 1)|( (lowerNib % 10) << 0);
}
void updateMin(int min){
int lowerNib = min % 10; //second column
lowerNib = baseConv(lowerNib);
int upperNib = min / 10; //first column
upperNib = baseConv(upperNib);
PORTC = (((upperNib/1000) % 10) << 7)|(((upperNib/100) % 10) << 6)|(((upperNib/10) % 10) << 5)|((upperNib % 10) << 4)|(((lowerNib/1000) % 10) << 3)|(((lowerNib/100) % 10) << 2)|(((lowerNib/10) % 10) << 1)|( (lowerNib % 10) << 0);
}
void updateHour(int hour){
int lowerNib = hour % 10; //second column
lowerNib = baseConv(lowerNib);
int upperNib = hour / 10; //first column
upperNib = baseConv(upperNib);
PORTD = (((upperNib/1000) % 10) << 7)|(((upperNib/100) % 10) << 6)|(((upperNib/10) % 10) << 5)|((upperNib % 10) << 4)|(((lowerNib/1000) % 10) << 3)|(((lowerNib/100) % 10) << 2)|(((lowerNib/10) % 10) << 1)|( (lowerNib % 10) << 0);
}
void displayTime_BCD(int _hour, int _min, int _sec){
while(1)
{
for (; _hour < 24; ++_hour )
{
updateHour(_hour);
for (; _min < 60; ++_min )
{
updateMin(_min);
for (; _sec < 60; ++_sec )
{
updateSec(_sec);
if (bit_is_clear(PINA, 3)){
configureMode(_hour, _min, _sec);
}
_delay_ms(1000);
}
}
}
}
}
void configureMode(int hour, int min, int sec){
sec = 0;
_delay_ms(2000);
while(1){
if (bit_is_clear(PINA, 0))
{
++hour;
if (hour >= 24)
{
hour %= 24;
}
updateHour(hour);
}
if (bit_is_clear(PINA, 1))
{
min +=10;
if (min >= 60)
{
min %= 60;
++hour;
if (hour >= 24)
{
hour %= 24;
updateHour(hour);
}
}
updateMin(min);
}
if (bit_is_clear(PINA, 2))
{
++min;
if (min >= 60)
{
min %= 60;
++hour;
if (hour >= 24)
{
hour %= 24;
updateHour(hour);
}
}
updateMin(min);
}
if (bit_is_clear(PINA, 3))
{
displayTime_BCD(hour, min, sec);
}
}
}
void main(void)
{
//DECLARE THE DATA DIRECTION BITS
DDRB = DDRC = 0x7F;
DDRD = 0x3F;
DDRA = 0x00;
//FIRST FOUR PINS OF A AND PIN 7 OF B WILL BE USED FOR INPUT
PORTB = 0b10000000;
PORTA = 0x0F;
configureMode(0,0,0);
}
```

Last edited: