实验器材:
STM32F407开发板
硬件资源:
1,DS0(连接在PF9) LED
2,DS1(连接在PF10)
3.SDA---PB9
4.SCL---PB8
5.SD---PE1
6.ADDR---PE0
7.INTB--- 悬空
FDC2214的stm32单片机源程序如下:
- /*******************************************************************************************
- 开发平台:STM32F407ZET6
- 功能说明:FDC2214传感器测试
- 单 位:湖北汽车工业学院电气与信息工程学院
- *******************************************************************************************/
- #include "sys.h"
- #include "delay.h"
- #include "led.h"
- #include "i2c.h"
- #include "fdc2214.h"
-
- int main(void)
- {
- int result=0;
- u32 temp = 0;
- Stm32_Clock_Init(336,8,2,7);//设置时钟,168Mhz
- delay_init(168); //初始化延时函数
- I2C_Init(); //I2C初始化
- LED_Init(); //初始化LED时钟
- fdc2214_Init(); //FDC2214初始化
- //LED0 = 0;
- while(1)
- {
- LED1 = 0;
-
- temp=FDC_read_16bit(0x1A);
- // temp =
- printf("%d\r\n",result);
-
- delay_ms(500);
- }
- }
复制代码- /*******************************************************************************************
- 开发平台:STM32F407ZET6
- 功能说明:FDC2214传感器读写数据
- 修改日期:
- 单 位:湖北汽车工业学院电气与信息工程学院
- *******************************************************************************************/
- #include "fdc2214.h"
- #include "delay.h"
- #include "i2c.h"
- #include "usart.h"
- #include "led.h"
- #include "FDC_cmd.h"
- //#define slaver_addr 必须声明
- //读Channel x 的转换结果(值)
- u16 FDC_read_CHx(u8 CHx)
- {
- u8 reg_addr1 = 0; //MSB地址
- u8 reg_addr = 0; //LSB地址
- u16 data = 0;//接收到的数据
-
- switch(CHx)
- {
- case 0:
- {
- reg_addr1 = DATA_CH0;
- reg_addr = DATA_LSB_CH0;
- }break;
- case 1:
- {
- reg_addr1 = DATA_CH1;
- reg_addr = DATA_LSB_CH1;
- }break;
- case 2:
- {
- reg_addr1 = DATA_CH2;
- reg_addr = DATA_LSB_CH2;
- }break;
- case 3:
- {
- reg_addr1 = DATA_CH3;
- reg_addr = DATA_LSB_CH3;
- }break;
- // data = ;
- default: return 0; //输入错误
-
- }
-
- //printf("%d \r\n",LCD_read_16bit(reg_addr1));
- //printf("%d \r\n",LCD_read_16bit(reg_addr)); //不知道LSB寄存器有什么用,手册也没见有
- data = FDC_read_16bit(reg_addr1); //读取寄存器值
- data = data & 0x0FFF; //转换结果在 [11:0],把它取出来
-
- return data;
- }
- //fdc2214初始化(寄存器的配置)重点
- void fdc2214_Init()
- {
- I2C_Init(); //I2C初始化
- FDC_SD = 0; // 上电。0---退出睡眠模式,1---进入休眠模式 pin---PE1
- FDC_ADDR = 1; // 选择FDC地址 0---0x2a 1---0x2b pin---PE0
- delay_ms(500); //延时500ms
-
- FDC_write_16bit(RCOUNT_CH0,0x8329);//ADDRESS:0x08
- FDC_write_16bit(RCOUNT_CH1,0x8329);//ADDRESS:0x09
- FDC_write_16bit(RCOUNT_CH2,0x8329);//ADDRESS:0x0A
- FDC_write_16bit(RCOUNT_CH3,0x8329);//ADDRESS:0x0B
-
- FDC_write_16bit(SETTLECOUNT_CH0,0x000A); //ADDRESS:0x10
- FDC_write_16bit(SETTLECOUNT_CH1,0x000A); //ADDRESS:0x11
- FDC_write_16bit(SETTLECOUNT_CH2,0x000A); //ADDRESS:0x12
- FDC_write_16bit(SETTLECOUNT_CH3,0x000A); //ADDRESS:0x13
-
- FDC_write_16bit(CLOCK_DIVIDERS_CH0,0x2002);//ADDRESS:0x14
- FDC_write_16bit(CLOCK_DIVIDERS_CH1,0x2002);//ADDRESS:0x15
- FDC_write_16bit(CLOCK_DIVIDERS_CH2,0x2002);//ADDRESS:0x16
- FDC_write_16bit(CLOCK_DIVIDERS_CH3,0x2002);//ADDRESS:0x17
-
- FDC_write_16bit(ERROR_CONFIG,0x0000); //ADDRESS:0x19
-
- FDC_write_16bit(MUX_CONFIG,0xC20D); //ADDRESS:0x1B
-
- FDC_write_16bit(DRIVE_CURRENT_CH0,0x7C00); //ADDRESS:0x1E
- FDC_write_16bit(DRIVE_CURRENT_CH1,0x7C00); //ADDRESS:0x1F
- FDC_write_16bit(DRIVE_CURRENT_CH2,0x7C00); //ADDRESS:0x20
- FDC_write_16bit(DRIVE_CURRENT_CH3,0x7C00); //ADDRESS:0x21
- FDC_write_16bit(CONFIG,0x1601); //bit13 清零 退出睡眠模式
-
-
- fdc2214_write(CLOCK_DIVIDERS_CH0,0x2001); //设置为单通道模式
- fdc2214_write(DRIVE_CURRENT_CH0,0x7C00); //传感器驱动电流配置
- fdc2214_write(SETTLECOUNT_CH0,0x000A); //传感器建立最短时间
- fdc2214_write(RCOUNT_CH0,0x2089); //计数器寄存器
- fdc2214_write(MUX_CONFIG,0xC20D); //多通道模式
- fdc2214_write(ERROR_CONFIG,0x0000);
- fdc2214_write(CONFIG,0x1401); //使用内部晶振
-
-
- }
-
-
- /* 向 FDC2214 写数据 */
- void fdc2214_write(int dateAddr,int date)
- {
- u16 FDC2214_Address;
-
- unsigned char REG_data_H=0,REG_data_L=0;
- REG_data_H=(unsigned char) (date&0XFF);
- REG_data_L=(unsigned char) ((date&0XFF00)>>8);
-
- I2C_Start();
- I2C_Send_Byte(FDC2214_Address);
- while(I2C_Wait_Ack());
- LED0=0;
-
- I2C_Send_Byte(dateAddr);
- while(I2C_Wait_Ack());
-
- I2C_Send_Byte(REG_data_H);
- while(I2C_Wait_Ack());
-
- I2C_Send_Byte(REG_data_L);
- while(I2C_Wait_Ack());
-
- I2C_Stop();
- }
- /* 从 FDC2214 读数据 */
- unsigned int FDC2214_Read(int firstAddr,int SecondAddr)
- {
- u8 FDC2214_W;
- u8 FDC2214_R;
- unsigned int temp; //定义一个变量,作为28位数据的返回值
- u8 result[4]; //定义4个8位的数据结果
- I2C_Start(); //I2C开始
- I2C_Send_Byte(FDC2214_W); //I2C发送一个字节,对FDC2214进行寻址,并将后续为写操作(FDC2214_W)
- while(I2C_Wait_Ack()); //等待应答
- I2C_Send_Byte(firstAddr); //发送一个字节,选中第一个寄存器(地址),用于存储高12位数据
- while(I2C_Wait_Ack());
- I2C_Stop();
-
- I2C_Start();
- I2C_Send_Byte(FDC2214_R); //I2C发送一个字节,对FDC2214进行寻址,并将后续为读操作(FDC2214_R)
- while(I2C_Wait_Ack());
- result[0]=I2C_Read_Byte(1); //1:有应答
- result[0] = result[0]<<4; //先左移4位,将第一个数据寄存器的高4位(非数据位)移出去
- result[0] = result[0]>>4; //再右移4位
- result[1]=I2C_Read_Byte(1); //temp=高字节+低字节 此时已经将12位数据读出
- I2C_Stop();
-
- I2C_Start();
- I2C_Send_Byte(FDC2214_W); //I2C发送一个字节,对FDC2214进行寻址,并将后续为写操作(FDC2214_W)
- while(I2C_Wait_Ack());
- I2C_Send_Byte(SecondAddr); //发送一个字节,选中第二个寄存器(地址),用于存储低16位数据
- while(I2C_Wait_Ack());
-
- I2C_Start();
- I2C_Send_Byte(FDC2214_R); //I2C发送一个字节,对FDC2214进行寻址,并将后续为读操作(FDC2214_R)
- while(I2C_Wait_Ack()); //接下来读取低16位数据
- result[2]=I2C_Read_Byte(1); //读取出高8位
- result[3]=I2C_Read_Byte(1); //读取出低8位
- //将读取出来的4次数据进行整合
- temp = (unsigned int)(((result[0]<< 24) | (result[1] << 16) | (result[2] << 8) | (result[3] & 0xff))); //将4次读出来的数据进行整合
- return(temp);
- }
复制代码
所有资料51hei提供下载:
FDC2214(LJA).rar
(149.56 KB, 下载次数: 89)
|