PWM from PIC16F877A....

Discussion in 'Embedded Systems and Microcontrollers' started by RRITESH KAKKAR, Apr 18, 2013.

  1. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    HI,

    I have written this code for PWM generation for DC motor Speed control...
    need to be check as per data sheet..!!


    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    3. #define _XTAL_FREQ 20000000
    4.  
    5. main(){
    6. TRISC=0x00;// out put for PWM
    7. CCP1CON=0b11111000; //pwm mode
    8. PR2=0xff;//for 19.9khz freq
    9. CCPR1L=0x0f;/duty cycle random
    10. T2CON=0b01100000;//pescallaer to 1:4
    11. CCP1IE=0;//disable interput
    12.  
     
    Meixner likes this.
  2. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi again,

    I was testing code in oshon but port c output waveform remain at zero, why??

    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    4. #define _XTAL_FREQ 20000000
    5.   main(){   TRISC=0x00;         // out put for PWM
    6. CCP1CON=12;         //pwm mode
    7.  PR2=55;                //for freq
    8. while(1){
    9.  CCPR1L=0x02;            //duty cycle random
    10.  T2CON=0b01100000;        //pescallaer to 1:1
    11.  CCP1IE=0;        //disable interput  
    12. }
    13. }
     
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You need to turn timer 2 on..... go read the datasheet to see how...
     
  4. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi again,

    The error was confused with bits.....
    it is working

    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    3.  #define _XTAL_FREQ 20000000    
    4. void main()      
    5.   {         unsigned char   dc ;  
    6.          TRISC = 0 ;                     // set PORTC as output
    7.         PORTC = 0 ;                     // clear PORTC           /*          * configure CCP module as 4000 Hz PWM output          */         PR2 = 0b11111001 ;    
    8.      T2CON = 0b00000100 ;      
    9.    CCP1CON = 0b00111100;    
    10.    while(1)                       // forever                 {                 /*                  * PWM resolution is 10 bits                  * don't use last 2 less significant bits CCPxCON,                  * so only CCPRxL have to be touched to change duty cycle                  */                
    11.  for(dc = 0 ; dc < 255 ; dc++)                         {                         CCPR1L = dc ;                           __delay_ms(100) ;                         }                   }         }


    ADC result to PWM at 20KHZ

    Code ( (Unknown Language)):
    1.  
    2.  
    3.  
    4.   #include <htc.h>
    5. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    6.  #define _XTAL_FREQ 20000000  
    7.   void main()  
    8.       {     TRISB=0X00; TRISA = 0xff ;
    9. ADCON1=0b00000000;
    10.  ADCON0=0b10000001;//000 = channel 0, (RA0/AN0)    
    11.  ADIF=0;    
    12.  ADIE=1;  
    13.    PEIE=1;
    14.  TRISC = 0 ;  
    15.       PR2 = 0b11111001 ;
    16.    T2CON = 0b00000100 ;
    17.     CCP1CON = 0b00111100;                
    18.           PORTC = 0 ;  
    19.  while(1){              
    20.   __delay_us(10);
    21.  GO_DONE=1;    
    22.  __delay_us(10);                        CCPR1L = ADRESH ;               }}
     
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Great, glad to hear it!
     
  6. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi,

    for wireless transfer of adc result to another uC with UART...

    will this work just for test after that i will modifies it
    for Tx
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3.  __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    4.  #define _XTAL_FREQ 20000000  
    5.  void  HSerinit();  
    6.  void main(void)                        // program entry
    7.     {      
    8. int index = 0;  
    9.   unsigned char ch;
    10.     ADCON1 = 0x6;                    // Analogue off      
    11.                    HSerinit();  
    12.      __delay_ms(150);  
    13.    while(1)                        // endless Loop    
    14.     {              while(!TXIF);                    // Wait for module to finish  
    15.        TXREG = 'A';            
    16.   }  
    17.   }
    18. void HSerinit() {  
    19.   TRISC = 0x80;                    // TX was an input!    
    20.  SPBRG = 129;                    // 20Mhz xtal 9600 BAUD    
    21.  TXSTA = 0x24;                    // TXEN and BRGH    
    22.  RCSTA = 0x90;                    // SPEN and CREN }
    FOR rX
    Code ( (Unknown Language)):
    1.  
    2.  
    3. #include <htc.h>
    4. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    5.  #define _XTAL_FREQ 20000000    
    6.  unsigned char  HSerin(void);
    7.  void HSerout(unsigned char ch), HSerinit(void);  
    8. void main(void)                       // program entry
    9.     {       TRISB=0X00;    
    10. PORTB=0X00;    
    11. int index = 0;  
    12.    unsigned char ch;  
    13.   ADCON1 = 0x6;                    // Analogue off
    14.                          HSerinit();    
    15.    __delay_ms(150);  
    16.    while(1)                        // endless Loop
    17.         {        
    18.   if(RCREG=='A'){     RB7=1;}                //FOR TESTING RECEIVED DATA....!!  
    19.         }  
    20.   }
    21.   void HSerinit()
    22.     {     TRISC = 0xC0;                    // should ideally be set    
    23.  SPBRG = 129;                    // 20Mhz xtal 9600 BAUD  
    24.   TXSTA = 0x24;                    // TXEN and BRGH  
    25.   RCSTA = 0x90;                    // SPEN and CREN     }
     
  7. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    new code for wireless transfer of adc result to pwm...


    for Tx....

    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    4. #define _XTAL_FREQ 20000000
    5.  
    6. void  HSerinit();
    7.  
    8. void main(void)                        
    9.     {
    10.  
    11. TRISA = 0xff ;
    12. ADCON1=0b00000000;
    13. ADCON0=0b10000001;//000 = channel 0, (RA0/AN0)
    14.     ADIF=0;
    15.     ADIE=1;
    16.     PEIE=1;                                  
    17.     HSerinit();
    18.        __delay_ms(150);
    19.     while(1)                
    20.         {
    21.  
    22.     __delay_ms(10);
    23.     GO_DONE=1;
    24.     __delay_ms(10);
    25.        
    26.          while(!TXIF);                
    27.          TXREG = ADRESH;              
    28.  
    29.         }
    30.     }
    31. void HSerinit()
    32. {
    33.     TRISC = 0x80;                    // TX was an input!
    34.     SPBRG = 129;                    // 20Mhz xtal 9600 BAUD
    35.     TXSTA = 0x24;                    // TXEN and BRGH
    36.     RCSTA = 0x90;                    // SPEN and CREN
    37. }



    for rx ADC result to PWM


    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    3. #define _XTAL_FREQ 20000000
    4.  
    5. void  HSerinit();
    6.  
    7. void main()                        
    8.     {
    9.  
    10.  
    11.     PR2 = 0b11111001 ;
    12.     T2CON = 0b00000100 ;  
    13.     CCP1CON = 0b00111100;                  
    14.   ADCON1 = 0x6;  
    15.  HSerinit();
    16.  __delay_ms(150);
    17.     while(1)
    18.         {
    19.      while(!RCIF);                    // Wait for a character
    20.         CCPR1L= RCREG;
    21.  
    22. }
    23.     }
    24.  
    25.  
    26.  
    27.  
    28.  
    29.  
    30.  
    31.  
    32.  
    33.  
    34.  
    35.  
    36.  
    37.  
    38.  
    39. void HSerinit()
    40. {
    41.     TRISC = 0x80;                    // TX was an input!
    42.     SPBRG = 129;                    // 20Mhz xtal 9600 BAUD
    43.     TXSTA = 0x24;                    // TXEN and BRGH
    44.     RCSTA = 0x90;                    // SPEN and CREN
    45. }
     
  8. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Let's stick with the serial thread for now. This thread has accomplished its goal...
     
  9. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    At least can you check is there any problem, will it will work?
     
Loading...