In this series I will build a remote-controlled timer using a PIC16F1829. The remote-controlled timer will be built in a series of steps, A, B, C, …
A simple prototype board used to build and program the remote-switch.
Source code can be found at Github.
1) The problem
In our house we have a number of timers controlling lamps.
Turned on in the morning, off during daytime, on in the evening and then off during the night.
But as the sunrise-sunset in the winter (mid Dec) is 09:30 – 14:15 and in the summer (mid Jun) 01:50 – 22:10 the timers have to be adjusted frequently. Perhaps once a month.
2) The idea
Use a remote switch set.
And use a PIC microcontroller to handle the remote transmitter.
3) Inside the remote transmitter.
Powered by a 3V battery.
Easy to replace the keys with cables to the PIC controller board I have to build. Using only the A on/off switches.
And in the same time replace the Lithium battery with 2 AA cells.
4) General design decisions
How to make PIC to simulate the switch, assuming there is some sort of scanning used in the remote transmitter? Why not use a switch controlled by the PIC? A CD4066 should be fine.
A quick test with a CD4066 showed it worked fine with one of the four switches in a CD4066. But as the CD4066 contains 4 switches I used 2 switches in parallel (2 for A on and 2 for A off) to make sure it will work even with another batch of CD4066/remote transmitter.
How to set time. I don’t want to have some display/buttons to set time. The solution is to have a UART/”RS232” interface to a PC and set time that way. This also gives the possibility to change the on/off time.
Yes, I can attach a 32kHz crystal to the Timer1 in the PIC and once initialized from the PC the PIC can handle time.
But the use case to “Change battery” created a bit of problems. The PIC will restart and the time has to be reinitialized from a PC. Not very user friendly.
And then there is leap years. Implement a leap year algorithm in the PIC? No, do not want to do that.
The solution to time was to use a RTC chip. The DS1302 handled both problems (change battery and leap years). The DS1302 interface is not I2C (compared to DS1307) but the possibility to use a backup supercap for the DS1302 made the DS1302 a “better” choice.
Many PIC’s have a built in UART and with the Microchip MCP2221, or other solutions, it is easy to interface with a PC using USB/UART.
Any modern 8-bit PIC can be used but as I have used the PIC16F1829 family before, I selected PIC16F1829. I also have the AC244063 Emulation Extension Pak if needed.
The PIC16F1829 family is nice from a debugging point of view as it has 3 built in breakpoints without using the AC244063. But if I really get into software troubleshooting the AC244063 combined with my REAL ICE is a great help.
No need for any fancy tool. All right, I have access to a Real ICE and an emulator extension pack, AC244063, but it is only “nice to have”. So in this project, I used a Curiosity board! A simple homemade adapter makes the Curiosity an excellent programming/debugging tool. Cheap (US $20) and supported by Microchip MPLAB X.
(And the Curiosity also can provide the necessary 3.3 volt for the board I have to build.)
A number of LED’s controlled by the PIC can help a lot in debugging. A RGB LED was included in the design.
Adjusting the on/off time once a month is enough. The PC can initialize the time for every month.
A simple Windows application can be used to select on/off time for every month and then send it to the PIC with current date/time.
Why should the PIC handle this? Run the PIC on winter time all year around. The PC program can easily handle daylight saving from the user perspective and convert it to winter time for the PIC.
Saving battery life
Keep the PIC in sleep mode as much as possible is a simple rule.
Putting the 1829 in sleep most of the time, how to wake it up when needed?
The only reasonable alternatives are
- WDT (watch dog timer)
- IOC (interrupt on change) on some pins.
As not all 1829 pins are capable of IOC so care must be taken to use IOC capable pins for those inputs that needs to wake up the 1829.
The pin RA4 can output the MCU clock (Fosc/4). This can be handy in debugging to see when the 1829 goes to sleep and wakes up. (No clock output during sleep, easy to debug with a simple oscilloscope, if needed.)
MPLAB X is the natural choice. Free and supported by Microchip. Used version 4.20.
Language: MPASM. (Yes I know, using C it is easy to “shoot oneself in the foot”, but using assembler you do it all the time.) But to really get to know a CPU/MCU, assembler is the only way. Used version 5.77 included in MPLAB x 4.20.
Programmer/Debugger: Microchip Cuirosity board (DM164137)