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

Problem with PWM

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sanddune008



Joined: 23 Oct 2008
Posts: 38

View user's profile Send private message

Problem with PWM
PostPosted: Tue Nov 18, 2008 5:07 am     Reply with quote

Hi All,

Following is the code for generating 1khz PWM signal......

Code:

#include <16F628A.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP,NOCPD       
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1)

void main(void)               //Program stepping point
{
   //Initialize I/O pins
   //App_init();   
   setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM
   setup_timer_2(T1_DIV_BY_4,249,1);
   //To generate 1khz PWM at 25% duty cycle at pin B3 CCP1
   set_pwm1_duty(62);
   printf("\r\nInitialized\n\r");
   while(1)
   {                           
      //printf("\r\nInswhile\n\r");
      RED_LED_ON;
      delay_ms(1000);//Debug - remove this
      RED_LED_OFF;//Debug - remove this
      delay_ms(1000);//Debug - remove this
   }
}



Observed behaviour.

1. Not able view the PWM signal output at the CCP1 PIN B3 on Oscilloscope
2. LED is ON all the time instead of blinking....Why is it so?are my fuses correct?

Is the code correct?

will this function turn on the timer2
setup_timer_2(T1_DIV_BY_4,249,1);?


Thanks in advance.......


[/code]
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue Nov 18, 2008 5:52 am     Reply with quote

You need to define RED_LED_ON and RED_LED_OFF

You could try OUTPUT_LOW(PIN_B3);
This will set pin B3 to output mode which the data sheet says you must do. you could also use fast_io and set the tris value instead but this way seems easier.
I would have expected CCS to do this for you though when you use setup_ccp1(CCP_PWM);
sanddune008



Joined: 23 Oct 2008
Posts: 38

View user's profile Send private message

PostPosted: Tue Nov 18, 2008 6:38 am     Reply with quote

Thanks............

Wayne_ wrote:

You need to define RED_LED_ON and RED_LED_OFF


I am sorry didn't mention the macro defintion
it goes like this #define RED_LED_ON output_low(RED_LED) and so on


Wayne_ wrote:

You could try OUTPUT_LOW(PIN_B3);


I tried adding the above mentioned line no go.

Still not able to view the PWM signal at the CCP1 pin.....

what's wrong with the code?

Code:


#use delay(clock=4000000)

void main(void)               //Program stepping point
{
   //Initialize I/O pins
   OUTPUT_LOW(PIN_B3);
   //App_init();
   setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM
   setup_timer_2(T1_DIV_BY_4,249,1);
   //To generate 1khz PWM at 25% duty cycle at pin B3 CCP1
   set_pwm1_duty(62);
   //setup_comparator(NC_NC_NC_NC );
   //printf("\r\nInitialized\n\r");
   while(1)
   {                           
      //printf("\r\nInswhile\n\r");
      RED_LED_ON;
      delay_ms(1000);//Debug - remove this
      RED_LED_OFF;//Debug - remove this
      delay_ms(1000);//Debug - remove this
   }
}

[/code]
sanddune008



Joined: 23 Oct 2008
Posts: 38

View user's profile Send private message

PostPosted: Tue Nov 18, 2008 8:27 am     Reply with quote

Is this line below necessary

setup_comparator(NC_NC_NC_NC ); //Disable comparator.


@wayne
I thought I/O direction was handled by Compiler automatically isn't?......

Will my TRIS(using fast_io) definition affect the UART interface.
eg: set_tris_B(0x00); //will make all my PORT I/O pins as output, what will
happen to my UART pin which is part of the PORT.

Correct me if i have misunderstood

Anyways Is there anything else that needs to be added for PWM generation?

Thanks in advance
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 18, 2008 12:03 pm     Reply with quote

Quote:
void main(void)
{
//Initialize I/O pins
OUTPUT_LOW(PIN_B3);
//App_init();
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_timer_2(T1_DIV_BY_4,249,1);
set_pwm1_duty(62);
//setup_comparator(NC_NC_NC_NC );
//printf("\r\nInitialized\n\r");
while(1)
{

Look closely at the line in bold.
sanddune008



Joined: 23 Oct 2008
Posts: 38

View user's profile Send private message

PostPosted: Tue Nov 18, 2008 1:28 pm     Reply with quote

PCM programmer wrote:


Look closely at the line in bold.



........Laughing ........Thanks PCM

Will my TRIS(using fast_io) definition affect the UART interface.
eg: set_tris_B(0x00); //will make all my PORT I/O pins as output, what will
happen to my UART pin which is part of the PORT.
Correct me if i have misunderstood


Last edited by sanddune008 on Tue Nov 18, 2008 1:33 pm; edited 2 times in total
sanddune008



Joined: 23 Oct 2008
Posts: 38

View user's profile Send private message

PostPosted: Wed Nov 19, 2008 2:09 am     Reply with quote

Hello,

As I am sending a sequence of bits 0xD5
Encoding of bit 0 is mention as below

Contains 38 khz pulses, for duration == (1/38Khz)*13
And No pulses for duration == (1/38Khz)*82

And different encoding for bit 1
Now I would like to know is, Is there a way to stop the PWM signal
generation at CCP1 pin for duration == (1/38Khz)*82 ?

How do I go about with encoding bit 0 using PWM and timers, don't want the delay_ms() approach as I have multiple interrupts?


thanks
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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