Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Why is this PWM not working? (mikroC code)

Status
Not open for further replies.
Joined
Jul 25, 2012
Messages
1,192
Helped
171
Reputation
342
Reaction score
162
Trophy points
1,343
Activity points
0
Why this pwm is not working? Nothing appears on the oscilloscope.

mikroC Code

Code:
#define SW PORTA.F0

void main() {

     int speed_counter;
     TRISA = 0xFF;
     TRISC = 0x00;
     PORTA = 0x00;
     PORTC = 0x00;


     PWM1_Init(33000);
     PWM1_Start();
     speed_counter = 0;
     
     while(1) {
    
          if(SW == 1) {
                Delay_ms(20);
                if(SW == 1) {
                   speed_counter = speed_counter + 1;
                }
          }
     
          if(speed_counter > 4) {
               speed_counter = 0;
          }
     
          switch (speed_counter) {
                 case 0: {
                      PWM1_Set_Duty(0);
                      break;
                 }
          
                 case 1: {
                      PWM1_Set_Duty(64);
                      break;
                 }
          
                 case 2: {
                      PWM1_Set_Duty(127);
                      break;
                 }
          
                 case 3: {
                      PWM1_Set_Duty(192);
                      break;
                 }
          
                 case 4: {
                      PWM1_Set_Duty(255);
                      break;
                 }
          
                 default:
                         ;
          
          }
     }
}
 

Attachments

  • PIC16F877_PWM.rar
    17.4 KB · Views: 83

re: Why this is not working?

Why this pwm is not working? Nothing appears on the oscilloscope.

mikroC Code

Code:
#define SW PORTA.F0

void main() {

     int speed_counter;
     TRISA = 0xFF;
     TRISC = 0x00;
     PORTA = 0x00;
     PORTC = 0x00;


     PWM1_Init(33000);
     PWM1_Start();
     speed_counter = 0;
     
     while(1) {
    
          if(SW == 1) {
                Delay_ms(20);
                if(SW == 1) {
                   speed_counter = speed_counter + 1;
                }
          }
     
          if(speed_counter > 4) {
               speed_counter = 0;
          }
     
          switch (speed_counter) {
                 case 0: {
                      PWM1_Set_Duty(0);
                      break;
                 }
          
                 case 1: {
                      PWM1_Set_Duty(64);
                      break;
                 }
          
                 case 2: {
                      PWM1_Set_Duty(127);
                      break;
                 }
          
                 case 3: {
                      PWM1_Set_Duty(192);
                      break;
                 }
          
                 case 4: {
                      PWM1_Set_Duty(255);
                      break;
                 }
          
                 default:
                         ;
          
          }
     }
}

Could you please provide the rest of the pertinent source code so that I can see your initialization functions. While the code above looks fine from what I could tell, there might be something that is not being initialized correctly.

Also, would it be possible for you to provide a .pdf of your schematic. Unfortunately I do not have a copy of OrCAD installed on my machine.

Regards,
Willis
 

re: Why this is not working?

There is no Initialization. I have posted the full code. All PWM things are done by the PWM library functions. That is a proteus .dsn file.
 

Attachments

  • pic16f877_pwm.PDF
    23.4 KB · Views: 99

re: Why this is not working?

you must delete the last flower bracket. which is extra. and you must power on the microcontroller, thus, attach a logic state to the pin^1(Vpp) of pic microcontroller. and make it logic high.

Code:
#define SW PORTA.F0

void main() {

     int speed_counter;
     TRISA = 0xFF;
     TRISC = 0x00;
     PORTA = 0x00;
     PORTC = 0x00;


     PWM1_Init(33000);
     PWM1_Start();
     speed_counter = 0;

     while(1) 
          {

          if(SW == 1) 
                {
                   Delay_ms(20);
                   speed_counter = speed_counter + 1;
                }
          }

          if(speed_counter > 4) {
               speed_counter = 0;
          }

          switch (speed_counter) {
                 case 0: {
                      PWM1_Set_Duty(0);
                      break;
                 }

                 case 1: {
                      PWM1_Set_Duty(64);
                      break;
                 }

                 case 2: {
                      PWM1_Set_Duty(127);
                      break;
                 }

                 case 3: {
                      PWM1_Set_Duty(192);
                      break;
                 }

                 case 4: {
                      PWM1_Set_Duty(255);
                      break;
                 }

                 default:
                         ;

          }
     }
 

re: Why this is not working?

Yes. I did that too, but I don't get the pulses.
 

re: Why this is not working?

ok, what is your aim to do with that?
be specific in your question. what percentage of pulses do you need exactly?
 

Re: Why this is not working???????

I need 25%, 50% and 75% PWM Pulses. It is used to control the speed of motor. But in simulation the motor is running on its own without button being pressed.

I did the same circuit with PIC16F887 and it is working fine.
 

Attachments

  • PIC16F887_PWM.rar
    18.6 KB · Views: 75
Last edited:

re: Why this is not working?

Hi friend

Check alternative function over Port A pin which is you had to use as a input.
There is one possibility, that you initialize speed_counter = 0; and if your input pin not working, your switch-case logic select case 0 where PWM duty cycle is 0. So you don't get any outpur.
So check your input port pin as well as for testing purpose initialize speed_counter = 1 or 2 or 3;

Best of luck.
 

Re: Why this is not working?

OK. Thanks It is working fine. The problem was with the switch.
 

Attachments

  • PIC16F877_PWM.rar
    17.8 KB · Views: 82

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top