11from machine import Pin , SPI
2- import utime
2+
3+ from micropython import const
4+
5+ CS5460A_RMS_VOLTAGE = const (0x18 )
6+
7+ CS5460A_RMS_CURRENT = const (0x16 )
8+
9+ CS5460A_TRUE_POWER = const (0x14 )
10+
11+ CS5460A_CFG_READ = const (0x00 ) # //reg read: config
12+
13+ CS5460A_CFG_READ_IGN = const (0x04 ) # //reg read: Ign
14+
15+ CS5460A_CFG_READ_VGN = const (0x08 ) # //reg read: Vgn
16+
17+ CS5460A_CFG_READ_CYCLE = const (0x0a ) # //reg read: cycle count
18+
19+ CS5460A_CFG_POWER_UP = const (0xa0 ) # // power-up/halt
20+
21+ CS5460A_CFG_GAIN = const (0x40 ) # // reg write: config. PGA Gain 10x, IHPF=1, VHPF=1
22+
23+ CS5460A_CFG_GAIN1 = const (0x01 )
24+
25+ CS5460A_CFG_GAIN2 = const (0x00 )
26+
27+ CS5460A_CFG_GAIN3 = const (0x61 )
28+
29+ CS5460A_CFG_IGN = const (0x44 ) # // reg write: Ign [current chan gain].
30+
31+ CS5460A_CFG_IGN1 = const (0x40 )
32+
33+ CS5460A_CFG_IGN2 = const (0x00 )
34+
35+ CS5460A_CFG_IGN3 = const (0x00 )
36+
37+ CS5460A_CFG_VGN = const (0x48 ) # reg write: Vgn [voltage chan gain]
38+
39+ CS5460A_CFG_VGN1 = const (0x41 )
40+
41+ CS5460A_CFG_VGN2 = const (0xA0 )
42+
43+ CS5460A_CFG_VGN3 = const (0xEA )
44+
45+ CS5460A_START_CONV = const (0xe8 ) #command : start convert
46+
347
448class cs5460a (object ):
5- def __init__ (self , spi , cs = 4 , rst = 2 ):
49+ import utime
50+ def __init__ (self , spi , cs = 2 , rst = 4 ):
651 self .spi = spi
752 self .cs = Pin (cs , Pin .OUT )
853 self .rst = Pin (rst , Pin .OUT )
954 self .FLOAT24 = 16777216.0 # 2^24
10- self .VOLTAGE_MULTIPLIER = (1 / self .FLOAT24 * 385 )
55+ self .VOLTAGE_MULTIPLIER = (1 / self .FLOAT24 * 367 )
1156 self .CURRENT_MULTIPLIER = (1 / self .FLOAT24 * 11.66 )
12- self .POWER_MULTIPLIER = (1 / self .FLOAT24 * 1.024 * 385 * 11.66 * 2 )
57+ self .POWER_MULTIPLIER = (1 / self .FLOAT24 * 1.024 * 367 * 11.66 * 2 )
1358
1459 def read (self , addr ):
1560 b = bytearray ([0xfe , 0xfe , 0xfe ])
@@ -28,24 +73,24 @@ def write(self, addr, data):
2873 self .cs .value (1 )
2974 # print('write_ok')
3075
31- def cs5460a_init (self ):
76+ def cs5460a_setup (self ):
3277 self .rst .value (0 )
3378 utime .sleep_ms (50 )
3479 self .rst .value (1 ) # reset the cs5460a
3580
3681 self .cs .value (0 )
37- self .spi .write (bytearray ([0xa0 ]))
82+ self .spi .write (bytearray ([CS5460A_CFG_POWER_UP ]))
3883 self .cs .value (1 ) # the command of the power_up
3984
40- self .write (0x40 , bytearray ([0x00 , 0x00 , 0x61 ])) # set
41- self .write (0x48 , bytearray ([0x46 , 0xfa , 0xcb ]))
42- self .write (0x44 , bytearray ([0x42 , 0x66 , 0xb4 ]))
85+ self .write (CS5460A_CFG_GAIN , bytearray ([CS5460A_CFG_GAIN1 , CS5460A_CFG_GAIN2 , CS5460A_CFG_GAIN3 ])) # set
86+ self .write (CS5460A_CFG_VGN , bytearray ([CS5460A_CFG_VGN1 , CS5460A_CFG_VGN2 , CS5460A_CFG_VGN3 ]))#V
87+ self .write (CS5460A_CFG_IGN , bytearray ([CS5460A_CFG_IGN1 , CS5460A_CFG_IGN2 , CS5460A_CFG_IGN3 ]))#A
4388
4489 self .cs .value (0 )
45- self .spi .write (bytearray ([0xe8 ]))
90+ self .spi .write (bytearray ([CS5460A_START_CONV ]))
4691 self .cs .value (1 ) # start to convert
4792
48- def conv (self , true_power ):
93+ def _conv (self , true_power ):
4994 if true_power [0 ] > 0x80 :
5095 a = bytearray ([~ true_power [0 ]])
5196 a [0 ] &= 0x7f
@@ -59,29 +104,51 @@ def conv(self, true_power):
59104 temp = ((true_power [0 ] + true_power [0 ] * 256 + true_power [0 ] * 65536 ) + 1 )
60105 return temp
61106
107+ def read_u (self ):
108+ voltage = self .read (CS5460A_RMS_VOLTAGE )
109+ temp = (voltage [2 ] + voltage [1 ] * 256 + voltage [0 ] * 65536 )
110+ V = self .VOLTAGE_MULTIPLIER * temp
111+ return V
112+
113+ def read_i (self ):
114+ current = self .read (CS5460A_RMS_CURRENT )
115+ temp = (current [2 ] + current [1 ] * 256 + current [0 ] * 65536 )
116+ A = self .CURRENT_MULTIPLIER * temp
117+ return A
118+
119+ def read_p (self ):
120+ true_power = self .read (CS5460A_TRUE_POWER )
121+ temp = self ._conv (true_power )
122+ P = self .POWER_MULTIPLIER * temp
123+ return P
124+
125+
126+ from machine import Pin
127+ import utime
128+
129+ p = Pin (18 , Pin .OUT )
130+ p .value (1 )
131+
62132
63133def unit_test ():
64- vspi = SPI (- 1 , sck = Pin (18 ), mosi = Pin (23 ), miso = Pin (19 ), baudrate = 2000000 ) # -1 software spi
134+ vspi = SPI (- 1 , sck = Pin (5 ), mosi = Pin (23 ), miso = Pin (19 ), baudrate = 2000000 ) # -1 software spi
65135 ts = cs5460a (vspi )
66- ts .cs5460a_init () # 初始化
136+ ts .cs5460a_setup () # 初始化
67137
68138 while True :
69139 utime .sleep_ms (1000 )
70140 k = ts .read (0x1e )
71- print (k )
72- voltage = ts .read (0x18 )
73- current = ts .read (0x16 )
74- true_power = ts .read (0x14 )
75- temp1 = (current [2 ] + current [1 ] * 256 + current [0 ] * 65536 )
76- temp2 = (voltage [2 ] + voltage [1 ] * 256 + voltage [0 ] * 65536 )
77- t = ts .conv (true_power )
78- A = ts .CURRENT_MULTIPLIER * temp1
79- V = ts .VOLTAGE_MULTIPLIER * temp2
80- P = ts .POWER_MULTIPLIER * t
141+ # print(k)
142+ # current=ts.read(0x16)
143+ # temp = (current[2] + current[1] * 256 + current[0] * 65536)/16777216
144+ # print(temp)
145+ V = ts .read_u ()
146+ A = ts .read_i ()
147+ P = ts .read_p ()
81148 print ('current=%.2f A' % A )
82149 print ('voltage=%.2f V' % V )
83150 print ('ture_power=%.2f W' % P )
84151
85152
86153if __name__ == '__main__' :
87- unit_test ()
154+ unit_test ()
0 commit comments