完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我把51上的1242例程移植到STM32上,但是一直无法驱动1242,代码如下#ifndef __ADS1242_H__
#define __ADS1242_H__ //头文件的包含 //#include #include "sys.h" #include "spi.h" #include "stdio.h" #include "delay.h" #define uchar unsigned char #define uint unsigned int #define ulong unsigned long //#define _Nop() __nop(); //定义空指令 #define RTD_A 3.9083e-3 #define RTD_B -5.775e-7 /*定义ADS1242的寄存器地址 */ #define ADS1242_SETUP 0x00 #define ADS1242_MUX 0x01 #define ADS1242_ACR 0x02 #define ADS1242_ODAC 0x03 #define ADS1242_DIO 0x04 #define ADS1242_DIR 0x05 #define ADS1242_IOCON 0x06 #define ADS1242_OCR0 0x07 #define ADS1242_OCR1 0x08 #define ADS1242_OCR2 0x09 #define ADS1242_FSR0 0x0a #define ADS1242_FSR1 0x0b #define ADS1242_FSR2 0x0c #define ADS1242_DOR2 0x0d #define ADS1242_DOR1 0x0e #define ADS1242_DOR0 0x0f /*定义ADS1242的寄存器地址 */ #define ADS1242_CMD_RDATA 0x01 #define ADS1242_CMD_RDATAC 0x03 #define ADS1242_CMD_STOPC 0x0f #define ADS1242_CMD_RREG 0x10 #define ADS1242_CMD_WREG 0x50 #define ADS1242_CMD_SELFCAL 0xf0 #define ADS1242_CMD_SELFOCAL 0xf1 #define ADS1242_CMD_SELFGCAL 0xf2 #define ADS1242_CMD_SYSOCAL 0xf3 #define ADS1242_CMD_SYSGCAL 0xf4 #define ADS1242_CMD_WAKEUP 0xfb #define ADS1242_CMD_DSYNC 0xfc #define ADS1242_CMD_SLEEP 0xfd #define ADS1242_CMD_RESET 0xfe /*配置寄存器位*/ #define BUFFER_ON (1<<4) /*ACR寄存器BUFFER启用*/ #define BUFFER_OFF (0<<4) /*ACR寄存器BUFFER不用*/ #define RANGE_0 (0<<2) /*配置ACR寄存器的RANGE位为0*/ #define RANGE_1 (1<<2) /*配置ACR寄存器的RANGE位为1*/ #define SPEED_0 (0<<5) /**/ #define SPEED_1 (1<<5) #define DATARATE_3_75 (0x02) /*配置ACR寄存器的Data Rate为3.75HZ*/ #define DATARATE_7_5 (0x01) /*配置ACR寄存器的Data Rate为7.5HZ*/ #define DATARATE_15 (0x00) /*配置ACR寄存器的Data Rate为15HZ*/ #define PGA_1 (0x00) /*配置SETUP寄存器的PGA为1 */ #define PGA_2 (0x01) /*配置SETUP寄存器的PGA为2 */ #define PGA_4 (0x02) /*配置SETUP寄存器的PGA为4 */ #define PGA_8 (0x03) /*配置SETUP寄存器的PGA为8 */ #define PGA_16 (0x04) /*配置SETUP寄存器的PGA为16 */ #define PGA_32 (0x05) /*配置SETUP寄存器的PGA为32 */ #define PGA_64 (0x06) /*配置SETUP寄存器的PGA为64 */ #define PGA_128 (0x07) /*配置SETUP寄存器的PGA为128 */ #define IO_PDWN GPIO_SetBits(GPIOB, GPIO_Pin_11) //#define IO_PDWN PBout(11) #define IO_DRDY PBin(0) #define IO_SCLK PAout(5) #define IO_DOUT PAin(6) #define IO_DIN PAout(7) #define IO_CS PBout(10) /*定义IO口*/ //***it IO_PDWN= P1^0; //***it IO_DRDY= P1^1; //***it IO_SCLK= P1^2; //***it IO_DOUT= P1^3; //***it IO_DIN= P1^4; //***it IO_CS= P1^5; void ADS1242Init(); /*写一个字节的数据 */ void WriteByte(uchar Dat); /*读一个字节的数据 */ uchar ReadByte(); /*写寄存器 */ void WriteReg(uchar RegAdd,uchar RegDat); /*读寄存器 */ uchar ReadReg(uchar RegAdd); /*选择AD通道 */ void SelectChannel(uchar PosChannel,uchar NegChannel); /*等待AD转换完成 */ void WaitADConComplete(); /*从DOR寄存器读取转换结果 */ ulong ReadDataFromDOR(uchar PosChannel,uchar NegChannel); /*上电自校正*/ void SelfCalibrate(); /*供主程序调用*/ ulong ADS1242Convert(uchar PosChannel,uchar NegChannel); u16 readRTD(int cs); #endif #include "ADS1242.h" #include "delay.h" void ADS1242Init() { SPI1_Init(); IO_PDWN ; IO_DOUT = 0; } /*写一个字节的数据 */ //void SPI_DIN(u8 assert) //{ //if (assert) { //GPIO_ResetBits(GPIOA, GPIO_Pin_7); /* 选中ADS1243 */ //} else { //GPIO_SetBits(GPIOA, GPIO_Pin_7); //} //} void WriteByte(uchar Dat) { char i; IO_CS = 0; IO_SCLK = 0; __nop(); for(i=0;i<8;i++) { // IO_DIN = CY; IO_DIN = Dat&0x80; // SPI_DIN(Dat&0x80); IO_SCLK = 1; Dat = Dat<<1; __nop(); __nop(); IO_SCLK = 0; __nop(); } IO_CS = 1; // SPI_DIN(1); } /*读一个字节的数据*/ uchar ReadByte() { uchar i,RDat; RDat = 0; IO_CS = 0; IO_SCLK = 0; __nop(); for(i=0;i<8;i++) { IO_SCLK = 1; __nop(); RDat = (RDat<<1)|IO_DOUT; IO_SCLK = 0; __nop(); __nop(); // DelayUs2x(2); } IO_CS = 1; return RDat; // return 10000; } /*写寄存器 */ void WriteReg(uchar RegAdd,uchar RegDat) /*寄存器地址,写入数据*/ { WriteByte(ADS1242_CMD_WREG + RegAdd); /*写寄存器命令+寄存器地址*/ WriteByte(0x00); WriteByte(RegDat); } /*读寄存器 */ uchar ReadReg(uchar RegAdd) /*寄存器地址 */ { WriteByte(ADS1242_CMD_RREG + RegAdd); /*读寄存器命令+寄存器地址*/ WriteByte(0x00); return (ReadByte()); } /*选择AD通道 */ void SelectChannel(uchar PosChannel,uchar NegChannel) /*正向通道,负向通道*/ { // WriteReg(ADS1242_MUX,((PosChannel<<4)+NegChannel)); WriteReg(ADS1242_MUX,10); } /*等待AD转换完成 */ void WaitADConComplete() { IO_CS = 0; while(!IO_DRDY); /*DRDY为0表示转换完成*/ while(IO_DRDY); IO_CS = 1; } /*从DOR寄存器读取转换结果 */ ulong ReadDataFromDOR(uchar PosChannel,uchar NegChannel) /*正负通道号*/ { ulong temp; // char i; uchar DataMSB,DataMid,DataLSB; IO_CS = 0; SelectChannel(PosChannel,NegChannel); /*选择通道*/ // WaitADConComplete(); WriteByte(0x01); /*发送读数据命令*/ IO_CS = 0; delay_ms(1); DataMSB = ReadReg(ADS1242_DOR2); DataMid = ReadReg(ADS1242_DOR1); DataLSB = ReadReg(ADS1242_DOR1); /*发送读数据命令*/ IO_CS=1; temp = (DataMSB<<16) + (DataMid<<8) + DataLSB; temp = DataLSB; return temp; } /*上电自校正*/ void SelfCalibrate() { IO_PDWN ; // WriteByte(ADS1242_CMD_RESET); WriteReg(ADS1242_SETUP,PGA_1); WriteReg(ADS1242_ACR,BUFFER_OFF + RANGE_0 + DATARATE_3_75); /*配置ACR寄存器*/ WriteReg(ADS1242_SETUP,PGA_1); WriteReg(ADS1242_ODAC,0x00); /*上电自校正*/ IO_CS = 0; while(IO_DRDY); /*等待校正完成*/ IO_CS = 1; WriteReg(ADS1242_ACR,BUFFER_ON + RANGE_0 + DATARATE_3_75); /*配置ACR寄存器*/ } /*供主程序调用*/ ulong ADS1242Convert(uchar PosChannel,uchar NegChannel) { ulong temp; ADS1242Init(); // SelfCalibrate(); temp = ReadDataFromDOR(PosChannel,NegChannel); temp = ReadDataFromDOR(PosChannel,NegChannel); /*取第二个数据*/ return temp; } float temperature(float RTDnominal, float refResistor,int cs) { float Z1, Z2, Z3, Z4, Rt, temp; float rpoly; // Rt = readRTD(cs); Rt=ReadDataFromDOR(0,1); Rt /= 32768; Rt *= refResistor; Z1 = -RTD_A; Z2 = RTD_A * RTD_A - (4 * RTD_B); Z3 = (4 * RTD_B) / RTDnominal; Z4 = 2 * RTD_B; temp = Z2 + (Z3 * Rt); temp = (sqrt(temp) + Z1) / Z4; if (temp >= 0) return temp; // ugh. rpoly = Rt; temp = -242.02; temp += 2.2228 * rpoly; rpoly *= Rt; // square temp += 2.5859e-3 * rpoly; rpoly *= Rt; // ^3 temp -= 4.8260e-6 * rpoly; rpoly *= Rt; // ^4 temp -= 2.8183e-8 * rpoly; rpoly *= Rt; // ^5 temp += 1.5243e-10 * rpoly; return temp; } /* u16 readRTD(int cs) { // uint8_t t = 0; uint16_t rtd = 0; uint16_t dtemp[2]; // t = readRegister8(MAX31856_CONFIG_REG); // t |= MAX31856_CONFIG_1SHOT; // writeRegister8(MAX31856_CONFIG_REG, t); // delay_ms(100); //单次转换读取时间在片选信号拉高后在50HZ工作模式下需要约62ms,60hz约52ms dtemp[0]=ReadReg(0x01); dtemp[1]=ReadReg(0x02); rtd=(dtemp[0]<<8)+(dtemp[1]); // rtd = readRegister16(MAX31856_RTDMSB_REG); // remove fault rtd >>= 1; return rtd; } */ #include "spi.h" ////////////////////////////////////////////////////////////////////////////////// //本程序只供学习使用,未经作者许可,不得用于其它任何用途 //Mini STM32开发板 //SPI 驱动函数 //正点原子@ALIENTEK //技术论坛:www.openedv.com //修改日期:2010/6/13 //版本:V1.0 //版权所有,盗版必究。 //Copyright(C) 正点原子 2009-2019 //All rights reserved ////////////////////////////////////////////////////////////////////////////////// //以下是SPI模块的初始化代码,配置成主机模式,访问SD Card/W25X16/24L01/JF24C //SPI口初始化 //这里针是对SPI1的初始化 SPI_InitTypeDef SPI_InitStructure; void SPI1_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA|RCC_APB2Periph_SPI1, ENABLE ); /* GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 |GPIO_Pin_7 |GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOA,&GPIO_InitStructure); /*设置SPI2引脚: SCK, MOSI*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA,&GPIO_InitStructure); /***********************************************/ /* Configure PB12 pin: DRDY pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; /* 浮空输入 */ GPIO_Init(GPIOB, &GPIO_InitStructure); // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /* 推挽输出 */ // GPIO_Init(GPIOB, &GPIO_InitStructure); //GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7); GPIO_SetBits(GPIOB,GPIO_Pin_10|GPIO_Pin_2); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工 SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构 SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //选择了串行时钟的稳态:时钟悬空高 SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //数据捕获于第2个时钟沿 SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制 SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //定义波特率预分频的值:波特率预分频值为256 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始 SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式 SPI_Init(SPI1, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器 SPI_Cmd(SPI1, ENABLE); //使能SPI外设 //SPI1_ReadWriteByte(0xff);//启动传输 } |
|
相关推荐
4个回答
|
|
看看时序是不是和51一样
|
|
|
|
程序里没看到对PB11的设置,程序引脚设置是否与实际电路相符合
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1967 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1823 浏览 3 评论
4403 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1970 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
2477 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-19 04:20 , Processed in 0.592200 second(s), Total 55, Slave 48 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号