Making 24 hour clock

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
So, now i have been playing a little, and now i will say it's finished :)

Rich (BB code):
#include <htc.h>
//#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
__CONFIG (FCMEN_ON & IESO_OFF & BOREN_OFF & CPD_OFF & CP_OFF & MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_INTRCIO);
#define LED RC0
#define set_hour RC7
#define set_minut RC5
#define set_week RC6
#define set_time RC3
#define _XTAL_FREQ  4000000
//global defs
volatile unsigned char week_day;
volatile unsigned char hour, minut, sec, x, xx, y, i, sel;
volatile bit minut_tick, set_time_flag,regn_tick ;
volatile bit blink;
volatile int mm;
char timestr[3], regn[4], idag[4];
 const char * const day_of_week_names[] = {"Mandag ",
                                          "Tirsdag",
                                          "Onsdag ",
                                          "Torsdag",
                                          "Fredag ",
                                          "Lordag ",
                 "Sondag " 
                                         };
static void interrupt
isr(void)   // Here is interrupt function - the name is unimportant.
{
 if(TMR1IF) 
  {// Was this a timer overflow?
      TMR1IF=0;//Clear interrupt flag, ready for next
   TMR1H=0x80;
        //If we set TMR1 to start at 0x8000 (32768), the TMR1 will overflow every 1 second
     regn_tick=1;
   blink=!blink;
         LED=blink; 
         sec++;
  if (sec==5)
   {
   minut_tick=1;
   sec=0;
   }
      }
}
void sluk_ur (void)
{
set_time_flag=1;
GIE = 0;  // Global interrupt disable as we are setting the time
 TMR1ON=0;       //timer1 off
 sec=0;
 TMR1H=0x80;
 TMR1L=0;
 LED=1;
}
void start_ur (void)
{
GIE = 1;  // Global interrupt enable
 TMR1ON=1;       //timer1 on
    set_time_flag=0;
}
void setup(void)
{   TRISA0=0;
    TRISA2=1;
    TRISC=0b11111000;//RC0-RC2 out,RC3-RC6 in,RC7 in
    TRISB=0; //All port B output
 GIE = 0;  // Global interrupt disable just in case
 ANSEL=0;
 ANSELH=0;//turn off all analog functions
//timer1 settings
 TMR1H=0x80;
 TMR1L=0;
 //T1CON=0b00000110;//used during debug
    T1CON=0b00001110;
 // See datasheet REGISTER 6-1: T1CON: TIMER 1 CONTROL REGISTER
 // bit order T1GINV TMR1GE T1CKPS1 T1CKPS0 T1OSCEN !T1SYNC TMR1CS TMR1ON
 //Timer1 Interrupt prepare
 TMR1IE=1;// PIE1 register
 PEIE=1; //INTCON register
 PIR1=0; // Clear all bits PERIPHERAL INTERRUPT REQUEST REGISTER 1
   //todo now in order to generate interrupt GEI=1 and TMR1ON=1
//timer0 settings.
//INTCON=0b01100000;
 //setup LCD
    lcd_init();
 lcd_goto(0); // select first line
 //set the globals
week_day=5;
minut=0;
sec=0;
hour=0;
minut_tick=0;
blink=0;
TMR0=eeprom_read(31);
//regn_tick=0;//clear the tick
 }
void lcd_tiden(void)
{   
lcd_clear();
lcd_goto(0x00);
lcd_puts("Klokken er : ");
utoa(timestr, hour, 10);
 if (hour<10)lcd_puts("0");
lcd_puts(timestr);
lcd_puts(":");
utoa(timestr, minut, 10);
 if (minut<10)lcd_puts("0");
lcd_puts(timestr);
 lcd_goto(0x40); // Select second line
 lcd_puts("Dagen er   : ");
    
 for (unsigned char i=0;i<7;i++) lcd_putch(day_of_week_names[week_day]);
lcd_goto(0x00);
 
}
 
 void reset (void)
 {
  lcd_clear();
  lcd_goto(0x04);
  lcd_puts("Slet total ?");
  lcd_goto(0x45);
  lcd_puts("JA / NEJ ");
  lcd_goto(0x00);
 x=0;
 while (x<100)
 {
  if (!set_week)
  {TMR0=0;
   eeprom_write(31,0);
   eeprom_write(32,0);
   lcd_clear();
   lcd_goto(0x06);
   lcd_puts("Slettet !");
   lcd_goto(0x00);
   __delay_ms(1000);
  x=99;
  }
  if(!set_minut)x=0;
  
  x++; 
  __delay_ms(100);
 }
 
 }
 

void vis_regn (void)
{ 
 if (mm<TMR0)
 {
lcd_clear();
lcd_goto(0x00);
lcd_puts("Regn Total  : ");
if (TMR0<100)lcd_puts(" ");
if (TMR0<10)lcd_puts(" ");
utoa(regn, TMR0, 10);
lcd_puts(regn);
lcd_puts(" MM");
eeprom_write(31,TMR0);
lcd_goto(0x40);
lcd_puts("Regn ");
 for (unsigned char i=0;i<7;i++) lcd_putch(day_of_week_names[week_day]);
 lcd_puts(": ");
x= eeprom_read(32);
xx=TMR0-x;
if (xx<100)lcd_puts(" ");
if (xx<10)lcd_puts(" ");
utoa(idag, xx, 10);
 lcd_puts(idag);
 lcd_puts(" MM");
 lcd_goto(0x00);
 mm= atoi(regn);
}
}
void select (void)
 { 
 sel=1;
  x=0;
  __delay_ms(100);
  while (x<70)
 {
 if (!set_hour)
 { lcd_goto(0x40);
  lcd_puts("Regn dag -");
  lcd_goto(0x4a);
  if (sel<10)lcd_puts(" ");
  utoa(regn, sel, 10);
  lcd_puts(regn);
  lcd_goto(0x4c);
  lcd_puts(" :");
  y=eeprom_read(sel-1);
  if (y<100)lcd_puts(" ");
  if (y<10)lcd_puts(" ");
  utoa(regn, y, 10);
  
  lcd_puts(regn);
     lcd_puts(" MM");
  sel++;
     x=0;
     if (sel==31)sel=1;
 __delay_ms(255);
 }
 __delay_ms(100);
  x++; 
 
 }
lcd_goto(0x00);
mm=-1;
vis_regn();
}
void midnat (void)
{
week_day++;
if (week_day==7) week_day=0;
x=eeprom_read(31);
eeprom_write(32,x);
i=0;
while (i<30)
{
 x=eeprom_read(i+1);
eeprom_write(i,x);
i++;
}
mm=-1;
vis_regn();
}
void ur (void)
{
  
  minut++;
  if (minut>=60)
{   minut=0;
   hour++;
   if (hour>=24)
  { hour=0;
       midnat();
   }
}
 
}
void stil_ur (void)
{
 sluk_ur();
 lcd_tiden();
 
   do
   { 
    if (!set_hour)
                 {
                         hour++;
                  if (hour==24) hour=0;
                   lcd_tiden();
                  __delay_ms(200); 
                } 
    if (!set_minut)
               {
      minut++;
      if (minut==60) minut=0;
      lcd_tiden();
                   __delay_ms(200); 
               }    
    if (!set_week)
               {
      week_day++;
                  if (week_day==7) week_day=0;
                  lcd_tiden();
                 __delay_ms(200); 
                }    
         
    } while (!set_time);
 start_ur();
 mm=-1;
vis_regn();
 }
 
void main (void) //hovedprogram
{
setup();
mm=-1;
timestr[2]='\0';
vis_regn();
GIE=1;
TMR1ON=1;
 mm=-1;
       while (1)
   {
     if (minut_tick)
     {
  ur();
  minut_tick=0; 
 
  }  // (minut_tick)
    if (regn_tick)
    {
     vis_regn();
    } 
if (!set_time)
{
stil_ur();
}
if (!set_week)
{lcd_tiden();
 x=0;
while (x<100)
{if (!set_week)x=0;
__delay_ms(100);
x++;
}
mm=-1;
}
if(!set_hour)
{
select();
}
if (!set_minut)
{reset();
 mm=-1;
vis_regn(); 
}
  // flere hvis.....
}
}
 //end while endless loop
//End main   


See what u think of it,
I works well, but need to change from RC4 to RC7, couse somehow the RC4 stayed "low" from pressed until power loos.
Works ok with RC7.
 

t06afre

Joined May 11, 2009
5,934
So, now i have been playing a little, and now i will say it's finished :)
See what u think of it,
I works well, but need to change from RC4 to RC7, couse somehow the RC4 stayed "low" from pressed until power loos.
Works ok with RC7.
You are perhaps a little cheapskate on your comments;). Also if you declare variables above main(). They will be defined global. And the memory will reserved. But if you define them inside a function. The memory allocation will be temporary. The variables char timestr[3], regn[4], idag[4];. Could all have been defined inside the function. As the strings may be discharged after they are sent to the LCD. If you plan to use this forum for help in C coding next time. I think it would be smart to use english name for the functions. Using indentation will make your code more easy to read http://en.wikipedia.org/wiki/Indent_style But all this is minor details. We all have learn how to crawl, before we can walk, and walk before we can run.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Also if you declare variables above main(). They will be defined global. And the memory will reserved. But if you define them inside a function. The memory allocation will be temporary. The variables char timestr[3], regn[4], idag[4];. Could all have been defined inside the function. As the strings may be discharged after they are sent to the LCD.
Have done that now., not much different in mem..
 

THE_RB

Joined Feb 11, 2008
5,438
...
Also if you declare variables above main(). They will be defined global. And the memory will reserved. But if you define them inside a function. The memory allocation will be temporary.
...
That's generally true in C compilers but in C compilers for microcontrollers like PICs etc generally even the vars inside functions will be permanently allocated.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Now it's finished, and finaly the rain has come.

But there is a problem.

When midnight, it "forgets" to "store" in eeprom.

Any reason why not ?
Total is okay stored.

Here is the rutine to change t midnight,

Rich (BB code):
void midnat (void)   ' now it has become midnight
{
week_day++;
if (week_day==7) week_day=0;  ' set weekday
x=eeprom_read(31);  ' 31 and 32 is for total rain ect.
eeprom_write(32,x);
i=0;           ' set the counter to 0.
while (i<30)    ' while to shift all rain from last 30 days,
{
 x=eeprom_read(i+1);  ' read day- 2 ect
eeprom_write(i,x);  ' write it in day -1   ect.
i++;     ' add one more to counter.
}
vis_regn();   ' show the info on lcd again.
}
have tried to declare "x" and "i" as int, and unsigned char,
in all possible ways,
Still it will not stored right,


i cant figure it out.
 
Top