如题,这两天用
STM32F103C8T6采集ATT7053BU的数据,结果采集上来的数据始终固定不变,我传不同的寄存器地址,他的数据都不变,跪求懂得大神来围观围观,知道知道小弟,感激不尽!
这是我的代码的主要部分,附图是我用串口发出来以后全是一样的数据,附件是我的工程。跪求大神帮忙看看。
- /**********************************************************************
- //函数名称:Init_ATT7053(void)
- //功能 :初始化ATT7053
- **********************************************************************/
- void Init_ATT7053(void)
- {
- int j;
-
- <span style="line-height: 27.2000007629395px;"> </span>SPI_configuration();
-
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_ResetBits(CS_PORT, CS_PIN); //片选拉低
- <span style="line-height: 27.2000007629395px;"> </span>for (j=50; j>0; j--);
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_SetBits(CS_PORT, CS_PIN); //片选拉高
- <span style="line-height: 27.2000007629395px;"> </span>for (j=50; j>0; j--);
- <span style="line-height: 27.2000007629395px;"> </span>SPI_Write(0x33,0x0055); //0x33:软件复位寄存器。0x55:复位
- <span style="line-height: 27.2000007629395px;"> </span>SPI_Write(0x40,0x2000); //0x40:EMU配置寄存器。0x2000:bit13=1,能量寄存器读后清零
- <span style="line-height: 27.2000007629395px;"> </span>SPI_Write(0x43,0x0007); //ADC开关寄存器,I1,I2,U全部打开
-
- <span style="line-height: 27.2000007629395px;"> </span>SPI_Write(0x59,0x0001); //设置电流 电压增益
- <span style="line-height: 27.2000007629395px;"> </span>SPI_Write(0x41,0x0000); //设置电流 电压增益 -------------->0x41,page28,时钟配置寄存器
- }
复制代码- //配置引脚接口
- void SPI_configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- <span style="line-height: 27.2000007629395px;"> </span>RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);//使能GPIOA、GPIOB
- <span style="line-height: 27.2000007629395px;"> </span>///GPIO_InitStructure.GPIO_Pin =RST_5463_PIN | SCLK_PIN | SDI_PIN;
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_InitStructure.GPIO_Pin = SCLK_PIN;
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出:即通用IO口输出功能
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_Init(GPIOA, &GPIO_InitStructure);
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_InitStructure.GPIO_Pin =CS_PIN;
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_Init(GPIOA, &GPIO_InitStructure);
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_InitStructure.GPIO_Pin = SDI_PIN;
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_Init(GPIOA, &GPIO_InitStructure);
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_InitStructure.GPIO_Pin =SDO_PIN;
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- <span style="line-height: 27.2000007629395px;"> </span>GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
复制代码- //写函数
- void SPI_Write(u8 address, u32 write_data)
- {
- char i;
- int j;
- u32 temp_data = 1;
-
- GPIO_SetBits(CS_PORT, CS_PIN);
- for (j=50; j>0; j--);
- GPIO_ResetBits(SCLK_PORT, SCLK_PIN); //确保CLK初始状态是0
- for (j=50; j>0; j--);
- GPIO_ResetBits(CS_PORT, CS_PIN); //片选置0,生效
-
- address=address|0x80;
- for(i=8; i>0; i--)
- {
- GPIO_SetBits(SCLK_PORT, SCLK_PIN);
- for (j=50; j>0; j--);
-
- if(((address & (0x01<<(i-1)))>>(i-1))) //address 地址从0x00到0x7F
- {
- GPIO_SetBits(SDI_PORT, SDI_PIN); //1表示主机向从机写数据
- for (j=50; j>0; j--);
- }
- else
- {
- GPIO_ResetBits(SDI_PORT, SDI_PIN);
- for (j=50; j>0; j--);
- }
-
- GPIO_ResetBits(SCLK_PORT, SCLK_PIN); //从机需要在下降沿取数据
- for (j=50; j>0; j--);
- }
-
- for (j=50; j>0; j--);
-
- for(i=23;i>0;i--)
- {
- GPIO_SetBits(SCLK_PORT, SCLK_PIN);
- for (j=50; j>0; j--);
-
- if(((write_data & (temp_data<<i))>>i)) //address 地址从0x00到0x7F
- {
- GPIO_SetBits(SDI_PORT, SDI_PIN);
- for (j=50; j>0; j--);
- }
- else
- {
- GPIO_ResetBits(SDI_PORT, SDI_PIN);
- for (j=50; j>0; j--);
- }
-
- GPIO_ResetBits(SCLK_PORT, SCLK_PIN); //从机需要在下降沿取数据
- for (j=50; j>0; j--);
- }
- GPIO_SetBits(CS_PORT, CS_PIN);
-
- }
复制代码
- //读函数
- u32 SPI_Read(u8 address)
- {
- char i;
- int j;
- u32 temp_data;
- u32 miso_val;
-
- temp_data = 0;
-
- GPIO_SetBits(CS_PORT,CS_PIN); //片选初始状态为1
- for (j=50; j>0; j--);
- GPIO_ResetBits(SCLK_PORT, SCLK_PIN); //确保CLK初始状态是0
- for (j=50; j>0; j--);
- GPIO_ResetBits(CS_PORT, CS_PIN); //片选置0,生效
- for (j=50; j>0; j--)
- {
-
- }
-
- address=address&0x7F;
- for(i=8; i>0; i--)
- {
- GPIO_SetBits(SCLK_PORT, SCLK_PIN); //CLK置高
- for (j=50; j>0; j--);
-
- if( (address & ( 0x01<<(i-1) )) >> (i-1) ) //address 地址从0x00到0x7F
- {
- GPIO_SetBits(SDI_PORT, SDI_PIN); //1表示主机向从机写数据
- }
- else
- {
- GPIO_ResetBits(SDI_PORT, SDI_PIN);
- }
- for (j=50; j>0; j--);
- GPIO_ResetBits(SCLK_PORT, SCLK_PIN); //CLK置低(下降沿),从机需要在下降沿取数据
- for (j=50; j>0; j--);
- }//从机接收到命令后,开始送出数据,上升沿送数据
- for(i=24; i>0; i--)
- {
- GPIO_SetBits(SCLK_PORT, SCLK_PIN); //
-
- if(GPIO_ReadInputDataBit(SDO_PORT, SDO_PIN))
- miso_val=0x01;
- else
- miso_val=0x00;
-
- temp_data = temp_data|(miso_val<<(i-1));
- for (j=50; j>0; j--);
- GPIO_ResetBits(SCLK_PORT, SCLK_PIN); //从机需要在下降沿取数据
- for (j=50; j>0; j--);
- }
- GPIO_SetBits(CS_PORT, CS_PIN);
- return temp_data;
- }
复制代码- /*
- * 函数名:main
- * 描述 :主函数
- * 输入 : 无
- * 输出 :无
- */
- int main(void)
- {
- <span style="line-height: 27.2000007629395px;"> </span>u32 temp_data = 0;
-
- <span style="line-height: 27.2000007629395px;"> </span>/* 配置系统时钟为 70M */
- <span style="line-height: 27.2000007629395px;"> </span>SystemInit();
- <span style="line-height: 27.2000007629395px;"> </span>/* USART1 config 9600 8-N-1 */
- <span style="line-height: 27.2000007629395px;"> </span>USART1_Config();
- <span style="line-height: 27.2000007629395px;"> </span>Init_ATT7053(); //Include SPI_configuration.
-
-
- <span style="line-height: 27.2000007629395px;"> </span>while (1){
- <span style="line-height: 27.2000007629395px;"> </span><span style="line-height: 27.2000007629395px;"> </span><span style="line-height: 1.7;">printf("This is test printf
");</span>
复制代码
-
-
-
STM32_USART1_TXD ATT7053BU.rar
下载积分: 积分 -1 分
1.2 MB, 下载次数: 71, 下载积分: 积分 -1 分
一周热门 更多>