Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
junhuanchen committed Feb 3, 2019
2 parents b905da8 + 803a037 commit d51b297
Showing 1 changed file with 91 additions and 24 deletions.
115 changes: 91 additions & 24 deletions 07.sensors/cs5460a/cs5460a.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,60 @@
from machine import Pin, SPI
import utime

from micropython import const

CS5460A_RMS_VOLTAGE = const(0x18)

CS5460A_RMS_CURRENT = const(0x16)

CS5460A_TRUE_POWER = const(0x14)

CS5460A_CFG_READ = const(0x00) # //reg read: config

CS5460A_CFG_READ_IGN = const(0x04) # //reg read: Ign

CS5460A_CFG_READ_VGN = const(0x08) # //reg read: Vgn

CS5460A_CFG_READ_CYCLE = const(0x0a) # //reg read: cycle count

CS5460A_CFG_POWER_UP = const(0xa0) # // power-up/halt

CS5460A_CFG_GAIN = const(0x40) # // reg write: config. PGA Gain 10x, IHPF=1, VHPF=1

CS5460A_CFG_GAIN1 = const(0x01)

CS5460A_CFG_GAIN2 = const(0x00)

CS5460A_CFG_GAIN3 = const(0x61)

CS5460A_CFG_IGN= const(0x44) # // reg write: Ign [current chan gain].

CS5460A_CFG_IGN1 = const(0x40)

CS5460A_CFG_IGN2 = const(0x00)

CS5460A_CFG_IGN3 = const(0x00)

CS5460A_CFG_VGN = const(0x48) # reg write: Vgn [voltage chan gain]

CS5460A_CFG_VGN1 = const(0x41)

CS5460A_CFG_VGN2 = const(0xA0)

CS5460A_CFG_VGN3 = const(0xEA)

CS5460A_START_CONV = const(0xe8) #command : start convert


class cs5460a(object):
def __init__(self, spi, cs=4, rst=2):
import utime
def __init__(self, spi, cs=2, rst=4):
self.spi = spi
self.cs = Pin(cs, Pin.OUT)
self.rst = Pin(rst, Pin.OUT)
self.FLOAT24 = 16777216.0 # 2^24
self.VOLTAGE_MULTIPLIER = (1 / self.FLOAT24 * 385)
self.VOLTAGE_MULTIPLIER = (1 / self.FLOAT24 * 367)
self.CURRENT_MULTIPLIER = (1 / self.FLOAT24 * 11.66)
self.POWER_MULTIPLIER = (1 / self.FLOAT24 * 1.024 * 385 * 11.66 * 2)
self.POWER_MULTIPLIER = (1 / self.FLOAT24 * 1.024 * 367 * 11.66 * 2)

def read(self, addr):
b = bytearray([0xfe, 0xfe, 0xfe])
Expand All @@ -28,24 +73,24 @@ def write(self, addr, data):
self.cs.value(1)
# print('write_ok')

def cs5460a_init(self):
def cs5460a_setup(self):
self.rst.value(0)
utime.sleep_ms(50)
self.rst.value(1) # reset the cs5460a

self.cs.value(0)
self.spi.write(bytearray([0xa0]))
self.spi.write(bytearray([CS5460A_CFG_POWER_UP]))
self.cs.value(1) # the command of the power_up

self.write(0x40, bytearray([0x00, 0x00, 0x61])) # set
self.write(0x48, bytearray([0x46, 0xfa, 0xcb]))
self.write(0x44, bytearray([0x42, 0x66, 0xb4]))
self.write(CS5460A_CFG_GAIN, bytearray([CS5460A_CFG_GAIN1, CS5460A_CFG_GAIN2, CS5460A_CFG_GAIN3])) # set
self.write(CS5460A_CFG_VGN , bytearray([CS5460A_CFG_VGN1, CS5460A_CFG_VGN2, CS5460A_CFG_VGN3]))#V
self.write(CS5460A_CFG_IGN, bytearray([CS5460A_CFG_IGN1, CS5460A_CFG_IGN2, CS5460A_CFG_IGN3]))#A

self.cs.value(0)
self.spi.write(bytearray([0xe8]))
self.spi.write(bytearray([CS5460A_START_CONV]))
self.cs.value(1) # start to convert

def conv(self, true_power):
def _conv(self, true_power):
if true_power[0] > 0x80:
a = bytearray([~true_power[0]])
a[0] &= 0x7f
Expand All @@ -59,29 +104,51 @@ def conv(self, true_power):
temp = ((true_power[0] + true_power[0] * 256 + true_power[0] * 65536) + 1)
return temp

def read_u(self):
voltage = self.read(CS5460A_RMS_VOLTAGE)
temp = (voltage[2] + voltage[1] * 256 + voltage[0] * 65536)
V = self.VOLTAGE_MULTIPLIER * temp
return V

def read_i(self):
current = self.read(CS5460A_RMS_CURRENT)
temp = (current[2] + current[1] * 256 + current[0] * 65536)
A = self.CURRENT_MULTIPLIER * temp
return A

def read_p(self):
true_power = self.read(CS5460A_TRUE_POWER)
temp = self._conv(true_power)
P = self.POWER_MULTIPLIER * temp
return P


from machine import Pin
import utime

p = Pin(18, Pin.OUT)
p.value(1)


def unit_test():
vspi = SPI(-1, sck=Pin(18), mosi=Pin(23), miso=Pin(19), baudrate=2000000) # -1 software spi
vspi = SPI(-1, sck=Pin(5), mosi=Pin(23), miso=Pin(19), baudrate=2000000) # -1 software spi
ts = cs5460a(vspi)
ts.cs5460a_init() # 初始化
ts.cs5460a_setup() # 初始化

while True:
utime.sleep_ms(1000)
k = ts.read(0x1e)
print(k)
voltage = ts.read(0x18)
current = ts.read(0x16)
true_power = ts.read(0x14)
temp1 = (current[2] + current[1] * 256 + current[0] * 65536)
temp2 = (voltage[2] + voltage[1] * 256 + voltage[0] * 65536)
t = ts.conv(true_power)
A = ts.CURRENT_MULTIPLIER * temp1
V = ts.VOLTAGE_MULTIPLIER * temp2
P = ts.POWER_MULTIPLIER * t
# print(k)
# current=ts.read(0x16)
# temp = (current[2] + current[1] * 256 + current[0] * 65536)/16777216
# print(temp)
V = ts.read_u()
A = ts.read_i()
P = ts.read_p()
print('current=%.2f A' % A)
print('voltage=%.2f V' % V)
print('ture_power=%.2f W' % P)


if __name__ == '__main__':
unit_test()
unit_test()

0 comments on commit d51b297

Please sign in to comment.