|
|
View previous topic :: View next topic |
Author |
Message |
kolopipo Guest
|
PWM PIC16F877 problem. |
Posted: Thu Nov 19, 2009 1:32 pm |
|
|
Hi all,
I am generating a PWM using PIC16F877. I had been trying out example code in CCS compiler for setting up PWM. When doing simulation using Proteus Isis, I can't see any PWM pulse from pin C2. Please help. Is it the frequency problem? Or the header file got problem? I still can't get any pulse even though had tried many value of duty cycle. Can't even get high signal from it.Below is my code. It is very simple program to just generate PWM. I am using 20Mhz crystal in proteus simulation.
Code: |
#include <16F877.h>
#device adc=8
#use delay(clock=20000000)
#fuses NOWDT,HS, NOPUT, NOPROTECT, BROWNOUT, LVP, NOCPD, NOWRT, NODEBUG
//////////Main Function//////////
void main ()
{
int16 duty_time;
set_tris_C(0b00000000);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1,249,1);
setup_CCP1(CCP_PWM);
while (1){
set_pwm2_duty(125);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 19, 2009 1:38 pm |
|
|
Quote: | setup_CCP1(CCP_PWM);
set_pwm2_duty(125); |
There is something about these two lines that is inconsistent.
Can you see it ? |
|
|
kolopipo Guest
|
|
Posted: Thu Nov 19, 2009 7:10 pm |
|
|
I did change it later to set_pwm1_duty(125); but still cant see any PWM outputs from it. Could it be because the frequency is too fast until the simulator cannot detect it? I doubt it though. Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 19, 2009 7:40 pm |
|
|
Quote: |
#fuses NOWDT,HS, NOPUT, NOPROTECT, BROWNOUT, LVP, NOCPD, NOWRT, NODEBUG
|
Change that fuse to NOLVP. Proteus may not look at that fuse setting,
but if you have real hardware, then using LVP can cause a problem.
The PIC will lock-up if the PGM pin goes to a high level. Use NOLVP.
Have you ever made this PIC do anything in Proteus, such as turning
on an LED ?
What is your CCS compiler version ? It's given at the top of the .LST
file. The .LST file will be in your project directory after you compile a file. |
|
|
kolopipo Guest
|
|
Posted: Thu Nov 19, 2009 8:14 pm |
|
|
Thanks for the fast reply. I am able to use the Proteus to simulate PIC16f877 to run a dc motor. I am doing the PWM to control speed. The version of the ccs is Version 3.207. Had tried the following fuses but still fail. I am suspecting there mayb some bug with CCS since even the PWM example given in CCS could not generate any PWM pulses. Do I need to disable timer1 and set the internal clock frequency for timer0? I don't think need to do so since PWM is using timer2.
Code: |
#fuses hs,noprotect,nowdt,nolvp,nobrownout
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 20, 2009 11:39 am |
|
|
I installed vs. 3.207 and made the changes to your program that I
mentioned above (NOLVP fuse and set_pwm1_duty), compiled it,
and ran it on a 16F877 on a PicDem2-Plus board. It works.
The oscilloscope shows a 40 KHz squarewave (50% duty cycle) on
pin C2. Therefore, the problem is not in the program or the compiler. |
|
|
kolopipo Guest
|
|
Posted: Fri Nov 20, 2009 12:00 pm |
|
|
Thank you for verifying it. Then, it must be Proteus problem since it is not the compiler or program problem as you said. The verification using oscilloscope further prove that the program works. Thank you. Have you use Proteus before? Just asking this just in case you have some experience in using it. Now I will start troubleshooting Proteus. Again thank you so much because I had been scratching my head for two days thinking there are problems with my program or compiler. Your time used in verifying the program using oscilloscope is greatly appreciated. |
|
|
Guest
|
|
Posted: Thu Dec 17, 2009 5:20 am |
|
|
With 20MHz
setup_timer_2(T2_DIV_BY_1,249,1);
20e6 / (1 * 249) => 20KHz, Right? |
|
|
Ttelmah Guest
|
|
Posted: Thu Dec 17, 2009 5:43 am |
|
|
The answer is right, but the calculation is wrong.....
The count used for Timer2, is the loaded counter value + 1.
The clock fed to Timer2, is the master oscillator /4, divided by the prescaler (1 for the code shown).
So the calculation is:
20E6 / ((249+1)*4*1) = 20000
Best Wishes |
|
|
|
|
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
|