PWM 10 bits [library]

General discussion on mikroC.
Post Reply
Author
Message
Madman07
Posts: 37
Joined: 06 Sep 2008 18:23
Location: Poland, Rawicz

PWM 10 bits [library]

#1 Post by Madman07 » 29 Nov 2008 17:22

A long ago I've wrote PWM 10 bits library for PIC16F877A. According to this site http://www.micro-examples.com/public/mi ... lator.html PWM is working at 7812.50Hz. The code is for C, but it may be easy converted to other languages. Ussage is simply: in the begginig of program, write PWM_10bit_Init(); Now, you can use PWM1_10bit and PWM2_10bit. Duty_ratio is from 0 to 1023.

Code: Select all

void PWM_10bit_Init() {
     TRISC.F1 = 0;
     TRISC.F2 = 0;
     PORTC.F1 = 0;
     PORTC.F2 = 0;
     PIE1     = 0;
     PIR1     = 0;
     PIE2     = 0;
     PIR2     = 0;
     PR2      = 0xFF;
     T2CON    = 0b00000101;
     CCPR1L   = 0b01111111;
     CCP1CON  = 0b00111100;
     CCPR2L   = 0b01111111;
     CCP2CON  = 0b00111100;
}
void PWM1_10bit(int duty_ratio) {
     CCPR1L     = duty_ratio >> 2;
     CCP1CON.F4 = duty_ratio;
     CCP1CON.F5 = duty_ratio >> 1;
}
void PWM2_10bit(int duty_ratio) {
     CCPR2L     = duty_ratio >> 2;
     CCP2CON.F4 = duty_ratio;
     CCP2CON.F5 = duty_ratio >> 1;
}

marco
Posts: 57
Joined: 03 Jan 2008 20:34

#2 Post by marco » 11 Dec 2009 17:52

hello my friend !

i´m sory, but i´m a beginner at pic !
i don´t understeand how do i change 0 to 100% in 1024 steaps!

í have spend somme days, and i steel dont´get it !

Could any one help me?!

void main()
{
unsigned char dc ;

TRISC = 0 ; // set PORTC as output
PORTC = 0 ; // clear PORTC

PR2 = 0b11111111 ;
T2CON = 0b00000101 ;
CCP1CON = 0b00001100 ;
CCP2CON = 0b0111100 ;
dc= 25; ---> 0 to 256 steaps
// forever
do {

CCPR1L = dc ;
Delay_ms(10) ;

} while(1);
}

Madman07
Posts: 37
Joined: 06 Sep 2008 18:23
Location: Poland, Rawicz

#3 Post by Madman07 » 13 Dec 2009 19:35

It depend what u wanna controll. Maybe just use from 23 to 1023, then 1% = 33, 2% = 43 etc... (for example for LED, or some motor). It will be very fast to calculate for uc. Or just use % * 10,23, but it will give u some errors, for example it will give 10, not 10,23 to output.

And look here, change

[/code]CCP1CON = 0b00001100 ;
CCP2CON = 0b0111100 ;

Code: Select all


for

void PWM1_10bit(int duty_ratio) {
CCPR1L = duty_ratio >> 2;
CCP1CON.F4 = duty_ratio;
CCP1CON.F5 = duty_ratio >> 1;
}

Code: Select all


and then duty_ratio is from 0 to 1023
P.S. Sorry for my bad English :)

Madman07
Posts: 37
Joined: 06 Sep 2008 18:23
Location: Poland, Rawicz

#4 Post by Madman07 » 13 Dec 2009 19:36

It depend what u wanna controll. Maybe just use from 23 to 1023, then 1% = 33, 2% = 43 etc... (for example for LED, or some motor). It will be very fast to calculate for uc. Or just use % * 10,23, but it will give u some errors, for example it will give 10, not 10,23 to output.

And look here, change

Code: Select all

CCP1CON = 0b00001100 ;
CCP2CON = 0b0111100 ; 
for

Code: Select all

void PWM1_10bit(int duty_ratio) {
     CCPR1L     = duty_ratio >> 2;
     CCP1CON.F4 = duty_ratio;
     CCP1CON.F5 = duty_ratio >> 1;
}
and then duty_ratio is from 0 to 1023
P.S. Sorry for my bad English :)

dronology
Posts: 106
Joined: 29 Dec 2007 23:48
Location: istanbul
Contact:

#5 Post by dronology » 22 Dec 2009 11:19

here is the 10bit PWM code that I wrote 2 years ago for 18F8722. You can find information about PWM flags at microchip datasheet of 18f8722 p184-p186. I think it is very easy to rewrite this code for 16F877 as you require.

Code: Select all



/********************************************************
  *	        10-bit PWM code for PIC18F8722 IC         *                
  *                  by Deniz ELMASLI                   *
  * Version 2.2                                         *
  * First Release : 24.10.2008   -   10:12 Thursday     *
  * Last Revision : 25.01.2008   -   12:59 Tuesday      *
  * Ide(s) : MikroC V7.0                                *
  * Target Platform: PIC                                *
  * MCU : 18F8722                                       *
  * Contact: elmasli@gmail.com                          *
  * Web    : www.dronology.com                          *
  *                                                     *
  *     This code in intended to work with PIC IC. Its  *
  * purpose is using MCU's 10-bit PWM property.         *
  * ver 2.2: start function is optimized                *
  *******************************************************

*/

// initialization function
void PWM_10bits_init(){
        PR2 = 0b11111111 ;
        T2CON = 0b00000111 ;
        }
        
        
        
        
// PWM1 port start (it will generate 0V on corresponded pin
void PWM_10bits_start4(){
        CCPR4L = 0b00000000 ;
        CCP4CON = 0b00001100 ;
        }




// main PWM function. This function generates voltage output according to given parametric value
void PWM_10bits_duty4(int val){

       int LSB2bits;
       int MSB8bits;
       
       if(val > 1023)
          val =1023;
       if(val < 0)
          val =0;

          
       LSB2bits = serials(val);


       //check the datasheet.
       if(LSB2bits ==0)
            CCP4CON = 0b00001100;
       else if(LSB2bits ==1)
            CCP4CON = 0b00011100;
       else if(LSB2bits ==2)
            CCP4CON = 0b00101100;
       else if(LSB2bits ==3)
            CCP4CON = 0b00111100;

       //PORTB=LSB2bits ;


       MSB8bits = ((val-LSB2bits) >> 2);
       //PORTD = LSB2bits;


       CCPR4L = MSB8bits ;

  }
R. Giskard Reventlov & R. Daneel Olivaw

http://www.circuitechs.com
http://www.dronology.com

mizort1989
Posts: 11
Joined: 25 Dec 2009 19:28

#6 Post by mizort1989 » 25 Dec 2009 23:20

is the init function is also the start function or what

Madman07
Posts: 37
Joined: 06 Sep 2008 18:23
Location: Poland, Rawicz

#7 Post by Madman07 » 26 Dec 2009 14:51

Yes, in my functions init mean start. But u can stop PWM by clearing registers (i think that, I dont remember that well).
P.S. Sorry for my bad English :)

mizort1989
Posts: 11
Joined: 25 Dec 2009 19:28

#8 Post by mizort1989 » 28 Dec 2009 11:06

thanks for this great library

Post Reply

Return to “mikroC General”