冒个泡 前段时间做机床 改了一个AS5600角度传感器的库 有需要的吗

ESP32、ESP8266
ESP32-S2、ESP32-S3、ESP32-C3
回复
sf5a1
帖子: 10
注册时间: 2019年 12月 5日 13:53

冒个泡 前段时间做机床 改了一个AS5600角度传感器的库 有需要的吗

#1

帖子 sf5a1 » 2020年 12月 14日 20:00

#///////////////////////////////////////#
# 2020-11-26
#
# Micropython Esp8266 MINI
#
# SDA = 4 D2
# SCL = 5 D1
#For Example
# import as5600
# Test = as5600.AS5600()
# Value_AS5600 = Test.printtf()
# print(Value_AS5600)
#///////////////////////////////////////

import time
import machine
from machine import I2C , Pin


Y_SCK_PIN=5
Y_SDA_PIN=4

class AS5600:
def __init__(self):
#self.Y_SCK_PIN=5
#self.Y_SDA_PIN=4
self.address=0x36
self.i2c = I2C(scl=machine.Pin(Y_SCK_PIN),sda=machine.Pin(Y_SDA_PIN),freq=100000)
self.Y_SCK_H()
self.Y_SDA_H()

def Y_SCK_H(self):
pin = machine.Pin(Y_SCK_PIN, machine.Pin.OUT)
pin.value(1)
def Y_SCK_L(self):
pin = machine.Pin(Y_SCK_PIN, machine.Pin.OUT)
pin.value(0)
def Y_SDA_H(self):
pin = machine.Pin(Y_SDA_PIN, machine.Pin.OUT)
pin.value(1)
def Y_SDA_L(self):
pin = machine.Pin(Y_SDA_PIN, machine.Pin.OUT)
pin.value(0)

def Y_READ_SDA(self):
pin = machine.Pin(Y_SDA_PIN,machine.Pin.IN)
return pin.value()

def Y_IIC_Start(self):
self.Y_SDA_H()
self.Y_SCK_H()
time.sleep_us(5)
self.Y_SDA_L()
time.sleep_us(5)
self.Y_SCK_L()
time.sleep_us(2)

def Y_IIC_Stop(self):
self.Y_SCK_L()
self.Y_SDA_L()
time.sleep_us(5)
self.Y_SCK_H()
time.sleep_us(5)
self.Y_SDA_H()
time.sleep_us(5)

def Y_IIC_Wait_Ack(self):
ucErrTime=0
self.Y_SDA_H()
time.sleep_us(2)
self.Y_SCK_H()
time.sleep_us(2)
while(self.Y_READ_SDA()):
ucErrTime=ucErrTime+1
if(ucErrTime>250):
self.Y_IIC_Stop()
return 1
self.Y_SCK_L()
time.sleep_us(2)
return 0

def Y_IIC_Ack(self):
self.Y_SCK_L()
self.Y_SDA_L()
time.sleep_us(2)
self.Y_SCK_H()
time.sleep_us(5)
self.Y_SCK_L()
time.sleep_us(2)
self.Y_SDA_H()
time.sleep_us(2)
def Y_IIC_Nack(self):
self.Y_SCK_L()
self.Y_SDA_H()
time.sleep_us(2)
self.Y_SCK_H()
time.sleep_us(5)
self.Y_SCK_L()
time.sleep_us(2)
self.Y_SDA_H()
time.sleep_us(2)

def Y_IIC_SendByte(self, Txdata):
#u8 i
for i in range(0,8):
self.Y_SCK_L()
if(Txdata & 0x80):
self.Y_SDA_H()
else:
self.Y_SDA_L()
time.sleep_us(5)
self.Y_SCK_H()
time.sleep_us(5)
self.Y_SCK_L()
Txdata<<=1
time.sleep_us(5)
time.sleep_us(2)
self.Y_SDA_H()
time.sleep_us(2)
def Y_IIC_ReadByte(self, ack):
receive=0
time.sleep_us(2)
self.Y_SDA_H()
time.sleep_us(2)
for i in range(0,8):
self.Y_SCK_L()
time.sleep_us(5)
self.Y_SCK_H()
receive <<=1
time.sleep_us(5)
#pinMode(Y_SDA_PIN, INPUT);
if (self.Y_READ_SDA()):
receive |= 0x01
else:
receive &= 0xfe
time.sleep_us(5)
if ( not ack):
self.Y_IIC_Nack()
else:
self.Y_IIC_Ack()
self.Y_SCK_L()
time.sleep_us(2)
return receive

def AS5600_IIC_Write_OneByte(self, deviceaddr, writeaddr, writedata):

self.Y_IIC_Start()
self.Y_IIC_SendByte(deviceaddr & 0xfe)
self.Y_IIC_Wait_Ack()
self.Y_IIC_SendByte(writeaddr)
self.Y_IIC_Wait_Ack()
self.Y_IIC_SendByte(writedata)
self.Y_IIC_Wait_Ack()
self.Y_IIC_Stop()
time.sleep_ms(10)
def AS5600_IIC_Read_OneByte(self, deviceaddr, readaddr):
self.Y_IIC_Start()
self.Y_IIC_SendByte(deviceaddr & 0xfe)
self.Y_IIC_Wait_Ack()
self.Y_IIC_SendByte(readaddr)
self.Y_IIC_Wait_Ack()

self.Y_IIC_Start()
self.Y_IIC_SendByte(deviceaddr | 0x01)
self.Y_IIC_Wait_Ack()
temp=self.Y_IIC_ReadByte(0)
self.Y_IIC_Stop()
return temp
def printtf(self):
value = self.AS5600_IIC_Read_OneByte((0x36<<1),0x0e)
value <<= 8
value |= self.AS5600_IIC_Read_OneByte((0x36<<1),0x0f)
print(value)
return value

#while 1:
# rr=AS5600()
#rr.printtf()
#time.sleep(1)

以上是IIC模式
以下是模拟量模式
#///////////////////////////////////////#
# 2020-11-26
#
# Micropython Esp8266 MINI
#
# VCC = 3.3V
# GND = GND
# OUT = A0
#For Example
# import machine
# import machine
# adc=ADC(0)
# print(adc.read())
#
#
#///////////////////////////////////////

import time
from machine import Pin,ADC


while 1:
adc=ADC(0)
print(adc.read())
time.sleep(1)

另求各位大侠帮我研究一下ESP8266做个红外遥控器,我家的电视遥控器丢了
 

头像
shaoziyang
帖子: 2372
注册时间: 2019年 10月 21日 13:48

Re: 冒个泡 前段时间做机床 改了一个AS5600角度传感器的库 有需要的吗

#2

帖子 shaoziyang » 2020年 12月 14日 22:23

发帖时,用工具栏第三行的插入程序片段(第7个按钮)功能,代码格式就不会变。

sf5a1
帖子: 10
注册时间: 2019年 12月 5日 13:53

Re: 冒个泡 前段时间做机床 改了一个AS5600角度传感器的库 有需要的吗

#3

帖子 sf5a1 » 2020年 12月 22日 16:44

好的 领导   
我太懒了   就不立新帖了   直接再发一遍哦  嘿嘿

代码: 全选

#///////////////////////////////////////#
#        2020-11-26
#
#   Micropython  Esp8266 MINI
#
#   SDA = 4  D2
#   SCL = 5  D1
#For Example
#                   import as5600
#                   Test = as5600.AS5600()
#                   Value_AS5600 = Test.printtf()
#                   print(Value_AS5600)
#///////////////////////////////////////

import time
import machine
from machine import I2C , Pin


Y_SCK_PIN=5
Y_SDA_PIN=4

class AS5600:
  def __init__(self):
    #self.Y_SCK_PIN=5
    #self.Y_SDA_PIN=4
    self.address=0x36
    self.i2c = I2C(scl=machine.Pin(Y_SCK_PIN),sda=machine.Pin(Y_SDA_PIN),freq=100000)
    self.Y_SCK_H()
    self.Y_SDA_H()
    
  def Y_SCK_H(self):
    pin = machine.Pin(Y_SCK_PIN, machine.Pin.OUT)
    pin.value(1)
  def Y_SCK_L(self):
    pin = machine.Pin(Y_SCK_PIN, machine.Pin.OUT)
    pin.value(0)
  def Y_SDA_H(self):
    pin = machine.Pin(Y_SDA_PIN, machine.Pin.OUT)
    pin.value(1)
  def Y_SDA_L(self):
    pin = machine.Pin(Y_SDA_PIN, machine.Pin.OUT)
    pin.value(0)
  
  def Y_READ_SDA(self):
    pin = machine.Pin(Y_SDA_PIN,machine.Pin.IN)
    return pin.value()
  
  def Y_IIC_Start(self):
    self.Y_SDA_H()
    self.Y_SCK_H()
    time.sleep_us(5)
    self.Y_SDA_L()       
    time.sleep_us(5)
    self.Y_SCK_L()
    time.sleep_us(2)
    
  def  Y_IIC_Stop(self):
    self.Y_SCK_L()             
    self.Y_SDA_L()            
    time.sleep_us(5)
    self.Y_SCK_H()
    time.sleep_us(5)       
    self.Y_SDA_H()        
    time.sleep_us(5)       
  
  def Y_IIC_Wait_Ack(self):
    ucErrTime=0
    self.Y_SDA_H()
    time.sleep_us(2) 
    self.Y_SCK_H()
    time.sleep_us(2)  
    while(self.Y_READ_SDA()):
      ucErrTime=ucErrTime+1
      if(ucErrTime&gt;250):
        self.Y_IIC_Stop()
        return 1
    self.Y_SCK_L()             
    time.sleep_us(2)  
    return 0 
    
  def Y_IIC_Ack(self):
    self.Y_SCK_L()               
    self.Y_SDA_L()           
    time.sleep_us(2)
    self.Y_SCK_H()
    time.sleep_us(5)      
    self.Y_SCK_L()           
    time.sleep_us(2)      
    self.Y_SDA_H()
    time.sleep_us(2)
  def Y_IIC_Nack(self):
    self.Y_SCK_L()          
    self.Y_SDA_H()          
    time.sleep_us(2)
    self.Y_SCK_H()
    time.sleep_us(5)      
    self.Y_SCK_L()           
    time.sleep_us(2)    
    self.Y_SDA_H() 
    time.sleep_us(2)

  def Y_IIC_SendByte(self, Txdata):
    #u8 i
    for i in range(0,8):
      self.Y_SCK_L()           
      if(Txdata &amp; 0x80): 
        self.Y_SDA_H()          
      else:
        self.Y_SDA_L()
      time.sleep_us(5) 
      self.Y_SCK_H()     
      time.sleep_us(5) 
      self.Y_SCK_L()
      Txdata&lt;&lt;=1
      time.sleep_us(5)
    time.sleep_us(2)  
    self.Y_SDA_H()
    time.sleep_us(2)
  def Y_IIC_ReadByte(self, ack):
    receive=0  
    time.sleep_us(2)
    self.Y_SDA_H() 
    time.sleep_us(2) 
    for i in range(0,8):
      self.Y_SCK_L()     
      time.sleep_us(5)
      self.Y_SCK_H()     
      receive &lt;&lt;=1
      time.sleep_us(5)
    #pinMode(Y_SDA_PIN, INPUT); 
      if (self.Y_READ_SDA()):
        receive |= 0x01
      else:
        receive &amp;= 0xfe
      time.sleep_us(5)
    if ( not ack):
      self.Y_IIC_Nack()
    else:
      self.Y_IIC_Ack()
    self.Y_SCK_L()           
    time.sleep_us(2)
    return receive

  def AS5600_IIC_Write_OneByte(self, deviceaddr, writeaddr, writedata):
    
    self.Y_IIC_Start()
    self.Y_IIC_SendByte(deviceaddr &amp; 0xfe)
    self.Y_IIC_Wait_Ack()
    self.Y_IIC_SendByte(writeaddr)
    self.Y_IIC_Wait_Ack()
    self.Y_IIC_SendByte(writedata)
    self.Y_IIC_Wait_Ack()
    self.Y_IIC_Stop()
    time.sleep_ms(10)               
  def AS5600_IIC_Read_OneByte(self, deviceaddr, readaddr):
    self.Y_IIC_Start()
    self.Y_IIC_SendByte(deviceaddr &amp; 0xfe)
    self.Y_IIC_Wait_Ack()
    self.Y_IIC_SendByte(readaddr)
    self.Y_IIC_Wait_Ack()

    self.Y_IIC_Start()
    self.Y_IIC_SendByte(deviceaddr | 0x01)
    self.Y_IIC_Wait_Ack()
    temp=self.Y_IIC_ReadByte(0)
    self.Y_IIC_Stop()
    return temp
  def printtf(self):
    value =  self.AS5600_IIC_Read_OneByte((0x36&lt;&lt;1),0x0e)
    value &lt;&lt;= 8
    value |= self.AS5600_IIC_Read_OneByte((0x36&lt;&lt;1),0x0f)
    print(value)
    return value
  
#while 1:
 # rr=AS5600()
  #rr.printtf()
  #time.sleep(1)
  
以上是IIC模式
以下是模拟量模式

#///////////////////////////////////////#
#       2020-11-26
#
#   Micropython  Esp8266 MINI
#
#   VCC = 3.3V  
#   GND = GND
#   OUT = A0
#For Example
#                   import machine
#                   import machine
#                   adc=ADC(0)
#                   print(adc.read())
#                   
#                   
#///////////////////////////////////////

import time
from machine import Pin,ADC


while 1:
  adc=ADC(0)
  print(adc.read())
  time.sleep(1)


 

回复

  • 随机主题
    回复总数
    阅读次数
    最新文章