as5600.h
as5600.h
如下:
#ifndef __AS5600__
#define __AS5600__
#include "sys.h"
#define Slave_Addr 0x36
#define Write_Bit 0
#define Read_Bit 1
#define Angle_Hight_Register_Addr 0x0C
#define Angle_Low_Register_Addr 0x0D
void AS5600_Init ( void );
u16 AS5600_Read_Len ( u8 addr, u8 reg, u8 len, u8 *buf );
float Get_Angle ( void );
#endif
as5600.c
as5600.c
如下:
#include "as5600.h"
#include "myiic.h"
#include "delay.h"
void AS5600_Init ( void ) {
IIC_Init();
}
u16 AS5600_Read_Len ( u8 addr, u8 reg, u8 len, u8 *buf ) {
IIC_Start();
IIC_Send_Byte ( ( addr << 1 ) | Write_Bit );
if ( IIC_Wait_Ack() ) {
IIC_Stop();
return 1;
}
IIC_Send_Byte ( reg );
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte ( ( addr << 1 ) | Read_Bit ); // 发送器件地址 + 读命令
IIC_Wait_Ack(); // 等待应答
while ( len ) {
if ( len == 1 ) {
*buf = IIC_Read_Byte ( 0 ); // 读数据,发送nACK
}