ADC Controlled PWM Only Works in Flowcode

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

ADC Controlled PWM Only Works in Flowcode

Post by medelec35 »

This is how software works in flowcode:

Start: A5 and A3 not pressed, Normal and boost ADC set on min, Duty cycle = 10% & A4 LED remains off
Vary Normal ADC; duty cycle varies between 10% (ADC0=1%) and 42% (ADC0=100%)
Vary Boost ADC; duty cycle not affected.

A5 pressed. Vary Boost ADC; Duty cycle varies between 42% (ADC1=1%) and 82% (ADC1=100%)
Vary Normal ADC duty cycle not affected.
Finally while A3 is pressed a counter (Pulse) is increment, and as soon as A3 is released counter is reset to zero.
If A3 stays pressed, then after a few seconds counter = 255, A4 LED lights, and stays on until A3 is released.



After programming chip here is what happens when power is applied to hardware

Start: A5 and A3 not pressed, Normal and boost ADC set on min, Duty cycle = 10% & A4 LED remains off

Vary Normal ADC; duty cycle does not vary at all.
Vary Boost ADC; duty cycle varies between 10% (ADC1=1%) and 42% (ADC1=100%)

A5 pressed. Vary Boost ADC; Duty cycle varies between 42% (ADC1=1%) and 82% (ADC1=100%)
A3 is pressed. LED stays off.

So it appears only Boost sets duty cycle from 10% to 82%, (depending if A5 is pressed or released).
Normal ADc(ADC0) has no control over duty cycle.

Config has been set to 0x2007,0xd4.

Maybe there are settings I have missed. If anyone can help to make this run on a PIC12F683, I would be most Greatful

Thank you
Attachments
ADC Controlled PWM 12f683B.fcf
(9 KiB) Downloaded 396 times
Last edited by medelec35 on Thu Jan 29, 2009 8:22 pm, edited 1 time in total.
Martin

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: ADC CONTROLLED PWM ONLY WORKS IN FLOWCODE

Post by medelec35 »

I believe I worked out why Flowcode simulator is working, but actual hardware code is loaded into does not.

Studying asm file that was compiled by flow it looks like several different variables are assigned to the same address i.e. 0x29

In this case both ADC0 and ADC1 plus quiet a few different variables are assigned to 0x00000029

__div_8_8_00000_arg_a EQU 0x00000029 ; bytes:1
FCD_PWM0_E_0001D_arg_nIdx EQU 0x00000029 ; bytes:1
FCD_PWM0_S_0001F_arg_nIdx EQU 0x00000029 ; bytes:1
FCD_ADC0_S_00022_1_old_tris EQU 0x00000029 ; bytes:1
CompTempVarRet623 EQU 0x00000029 ; bytes:1
FCD_ADC1_S_00025_1_old_tris EQU 0x00000029 ; bytes:1
CompTempVarRet629 EQU 0x00000029 ; bytes:1
CompTempVar635 EQU 0x00000029 ; bytes:1
CompTempVar638 EQU 0x00000029 ; bytes:1
CompTempVar639 EQU 0x00000029 ; bytes:1
CompTempVar641 EQU 0x00000029 ; bytes:1
delay_ms_00000_arg_del EQU 0x00000029 ; bytes:1

I do not know much about C, however I am assuming the following means both ADC0 and ADC1 are also using same variables

void FCD_ADC0_SampleADC()
{
//This is from the FCD file...
//set up ADC conversion
char old_tris, cnt;
#define MX_ADC_SAMP_TIME 40
#define MX_ADC_TRIS_REG trisio

//find appropriate bit
#if (0 == 0)
#define MX_ADC_TRIS_MSK 0x01
#define MX_ADC_ANS_MSK 0x01
#endif
#if (0 == 1)
#define MX_ADC_TRIS_MSK 0x02
#define MX_ADC_ANS_MSK 0x02
#endif
#if (0 == 2)
#define MX_ADC_TRIS_MSK 0x04
#define MX_ADC_ANS_MSK 0x04
#endif
#if (0 == 3)
#define MX_ADC_TRIS_MSK 0x10
#define MX_ADC_ANS_MSK 0x08
#endif

And

void FCD_ADC1_SampleADC()
{
//This is from the FCD file...
//set up ADC conversion
char old_tris, cnt;
#define MX_ADC_SAMP_TIME 40
#define MX_ADC_TRIS_REG trisio

//find appropriate bit
#if (1 == 0)
#define MX_ADC_TRIS_MSK 0x01
#define MX_ADC_ANS_MSK 0x01
#endif
#if (1 == 1)
#define MX_ADC_TRIS_MSK 0x02
#define MX_ADC_ANS_MSK 0x02
#endif
#if (1 == 2)
#define MX_ADC_TRIS_MSK 0x04
#define MX_ADC_ANS_MSK 0x04
#endif
#if (1 == 3)
#define MX_ADC_TRIS_MSK 0x10
#define MX_ADC_ANS_MSK 0x08
#endif

If I am correct, would it be a better idea for Flowcode to assign a new register for each variable i.e. same as you would in asm code.

To get over this problem I will have to reorganise the flow chart so ADC1 is completely ignored, until GP5 is high, then only ADC0 is bypassed.

Is this a bug with flowcode, or am I wrong?
Martin

User avatar
Steve
Matrix Staff
Posts: 3433
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Re: ADC CONTROLLED PWM ONLY WORKS IN FLOWCODE

Post by Steve »

The variables at location 0x29 are all "local" variables in subroutines, so they can happily share the same address because their value never needs to be retained between subroutine calls. So I doubt that this would be the problem.

Try setting the config word to 0x30D4 instead.

Here are 2 other things to check:

1) make sure that GP5 is behaving correctly as an input on your hardware

2) make sure that AN0 and AN1 are reading different voltages correctly on your hardware

If you do find a problem with Flowcode, please provide us with a simple program that demonstrates this error and we'll try to fix it or provide a workaround.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: ADC CONTROLLED PWM ONLY WORKS IN FLOWCODE

Post by medelec35 »

Hardware is now working! :P

Thank you for suggestions.
Changing config to 30D4 did not solve problem, I did try that 1st, but programmer just changed config back to 00D4


Problem was as stated in previous post. ADC0 was stored at 0x29, then ADC1 was also stored at 0x29.
This register was then used to calculate Duty cycle.
However since ADC0 was always over written by ADC1, in hardware it was as though ADC0 was not there.
Simply by rearranging flowchart (see attachment) has solved problem.

ADC0 and ADC1 should not be equated to same registers, just encase one need is required to be read directly read after another, since register will have only have last read ADC stored.
Attachments
ADC Controlled PWM 12f683B.fcf
(9 KiB) Downloaded 365 times
Martin

User avatar
Steve
Matrix Staff
Posts: 3433
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Re: ADC CONTROLLED PWM ONLY WORKS IN FLOWCODE

Post by Steve »

Yes, I see the problem. It's not a bug in Flowcode, though.

When you perform an ADC conversion, the result of the conversion is stored in the PIC registers ADRESH and ADRESL. If you perform another ADC conversion before reading out these values, then the original values are lost. This is what was happenign with your first program.

The way you have solved it works well. Another way would have been to do this:

Code: Select all

SampleADC(0)
ReadADC(0)
SampleADC(1)
ReadADC(1)
instead of this:

Code: Select all

SampleADC(0)
SampleADC(1)
ReadADC(0)
ReadADC(1)

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: ADC CONTROLLED PWM ONLY WORKS IN FLOWCODE

Post by medelec35 »

Thank you Steve.

I can see the logic in what you are saying.

In the original program posted I should of sampled ADC0 then read as byte, stored as variable ADC0X

Then sampled ADC1 then read as byte, stored as variable ADC1X

But I am asking adc channel to be read twice when only once is necessary as in the 2nd attachment.

So hands up, Flowcode is not at fault. :oops:
It is a great bit of software, money well spent!
Martin

Post Reply