PIC18F2580 DC motor driver program not working

Status
Not open for further replies.

Cantafford

Member
Hello,

I'm trying to interface a DC motor with a LD239D full bridge driver using a PIC18F2580.
The truth table for the LD239D is(in my case):
Rc0 = 1; Rc1=0; => CW rotation
Rc0 = 0; Rc1=1; => CCW rotation
Rc0 = 0; Rc1=0; => motor stops
Rc0 = 1; Rc1=1; => motor stops


This is my schematic:


And this is the code:
Code:
/*
* File:   driver.c
* Author: me
*
* Created on October 23, 2015, 11:06 AM
*/

#include <stdio.h>
#include <stdlib.h>
#include "config.h"

void main()
{
OSCCON = 0x76; // set internal oscillator to 8Mhz
TRISA = 0b11111111; // portA as input(the switches)
TRISC = 0b11111100; // set portC as output(RC0 = in1, RC1 = in2)

while(1)
{
    
     if(PORTAbits.RA0 == 0 && PORTAbits.RA1 == 1 && PORTAbits.RA2 == 1) // clockwise
     {LATCbits.LATC0 = 1; LATCbits.LATC1 = 0;}
 

     if(PORTAbits.RA0 == 1 && PORTAbits.RA1 == 0 && PORTAbits.RA2 == 1) // counter clockwise
     {LATCbits.LATC0 = 0; LATCbits.LATC1 = 1;}
   
     if(PORTAbits.RA2 == 0) // motor stops
     {LATCbits.LATC0 = 0; LATCbits.LATC1 = 0;}
}
    
}

I'm sure it's obvious however what I want to do is: basically when button 1 is pressed and the others are not motor will run CW, when button 2 is pressed and the others are not go CCW and when third one's pushed the motor shall stop regardless of what direction it's spinning.

The program worked fine if I added some delays instead of those ifs. When I use the if's however the RC0 and RC1 stay in 0 forever no matter what buttons I press. Obviously I'm missing something very simple but I just can't figure out what.

Any help will be appreciated. Thank you for reading!
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…