' Motor Drive software with forward and reverse
' Started....30/09/14
' Microcontroller used: Microchip Technology 16F88
' microchip.com
' PicBasic Pro Code: micro-Engineering Labs, Inc.
' melabs.com
'-------Program Desciption--------
' Reads motor Hall switches and output signals to motor drivers. Rotational direction changed by input signal
 #CONFIG
 __CONFIG    _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_ON & _WDT_OFF & _INTRC_IO
 #ENDCONFIG
 
'------------Variable-------------
 A VAR BYTE ' received code


'----------Initialization---------

DEFINE OSC 8  'oscillator 8MHz




INTCON.7 = 1  'interups disabled
 ANSEL = %00000000 'all digital



TRISB = %00000000 ' Set all RBs to otputs

 
PORTB = %00000000 'set all RBs low 
TRISA = %00111111 ' RA6,RA7 outputs rest inputs

PORTA = %00000000 ' all ports low 

OSCCON = %01111000  'oscillator 8 Mhz

 A = %00000000
 
'------------Main Code------------
 HPWM 1,127,22800

 Start: 
 if PORTA.3 = 1 then goto  clockwise
 if PORTA.3 = 0 then goto anticlockwise

 clockwise:
If PORTA.0 = 1 and PORTA.1 = 0 and PORTA.2 = 0 THEN GOTO POS2
if PORTA.0 = 1 and PORTA.1 = 1 and PORTA.2 = 0 then goto POS3
if PORTA.0 = 0 and PORTA.1 = 1 and PORTA.2 = 0 then goto POS4
if PORTA.0 = 0 and PORTA.1 = 1 and PORTA.2 = 1 then goto POS5
if PORTA.0 = 0 and PORTA.1 = 0 and PORTA.2 = 1 then goto POS6
if PORTA.0 = 1 and PORTA.1 = 0 and PORTA.2 = 1 then goto POS1
GOTO Start
POS1:

PORTB.6 = 0 
PORTB.1 = 0
PORTB.2 = 1
PORTB.3 = 1
PORTB.4 = 1 
PORTB.5 = 0

GOTO  START

POS2:

PORTB.6 = 1 
PORTB.1 = 1  
PORTB.2 = 0
PORTB.3 = 0
PORTB.4 = 1 
PORTB.5 = 0

goto Start

POS3:

PORTB.6 = 1 
PORTB.1 = 1   
PORTB.2 = 1
PORTB.3 = 0
PORTB.4 = 0 
PORTB.5 = 0

goto Start

POS4:

PORTB.6 = 0 
PORTB.1 = 0   
PORTB.2 = 1
PORTB.3 = 0
PORTB.4 = 1 
PORTB.5 = 1

goto Start

POS5:

PORTB.6 = 1 
PORTB.1 = 0   
PORTB.2 = 0
PORTB.3 = 0
PORTB.4 = 1 
PORTB.5 = 1

goto Start

POS6:


PORTB.6 = 1 
PORTB.1 = 0   
PORTB.2 = 1
PORTB.3 = 1
PORTB.4 = 0 
PORTB.5 = 0

goto start   

 
 
 anticlockwise:
If PORTA.0 = 1 and PORTA.1 = 0 and PORTA.2 = 0 THEN GOTO POS1A
if PORTA.0 = 1 and PORTA.1 = 1 and PORTA.2 = 0 then goto POS2A
if PORTA.0 = 0 and PORTA.1 = 1 and PORTA.2 = 0 then goto POS3A
if PORTA.0 = 0 and PORTA.1 = 1 and PORTA.2 = 1 then goto POS4A
if PORTA.0 = 0 and PORTA.1 = 0 and PORTA.2 = 1 then goto POS5A
if PORTA.0 = 1 and PORTA.1 = 0 and PORTA.2 = 1 then goto POS6A
GOTO Start
POS1A:

PORTB.6 = 1 
PORTB.1 = 0
PORTB.2 = 0
PORTB.3 = 0
PORTB.4 = 1 
PORTB.5 = 1

GOTO  START

POS2A:

PORTB.6 = 1 
PORTB.1 = 0  
PORTB.2 = 1
PORTB.3 = 1
PORTB.4 = 0 
PORTB.5 = 0

goto Start

POS3A:

PORTB.6 = 0 
PORTB.1 = 0   
PORTB.2 = 1
PORTB.3 = 1
PORTB.4 = 1 
PORTB.5 = 0

goto Start

POS4A:

PORTB.6 = 1 
PORTB.1 = 1   
PORTB.2 = 0
PORTB.3 = 0
PORTB.4 = 1 
PORTB.5 = 0

goto Start

POS5A:

PORTB.6 = 1 
PORTB.1 = 1   
PORTB.2 = 1
PORTB.3 = 0
PORTB.4 = 0 
PORTB.5 = 0

goto Start

POS6A:


PORTB.6 = 0 
PORTB.1 = 0   
PORTB.2 = 1
PORTB.3 = 0
PORTB.4 = 1 
PORTB.5 = 1

goto start   

 
 
 
