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?
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: