2020电赛ads1292x模块stm32代码
main.c
为工程的主程序文件夹
stm32f10x_conf.h
参数设置文件,起到应用和库之间界面的作用
用户使用前必须自己修改该文件
也可以修改外部晶振的参数
也可以是用该文件在编译前使能Debug或者Release模式
由不同的define语句组成
stm32f10x_it.c
外设中断函数文件
用户可以加入自己的中断程序代码
对于指向同一个中断向量的多个不同中断请求,可以利用函数通过判断外设的中断标志位来确定准确的中断源
库函数提供了这些函数的名称
stm3210x_it.h
该头文件包含了所有中断处理程序的原型。
单片机源程序如下:
- /*ADS1292R驱动程序*/
- #include"ADS1292R.H"
- /**初始化ADS1292R**/
- void ADS_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- SPI_InitTypeDef SPI_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- EXTI_InitTypeDef EXTI_InitStructure;
- /*ADS1292用到的IO口初始化*/
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//最高输出速率50MHz
- GPIO_InitStructure.GPIO_Pin = ADS_PWDN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
- GPIO_Init(ADS_PWDN_G, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//最高输出速率50MHz
- GPIO_InitStructure.GPIO_Pin = ADS_GPIO2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
- GPIO_Init(ADS_GPIO2_G, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//最高输出速率50MHz
- GPIO_InitStructure.GPIO_Pin = ADS_DRDY;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
- GPIO_Init(ADS_DRDY_G, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//最高输出速率50MHz
- GPIO_InitStructure.GPIO_Pin = ADS_CS;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
- GPIO_Init(ADS_CS_G, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//最高输出速率50MHz
- GPIO_InitStructure.GPIO_Pin = ADS_SCLK;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
- GPIO_Init(ADS_SCLK_G, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//最高输出速率50MHz
- GPIO_InitStructure.GPIO_Pin = ADS_DOUT;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
- GPIO_Init(ADS_DOUT_G, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//最高输出速率50MHz
- GPIO_InitStructure.GPIO_Pin = ADS_DIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
- GPIO_Init(ADS_DIN_G, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//最高输出速率50MHz
- GPIO_InitStructure.GPIO_Pin = ADS_START;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
- GPIO_Init(ADS_START_G, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//最高输出速率50MHz
- GPIO_InitStructure.GPIO_Pin = ADS_GPIO1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
- GPIO_Init(ADS_GPIO1_G, &GPIO_InitStructure);
- /*SPI接口初始化*/
- SPI_Cmd(SPI1,DISABLE);
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //双线双向全双工
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master;//SPI主模式
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;//8bit数据
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;//CLK空闲时为高电平
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;//数据捕获于第二个时钟沿
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//片选用软件控制
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;//SPI频率
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//高位在前
- SPI_InitStructure.SPI_CRCPolynomial = 7;//crc7,stm32spi带硬件ecc
- SPI_Init(SPI1, &SPI_InitStructure);
- SPI_Cmd(SPI1,ENABLE);
- /*DRDY中断接收初始化
- NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;//选择中断通道3
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//抢占式中断优先级设置为1
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//响应式中断优先级设置为1
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中断
- NVIC_Init(&NVIC_InitStructure);
- EXTI_ClearITPendingBit(EXTI_Line3);//清除中断标志
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource3);//选择管教
- EXTI_InitStructure.EXTI_Line = EXTI_Line3; //选择中断线路3
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //设置为中断请求,非事件请求
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //Falling设置中断触发方式为下降沿触发
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;//外部中断使能
- EXTI_Init(&EXTI_InitStructure);*/
- }
- /**ADS1292R上电复位 **/
- void ADS_PowerOnInit(void)
- { unsigned char sj[9];
- ADS_START_H;
- ADS_CS_H;
- ADS_PWDN_L;//进入掉电模式
- delay_ms(1000);
- ADS_PWDN_H;//退出掉电模式
- delay_ms(1000);//等待稳定
- ADS_PWDN_L;//发出复位脉冲
- delay_us(10);
- ADS_PWDN_H;
- delay_ms(1000);//等待稳定,可以开始使用ADS1292R
- ADS_START_L;
- ADS_CS_L;
- ADS_SPI(SDATAC);//发送停止连续读取数据命令
- ADS_REG(WREG|CONFIG2,0XA0); //使用内部参考电压
- delay_ms(10);//等待内部参考电压稳定
- ADS_REG(WREG|CONFIG1,0X02); //设置转换速率为500SPS
- ADS_REG(WREG|CH1SET,0X01); //设置通道1为短路
- ADS_REG(WREG|CH2SET,0X01); //设置通道2为短路
- sj[1]=ADS_REG(RREG|CONFIG1,0X00);
- sj[2]=ADS_REG(RREG|CH1SET,0X00);
- sj[3]=ADS_REG(RREG|CH2SET,0X00);
- printf("%X ",sj[1]);
- printf("%X ",sj[2]);
- printf("%X \n",sj[3]);
- /* ADS_SPI(RDATAC);//回到连续读取数据模式,检测噪声数据
- ADS_START_H;//启动转换
- while(GPIO_ReadInputDataBit(ADS_DRDY_G,ADS_DRDY)==SET);
- ADS_Read(sj);
- ADS_START_L;
- ADS_SPI(SDATAC);
- ADS_REG(WREG|CONFIG2,0XA0);
- ADS_REG(WREG|CH1SET,0X80);
- ADS_REG(WREG|CH2SET,0X00);
- ADS_SPI(RDATAC);
- ADS_START_H; */
-
-
- }
- /**对ADS1292R内部寄存器进行操作 **/
- unsigned char ADS_REG(unsigned char com,unsigned data)
- {
- unsigned char i;
- for(i=0;i<45;i++);
- ADS_SPI(com);
- for(i=0;i<45;i++);
- ADS_SPI(0X00);
- for(i=0;i<45;i++);
- if((com&0x20)==0x20)//判断是否为读寄存器指令
- { ADS_SPI(0X00);
- for(i=0;i<45;i++);
- }
- return (ADS_SPI(data));
- }
- /**通过SPI总线与ADS1292R通信**/
- unsigned char ADS_SPI(unsigned char com)
- {
- while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);//检测发送寄存器是否为空,上一个数据发送完成
- SPI_I2S_SendData(SPI1, com);//发送数据
- while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);//检测接收标志是否非空,发送完成
- return (SPI_I2S_ReceiveData(SPI1)); //返回接收到的数据
- }
- /*读取72位的数据1100+LOFF_STAT[4:0]+GPIO[1:0]+13个0+2CHx24位,共9字节*/
- void ADS_Read(unsigned char *data)
- { unsigned char i;
- for(i=0;i<9;i++)
- { *data=ADS_SPI(0X00);
- data++;
- }
- }
复制代码
所有资料51hei提供下载:
ADS1292R.zip
(10.91 KB, 下载次数: 34)
|