0
\$\begingroup\$

I'm trying to make a simple delay turn-on function using Timer0 inside the PIC16F57 itself.All the example I can find on internet mostly come with build-in interrupt function which the PIC16F57 doesn't have,at least on the datasheet.Any way or hint to workaround?

Objective is quite simple:

  1. a switch was pressed(input stay HIGH)
  2. LED will turn ON after 3s
  3. LED will remain ON if switch is still being pressed/latched
  4. if switch input is LOW logic level,then LED will off

Did try with normal __delay_ms(3000) but it's not practical since switch off the LED also need to wait for 3s if the switch on routine is not completed.

I'm using MPLAB XIDE in C language and no way to change the mcu to other model as the PCB is already fixed for PIC16F57.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Maybe have a look for newer, cheaper and pin compatible controller. \$\endgroup\$
    – Mike
    Nov 12, 2019 at 6:57
  • \$\begingroup\$ It's been 30 years for me. Then, it was the PIC16C57 (OTP, as no flash, yet.) I never used C on that device. No room for that (at the time, anyway.) Just 2k program memory. You will want to debounce the switch (because otherwise humans will notice problems) prior to software condition testing. You haven't specified how long the LED should be on if the switch is released prior to the 3 s delay. Is it just pulsed? Or never on? or? \$\endgroup\$
    – jonk
    Nov 12, 2019 at 18:30
  • \$\begingroup\$ Also, are you aware that you do have interrupts on the device. They take the form of a WDT reset. But those resets can be disambiguated from a hard power-on reset. And you can adjust the prescaler as well as choosing to replace the RC timer with Timer0 to drive it, if you want. So it's not perfect. But it's a serviceable approach for regular interrupt events. \$\endgroup\$
    – jonk
    Nov 12, 2019 at 18:39

1 Answer 1

3
\$\begingroup\$

The PIC16F57 has no interrupts so everything must be done by polling. The trick with polling is to do it in a way that doesn't block other operations.

Instead of waiting for 3 seconds after detecting the switch closure, wait for a much shorter time eg. 30ms, increment a counter, then check the switch again. If the switch is now open then reset the counter and go back to waiting for the switch to close. If the counter reaches 100 the switch has been closed for 100*30ms = 3 seconds, so you can turn on the LED and then just wait for the switch to open.

This method is a simple form of cooperative multitasking, where each 'task' (checking the switch, counting time, operating the LED) gets control of the CPU in sequence. It relies on each task only 'running' for a short time before handing control to the next one, and storing its current state (in this case the time count) in static variables so it can pick up from where it was the last time it 'ran'. With careful coding you can make the MCU appear to be doing several things simultaneously.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I built a simple timer for PCB exposure using the 16F57. I programmed accurately timed "fake interrupts" by using a Bresenham counter and a vertical counter to debounce the input switches. In addition it drives 4 seven segment digits. The reason for using this particular chip, it's very cheap, has a lot of IO pins, and good enough for this simple task, though it requires a bit more of programming. The task OP is requiring it to do will certainly not be a problem if he follows your answer. \$\endgroup\$
    – Bart
    Nov 12, 2019 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.