这是逻辑分析仪测出来的。
这是我的程序。
只用的spi的单向发送送模式。
/* INCLUDES */
#include "User_Define.h"
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_spi.h"
#define LCD_CS_SET GPIO_SetBits(GPIOA,GPIO_Pin_4)
#define LCD_CS_RESET GPIO_ResetBits(GPIOA,GPIO_Pin_4)
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void SPI1_Configuration(void);
void SPI1_Writebyte(u8 data);
void delay(unsigned char temp)
{
int a,j;
for(a=0;a<temp;a++)
for(j=0;j<100;j++);
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration(); //端口初始化
//NVIC_Configuration(); //中断源配置
SPI1_Configuration();
//__enable_irq();
while(1){
delay(10);
SPI1_Writebyte(0xaa);
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ; //SPI CS
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 ; //SPI MISO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 // SPI MOSI
| GPIO_Pin_5; //SPI CLK
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void RCC_Configuration(void)
{
//--------------------------- CLK INIT, HSE PLL ----------------------------
ErrorStatus HSEStartUpStatus;
//RCC reset
RCC_DeInit();
//开启外部时钟 并执行初始化
RCC_HSEConfig(RCC_HSE_ON);
//等待外部时钟准备好
HSEStartUpStatus = RCC_WaitForHSEStartUp();
//启动失败 在这里等待
while(HSEStartUpStatus == ERROR);
//设置内部总线时钟
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
//外部时钟为8M 这里倍频到72M
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);
//----------------------------- CLOSE HSI ---------------------------
//关闭内部时钟HSI
RCC_HSICmd(DISABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA
| RCC_APB2Periph_SPI1 //SPI1
, ENABLE);
}
void SPI1_Configuration(void)
{
SPI_InitTypeDef SPI_InitStructure;
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; //双工模式
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //SPI主模式
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8bit数据
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //CLK空闲时为高电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //CLK上升沿采样,因为上升沿是第二个边沿动作,所以也可以理解为第二个边沿采样
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //片选用软件控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //SPI频率
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //高位在前
//SPI_InitStructure.SPI_CRCPolynomial = 7; //crc7,stm32spi带硬件ecc
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
void SPI1_Writebyte(u8 data)
{
LCD_CS_RESET ;
//while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI1, data);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
LCD_CS_SET;
}
------解决思路----------------------
初步感觉,你的检查
SPI_I2S_SendData(SPI1, data); 这个函数,是否有拉高CS的动作