void SPI1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1 | RCC_APB2Periph_AFIO, ENABLE);
/* 初始化SCK、MISO、MOSI引脚 */
GPIO_InitStructure.GPIO_Pin = AD_CLK_PIN | AD_SI_PIN | AD_SO_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(AD_PORT, &GPIO_InitStructure);
GPIO_SetBits(AD_PORT,AD_CLK_PIN | AD_SI_PIN | AD_SO_PIN);;
/* 初始化配置STM32 SPI1 */
SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex; //SPI设置为双线双向全双工
SPI_InitStructure.SPI_Mode=SPI_Mode_Master; //设置为主SPI
SPI_InitStructure.SPI_DataSize=SPI_DataSize_16b; //SPI发送接收16位帧结构 关键!!!
SPI_InitStructure.SPI_CPOL=SPI_CPOL_Low; //时钟悬空低
SPI_InitStructure.SPI_CPHA=SPI_CPHA_1Edge; //数据捕获于第1个时钟沿
SPI_InitStructure.SPI_NSS=SPI_NSS_Soft; //NSS由外部管脚管理
SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_32; //波特率预分频值为32
SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB; //数据传输从MSB位开始
SPI_InitStructure.SPI_CRCPolynomial=7; //CRC多项式为7
SPI_Init(SPI1,&SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPI1寄存器
SPI_Cmd(SPI1,ENABLE); //STM32使能SPI1
}
u16 Read_TLC2543(u8 ch)
{
u8 timeout;
u16 chx,ADCdata;
chx=ch<<12;
chx|=0x0c00;
AD_CS1=0;
delay_us(10);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==RESET)
{
timeout++;
if(timeout>200)
return 0;
}
SPI_I2S_SendData(SPI1,chx);
timeout=0;
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==RESET)
{
timeout++;
if(timeout>200)
return 0;
}
ADCdata=SPI_I2S_ReceiveData(SPI1);
delay_us(10);
AD_CS1=1;
ADCdata>>=4;
return ADCdata;
}
头文件
#ifndef __TLC2543_H
#define __TLC2543_H
#include "sys.h"
#define AD_CLK PAout(5)
#define AD_CLK_PIN GPIO_Pin_5
#define AD_PORT GPIOA
#define AD_SI PAout(7)
#define AD_SI_PIN GPIO_Pin_7
#define AD_SO PAout(6)
#define AD_SO_PIN GPIO_Pin_6
#define AD_CS1 PAout(4)
#define AD_CS1_PIN GPIO_Pin_4
#define AD_CS1_PORT GPIOA
#define AD_CS2 PCout(5)
#define AD_CS2_PIN GPIO_Pin_5
#define AD_CS2_PORT GPIOC
#define AD_DRDY1_PIN GPIO_Pin_4
#define AD_DRDY1_PORT GPIOC
#define AD_DRDY2_PIN GPIO_Pin_0
#define AD_DRDY2_PORT GPIOB
extern u8 AD_CH;
extern u16 Vadc[9];
extern u16 Aadc[9];
extern _Bool AD_DRDY1;
extern _Bool AD_DRDY2;
void TLC2543_GPIO_Configuration(void);
void TLC2543_NVIC_Configuration(void);
void SPI1_Configuration(void);
void TLC2543_Init(void);
u16 Read_TLC2543(u8 ch,u8 ad);
#endif