[SOLVED] Using PWM in 16f877A with adc

Status
Not open for further replies.

arnab913

Junior Member level 3
Joined
Jul 27, 2012
Messages
30
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Chittagong, Bangladesh
Activity points
1,515
Hello,
I am working with the 16f877A,using PWM with ADC. I've writtten a code,but it is not working.....I am cheaking it again & again..but nothing. :-(
The motor works without the ADC value...wheather it is 5 or 0V.
I don't understand why???
Here is the code....
Code:
double pwm;
void main() {
   PWM1_Init(1000);              // PWM of 1kHz
  ADCON1=101000010;
  TRISA=0b00000001;
  PORTA=0;
   TRISB=0b00000010;             // portb as output
   PORTB=0;
   PWM1_Start();         // starting PWM
   if (PORTB.f1==1)
    {
 while(1)
  {

    pwm=ADC_Read(0);
   PWM1_Set_Duty(pwm);

            }
   delay_ms(100);
     }
}
Oscilloscope is not also showing PWM! Pls help.....
Here the image
 

Attachments

  • csg.png
    39.7 KB · Views: 83

Try this code and see if it works.

Code:
double pwm;

void main() {
   PWM1_Init(2000);                     // PWM of 1kHz
   TRISA = 0x01;
   PORTA = 0x00;
   TRISB = 0x02;                  // portb as output
   PORTB = 0x00;
   ADCON1 = 0b101001110;
   CMCON = 0x07;

   PWM1_Start();                        // starting PWM

   while(1) {
      if(PORTB.f1 == 1)
      {
         pwm =  ADC_Read(0);
         PWM1_Set_Duty(pwm);
         delay_ms(100);
      }
   }
}

You are not assigning any value to RB0. How can it control anything?

Check this. http://www.robotroom.com/PWM5.html
 
Last edited:

Thanx for reply,but it is still not working......not understanding why,is there any problem in the schematic??? :-(
 

zip and post your shcematic. It is working. Check this code.

Code:
double pwm;

void main() {
   PWM1_Init(1000);              // PWM of 1kHz
   TRISA = 0b00000001;
   PORTA = 0x00;
   TRISB = 0b00000010;             // portb as output
   PORTB = 0x00;
   ADCON1 = 0b101001110;
   CMCON = 0x07;

   PWM1_Start();                  // starting PWM
   
   while(1) {
      if(PORTB.F1 == 1)
      {
         pwm =  ADC_Read(0);
         PWM1_Set_Duty(pwm);
         delay_ms(100);
      }
   }
}

Just Connect RC2/CCP1 pin to the transistor.
 

Attachments

  • arnab913.rar
    18.7 KB · Views: 66
  • ss14.jpg
    202.9 KB · Views: 75
Last edited:
Thanx to internetuser2k12
Yes!! it worked!!
I made mistake connecting the transistor pin....
But I couldn't understand ---"CMCON = 0x07;"
what it means? and what is the use of the diode across the motor?
Thanx in advance...
 

CMCON = 0x07 means the Comparator is disabled. Diode accross the motor is to bypass back emf induced while switching on and off the motor.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…