分享到:
做CS5532的驱动的时候,发现好多网上的代码不全,这个底层驱动基本都是自己写的,可以完美驱动CS5532,常用的寄存器配置都做了宏的定义。可以完美移植到其他MCU中。
#ifndef __CS5532_H__ #define __CS5532_H__ #include "stm32f0xx_hal.h" #include "main.h" #include "com.h" //SDI 从器件输入,主器件输出 //SDO 从器件输出,主器件输入 #define Set_CS5532_SCLK_High() HAL_GPIO_WritePin(CS5532_SCK_GPIO_Port,CS5532_SCK_Pin,GPIO_PIN_SET) #define Set_CS5532_SCLK_Low() HAL_GPIO_WritePin(CS5532_SCK_GPIO_Port,CS5532_SCK_Pin,GPIO_PIN_RESET) #define Set_CS5532_SDI_High() HAL_GPIO_WritePin(CS5532_SDI_GPIO_Port,CS5532_SDI_Pin,GPIO_PIN_SET) #define Set_CS5532_SDI_Low() HAL_GPIO_WritePin(CS5532_SDI_GPIO_Port,CS5532_SDI_Pin,GPIO_PIN_RESET) #define Read_CS5532_SDO_Data() HAL_GPIO_ReadPin(CS5532_SDO_GPIO_Port,CS5532_SDO_Pin) //The CS5532-ASZ comm define #define SYNC1 0xFF #define SYNC0 0xFE #define CS5532_NULL 0x00 #define W_ALL_OFFSET_REG 0x41 #define R_ALL_OFFSET_REG 0x49 #define W_ALL_GAIN_REG 0x42 #define R_ALL_GAIN_REG 0x4A #define W_ALL_SETUP_REG 0x45 #define R_ALL_SETUP_REG 0x4D #define W_OFFSET_REG1 0x01 #define R_OFFSET_REG1 0x09 #define W_OFFSET_REG2 0x11 #define R_OFFSET_REG2 0x19 #define W_OFFSET_REG3 0x21 #define R_OFFSET_REG3 0x29 #define W_OFFSET_REG4 0x31 #define R_OFFSET_REG4 0x39 #define W_GAIN_REG1 0x02 #define R_GAIN_REG1 0x0A #define W_GAIN_REG2 0x12 #define R_GAIN_REG2 0x1A #define W_GAIN_REG3 0x22 #define R_GAIN_REG3 0x2A #define W_GAIN_REG4 0x32 #define R_GAIN_REG4 0x3A #define W_SETUP_REG1 0x05 #define R_SETUP_REG1 0x0D #define W_SETUP_REG2 0x15 #define R_SETUP_REG2 0x1D #define W_SETUP_REG3 0x25 #define R_SETUP_REG3 0x2D #define W_SETUP_REG4 0x35 #define R_SETUP_REG4 0x3D #define W_CONFIG_REG 0x03 #define R_CONFIG_REG 0x0B #define START_SINGLE (0x80) #define START_CONTINUOUS (0xC0) #define SETUP1 (0x00<<3) #define SETUP2 (0x01<<3) #define SETUP3 (0x02<<3) #define SETUP4 (0x03<<3) #define SETUP5 (0x04<<3) #define SETUP6 (0x05<<3) #define SETUP7 (0x06<<3) #define SETUP8 (0x07<<3) // Configuration Register #define STANDBY_MODE (0x00L<<31)// #define SLEEP_MODE (0x01L<<31)// #define POWER_SAVE_MODE (0x01L<<30) // #define NORMAL_MODE (0x00L<<29) // #define SYSTEM_RESET (0x01L<<29) // #define RESET_STATUS (0x01L<<28) // #define NORMAL_INPUTS (0x00L<<27) // #define SHORT_INPUTS (0x01L<<27) // #define VREF_HIGH (0x00L<<25) //2.5V-VA+ #define VREF_LOW (0x01L<<25)// #define CR_A1_0 (0x00L<<24) #define CR_A1_1 (0x01L<<24) #define CR_A0_0 (0x00L<<23) #define CR_A0_1 (0x01L<<23) #define LINE_FREQ_60 (0x00L<<19) // #define LINE_FREQ_50 (0x01L<<19) //5/6 //=== Channel Setup Register === //Channel Select Bits #define SWITCH_CH1 (0x00L<<30) //CS1=0,CS0=0 #define SWITCH_CH2 (0x01L<<30) //CS1=0,CS0=1 //Gain Bits #define Gain1 (0x00L<<27) #define Gain2 (0x01L<<27) #define Gain4 (0x02L<<27) #define Gain8 (0x03L<<27) #define Gain16 (0x04L<<27) #define Gain32 (0x05L<<27) #define Gain64 (0x06L<<27) //Converter Speed FRS=0 #define DATARATE_100 (0x00L<<23) // #define DATARATE_50 (0x01L<<23) #define DATARATE_25 (0x02L<<23) #define DATARATE_12P5 (0x03L<<23) #define DATARATE_6P25 (0x04L<<23) #define DATARATE_3200 (0x08L<<23) #define DATARATE_1600 (0x09L<<23) #define DATARATE_800 (0x0aL<<23) #define DATARATE_400 (0x0bL<<23) #define DATARATE_200 (0x0cL<<23) //power mode #define BIPOLAR_MODE (0x00L<<22) // #define UNIPOLAR_MODE (0x01L<<22)// //=== Gain Register === #define GAIN_32 (0x01L<<29) #define GAIN_16 (0x01L<<28) #define GAIN_8 (0x01L<<27) #define GAIN_4 (0x01L<<26) #define GAIN_2 (0x01L<<25) #define GAIN_1 (0x01L<<24) #define AD_CH2 START_SINGLE+SETUP1 #define AD_CH1 START_SINGLE+SETUP3 void WriteCs5532Byte(uint8_t da); uint8_t ReadCs5532Byte(void); void WriteCs5532Dword(uint32_t da); extern uint8_t ReadCs5532AD(uint8_t ADCHANNEL,uint32_t *out); extern void cs5532_init(void); #endif
#include "cs5532.h" /********************************************************************************************** // 函数: void WriteCs5532Byte(uint8_t da) // 描述: 给CS5532写1个字节的数据 // 参数: 给CS5532写入的数据,1个字节 // 返回: non // 说明: non // 版本: V1.0, 2018-09-07 ************************************************************************************************/ void WriteCs5532Byte(uint8_t da) { uint8_t i; Set_CS5532_SCLK_Low(); for(i=0;i<8;i++) { if((da&0x80)==0x80) { Set_CS5532_SDI_High(); } else { Set_CS5532_SDI_Low(); } Set_CS5532_SCLK_High(); __NOP(); __NOP(); Set_CS5532_SCLK_Low(); __NOP(); __NOP(); da = da<<1; } } /********************************************************************************************** // 函数: uint8_t ReadCs5532Byte(void) // 描述: CS5532读出一个字节的数据 // 参数: non // 返回: CS5532读出的一个字节数据 // 说明: non // 版本: V1.0, 2018-09-07 ************************************************************************************************/ uint8_t ReadCs5532Byte(void) { uint8_t i,dat; Set_CS5532_SCLK_Low(); for(i=0;i<8;i++) { dat = dat<<1; if(Read_CS5532_SDO_Data() == GPIO_PIN_SET) { dat = dat|0x01; } Set_CS5532_SCLK_High(); __NOP(); __NOP(); Set_CS5532_SCLK_Low(); __NOP(); __NOP(); } return dat; } /********************************************************************************************** // 函数: void WriteCs5532Dword(uint32_t da) // 描述: 给CS5532写4个字节的数据 // 参数: 给CS5532写入的数据,long类型,32位数据 // 返回: non // 说明: non // 版本: V1.0, 2018-09-07 ************************************************************************************************/ void WriteCs5532Dword(uint32_t da) { union type1 { uint8_t ch[4]; uint32_t lon; }; union type1 temp; temp.lon= da; WriteCs5532Byte(temp.ch[3]); WriteCs5532Byte(temp.ch[2]); WriteCs5532Byte(temp.ch[1]); WriteCs5532Byte(temp.ch[0]); } /********************************************************************************************** // 函数: static uint32_t ReadCs5530(void) // 描述: 读取CS5532的数据 // 参数: 1.选择AD的通道 2.读出的AD数据的地址指针 // 返回: 1:读取AD OK 0:读取AD溢出 // 说明: 参数1:AD_CH1和AD_CH2 // 版本: V1.0, 2018-09-14 ************************************************************************************************/ uint8_t ReadCs5532AD(uint8_t ADCHANNEL,uint32_t *out) { union type1 { uint8_t ch[4]; uint32_t lon; }; static union type1 temp; WriteCs5532Byte(ADCHANNEL); while(Read_CS5532_SDO_Data() != GPIO_PIN_RESET); Set_CS5532_SDI_Low(); WriteCs5532Byte(CS5532_NULL); temp.ch[3] = ReadCs5532Byte(); temp.ch[2] = ReadCs5532Byte(); temp.ch[1] = ReadCs5532Byte(); temp.ch[0] = ReadCs5532Byte(); // UART1_Waite_SendByte(temp.ch[0]); if((temp.ch[0]&0x04)==0x04) { return 0;// } temp.lon = temp.lon>>8; *out = temp.lon; return 1; } /********************************************************************************************** // 函数: static void cs5532_init(void) // 描述: CS5530的初始化,固定速度为15sps // 参数: non // 返回: non // 说明: non // 版本: V1.0, 2018-09-07 ************************************************************************************************/ void cs5532_init(void) { uint8_t loop; uint32_t da; // // //延时20ms,等待晶振启动 HAL_Delay(20); // //发送ADC串口复位指令,初始化ADC到可访问状态 // //发送SYNC1命令字,至少15个,这里发送30个 for (loop=0;loop<30;loop++) { WriteCs5532Byte(SYNC1); } // //发送一个SYNC0命令字 WriteCs5532Byte(SYNC0); // //启动系统复位 WriteCs5532Byte(W_CONFIG_REG);//写配置寄存器 WriteCs5532Dword(SYSTEM_RESET); HAL_Delay(20); //直接延时,不进行复位成功的循环检测 //// 复位后读取配置寄存器的时候需要连续读两次,第一次RV为1,第二次RV为0 WriteCs5532Byte(R_CONFIG_REG);//读配置寄存器 ReadCs5532Byte(); ReadCs5532Byte(); ReadCs5532Byte(); ReadCs5532Byte(); WriteCs5532Byte(R_CONFIG_REG);//读配置寄存器 ReadCs5532Byte(); ReadCs5532Byte(); ReadCs5532Byte(); ReadCs5532Byte(); WriteCs5532Byte(W_CONFIG_REG);//写配置寄存器 // WriteCs5532Dword(NORMAL_MODE+LINE_FREQ_50+NORMAL_INPUTS); //正常输入,FRS=1 WriteCs5532Dword(NORMAL_MODE+LINE_FREQ_50+SHORT_INPUTS); //正常输入,FRS=1 WriteCs5532Byte(W_SETUP_REG1);//写通道设置寄存器1,物理通道1,速度12.5sps,增益4 WriteCs5532Dword(SWITCH_CH1+Gain1+DATARATE_12P5+UNIPOLAR_MODE); // WriteCs5532Byte(W_SETUP_REG2);//写通道设置寄存器2,物理通道2,速度12.5sps,增益4 WriteCs5532Dword(SWITCH_CH2+Gain1+DATARATE_12P5+UNIPOLAR_MODE); // } /********************************************************************************************** // 函数: uint8_t TestChannelA(void) // 描述: 读取CS5532的通道A状态 // 参数: non // 返回: 1.CS5532通道A正常 0.CS5532通道A异常 // 说明: 物理通道0 // 版本: V1.0, 2018-09-17 ************************************************************************************************/ uint8_t TestChannelA(void) { uint32_t da; if(ReadCs5532AD(AD_CH1,&da) == 1) { if((da&0x00000007) == 0x00000000) { return 1; } } return 0; } /********************************************************************************************** // 函数: uint8_t TestChannelB(void) // 描述: 读取CS5532的通道B状态 // 参数: non // 返回: 1.CS5532通道A正常 0.CS5532通道A异常 // 说明: 物理通道1 // 版本: V1.0, 2018-09-17 ************************************************************************************************/ uint8_t TestChannelB(void) { uint32_t da; if(ReadCs5532AD(AD_CH2,&da) == 1) { if((da&0x00000007) == 0x00000001) { return 1; } } return 0; }
(0 )
(0 )
回复
举报
- 0000000000000000
-
1888 发帖7917 回复34980 积分
- 私信他 +关注
发表回复
块
导
航
举报
请选择举报类别
- 广告垃圾
- 违规内容
- 恶意灌水
- 重复发帖