CPU = 16F876A MHZ = 10 CONFIG 16242 ; Motor controller rev 1 ; Written by Chris Keeser ; Created 5/21/04 ;========Hardware setup==================== clear ; reset all variables to 0 setpullups pu_on ; Enable 10k ohm pullups on B0 - B7 sethserial H19200 ; sets up hardware USART in the pic 16f876 for 14400 baud ;=========================================== ;=================PIN CONSTANTS============= green con B3 ; onboard green led yellow con B2 ; onboard yellow led red con B1 ; onboard red led s_out con C6 ; Hardware serial out pin s_in con C7 ; Hardware serial in pin int con B0 ; interrupt on edge pin B0, used to interrupt the motor controller int_pin var portB.bit0 ; interrupt pin input battery_pin con A5 ; this is the analog input for the raw battery voltage 8k - 1k v-div. ;==H-bridge A======== current_sense_A con A0 ; analog input for current of A bridge. 1 volt / amp temp_flag_A var portB.bit4 ; active low flag for bridge A temp warning ~ 145 C dir_A con C0 ; controls direction for A. 1 = (1+, 2-) 0 = (1-, 2+), should be 1 for braking pwm_A con 0 ; CCP1, needs to be 1 for braking, 0 for freewheeling w/brake @ 1 pin C2 pwm_A_pin con C2 ; incase you need to use the actual pwm pin brake_A con C4 ; active high, dir pin determines sink or source shorting, pwm must be 1 ;==H-bridge B========= current_sense_B con A1 ; analog input for current of B bridge. 1 volt / amp temp_flag_B var portB.bit5 ; active low flag for bridge B temp warning ~ 145 C dir_B con C3 ; controls direction for B. 1 = (1+, 2-) 0 = (1-, 2+), should be 1 for braking pwm_B con 1 ; CCP2, needs to be 1 for braking, 0 for freewheeling w/brake @ 1 pin C1 pwm_B_pin con C1 ; incase you need to use the actual pwm pin brake_B con C5 ; active high, dir pin determines sink or source shorting, pwm must be 1 ;================================================ ;============NUMERICAL CONSTANTS================= period con 1024 ; period in clk cycles, max value = 16383 clk con 2 ; value for A to D sample conversion, 2 is slowest based on clk cycles adron con %10001001 ; allows pins a0 - a5 to be used for analog input battery_mult con 0.0452 ; (max V) 5/1024 (A to D steps) * 9.1 (ratio of V div.) ;================================================ ;==============VARIABLES========================= test var long current_A var word current_B var word battery_voltage var word ;================================================ ;===============PRE CONDITIONS=================== output brake_A output brake_B output dir_A output dir_B input temp_flag_A input temp_flag_B input int_pin ;================================================ ;============MAIN FUNCTION======================= Main hserout ["TEST",10,13,"Serial communications established"] pause 1000 high green toggle dir_A ;(1+, 2-) toggle dir_B ;(1+, 2-) low brake_A ;NOT BRAKING, MUST BE LOW, otherwise H-bridge will not function low brake_B for test = 0 to period pause 10 adin current_sense_A,clk,adron,current_A hserout[dec current_A," "] adin current_sense_B,clk,adron,current_B hserout[dec current_B," "] adin battery_pin,clk,adron,battery_voltage hserout[dec battery_voltage] if (int_pin = 0 ) then hserout[13,10] else hserout[13] endif if (temp_flag_A = 0) OR (current_A > 50) then high yellow else low yellow endif if (temp_flag_B = 0) OR (current_B > 50) then high red else low red endif hpwm pwm_A,period,test hpwm pwm_B,period,test next pause 2000 goto main ;================================================ ;===========SUBROUTINES========================== ;================================================ ;=============INTERRUPT SERVICE ROUTINES========= ;================================================ ;============ON RESET ROUTINES=================== ;================================================ stop