Making 24 hour clock

t06afre

Joined May 11, 2009
5,934
I just checked my old 16F88 single chip Charlieplexed Clock/Calendar/Timer project which was written in BoostC and it only uses 711 words of program memory
Nice and small project. Your crystal is to big to be clock crytsal. Did you use timer 1 , and the compare module approach? And what was your timer tick interrupt rate?
 

MMcLaren

Joined Feb 14, 2010
861
MMcLaren said:
I just checked my old 16F88 single chip Charlieplexed Clock/Calendar/Timer project (written in BoostC) and it only uses 711 words of program memory...
Nice and small project. Your crystal is to big to be clock crytsal. Did you use timer 1 , and the compare module approach? And what was your timer tick interrupt rate?
The crystal is 16-MHz (what I had on hand) and I'm using 1-msec (1-kHz) TMR2 interrupts to drive the display and the RTC code. I've attached the source if anyone would like to take a peek.

Regards, Mike
 

Attachments

Last edited:

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
some question abouth C , i am use to VB.net. in windowsprogramming
but i think i will figure it out ,. }}}} and //

and need the r1 in my setup, but fix it tomorrow, not that big deal
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Got my setup fixed and all works well.
Changed some code..
Rich (BB code):
void lcd_format_and_send(void)
{          blink=!blink;
           LED=blink; 
           if (blink) lcd_puts("Next LED will be off");
            else lcd_puts("Next LED will be on ");
   lcd_goto(0x40); // Select second line
             //lcd_puts("day of week ");
            //lcd_putch(week_day+48);
            lcd_puts(day_of_week_names[week_day]);
            lcd_goto(0x00);
}
void skriv(void)
{  week_day++;
            if (week_day==7) week_day=0;
            lcd_format_and_send();//send time to LCD
  
}
void main (void) 
 { setup();
      GIE=1;
   TMR1ON=1;
       while (1)
        {
           if (timer_tick)
    { 
skriv();
 timer_tick=0; 
}  // end if (timer_tick) Memo to my self put all this in sub function
 } //end while endless loop
 }//End main
take some time to get it working, cause the R1 i have mounted wrong, and it did not work,. but now all is okay,
Wondering how to make the clock, do i need a "counter" that counts 60 tick to get the minutes, cause i do not need the second
 

t06afre

Joined May 11, 2009
5,934
You will also generate a timer_tick every second. So yes you have to count up to 60 seconds in silence before updating the display.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
143.26 illegal conversion of integer to pointer

Rich (BB code):
if (blink) lcd_puts(minut);
have decrared
Rich (BB code):
volatile int time,minut,sec;
why ?
 

t06afre

Joined May 11, 2009
5,934
The lcd_puts funtion expect a string as input. Also take a look at section "
3.4 SUPPORTED DATA TYPES AND VARIABLES" in the manual in the "........\HI-TECH Software\PICC\9.83\docs" folder. In micro controllers ram are a scarce commodities. So no need to use a int then the number is in the signed/unsigned number range.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Still the same, even after removing the declared int
it should be possible to wite a number on screen.

rebuild again, and now it's okay.
 

t06afre

Joined May 11, 2009
5,934
You are confusing strings and numbers. In C language, strings are stored in an array of char type along with the null terminating character "\0" at the end. To create a string in C you create an array of chars and set each element in the array to a char value that makes up the string. When sizing the string array you need to add plus one to the actual size of the string to make space for the null terminating character, "\0" So if you want to send the number 59 to the LCD you have to create an array of char containing the ASCII codes 53 and 57 and a '\0" As an example
Rich (BB code):
char sec[3];
sec[0]=53;
sec[1]=57;
sec[2]=0;
lcd_puts(sec);
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
okay, so for all second i need to make an array of 60 elements.
sec(0)=00;
sec(1)=01;
sec(2)=02;
...
...
sec(59)=59;

is that what u mean ?
 

t06afre

Joined May 11, 2009
5,934
Lol :Dwell regarding syntax it is nothing wrong in code something like this
Rich (BB code):
const char * ascii_time[] = {"00","01"........"58","59"};
unsigned char seconds;
lcd_puts(ascii_time[seconds]);
but DoH! do not do that. You can do someting like this
Rich (BB code):
char timestr[3]
void main(void)
{timestr[2]='\0';
.
.
.
utoa(timestr, hours, 10);
lcd_puts(timestr);
lcd_puts(":");
utoa(timestr, minutes, 10);
}
I leave to you to look the utoa function
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Have found out how to use it.
but another problem has accoured.
Rich (BB code):
void lcd_format_and_send(void)
{ 
lcd_goto(0x08);
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(day_of_week_names[week_day]);
 
}
Second line is allways empty.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Can you post the full program?
Sure
Rich (BB code):
///*// Config Register: CONFIG
//#define CONFIG               0x2007
//// Oscillator Selection bits
//// RC oscillator: CLKOUT function on RA4/OSC2/CLKOUT pin, RC on RA5/OSC1/CLKIN
//#define FOSC_EXTRCCLK        0xFFFF
//// RCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, RC on RA5/OSC1/CLKIN
//#define FOSC_EXTRCIO         0xFFFE
//// INTOSC oscillator: CLKOUT function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN
//#define FOSC_INTRCCLK        0xFFFD
//// INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN
//#define FOSC_INTRCIO         0xFFFC
//// EC: I/O function on RA4/OSC2/CLKOUT pin, CLKIN on RA5/OSC1/CLKIN
//#define FOSC_EC              0xFFFB
//// HS oscillator: High-speed crystal/resonator on RA4/OSC2/CLKOUT and RA5/OSC1/CLKIN
//#define FOSC_HS              0xFFFA
//// XT oscillator: Crystal/resonator on RA4/OSC2/CLKOUT and RA5/OSC1/CLKIN
//#define FOSC_XT              0xFFF9
//// LP oscillator: Low-power crystal on RA4/OSC2/CLKOUT and RA5/OSC1/CLKIN
//#define FOSC_LP              0xFFF8
//// Watchdog Timer Enable bit
//// WDT enabled
//#define WDTE_ON              0xFFFF
//// WDT disabled and can be enabled by SWDTEN bit of the WDTCON register
//#define WDTE_OFF             0xFFF7
//// Power-up Timer Enable bit
//// PWRT disabled
//#define PWRTE_OFF            0xFFFF
//// PWRT enabled
//#define PWRTE_ON             0xFFEF
//// MCLR Pin Function Select bit
//// MCLR pin function is MCLR
//#define MCLRE_ON             0xFFFF
//// MCLR pin function is digital input, MCLR internally tied to VDD
//#define MCLRE_OFF            0xFFDF
//// Code Protection bit
//// Program memory code protection is disabled
//#define CP_OFF               0xFFFF
//// Program memory code protection is enabled
//#define CP_ON                0xFFBF
//// Data Code Protection bit
//// Data memory code protection is disabled
//#define CPD_OFF              0xFFFF
//// Data memory code protection is enabled
//#define CPD_ON               0xFF7F
//// Brown-out Reset Selection bits
//// BOR enabled
//#define BOREN_ON             0xFFFF
//// BOR enabled during operation and disabled in Sleep
//#define BOREN_NSLEEP         0xFEFF
//// BOR controlled by SBOREN bit of the PCON register
//#define BOREN_SBODEN         0xFDFF
//// BOR disabled
//#define BOREN_OFF            0xFCFF
//// Internal External Switchover bit
//// Internal External Switchover mode is enabled
//#define IESO_ON              0xFFFF
//// Internal External Switchover mode is disabled
//#define IESO_OFF             0xFBFF
//// Fail-Safe Clock Monitor Enabled bit
//// Fail-Safe Clock Monitor is enabled
//#define FCMEN_ON             0xFFFF
//// Fail-Safe Clock Monitor is disabled
//#define FCMEN_OFF            0xF7FF*/
#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 _XTAL_FREQ  4000000
//global defs
volatile unsigned char week_day;
volatile unsigned char hour, minut, sec;
volatile bit timer_tick, minut_tick;
volatile bit blink;
char timestr[3];
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
     timer_tick=1;
 blink=!blink;
           LED=blink; 
          
  sec++;
  if (sec==60)
 {
 minut_tick=1;
sec=0;
}
  }//we are done here
}
void setup(void)
{   TRISA0=0;
    TRISC=0b01111000;//RC0-RC2 out,RC3-RC6 in,RC7 out
    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
 //setup LCD
    lcd_init();
 lcd_goto(0); // select first line
 //set the globals
     week_day=4;
minut=0;
sec=0;
hour=0;
timer_tick=0;
blink=0;
    //other misc settings
timer_tick=0;//clear the tick
    }
void lcd_format_and_send(void)
{    
week_day=4;     
lcd_goto(0x08);
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(day_of_week_names[week_day]);
          
}
 
void ur (void)
{
  
  minut++;
  if (minut>=60)
{   minut=0;
   hour++;
   if (hour>=24)
  { hour=0;
   
   // midnat();
   }
}
 lcd_format_and_send();
}
void main (void) 
 { setup();
timestr[2]='\0';
      GIE=1;
   TMR1ON=1;
       while (1)
         {
            if (minut_tick)
       { 
    ur();
    //skriv();
     minut_tick=0; 
    }  // end if (minut_tick)
 // flere hvis.....
 } //end while endless loop
 }//End main
 
Top