CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Why can't I generate pulses with pwm? help please
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
memoally



Joined: 25 Jan 2010
Posts: 35

View user's profile Send private message MSN Messenger

Why can't I generate pulses with pwm? help please
PostPosted: Thu Feb 04, 2010 4:38 pm     Reply with quote

Hi everyone. I use 16f873. Our aim is to use pwm module of this pic for a project. But I can't generate pulses with this PIC although I can use pwm modules for other pics (e.g. 18f4620, 18f4431). I tried a simple network to generate pulse with via external interrupt but it failed. I request you to help me to find where the problem is or which part of code or network is wrong? Here is the code:
Code:

#include "C:\ccsc pic uygulamalar\kendi uygulamalarım\ uyg4_for_pwm.h"
int duty;
#int_EXT
void  EXT_isr(void)
{
   duty=duty+10;
   set_pwm2_duty(duty);
}

void main()
{
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_16,255,1);
   setup_ccp2(CCP_PWM);
   set_pwm2_duty(10);
   enable_interrupts(INT_EXT);
   ext_int_edge(H_TO_L);
   enable_interrupts(GLOBAL);
   
   set_tris_c(0x00);
   while(1);
}

I also add the network to a image site.here it is:
http://img508.imageshack.us/img508/765/network.jpg

I also add the network while it is being animated.here it is:
http://img46.imageshack.us/img46/2213/animatingresult.jpg

I want to increase duty cycle via pressing button, but I got flat response as it is in the picture. This network works for other pics.

I don't know how to add source codes or proteus network files, so forgive me. If anyone knows where the problem is, please correct me.
_________________
compiler version: 4.105
pic: 16f876
clock frequency: 4MHz
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 04, 2010 6:38 pm     Reply with quote

Try a simple test program for pwm on CCP2. See if you get anything
on the oscilloscope.
Code:

#include <16F873.H>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)

//=====================================
void main(void)
{
setup_ccp2(CCP_PWM);   
setup_timer_2(T2_DIV_BY_1, 255, 1);       
set_pwm2_duty(128);

while(1); 
}
Rohit de Sa



Joined: 09 Nov 2007
Posts: 282
Location: India

View user's profile Send private message Visit poster's website

PostPosted: Thu Feb 04, 2010 11:21 pm     Reply with quote

PWM requires the use of timer resources. When dealing with timers always mention the clock speed. It helps in sorting out issues. The relevant equations are:
Code:
(Tpwm)=4(Tosc)(Prescale)(PR2+1)

or
Code:
(Fpwm)=(Fosc)/(4.(Prescale)(PR2+1))

And,
Code:
(Ton)=(duty value)(Tosc)(Prescale)
Depending on your clock speed, it is possible that the output is always high; ie, if Ton>Tpwm. I don't know what your clock speed is, so I can't troubleshoot.

Rohit
Rohit de Sa



Joined: 09 Nov 2007
Posts: 282
Location: India

View user's profile Send private message Visit poster's website

PostPosted: Fri Feb 05, 2010 3:36 am     Reply with quote

I wrote:
I don't know what your clock speed is, so I can't troubleshoot
I was just doing a little bit of algebraic rearrangement and I realized that although knowing Fosc is important, if it's just a quick check that you want to make, you don't _need_ it, per se. Just make sure that
Code:
  (Tpwm)                    >  (Ton)
» 4(Tosc)(Prescale)(PR2+1)  >  (duty value)(Tosc)(Prescale)
» 4(PR2+1)                  >  (duty value)

Razz
Rohit
memoally



Joined: 25 Jan 2010
Posts: 35

View user's profile Send private message MSN Messenger

PostPosted: Fri Feb 05, 2010 10:30 am     Reply with quote

first of all thank you,pcm programmer;but it just did not work. i know your coding is true, i also know my codes were true but i can not get the right result Smile the interesting part is that i can get what i want with 18f pics, but i can not get it with various 16f pics,like 16f628a,16f877,16f870 and 16f872. even a simple led blinking problem is not working with 16f's where as there is no problem for 18f's :D i think it may come from program (?).
i use ccs picc PCWHD compiler. may the problem come from this?

rohit i also thank you, my clock speed was 4MHz, i tried various speeds but i can not get anything. i also tried external clock with crystal and capasitors,but i did not work again. Smile

an interesting problem and i am about to go crazy :D

by the way,you should advise to use 18f pics :D but i am to program a 16f872 pic which the pcb is ready Smile so i am limited with pin numbers and circuit schematic Smile
_________________
compiler version: 4.105
pic: 16f876
clock frequency: 4MHz
Guest








Re: Why can't I generate pulses with pwm? help please
PostPosted: Fri Feb 05, 2010 10:41 am     Reply with quote

memoally wrote:
Hi everyone. I use 16f873. Our aim is to use pwm module of this pic for a project. But I can't generate pulses with this PIC although I can use pwm modules for other pics (e.g. 18f4620, 18f4431). I tried a simple network to generate pulse with via external interrupt but it failed. I request you to help me to find where the problem is or which part of code or network is wrong? Here is the code:
Code:

#include "C:\ccsc pic uygulamalar\kendi uygulamalarım\ uyg4_for_pwm.h"
int duty;
#int_EXT
void  EXT_isr(void)
{
   duty=duty+10;
   set_pwm2_duty(duty);
}

void main()
{
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_16,255,1);
   setup_ccp2(CCP_PWM);
   set_pwm2_duty(10);
   enable_interrupts(INT_EXT);
   ext_int_edge(H_TO_L);
   enable_interrupts(GLOBAL);
   
   set_tris_c(0x00);
   while(1);
}

This code has no fuse and clock speed settings. How can it work? Why don't you try a simple program WITH the #fuse and clock settings and just try using a simple delay statement to turn a pin on high and low at a 1 second interval. You can check it on the scope.
memoally



Joined: 25 Jan 2010
Posts: 35

View user's profile Send private message MSN Messenger

PostPosted: Fri Feb 05, 2010 11:09 am     Reply with quote

Where do you know there is no fuse options? There is a header file you see? I can set them in the program menu, and it creates which fuses will be used or not, here it is:
Code:

#include <16F873.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES RC                       //Resistor/Capacitor Osc with CLKOUT
#FUSES NOPUT                    //No Power Up Timer
#FUSES PROTECT                  //Code protected from reads
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD

#use delay(clock=4000000)

_________________
compiler version: 4.105
pic: 16f876
clock frequency: 4MHz
Guest








PostPosted: Fri Feb 05, 2010 12:37 pm     Reply with quote

Okay, did you try to turn a single pin high and low using a simple delayms function to do a simple test first?
memoally



Joined: 25 Jan 2010
Posts: 35

View user's profile Send private message MSN Messenger

PostPosted: Fri Feb 05, 2010 1:05 pm     Reply with quote

yes it is also not working... :(((( there is a problem with 16f's :(((
_________________
compiler version: 4.105
pic: 16f876
clock frequency: 4MHz
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 05, 2010 1:32 pm     Reply with quote

Quote:
#include <16F873.h>
#device adc=8

#FUSES NOWDT //No Watch Dog Timer
#FUSES RC //Resistor/Capacitor Osc with CLKOUT

Why are you doing this ? Are you really using the external resistor
and capacitor oscillator ? If so, what are the values ?
memoally



Joined: 25 Jan 2010
Posts: 35

View user's profile Send private message MSN Messenger

PostPosted: Fri Feb 05, 2010 1:37 pm     Reply with quote

When I use crystal, I set fuses as XT. I tried a 8MHz crystal and two 22pF capacitors into ocs1/clkin and osc2/clkout pins. I also tried 4,2,1 Mhz crystals...
_________________
compiler version: 4.105
pic: 16f876
clock frequency: 4MHz
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 05, 2010 1:44 pm     Reply with quote

Is this project being done on a real hardware board, is it all just a
Proteus project ?
memoally



Joined: 25 Jan 2010
Posts: 35

View user's profile Send private message MSN Messenger

PostPosted: Fri Feb 05, 2010 1:51 pm     Reply with quote

i also tried it on real hardware,again disappointment :(
_________________
compiler version: 4.105
pic: 16f876
clock frequency: 4MHz
memoally



Joined: 25 Jan 2010
Posts: 35

View user's profile Send private message MSN Messenger

PostPosted: Fri Feb 05, 2010 1:52 pm     Reply with quote

maybe i should rework on timers but i dont think that the resource of the problem is from timer. (?)...
_________________
compiler version: 4.105
pic: 16f876
clock frequency: 4MHz
Rohit de Sa



Joined: 09 Nov 2007
Posts: 282
Location: India

View user's profile Send private message Visit poster's website

PostPosted: Fri Feb 05, 2010 7:38 pm     Reply with quote

Leave timers and PWM aside for the moment. Try getting an LED to flash first. A flashing LED is to microcontroller programming what "hello world" is to coding on a PC. Smile

Rohit
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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