Pulse Width Modulation PWM using 16f84a and H-bridge

Thread Starter

hbyte

Joined Aug 18, 2012
4
I have a H-bridge that I wish to use to change the direction of a dc motor - works well using 16f84a to switch between pins connected to the h-bridge running at full speed without using PWM.

The problem I have is when I implement PWM using a function I can no longer switch from one pin to the other. Here is the source code which compiles using Hi-Tech C. The result is that the comparitor is not realised
and the state is not changed - only 1 pin remains active throughout using PWM. How can I acheive this using this MCU?

Rich (BB code):
#include <htc.h>
#include <stdlib.h>
#include <stdio.h>
#include <pic16f84a.h>

unsigned int i;

#define __16F84A

    __CONFIG(CP_OFF & WDTE_OFF & PWRTE_ON & FOSC_XT);

/*PWM function*/    
void _pwm(unsigned int state)
{

unsigned int i;

for(i=0;i<1000;i++){

if(i>700){
if(state==1){
PORTB = 0b00010000;
}
else{
PORTB = 0b00001000;
}  
}else{
PORTB=0;
}

}

        }







void main(void) {
  /* set both ports as outputs */ 
  TRISA = TRISB =0b00000000;   //0;

  /* start with 0V on all output pins/ports */
  PORTA = 0;
  PORTB = 0;

unsigned int state,t,i;

  /* an infinite loop; for ever run the following code */
  while(1) {
    
  t++;

if(t>400000){         //why isnt this realised ?
t=0;
if(state==1){state=2;}else{state=1;}


 
}
    
_pwm(state);
     }



}
 
Last edited by a moderator:

Thread Starter

hbyte

Joined Aug 18, 2012
4
Ok thanks for that , the maximum size I presume to be below the comparitor value. What can I use instead - will "int" suffice instead of "unsigned int" .

Your help is appreciated.
 

BMorse

Joined Sep 26, 2009
2,675
t is an unsigned int. What is the largest value an unsigned int can hold?
Unsigned Int max value is 65535 (16 Bits)

the Value he is using to compare is 19 bits (400000)

so 't' will just keep rolling over once 65535 is reached and will never reach 400000......
 

Thread Starter

hbyte

Joined Aug 18, 2012
4
Neither unsigned long or unsigned short int works, initialised t=0 (thanks) and
used ++t instead of t++.

None of these changes the state/pins.

The comparitor just doesnt work regardless of what type of int I am using.
This seem's so trivial but I cant see the answer!
 

BMorse

Joined Sep 26, 2009
2,675
if(state==1){
PORTB = 0b00010000;
}
else{
PORTB = 0b00001000;
}
}else{
PORTB=0;
}
could be your problem right here, you have 2 else statements, the last statement will always assign 0 to PORTB if state is not = to 1.

Remove the last else and see what happens....
 

Thread Starter

hbyte

Joined Aug 18, 2012
4
If you can try testing it yourself. This part works ok it needs the second else to satisfy the pwm :-

psuedo code:

for (i=0;i<total;i++)
if i<value
then
portb = 0;

the duty cycle would be value/total * 100

therefore

value ~ motor speed

If you start with state=2 then the motor turns in the opposite direction and stays there at a fixed speed dictated by the pwm the opposite direction for state=1.

It is definately not realising the 2nd comparitor. It should change direction every 400000 cycles.
 
Top