April Fools' Pic Project

Discussion in 'Embedded Systems and Microcontrollers' started by Art, Apr 1, 2015.

  1. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    Hi Guys :)
    Here's a pic program that can tell what osc frequency is clocking it, and adjust accordingly to make an LED flash at one pulse per second.
    Cheers, Art.

    Last edited: Apr 1, 2015
  2. R!f@@

    AAC Fanatic!

    Apr 2, 2009
    It's not a program .
  3. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    Actually, many PIC's have two, independent oscillators today. One is fixed and the other is variable. Using the fixed (lower frequency one) oscillator to determine another frequency, such as the other internal oscillator shouldn't be that difficult. Of course, I realize this was not a serious post.

  4. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    You realised wrong ;)
    It’s a 16F628A configured for HS (not just running from internal RC osc).
    I could have, but did not, initially made it run the LED at a freq representative of the clock freq
    prior to flashing the LED at 1 Hz.
    Also, I accidentally uploaded in SD... fixing that now.

    It’s real and everything about the circuit is is as I say it is.
    The 1 Hz pulse is driven by timer1 overflow interrupt.

  5. R!f@@

    AAC Fanatic!

    Apr 2, 2009
    Like I said it not program...rather a video file
  6. Alberto

    Active Member

    Nov 7, 2008
    Art, let me give a try. You generate a PWM out to a fixed frequency for a given cristal and you monitor it using the internal timer. If the PWM frequency is higher or lower than the expected one then the program adjusts the time interval to keep the flashing rate of 1 hertz costant.
    Am I close?

    Last edited: Apr 2, 2015
  7. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    I’m not sure if you’re saying it’s fake, or the program is simple?
    Yes it’s a program only functional to demonstrate what’s happening with no other purpose.

    @ Alberto:
    I don’t know how you’d look at the PWM channel without another wire connected to the pic,
    or if you can even generate hardware PWM with the internal clock when an external clock is selected for clocking the pic.
    I’m not saying it’s not possible, I haven’t checked datasheets in that regard.

    It was figured out on another forum, so in English;

    Program starts.

    Check status bit 4 to see if the pic is starting after watchdog timer reset or cold powerup.

    If it’s a cold start, enter a loop that does not reset the watchdog, but writes a zero value to on-chip EEPROM every 100ms
    (calculated at 20MHz) and increments a variable addressing the on-chip EEPROM location.
    The watchdog timer will reset the pic well before 30 locations are written so long as the pic is running 25MHz or under.

    If it was started after a watchdog timer reset, count the number of zero values on on-chip EEPROM,
    and populate the on-chip EEPROM with all 0xFF values again for next time.
    From now on, reset the watchdog timer so it will not reset again.
    Compare the count of zero values with a lookup table to find the crystal frequency.

    Set the timer1 preload values accordingly to time 100Hz interrupt so seconds can be counted.
    Cheers, Art.
  8. R!f@@

    AAC Fanatic!

    Apr 2, 2009
    I am saying I do not see a program but just a video file.
    Have you attached a prog ?
  9. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    Well obviously the idea was to figure it out.
    There is more than one way. This is PBP with some inline assembler
    (just the part to get a count from which the clock freq can be determined)
    Code (Text):
    2. '
    3. DEFINE OSC 20 ‘needed so the 100ms pause period is known
    4. '
    5. if status.bit4 = 1 then 'was not reset by watchdog
    6. for percount = 0 to 255 'do eeprom writes until wdt reset
    7. pause 100 '
    8. @ MOVF _percount ,W ;copy counter value to timer low byte
    9. @ MOVWF TMR1L ;load timer with count value
    10. next percount '
    11. makereset: 'failsafe to make reset occur
    12. goto makereset '
    13. '
    14. else 'hardware was reset by watchdog
    15. @ clrwdt ;
    16. @ MOVF TMR1L ,W ;copy counter value to timer low byte
    17. @ MOVWF _wrtcount ;reload timer with correct value
    18. endif '
    19. '