I'm facing with a problem in my H bridge design.
I am using PIC 18F45K20 Microchip Controller along with BJT tranzistors to make a servo direction for a 5V motor. Everything is fine but i can't get around with C code for microcontroller.
I'm using C18 Compiler.
I want to get the signal from a switch on the development bord and based on this signal i want to change the rotation sense of the motor.
Furthermore, if anybody know how i can change motor speed based upon duty cicle and a varistor placed on the development bord it will be great.
Can anyone help me with debug this code please?
I am using PIC 18F45K20 Microchip Controller along with BJT tranzistors to make a servo direction for a 5V motor. Everything is fine but i can't get around with C code for microcontroller.
I'm using C18 Compiler.
I want to get the signal from a switch on the development bord and based on this signal i want to change the rotation sense of the motor.
Furthermore, if anybody know how i can change motor speed based upon duty cicle and a varistor placed on the development bord it will be great.
Can anyone help me with debug this code please?
Rich (BB code):
#include "p18f45k20.h"
#pragma config WDTEN=OFF #pragma config CPD=OFF #pragma config BOREN=OFF #pragma config IESO=OFF #pragma config CP2=OFF #pragma config FCMEN=OFF #pragma config PWRT=ON #pragma config MCLRE=OFF
#define FOSC 8000000L // Using Internal Clock of 8 MHz
// Delay Function
void _delay_us( unsigned int x) {unsigned char us;
us = (x)/(12000000/FOSC)|1;
while(--us != 0) continue; }
void _delay_ms(unsigned int ms){
unsigned char i;
if (ms == 0) return;
do {
i = 4;
do {
_delay_us(164);
} while(--i);
} while(--ms);
}
void main(void){
unsigned int ipwm;
unsigned char direction;
OSCCON=0x70; // Select 8 Mhz internal clock
TRISC = 0x00; // Set All on PORTC as Output
TRISA = 0b00000000; // Input for RA0 and RA1
ANSEL = 0x01; // Set PORT AN0 to analog input AN1 to AN7 digital I/O
ANSELH = 0x00; // Set PORT AN8 to AN11 as Digital I/O
PORTC = 0x00; // Turn Off all PORTC
/* Init PWM for Full Bridge Output */
CCP1CON=0b01001100; // Full Bridge Forward; P1A, P1C active-high; P1B, P1D active-high
CCPR1L=0; // Start with zero Duty Cycle
T2CON=0b00000101; // Postscale: 1:1, Timer2=On, Prescale = 1:4
PR2=0x65; // Frequency: 4.90 KHz
TMR2=0; // Start with zero Counter
/* Init ADC */
ADCON0=0b00000000;
ADCON1=0b00000000; // Select Left justify result. ADC port channel 0
// Select the FRC for 8 MHz
ADCON2 =0b0101011;
ADCON0bits.ADON=1;
direction=0; // Start with Forward Direction
ipwm=0;
for(;;) {
if ( PORTAbits.RA1==1) { // Change the Motor Direction when pressed
_delay_ms(1);
if ( PORTAbits.RA1==1) { // Read again for simple debounce
if (direction == 0) {
direction=1; // Reverse direction
CCP1CONbits.P1M1=1;
CCP1CONbits.P1M0=1;
} else {
direction=0; // Forward direction
CCP1CONbits.P1M1=1;
CCP1CONbits.P1M0=0;
}}}
ADCON0bits.GO_DONE=1; // initiate conversion on the channel 0
while(ADCON0bits.GO_DONE) continue; // Wait conversion done
ipwm = ADRESH; // Get the highest 8 bit MSB result, ignore the 2 bit LSB
CCPR1L=ipwm; // Set the Duty Cycle base on the ADC result
}}