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 CCS Technical Support

PIC16F88 and PWM

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



Joined: 10 Feb 2004
Posts: 7

View user's profile Send private message

PIC16F88 and PWM
PostPosted: Tue Jun 24, 2008 12:51 am     Reply with quote

Hello,
I'm trying to do PWM with a PIC16F88 and it's not working.
My example is very simple and I don't understand where i'm wrong.
With output_low and output _high pin B3 is working fine but not with PWM !
I'tried to compile it with v4.032 and v3.190 no change !
Can somebody show me the way ...please

The Code :

#include <16F88.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=115200,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8,errors)


void main() {
byte value;

set_tris_b(0b11110111); // B3 Output
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_4, 255, 1);
set_pwm1_duty(0); // PWM off

delay_ms(1000);
printf("Starting\n\r");

while( TRUE )
{
value++;
printf("%2X\r",value);
set_pwm1_duty(value);
delay_ms(100);
}
}


Many thanks in advance
Xavier
sandropiovesana



Joined: 06 May 2008
Posts: 9

View user's profile Send private message

PostPosted: Tue Jun 24, 2008 2:00 am     Reply with quote

I've fast read your code, my first idea is:

try to change:
setup_timer_2(T2_DIV_BY_4, 255, 1);

with:
setup_timer_2(T2_DIV_BY_4, 100, 1);

Good Luck
stewart



Joined: 28 Nov 2005
Posts: 12

View user's profile Send private message

PostPosted: Tue Jun 24, 2008 9:11 am     Reply with quote

xavier

PWM is super easy. It simply is based on 3 things: TMR2, CCPR1 and PR2

CCPR1 holds the period, at which point TMR2 is reset, and the output goes high. when TMR2 matches the value of PR2, the output is cleared.

Read the data sheet in this area, look-up the actual addresses of these registers and look at the listing file to see what these functions really do.

I can have a PWM running in less than 20 instructions in assembly, you have less than 4 statements to look at what they are doing in this C program.

DIVE INTO THE CODE IT HAS GENERATED

Dig a bit,

Stewart
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 24, 2008 12:09 pm     Reply with quote

I stripped your program down somewhat by getting rid of all the printf
statements, and then I compiled it with vs. 4.074 and ran it on a
PicDem2-Plus board. It works. I see PWM output on pin B0.
Code:

#include <16F88.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

//==========================================
void main()
 {
byte value;

value = 0;

setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_4, 255, 1);
set_pwm1_duty(0); // PWM off

delay_ms(1000);

while(1)
  {
   value++;
   set_pwm1_duty(value);
   delay_ms(100);
  }

}


--------
Edit: Initialized 'value' to 0.


Last edited by PCM programmer on Wed Aug 26, 2009 7:13 pm; edited 1 time in total
xavier



Joined: 10 Feb 2004
Posts: 7

View user's profile Send private message

PIC16F88 and PWM
PostPosted: Wed Jun 25, 2008 1:38 am     Reply with quote

Many thanks PCM programmer for your help I found my pb ! (you mention RB0 in your post)

It was just because my scope was on RB3 instead of RB0 !!!
I'm so sorry i was sure pwm pin was RB3. (as i said on my first post)

Anyway all is working fine now ( since the beginning in fact ! )

Thanks again
Xavier
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