Author Topic: Let sleeping dogs lie  (Read 2437 times)

0 Members and 1 Guest are viewing this topic.

Offline PerranOakTopic starter

  • Frequent Contributor
  • **
  • Posts: 548
  • Country: gb
Let sleeping dogs lie
« on: August 16, 2018, 03:32:12 pm »
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 the sleep command (I'm in assembly) and wake it up using the watchdog. I am using Timer1 elsewhere 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 occur regardless of 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 in sleep.

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.
You can release yourself but the only way to go is down!
RJD
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: Let sleeping dogs lie
« Reply #1 on: August 16, 2018, 04:01:41 pm »
so you are using TIMER1 interrupt to do something, correct? because
Quote
For the device to
wake-up through an interrupt event, the corresponding
interrupt enable bit must be enabled

so 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 TMR1 external clock is unaffected, but if TMR1 is using the internal clock in theory FOSC is disabled during sleep
 
The following users thanked this post: PerranOak

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3146
  • Country: ca
Re: Let sleeping dogs lie
« Reply #2 on: August 16, 2018, 06:15:53 pm »
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.
 

Offline PerranOakTopic starter

  • Frequent Contributor
  • **
  • Posts: 548
  • Country: gb
Re: Let sleeping dogs lie
« Reply #3 on: August 17, 2018, 11:32:21 am »
Thank you both.

Basically, I want to measure a temperature after various intervals from one second right up to one hour. I use Timer1 for the lower intervals and sleep/watchdog for longer ones. So, I guess, when using sleep I don't need Timer1 so 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 for Timer1 but when using sleep I'm not using Timer1 so that's ok?

I see, so if I clear T1IE then 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.
You can release yourself but the only way to go is down!
RJD
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12856
Re: Let sleeping dogs lie
« Reply #4 on: August 17, 2018, 11:56:51 am »
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.
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3146
  • Country: ca
Re: Let sleeping dogs lie
« Reply #5 on: August 17, 2018, 02:45:15 pm »
I see, so if I clear T1IE then the interrupt will not wake the PIC?

Clearing T1IE will prevent waking up.
 
The following users thanked this post: PerranOak

Offline PerranOakTopic starter

  • Frequent Contributor
  • **
  • Posts: 548
  • Country: gb
Re: Let sleeping dogs lie
« Reply #6 on: August 17, 2018, 03:41:40 pm »
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!
You can release yourself but the only way to go is down!
RJD
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12856
Re: Let sleeping dogs lie
« Reply #7 on: August 17, 2018, 04:27:21 pm »
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.)
 

Offline PerranOakTopic starter

  • Frequent Contributor
  • **
  • Posts: 548
  • Country: gb
Re: Let sleeping dogs lie
« Reply #8 on: August 17, 2018, 05:06:28 pm »
Oh bugger!

I've used the following:

RA0: LM35 temp sensor
RA1: 7-seg LED(1) cathode
RA2: 7-seg LED(2) cathode
RA3: 7-seg LED(3) cathode
RA4: potentiometer (dimmer)
RA5: Vdd
RA6: run/store switch
RA7: mode input button
RB0-7: 7-seg LED anodes

I'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 clear T1CON/TMR1ON rather than TMR1IE so 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.
You can release yourself but the only way to go is down!
RJD
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12856
Re: Let sleeping dogs lie
« Reply #9 on: August 17, 2018, 05:50:20 pm »
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.
 

Offline PerranOakTopic starter

  • Frequent Contributor
  • **
  • Posts: 548
  • Country: gb
Re: Let sleeping dogs lie
« Reply #10 on: August 18, 2018, 05:57:15 pm »
Ah right thanks. I'm hoping that 4 AAs would keep it going for 10 days or so.
You can release yourself but the only way to go is down!
RJD
 

Offline kcozens

  • Contributor
  • Posts: 44
  • Country: ca
Re: Let sleeping dogs lie
« Reply #11 on: August 21, 2018, 03:29:37 pm »
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.
 

Offline PerranOakTopic starter

  • Frequent Contributor
  • **
  • Posts: 548
  • Country: gb
Re: Let sleeping dogs lie
« Reply #12 on: August 23, 2018, 03:06:31 pm »
Thanks, that sounds intriguing; I'll search for it.
You can release yourself but the only way to go is down!
RJD
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf