Wednesday, September 16, 2009

POV/BCD Clock

POV(persistence of vision)/BCD(binary coded decimal) Clock


*POV mode:

*BCD mode:



source code for PIC16F microcontroller:
 /////////////////////////////////////////////////////  
// Persistence Of Vision (POV) clock
// Binary Coded Decimal (BCD) clock
// Hi-Tech C code by YUS of ElectronicsLab.Ph
// revision 2.1 Nov 24, 2008
/////////////////////////////////////////////////////

#include <htc.h>
#define _XTAL_FREQ 32768
__CONFIG(XT & WDTDIS & PWRTEN & BORDIS & DUNPROT & WRTEN & UNPROTECT);

#define leds PORTC
#define leds_tris() TRISC = 0x00;
#define adjust RB7
#define adjust_minutes RB6
#define adjust_hours RB5

const unsigned char digits[10][5]={ //10 digits, 5 bytes each
0xc1,0xae,0xb6,0xba,0xc1, // 0
0xff,0xbd,0x80,0xbf,0xff, // 1
0xbd,0x9e,0xae,0xb6,0xb9, // 2
0xde,0xbe,0xba,0xb4,0xce, // 3
0xe7,0xeb,0xed,0x80,0xef, // 4
0xd8,0xba,0xba,0xba,0xc6, // 5
0xc3,0xb5,0xb6,0xb6,0xcf, // 6
0xfe,0x8e,0xf6,0xfa,0xfc, // 7
0xc9,0xb6,0xb6,0xb6,0xc9, // 8
0xf9,0xb6,0xb6,0xd6,0xe1,}; // 9
/*
const unsigned char am_pm[2][11]={
0x81,0xee,0xee,0xee,0x81,0xff,0x80,0xfd,0xf3,0xfd,0x80, // AM
0x80,0xf6,0xf6,0xf6,0xf9,0xff,0x80,0xf6,0xf6,0xf6,0xf9,}; // PM
*/
unsigned char hours, minutes, seconds;
//unsigned char hours_tens, hours_ones, minutes_tens, minutes_ones;

// Interrupt Handler
void interrupt isr()
{
// Timer0 Interrupt - Freq = 0.50 Hz - Period = 2.000000 seconds
if (TMR0IF) // timer 0 interrupt flag
{
TMR0IF = 0; // clear the flag
seconds += 2; //increment time
}
}
void initialize(void)
{
//initial time
hours = 12;
minutes = 34;
seconds = 0;

//Timer0 Registers Prescaler= 64 - TMR0 Preset = 0 - Freq = 0.50 Hz - Period = 2.000000 seconds
T0CS = 0; // bit 5 TMR0 Clock Source Select bit...0 = Internal Clock (CLKO) 1 = Transition on T0CKI pin
T0SE = 0; // bit 4 TMR0 Source Edge Select bit 0 = low/high 1 = high/low
PSA = 0; // bit 3 Prescaler Assignment bit...0 = Prescaler is assigned to the Timer0
PS2 = 1; // bits 2-0 PS2:PS0: Prescaler Rate Select bits
PS1 = 0;
PS0 = 1;
TMR0 = 0; // preset for timer register

// Interrupt Registers
INTCON = 0; // clear the interrpt control register
TMR0IE = 1; // bit5 TMR0 Overflow Interrupt Enable bit...1 = Enables the TMR0 interrupt
TMR0IF = 0; // bit2 clear timer 0 interrupt flag
GIE = 1; // bit7 global interrupt enable

leds_tris();
RBPU = 0; //enable portB internal pullups
}
void leds_on(unsigned char char_byte) //0xff = all LEDs off; 0x00 = all LEDs on
{
leds = char_byte; // use port C for LEDs
NOP();
}
void check_time(void)
{
if (seconds >= 60) { seconds-=60; minutes+=1; }
if (minutes >= 60) { minutes-=60; hours+=1; }
if (hours > 12) { hours -=12; } //12-hour format
//if (hours > 24) { hours -=24; } //24-hour format
}
void pov_display(void) // persistence of vision (POV) mode
{
unsigned char i, digit, hours_tens[5], hours_ones[5], minutes_tens[5], minutes_ones[5];
digit = hours/10;
for(i=0; i<5; i++) hours_tens[i]= digits[digit][i];
digit = hours%10;
for(i=0; i<5; i++) hours_ones[i]= digits[digit][i];
digit = minutes/10;
for(i=0; i<5; i++) minutes_tens[i]= digits[digit][i];
digit = minutes%10;
for(i=0; i<5; i++) minutes_ones[i]= digits[digit][i];
if ((hours/10)!=0) //'wag ng i-display kung wala naman
{
// hours' tens digit
leds_on(hours_tens[0]);
leds_on(hours_tens[1]);
leds_on(hours_tens[2]);
leds_on(hours_tens[3]);
leds_on(hours_tens[4]);
}
// hours' ones digit
leds_on(hours_ones[0]);
leds_on(hours_ones[1]);
leds_on(hours_ones[2]);
leds_on(hours_ones[3]);
leds_on(hours_ones[4]);
leds_on(0xff);
_delay(5);
// colon
leds_on(0xc9);
_delay(5);
leds_on(0xff);
// minutes' tens digit
leds_on(minutes_tens[0]);
leds_on(minutes_tens[1]);
leds_on(minutes_tens[2]);
leds_on(minutes_tens[3]);
leds_on(minutes_tens[4]);
leds_on(0xff);
_delay(2);
// minutes' ones digit
leds_on(minutes_ones[0]);
leds_on(minutes_ones[1]);
leds_on(minutes_ones[2]);
leds_on(minutes_ones[3]);
leds_on(minutes_ones[4]);
leds_on(0xff);
}
void binary_display(void) //binary coded decimal (BCD) mode
{
leds_on(~( ( (hours/10)<<4) + (hours%10) ) ); // BCD hours
__delay_ms(1000);
leds_on(0xff);
__delay_ms(200);
leds_on(~( ( (minutes/10)<<4) + (minutes%10) ) ); //BCD minutes
__delay_ms(1000);
leds_on(0xff);
__delay_ms(500);
}
void adjust_time(void)
{
TMR0IE = 0; //disable timer0interrupt
while( adjust_hours && adjust_minutes ) binary_display();
while(!adjust)
{
// with BCD display
if(!adjust_minutes)
{
minutes+=1;
check_time();
leds_on(~( ( (minutes/10)<<4) + (minutes%10) ) ); //BCD minutes
__delay_ms(500);
}
if(!adjust_hours)
{
hours+=1;
check_time();
leds_on(~( ( (hours/10)<<4) + (hours%10) ) ); // BCD hours
__delay_ms(500);
}
}
TMR0IE = 1; //enable timer0 interrupt
}
void main()
{
initialize();
while(1)
{
check_time();
if(!adjust) adjust_time();
//choose either pov or binary mode
pov_display(); __delay_ms(200);
//binary_display(); __delay_ms(200);
}
}


9 comments:

  1. sis penge schematic ha... hehe thanks

    ReplyDelete
  2. tukayo.. hehe..
    ito, nahalukay ko na ulit ang proteus schematic nito: http://www.4shared.com/file/138252902/b0004184/pov_binary_clock.html

    ReplyDelete
  3. Hello
    I like it this clock,nice work,I want to built it,Can you post me the code in Hex format,because I know to programm just with hex:(
    Here is my email:macobt@hotmail.com
    Regards macobt

    ReplyDelete
  4. HEX codes below (compiled using HiTech PICC v9.7)
    POV mode: http://codepad.org/J8P3MPto
    Binary mode: http://codepad.org/XVGr3RDa

    ReplyDelete
  5. pag natuto na ko pag program ng mcu eto una kong gagawin.

    ReplyDelete
  6. can u please send me the circuit digaram plz with the list of components

    sexy_hot2012@hotmail.com

    ReplyDelete
  7. I am really happy to say it’s an interesting post to read. I learn new information from your article, you are doing a great job. Keep it up, Teresa Helle This profile can give you a Free Gift Card Generator online 2019

    ReplyDelete