PWM Control with led using PIC18F87K22

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

Moderator: Benj

r_teixeir
Posts: 96
Joined: Fri Jul 15, 2016 10:51 am
Has thanked: 24 times
Been thanked: 5 times
Contact:

PWM Control with led using PIC18F87K22

Post by r_teixeir »

Hi all, I just wanted to know what am I doing wrong about the PWM control using the Pic18F87K22 with leds and buttons.

Can somebody give me an hint?

Thanks I Appreciate

Rod

r_teixeir
Posts: 96
Joined: Fri Jul 15, 2016 10:51 am
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: PWM Control with led using PIC18F87K22

Post by r_teixeir »

CONTROL_PWM_PIC18F87K22.fcfx
(13.79 KiB) Downloaded 395 times

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

Re: PWM Control with led using PIC18F87K22

Post by medelec35 »

Hi Rod,
In your flowchart, you are only reading switches once!
So if any change state, they will not be detected.
You need to keep reading switches so they can update variables when they change state.
Just move the inputs to within main loop:
Input Switches.png
(23.1 KiB) Downloaded 6427 times
The other thing I have noticed is your using a 5ms delay which is too small if pressing a button.

If you don't increase it then as soon as button is pressed the PWM will have gone from minimum to maximum value on hardware.
Probably will be OK for simulation purposes.

Martin
Martin

r_teixeir
Posts: 96
Joined: Fri Jul 15, 2016 10:51 am
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: PWM Control with led using PIC18F87K22

Post by r_teixeir »

Hi Medelec, thanks for the reply. I am at work right now, and i[ll give it a try later at home.
Considering the code, I set up 40ms debouncing for inputs configuration on the properties painel for the buttons, I thought I was ok by doing that.

The inputs for the buttons, I also thought I could inialize before the while loop, but i´ll test later on to see if it worked nicely the way you explained.

Thanks again

Rod

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

Re: PWM Control with led using PIC18F87K22

Post by medelec35 »

Hi Rod,
r_teixeir wrote:I set up 40ms debouncing for inputs configuration on the properties painel for the buttons, I thought I was ok by doing that.
Still maybe too quick, perhaps trial and error adjustment when testing on hardware.
r_teixeir wrote:he inputs for the buttons, I also thought I could inialize before the while loop,
No does not work like that.
Input icons, Switches ADC's and output icons + basic LED's etc. (any component that does not have Start or initialize) Don't require initialization.
They or only used at the required point of flowchart.
E.g only at the point you need to read input or produce an output.

Martin
Martin

r_teixeir
Posts: 96
Joined: Fri Jul 15, 2016 10:51 am
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: PWM Control with led using PIC18F87K22

Post by r_teixeir »

Hi Medelec, thanks for the tips about the initialization of components! I did not know about these details!

The buttons worked on simulation with the changing of button press on the pwm. On the other side, when i test it on hardware iam having trouble with fading of the leds.
Ive already tested on C from MikroC it worked nicely with leds changing plus and minus so I know the program is solid. Do you know why iam not getting the leds to be changing on flowcode. I want to pass this first test so I can start my pid code open loop and closed loop coding. I Have the same problem with the adc control using the potentiometer which I can upload na attachment later on.

Even though on my code I have a macro for pwm set duty cycle for the pin C2 and C1, and I know that this should output the variability of period to the pin c1 and c2, so I considered including as well to include the variable current_duty and current_duty1 to the output C1 and C2 , just to make sure the leds has a changing due to the pwm. But unfortunetely it didnt work. Can you please help with with this issue?

Thanks again

Rod

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

Re: PWM Control with led using PIC18F87K22

Post by medelec35 »

Hi Rod,
Your welcome.

When using PWM you don't use output Icons.
The outputs are set-up within the PWM component itself.
So long as you connect the LED's to the pins stated within PWM properties, all should be well.
You can also remove C1 & C2 LED's from your controls panel.
They won't affect performance but won't show anything anyway.

Also the

Code: Select all

ancon0 = 0;
ancon1 = 0;
ancon2 = 0;
are not required as it should be already be taken care off automatically.
Flowcode is designed so as little C code is used as possible.
Martin

r_teixeir
Posts: 96
Joined: Fri Jul 15, 2016 10:51 am
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: PWM Control with led using PIC18F87K22

Post by r_teixeir »

Hi Medelec, thanks for the tips, very nice.
I´ve already tested a pwm fade code from the matrixtsl website : https://www.matrixtsl.com/wikiv7/index. ... 9ba088505c

I downloaded into my hardware easypic-pro, that uses the pic18F87K22, https://shop.mikroe.com/easypic-pro, I kept the configuration of the bits for clocks, watchdogs the same as I uploaded above, butit keeps on not fading the led on c1 and c2 in hardware. I can see it utrning on and of if I press the button, but no fading.

Is there some config that i should be missing? In my other bards using the pic16F877A it works perfect. On the other side on this pic18F87k22, there is no changing....fading of leds

Thanks

Rod

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

Re: PWM Control with led using PIC18F87K22

Post by medelec35 »

Hi Rod,
Your welcome.
What I always do before developing any new project is to set up a 1 second flash test.
Once the timing is correct, I start to develop my project.
If LED is not flashing at the correct rate then you have a hardware or configuration settings issue.
If LED is flashing at the correct rate then I will look deeper at your flowchart.

Can you post your Hex file?

Martin
Martin

r_teixeir
Posts: 96
Joined: Fri Jul 15, 2016 10:51 am
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: PWM Control with led using PIC18F87K22

Post by r_teixeir »

Ok I understand. I will post it later at home, because Iam work right now.
Thanks again
Rod

r_teixeir
Posts: 96
Joined: Fri Jul 15, 2016 10:51 am
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: PWM Control with led using PIC18F87K22

Post by r_teixeir »

Hi Medelc,
I tested both codes: The one-sec delay ran perfectly; However on the program for pwm it did not even light up the leds

I uploaded both hexdecimal codes

The PWM is frustrating, hope you can help with it, Ive tested with diferente delas, still no fading leds.

Thank you

Rod
Attachments
Led_control_1s_Delay_Button_Control_PWM.hex
(2.48 KiB) Downloaded 237 times
Led_control_1s_Delay.hex
(908 Bytes) Downloaded 214 times

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

Re: PWM Control with led using PIC18F87K22

Post by medelec35 »

Hi Rod,
There are a few things you should do.
1) Check you have the latest version of Flwocode which is 6.1.4.0
2) Even if just updated with latest version of V6, Replace attached file in Flowcode 6\CAL\PIC folder which is a fix from this post
3) Change the last decision branch from

Code: Select all

RA1 == 0
to

Code: Select all

RA3 == 0
In Flowcode you only require one = as two of them are not required.

Martin
Attachments
PIC_CAL_PWM.c
(14.18 KiB) Downloaded 219 times
Martin

r_teixeir
Posts: 96
Joined: Fri Jul 15, 2016 10:51 am
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: PWM Control with led using PIC18F87K22

Post by r_teixeir »

Hi Medelec,
1st step: I do have the version you´ve mention on last post: 6.1.4.0(08/11/2016). I also attached the file to replace the existing pmw cal into the pic folder in c drive.
After doing the 1st step, I decided to run the code considering the tips about the C code for configuring pins during initializations for analog or digital, as you said that flowcode takes care of it if left out of the flowchart.
I also performed tests including and not including the leds and equaling the current_duty to them. It also did not work the fading of leds. But indeed it turned them on and off but as if they were digital 0 or 1, no fading. If I take the leds off of the code they dont even light up on my board considering they are functioning right and the dipswitches are enabled. Which means, if I only leave the component macro for pwm to be outputting the variable current_duty that I called pointing to the C2 or C1 in the macro properties, when download into the hardware the leds dont even light up.

Well, If you have any other tips I would appreciate very much

Thank s

Rod
Attachments
Led_control_1s_Delay_Button_Control_PWM.hex
(2.77 KiB) Downloaded 118 times
Led_control_1s_Delay_Button_Control_PWM.fcfx
(12.24 KiB) Downloaded 151 times

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

Re: PWM Control with led using PIC18F87K22

Post by medelec35 »

Hi Rod.
I have attached two flowcharts to try.
If the Led_control Test hardware one works(only tests switches and LED's, not PWM) but V2 of LED control does not then there maybe a bug with PIC18F87K22 PWM so best for Matrix staff to see what the situation is.
For simulation purposes I have changed the components to the 3D system panel as it more clearly shows when switches are being pressed.
Just make sure than the Table top size is down to a minimum.
To do that:
System panel1.png
(69.9 KiB) Downloaded 6312 times
If the Led_control Test hardware works then all it should do is read when any of the 4 i/p's goes low, then enables LED only while i/p is low.

Do you have pull-up resistors connected to RA0 to RA3?

Martin
Attachments
Led_control Test hardware.fcfx
(11.41 KiB) Downloaded 156 times
Led_control_1s_Delay_Button_Control_PWM V2.fcfx
(12.21 KiB) Downloaded 154 times
Martin

r_teixeir
Posts: 96
Joined: Fri Jul 15, 2016 10:51 am
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: PWM Control with led using PIC18F87K22

Post by r_teixeir »

Hi Medelec, I tried to run the led_control_test_hardware, and the c1 output initializes and stays on all the time, it does not change at all from off to on! The led C1 stays on all the time, it does not vary at all with the pressing of the button RA0 neither RA1. The C2 is perfect and is running like simulation, which means, that when button is pressed there is a change of state on C2 output from 0 to 1 (on).

I also changed the configuration of the Project options as you can see on the Picture attached, but during download into the hardware the code hasnt also changed at all, it keeps the c1 as on all the time. It does matter if in the Project options the ccp2 mux is in RC1 or the CCP2 RE7.

OBS: I uploaded a Picture which shows the board and its pull up resistor, and the utput C1 led on all the time in run mode.

So what whats the next step?

Thanks

Rod
Attachments
C1_Output_On_and_Pull_Up_Resistor.JPG
(147.75 KiB) Downloaded 6286 times
Project Options.jpg
Project Options.jpg (161.94 KiB) Viewed 12251 times

r_teixeir
Posts: 96
Joined: Fri Jul 15, 2016 10:51 am
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: PWM Control with led using PIC18F87K22

Post by r_teixeir »

Hi Medelec, On the second code: Led_control_1s_Delay_Button_Control_PWM V2.fcfx, I ran the code after downloaded into chip, and it is amazing that nothing happenned! It does not show anything and it doesnt light up no leds.

I also tested changing the ccp2 mux from rc1 to re7 and vice versa and nothing happened as well.

What else can we do?

Thanks

Rod

r_teixeir
Posts: 96
Joined: Fri Jul 15, 2016 10:51 am
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: PWM Control with led using PIC18F87K22

Post by r_teixeir »

In the mikroc pwm library there is C version for there pic16f887, i modified for the pic18f87k22 and it worked perfectly. In flowcode we are applying the same principle and something is happenning. I was thinking about maybe It could be the frequency for the pwm in the componentes properties, is there a way to change It? In the c code the parameter is configured as 5000 and it is a constant long Freq based on correct values in respect with Fosc.
The website is refered as: pwm library MikroChttps://download.mikroe.com/documents/compilers/mikroc/pic/help/pwm_library.htm

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

Re: PWM Control with led using PIC18F87K22

Post by medelec35 »

Hi Rod.
Only changed target device to 16F1937 today as that what chip I'm testing with.
Working perfect for me.
Had one LED on maximum brightness, while other one on minimum brightness.
Both the waveforms and appearance are correct:
PWM Waveform LEDs1.png
(68.6 KiB) Downloaded 6266 times
PWM LED1.jpg
PWM LED1.jpg (45.79 KiB) Viewed 12231 times
.
So as you can see its not the flowchart that's wrong unless solely down to configuration settings?
D2 is bright and D1 is dim!

Can you look in the \Flowcode 6\CAL\PIC\PIC_CAL_PWM file and let me know from properties the exact size, and modified date.
If you have the correct version and Flowcode has been restarted since replacing file, then PIC18F87K22 PWM requires further investigation.
I did try hex file for PIC18F87K22 in proteus simulation and it works as PWM could be adjusted.
The only thing I can suggest is recompile to hex of the flwochart pwm that I sent, then sent you.
Can you also send the attached hex file to your target device and see if that works?
Attachments
Led_control_1s_Delay_Button_Control_PWM V2.hex
(2.57 KiB) Downloaded 128 times
Martin

r_teixeir
Posts: 96
Joined: Fri Jul 15, 2016 10:51 am
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: PWM Control with led using PIC18F87K22

Post by r_teixeir »

Hio Medelec, I News about the Led_control_Hardware_Test. I took out all the jumpers on the board and I realized that the RA0 had a jumper on it for analog. So this first program worked.

Now the other program that has the pwm control, still no answer. The leds dont light up neither fade.

Thanks

Rod

r_teixeir
Posts: 96
Joined: Fri Jul 15, 2016 10:51 am
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: PWM Control with led using PIC18F87K22

Post by r_teixeir »

Hi Medelc great News, the hexadecimal file you sent, worked perfectly. What did you do?

Thank you

Rod

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

Re: PWM Control with led using PIC18F87K22

Post by medelec35 »

That is from the

Code: Select all

 Led_control_1s_Delay_Button_Control_PWM V2.fcfx
I have sent you. Not changed anything.
So it looks like you have not updated PIC_CAL_PWM.c properly or reloaded your flowchart since replacing the file!

Martin
Martin

r_teixeir
Posts: 96
Joined: Fri Jul 15, 2016 10:51 am
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: PWM Control with led using PIC18F87K22

Post by r_teixeir »

The size of the file is attached. If you need any further info, let me know.

Thank you

Rod
Attachments
PWM_CAL.jpg
PWM_CAL.jpg (101.73 KiB) Viewed 12222 times

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

Re: PWM Control with led using PIC18F87K22

Post by medelec35 »

That's the issue!
If you download it it should show the modified date as the date you downloaded it, Not the 18/12/2015
So try re-downloading and replace existing

Code: Select all

PIC_CAL_PWM.c
I have double checked and the file I posted is the correct version.
Make sure the file you have is not from 18.12.15:
PIC_CAL_PWM.png
(60.81 KiB) Downloaded 6254 times
Notepad++ has a plug-in that allows you to compare files with text in.

Martin
Martin

r_teixeir
Posts: 96
Joined: Fri Jul 15, 2016 10:51 am
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: PWM Control with led using PIC18F87K22

Post by r_teixeir »

Hi Medelec, I modified the file as instructed. The program worked only for C2 output. The C1 output does not fade at all. It does not light up, it stays on 0 all the time.

Thank you

Rod
Attachments
PWM_CAL.jpg
PWM_CAL.jpg (100.49 KiB) Viewed 12213 times

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

Re: PWM Control with led using PIC18F87K22

Post by medelec35 »

Your heading in the right direction.
Try again with attached flowchart.

Martin
Attachments
Led_control_1s_Delay_Button_Control_PWM V2.fcfx
(12.21 KiB) Downloaded 181 times
Martin

Post Reply