-
Notifications
You must be signed in to change notification settings - Fork 0
/
PWM_ADCcontrolled.py
executable file
·53 lines (44 loc) · 1.2 KB
/
PWM_ADCcontrolled.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python
import sys
#import time
ADC0_PATH = "/sys/bus/iio/devices/iio:device0/in_voltage"
PWM0_PATH = "/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0"
dc = 0
ADCmax = 2768
c=1e-9
f =60 #f=1/period --> frequency in hertz| 1 = 1ns |
def readAnalog(number):
"""This function reads a RAW ADC value from the path"""
aps = ADC0_PATH + str(number) + "_raw"
fo1 = open(aps,"r")
acr = fo1.read()
return acr
def ctrlPWM(parameter, value, pwm0path=PWM0_PATH +"/pwm-0:0"):
fo2 = open(pwm0path + parameter,"w")
fo2.write(value)
fo2.close()
return
def f2t(freq):
time = 1/(freq*c)
return time
def dct(decimal):
dc = int(period*float(decimal)/ADCmax)
if dc<(0.05*period):
dc = 0.05*period
elif dc>(0.95*period):
dc = 0.95*period
return dc
#Application begins:
print("Starting ADC Script")
#config-pin P1_33 pwm
print("Reading Analog 0")
#Set initial PWM values
period = f2t(f)
mindc = int(0.05*period)
ctrlPWM("/period", str(int(period)))
ctrlPWM("/duty_cycle", str(mindc))
ctrlPWM("/enable", "0")
ctrlPWM("/enable", "1")
while True:
ctrlPWM("/duty_cycle", str(int(dct(readAnalog(0)))))
print("End of test Script")