Quero fazer um girar um motor DC de 12V no sentido horário e anti-horário controlando a sua velocidade no PIC18F2331.
Estou configurando o RB0 e RB1 para acionar uma ponte H.
Se apenas coloco, alternadamente, os pinos RB0 ou RB1 em nivel alto colocando o oposto em nível baixo, já aciono o motor no sentido horário e anti-horário. Ou seja, o harware funciona. O problema é fazer o mesmo com o PWM.
Abaixo o código no C18 que usando. Onde estou errando?
O cristal é de 4MHz
OBS: Uso uma rotina de PISCA_LED só para informar que a programa está rodando.
- Código: Selecionar todos
/* Compile options: -ml (Large code model) */
#include <p18f2331.h>
#include <stdio.h>
#include <delays.h>
#include <portb.h>
#include <timers.h>
#include <pcpwm.h>
#pragma config WDTEN = OFF
#pragma config OSC = XT
#pragma config LVP = OFF
#pragma config PWMPIN = OFF
/* LED is in RB4 */
#define LED01 0x10 // 00010000
#define RB0_ON 0x05 // 00000101
#define RB1_ON 0x06 // 00000110
void piscaLED(int led);
void main (void)
{
/*
at 10 bits resolution => 1024/3 = 341
reaching about 30 RPMs
*/
int dutyCycleFast = 341; // about 30% duty cycle
int dutyCycleSlow = 10; // about 65 duty cycle
/*
PWM0 and PWM1 pins enabled for PWM output
PWM I/O pin pair (PWM0, PWM1) is in the Independent mode
*/
int pwmcon0 = 0x21;
int pwmcon1 = 0x00;
// PWM time base operates in a Free-Running mode
int ptcon0 = 0x00;
// PTEN = 1, PTDIR = UP
int ptcon1 = 0x80;
int period = 0xff; // PWMf = 3,906 Khz
int sptime = 0x00;
/* All PORTB pins are output */
TRISB = 0;
/* Configs for PWM output
Interrupt disable & 1:1 prescaler & 1:1 postscaler (don´t care)
*/
OpenTimer2( TIMER_INT_OFF & T2_PS_1_1 & T2_POST_1_1);
Openpcpwm(pwmcon0, pwmcon1, ptcon0, ptcon1, period, sptime);
Setdc0pcpwm(dutyCycleFast);
Setdc1pcpwm(0);
while (1) {
/* RB2 = 1,2EN SN754410EN H-Bridge
RB1 = PWM1
RB0 = PWM0
*/
/* liga RB0, desliga RB1) */
PORTB &= 0xF4;
Delay10TCYx(10);
piscaLED(LED01);
}
}
void piscaLED(int led) {
int i;
for(i=0;i<10; i++) {
/* Delay de 1s */
Delay1KTCYx(4);
/* Pisca led */
//PORTB &= 0xF0;
PORTB ^= led;
}
}