示波器查看NSS、SCK、MOSI波形正确,就是在MISO上没有数据输出
大大们帮我看看,是否有哪儿配置错误或者写得不正确
真是衰,就没有用SPI调通过一个东东。
#include "stm32f10x.h"
#include "platform_config.h"
#include "AD7799.h"
#include "Init.h"
/* Exported macro ------------------------------------------------------------*/
//#define SPI_MODE
#define AD_CS_HIGH() GPIO_SetBits(GPIO_AD_NSS, AD_NSS)
#define AD_CS_LOW() GPIO_ResetBits(GPIO_AD_NSS, AD_NSS)
#define AD_SCLK_HIGH() GPIO_SetBits(GPIO_AD, AD_SCLK)
#define AD_SCLK_LOW() GPIO_ResetBits(GPIO_AD, AD_SCLK)
#define AD_MISO_HIGH() GPIO_SetBits(GPIO_AD, AD_MISO)
#define AD_MISO_LOW() GPIO_ResetBits(GPIO_AD, AD_MISO)
#define AD_MISORead() GPIO_ReadInputDataBit(GPIO_AD, AD_MISO)
#define AD_MOSI_HIGH() GPIO_SetBits(GPIO_AD, AD_MOSI)
#define AD_MOSI_LOW() GPIO_ResetBits(GPIO_AD, AD_MOSI)
/*******************************************************************************
* Function Name : AD_Init
* Description : Initializes the peripherals used by the SPI ENERGY (CS5460) driver.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void AD_SPIInit(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
/* Enable SPI1 and GPIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AD | RCC_APB2Periph_GPIO_AD | RCC_APB2Periph_GPIO_AD_NSS | RCC_APB2Periph_AFIO, ENABLE);
/* Configure SPI1 pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = AD_SCLK | AD_MISO | AD_MOSI;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_AD, &GPIO_InitStructure);
/*
GPIO_InitStructure.GPIO_Pin = AD_INT;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_ENERGY, &GPIO_InitStructure);
*/
GPIO_InitStructure.GPIO_Pin = AD_NSS;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIO_AD_NSS, &GPIO_InitStructure);
/* Deselect the FLASH: Chip Select high */
AD_CS_HIGH();
// AD_INT_HIGH();
/* Connect Key Button EXTI Line to Key Button GPIO Pin */
GPIO_EXTILineConfig(AD_PORT_SOURCE, AD_PIN_SOURCE);
/* Configure Key Button EXTI Line to generate an interrupt on falling edge */
EXTI_InitStructure.EXTI_Line = AD_EXTI_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
// EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
// EXTI_Init(&EXTI_InitStructure);
/* Configure one bit for preemption priority */
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* 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_16;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI_AD, &SPI_InitStructure);
/* Enable SPI1 */
SPI_Cmd(SPI_AD, ENABLE);
}
/*---------------------------------------------------------
Func: AD7799忙判断
Time: 2012-3-29
Ver.: V1.0
Note: 0/OK >0/ERROR,timeout
---------------------------------------------------------*/
bool AD_WaitBusy()
{
int i;
AD_CS_LOW();
i=0;
while(AD_MISORead()){
i++;
if(i>2000)
return TRUE;
}
AD_CS_HIGH();
return FALSE;
}
/*---------------------------------------------------------
Func: AD7799读取寄存器数据
Time: 2012-3-29
Ver.: V1.0
Note:
---------------------------------------------------------*/
void AD_RegRead(u8 RegAddr,u8 *Buffer,int Length)
{
AD_CS_LOW();
RegAddr|=AD_OP_READ;
while (SPI_I2S_GetFlagStatus(SPI_AD, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI_AD,RegAddr);
for(int i=0;i<Length;i++)
{
while (SPI_I2S_GetFlagStatus(SPI_AD, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI_AD,0x0);
while (SPI_I2S_GetFlagStatus(SPI_AD, SPI_I2S_FLAG_RXNE) == RESET);
Buffer = SPI_I2S_ReceiveData(SPI_AD);
}
AD_CS_HIGH();
}
/*---------------------------------------------------------
Func: AD7799写入寄存器数据
Time: 2012-3-29
Ver.: V1.0
Note:
---------------------------------------------------------*/
void AD_RegWrite(u8 RegAddr,u8 *Buffer,int Length)
{
AD_CS_LOW();
RegAddr|=AD_OP_WRITE;
while (SPI_I2S_GetFlagStatus(SPI_AD, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI_AD,RegAddr);
for(int i=0;i<Length;i++)
{
while (SPI_I2S_GetFlagStatus(SPI_AD, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI_AD,Buffer);
}
AD_CS_HIGH();
}
/*---------------------------------------------------------
Func: AD7799复位
Time: 2012-3-29
Ver.: V1.0
Note: 0/OK >0/Error
---------------------------------------------------------*/
void AD_Reset()
{
u8 Cmd[4]={0xFF,0xFF,0xFF,0xFF};
AD_CS_LOW();
for(int i=0;i<4;i++)
{
while (SPI_I2S_GetFlagStatus(SPI_AD, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI_AD,Cmd);
}
AD_CS_HIGH();
}
/*---------------------------------------------------------
Func: AD7799通道内部校准
Time: 2012-3-29
Ver.: V1.0
Note: 0/OK >0/Error
---------------------------------------------------------*/
bool AD_Calibrate(u16 cfg)
{
bool R=FALSE;
u8 Cmd[2];
Cmd[0]=cfg/0x100;
Cmd[1]=cfg%0x100;
AD_RegWrite(AD_REG_CONFIG,Cmd,2); //设置配置寄存器
Cmd[0]=AD_MODE_IZCM/0x100;
Cmd[1]=AD_MODE_URA15;
AD_RegWrite(AD_REG_MODE,Cmd,2); //进行内部零度校准
R|=AD_WaitBusy(); //等待校准完成
Cmd[0]=AD_MODE_IFCM/0x100;
Cmd[1]=AD_MODE_URA15;
AD_RegWrite(AD_REG_MODE,Cmd,2); //进行内部零度校准
R|=AD_WaitBusy(); //等待校准完成
return R;
}
/*---------------------------------------------------------
Func: AD7799初始化
Time: 2012-3-29
Ver.: V1.0
Note: 0/OK >0/Error
---------------------------------------------------------*/
bool AD_Init(u16 cfg0,u16 cfg1,u16 cfg2)
{
u8 ID;
AD_SPIInit();
AD_Reset();
DelayMs(10);
// Wrtos_TaskDelay(10);
AD_RegRead(AD_REG_STATUS,&ID,1); //读取器件ID
AD_RegRead(AD_REG_ID,&ID,1); //读取器件ID 任何时候这儿读出都是0,明显不对
if((ID==0xFF)||(ID==0x00))
return TRUE;
AD_Calibrate(cfg0); //通道1校准
AD_Calibrate(cfg1); //通道2校准
AD_Calibrate(cfg2); //通道3校准
// Cmd[0]=S6|S5|S4;
// AD7799_WriteReg(ADC_REG_IO,Cmd,1);
return FALSE;
}
/*---------------------------------------------------------
Func: AD7799开始转换
Time: 2012-3-29
Ver.: V1.0
Note:
---------------------------------------------------------*/
void AD_Start(u16 cfg,u16 mode)
{
u8 Cmd[2];
Cmd[0]=cfg/0x100;
Cmd[1]=cfg%0x100;
AD_RegWrite(AD_REG_CONFIG,Cmd,2);
Cmd[0]=mode/0x100;
Cmd[1]=mode%0x100;
AD_RegWrite(AD_REG_MODE,Cmd,2);
}
/*---------------------------------------------------------
Func: AD7799读取转换结果
Time: 2012-3-29
Ver.: V1.0
Note:
---------------------------------------------------------*/
u32 AD_Read()
{
UNION_V v;
v.v_char[0]=0;
AD_RegRead(AD_REG_DATA,&v.v_char[1],3);
// D=TTS_D32FromArray(Cmd,1);
return v.v_long;
} |
|