本帖最后由 zourunbeng 于 2014-1-15 13:48 编辑
硬件部分如下图:
软件部分代码如下,
//PA3: DRDY
//PA4:CS 未用
//PA5:SCK
//PA6:MISO
//PA7:MOSI
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
//PCLK1(APB1) = HCLK/2
RCC_PCLK1Config(RCC_HCLK_Div2);
//PCLK2(APB2) = HCLK
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_ADCCLKConfig(RCC_PCLK2_Div2);
FLASH_SetLatency(FLASH_Latency_2);
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource()!=0x08);
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* SPI1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
}
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &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);
SPI_Cmd(SPI1, DISABLE);
/* SPI1 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
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_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
//SPI1->CR1|=1<<6;
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE); //
SPIx_ReadWriteByte(0xff);//
}
u8 SPIx_ReadWriteByte(u8 TxData)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/* Send byte through the SPI1 peripheral */
SPI_I2S_SendData(SPI1, TxData);
/* Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
GPIO_SetBits(GPIOA,GPIO_Pin_4);
return SPI_I2S_ReceiveData(SPI1);
}
void TM7705_Init(void)
{
unsigned char i ;
for(i = 0; i < 100; i++)
{
SPIx_ReadWriteByte(0xFF);
}
SPIx_ReadWriteByte(0x20) ;
Delay(1000);
SPIx_ReadWriteByte(0x02) ;
Delay(1000);
SPIx_ReadWriteByte(0x10) ;
Delay(1000);
SPIx_ReadWriteByte(0x44) ;
Delay(100000);
}
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) ;
}
u16 ReadTM7705(void)
{
unsigned long Ret = 0;
SPIx_ReadWriteByte(0x38) ;
while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3 )== 1)
{
;
}
Ret = ReadTM7705_16BitValue();
return Ret;
}
int main(void)
{
RCC_Configuration();
Delay(5000000);//
GPIO_Configuration();
SPI1_Init();
TM7705_Init();
init_watchdog();
for (;;)
{
;}
}
现在就是DOUT没有输出,要么改下Ix_ReadWriteByte(0x20) ;
Delay(1000);
SPIx_ReadWriteByte(0x02) ;
Delay(1000);
SPIx_ReadWriteByte(0x10) ;
Delay(1000);
SPIx_ReadWriteByte(0x44) ; --------这里改成SPIx_ReadWriteByte(0x7c) ;
Delay(100000);, 就输出数据是0XFFFF,请各位大侠指点一下,谢谢!
|