Created
December 16, 2019 10:29
-
-
Save Electronza/842e6f7a57b7f0f797881d218d41d97b to your computer and use it in GitHub Desktop.
Easy PIC v7 Word Clock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************* | |
____ __ ____ ___ ____ ____ __ __ _ ____ __ | |
( __)( ) ( __)/ __)(_ _)( _ \ / \ ( ( \(__ ) / _\ | |
) _) / (_/\ ) _)( (__ )( ) /( O )/ / / _/ / \ | |
(____)\____/(____)\___) (__) (__\_) \__/ \_)__)(____)\_/\_/ | |
Project name: GLCD WORD CLOCK | |
Project page: https://electronza.com/word-clock-glcd-pic18f45k22-mikroc/ | |
Description: Easy PIC v7 Word Clock with GLCD | |
********************************************************************/ | |
/* Test configuration: | |
MCU: PIC18F45K22 | |
http://ww1.microchip.com/downloads/en/DeviceDoc/41412D.pdf | |
Dev.Board: EasyPIC7 - ac:GLCD | |
http://www.mikroe.com/easypic/ | |
Oscillator: 8.0000 MHz Crystal | |
Ext. Modules: GLCD 128x64, KS108/107 controller | |
http://www.mikroe.com/store/components/various/ | |
SW: mikroC PRO for PIC | |
http://www.mikroe.com/mikroc/pic/ | |
*/ | |
// Glcd module connections | |
char GLCD_DataPort at PORTD; | |
sbit GLCD_CS1 at LATB0_bit; | |
sbit GLCD_CS2 at LATB1_bit; | |
sbit GLCD_RS at LATB2_bit; | |
sbit GLCD_RW at LATB3_bit; | |
sbit GLCD_EN at LATB4_bit; | |
sbit GLCD_RST at LATB5_bit; | |
sbit GLCD_CS1_Direction at TRISB0_bit; | |
sbit GLCD_CS2_Direction at TRISB1_bit; | |
sbit GLCD_RS_Direction at TRISB2_bit; | |
sbit GLCD_RW_Direction at TRISB3_bit; | |
sbit GLCD_EN_Direction at TRISB4_bit; | |
sbit GLCD_RST_Direction at TRISB5_bit; | |
// End Glcd module connections | |
// BUTTON DECLATATION | |
sbit HOURS_BTN at LATE2_bit; | |
sbit MINUTES_BTN at LATE1_bit; | |
sbit READY_BTN at LATE0_bit; | |
sbit HOURS_Direction at TRISE2_bit; | |
sbit MINUTES_Direction at TRISE1_bit; | |
sbit READY_Direction at TRISE0_bit; | |
// Variable declartations | |
short hour; | |
short minute; | |
unsigned long counter; | |
unsigned short disp_minute; | |
volatile unsigned short disp_hour; | |
volatile unsigned long count; // it must be declared as volatile!!! | |
char *someText; | |
char hdisp [4]; | |
// Generated using Mikroe Timer Calculator | |
// System clock is too fast to generate one Interrupt every minute | |
// so I combine the use of a counter and a 250ms Interrupt | |
// one minute is 60 * 4 * 250ms = 240 * 250 ms | |
// Timer1 | |
// Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 250 ms | |
//Place/Copy this part in declaration section | |
void InitTimer1(){ | |
T1CON = 0x31; | |
TMR1IF_bit = 0; | |
TMR1H = 0x0B; | |
TMR1L = 0xDC; | |
TMR1IE_bit = 1; | |
INTCON = 0xC0; | |
} | |
void Interrupt(){ | |
if (TMR1IF_bit){ | |
TMR1IF_bit = 0; | |
TMR1H = 0x0B; | |
TMR1L = 0xDC; | |
//Enter your code here | |
count++; | |
} | |
} | |
void delay2S(){ // 2 seconds delay function | |
Delay_ms(2000); | |
} | |
Glcd_Word_Clock(unsigned char hours, unsigned char minutes){ | |
Glcd_Fill(0x00); | |
someText = "IT'S"; | |
Glcd_Write_Text(someText, 0, 0, 2); // Write string | |
disp_hour = hours; | |
switch (minutes) | |
{case 5: | |
someText = "FIVE"; | |
Glcd_Write_Text(someText, 0, 2, 2); // Write string | |
someText = "PAST"; | |
Glcd_Write_Text(someText, 0, 3, 2); // Write string | |
break; | |
case 10: | |
someText = "TEN"; | |
Glcd_Write_Text(someText, 102, 0, 2); // Write string | |
someText = "PAST"; | |
Glcd_Write_Text(someText, 0, 3, 2); // Write string | |
break; | |
case 15: | |
someText = "QUARTER"; | |
Glcd_Write_Text(someText, 0, 1, 2); // Write string | |
someText = "PAST"; | |
Glcd_Write_Text(someText, 0, 3, 2); // Write string | |
break; | |
case 20: | |
someText = "TWENTY"; | |
Glcd_Write_Text(someText, 77, 1, 2); // Write string | |
someText = "PAST"; | |
Glcd_Write_Text(someText, 0, 3, 2); // Write string | |
break; | |
case 25: | |
someText = "TWENTY"; | |
Glcd_Write_Text(someText, 77, 1, 2); // Write string | |
someText = "FIVE"; | |
Glcd_Write_Text(someText, 0, 2, 2); // Write string | |
someText = "PAST"; | |
Glcd_Write_Text(someText, 0, 3, 2); // Write string | |
break; | |
case 30: | |
someText = "HALF"; | |
Glcd_Write_Text(someText, 49, 0, 2); // Write string | |
someText = "PAST"; | |
Glcd_Write_Text(someText, 0, 3, 2); // Write string | |
break; | |
case 35: | |
someText = "THIRTY"; | |
Glcd_Write_Text(someText, 77, 1, 2); // Write string | |
someText = "FIVE"; | |
Glcd_Write_Text(someText, 0, 2, 2); // Write string | |
someText = "PAST"; | |
Glcd_Write_Text(someText, 0, 3, 2); // Write string | |
break; | |
case 40: | |
disp_hour++; | |
if (disp_hour > 12){ | |
disp_hour = 0; | |
} | |
someText = "TWENTY"; | |
Glcd_Write_Text(someText, 77, 1, 2); // Write string | |
someText = "TO"; | |
Glcd_Write_Text(someText, 112, 2, 2); | |
break; | |
case 45: | |
disp_hour++; | |
if (disp_hour > 12){ | |
disp_hour = 0; | |
} | |
someText = "QUARTER"; | |
Glcd_Write_Text(someText, 0, 1, 2); // Write string | |
someText = "TO"; | |
Glcd_Write_Text(someText, 112, 2, 2); | |
break; | |
case 50: | |
disp_hour++; | |
if (disp_hour > 12){ | |
disp_hour = 0; | |
} | |
someText = "TEN"; | |
Glcd_Write_Text(someText, 102, 0, 2); // Write string | |
someText = "TO"; | |
Glcd_Write_Text(someText, 112, 2, 2); | |
break; | |
case 55: | |
disp_hour++; | |
if (disp_hour > 12){ | |
disp_hour = 0; | |
} | |
someText = "FIVE"; | |
Glcd_Write_Text(someText, 0, 2, 2); // Write string | |
someText = "TO"; | |
Glcd_Write_Text(someText, 112, 2, 2); | |
break; | |
} | |
// We finished with the minutes, now we do the same for hours | |
switch (disp_hour) | |
{case 1: | |
someText = "ONE"; | |
Glcd_Write_Text(someText, 53, 3, 2); // Write string | |
break; | |
case 2: | |
someText = "TWO"; | |
Glcd_Write_Text(someText, 103, 3, 2); // Write string | |
break; | |
case 3: | |
someText = "THREE"; | |
Glcd_Write_Text(someText, 0, 4, 2); // Write string | |
break; | |
case 4: | |
someText = "FOUR"; | |
Glcd_Write_Text(someText, 50, 4, 2); // Write string | |
break; | |
case 5: | |
someText = "FIVE"; | |
Glcd_Write_Text(someText, 93, 4, 2); // Write string | |
break; | |
case 6: | |
someText = "SIX"; | |
Glcd_Write_Text(someText, 0, 5, 2); // Write string | |
break; | |
case 7: | |
someText = "SEVEN"; | |
Glcd_Write_Text(someText, 35, 5, 2); // Write strin | |
break; | |
case 8: | |
someText = "EIGHT"; | |
Glcd_Write_Text(someText, 85, 5, 2); // Write string | |
break; | |
case 9: | |
someText = "NINE"; | |
Glcd_Write_Text(someText, 0, 6, 2); // Write string | |
break; | |
case 10: | |
someText = "TEN"; | |
Glcd_Write_Text(someText, 41, 6, 2); // Write string | |
break; | |
case 11: | |
someText = "ELEVEN"; | |
Glcd_Write_Text(someText, 75, 6, 2); // Write string | |
break; | |
case 12: | |
someText = "TWELVE"; | |
Glcd_Write_Text(someText, 0, 7, 2); // Write string | |
break; | |
} | |
someText = "O'CLOCK"; | |
Glcd_Write_Text(someText, 66, 7, 2); // Write string | |
Delay_ms(2000); | |
} | |
void main() { | |
hour = 1; | |
minute = 0; | |
// SET BUTTONS AS INPUTS | |
HOURS_Direction = 1; | |
MINUTES_Direction = 1; | |
READY_Direction = 1; | |
ANSELE = 0x00; | |
ANSELB = 0; // Configure PORTB pins as digital I/O | |
ANSELD = 0; // Configure PORTD pins as digital I/O | |
Glcd_Init(); // Initialize GLCD | |
Glcd_Fill(0x00); // Clear GLCD | |
//Initial screen | |
Glcd_Set_Font(System3x5, 3, 5, 32); // Change font | |
someText = "PIC18F45K22 GLCD FAKE WORD CLOCK"; | |
Glcd_Write_Text(someText, 0, 0, 1); // Write string | |
someText = "--------------------------------"; | |
Glcd_Write_Text(someText, 0, 1, 1); // Write string | |
someText = "MIKROC CODE BY:"; | |
Glcd_Write_Text(someText, 0, 2, 1); // Write string | |
someText = "MICROCONTROLLER-PROJECTS.COM"; | |
Glcd_Write_Text(someText, 0, 3, 1); // Write string | |
someText = "--------------------------------"; | |
Glcd_Write_Text(someText, 0, 4, 1); // Write string | |
someText = "PROGRAMMING MODE"; | |
Glcd_Write_Text(someText, 0, 5, 1); // Write string | |
someText = "RE2 - HOURS, RE1 - MINUTES"; | |
Glcd_Write_Text(someText, 0, 6, 1); // Write string | |
someText = "RE0 - RUN, RESET TO PROGRAM"; | |
Glcd_Write_Text(someText, 0, 7, 1); // Write string | |
Delay2s(); | |
// WAIT FOR A PROGRAMMING BUTTON TO BE PRESSED | |
// DEBOUNCING SET TO 1MS | |
while(Button(&PORTE, 2, 1, 1) & Button(&PORTE, 1, 1, 1)){ | |
Delay_ms(50); //Just wait and do nothing | |
} | |
// ENTER PROGRAMMING MODE | |
Glcd_Fill(0x00); | |
someText = "SET TIME IN 12H FORMAT"; | |
Glcd_Write_Text(someText, 0, 0, 1); // Write string | |
someText = "--------------------------------"; | |
Glcd_Write_Text(someText, 0, 1, 1); // Write string | |
someText = "RE2 TO INCREMENT HOURS"; | |
Glcd_Write_Text(someText, 0, 2, 1); // Write string | |
someText = "RE1 TO INCREMENT MINUTES"; | |
Glcd_Write_Text(someText, 0, 3, 1); // Write string | |
someText = "RE0 TO END"; | |
Glcd_Write_Text(someText, 0, 4, 1); // Write string | |
someText = "--------------------------------"; | |
Glcd_Write_Text(someText, 0, 5, 1); // Write string | |
someText = "TIME IS:"; | |
Glcd_Write_Text(someText, 0, 6, 1); // Write string | |
ShortToStr(hour, hdisp); | |
Glcd_Write_Text(hdisp, 35, 6, 1); // Write string | |
ShortToStr(minute, hdisp); // Write string | |
Glcd_Write_Text(hdisp, 51, 6, 1); // Write string | |
someText = ":"; | |
Glcd_Write_Text(someText, 53, 6, 1); | |
while (Button(&PORTE, 0, 1, 1)){ | |
if (Button(&PORTE, 2, 1, 0)){ // increment hours | |
someText = " "; | |
Glcd_Write_Text(someText, 35, 6, 1); | |
hour++; | |
if (hour > 12){ | |
hour = 0; | |
} | |
ShortToStr(hour, hdisp); | |
Glcd_Write_Text(hdisp, 35, 6, 1); // Write string | |
ShortToStr(minute, hdisp); // Write string | |
Glcd_Write_Text(hdisp, 51, 6, 1); // Write string | |
someText = ":"; | |
Glcd_Write_Text(someText, 53, 6, 1); | |
Delay_ms(200); | |
} | |
if (Button(&PORTE, 1, 1, 0)){ // increment hours | |
someText = " "; | |
Glcd_Write_Text(someText, 35, 6, 1); | |
minute++; | |
if (minute > 59){ | |
minute = 0; | |
} | |
ShortToStr(hour, hdisp); | |
Glcd_Write_Text(hdisp, 35, 6, 1); // Write string | |
ShortToStr(minute, hdisp); // Write string | |
Glcd_Write_Text(hdisp, 51, 6, 1); // Write string | |
someText = ":"; | |
Glcd_Write_Text(someText, 53, 6, 1); | |
Delay_ms(200); | |
Delay_ms(200); | |
} | |
} | |
// TIME IS SET. NOW WE GO TO THE WORD CLOCK ROUTINE | |
count = 0; | |
InitTimer1(); | |
Glcd_Set_Font(Character8x7, 8, 7, 32); // Change font for clock | |
// now we update the lcd | |
disp_minute = 5 * (unsigned) ( minute / 5 ); //round to five | |
Glcd_Word_Clock(hour, disp_minute); | |
while(1) { | |
// we count from 5 min to 5 min | |
if (count == 240){ // 1 min = 60 sec = 60 * (240 * 250ms) = 14400 * 250ms | |
count = 0; | |
minute++; | |
if (minute > 59){ | |
minute = 0; | |
hour = hour++; | |
if (hour > 12){ | |
hour = 0; | |
} | |
} | |
disp_minute = 5 * (unsigned) ( minute / 5 ); //round to five | |
Glcd_Word_Clock(hour, disp_minute); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment