/******************************************************************************* ||============================================================================|| ||::::::::::::::::::::::::::|Name of the Project|:::::::::::::::::::::::::::::|| || Control of a H-Brided motor with PIC16F877A || || || || * Autor: || || Guilherme Nabeyama || || * Descrição: || || This is a simple control of a motor using a PIC16F877A, H-Briged and || || DEMUX, the Microcontroller PIC recive the analog input and convert it || || to a movement of the motor || || * Device Configuration || || MCU: PIC16F877A || || Oscillator: HS, 4.0000 MHz || || Ext. Modules: - || || SW: mikroC v8.2 || ||============================================================================|| *******************************************************************************/ void main(){ int j; int oj; int leitura; TRISB.F7 = 0; // PORT is output PORTB.F7 = 0; // Set PORT to 0 TRISB.F6 = 0; // PORT is output PORTB.F6 = 0; // Set PORT to 0 TRISB.F5 = 0; // PORT is output PORTB.F5 = 0; // Set PORT to 0 TRISB.F0 = 1; // PORT is input PORTB.F0 = 0; // Set PORT to 0 TRISB.F1 = 1; // PORT is input PORTB.F1 = 0; // Set PORT to 0 TRISB.F3 = 1; // PORT is input PORTB.F3 = 0; // Set PORT to 0 TRISB.F4 = 1; // PORT is input PORTB.F5 = 0; // Set PORT to 0 ADCON1 = 0x80; // Configure analog inputs and Vref TRISA = 0xFF; // PORTA is input PORTC = 0xFF; // Set PORTC to $FF TRISC = 0; // PORTC is output Pwm_Init(5000); // Initialize PWM module j = 0; // Initial value for j oj = 0; // oj will keep the 'old j' value Pwm_Start(); // Start PWM while (1) { // Endless loop leitura = Adc_Read(2); // Get results of AD conversion if ((leitura <= 514) && (leitura >=508)) { // Read of Analog is 0 to 1024 j = 0; } if (leitura >= 515) { // Make the motor rotate clockwise j = (leitura - 514)/2; PORTB.F5 = 0 ; PORTB.F6 = 0 ; PORTB.F7 = 0 ; } if (leitura <= 507) { // Make the motor rotate j = (-leitura + 1024)/2; // counterclockwise PORTB.F5 = 0 ; PORTB.F6 = 1 ; PORTB.F7 = 0 ; } Pwm_Change_Duty(j); // set new duty ratio, Delay_ms(200); // Slow down a bit } }