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 );
} else {
*buf = IIC_Read_Byte ( 1 ) & 0x001f;
}
len--;
buf++;
}
IIC_Stop();
return 0;
}
float Get_Angle ( void ) {
u8 buf[2] = {0};
u8 i = 0;
float temp = 0;
float temp1 = 0.0;
for ( i = 0; i < 20; i++ ) {
AS5600_Read_Len ( Slave_Addr, Angle_Hight_Register_Addr, 2, buf );
temp1 += ( ( buf[0] << 8 ) | buf[1] );
delay_ms ( 5 );
}
temp = temp1 / 20;
return temp / 4096 * 360;
}
myiic.h
??myiic.h 如下:
#ifndef __I2C_H
#define __I2C_H
#include "sys.h"
#define IIC_SCL PAout(11)
#define IIC_SDA PAout(12)
#define READ_SDA PAin(12)
void IIC_Init ( void );
void SDA_IN ( void );
void SDA_OUT ( void );
void IIC_Start ( void );
void IIC_Stop ( void );
u8 IIC_Wait_Ack ( void );
void IIC_Ack ( void );
void IIC_NAck ( void );
void IIC_Send_Byte ( u8 txd );
u8 IIC_Read_Byte ( u8 ack );
#endif
myiic.c
??myiic.c 如下:
#include "myiic.h"
#include "delay.h"
void IIC_Init ( void ) {
GPIO_InitTypeDef GPIO_Init_Structure;
RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOA, ENABLE );
GPIO_Init_Structure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
GPIO_Init_Structure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init_Structure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init ( GPIOA, &GPIO_Init_Structure );
GPIO_SetBits ( GPIOA, GPIO_Pin_11 | GPIO_Pin_12 );
}
void SDA_IN ( void ) {
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init ( GPIOA, &GPIO_InitStructure );
}
void SDA_OUT ( void ) {
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init ( GPIOA, &GPIO_InitStructure );
}
void IIC_Start ( void ) {
SDA_OUT();
IIC_SDA = 1;
IIC_SCL = 1;
delay_us ( 5 );
IIC_SDA = 0;
delay_us ( 5 );
IIC_SCL = 0;
}
void IIC_Stop ( void ) {
SDA_OUT();
IIC_SDA = 0;
IIC_SCL = 1;
delay_us ( 5 );
IIC_SDA = 1;
delay_us ( 5 );
}
void IIC_Ack ( void ) {
IIC_SCL = 0;
SDA_OUT();
IIC_SDA = 0;
delay_us ( 5 );
IIC_SCL = 1;
delay_us ( 5 );
IIC_SCL = 0;
}
void IIC_NAck ( void ) {
IIC_SCL = 0;
SDA_OUT();
IIC_SDA = 1;
delay_us ( 5 );
IIC_SCL = 1;
delay_us ( 5 );
IIC_SCL = 0;
}
u8 IIC_Wait_Ack ( void ) {
u8 ucErrTime = 0;
IIC_SDA = 1;
delay_us ( 5 );
SDA_IN();
IIC_SCL = 1;
delay_us ( 5 );
while ( READ_SDA ) {
ucErrTime++;
if ( ucErrTime > 250 ) {
return 1;
}
}
IIC_SCL = 0;
return 0;
}
void IIC_Send_Byte ( u8 txd ) {
u8 t;
SDA_OUT();
IIC_SCL = 0;
for ( t = 0; t < 8; t++ ) {
IIC_SDA = ( txd & 0x80 ) >> 7;
if ( ( txd & 0x80 ) >> 7 ) {
IIC_SDA = 1;
} else {
IIC_SDA = 0;
}
txd <<= 1;
delay_us ( 5 );
IIC_SCL = 1;
delay_us ( 5 );
IIC_SCL = 0;
delay_us ( 5 );
}
}
u8 IIC_Read_Byte ( u8 ack ) {
unsigned char i, receive = 0;
SDA_IN();
for ( i = 0; i < 8; i++ ) {
IIC_SCL = 0;
delay_us ( 5 );
IIC_SCL = 1;
receive <<= 1;
if ( READ_SDA ) {
receive++;
}
delay_us ( 5 );
}
if ( !ack ) {
IIC_NAck();
} else {
IIC_Ack();
}
return receive;
}
main.c
??main.c 如下:
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "led.h"
#include "as5600.h"
int main ( void ) {
SystemInit();
delay_init ( 72 );
NVIC_Configuration();
uart_init ( 9600 );
LED_Init();
AS5600_Init();
while ( 1 ) {
float Core_Angle = Get_Angle();
printf ( "磁芯角度是%6.2f\r\n", Core_Angle );
LED1 = !LED1;
delay_ms ( 500 );
}
}
|