Temperature

t06afre

Joined May 11, 2009
5,934
@Spinnaker. The project is in basic this design. The OP used a 16F690 internal osc and and a watch crystal to drive timer1. Due to space problems in the 16F690. The OP are now trying to move to the 16F1509. But struggles somewhat with getting this on the air
 

t06afre

Joined May 11, 2009
5,934
Maybe obvious for someone, but why R1 on the crystal? I have not seen that before.

Is the crystal recycled from a watch?
Yes it is a watch crystal only driving timer1. The MCU run on internal oscillator. The series resistance of a watch crystal is quite high. On the order of several Kohms, typical around 40Kohm. It is important, that care be taken to limit the drive to the crystal. Only a fraction of a mA of crystal current will damage this unit, causing it to stop oscillation.
http://www.crystek.com/documents/appnotes/Pierce-GateIntroduction.pdf
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
Have now tried several thing,
But if i use FOSC_INTOSC it runs the interrupt, SIMULATION.
If i use FOSC_INTOSC it will NOT run on chip.
If i use FOSC_XT it will run 4*speed on Chip.
The code is th one that t06afre posted.
just a minor modification.
Rich (BB code):
void lcd_format_and_send(void)
{   char timestr[3];
//lcd_clear();
lcd_goto(0x00);
lcd_puts("Klokken : ");
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_puts(":");
utoa(timestr, sec, 10);
 if (sec<10)lcd_puts("0");
lcd_puts(timestr);
  }
That way i can time it ex by stopwatch, 10 sec and the circiut counts to 40. sec.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
Please show your code that you use to setup the timer. The problem is most likely in there.
Rich (BB code):
 #include <htc.h>
//#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
__CONFIG (FCMEN_ON & IESO_OFF & BOREN_OFF & CP_OFF & MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_INTOSC);//XT
__CONFIG (LVP_ON & LPBOR_OFF & BOREN_ON & STVREN_ON & WRT_OFF);
#define LED RC0
#define _XTAL_FREQ  4000000
//global defs
volatile unsigned char week_day;
volatile unsigned char hour, minut, sec;
volatile bit timer_tick;
volatile bit blink;
const char * const day_of_week_names[] = {"Monday feels so bad",
                                          "Tuesday feel better",
                                          "Wednesday don't go ",
                                          "Thursday goes slow ",
                                          "Friday on my mind  ",
                                          "Saturday Night Live",
                 "Sunday be sporty   " 
                                         };
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;
  }//we are done here
}
void setup(void)
{ 
 IRCF0=1;
      IRCF1=0;
      IRCF2=1;
      IRCF3=1;//4MHz clock speed
//REGISTER 5-1: OSCCON: OSCILLATOR CONTROL REGISTER
// bit 6-3 IRCF<3:0>: Internal Oscillator Frequency Select bits
//1111 = 16MHz
//1110 = 8MHz
//1101 = 4MHz
//1100 = 2MHz
//1011 = 1MHz
//1010 = 500 kHz(1)
//1001 = 250 kHz(1)
//1000 = 125 kHz(1)
//0111 = 500 kHz (default upon Reset)
//0110 = 250 kHz
//0101 = 125 kHz
//0100 = 62.5 kHz
//001x = 31.25 kHz
//000x = 31kHz LF 
    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
 ANSELA=0;
       ANSELB=0;
 ANSELC=0;//turn off all analog functions
//timer1 settings
 TMR1H=0x80;
 TMR1L=0;
 //T1CON=0b00000110;//used during debug
    T1CON=0b10001100;
 // See datasheet REGISTER 19-1: T1CON: TIMER 1 CONTROL REGISTER
 // TMR1CS<1:0> T1CKPS<1:0> T1OSCEN !T1SYNC — TMR1ON
//bit 7-6 TMR1CS<1:0>: Timer1 Clock Source Select bits
//11 =Timer1 clock source is Capacitive Sensing Oscillator (CAPOSC)
//10 =Timer1 clock source is pin or oscillator:
//If T1OSCEN = 0:
//External clock from T1CKI pin (on the rising edge)
// T1OSCEN = 0:
//Crystal oscillator on SOSCI/SOSCO pins
//01 =Timer1 clock source is system clock (FOSC)
//00 =Timer1 clock source is instruction clock (FOSC/4)
//bit 5-4 T1CKPS<1:0>: Timer1 Input Clock Prescale Select bits
//11 = 1:8 Prescale value
//10 = 1:4 Prescale value
//01 = 1:2 Prescale value
//00 = 1:1 Prescale value
//bit 3 T1OSCEN: LP Oscillator Enable Control bit
//1 = Dedicated Timer1 oscillator circuit enabled
//0 = Dedicated Timer1 oscillator circuit disabled
//bit 2 T1SYNC: Timer1 Synchronization Control bit
//1 = Do not synchronize asynchronous clock input
//0 = Synchronize asynchronous clock input with system clock (FOSC)
//bit 1 Unimplemented: Read as ‘0’
//bit 0 TMR1ON: Timer1 On bit
//1 = Enables Timer1
//0 = Stops Timer1 and clears Timer1 gate flip-flop
 TMR1GE=0;
//T1GCON: TIMER1 GATE CONTROL REGISTER
//bit 7 TMR1GE: Timer1 Gate Enable bit
//If TMR1ON = 0:
//This bit is ignored
//If TMR1ON = 1:
//1 = Timer1 counting is controlled by the Timer1 gate function
//0 = Timer1 counts regardless of Timer1 gate function
  //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;
     timer_tick=0;
     blink=0;
     //other misc settings
      timer_tick=0;//clear the tick
    }
void lcd_format_and_send(void)
{   char timestr[3];
//lcd_clear();
lcd_goto(0x00);
lcd_puts("Klokken : ");
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_puts(":");
utoa(timestr, sec, 10);
 if (sec<10)lcd_puts("0");
lcd_puts(timestr);
   //         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 main (void) 
 { setup();
      GIE=1;
   TMR1ON=1;
       while (1)
        {
           if (timer_tick)
           {
            sec++;
         if (sec==60)
   {
   sec=0;
        minut++;
  if (minut>=60)
{   minut=0;
   hour++;
   if (hour>=24)
  { hour=0;
      
  }
}         
   }        
            lcd_format_and_send();//send time to LCD
            timer_tick=0; 
            //timer_tick=1;//just for debug
           }// end if (timer_tick) Memo to my self put all this in sub function
         }//end while endless loop
 }//End main
 

t06afre

Joined May 11, 2009
5,934
Let us do some basic checking. Do you have a LEDs connected on your board? I will first suggest a simple program running on the internal oscillator. Then just using the __delay_ms function to switch on and off a LED. This will check that the MCU is running on the clock speed we expect.
 

t06afre

Joined May 11, 2009
5,934
Also in order to not harm the crystal then doing this simple test. Set RA4 and RA5 as input. And also set the CLKOUTEN_OFF option in the first config word. It may in fact be that this setting is important your program. I did say something about this in post #103 did you test it back then?
 

spinnaker

Joined Oct 29, 2009
7,830
Let us do some basic checking. Do you have a LEDs connected on your board? I will first suggest a simple program running on the internal oscillator. Then just using the __delay_ms function to switch on and off a LED. This will check that the MCU is running on the clock speed we expect.
I understood from the OP that the MCU is running with the XTAL. It is just running 4X faster than expected. More than likely it is timer initialization but I just do not have time right now to look at it.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
Have study a lit.
T1CON=0b10001100; ,
if that is changed to
T1CON=0b10101100;
i think i should count correct, or ??
From page 175
19.8 Timer1 Control Registers
bit 7-6​
TMR1CS<1:0>: Timer1 Clock Source Select bits

11​
=Timer1 clock source is Capacitive Sensing Oscillator (CAPOSC)

10​
=Timer1 clock source is pin or oscillator:
If T1OSCEN =
0:
External clock from T1CKI pin (on the rising edge)
If T1OSCEN =
1:
Crystal oscillator on SOSCI/SOSCO pins

01​
=Timer1 clock source is system clock (FOSC)

00​
=Timer1 clock source is instruction clock (FOSC/4)
bit 5-4
T1CKPS<1:0>: Timer1 Input Clock Prescale Select bits

11​
= 1:8 Prescale value

10
= 1:4 Prescale value

01​
= 1:2 Prescale value

00​
= 1:1 Prescale value
bit 3
T1OSCEN: LP Oscillator Enable Control bit

1​
= Dedicated Timer1 oscillator circuit enabled

0​
= Dedicated Timer1 oscillator circuit disabled
bit 2
T1SYNC: Timer1 Synchronization Control bit

1​
= Do not synchronize asynchronous clock input

0​
= Synchronize asynchronous clock input with system clock (FOSC)
bit 1
Unimplemented: Read as ‘0’
bit 0
TMR1ON: Timer1 On bit

1​
= Enables Timer1

0 = Stops Timer1 and clears Timer1 gate flip-flop

Have not time to test today, but what do you guys mean ?


 

spinnaker

Joined Oct 29, 2009
7,830
I don't see a preset. I don't think you can get 1 second delay at 32.768khz without a preset.

I have another project I really need to work on so sorry but I do not have time to test out the code right now.

Others might criticize me but I like to use this tool.

http://pictimer.picbingo.com/


It really helps to find the setting that you need. Remember that your timer setting will be OSC/4.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
Well. check again :)
Rich (BB code):
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
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
When i sat
Rich (BB code):
 T1CON=0b10101100;
instead of
Rich (BB code):
T1CON=0b10001100;
i get a slower speed,
takes now 40 real seconds to count 60 sec in chip.
if i change the TMR1H=0x80 to TMR1=0x80
i get a slower speed
80 real seconds to count to 60 sec in chip.
Why ??
 

t06afre

Joined May 11, 2009
5,934
When i sat
Rich (BB code):
 T1CON=0b10101100;
instead of
Rich (BB code):
T1CON=0b10001100;
i get a slower speed,
takes now 40 real seconds to count 60 sec in chip.
if i change the TMR1H=0x80 to TMR1=0x80
i get a slower speed
80 real seconds to count to 60 sec in chip.
Why ??
First then you post something like this. You must also post something that explian your changes. It could be as easy as using copy and paste from the datasheet
bit 7-6​
TMR1CS<1:0>: Timer1 Clock Source Select bits

11​
=Timer1 clock source is Capacitive Sensing Oscillator (CAPOSC)

10​
=Timer1 clock source is pin or oscillator:
If T1OSCEN =
0:
External clock from T1CKI pin (on the rising edge)
If T1OSCEN =
1:
Crystal oscillator on SOSCI/SOSCO pins

01​
=Timer1 clock source is system clock (FOSC)

00​
=Timer1 clock source is instruction clock (FOSC/4)
bit 5-4
T1CKPS<1:0>: Timer1 Input Clock Prescale Select bits

11​
= 1:8 Prescale value

10​
= 1:4 Prescale value

01​
= 1:2 Prescale value

00​
= 1:1 Prescale value
bit 3
T1OSCEN: LP Oscillator Enable Control bit

1​
= Dedicated Timer1 oscillator circuit enabled

0​
= Dedicated Timer1 oscillator circuit disabled
bit 2
T1SYNC: Timer1 Synchronization Control bit

1​
= Do not synchronize asynchronous clock input

0​
= Synchronize asynchronous clock input with system clock (FOSC)
bit 1
Unimplemented: Read as ‘0
bit 0
TMR1ON: Timer1 On bit

1​
= Enables Timer1

0 = Stops Timer1 and clears Timer1 gate flip-flop

Also I am puzzled by the fact that your clock goes to fast. It is not normal operation of a crystal oscillator to behave like this. Did you also include the CLKOUTEN_OFF in the first config word? Even if suggestions do not work. It is important that you report back. We need to know both working and non working ideas​
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
Ok, try to remember another time,
Here is the complete code, that do not run at right speed.
Rich (BB code):
#include <htc.h>
//#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
__CONFIG (CLKOUTEN_OFF & FCMEN_ON & IESO_OFF & BOREN_OFF & CP_OFF & MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);//INTOSC);//XT
__CONFIG (LVP_ON & LPBOR_OFF & BOREN_ON & STVREN_ON & WRT_OFF);
#define LED RC0
#define _XTAL_FREQ  4000000
//global defs
volatile unsigned char week_day;
volatile unsigned char hour, minut, sec;
volatile bit timer_tick;
volatile bit blink;
const char * const day_of_week_names[] = {"Monday feels so bad",
                                          "Tuesday feel better",
                                          "Wednesday don't go ",
                                          "Thursday goes slow ",
                                          "Friday on my mind  ",
                                          "Saturday Night Live",
                 "Sunday be sporty   " 
                                         };
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;
   TMR1L=0;
        //If we set TMR1 to start at 0x8000 (32768), the TMR1 will overflow every 1 second
     timer_tick=1;
  }//we are done here
}
void setup(void)
{ 
 IRCF0=1;
      IRCF1=0;
      IRCF2=1;
      IRCF3=1;//4MHz clock speed
//REGISTER 5-1: OSCCON: OSCILLATOR CONTROL REGISTER
// bit 6-3 IRCF<3:0>: Internal Oscillator Frequency Select bits
//1111 = 16MHz
//1110 = 8MHz
//1101 = 4MHz
//1100 = 2MHz
//1011 = 1MHz
//1010 = 500 kHz(1)
//1001 = 250 kHz(1)
//1000 = 125 kHz(1)
//0111 = 500 kHz (default upon Reset)
//0110 = 250 kHz
//0101 = 125 kHz
//0100 = 62.5 kHz
//001x = 31.25 kHz
//000x = 31kHz LF 
    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
 ANSELA=0;
       ANSELB=0;
 ANSELC=0;//turn off all analog functions
//timer1 settings
 TMR1H=0x80;
 TMR1L=0;
 //T1CON=0b00000110;//used during debug
  T1CON=0b10101100;
 //   T1CON=0b10001100; //*4 speed
 // See datasheet REGISTER 19-1: T1CON: TIMER 1 CONTROL REGISTER
 // TMR1CS<1:0> T1CKPS<1:0> T1OSCEN !T1SYNC — TMR1ON
//bit 7-6 TMR1CS<1:0>: Timer1 Clock Source Select bits
//11 =Timer1 clock source is Capacitive Sensing Oscillator (CAPOSC)
//10 =Timer1 clock source is pin or oscillator:
//If T1OSCEN = 0:
//External clock from T1CKI pin (on the rising edge)
// T1OSCEN = 0:
//Crystal oscillator on SOSCI/SOSCO pins
//01 =Timer1 clock source is system clock (FOSC)
//00 =Timer1 clock source is instruction clock (FOSC/4)
//bit 5-4 T1CKPS<1:0>: Timer1 Input Clock Prescale Select bits
//11 = 1:8 Prescale value
//10 = 1:4 Prescale value
//01 = 1:2 Prescale value
//00 = 1:1 Prescale value
//bit 3 T1OSCEN: LP Oscillator Enable Control bit
//1 = Dedicated Timer1 oscillator circuit enabled
//0 = Dedicated Timer1 oscillator circuit disabled
//bit 2 T1SYNC: Timer1 Synchronization Control bit
//1 = Do not synchronize asynchronous clock input
//0 = Synchronize asynchronous clock input with system clock (FOSC)
//bit 1 Unimplemented: Read as ‘0’
//bit 0 TMR1ON: Timer1 On bit
//1 = Enables Timer1
//0 = Stops Timer1 and clears Timer1 gate flip-flop
 TMR1GE=0;
//T1GCON: TIMER1 GATE CONTROL REGISTER
//bit 7 TMR1GE: Timer1 Gate Enable bit
//If TMR1ON = 0:
//This bit is ignored
//If TMR1ON = 1:
//1 = Timer1 counting is controlled by the Timer1 gate function
//0 = Timer1 counts regardless of Timer1 gate function
  //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;
     timer_tick=0;
     blink=0;
     //other misc settings
      timer_tick=0;//clear the tick
    }
void lcd_format_and_send(void)
{   char timestr[3];
//lcd_clear();
lcd_goto(0x00);
lcd_puts("Klokken : ");
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_puts(":");
utoa(timestr, sec, 10);
 if (sec<10)lcd_puts("0");
lcd_puts(timestr);
   //         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 main (void) 
 { setup();
      GIE=1;
   TMR1ON=1;
       while (1)
        {
           if (timer_tick)
           {
            sec++;
         if (sec==60)
   {
   sec=0;
        minut++;
  if (minut>=60)
{   minut=0;
   hour++;
   if (hour>=24)
  { hour=0;
 
  }
}         
   }        
            lcd_format_and_send();//send time to LCD
            timer_tick=0; 
            //timer_tick=1;//just for debug
           }// end if (timer_tick) Memo to my self put all this in sub function
         }//end while endless loop
 }//End main
 

nerdegutta

Joined Dec 15, 2009
2,689
Ok, try to remember another time,
Here is the complete code, that do not run at right speed.
Rich (BB code):
#include <htc.h>
//#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
__CONFIG (CLKOUTEN_OFF & FCMEN_ON & IESO_OFF & BOREN_OFF & CP_OFF & MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);//INTOSC);//XT
__CONFIG (LVP_ON & LPBOR_OFF & BOREN_ON & STVREN_ON & WRT_OFF);
#define LED RC0
#define _XTAL_FREQ  4000000
//global defs
volatile unsigned char week_day;
volatile unsigned char hour, minut, sec;
volatile bit timer_tick;
volatile bit blink;
const char * const day_of_week_names[] = {"Monday feels so bad",
                                          "Tuesday feel better",
                                          "Wednesday don't go ",
                                          "Thursday goes slow ",
                                          "Friday on my mind  ",
                                          "Saturday Night Live",
                 "Sunday be sporty   " 
                                         };
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;
   TMR1L=0;
        //If we set TMR1 to start at 0x8000 (32768), the TMR1 will overflow every 1 second
     timer_tick=1;
  }//we are done here
}
void setup(void)
{ 
 IRCF0=1;
      IRCF1=0;
      IRCF2=1;
      IRCF3=1;//4MHz clock speed
//REGISTER 5-1: OSCCON: OSCILLATOR CONTROL REGISTER
// bit 6-3 IRCF<3:0>: Internal Oscillator Frequency Select bits
//1111 = 16MHz
//1110 = 8MHz
//1101 = 4MHz
//1100 = 2MHz
//1011 = 1MHz
//1010 = 500 kHz(1)
//1001 = 250 kHz(1)
//1000 = 125 kHz(1)
//0111 = 500 kHz (default upon Reset)
//0110 = 250 kHz
//0101 = 125 kHz
//0100 = 62.5 kHz
//001x = 31.25 kHz
//000x = 31kHz LF 
    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
 ANSELA=0;
       ANSELB=0;
 ANSELC=0;//turn off all analog functions
//timer1 settings
 TMR1H=0x80;
 TMR1L=0;
 //T1CON=0b00000110;//used during debug
  T1CON=0b10101100;
 //   T1CON=0b10001100; //*4 speed
 // See datasheet REGISTER 19-1: T1CON: TIMER 1 CONTROL REGISTER
 // TMR1CS<1:0> T1CKPS<1:0> T1OSCEN !T1SYNC — TMR1ON
//bit 7-6 TMR1CS<1:0>: Timer1 Clock Source Select bits
//11 =Timer1 clock source is Capacitive Sensing Oscillator (CAPOSC)
//10 =Timer1 clock source is pin or oscillator:
//If T1OSCEN = 0:
//External clock from T1CKI pin (on the rising edge)
// T1OSCEN = 0:
//Crystal oscillator on SOSCI/SOSCO pins
//01 =Timer1 clock source is system clock (FOSC)
//00 =Timer1 clock source is instruction clock (FOSC/4)
//bit 5-4 T1CKPS<1:0>: Timer1 Input Clock Prescale Select bits
//11 = 1:8 Prescale value
//10 = 1:4 Prescale value
//01 = 1:2 Prescale value
//00 = 1:1 Prescale value
//bit 3 T1OSCEN: LP Oscillator Enable Control bit
//1 = Dedicated Timer1 oscillator circuit enabled
//0 = Dedicated Timer1 oscillator circuit disabled
//bit 2 T1SYNC: Timer1 Synchronization Control bit
//1 = Do not synchronize asynchronous clock input
//0 = Synchronize asynchronous clock input with system clock (FOSC)
//bit 1 Unimplemented: Read as ‘0’
//bit 0 TMR1ON: Timer1 On bit
//1 = Enables Timer1
//0 = Stops Timer1 and clears Timer1 gate flip-flop
 TMR1GE=0;
//T1GCON: TIMER1 GATE CONTROL REGISTER
//bit 7 TMR1GE: Timer1 Gate Enable bit
//If TMR1ON = 0:
//This bit is ignored
//If TMR1ON = 1:
//1 = Timer1 counting is controlled by the Timer1 gate function
//0 = Timer1 counts regardless of Timer1 gate function
  //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;
     timer_tick=0;
     blink=0;
     //other misc settings
      timer_tick=0;//clear the tick
    }
void lcd_format_and_send(void)
{   char timestr[3];
//lcd_clear();
lcd_goto(0x00);
lcd_puts("Klokken : ");
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_puts(":");
utoa(timestr, sec, 10);
 if (sec<10)lcd_puts("0");
lcd_puts(timestr);
   //         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 main (void) 
 { setup();
      GIE=1;
   TMR1ON=1;
       while (1)
        {
           if (timer_tick)
           {
            sec++;
         if (sec==60)
   {
   sec=0;
        minut++;
  if (minut>=60)
{   minut=0;
   hour++;
   if (hour>=24)
  { hour=0;
 
  }
}         
   }        
            lcd_format_and_send();//send time to LCD
            timer_tick=0; 
            //timer_tick=1;//just for debug
           }// end if (timer_tick) Memo to my self put all this in sub function
         }//end while endless loop
 }//End main
Not that I think it matters, but in your void setup() you have
Rich (BB code):
TRISA0=0;
is that correct?

... and you have timer_tick=0; two times with blink=0; between...
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
could be i will have to set them all.
both TRISA and ANSELA
Check it later.

The timer_tick , i dont think i will matter, it is just in the setup.

TRISA0=0; changed to TRISA=0; No change i speed.....
 
Last edited:

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
Have been checking again and again,
All is set correct,
I can get it to run to fast ( aprox 40 % )
I can get it to run to slow (aprox 40% of normal speed.)

Circiut is ok, cause when shifting the 16F1509 with the 16F690 it runs at normal speed.

Where can the bug be ?
 

spinnaker

Joined Oct 29, 2009
7,830
Did you use the calculator I have posted?

My suggestion is to take the code that you have but simplify it for testing. Create a new program Leave out all of the temperature and LCD code. Just have the timer code. Get that to work. You could add some simple code for the ISR to turn a light on and off maybe but that should be it,


Also instead of setting T1CON like this T1CON=0b10101100;
change your code so it sets the individual bits. It will make your code mode readable.

This is just an example from something I have done:

Rich (BB code):
T1CKPS1 = 0;    // bits 5-4  Prescaler Rate Select bits
T1CKPS0 = 0;
T1OSCEN = 1;    // bit 3 Timer1 Oscillator Enable Control: bit 1=on
T1SYNC  = 1;    // bit 2 Timer1 External Clock Input Synchronization Control bit: 1=Do not synchronize external clock input
    T1CONbits.TMR1CS  = 0;    // bit 1 Timer1 Clock Source Select bit: 0=Internal clock (FOSC/4) / 1 = External clock from pin T1CKI (on the rising edge)
        

 TMR1IP = 0;    // Timer1 is low priority interrupt  
 TMR1IE = 1;    // Enable interrupts for Timer 1
See how much easier it is to read? That alone won't solve the problem but it might help you see the problem.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
Ok,
Have changes it and have found a typo.
Rich (BB code):
TMR1CS1=1; //CLOCK SOURCE SELECTIONS
TMR1CS0=1;
//11 =Timer1 clock source is Capacitive Sensing Oscillator (CAPOSC)
//10 =Timer1 clock source is pin or oscillator:
//If T1OSCEN = 0:
//External clock from T1CKI pin (on the rising edge)
//If T1OSCEN = 1:
//Crystal oscillator on SOSCI/SOSCO pins
//01 =Timer1 clock source is system clock (FOSC)
//00 =Timer1 clock source is instruction clock (FOSC/4)
 
 // <1:0>: Timer1 Input Clock Prescale Select bits
T1CKPS1=0;
T1CKPS0=0;
//11 = 1:8 Prescale value
//10 = 1:4 Prescale value
//01 = 1:2 Prescale value
//00 = 1:1 Prescale value
T1OSCEN=1;//LP Oscillator Enable Control bit
//1 = Dedicated Timer1 oscillator circuit enabled
//0 = Dedicated Timer1 oscillator circuit disabled
nT1SYNC=1; //: Timer1 Synchronization Control bit
TMR1ON=0;//: Timer1 On bit
TMR1H = 0x80;     // preset for timer1 MSB register
TMR1L = 0x00;     // preset for timer1 LSB register
When changed from
//10 =Timer1 clock source is pin or oscillator:
to
//11 =Timer1 clock source is Capacitive Sensing Oscillator (CAPOSC)
i got it to work,

BUT, on 24 minutes , it have lost 10 sec. Why ?

Set the timer1 to 3ffd = 32765 , cause it takes tree instruction, to reset timer and start again.
But still it looses some seconds.
 
Last edited:
Top