View previous topic :: View next topic |
Author |
Message |
sanddune008
Joined: 23 Oct 2008 Posts: 38
|
Problem with PWM |
Posted: Tue Nov 18, 2008 5:07 am |
|
|
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
|
|
Posted: Tue Nov 18, 2008 5:52 am |
|
|
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
|
|
Posted: Tue Nov 18, 2008 6:38 am |
|
|
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
|
|
Posted: Tue Nov 18, 2008 8:27 am |
|
|
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
|
|
Posted: Tue Nov 18, 2008 12:03 pm |
|
|
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
|
|
Posted: Tue Nov 18, 2008 1:28 pm |
|
|
PCM programmer wrote: |
Look closely at the line in bold. |
........ ........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
|
|
Posted: Wed Nov 19, 2008 2:09 am |
|
|
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 |
|
|
|