1. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Hi
    Have this simpel setup. , see attached schematic.
    The problem is that it will only Work, when i put my finger in near of the xtal ( not touch )
    When it then runs, it runs to slow.
    ex 60 times at 1 sec each gives 1 minut, but it takes 77 seconds to do it in real seconds.

    here is the code
    i use a 16F1509 microchip.


    Code (Text):
    1. #include <htc.h>
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include "lcd.h"
    5. #include <string.h>
    6.  
    7. #pragma config FOSC = LP  // Oscillator Selection Bits (XT Oscillator, Crystal/resonator connected between OSC1 and OSC2 pins)
    8. #pragma config WDTE = OFF  // Watchdog Timer Enable (WDT disabled)
    9. #pragma config PWRTE = ON  // Power-up Timer Enable (PWRT disabled)
    10. #pragma config MCLRE = OFF  // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
    11. #pragma config CP = OFF  // Flash Program Memory Code Protection (Program memory code protection is disabled)
    12. #pragma config BOREN = OFF  // Brown-out Reset Enable (Brown-out Reset enabled)
    13. #pragma config CLKOUTEN = OFF  // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
    14. #pragma config IESO = OFF  // Internal/External Switchover Mode (Internal/External Switchover Mode is enabled)
    15. #pragma config FCMEN = ON  // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
    16.  
    17. // CONFIG2
    18. #pragma config WRT = OFF  // Flash Memory Self-Write Protection (Write protection off)
    19. #pragma config STVREN = ON  // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
    20. #pragma config BORV = LO  // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
    21. #pragma config LPBOR = OFF  // Low-Power Brown Out Reset (Low-Power BOR is disabled)
    22. #pragma config LVP = ON  // Low-Voltage Programming Enable (Low-voltage programming enabled)
    23.  
    24. #define _XTAL_FREQ 16000000
    25. #define FOSC 16000000L
    26.  
    27. #define set_hour RB4 //Buttons
    28. #define set_minut RC2
    29. #define set_week RC1
    30. #define set_time RC3
    31.  
    32. bit sec;
    33. int secund;
    34. char test[10];
    35.  
    36.  
    37. static void interrupt isr(void)  // Here is interrupt function - the name is unimportant.
    38. {
    39.  if(TMR1IF)
    40.  TMR1L=0x00;
    41.  TMR1H=0x80;
    42.  TMR1IF=0;
    43.  sec=1;
    44.  RA2=!RA2;
    45. }
    46.  
    47. void setup (void)
    48. {
    49.  
    50. IRCF0=1; //16 mhz
    51. IRCF1=1;
    52. IRCF2=1;
    53. IRCF3=1;
    54. SCS0=0; // osc til 1 sec
    55. SCS1=0;
    56.  TRISA=0; //set all port out
    57.  TRISA2=0; // set port 2 ind
    58.  TRISA4=1; // set port 4 ind.
    59.  TRISA5=1; // set port 5 ind.
    60.  TRISB4=1; // port B 4 in
    61.  TRISB5=1; // port b 5 in
    62.  TRISB6=0; // b6 ud
    63.  TRISB7=1; // port b7 in
    64. TRISC0=0;
    65. TRISC1=1;
    66. TRISC2=1;
    67. TRISC3=1;
    68. TRISC4=0;
    69. TRISC5=0;
    70. TRISC6=0;
    71. TRISC7=0;
    72.  
    73. TMR1CS0=0;
    74. TMR1CS1=1;
    75. T1OSCEN=1;
    76.  
    77.  GIE = 0;  // Global interrupt disable just in case
    78.  //ANSELA = 0b00000001;  // Set PORT AN0 to analog input AN1 to AN7 digital I/O
    79.  ANSELA=0; // set alle digital.
    80.  ANSELB=0;
    81.  ANSELC=0;//turn off all analog functions
    82.  lcd_init();
    83.  T1CKPS1=0;
    84.  T1CKPS0=0;
    85. }
    86.  
    87. void main (void)
    88. {
    89. setup();  //do the setup
    90.   lcd_clear();
    91.   lcd_goto(0x00);
    92.   lcd_puts("1234567890abcdefghij");
    93.   lcd_goto(0x40);
    94.   lcd_puts("abcdefghij1234567890");
    95.  
    96.   __delay_ms(1000);
    97.   lcd_clear();
    98.   TMR1L=0x00;
    99.   TMR1H=0x80;
    100.   TMR1ON=1; // turn on timer1
    101.   sec=0;
    102.   PEIE=1;
    103.   TMR1IE=1;
    104.   TMR1GE=1;
    105.   GIE=1;
    106. lcd_goto(0x00);
    107.  
    108.   while (1)
    109.   {
    110.   if (sec==1)
    111.   {
    112.   secund++;
    113.   //if (secund==60) secund =0
    114.   itoa(test, secund, 10);
    115.   lcd_goto(0x01);
    116.  
    117.   lcd_puts(test);//*/
    118.   sec=0;
    119.   }
    120.  
    121.  
    122.  
    123.   }
    124. }
    125.  
    126.  
    127.  
     
    JSCV likes this.
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    Why R1?
     
    JSCV likes this.
  3. JSCV

    New Member

    Oct 3, 2015
    13
    1
    Hello FroceMaster,

    Your schematics show as nerdegutta says R1, This can be removed unless you have some special reason to put it there.
    For the capacitors, you need to check the datasheet of the oscillator and make sure you use the correct value.
    Also make sure the oscillator circuit is as close to the micro controller as possible with a short route to ground.

    For your code i see :
    #define _XTAL_FREQ 1600000
    #define FOSC 16000000L
    You define your oscillator is running at 16mhz however in your schematic you use an oscillator of 32.768kHz ? (No units mentioned)
     
  4. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Re R1, i heard from other that i should use the resistor.

    The _XTAL_FREQ should be the xtal of the internal clock, as far as i know.
    These 2 lines have always been in my project, and have worked so far.
     
  5. JSCV

    New Member

    Oct 3, 2015
    13
    1
    Hello FroceMaster,

    I see i made a mistake interpreting your schematic and code, you are using the internal oscillator.
    So please forget the last part of the post, You can refer to page 53 of the datasheet for more details on using crystal oscillator.
    I think the most important thing is to get the oscillator running properly before troubleshooting any further.

    Schematics in data sheet :
    upload_2015-10-4_20-27-24.png
     
  6. bertus

    Administrator

    Apr 5, 2008
    15,648
    2,348
    Hello,

    Sometimes resistors are used on xtal oscillators.
    The one in series with the gate has a much lower value as in your schematic:

    xtal_oscillator_using_gates.png

    Bertus
     
  7. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    the xtal extrern is for use to a 1 sec . to use in Watch to tell the time,
    could i do a simple circiut that came with a 1 minut impuls i would be better.
     
  8. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    Code (C):
    1. #pragma config FOSC = LP  // Oscillator Selection Bits (XT Oscillator, Crystal/resonator connected between OSC1 and OSC2 pins
    This code tells the MCU you are using a Low Power crystal or resonator on OSC1 and OSC2.


    Code (C):
    1. #define _XTAL_FREQ 16000000
    This code line tells the compiler to use 16MHz. Used for __delay_ms and such.

    Am I right or wrong?
     
  9. JSCV

    New Member

    Oct 3, 2015
    13
    1
    Depending on the precision you require you could choose to run the counter off the internal 16mHz(1%) clock using a internal prescaler, this would eliminate any off the external circuitry used for driving the timer/counter.
     
  10. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    sure i can, but but, if i get ex 2 MHz FOSC=500 KHz, that will give at 16 bit = 65535, and prescale 1:8 is 524280
    by selecting value "F424" to timer1 the overflow should happens at 62500 * 8 = 500.000 and that should be 1 second.

    Have now written this, but now it Count very fast but unstable, with no x-tal
     
    JSCV likes this.
  11. JSCV

    New Member

    Oct 3, 2015
    13
    1
    Please

    I cant see the code you have written but you should use timer 2, since your internal clock is 16MHZ, Timer 1 will give you an interrupt much quicker than you expected.

    If you use timer 2 with max prescaling(64) it will be : 16 MHz / 4 / 64 = F424
     
    Last edited: Oct 4, 2015
  12. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    But timer 2 is only 8 bit.
     
  13. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    4000000 / 64 = 62500
    62500 / 256(8 bit ) = 244.14
    Postscaler 244.14 / 16 = 15.25 times pr second.
    Cant use that.
     
  14. dannyf

    Well-Known Member

    Sep 13, 2015
    1,825
    364
    You will need to check the datasheet to see what kind of connections it needs to run the LP oscillator. Those oscillators tend to have low gain so putting a serial resistor there further reduces the gain -> could be why yours isn't oscillating.

    Your code is simply using the timer to advance time, in a quite imprecise way -> if you meant it to be an RTC, it is implemented incorrectly.

    You can replicate the same by running the chip on other timers. For example, 16Mhz oscillator -> 4Mhz Fosc, at 1:64 prescaler, 31250 count -> 0.5 second per roll-over.

    Pretty nice for an RTC + blink generator.

    If you have a 8-bit counter, you can then use a 250 count period, and every 125 isr = 0.5 second.

    Same concept, different math.
     
  15. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    Microchip's AN849 has all you need to know about a PIC oscillator and how and when to choose the R/C components.

    As the others have said, getting the period right is mostly a matter of counting clocks once the osc is running.
    @nerdegutta 's point is valid. 16MHz is unlikely to run well with a LP oscillator config setting..
     
  16. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Correct me if iam wrong.
    If i set 2 MHz, then FOSC = 500 kHz.
    Prescale 1:64
    TMR2 rollover at 245
    Postscale 1:16
    Then i give a 1 sec periode ?
     
  17. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,395
    497
  18. dannyf

    Well-Known Member

    Sep 13, 2015
    1,825
    364
    A crystal's pullability is very low so if you don't need super-precise frequency performance, no need for C1/C2.
     
  19. dannyf

    Well-Known Member

    Sep 13, 2015
    1,825
    364
    It also explains why crystals with 6pf load capacitance is so popular now-a-days.
     
  20. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    I will try to Work with the internal, and set speed at 2mhz,
    The questions is now, do it produce a around 1 Hz overflow at TMR2 ??
    or am i getting it wrong ?
     
Loading...