Page 1 of 1
IR remote control with SLEEP mode
Posted: Thu Sep 17, 2009 1:11 pm
by Spanish_dude
Hi everyone,
I made an IR remote control, based on a SAMSUNG remote control. I programmed 4 switches and instead of a 32 bit signal like the SAMSUNG remote control I programmed it so it will send a 16 bit signal., 8bits for the Address and 8bits for the Command.
I would like to add the sleep mode and a watchdog, but I never used it before so I don't know how to program it, where I need to add that code, ...
I want that the microcontroller go in sleep mode when there is no switch pressed. The watchdog will reset the program every second or something like that

.
What I know about the sleep mode is that the microcontroller uses less current and the watchdog is used for "bringing" back the microcontroller to his normal mode. I'm not sure 100% ^^.
Can anyone help me with that ? (I already downloaded the Custom code for the watchdog)
Thx in advance
Nicolas
(Sorry for my bad english, as you already noticed I'm a Spanish dude

)
Re: IR remote control with SLEEP mode
Posted: Thu Sep 17, 2009 2:51 pm
by Benj
Hello Nicolas
Great about the IR remote control, have you managed to get it working correctly.
The sleep function is called by using a C code icon containing the following section of code.
sleep();
Once you have entered sleep mode you have two options for getting your device up and running again.
1) Use an interrupt. Any enabled interrupt that occurs after the sleep has been invoked will cause the device to wake up and start from where it left off. This could be a timer or a pin / port interrupt.
2) Use the watchdog timer. The watchdog timer acts to reset your device if the hardware counter attached to the watchdog reaches its max count value and overflows. This will wake your device from sleep but will start the program from the beginning. You will have to ensure that the watchdog counter gets cleared on a regular basis so your program does not keep restarting.
The code to reset the watchdog counter is done by again using a C code icon containing the following.
clear_wdt();
The watchdog custom code you have downloaded allows you to create delays via component macros that will automatically reset the watchdog counter.
Let me know how you get on.
Re: IR remote control with SLEEP mode
Posted: Thu Sep 17, 2009 3:36 pm
by Spanish_dude
Thx for your reply
How can I activate the watchdog timer ? Could you give me a little example ?
I understand the sleep mode, but not that watchdog timer.
If y do something like that :
This will activate the sleep mode and every second the program will restart ??
Thx for your help
Nicolas
Re: IR remote control with SLEEP mode
Posted: Thu Sep 17, 2009 4:43 pm
by Benj
Hello
You activate the watchdog timer via the Chip -> Configuration menu. Once in here select switch to expert mode and you have a whole host of settings including enabling the watchdog timer.
Once you have enabled the watchdog in your program you must reset the counter before it times out. Eg if you have delays in your program then the delays will probably cause the watchdog to reset the device. The delay functions in the custom component allow you to use delays whilst keeping the watchdog from overflowing, effectivly keeping the device running.
Re: IR remote control with SLEEP mode
Posted: Thu Sep 17, 2009 6:08 pm
by Spanish_dude
So if I did understand it right, the "WD_Delay_S();" is only a delay.
In my program I have used many delays in Βµs and ms. Do I need to change those delays by the WD_Delay_ms ?? Isn't there a fuction for delays in Βµs ??
I now know how to activate de watchdog, but how do you reset the watchdog ? clear_wdt(); ? When do I need to reset the watchdog, How do I know that the watchdog isn't 'overflowing' ? ...
I still don't understand it very wel
thx again for helping me
PS.: Did you know that when you write the flowcode file like this: ex. DΓ©lay.fcf instead of Delay.fcf, the compiler won't compile in ASM and HEX (error: faillure) ??
Benj wrote:Great about the IR remote control, have you managed to get it working correctly
Thx!
I think about creating a topic where I explain how I did it. It's very easy, there's no need of an interrupt or something like that. Al you need to do is write, in the macro I made, the Address and the Command you want to send with the IR Led (in Byte). First I want to finish it by adding the watchdog timer and the sleepmode
Re: IR remote control with SLEEP mode
Posted: Fri Sep 18, 2009 8:09 am
by Benj
Hello
You should be able to use the standard us delays. Are you using Flowcode V3 or V4. If you are using V3 then yes I would switch all the ms delays to use the custom ms delay. If you are using V4 then I would enable watchdog delays in the Project option window.
You reset the watchdog with the C code clear_wdt(); command or you can call the watchdog enabled delay.
You will know the watchdog isnt overflowing because it will not reset constantly and your program will run as normal. When you put the device to sleep the device will wait for a few ms and then restart from the beginning. I would also do a clear watchdog call just before you enable sleep to give you the longest sleep time possible.
You can adjust the rate of the watchdog timer by modifying the option register. The device datasheet will be able to give you the specifics.
Re: IR remote control with SLEEP mode
Posted: Fri Sep 18, 2009 9:34 am
by Spanish_dude
I'm using Flowcode V3.
Thx for your help, I will try to add that to my code.
I will do the following:
- Replace the ms delay by the C code WD_Delay_ms();
- Add the C code clear_wdt();
- Add the C code sleep();
How can I see that the microcontroller is in sleep mode ? Can I measure it with a scope ?
EDIT: I just wanted to tell you that my version of flowcode wont compile the custom code

...
Isn't there a way to do it without the Custom code ?
If I really need to change my flowcode version, is it possible with a Student version of flowcode v3 to upgrade to a Single user version (suppose it's the Pro version) of flowcode v4 ??
EDIT 2: I got a pro version of flowcode thx to one of my teachers
But when I compile, I have this error:
Launching the compiler...
C:\Program Files\Matrix Multimedia\Flowcode V3\BoostC\boostc.pic16.flowcode.exe -v -t PIC16F88 "telecommande.c"
BoostC Optimizing C Compiler Version 6.70 (for PIC16 architecture)
http://www.sourceboost.com
Copyright(C) 2004-2007 Pavel Baranov
Copyright(C) 2004-2007 David Hobday
Licensed to FlowCode User under Single user Full License for 1 node(s)
Limitations: PIC12,PIC16 max code size:Unlimited, max RAM banks:Unlimited, Non commercial use only
telecommande.c
Starting preprocessor: "C:\Program Files\Matrix Multimedia\Flowcode V3\BoostC\pp.exe" "C:\Documents and Settings\Nicolas\Bureau\test watchdog\telecommande.c" -i "C:\Program Files\Matrix Multimedia\Flowcode V3\BoostC\include" -d _PIC16F88 -la -c2 -o telecommande.pp -v -d _BOOSTC -d _PIC16
C:\Documents and Settings\Nicolas\Bureau\test watchdog\telecommande.c(106): error: missing semicolon
C:\Documents and Settings\Nicolas\Bureau\test watchdog\telecommande.c(106): error: missing right paren
C:\Documents and Settings\Nicolas\Bureau\test watchdog\telecommande.c(106): error: missing semicolon
C:\Documents and Settings\Nicolas\Bureau\test watchdog\telecommande.c(103): error: failure
failure
........................
Return code = 1
Flowcode was unable to compile the flowchart's C code due to the following errors:
If your flowchart contains C code, please review this carefully. If your flowchart contains no C-code or you have thoroughly reviewed the code, contact Technical Support.
FINISHED
I don't know why this happens. I added the custom code for the watchdog.
Thx again for your help
Nicolas
Re: IR remote control with SLEEP mode
Posted: Fri Sep 18, 2009 2:35 pm
by Benj
Hello Nicolas
Please can you attach your FCF flowcode file and the C file that is generated and I will look into this problem for you.
In V4 you do not need the custom component as the delays are already watchdog compatible if you tick the watchdog box in the View -> Project options menu.
Re: IR remote control with SLEEP mode
Posted: Fri Sep 18, 2009 3:05 pm
by Spanish_dude
Hi Benj,
As you asked, I uploaded the flowcode file and the C file.
I hope you will find what's wrong
Thx, I'm looking forward to your reply ! ^^
Nicolas
Re: IR remote control with SLEEP mode
Posted: Fri Sep 18, 2009 3:17 pm
by Benj
Hello
Ok I have found out how to get this working. The problem is a V3 problem so in V3 simply click on the custom component drop down menu, select properties and in the box for %a value set this to 98.
Alternativly open the custom_code.c file in your components directory and change the line
#define MX_RATIO %a
to
#define MX_RATIO 98
and this should work.
If you are using V4 then you do not need to use the custom component code as previously mentioned.
Re: IR remote control with SLEEP mode
Posted: Fri Sep 18, 2009 4:01 pm
by Spanish_dude
Thx Benj,
Flowcode is compilling without any error now.
How can I see that the microcontroller is in sleep mode ? Can I measure it with a scope ?
Re: IR remote control with SLEEP mode
Posted: Sat Sep 19, 2009 9:04 am
by Spanish_dude
Hi Benj (or someone else),
I looked in the datasheet of the PIC (16F88) I'm using the option register and I wanted to ask you if what I want to do is right:
Code: Select all
// C code
option_reg = 0b11001111; // or in hex : 0xCF
Port B pull-ups : Disabled (bit 7 = 1)
Interrupt Edge : Rising edge (bit 6 = 1) (rising or falling edge doesn't really matter, I don't use that interrupt)
TMR0 Clock Source : Internal clock (bit 5 = 0)
TMR0 Source Edge : low-to-high transition on RA4 (bit 4 = 0) (low-to-high or high-to-low doesn't really matter, I don't use RA4 interrupt pin)
Prescaler Assignment : Prescaler is assigned to the WDT (bit 3 = 1)
Prescaler Rate : '1:128' (bit 2 - bit 0 = 111)
So if I use a 4Mhz xtal, the frequency of the watchdog will be 30,52 Hz ( 32,765 ms )
Re: IR remote control with SLEEP mode
Posted: Mon Sep 21, 2009 2:28 pm
by Benj
Hello
Yes your maths is spot on there the Watchdog timeout period will be around every 32.7ms.
For anyone else wondering how we came to this value here is the maths.
4MHz or 4000000 / 4 to get the internal clock speed (CLKO) = 1MHz or 1000000
1MHz / 128 prescaler is 7812.5 counts per second.
The watchdog timer count register is a byte so this gives us 256 count values before rollover to 7812.5 / 256 = 30.5176 rollovers per second.
To get this value as a time period instead of a frequency we can do 1 / 30.5176 to get a timeout of 0.032768 seconds or 32.7ms
Re: IR remote control with SLEEP mode
Posted: Mon Sep 21, 2009 4:40 pm
by Spanish_dude
Thx for everything Benj
I calculate the prescaler like this:
XTAL / (prescaler * 1024)
So with a xtal of 4Mhz and prescaler of 1:128
4 000 000 / (128 * 1024) = 30,518 Hz
Nicolas