IR transmission and Sleep Mode

Status
Not open for further replies.

eng1

New Member
I'm going to make some experiments in IR transmission with PIC16F628A and 4 buttons. I'd like to keep the PIC (transmitter) in Sleep Mode, until a button is pressed. An On-Change Interrupt will wake the device and start the transmission

A couple of questions:
1) after waking, where will the program start from?
2) after the interrupt, do I have to poll RB7:RB4 in the main loop of the program or let the ISR do that?

Thanks in advance.
 
It will start at the on change interupt vector, at the same CPU state that it was in before the sleep instruction was executed.
Not sure what the RB7:RB4 question means.
 
1.
Take a look at Table 14-6 for the PIC16F627/28/47 datasheet. Table shows the state of the controller for special power events. Waking up from sleep witth an interrupt will first advance the PC register by 1 and then vector to the interrupt routine 0x0004 if global bit is set.

Basically once the interrupt is completed with a RETFIE, the program will start from sleep location + 1. If you do not check to determine what caused the interrupt, then you no need to vector by not setting the global bit.


2.
To clear the interrupt, either clear RBIF or do a read/write on PORTB before sending RETFIE.
 
1) after waking, where will the program start from?
That depends on the GIE bit. If it is clear (Interupts disabled) then execution resumes with the instruction following the sleep instruction and no interupt routine is called. If the GIE is set, then donniedj's explanation is what happens.
do I have to poll RB7:RB4 in the main loop of the program or let the ISR do that
Not necessarily poll, but it may be helpfull to read the status of RB7:RB4 to decide how you wish to process the wakeup event. You can do this in either place.
 
donniedj said:
Waking up from sleep witth an interrupt will first advance the PC register by 1 and then vector to the interrupt routine 0x0004 if global bit is set.

Just to clarify that PIC wakes up from SLEEP will execute the instruction that follows the SLEEP and the result is the advancing the PC by 1 if the instruction is a one cycle instruction.

What about if the instruction is a GOTO?
 
eblc1388 said:
Just to clarify that PIC wakes up from SLEEP will execute the instruction that follows the SLEEP and the result is the advancing the PC by 1 if the instruction is a one cycle instruction.

What about if the instruction is a GOTO?

Make no difference - the PCL is incremented to the instruction following the SLEEP, not the one after. And in any case, ALL 16F instructions are one word long, the PCL doesn't increment twice for a GOTO.
 
Except for a little catch.

PIC does not increment the PC by 1. It just execute the pre-fetched instruction after wake up from sleep. This particular instruction will modify the PC if it is a GOTO instruction.

The next instruction after SLEEP is always executed, regardless whether the GIE bit is enabled or not. If it is enabled, then the PIC will goes to ISR after the instruction(in this case GOTO) is executed.
 
Hi, thank you for your answers.

I had a look at Table14-6 and Note1: "When the wake-up is due to an interrupt and global enable bit, GIE is set, the PC is loaded with the interrupt vector(0004h) after execution of PC+1". So, it is a good practice to put a NOP after the SLEEP instruction, isn't it?

What about this specification? I wouldn't like to misunderstand it...
• Dual Speed Internal Oscillator:
- Run-time selectable between 4 MHz and 37 kHz
- 4µs wake-up from Sleep, 3.0V, typical <<<<<

___________________________
EDIT: I read this after posting:
eblc1388 said:
The next instruction after SLEEP is always executed, regardless whether the GIE bit is enabled or not. If it is enabled, then the PIC will goes to ISR after the instruction(in this case GOTO) is executed.
I think you're right.
 
Last edited:
Yes it is a good idea to place a NOP after the SLEEP instruction.

Your other questions are answered on section 14.2.8. of the datasheet.

It explains how user can change, using software at runtime, the speed of the INTOSC oscillator from 4MHz into 48KHz to conserve power by changing the OSCF bit in the PCON register.

You do know that waking up from SLEEP take time(4us or longer) and therefore short events cannot wake the PIC.
 

I know that! I was asking of the 4 us specification only... doesn't it depend on the frequency of the oscillator? I'm going to use 4 MHz for now.
 
INTOSC speed has nothing to do here.

The datasheet mentioned the time to wake up from SLEEP is a fixed 6us if INTOSC mode is use(Table 14-3). Then PIC starts to execute instructions and it is here where the clock speed would then really matter.
 
eblc1388 said:
You do know that waking up from SLEEP take time(4us or longer) and therefore short events cannot wake the PIC.

Are you saying the PIC cannot detect a pulse that lasts less than 4us? Is that stated in the datasheet somewhere?
 
eblc1388 said:
The datasheet mentioned the time to wake up from SLEEP is a fixed 6us if INTOSC mode is use(Table 14-3).
You are correct.

Instead, they show that speed does matter with XT,HS and LP oscillators (Table14.3, Section14.4.3, Figure14-17).
The OST is not invoked in the INTOSC mode (if I understood correctly). Quite surprising in my opinion! If used a 4 MHz crystal, Tost would be 256 us, vs. 6 us for the internal oscillator running at the same frequency.
 
justDIY said:
Are you saying the PIC cannot detect a pulse that lasts less than 4us? Is that stated in the datasheet somewhere?

That was my assumption only, without any backing from the datasheet as I couldn't find any.

I based my assumption on the fact that INT can only be sensed by the PIC while there is a clock running. However, there is no clock in SLEEP mode. Unless the PIC somehow remember the port change INT event in hardware(the event is gone when the PIC has woken up and begin executing code, port pins have returned to their original level), then the port change condition would have gone too. So it is interesting to see what happens if one applies a 50ns pulse to one of the port pins while the PIC is in SLEEP.

I would like this special case to be covered by the datasheet but I could not locate any more information.
 
justDIY said:
I'll add it to my list of "things to test out"

Good Let me know the results.
Did you add the sleep feature to the low voltage timer we've talked about some time ago? We could look at some lines of code togheter, if you like.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…