#import import RPi.GPIO as GPIO import time from pad4pi import rpi_gpio import pigpio import spidev from gpiozero import MCP3008 #******************************************# KEYPAD = [ ["1", "2", "3", "A"], ["4", "5", "6", "B"], ["7", "8", "9", "C"], ["*", "0", "#", "D"] ] ROW_PINS = [17, 15, 14, 4] # BCM numbering COL_PINS = [24,22,27,5] # BCM numbering factory = rpi_gpio.KeypadFactory() keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS) #******************************************# spi = spidev.SpiDev() spi.open(0,0) pi=pigpio.pi() if not pi.connected: exit() Key = 0 keyFlag = False mainKey = 0 def analogInput(channel): spi.max_speed_hz = 3600 #f_cpu/frequency adc=spi.xfer2([1,(8+channel)<<4,0]) data = ((adc[1]&3)<<8) + adc[2] return data def Volts(data): volts=(data*3.3)/float(1023) #1024 volts=round(volts,2) return volts class Keystore: def __init__(self): #list to store them self.pressed_keys = ' ' #function to add to list def store_key(self,key): if(key=='A'): print(self.pressed_keys) keys=Keystore() def printKey(key): lcd_byte(ord(key),LCD_CHR) Key=key LUT = [671000,705000,733000,757000] i = 0 if((Key>='0' and Key < '9') or Key=='#' or Key=='*'): display5="Desired Volt:"+ Key lcd_string(display5,LCD_LINE_1) lcd_string("Invalid voltage",LCD_LINE_2) lcd_byte(ord(key),LCD_CHR) return else: lcd_string(Key,LCD_LINE_2) if(Key=="9"): i=0 display2= "Desired Volt: 9" lcd_string(display2,LCD_LINE_1) Key=float(ord('9')/25.3) Key=round(Key,2) pi.hardware_PWM(12, 70000, LUT[0]) elif(Key=="A"): i = 1 display2= "Desired Volt: 10" lcd_string(display2,LCD_LINE_1) Key=float(ord('A')/26) Key=round(Key,2) pi.hardware_PWM(12, 70000, LUT[1]) elif(Key=="B"): i = 2 display3= "Desired Volt: 11" lcd_string(display3,LCD_LINE_1) Key=float(ord('B')/24) Key=round(Key,2) pi.hardware_PWM(12, 70000, LUT[2]) else: i=3 display4= "Desired Volt: 12" lcd_string(display4,LCD_LINE_1) Key=float(ord('C')/22.3) Key=round(Key,2) pi.hardware_PWM(12, 70000, LUT[3]) dc = LUT[i] flagKey = True mainKey = Key #******************************************# # printKey will be called each time a keypad button is pressed keypad.registerKeyPressHandler(printKey) # Define GPIO to LCD mapping LCD_RS = 21 LCD_E = 20 LCD_D4 = 26 LCD_D5 = 19 LCD_D6 = 13 LCD_D7 = 6 # Define LCD parameters LCD_WIDTH = 16 # Maximum characters per line LCD_CHR = True LCD_CMD = False LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line # Timing constants E_PULSE = 0.0005 E_DELAY = 0.0005 #******************************************# def main(): # Main program block global pm global system_sts GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers GPIO.setup(LCD_E, GPIO.OUT) # E GPIO.setup(LCD_RS, GPIO.OUT) # RS GPIO.setup(LCD_D4, GPIO.OUT) # DB4 GPIO.setup(LCD_D5, GPIO.OUT) # DB5 GPIO.setup(LCD_D6, GPIO.OUT) # DB6 GPIO.setup(LCD_D7, GPIO.OUT) # DB7 # Initialise display lcd_init() lcd_byte(0x01, LCD_CMD) lcd_string("Enter Voltage",LCD_LINE_1) lcd_byte(0xC0, LCD_CMD) time.sleep(0.5) dc=714000 pi.hardware_PWM(12, 70000, dc) output= analogInput(0) outputVolts= Volts(output) output2 = output output2Volts = outputVolts while True: display = "Output Is: " + str(output2) lcd_string(display,LCD_LINE_2) time.sleep(1) while(True): output2=analogInput(0) output2Volts=Volts(output2) if(output2Volts >= 0.999 *outputVolts and output2Volts<=1.001* outputVolts): dc=dc pi.hardware_PWM(12, 70000, dc) break if(Key>output2Volts): dc=dc+840 # dc + dc_jump if(dc>759000): # max dc with 12v dc=759000 # 765000 #pi.hardware_PWM(12, 70000, dc) #break elif(dc<674000): dc=674000 else: dc = dc elif(Key759000): # max dc with 12v dc=759000 # 765000 #pi.hardware_PWM(12, 70000, dc) #break elif(dc<674000): dc=674000 else: dc = dc pi.hardware_PWM(12, 70000, dc) time.sleep(0.001) while keyFlag: output2= analogInput(0) output2Volts= Volts(output2) output2Volts= 4 * output2Volts display = "Output Is: " + str(output1) lcd_string(display,LCD_LINE_2) time.sleep(1) while(True): output2=analogInput(0) output2Volts=Volts(output2) Key = key Key = 4*Key if(Key >= 0.999 *output2Volts and Key<=1.001* output2Volts): dc=dc pi.hardware_PWM(12, 70000, dc) break if(Key>output2Volts): dc=dc+840 # dc + dc_jump elif(Key