PWM from PIC16F877A....

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
HI,

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


Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000

main(){
TRISC=0x00;// out put for PWM
CCP1CON=0b11111000; //pwm mode
PR2=0xff;//for 19.9khz freq
CCPR1L=0x0f;/duty cycle random
T2CON=0b01100000;//pescallaer to 1:4
CCP1IE=0;//disable interput
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hi again,

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

Rich (BB code):
#include <htc.h> 
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS); 
#define _XTAL_FREQ 20000000 
  main(){   TRISC=0x00;         // out put for PWM 
CCP1CON=12;         //pwm mode
 PR2=55;                //for freq 
while(1){
 CCPR1L=0x02;            //duty cycle random
 T2CON=0b01100000;        //pescallaer to 1:1
 CCP1IE=0;        //disable interput   
} 
}
 

tshuck

Joined Oct 18, 2012
3,534
Hi again,

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

Rich (BB code):
#include <htc.h> 
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS); 
#define _XTAL_FREQ 20000000 
  main(){   TRISC=0x00;         // out put for PWM 
CCP1CON=12;         //pwm mode
 PR2=55;                //for freq 
while(1){
 CCPR1L=0x02;            //duty cycle random
 T2CON=0b01100000;        //pescallaer to 1:1
 CCP1IE=0;        //disable interput   
} 
}
You need to turn timer 2 on..... go read the datasheet to see how...
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hi again,

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

Rich (BB code):
#include <htc.h> 
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
 #define _XTAL_FREQ 20000000     
void main()       
  {         unsigned char   dc ;  
         TRISC = 0 ;                     // set PORTC as output 
        PORTC = 0 ;                     // clear PORTC           /*          * configure CCP module as 4000 Hz PWM output          */         PR2 = 0b11111001 ;    
     T2CON = 0b00000100 ;      
   CCP1CON = 0b00111100;     
   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                  */                
 for(dc = 0 ; dc < 255 ; dc++)                         {                         CCPR1L = dc ;                           __delay_ms(100) ;                         }                   }         }


ADC result to PWM at 20KHZ

Rich (BB code):
  #include <htc.h> 
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
 #define _XTAL_FREQ 20000000   
  void main()   
      {     TRISB=0X00; TRISA = 0xff ; 
ADCON1=0b00000000;
 ADCON0=0b10000001;//000 = channel 0, (RA0/AN0)    
 ADIF=0;    
 ADIE=1;  
   PEIE=1; 
 TRISC = 0 ;   
      PR2 = 0b11111001 ; 
   T2CON = 0b00000100 ; 
    CCP1CON = 0b00111100;                 
          PORTC = 0 ;  
 while(1){              
  __delay_us(10);
 GO_DONE=1;    
 __delay_us(10);                        CCPR1L = ADRESH ;               }}
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
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
Rich (BB code):
#include <htc.h>
 __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
 #define _XTAL_FREQ 20000000  
 void  HSerinit();  
 void main(void)                        // program entry 
    {       
int index = 0;   
  unsigned char ch; 
    ADCON1 = 0x6;                    // Analogue off      
                   HSerinit();   
     __delay_ms(150);  
   while(1)                        // endless Loop     
    {              while(!TXIF);                    // Wait for module to finish   
       TXREG = 'A';             
  }   
  } 
void HSerinit() {   
  TRISC = 0x80;                    // TX was an input!    
 SPBRG = 129;                    // 20Mhz xtal 9600 BAUD    
 TXSTA = 0x24;                    // TXEN and BRGH    
 RCSTA = 0x90;                    // SPEN and CREN }
FOR rX
Rich (BB code):
#include <htc.h> 
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
 #define _XTAL_FREQ 20000000    
 unsigned char  HSerin(void);
 void HSerout(unsigned char ch), HSerinit(void);   
void main(void)                       // program entry 
    {       TRISB=0X00;     
PORTB=0X00;     
int index = 0;  
   unsigned char ch;   
  ADCON1 = 0x6;                    // Analogue off
                         HSerinit();     
   __delay_ms(150);  
   while(1)                        // endless Loop 
        {         
  if(RCREG=='A'){     RB7=1;}                //FOR TESTING RECEIVED DATA....!!   
        }   
  } 
  void HSerinit() 
    {     TRISC = 0xC0;                    // should ideally be set    
 SPBRG = 129;                    // 20Mhz xtal 9600 BAUD   
  TXSTA = 0x24;                    // TXEN and BRGH   
  RCSTA = 0x90;                    // SPEN and CREN     }
 

Thread Starter

RRITESH KAKKAR

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


for Tx....

Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
 
void  HSerinit();
 
void main(void)                        
    {

TRISA = 0xff ;
ADCON1=0b00000000;
ADCON0=0b10000001;//000 = channel 0, (RA0/AN0)
    ADIF=0;
    ADIE=1;
    PEIE=1;                                   
    HSerinit();
       __delay_ms(150);
    while(1)                
        {

    __delay_ms(10);
    GO_DONE=1;
    __delay_ms(10);
        
         while(!TXIF);                 
         TXREG = ADRESH;              
 
        }
    }
void HSerinit()
{
    TRISC = 0x80;                    // TX was an input!
    SPBRG = 129;                    // 20Mhz xtal 9600 BAUD
    TXSTA = 0x24;                    // TXEN and BRGH
    RCSTA = 0x90;                    // SPEN and CREN
}



for rx ADC result to PWM


Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
 
void  HSerinit();
 
void main()                        
    {


    PR2 = 0b11111001 ;
    T2CON = 0b00000100 ;   
    CCP1CON = 0b00111100;                  
  ADCON1 = 0x6;  
 HSerinit();
 __delay_ms(150);
    while(1)
        {
     while(!RCIF);                    // Wait for a character
        CCPR1L= RCREG; 

}
    }















void HSerinit()
{
    TRISC = 0x80;                    // TX was an input!
    SPBRG = 129;                    // 20Mhz xtal 9600 BAUD
    TXSTA = 0x24;                    // TXEN and BRGH
    RCSTA = 0x90;                    // SPEN and CREN
}
 
Top