Jump to content
Lucian

Pwm Funtions For Pic12f615

Recommended Posts

Are there functions in Sourceboost C Compiler for PWM?

 

I think we don't have. You can make your own function. I have a sample code made long back. see if it can help u.

Its made for PIC16F72, 1mhz RC clock.

#include <system.h>

// Set clock frequency to 1MHz.

#pragma CLOCK_FREQ 1000000

//set configuration fuse.
#pragma DATA _CONFIG, _RC_OSC & _WDT_OFF & _CP_OFF & _PWRTE_OFF

int duty_cycle;	//global variable

/*Interrupt service routine (ISR).On timer2 interrupt, program 
 will jump to this code location.  */	
void interrupt( void )
{
char pulse;
/*generate two 50hz 180 degree out of phase pulses & externally
AND it with the 100Hz pwm pin to get a modulated 50Hz to drive
mosfets connected to a transformer in a push-pull configuration.
A/D conversion of the the output voltage can act as feed back to
adjust the duty-cycle ( not yet implemented in this code).
*/
pulse = portb;  
pulse.0 = ~ pulse.0;
pulse.1 = ~ pulse.1;  
portb = pulse;

pir1.1=0;  //clear TMR2 to pr2 match interrupt flag
}

void load_duty_cycle(void)   
{	
ccpr1l.7 = duty_cycle.9;  
ccpr1l.6 = duty_cycle.8;		   
ccpr1l.5 = duty_cycle.7;  
ccpr1l.4 = duty_cycle.6;  
ccpr1l.3 = duty_cycle.5; 
ccpr1l.2 = duty_cycle.4;  
ccpr1l.1 = duty_cycle.3;  
ccpr1l.0 = duty_cycle.2;  
ccp1con.5 = duty_cycle.1;  
ccp1con.4 = duty_cycle.0; 
}



// The main code configures the pwm registers
void main()
{
trisa = 255;
  	trisb = 0;		 //configure port B as output
  	trisc = 0;	   // portc as out put
portb = 1;		 //set port B to 1
portc = 0;	   //clear portc



// enable interrupts
intcon.6=1;	 //enable peripheral interrupt	 
intcon.7=1;	 //enable global interrupt

pie1.1=1;	   //tmr2 to pr2 match interrupt

//configure the ccp1con register to enable PWM module.

/*ccpxm3:ccpxm0: mode select bits<3:0>
11xx = for PWM mode.  
*/
ccp1con=0b00001100; //


//PWM period=[(PR2)+1]*4*Tosc*(TMR2 prescaler value)

/*our pmw period is 10ms(100Hz),Tosc is 1/1000,000Hz & prescaler 
is 16.For which PR2+1 works out to 156.25. Lets round it to 156 &
make PR2 to 155. 
*/

//load the period value into pr2 register.
pr2 = 155;


/*timer2 control register
we have selected
bit6-3...(0000) 1:1 for post scaler
bit2.....(1) timer2 on
bit1-0...(11) prescaler is 16
*/

t2con = 0b00000111;  

/*PWM duty cycle = (ccpr1l:ccp1con<5:4>)*Tosc*(TMR2 prescaler value)
Here i have taken 2ms as an example value for duty cycle, Tosc & pre-
scaler are same as above.We will get 10 bit values when we work with
larger duty cycle but for our example we get the value as 125.
*/

//load the duty cycle values 


while( 1 )
  {
   int b0;
   for(b0=1;b0<563;b0++)
  {
	 duty_cycle = b0;
	 load_duty_cycle();
	 delay_ms(9);
   }

  }
}

 

 

Regards

Raghunathan

Share this post


Link to post
Share on other sites
Are there functions in Sourceboost C Compiler for PWM?

 

I think we don't have. You can make your own function. I have a sample code made long back. see if it can help u.

Its made for PIC16F72, 1mhz RC clock.

#include <system.h>

// Set clock frequency to 1MHz.

#pragma CLOCK_FREQ 1000000

//set configuration fuse.
#pragma DATA _CONFIG, _RC_OSC & _WDT_OFF & _CP_OFF & _PWRTE_OFF

int duty_cycle;	//global variable

/*Interrupt service routine (ISR).On timer2 interrupt, program 
 will jump to this code location.  */	
void interrupt( void )
{
char pulse;
/*generate two 50hz 180 degree out of phase pulses & externally
AND it with the 100Hz pwm pin to get a modulated 50Hz to drive
mosfets connected to a transformer in a push-pull configuration.
A/D conversion of the the output voltage can act as feed back to
adjust the duty-cycle ( not yet implemented in this code).
*/
pulse = portb;  
pulse.0 = ~ pulse.0;
pulse.1 = ~ pulse.1;  
portb = pulse;

pir1.1=0;  //clear TMR2 to pr2 match interrupt flag
}

void load_duty_cycle(void)   
{	
ccpr1l.7 = duty_cycle.9;  
ccpr1l.6 = duty_cycle.8;		   
ccpr1l.5 = duty_cycle.7;  
ccpr1l.4 = duty_cycle.6;  
ccpr1l.3 = duty_cycle.5; 
ccpr1l.2 = duty_cycle.4;  
ccpr1l.1 = duty_cycle.3;  
ccpr1l.0 = duty_cycle.2;  
ccp1con.5 = duty_cycle.1;  
ccp1con.4 = duty_cycle.0; 
}



// The main code configures the pwm registers
void main()
{
trisa = 255;
  	trisb = 0;		 //configure port B as output
  	trisc = 0;	   // portc as out put
portb = 1;		 //set port B to 1
portc = 0;	   //clear portc



// enable interrupts
intcon.6=1;	 //enable peripheral interrupt	 
intcon.7=1;	 //enable global interrupt

pie1.1=1;	   //tmr2 to pr2 match interrupt

//configure the ccp1con register to enable PWM module.

/*ccpxm3:ccpxm0: mode select bits<3:0>
11xx = for PWM mode.  
*/
ccp1con=0b00001100; //


//PWM period=[(PR2)+1]*4*Tosc*(TMR2 prescaler value)

/*our pmw period is 10ms(100Hz),Tosc is 1/1000,000Hz & prescaler 
is 16.For which PR2+1 works out to 156.25. Lets round it to 156 &
make PR2 to 155. 
*/

//load the period value into pr2 register.
pr2 = 155;


/*timer2 control register
we have selected
bit6-3...(0000) 1:1 for post scaler
bit2.....(1) timer2 on
bit1-0...(11) prescaler is 16
*/

t2con = 0b00000111;  

/*PWM duty cycle = (ccpr1l:ccp1con<5:4>)*Tosc*(TMR2 prescaler value)
Here i have taken 2ms as an example value for duty cycle, Tosc & pre-
scaler are same as above.We will get 10 bit values when we work with
larger duty cycle but for our example we get the value as 125.
*/

//load the duty cycle values 


while( 1 )
  {
   int b0;
   for(b0=1;b0<563;b0++)
  {
	 duty_cycle = b0;
	 load_duty_cycle();
	 delay_ms(9);
   }

  }
}

 

 

Regards

Raghunathan

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×