Let sleeping dogs lie - Page 1
Forum section: electronics / general

author: PerranOak
author_rank: Frequent Contributor
timestamp: August 16, 2018, 03:32:12 pm
content: I have read the datasheet for the PIC16F1827 that I'm using, ten times now and I still don't get it!I want to use thesleepcommand (I'm in assembly) and wake it up using the watchdog. I am usingTimer1elsewhere in the programme.The datasheet says (9.1):For the device to wake-up through an interrupt event, the corresponding interrupt enable bit must be enabled.Wake-up will occurregardlessof the state of the GIE bit.(my emphasis)Well, what chance does that give me!? I don't want the interrupt to wake it but you can't turn interrupts off! The PIC seems to ignore GIE when insleep.I realise that I must be being thick but I can't see what I've missed. This time I really have read the datasheet(!)Any help gratefully received.

author: JPortici
author_rank: Super Contributor
timestamp: August 16, 2018, 04:01:41 pm
content: so you are using TIMER1 interrupt to do something, correct? becauseQuoteso you don't have to clear GIE but T1IE before entering slepp.However, what is the clock source of timer 1? because in 9.0 it says that TMR1externalclock is unaffected, but if TMR1 is using the internal clock in theory FOSC is disabled during sleep

author: NorthGuy
author_rank: Super Contributor
timestamp: August 16, 2018, 06:15:53 pm
content: The enabled interrupts will wake you from sleep (of course, the interrupt may not be able to happen during sleep if it depends on the clock), but if GIE is off the ISR doesn't get executed. Instead, the execution will continue after the "sleep" instruction.If GIE is on, the first thing being executed after wake up will be the ISR.This gives you enough flexibility to do what you want, such as wake up with an interrupt you don't otherwise handle.

author: PerranOak
author_rank: Frequent Contributor
timestamp: August 17, 2018, 11:32:21 am
content: Thank you both.Basically, I want to measure a temperature after various intervals from one second right up to one hour. I useTimer1for the lower intervals andsleep/watchdog for longer ones. So, I guess, when usingsleepI don't needTimer1so I thought I'd disable it (as I normally do) by clearing GIE but this doesn't stop the interrupt from waking the PIC. I am using the internal clock forTimer1but when usingsleepI'm not usingTimer1so that's ok?I see, so if I clearT1IEthen the interrupt will not wake the PIC?I'm trying to be efficient with the battery and so don't want it waking all the time even if I can deal with this in the code.

author: Ian.M
author_rank: Super Contributor
timestamp: August 17, 2018, 11:56:51 am
content: The watchdog is notoriously inaccurate as a timer. If you need your measurement to be taken at a consistent interval, you will need a crystal controlled event to wake the PIC from sleep, either an external RTC chip with low frequency squarewave or alarm output connected to an interrupt capable pin on the PIC, or a simple 32.768KHz watch crystal (+ load caps) connected to T1OSI (RB6) and T1OSO (RB7) so Timer 1 can continue to run in sleep mode and trigger its overflow interrupt. N.B as these pins are used for ICSP, the crystal *MUST* be disconnected while programming the device.

author: NorthGuy
author_rank: Super Contributor
timestamp: August 17, 2018, 02:45:15 pm
content: Quote from: PerranOak on August 17, 2018, 11:32:21 amClearing T1IE will prevent waking up.

author: PerranOak
author_rank: Frequent Contributor
timestamp: August 17, 2018, 03:41:40 pm
content: Ian.M: thank you but a rough "hour" will be fine. Also, I have no pins left to use!NorthGuy: cheers, I'll get on with programming!

author: Ian.M
author_rank: Super Contributor
timestamp: August 17, 2018, 04:27:21 pm
content: Datasheet table 30-5 param 31 gives the WDT period as min 10ms typ 16ms max 27ms when the period select bits are set for 1:512 ( nominal 16ms period).That means that your hour is going to be very rough indeed - it could be as little as 37.5 minutes or as long as slightly over 101 minutes. Although it is possible to calibrate out some of the error, it varies with supply voltage and temperature so the remaining error is likely to be significant.The no pins left to use problem may be solvable. What do you currently have connected to the PIC? (Ideally post a schematic, but we can manage with a list of port pins and what they are used for.)

author: PerranOak
author_rank: Frequent Contributor
timestamp: August 17, 2018, 05:06:28 pm
content: Oh bugger!I've used the following:RA0: LM35 temp sensorRA1: 7-seg LED(1) cathodeRA2: 7-seg LED(2) cathodeRA3: 7-seg LED(3) cathodeRA4: potentiometer (dimmer)RA5: VddRA6: run/store switchRA7: mode input buttonRB0-7: 7-seg LED anodesI'm trying to practice basic techniques so I use old fashioned LEDs, pots and buttons and do ADC, multiplexing, de-bounce and all those things in assembly.Thank you.BTW, a thought on timers, would it be better to clearT1CON/TMR1ONrather thanTMR1IEso that the timer is not still running while its interrupt is disabled: you can't know where it will be in its count when you come back to it otherwise.

author: Ian.M
author_rank: Super Contributor
timestamp: August 17, 2018, 05:50:20 pm
content: Yep, you've screwed the pooch. Although it would be possible to add some diodes and multiplex the button and switch into RA5 used as an input pin, that would mean /MCLR would need to be disabled in the CONFIG, which makes it difficult to reprogram 18 pin parts with T1OSC enabled.Probably the least evil option would be to drop the clock speed to 31.25 kHz from the MFINTOSC and stay running at minimum speed rather than going to sleep and using the WDT to wake up. Its going to use a few tens of uA more current, so you may see the impact on battery life.

author: PerranOak
author_rank: Frequent Contributor
timestamp: August 18, 2018, 05:57:15 pm
content: Ah right thanks. I'm hoping that 4 AAs would keep it going for 10 days or so.

author: kcozens
author_rank: Contributor
timestamp: August 21, 2018, 03:29:37 pm
content: You can drive 8 7 segment displays using only 9 wires with charlieplexing. It would let you free up two pins. There are some trade-offs in using that method of multiplexing: number of pins vs display brightness and complexity of code. It is something you can investigate if you really want/need to free up some I/O pins.

author: PerranOak
author_rank: Frequent Contributor
timestamp: August 23, 2018, 03:06:31 pm
content: Thanks, that sounds intriguing; I'll search for it.