1. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Hi
    Have this problem.
    PIC 16F690 4 MHz FOSC
    have to read timer1(16bit) for 1 sec.
    update my LCD and some more.
    clear timer1,
    read timer1 for 1 sec Again,
    update my LCD and some more.
    ect.
    have to do this repeatly over and over.
    until either an input(buttons) or the input from timer1 is way off ( alarm sounds )
    Any hints on getting the interrups going ?
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,383
    495
    Can you use external circuit that is connected to external interrupt pin on the PIC? This way you get external signal that triggers interrupt, the interrupt routine does all the things you need done, then PIC goes back into "holding pattern" like running empty super loop. Then 1 second later another signal arrives, interrupt routing is activated, things that you need done are done, and PIC goes back to waiting.

    Another thing you could do is use external clock ic like Real Time Clock ic, DS1307 for example. DS1307 is not the best, there are better ic now that do the same thing. The PIC would poll the RTC, when it sees that the second changed, it would do your stuff, then go back to checking the time to see if the second changed.
     
  3. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Yes external could be the solution, but it also have some downsides.
    When i "Count" for 1 sec, then i will do all the updates of LCD, move to another input, get ready for NeXT "second" to arrive
    Lots of waiting time, assuming the external input is 1 Hz.
    If i could use internal timers ( second ticks) i should be able to do this
    read "Counts" for 1 sec, update LCD move to another input, and now select start om NeXT second, with internal timer on,.
    think it's difficult to explain,
    could i use timer0 to divide by 100, and the set 100hz as input , that way i could reset the timer to "100" when ready,

    Could connect a Xtal to RA4-5, but that will take my spare pins, but think i will go with that solution.
    connect a Xtal at 32.768 kHz and use an interrupt, but Again i will have some waiting time.
    difficult to deside.
     
  4. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,383
    495
    I guess I don't understand the function of the 1 second. Do you want to "do things" at the start of each second? Or. Do you want to have 1 second intervals between "doing things"?

    If you simply need 1 second interval between Action A and Action B, then use internal timers. To get 1 second from internal timers you just need to figure out how many times the timer will overflow. Example, lets say that the timer has maximum of 250 milliseconds, so the timer reaches 250 ms, overflow 1, you set it to start again, it reaches overflow 2, you set it again, it reaches overflow 3, you set it again, it reaches overflow 4, you now have four 250 ms intervals, 4*250 ms=1000 ms=1 second. That is how you create large time intervals using small timers.
     
  5. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    1. start a periode of 1 sec, stop by an interrupt,
    2 do stuff with the Counts from ex "timer0" ( external signal from RPM )
    3. read all my buttons input
    3.1 if buttons is pressed, do that stuff.
    4 select NeXT shaft to Counts RPM from.
    5 starts a new periode of 1 sec.
    ect.

    Yes i would have used timers if i could figure out how, cause if i use use that need 15 times to get to 1 sec, it would interrupt 15 times, ?
    if i run the internal clockspeed at 4 MHz it can only go Down to 66,667 msec. pr overflow, or i dont understand it.
     
  6. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,383
    495
    You would have the overflow flag turn on 15 times. If I remember the code right, you stop the timer, clear overflow flag, load zero into the timer, start timer. You can do that using for loop because you know exactly how many overflows you need for an interval of 1 second. The whole process should take maybe a few milliseconds, maybe even less than that.
     
  7. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    have read a Little on timer2 in 16f690
    as i get it, correct if wrong
    FOSC is 4 MHz
    input to timer2 is FOSC/4 = 1 MHz
    Prescaler set to 1:16 gives a 1000000/ 62500 Hz.
    Timer2 periode register ( PR2) set to 250.
    gives a timer2 rollover at 62500/250 = 250 Hz
    this goes to a POSTSCALER there will be set to 1:10
    output and interrupt = 25 hz ????
    then i need to Count to 25 before i finish the counting - the times it takes to check the timer. ex

    if(TMR2IF)
    { puls++;
    if (puls==25)
    { // stop timer2 and my RPM input
    }

    Somethin like that, do i need to take care of these 25 loops time, or is that just nothing ?
     
  8. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Study of datasheet.
    Taken from the datasheet

    6.4 Timer1 Oscillator
    A low-power 32.768 kHz crystal oscillator is built-in between pins OSC1 (input) and OSC2 (amplifier output). The oscillator is enabled by setting the T1OSCEN control bit of the T1CON register. The oscillator will continue to run during Sleep. The Timer1 oscillator is shared with the system LP oscillator. Thus, Timer1 can use this mode only when the primary system clock is derived from the internal oscillator or when the oscillator is in the LP mode. The user must provide a software time delay to ensure proper oscillator start-up.

    Will this mean that i can use this to 1 sec interrupt ? Without connecting Any thing to the pins ?
    Just preset timer1 to 32768 and it will rollover and create an interrupt.
     
  9. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,675
    2,722
    No. You need to connect a 32khz crystal (and caps to ground) between the pins. The oscillator circuit is provided. Not the crystal.
     
  10. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Thanx, ok then i will use the internal FOSC/4( set to 2 MHz) prescale 8. and set timer to 62500 and rollover every second?
    T=(4 /Fosc)∗Presc∗(Resolution−Preload)
    1 sec = 4 / 2.000.000*8*(65536-3036)
    Am i right ?
     
  11. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Anyone ??
    Have tried all day but cant get it to work. So is it possible to do ??
     
  12. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,509
    2,369
    Here is a sample file from Dave Benson book.
    Code (Text):
    1.  
    2. ;=======SECONDS.ASM=================================9/30/02==
    3.         list    p=16f870
    4.     __config  h'3f71'
    5.         radix   hex
    6. ;------------------------------------------------------------
    7. ;seconds demo
    8. ;    external oscillator, 32768 Hz, timer 1, prescaler 1:1
    9. ;------------------------------------------------------------
    10. ;       cpu equates (memory map)
    11.  
    12. ;------------------------------------------------------------
    13. ;------------------------------------------------------------
    14.         org     0x000
    15.     goto    start    ;skip over location pointed to by
    16.             ;   interrupt vector
    17.     org    0x004
    18.     goto    iserv
    19. ;
    20. start:
    21.         bsf        STATUS,    RP0  ;switch to bank 1
    22.         movlw   b'00000000' ;port B outputs
    23.         movwf    TRISB
    24.         movlw    b'00000110' ;turn off A/D, port A
    25.         movwf    ADCON1
    26.         bcf        STATUS,    RP0  ;switch back to bank 0
    27.         movlw    b'00000000' ;port B lines low
    28.         movwf    PORTB
    29.         bcf        INTCON,    7    ;disable global interrupts
    30.         bcf        INTCON,    6    ;disable peripheral interrupts
    31.         bsf        STATUS,    RP0  ;bank 1
    32.         bcf        PIE1,    0    ;disable tmr1 interrupts
    33.         bcf        STATUS,    RP0  ;bank 0
    34.         bcf        PIR1,    0       ;clear timer 1 interrupt flag
    35.         movlw    b'00001010' ;prescaler and tmr1 setup,
    36.         movwf    T1CON       ;   tmr1 off
    37.         clrf    TMR1H    ;clear timer 1 high
    38.         clrf    tmr1l    ;clear timer 1 low, clear prescaler
    39.         bsf        INTCON,    7    ;enable global interrupts
    40.         bsf        INTCON,    6    ;enable peripheral interrupts
    41.         bsf        STATUS,    RP0  ;bank 1
    42.         bsf        PIE1,    0    ;enable tmr1 interrupts
    43.         bcf        STATUS,    RP0  ;bank 0
    44.         bsf        T1CON,    0    ;timer 1 on
    45. circle:
    46.         goto    circle
    47. ;------------------------------------------------------------
    48. iserv:
    49.         bcf        PIR1,    0    ;clear timer 1 interrupt flag
    50.         bsf        TMR1H,    7    ;set up timer 1 to roll over at
    51.             ;   32,768 counts
    52.         btfss    PORTB,    0    ;port B, bit 0 STATUS?
    53.         goto    setbit    ;bit is clear
    54. clrbit:
    55.         bcf        PORTB,    0    ;clear port B, bit 0
    56.         retfie        ;return from interrupt
    57. setbit:
    58.         bsf        PORTB,    0    ;set port B, bit 0
    59.         retfie        ;return from interrupt
    60. ;------------------------------------------------------------
    61.         end
    62. ;------------------------------------------------------------
    63. ;at device programming time, select:
    64. ;       memory unprotected
    65. ;       watchdog timer disabled (default is enabled)
    66. ;       standard crystal XT (using 4 MHz osc for test)
    67. ;       power-up timer on
    68. ;    brown-out reset enabled
    69. ;    lvp disabled
    70. ;    debug mode disabled
    71. ;============================================================
    72.  
    73.  
    Or
    Code (Text):
    1.  
    2. ;=======TIME.ASM====================================9/30/02==
    3.         list    p=16f870
    4.     __config  h'3f71'
    5.         radix   hex
    6. ;------------------------------------------------------------
    7. ;seconds, minutes, hours demo
    8. ;    external oscillator, 32768 Hz, timer 1, prescaler 1:1
    9. ;------------------------------------------------------------
    10. ;       cpu equates (memory map)
    11. status    equ    0x03
    12. porta    equ    0x05
    13. portb   equ     0x06
    14. intcon    equ    0x0b
    15. pir1    equ    0x0c
    16. tmr1l    equ    0x0e
    17. tmr1h    equ    0x0f
    18. t1con    equ    0x10
    19. trisa    equ    0x85
    20. trisb    equ    0x86
    21. pie1    equ    0x8c
    22. adcon1    equ    0x9f
    23. sec    equ    0x20
    24. min    equ    0x21
    25. hr    equ    0x22
    26. ;------------------------------------------------------------
    27. ;    bit equates
    28. z    equ    2
    29. rp0    equ    5
    30. ;------------------------------------------------------------
    31.         org     0x000
    32.     goto    start    ;skip over location pointed to by
    33.             ;   interrupt vector
    34.     org    0x004
    35.     goto    iserv
    36. ;
    37. start    bsf    status,rp0  ;switch to bank 1
    38.     movlw   b'00000000' ;port B outputs
    39.         movwf    trisb
    40.     movlw    b'00000110' ;turn off A/D, port A
    41.     movwf    adcon1
    42.     movlw    b'00011111' ;port A inputs
    43.     movwf    trisa
    44.     bcf    status,rp0  ;switch back to bank 0
    45.     movlw    b'00000000' ;port B lines low
    46.     movwf    portb
    47.     bcf    intcon,7    ;disable global interrupts
    48.     bcf    intcon,6    ;disable peripheral interrupts
    49.     bsf    status,rp0  ;bank 1
    50.     bcf    pie1,0    ;disable tmr1 interrupts
    51.     bcf    status,rp0  ;bank 0
    52.     bcf    pir1,0       ;clear timer 1 interrupt flag
    53.     movlw    b'00001010' ;prescaler and tmr1 setup,
    54.     movwf    t1con       ;   tmr1 off
    55.     clrf    tmr1h    ;clear timer 1 high
    56.     clrf    tmr1l    ;clear timer 1 low, clear prescaler
    57.     clrf    sec
    58.     clrf    min
    59.     clrf    hr
    60.     bsf    intcon,7    ;enable global interrupts
    61.     bsf    intcon,6    ;enable peripheral interrupts
    62.     bsf    status,rp0  ;bank 1
    63.     bsf    pie1,0    ;enable tmr1 interrupts
    64.     bcf    status,rp0  ;bank 0
    65.     bsf    t1con,0    ;timer 1 on
    66. display    movf    sec,w    ;get seconds
    67.     movwf    portb    ;display seconds
    68. minutes    btfss    porta,0    ;display minutes?
    69.     goto    hours    ;no
    70.     movf    min,w    ;get minutes
    71.     movwf    portb    ;display minutes
    72.     goto    minutes
    73. hours    btfss    porta,2    ;display hours?
    74.     goto    display    ;no
    75.     movf    hr,w    ;get hours
    76.     movwf    portb    ;display hours
    77.     goto    hours
    78. ;------------------------------------------------------------
    79. iserv    bcf    pir1,0    ;clear timer 1 interrupt flag
    80.     bsf    tmr1h,7    ;set up timer 1 to roll over at
    81.             ;   32,768 counts
    82.     incf    sec,f
    83.     movf    sec,w    ;compare
    84.     sublw    d'60'    ;seconds = 60?
    85.     btfss    status,z    ;test z flag, skip next instruction
    86.                ;   if flag is set
    87.     retfie
    88.     clrf    sec    ;clear seconds
    89.     incf    min,f    ;increment minutes
    90.     movf    min,w    ;compare
    91.     sublw    d'60'    ;minutes = 60?
    92.     btfss    status,z    ;test z flag, skip next instruction
    93.                ;   if flag is set
    94.     retfie
    95.     clrf    min    ;clear minutes
    96.     incf    hr,f    ;increment hours
    97.     movf    hr,w    ;compare
    98.     sublw    d'24'    ;hours = 24?
    99.     btfsc    status,z    ;test z flag, skip next instruction
    100.                ;   if flag is clear
    101.     clrf    hr    ;clear hours
    102.     retfie
    103. ;------------------------------------------------------------
    104.         end
    105. ;------------------------------------------------------------
    106. ;at device programming time, select:
    107. ;       memory unprotected
    108. ;       watchdog timer disabled (default is enabled)
    109. ;       standard crystal XT (using 4 MHz osc for test)
    110. ;       power-up timer on
    111. ;    brown-out reset enabled
    112. ;    lvp disabled
    113. ;    debug mode disabled
    114. ;============================================================
    115.  
    116.  
    Also http://www.piclist.com/techref/member/jwn-hotmail-f41/TIMER_TUTORIAL.htm

    Max.
     
  13. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    The problem is that i will NOT use an external xtal, cause i do not have I/O enough.
    And i ASM code it is to difficult, rather C code. :)
     
  14. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Could this thereoe work
    Set 4 mhz system. Div by 4 is 1mhz
    Then prescale by 8. Gives a 125 khz input to timer1.
    Then set an input interrupt on change. Then start timer1 . Wait until next interrupt. Stop timer1 read the value. Take the value and calculate a RPM depend on how long time it takes from first to next interrupt.
    Or is that to unprecise to read rpm on. ??
    Ex timer1 counts 60000 this is then
    0.48 sec. Eq. 60/0.48 125 rpm asuming 1 tick each rpm ???
     
  15. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,675
    2,722
    Are you not aware of the ECCP module?
     
  16. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    And that is ???
     
  17. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,675
    2,722
    Look it up in the datasheet. Pay particular attention to the "Capture Mode".
     
Loading...