' This program reads the voltage on pin ADC0 (potentiometer)
' and controls an h-bridge driver while getting
' feedback on the current used to drive the TEC.
'
' 06/27/09
' File: TE_CONTROLLER_H-BRIDGE_V1-1.BAS
' See TE_CONTROLLER_H-BRIDGE_V1-1.sch for the eagle schematic.
' ----------- Declarations ------------------
Symbol AD_Val = W0 ' variable to hold ADC values
Symbol Sum = W1 ' variable to hold sum of ADC values
Symbol N = b6 ' repetitions to sum
Symbol Volts_place = W2 ' placeholder for voltage calc
Symbol Volts_Ones = b7 ' pot voltage 1's place
Symbol Volts_Tenths = b8 ' pot voltage 0.1's place
Symbol Volts_Hunds = b9 ' pot voltage 0.01's place
Symbol Duty = W5 ' duty of PWM
Symbol PWM_TEC = 2 ' peltier pin
Symbol In1 = 0 ' IN1 of H-bridge (1 = forward, 0 = reverse)
Symbol In2 = 1 ' IN2 of H-bridge (0 = forward, 1 = reverse)
Symbol Current_Ones = b14 ' current from feedback of driver, 1's place
Symbol Current_Tenths = b15 ' current 0.1's place
'------------ Initializations ---------------
high In1 ' run the device
low In2 ' in cool mode
'------------ Program -----------------------
Main:
gosub Check_Pot
gosub Set_TEC
gosub Read_Current
goto Main
'------------ Subroutines --------------------
Check_Pot: ' subroutine to read pot setting
Sum = 0
For N = 1 to 16
ReadADC10 0, AD_Val
Sum = Sum + AD_Val
Next
AD_Val = Sum / 16
Debug AD_Val
Volts_place = AD_Val * 5 / 1023
Volts_Ones = Volts_place
Volts_place = AD_Val * 5 % 1023
Volts_place = Volts_place * 10 / 1023
Volts_Tenths = Volts_place
Volts_place = AD_Val * 5 % 1023
Volts_place = Volts_place * 10 % 1023
Volts_place = Volts_place * 10 / 1023
Volts_Hunds = Volts_place
Return
Set_TEC: ' subroutine to set the PWM
' of the peltier according
Duty = AD_Val * 40 / 1023 ' to the pot setting
Duty = Duty * 10
pwmout PWM_TEC, 99, Duty
Pause 1000
Return
Read_Current: ' subroutine to read the current
' driving the peltier
Sum = 0 ' throught the H-bridge driver
For N = 1 to 16
ReadADC10 1, AD_Val
Sum = Sum + AD_Val
Next
AD_Val = Sum / 16
Current_Ones = AD_Val / 121 ' 0.59 V = 1 A; 121 divisions/1 A
Volts_place = AD_Val % 121
Volts_place = Volts_place * 10 / 121
Current_Tenths = Volts_place
Return