CLOCK.C
//EXTERNAL SOURCES
extern void tmr_check(int a, int b, int c);
extern void con_set();
// VARIABLES FOR LCD CONNECTIONS
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// INTERRUPT VARIABLES
int int_count = 0 ;
int dd, hh, mm, ss;
int bmin = 0;
char* wdays[7] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
//DAILY TIMER VARIABLES
int tm_set[8] = {1,1,1,1,1,1,1,0}; // Is timer enabled
int tm_on[8] = {930,2460,3480,5100,6480,7680,9000,0}; // time on in int
int tm_off[8] = {990,2520,3540,5160,6540,7740,9060,0}; // time off in int
int tm_act[8] = {0,0,0,0,0,0,0,0};
// timer is currently on
// INTERRUPT RTC
void interrupt(void) {
if(T0IF_bit == 1){ // Check for interrupt flag
T0IF_bit=0; //Reset flag
int_count++ ;
if(int_count > 15625){ // 1 sec 16 Mhz crystal = 16000000 / 4 / 256 =15625
int_count = 0 ;
ss++ ;
if(ss == 60){
ss = 0 ;
mm++ ;
bmin++;
if (bmin == 10080){
bmin = 0;
}
if(mm == 60){
mm = 0 ;
hh++ ;
if(hh == 24){
hh = 0 ;
dd++;
if (dd == 7){
dd = 0;
}
}
}
}
}
}
}
void main(){
//LCD
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_out(1,7,":");
Lcd_out(1,10,":");
//lcd_out(1,12,"55°c");
//TMR0
OPTION_REG = 8;
GIE_bit = 1; //Enable Global Interrupt
T0IF_bit = 0; //Clear interrupt flag
T0IE_bit = 1; //Enable TMR0 interrupt
PEIE_bit = 1; //Peripheral Interrupt Enable bit
//PUSH BUTTON SETTINGs
ADCON1 = 0;
TRISA = 1; //PortA all input
PORTA = 0;
TRISC = 15; // Configure PORTC as output Works on binary count 101 sets port RC0 and RC2
PORTC = 0; // Initial PORTC value
while(1){
//SECONDS
Lcd_Chr(1,12,48+(ss % 10)); // seconds counter LSB
Lcd_Chr(1,11,48+((ss /10) % 10)); //Seconds counter MSB
//MINUTES
Lcd_Chr(1,9,48+(mm % 10)); // minutes counter LSB
Lcd_Chr(1,8,48+((mm /10) % 10)); //minutes counter MSB
//HOURS
Lcd_Chr(1,6,48+(hh % 10)); // hours counter LSB
Lcd_Chr(1,5,48+((hh /10) % 10)); // hours counter MSB
//DAYS
Lcd_out(1,1,wdays[dd]); // days of the week
//CHECK FOR ACTIVE TIMERS
tmr_check(dd,hh,mm); // int cday, int chour,int cmin
//PUSHBUTTON ACCESS SETUP MODE
if (Button(&PORTC, 0, 1, 1)) {//SETUP MODE
Lcd_Cmd(_LCD_CLEAR);
con_set();
}
}
}
TIMERCHECK.C
int tm_val;
extern tm_set[8];extern tm_on[8];extern tm_off[8];
extern tm_act[8];extern char* wdays[7];
//SET TMR ACTIVE
void tm_ico(int dd, int val, char* day, char* msg){
tm_act[dd] = val; Lcd_out(2,1,day); Lcd_out(2,5,msg);
}
//CHECK TIMER STATUS
void tmr_check(int dd, int hh, int mm ){
//CALC BIG MINUTES CURRENT DATE & TIME
tm_val = ((dd*24)*60) + (hh * 60) + (mm);
if (tm_set[8]==1) //IF GROUP TIMER ACTIVE IGNORE ALL OTHER TIMERS
{
/* this is where you default to group timer */
}
else
{
if (tm_set[dd]== 1 ) //IS TIMER ENABLED
{
if (tm_act[dd] == 0) // if timer is not active then check on tm_on
{
if (tm_on[dd] == tm_val){tm_ico(dd,1,wdays[dd],"TMR ON ");} //if time_on >= current day val turn timer on
}
else
{
if (tm_off[dd] == tm_val){ tm_ico(dd,0,wdays[dd],"TMR OFF");} //if time_off >= current day val turn timer off
}
}
}
}
SETUP MODE.C
int mode;
extern dd, hh, mm, ss;
extern char* wdays[7];
int dint=0;
int txt;
void set_min(void){
//lcd_out(1,8,"test");
while(1) {
if (Button(&PORTC, 1, 1, 1)) {
Delay_ms(300);
mm++;
if(mm == 59) {mm = 0;}
}
if (Button(&PORTC, 2, 1, 1)) {
if(mm == 0) {mm = 59;}
Delay_ms(300);
mm--;
}
if (Button(&PORTC, 3, 1, 1)) { // ENTER
//Lcd_Cmd(_LCD_CLEAR);
}
Lcd_Chr(1,9,48+(mm % 10)); // minutes counter LSB
Lcd_Chr(1,8,48+((mm /10) % 10)); //minutes counter MSB
}
}
void set_hour(void){
lcd_out(1,5,"00");
while(1) {
if (Button(&PORTC, 1, 1, 1)) {
Delay_ms(300);
hh++;
if(hh == 24) {hh = 0;}
}
if (Button(&PORTC, 2, 1, 1)) {
if(hh == 0) {hh = 24;}
Delay_ms(300);
hh--;
}
if (Button(&PORTC, 3, 1, 1)) { // ENTER
set_min();
}
Lcd_Chr(1,6,48+(hh % 10)); // hours counter LSB
Lcd_Chr(1,5,48+((hh /10) % 10)); // hours counter MSB
}
}
void set_day(){
while(1){
if (Button(&PORTC, 1, 1, 1)) {
Delay_ms(300); dd++; if(dd == 7) {dd = 0;}
}
if (Button(&PORTC, 2, 1, 1)) {
if(dd == 0) {dd = 7;}Delay_ms(300);dd--;
}
if (Button(&PORTC, 3, 1, 1)) { // ENTER
set_hour();
}
lcd_out(1,1,wdays[dd]);
}
}
void con_set(){
while(1){
switch (mode) {
case 0: lcd_out(1,1,"CLOCK"); break;
case 1: lcd_out(1,1,"TIMERS"); break;
case 2: lcd_out(1,1,"TEMP"); break; }
if (Button(&PORTC, 1, 1, 1)) { //UP
mode++;
Delay_ms(300);
Lcd_Cmd(_LCD_CLEAR);
if (mode == 3) {mode=0;}
}
if (Button(&PORTC, 2, 1, 1)) { //DOWN
if (mode == 0) {mode=3;}
mode--;
Delay_ms(300);
Lcd_Cmd(_LCD_CLEAR);
}
if (Button(&PORTC, 3, 1, 1)) { // ENTER
Lcd_Cmd(_LCD_CLEAR);
Delay_ms(300);
set_day();
}
}
}