|
|
View previous topic :: View next topic |
Author |
Message |
gudurix
Joined: 16 Jul 2014 Posts: 4
|
18F4620 pwm2, unable to generate a signal |
Posted: Tue Feb 14, 2017 11:20 am |
|
|
Hi all:
I'm quite desperate with a very very simple question, but i am unable to solve the problem.
I had a longer program with several functionalities using 18F4520. The 18F4520 was into a PCB with inputs, outputs, etc. One of the features of the PCB was to generate a 40 kHz PWM Signal with timer 2 through the B3 pin. And it worked ok.
But for some reasons of pcb production, several PCBs were mounted with 18F4620 and 18F46k20.
I had to compile the complete program with 18F4620 and 18F46k20. Everything works well, except PWM2. I tried a lot of things, and anything seems to work. Any of the PCB with 18F4620 or 18F46k20 generate a signal PWM thorugh pin B3.
So, I went to the basic and tried to run a very simple program in 18F46k20 to test PWM2. The program is the following
Code: | #include "18F46k20.h"
#fuses HS, WDT256, NOPROTECT,NOLVP,BORV27,BROWNOUT,CCP2B3
#use delay(clock=4000000)
void main() {
set_tris_b(0b11110000);
enable_interrupts(INT_CCP2);
do {
setup_timer_2(T2_DIV_BY_1, 24, 1);
setup_ccp2(CCP_PWM);
set_pwm2_duty(25);
} while (TRUE);
}
|
I think my mind is becoming blur, because I can not see the reason why this simple program doesnt work.
Compiler version is 4.140
Can anybody help?
Best regards |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9254 Location: Greensville,Ontario
|
|
Posted: Tue Feb 14, 2017 11:27 am |
|
|
Maybe this...
Code: |
//timerPWM.c
//patch to get PWM running
#byte T2CON = 0xFBA
#byte PR2 = 0xFBB
#define setup_timer_2(prescaler, PR2val, postscaler) \
if(prescaler) \
{ \
PR2 = PR2val; \
T2CON = prescaler | ((postscaler -1) << 3); \
} \
else \
T2CON = 0;
//
//end of patch
|
will help?
Either PCM P or Mr T (?) provided it a long whle ago...
I don't know if it's appliable, but for my 18F46K22, it is necessary. Perhaps newer version of compiler has fixed it but I'm stuck in the 'old ages'...
Jay |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 304
|
|
Posted: Tue Feb 14, 2017 1:04 pm |
|
|
What voltage is this powered from?
The PIC18F4520 and PIC18F4620 operates from 4.2V-5.5V
The PIC18F46K20 operates from 1.8V-3.6V. |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 304
|
|
Posted: Tue Feb 14, 2017 1:30 pm |
|
|
A few observations on your test program.
You are not disabling the analog on pin B3.
You are enabling an interrupt without an interrupt handler.
You are continuously cycling through the setup commands instead of only executing them once.
The duty cycle value is greater than the period value. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19563
|
|
Posted: Tue Feb 14, 2017 2:17 pm |
|
|
First, you are resetting the timer every loop. Not going to work. Similarly you are reconfiguring the PWM each loop. Same applies. This wouldn't work with PWM1 either. Then there is the big problem, that you are setting the PWM to too large a value (bigger than the PR register). Remember if you feed an 8bit value into the duty function, it effectively multiplies it by 4. The value needs to be cast to being a long integer to access the full PWM resolution.
Code: |
#include "18F46k20.h"
#fuses HS, WDT256, NOPROTECT,NOLVP,BORV27,BROWNOUT,CCP2B3
#use delay(clock=4000000)
void main()
{
set_tris_b(0b11110000);
//enable_interrupts(INT_CCP2);
//never enable an interrupt without a handler
setup_timer_2(T2_DIV_BY_1, 24, 1);
setup_ccp2(CCP_PWM);
set_pwm2_duty(25L); //Must be cast to long
do
{
delay_cycles(10); //do nothing here
} while (TRUE);
}
|
This merrily gives a PWM on pin B3.
I started typing this, then had to have a long phone conversation. After posting it I see PCM_programmer has made the same points... |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|