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

Hardware PWM "Pin RB3"

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



Joined: 02 Aug 2010
Posts: 30

View user's profile Send private message

Hardware PWM "Pin RB3"
PostPosted: Tue Jan 17, 2012 11:45 am     Reply with quote

Hello,

I am using CCS version 4.106 and I would like to set a PWM output at pin RB3 on PIC16F727. After I followed the instruction to setup the pwm on The PIC datasheet under section 15.3.8 then setup the APFCON register to steer the pwm to pin RB3 I wasn't able to see any pwm on that pin.
The same code worked for RC2 (PWM1)
Any help will be appreciated. Here is the code:

Code:


//---------------------------------------------------
//function prototypes:
//---------------------------------------------------
void make_portb_pins_digital(void);
void init_pwm2(void);
void pwm2(int16 a2d);
//---------------------------------------------------
// Main
//---------------------------------------------------
void main()
{
   //MAKE RB3 DIGITAL OUTPUT
   make_portb_pins_digital();
   //INITIALISE PWM2
   init_pwm2();
   //SEND PWM ON RB3
   pwm2(50);
   while(1)
   {
   //DO NOTHING
   }
}
void make_portb_pins_digital(void)
{
   //GET THE BYTE ADDRESS OF PORTA AND B
   #byte ANSELB = getenv("SFR:ANSELB")
   #bit ANSELB_3=ANSELB.3
   //make selected pins on port B digital
   //------------------------------------
   //MAKE PIN RB3 DIGITAL :PWM OUTPUT
   ANSELB_3=0;
}

void init_pwm2(void)
{
   //Assign pwm2 to RB3
   //Disable the PWM pin (CCPx) output driver(s) by
   //setting the associated TRIS bit(s).
   TRISIOB_3=1;

   //Load the PR2 register with the PWM period value.
   PR2=40;              // 3000hz

   //Configure the CCP module for the PWM mode
   //by loading the CCPxCON register with the
   //appropriate values.
   CCP2CON=0b00001100;   // Set CCP2 PWM active high

   //Load the CCPRxL register and the DCxBx bits of
   //the CCPxCON register, with the PWM duty cycle
   //value.
   duty = 0;               // set duty cycle to 0
   CCP2CON_4 = duty_0;     // Store duty to registers as
   CCP2CON_5 = duty_1;     // a 10-bit word

   //store the eight MSbs in CCPR1L.
   CCPR2L = duty >> 2;     // store the remaining low byte

   //clear the TMR21F interrupt flag bit of PIR1 register
   PIR1_1=0;
   //Configure the T2CKPS bits of the T2CON
   //register with the Timer2 prescale value.
   //and
   //Enable Timer2 by setting the TMR2ON bit of
   //the T2CON register.
   T2CON=0b00000111;

   //Wait until Timer2 overflows, TMR2IF bit of the
   //PIR1 register is set.
   wait_for_TMR2IF:       
   if (PIR1_1 == 0)
     {
     goto wait_for_TMR2IF;
     }

   //enable PWM by setting pin to output
   TRISIOB_3=0;

   //Assign pwm2 to RB3
   APFCON_0=1;
}

void pwm2(int16 a2d)
{
   duty=a2d;
   CCP2CON_4 = duty_0;    // Store duty to registers as
   CCP2CON_5 = duty_1;    // a 10-bit word
   CCPR2L = duty >> 2;    // store the remaining low byte}
}



Last edited by kbruin79 on Tue Jan 17, 2012 12:15 pm; edited 2 times in total
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Jan 17, 2012 12:01 pm     Reply with quote

Well you could make your life and anyone trying to read your program a LOT easier by using the CCS builtin functions !!

I got 'lost' about 1/3 down and gave up. Too much reading for such a simple program.
kbruin79



Joined: 02 Aug 2010
Posts: 30

View user's profile Send private message

PostPosted: Tue Jan 17, 2012 12:19 pm     Reply with quote

temtronic wrote:
Well you could make your life and anyone trying to read your program a LOT easier by using the CCS built-in functions !!

I got 'lost' about 1/3 down and gave up. Too much reading for such a simple program.


I Simplified as much as possible, assume all constants, variables are defined. I will eventually use the built-in function but I would like to know why the code isn't working.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Jan 17, 2012 5:16 pm     Reply with quote

you SAY you are using CCS but you are not-- it looks more like the structure of one of the crummy register oriented C's like HT or S-boost or Mikro...

for example, this mess for init of pwm2 mode and duty cycle :

//Assign pwm2 to RB3
APFCON_0=1;

COULD be WAY better handled by a #FUSE STATEMENT for the part in question --
----
then this approximate reduction

Code:

void init_pwm2(void) {

  // do the proper  setup_timer2(...); FIRST 
   set_pwm2_duty(0);  // i do this BEFORE the setup enable
  setup_ccp2(ccp_PWM);
 // DONE   

}

THEN THIS!!!
void pwm2(int16 a2d)

is really the same as the CCS function
set_pwm2_duty();



this looks like falling back on assembler in HiTECH C - or one of the other really poor C for PIC compilers - this register defined - manipulation is NOT the CCS way at all, IHMO.

why not use the powerful C constructs CCS offers ??

perhaps you will get results if you READ the CCS manual
and try it their way next Very Happy Very Happy Arrow
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