Advertisement
Guest User

Using SPI connection ADT7310 with MaixPy

a guest
Dec 29th, 2019
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. from fpioa_manager import fm, board_info
  2. from Maix import GPIO
  3. from machine import SPI
  4. import time
  5. import lcd
  6. import image
  7.  
  8.  
  9. img = image.Image()
  10. img.draw_rectangle(0, 0, 320, 240, color=(255, 0, 0),fill=True)
  11. lcd.init(freq=15000000)
  12. lcd.display(img)
  13.  
  14. fm.register(21,fm.fpioa.GPIOHS0)#cs
  15. #fm.register(21,fm.fpioa.SPI1_SS0)#cs
  16. fm.register(24,fm.fpioa.SPI1_D0)#mosi
  17. fm.register(22,fm.fpioa.SPI1_D1)#miso
  18. fm.register(23,fm.fpioa.SPI1_SCLK)#sclk
  19.  
  20. ss=GPIO(GPIO.GPIOHS0,GPIO.OUT)
  21.  
  22. def spi_cs_on():
  23.     ss.value(0)
  24.  
  25. def spi_cs_off():
  26.     ss.value(1)
  27.  
  28.  
  29. spi01=SPI(SPI.SPI1,mode=SPI.MODE_MASTER,baudrate=125000,polarity=1,phase=1,bits=8,firstbit=SPI.MSB,mosi=fm.fpioa.SPI1_D0,miso=fm.fpioa.SPI1_D1,sck=fm.fpioa.SPI1_SCLK,cs0=-1)
  30. #spi01=SPI(SPI.SPI1,mode=SPI.MODE_MASTER,baudrate=125000,polarity=1,phase=1,bits=8,firstbit=SPI.MSB,mosi=fm.fpioa.SPI1_D0,miso=fm.fpioa.SPI1_D1,sck=fm.fpioa.SPI1_SCLK,cs0=fm.fpioa.SPI1_SS0)
  31.  
  32. spi_cs_off()
  33. buff=bytearray([0xff,0xff,0xff,0xff])
  34. spi_cs_on()
  35. spi01.write(buff, cs=SPI.CS0)
  36. spi_cs_off()
  37. time.sleep(0.1)
  38.  
  39. for count in range(17280):
  40.     # 16bit 1-shot
  41.     buff=bytearray([0x08,0xa0])
  42.     # 13bit 1-shot
  43.     #buff=bytearray([0x08,0x20])
  44.     spi_cs_on()
  45.     spi01.write(buff, cs=SPI.CS0)
  46.     spi_cs_off()
  47.  
  48.     count001 = 0;
  49.     while True:
  50.         time.sleep(0.01)
  51.         # ステータスレジスタの読み込み
  52.         buff=bytearray([0x40])
  53.         spi_cs_on()
  54.         spi01.write(buff,cs=SPI.CS0)
  55.         status_reg = bytearray(spi01.read(1,0x00,cs=SPI.CS0))
  56.         spi_cs_off()
  57.         count001 += 1
  58.         if count001 >= 100:
  59.             break
  60.         if ((status_reg[0] >> 7) & 1) == 0:
  61.             break
  62.  
  63.     status_str = "status=0x{0:02x}, {1:d} ms".format(status_reg[0], count001*10)
  64.     img.draw_rectangle(0, 0, 320, 240, color=(255, 0, 0),fill=True)
  65.     #img.draw_string(10,50,status_str,color=(255,255,255),scale=2,mono_space=False)
  66.     print(status_str)
  67.  
  68.     time.sleep(0.36)
  69.  
  70.     buff=bytearray([0x50])
  71.     spi_cs_on()
  72.     spi01.write(buff,cs=SPI.CS0)
  73.     temp_raw = bytearray(spi01.read(2,0x00,cs=SPI.CS0))
  74.     spi_cs_off()
  75.  
  76.     # 16bitの計算
  77.     temp = (temp_raw[0]*256+temp_raw[1])
  78.     if temp >= 32768:
  79.         temp - 65536
  80.     temp = temp / 128
  81.     temp_str = "{0:.4f} Celsius".format(temp)
  82.     img.draw_string(10,100,temp_str,color=(255,255,255),scale=5,mono_space=False)
  83.     print("{0:.4f} ℃".format(temp))
  84.     # 13bitの計算
  85.     #temp = (temp_raw[0]*256+temp_raw[1])/8
  86.     #if temp >= 4096:
  87.     #    temp - 8192
  88.     #temp = temp / 16
  89.     #print(temp)
  90.     lcd.display(img)
  91.     time.sleep(5.0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement