Replicating C code
Replicating C code
Please can you help with:
1) On the 16F690 datasheet it says RA2 is the interrupt pin, but when I use your Interrupt flowchart icon it lists RB0/INT as if RB0 is the interrupt one. My mistake or yours?
2) I am trying to develop a light dimmer using Microchip Application Note http://ww1.microchip.com/downloads/en/A ... 40171a.pdf with some changes like changing their 12C508 to a 16F690 and not using their large ButtonPress Macro.
They provide a crude flowchart and the C and ASM code. So using these I am trying to replicate it into Flowcode. As their version of C is a bit different to yours I can't just copy large blocks of their C into Flowcode and would not be able to easily modify that C to what I want as I don’t understand C well enough.
Basically they are converting the mains into a 0-5V 50Hz square wave and reading this into LineInput on Pin GP4, although I will use a different pin on the 690 and am not sure if I need an interrupt pin or not. When this changes from 0-1 or 1-0 it they know the zero crossing points of the mains.
They also seem (I am not completely clear on this) to be fine tuning the timing and start point of the TMR0 clock so it synchs itself with the external mains. They then fire the Triac relative to the TMR0 derived time rather than straight from the mains time/phase maybe because, due to electrical interference, the external zero crossing points could occasionally be missmeasured, but the internal clock will still be reasonably in synch. They fire the Triac at the right phase point controlled by the delay period (phase angle) value PercentOn. Dimming is altered by changing PercentOn.
My initial problem is their C lines that say:
for(Count = 0; Count < 60; Count++) //Allow power supply to stabilize
{
while(LineInput == 1);
while(LineInput == 0);
CLRWDT;
}
WREG = 0x85;
#asm ( OPTION); //1:64 tmr0 prescaler, pullups enabled
WREG = 0x1D;
while(LineInput == 1) //Synch to line phase
CLRWDT;
TMR0 = PercentOn; //Get Delay time
while(TMR0 >= 3 && LineInput == 0) //Delay to enter main at proper point
CLRWDT;
while(1) //Stay in this loop
I can’t see in Flowcode how to:
a) Preload TMR0 Module Register with the integer value PercentOn in their line TMR0 = PercentOn or whatever this line is doing. (TMR0 frequency is set at 61Hz by the 1:64 prescaler and 4MHz clock – which I need to alter to 50Hz for UK mains.)
b) Replicate the CLRWDT line and I am not clear what they are doing with this CLRWDT line.
The alternative is to give up on their code and do it myself from scratch using their general ideas. Am I being too ambitious to try to replicate their code into Flowcode?
1) On the 16F690 datasheet it says RA2 is the interrupt pin, but when I use your Interrupt flowchart icon it lists RB0/INT as if RB0 is the interrupt one. My mistake or yours?
2) I am trying to develop a light dimmer using Microchip Application Note http://ww1.microchip.com/downloads/en/A ... 40171a.pdf with some changes like changing their 12C508 to a 16F690 and not using their large ButtonPress Macro.
They provide a crude flowchart and the C and ASM code. So using these I am trying to replicate it into Flowcode. As their version of C is a bit different to yours I can't just copy large blocks of their C into Flowcode and would not be able to easily modify that C to what I want as I don’t understand C well enough.
Basically they are converting the mains into a 0-5V 50Hz square wave and reading this into LineInput on Pin GP4, although I will use a different pin on the 690 and am not sure if I need an interrupt pin or not. When this changes from 0-1 or 1-0 it they know the zero crossing points of the mains.
They also seem (I am not completely clear on this) to be fine tuning the timing and start point of the TMR0 clock so it synchs itself with the external mains. They then fire the Triac relative to the TMR0 derived time rather than straight from the mains time/phase maybe because, due to electrical interference, the external zero crossing points could occasionally be missmeasured, but the internal clock will still be reasonably in synch. They fire the Triac at the right phase point controlled by the delay period (phase angle) value PercentOn. Dimming is altered by changing PercentOn.
My initial problem is their C lines that say:
for(Count = 0; Count < 60; Count++) //Allow power supply to stabilize
{
while(LineInput == 1);
while(LineInput == 0);
CLRWDT;
}
WREG = 0x85;
#asm ( OPTION); //1:64 tmr0 prescaler, pullups enabled
WREG = 0x1D;
while(LineInput == 1) //Synch to line phase
CLRWDT;
TMR0 = PercentOn; //Get Delay time
while(TMR0 >= 3 && LineInput == 0) //Delay to enter main at proper point
CLRWDT;
while(1) //Stay in this loop
I can’t see in Flowcode how to:
a) Preload TMR0 Module Register with the integer value PercentOn in their line TMR0 = PercentOn or whatever this line is doing. (TMR0 frequency is set at 61Hz by the 1:64 prescaler and 4MHz clock – which I need to alter to 50Hz for UK mains.)
b) Replicate the CLRWDT line and I am not clear what they are doing with this CLRWDT line.
The alternative is to give up on their code and do it myself from scratch using their general ideas. Am I being too ambitious to try to replicate their code into Flowcode?
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Replicating C code
Hello,
1) Go with the datasheet, the interrupt naming in Flowcode is due to the fact that 9 times out of 10 the interrupt is on RB0.
2) Preloading timer 0 count register with PercentOn variable from Flowcode.
C Icon:
Clear watchdog is a command to reset the watchdog timer that resets the device if the code stops running. If you decide to enable the watchdog timer for your program then you canb clear it by using the following line of code in a C code icon.
1) Go with the datasheet, the interrupt naming in Flowcode is due to the fact that 9 times out of 10 the interrupt is on RB0.
2) Preloading timer 0 count register with PercentOn variable from Flowcode.
C Icon:
Code: Select all
tmr0 = FCV_PERCENTON;
Code: Select all
clr_wdt();
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: Replicating C code
Many thanks.
Is
while(TMR0 >= 3 && LineInput == 0);
correct C syntax in your C dialect as I don't think I can do that in Flowcode either?

Is
while(TMR0 >= 3 && LineInput == 0);
correct C syntax in your C dialect as I don't think I can do that in Flowcode either?
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Replicating C code
Hello,
You can do that code like this.
Create a Flowcode Byte variable named tmrcount.
Then using a c code icon.
Then using a while loop icon insert the following.
You can do that code like this.
Create a Flowcode Byte variable named tmrcount.
Then using a c code icon.
Code: Select all
FCV_TMRCOUNT = tmr0;
Code: Select all
(tmrcount >= 3 && LineInput == 0)
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: Replicating C code
Is there a list somewhere of the correct syntax for the C Code you use? I have found some C Code for various timers on the web and could reuse it but need to check it fits your syntax.
I assume I can’t use TMR0 as both an INT/RB0 and as a timer with prescaler, as Flowcode lets me enter it twice in each configuration without showing any compilation errors. I assume the second implementation just cancels the first one.
I need one timer to interrupt on LineInput changes (not what TMR0 is doing above) and another to increment a counter every 40us (or at least a repeatable time value on each increment despite code execution elsewhere). I guess I can use TMR1 for the latter if I get the clock/prescaler right, but your example of a custom timer in the help file is too difficult for me to understand.
The new routine could basically go:-
Interrupt on zero cross with TMR0.
Reset counter
Increment counter to an estimated 20msec (the length of one 50Hz cycle) in 40usec increments with TMR1
Check that next zero cross interrupt matches this 20msec. If not adjust TMR1 timing slightly.
Repeat till in synch
I assume I can’t use TMR0 as both an INT/RB0 and as a timer with prescaler, as Flowcode lets me enter it twice in each configuration without showing any compilation errors. I assume the second implementation just cancels the first one.
I need one timer to interrupt on LineInput changes (not what TMR0 is doing above) and another to increment a counter every 40us (or at least a repeatable time value on each increment despite code execution elsewhere). I guess I can use TMR1 for the latter if I get the clock/prescaler right, but your example of a custom timer in the help file is too difficult for me to understand.
The new routine could basically go:-
Interrupt on zero cross with TMR0.
Reset counter
Increment counter to an estimated 20msec (the length of one 50Hz cycle) in 40usec increments with TMR1
Check that next zero cross interrupt matches this 20msec. If not adjust TMR1 timing slightly.
Repeat till in synch
Re: Replicating C code
For instance how do I find out what the syntax/dialectechase wrote:Is there a list somewhere of the correct syntax/dialect for the C Code you use? I have found some C Code for various timers on the web and could reuse it but need to check it fits your syntax.
(tmrcount >= 3 && LineInput == 0)
(tmrcount >= 3 || LineInput == 0)
means as I need to know what symbols to use there in the middle for OR, AND, NOR and XOR.
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Replicating C code
Hello,
Here are some standard C code operators that are supported in Flowcode, Flowcode also supported the text based versions of the bitwise operators.
| OR - Bitwise OR
& AND - Bitwise AND
~ NOT - Bitwise NOT
^ XOR - Bitwise XOR
% MOD - Bitwise MOD
|| - Logical OR
&& - Logical AND
! - Logical NOT
This wikipedia page seems to detail the operators fairly well.
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
The Flowcode help file also details the supported operators on the Calculation Icon Properties page.
Here are some standard C code operators that are supported in Flowcode, Flowcode also supported the text based versions of the bitwise operators.
| OR - Bitwise OR
& AND - Bitwise AND
~ NOT - Bitwise NOT
^ XOR - Bitwise XOR
% MOD - Bitwise MOD
|| - Logical OR
&& - Logical AND
! - Logical NOT
This wikipedia page seems to detail the operators fairly well.
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
The Flowcode help file also details the supported operators on the Calculation Icon Properties page.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: Replicating C code
Thanks that is good.
So in general is there no syntax guide or complete list of all C commands you use? E.g. in example above the code I got off internet says clr_wdt(); but Flowcode uses CLRWDT; but there is no way I can find that out wthout a guide or bombarding you wtih questions.
At the moment my code, when compiling to ASM, says there may be a C Code error and I think it is likely to be just some bit of C in the custom timer2 interrupts that I have not correctly translated, even though it "compiles" to C OK. That is assuming that the code in your help file for custom interrupts is C Code and not something else.
I assume yours is C and not C++
So in general is there no syntax guide or complete list of all C commands you use? E.g. in example above the code I got off internet says clr_wdt(); but Flowcode uses CLRWDT; but there is no way I can find that out wthout a guide or bombarding you wtih questions.
At the moment my code, when compiling to ASM, says there may be a C Code error and I think it is likely to be just some bit of C in the custom timer2 interrupts that I have not correctly translated, even though it "compiles" to C OK. That is assuming that the code in your help file for custom interrupts is C Code and not something else.
I assume yours is C and not C++
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Replicating C code
Hello,
Most compilers are different which leads to problems like you are seeing. Converting code from one compiler to another is called porting the code.
Compilers like BoostC, HiTech, WINAVR, WINARM do come with documentation but this is not always ideal or straightforward, remember that technical people cannot really write understandable documentation and non technical people generally do not understand the ins and outs to be able to write a comprehensive guide.
A good way to find out the correct syntax for something is to look in the manual, then if nothing is jumping out visit the manufacturer of the compiler and see if they have a forum or something you can ask questions on. It is always good to have a play around yourself too and see if you can work it out. A compiler error is not going to break anything,
In general most of the compilers will allow certain things like the list of operators I gave to you before.
One of the key differences between say BoostC and HiTech C is that BoostC uses register names in lowercase whereas HiTech uses register names in upper case. You can normally find out the available registers on a device by looking in the compiler include folder at the definition file for your device.
Another key difference is that BoostC can access bits like this.
register.BIT
where HiTech can access bits like this.
REGISTERbits.BIT
Most compilers are different which leads to problems like you are seeing. Converting code from one compiler to another is called porting the code.
Compilers like BoostC, HiTech, WINAVR, WINARM do come with documentation but this is not always ideal or straightforward, remember that technical people cannot really write understandable documentation and non technical people generally do not understand the ins and outs to be able to write a comprehensive guide.
A good way to find out the correct syntax for something is to look in the manual, then if nothing is jumping out visit the manufacturer of the compiler and see if they have a forum or something you can ask questions on. It is always good to have a play around yourself too and see if you can work it out. A compiler error is not going to break anything,
In general most of the compilers will allow certain things like the list of operators I gave to you before.
One of the key differences between say BoostC and HiTech C is that BoostC uses register names in lowercase whereas HiTech uses register names in upper case. You can normally find out the available registers on a device by looking in the compiler include folder at the definition file for your device.
Another key difference is that BoostC can access bits like this.
register.BIT
where HiTech can access bits like this.
REGISTERbits.BIT
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel