Real time clock + external interrupt in one PIC possible?


Closed Thread
Results 1 to 34 of 34

Hybrid View

  1. #1
    Join Date
    Jan 2008
    Location
    Selm, Germany
    Posts
    116

    Default Real time clock + external interrupt in one PIC possible?

    Hello people!

    Its many years ago that i programmed something. Now i need to calculate the average speed of a car after pressing a button for start. I programmed a distance counter with a PIC16F88 and need now to measure the real time and divide distance by time. In my opinion that needs two interrupts.
    The distance is measures using "on interrupt" counting pulses from the driveshaft. Do I need two pics or one pic and an external clock?
    I could only use two types: 16F88 and 18F1320 becauce Iīm an inept programmer.
    regards in advance for any helpful suggestions.

  2. #2
    Join Date
    Jan 2008
    Location
    Selm, Germany
    Posts
    116


    Did you find this post helpful? Yes | No

    Default Re: Real time clock + external interrupt in one PIC possible?

    Woahh.... I read the datasheet and was striked by the perception that the 18F1320 has three (in words: 3) different interrupts to handle around.

    But.......how do I use them in PBP ? on interrupt 1? and on interrupt 2 ?

    please help

  3. #3
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: Real time clock + external interrupt in one PIC possible?

    Something like this:
    Code:
    "ON INTERRUPT GOTO Handler"
    
    DISABLE
    Handler:
    IF INT0IF = 1 THEN
      Counter = Counter + 1
      INT0IF = 0
    ELSEIF TMR1IF = 1 THEN
      Speed = Counter
      Counter = 0
      TMR1IF = 0
      TMR1H = (Preload value, HIGHBYTE)
      TMR1L = (Preload value, LOWBYTE)
    ENDIF
    RESUME
    ENABLE

  4. #4
    Join Date
    Jan 2008
    Location
    Selm, Germany
    Posts
    116


    Did you find this post helpful? Yes | No

    Default Re: Real time clock + external interrupt in one PIC possible?

    Thank you for the example,

    but could you explain to me what TMR1H and TMR1L are for?

    regards from an inept programmer...

  5. #5
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: Real time clock + external interrupt in one PIC possible?

    Timer1 can create an Interrupt when it "rolls over" from $FFFF to $0000. This creates TMR1IF = 1. Timer1 is a 16-bit counter/timer, which means it can reach 65,535d (Hex $FFFF) before going back to zero and tripping an Interrupt Flag. Since it is a 16-bit counter, the value in the counter needs 2 bytes, which are TMR1H(IGHBYTE) and TMR1L(OWBYTE). If you are running your OSC at 4 MHz, then your Fosc/4 = (4,000,000 / 4) 1,000,000 Instruction Cycles per Second. This equates to (1 / 1,000,000) 0.000001 second (1 us) time frame per Instruction Cycle. Options for Prescaler in the Timer1 SFR goes as high as 1:8. At max 1:8 you can get 8 us (0.000008 second) clicks on your Timer1. Since Timer1 maxes out at 65,535, you can run Timer1 up to 0.52428 seconds. This isn't a very "friendly" time frame to work with. However, it is slightly over a half second. If we calculate 0.5 seconds divided by our 0.000 008 second TMR1 clicks, we get 62,500 TMR1 increments yield 1/2 second (0.5 seconds).

    Back to computerese, a byte = max 255 while a word = max 65535. The architecture of the 8-bit Microchip MCUs means each address holds 8 bits, or 1 byte. In order to assign 16 bits to the Timer1 counter, it takes 2 addresses, or 2 bytes. Microchip has decided to name the 2 bytes required by Timer1 "TMR1H" for the upper byte value and "TMR1L" for the lower byte value. Sometimes it's easier to deal with our familiar decimal system. PBP lets us do that. Sometimes it's easier to deal with binary, as in setting Special Function Registers. PBP lets us do that as well. Sometimes you are forced to work with hexadecimal. Well, I find working with Hex easier with a calculator that came standard on both my MAC OS and Windows 10. It has Programmer function that easily converts any format (decimal, octal, hexadecimal, or binary) to any format (ditto).

    Back to your original question, since Timer1 trips a flag when it overflows, if you want a 1 second increment, it can't happen. [You could work with some of the slower factory oscillator speeds available in ALL PIC MCUs and do the conversion math for PBP.] Best you can get at PBP's slowest OSC speed is 0.52428 seconds. If you could work with 0.5 seconds, then all we have to do is start Timer1 part way through it's counting sequence so when it flips, we got a 1/2 second.

    Review, 4 MHz OSC + 1:8 Prescaler X 65,535 gives us 0.52428. If we want 0.50000 then all we have to do is take 0.5 and divide it by 0.000 008 and we get 62500. We don't want to START there, we want to END there. So we must subtract 62,500 from 65,535 and get 3035. That's bigger than 255, which means it is bigger than a byte. In order to contain such a large number, it takes 2 bytes (which max out at 65,535). I like working with decimal since that's what I grew up using. However, PIC requires that this decimal number be separated into it's upper byte and lower byte. You could convert it to binary and get %0000 1011 1101 1011, or convert it to hex and get $0B DB. If you wanted to load TMR1 with binary you would:

    TMR1H = %00001011
    TMR1L = %11011011

    Hex is shorter and accomplishes the same thing:

    TMR1H = $0B
    TMR1L = $DB

    Of course you could do it with decimal and:

    TMR1H = 11
    TMR1L = 219

    Bottom line, Timer1 will start counting at 3035 through 65,535 and trip a flag at the 1/2 second mark. With that flag (TMR1IF) you can direct your code to an Interrupt Handler to do whatever you require. Though lengthy, I hope this helps.

  6. #6
    Join Date
    Jan 2008
    Location
    Selm, Germany
    Posts
    116


    Did you find this post helpful? Yes | No

    Default Re: Real time clock + external interrupt in one PIC possible?

    Thank you mpgMike,

    so you convinced me to use an external real time clock. That needs an interrupt because the real time clock isnīt in sync with the pic s own clock.

    I need to get 0.1 sec timing increments because 1 sec isnīt fine enough for starting time, max time is about 600 sec so I need a long variable to calculate with.

    Hope i can work things out how to get the pic to use the real time clock but I guess there are lots of examples out there.

    For explanation: we (my wife and I) drive so called Oldtimer-rallyes in a 1983 Mustang convertible and were on an event last weekend where we had to drive at an average speed of 28 km/h as constant as possible AND the starting point of the road was equipped with a clock and a light gate for the starting point. The end point of the distance was kept hidden so no one knew how long the distance was and so we couldnīt calculate anything. Only possible way was to drive as constant as possible with the given speed of 28 km/h. And to make things worse, there are heroes out there driving the distance
    in 0,1 sec of the calculatet time . There were two of these hidden drives , the first we were 13 seconds off (which threw us out of the reach of a cup), the second we made it in 0,5 seconds of the sheduled time.
    My wife wasnīt happy about driving home without a cup (first event this year we drove without cup so shes used to getting something and shes the driver) so my task is it to get something done to increase our chances next time.

    Oh yeah the other guys use GPS and mobile phone apps and such.
    I hope you appreciate that the old guy (54 years) tries to solve the problem with PIC Hardware/software.......haha


    regards

    Miguel David

Similar Threads

  1. real time clock
    By maria in forum Code Examples
    Replies: 44
    Last Post: - 1st March 2022, 13:13
  2. Real time clock + external interrupt in one PIC possible?
    By Mugelpower in forum mel PIC BASIC
    Replies: 1
    Last Post: - 10th September 2017, 18:11
  3. Real Time Clock
    By in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd June 2012, 05:52
  4. Real time clock
    By PlantBob in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 10th February 2011, 14:01
  5. Real Time Clock
    By savnik in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th December 2006, 03:02

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts