标题:
贡献stm32的16位AD7750驱动代码(SPI2)
[打印本页]
作者:
panfeng881112
时间:
2011-4-5 17:09
标题:
贡献stm32的16位AD7750驱动代码(SPI2)
求点评!求讨论!
void AD7705_SPI_Init(void) // SPI 初始化
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//SPI接口配置
/* Configure SPI2 pins: SCK, MISO and MOSI ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);
SPI_Cmd(SPI2, DISABLE);
/* SPI2 Config -------------------------------------------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8个字节发送接受模式
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //72M 64分频
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
/* Enable SPI2 */
SPI_Cmd(SPI2, ENABLE);
}
void AD7705_Init(void) //AD初始化
{
AD7705_SPI_Init(); //初始化SPI
Delayus(1); //延时1us
AD7705_RESET(); //写1 进行复位
Delayus(1);
AD7705_RW(0x20); //写命令 准备设置时钟寄存器
Delayus(1);
AD7705_RW(0x08); //设置时钟寄存器
Delayus(1);
AD7705_RW(0x10); //写命令 准备设置模式
Delayus(1);
AD7705_RW(0x44); //设置模式 自校准,增益1,单极,无缓冲
SysTickDelay(1); //延时1ms
}
//读写ad7750芯片函数
unsigned short AD7705_RW(unsigned short data)
{
//读取数据放入软FIFO
u16 rdata;
//AD_CS(0);
/* Wait for SPI2 Tx buffer empty */
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
/* Send SPI2 data */
SPI_I2S_SendData(SPI2,data);
/* Wait for SPI2 data reception */
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
/* Read SPI2 received data */
rdata=SPI_I2S_ReceiveData(SPI2);
//AD_CS(1);
return rdata;
}
//写1复位
int AD7705_RESET(void)
{
int i=0;
for(i=0;i<5;i++)
{
SPI_I2S_SendData(SPI2,0xff);
SysTickDelay(1);
}
}
//采集函数
u32 GetAD7705(void)
{
u16 rdata=0,temp=0;
AD7705_Init();
temp=AD7705_RW(0x00);
temp&=0xff;
rdata=AD7705_RW(0x00);
rdata&=0xff;
temp=(temp<<8);
rdata|=temp;
SysTickDelay(1);
return rdata;
}
复制代码
欢迎光临 珩源工控论坛 (http://bbs.hymcu.com/)
Powered by Discuz! 7.2