中科因仑“3+1”工程特种兵精英论坛
标题:
AD7705在STM32F103RBT6上的移植[硬件SPI]
[打印本页]
作者:
我爱因仑风
时间:
2016-5-29 18:39
标题:
AD7705在STM32F103RBT6上的移植[硬件SPI]
利用STM32硬件SPI 与TM7705 收发数据
/*****************************主程序**************************/
//PA2:CS
//PA5:SCK
//PA6:MISO
//PA7:MOSI
#include "stm32f10x.h"
#include "spi.h"
#include "stdio.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
u16 ReadTM7705(void);
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (unsigned char) ch);// USART1 可以换成 USART2 等
while (!(USART1->SR & USART_FLAG_TXE));
return (ch);
}
int GetKey (void)
{
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* 配置USART1波特率:9600;字长:8bit;停止位:1;奇偶校验:无;关闭硬件流控制模式;开启发送、接收功能 */
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE, ENABLE);//使能串口1中断功能
USART_Cmd(USART1, ENABLE); //使能USART1
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PB.8) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/*********系统中断管理**********/
void NVIC_Configuration(void)
{
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置优先级分组:先占优先级0位,从优先级4位
}
/********配置系统时钟,使能各外设时钟***********/
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA
|RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO
|RCC_APB2Periph_SPI1, ENABLE );
}
/********TM7705初始化函数***********/
void TM7705_Init(void)
{
unsigned char i ;
for(i = 0; i < 100; i++)
{
SPIx_ReadWriteByte(0xFF); //持续DIN高电平写操作,恢复AD7705接口
}
SPIx_ReadWriteByte(0x20) ; //通道1 ,下一个写时钟寄存器
Delay(1000);
SPIx_ReadWriteByte(0x02) ; //写时钟寄存器设置更新速率为200Hz
Delay(1000);
SPIx_ReadWriteByte(0x10) ; //通道1 ,下一个写设置寄存器
Delay(1000);
SPIx_ReadWriteByte(0x44) ; //写设置寄存器 ,设置成双极性、无缓冲、增益为0、滤波器工作、自校准
Delay(1000000);
}
/********读16位数据************/
u16 ReadTM7705_16BitValue(void)
{
unsigned long DataL = 0;
unsigned long DataH = 0;
unsigned long Ret = 0;
DataH = SPIx_ReadWriteByte(0xFF);
DataH = DataH << 8;
DataL = SPIx_ReadWriteByte(0xFF);
Ret = DataH | DataL;
return(Ret) ;
}
/********读取AD7705第一通道数据************/
u16 ReadTM7705(void)
{
unsigned long Ret = 0;
SPIx_ReadWriteByte(0x38) ; //设置读当前通道数据
while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8 == 1))
{
;
}
Ret = ReadTM7705_16BitValue();
return Ret;
}
/********主函数************/
int main(void)
{
u16 b;
float c;
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
USART_Configuration();
SPIx_Init();
TM7705_Init();
while(1)
{
TM7705_Init();
b = ReadTM7705();
c = b*3.3/65535;
printf("%f\n",c);
}
}
/****************************SPI初始化*************************/
#include "spi.h"
//SPI口初始化
//这里针是对SPI1的初始化
void SPIx_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure SPI1 pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure I/O for Flash Chip select */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //SPI CS
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Deselect the FLASH: Chip Select high */
GPIO_SetBits(GPIOA,GPIO_Pin_2);
GPIO_SetBits(GPIOB,GPIO_Pin_8);
/* SPI1 configuration */
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; //数据捕获于第二个时钟沿
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //定义波特率预分频的值:波特率预分频值为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寄存器
//SPI1->CR1|=1<<6; //SPI设备使能
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE); //使能SPI外设
SPIx_ReadWriteByte(0xff);//启动传输
}
//SPIx 读写一个字节
//TxData:要写入的字节
//返回值:读取到的字节
u8 SPIx_ReadWriteByte(u8 TxData)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //检查指定的SPI标志位设置与否:发送缓存空标志位
/* Send byte through the SPI1 peripheral */
SPI_I2S_SendData(SPI1, TxData); //通过外设SPIx发送一个数据
/* Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); //检查指定的SPI标志位设置与否:接受缓存非空标志
/* Return the byte read from the SPI bus */
GPIO_SetBits(GPIOA,GPIO_Pin_2);
return SPI_I2S_ReceiveData(SPI1); //返回通过SPIx最近接收的数据
}
心得总结:
1.CS片选在硬件SPI中非常重要,CS在模拟SPI中可以省略,这也是前期一直不出结果的原因。
2.DRDY脚应该设置成上拉输入,MOSI/MISO/SCLK/CE设置成浮空输入
3.当模块数据准备好后,DRDY脚会产生低电平,单片机检测到后即可读取数据。
4.该模块的采样速度200Hz
5.STM32的硬件中有一个NSS脚,这个脚应该设置成软件管理
6.TM7705(国产天微公司)系AD7705的山寨版,性能略差
本人不喜欢无图,因为无图无J8。
希望对大家有帮助
欢迎指正
欢迎光临 中科因仑“3+1”工程特种兵精英论坛 (http://bbs.enlern.com/)
Powered by Discuz! X3.4