Re: Low power, PICs, PBP - tips
I just finished a very low power PBP project that I had to wade through and do a little PBP hacking. I thought I’d share some results with the forum.
Processor – a generic PIC16LF876A – not the lowest power or fastest but it’s working at an average running power of 3v @ 30 microamps. I use a 32KHz crystal, the onboard ADC, SEROUT2 at 150 baud (!), onboard EEPROM and internal PORTB pull-ups to read a hex switch plus a lot of I/O. I don’t use SLEEP as I need accurate timing and continuous processing for 9 months on a couple of AA cells. I also wanted to use plain vanilla PBP(3) as is without a lot of extra programming considerations.
Concessions made:
1. I used OSC 3 (there is no OSC 32K) as this was the lowest PBP offered (bummer)
2. Commands with implied timing like Pause and PauseUS are ~ 100x (actually 93.75x) slower than expected. IE ‘pause 10’ pauses for ~1 second. Just have to remember this but not a big concession.
3. You have to remember to turn things off after you use them like the ADC and Pull-Ups
Problems remaining:
1. Sometimes touching a pin on the processor may reset it. Spraying both sides of the PCB with a clear conformal coating like HumiSeal (almost) eliminates this problem.
2. Bringing a finger close to the chip (not even touching it) will cause a drastic increase in current consumed by the processor (??).
3. Sometimes I program a new (blank) Processor in-circuit and then check the supply current to find a >10x increase in expected supply current (~400 ua instead of 30 ua). I can "usually" permanently correct this problem by re-programming it with the same hex file (!!???). I use a meLabs USB EPIC programmer.
Notes:
- I used an meProg USB programmer with OSC = LP, WatchDog and PowerUp timers enabled, BrownOut and LV programming disabled.
- Unless you have an old computer with a built-in serial port, you may have to use something like a USB2Serial adapter that works at 150 baud – hard to find. Adapters with legitimate Prolific USB USB2Serial ICs will work.
- SEROUT2 (a lucky hack) works at 150 baud with a MODE value = 16399.
- If you use resources like ADC and internal pull-ups, you need to turn them off when not in use (they consume lots of power).
- You need to make all unused pins as outputs and set them to a (convenient) value like ‘0’. This includes in-circuit programming pins, if used.
-
Basic code (This is my much larger coder…much simplified. Hope I didn’t impose any errors):
Code:
Define OSC 3 ‘the lowest PBP will go. OSC is really a 32KHz external crystal.
INCLUDE "modedefs.bas" 'mainly for serout2 commands
‘[misc variable assignments as usual … ]
Define ADC_BITS 8 ‘ I only need 8 bits of precision
TRISA = %00000001 ‘AN0 is needed for input for ADC CHL-0
TRISB = %00001111 ‘Need lower 4 bits to read hex switch using internal PORTB pull-ups
TRISC = %01000000 ‘Misc output and input bits. PortC.7 is the serout2 bit and
‘PortC.6 is the serial input bit at 150 baud
ADCON0 = %00000000 ‘FOSC/2, CHL 0, make sure ADC is off for low power (B0 = 0 -> ADC=OFF)
ADCON1 = %00001110 ‘ADC chl 0 only, use Vdd-Vss as ref
OPTION_REG.7 = 1 ‘make sure PortB pull-ups are off (for now) for low power
Serout2 PORTC.7,16399, [“Start of Program”,10,13] ‘use serout2 MODE = 16399 for 150 baud with 32 KHz clock. A lucky value that works.
‘[Initialize misc variables]
For I = 1 to 5 ‘blink the LED 5 times at 1 sec period
LED = 1 ‘turn LED on
Pause 5 ‘actually pauses for ~ 0.5 sec
LED = 0 ‘turn LED off
Pause 5 ‘pause ~0.5 sec
Next I
Main: ‘main program
Gosub GetADCval
GOSUB GetHexSw
Goto Main ‘repeat loop
GetADCval:
ADCIN 0, ADCval ‘get AN0 analog value into ADCval (ADCIN will automatically turn on internal ADC function)
ADCON0 = %00000000 ‘turn off ADC after the conversion
Serout2 PORTC.7,16399,[“ADCval = “,DEC ADCval, “ “] ‘print ADCval @ 150baud
RETURN
GetHexSwBits:
OPTION_REG.7 = 0 ‘turn on PORTB pull-ups
HexSw = PORTB & %00001111 ‘read LS4 bits of PORTB
OPTION_REG.7 = 1 ‘turn off PORTB pull-ups for low power
Serout2 PORTC.7,16399,[“HexSw = “,DEC HexSw, 10, 13] ‘print HexSw with carriage-return, line-feed
RETURN
Good luck,
Mike Sinclair
Bookmarks