最近在调试SPI SPI1用DMA1 Channel3 来发送数据 发现一个奇怪的问题 发送的数据存储器的最后一个数据(我这边是字节,因为数据大小选择为BYTE)的最高位为1的时候,比如最后一个数据为0X80,DMA发送成功后,发送的数据线SPI1 MISI 保持为高电平,如果最后一个字节的最高位为0,则发送完毕保持低电平,不知道各位大虾有没有遇到过,是怎么回事啊?附上程序如下 my.h是自己随便写的头文件 就定义了下BufferSize 为6
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "my.h"
/* Local includes ------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
/* Private define ------------------------------------------------------------*/
ErrorStatus SendFlag=ERROR;
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint16_t i,j=0;
uint8_t SPI1_Buffer_Tx[BufferSize]={0xff,0x00,0x00,0x00,0x00,0x80};
/* Private functions ---------------------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
void SPI_Configuration(void);
void DMA_Configuration(void);
void USART1_PutChar(unsigned char c);
void UART4_PutChar(unsigned char c);
void SPI_PutChar(unsigned char c);
void delay(void);
/*******************************************************************************
* Function Name : main
* Description : Main program
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();
/* NVIC configuration ------------------------------------------------------*/
NVIC_Configuration();
/* USART configuration ------------------------------------------------------*/
USART_Configuration();
/* SPI configuration ------------------------------------------------------*/
SPI_Configuration();
/* DMA configuration ------------------------------------------------------*/
DMA_Configuration();
/* pinE.2 configuration ------------------------------------------------------*/
GPIO_ResetBits(GPIOE, GPIO_Pin_0);
while(1)
{
}
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */
RCC_DeInit(); //
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK/2 */
RCC_PCLK2Config(RCC_HCLK_Div2);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
/* Enable peripheral clocks --------------------------------------------------*/
// for small target (48pin) in mini-kit
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //打开五个IO口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //打开USART1时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); //打开UART4时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE); //打开SPI1时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE); //打开SPI2时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //打开DMA1时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE); //打开DMA2时钟
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure GPIO Of USART1-----------------------------------------*/
/* Configure USART1_Tx ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1_Rx ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure GPIO Of UART4-----------------------------------------*/
/* Configure UART4_Tx ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure UART4_Rx ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure Nomal GPIO -----------------------------------------*/
/* Configure PE.0 ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Configure GPIO Of SPI1 ---------------------------------------------*/
/* Configure SPI1_SCK|SPI1_MISI --------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 |GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure SPI1_MISO ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure GPIO Of SPI2 ---------------------------------------------*/
/* Configure SPI2_SCK|SPI2_MISI ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 |GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure SPI2_MISO ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
}
偶校验
void SPI_Configuration(void)
{
SPI_InitTypeDef SPI_InitStructure;
SPI_I2S_DeInit(SPI1); //初始化SPI1
// SPI_I2S_DeInit(SPI2); //初始化SPI2
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; //CPOL=1时钟悬空高
SPI_InitStructure.SPI_CPHA =SPI_CPHA_1Edge; //CPHA=1 数据捕获第2个
SPI_InitStructure.SPI_NSS =SPI_NSS_Soft; //软件NSS
SPI_InitStructure.SPI_BaudRatePrescaler =SPI_BaudRatePrescaler_32; //32分频
SPI_InitStructure.SPI_FirstBit =SPI_FirstBit_MSB; //高位在前
SPI_InitStructure.SPI_CRCPolynomial =7; //CRC7
SPI_Init(SPI1,&SPI_InitStructure); //初始化SPI1
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE); //SPI1的DMA发送使能
SPI_Cmd(SPI1, ENABLE); //使能SPI1
/*Configure SPI2-------------*/
// SPI_InitStructure.SPI_Mode =SPI_Mode_Slave; //SPI2为从设备
// SPI_Init(SPI2,&SPI_InitStructure); //初始化SPI2
// SPI_I2S_ITConfig(SPI2,SPI_I2S_IT_RXNE,ENABLE); //开SPI2接收中断
// SPI_Cmd(SPI2, ENABLE); //使能SPI2
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel3); //初始化DMA1的Channel3
// DMA_DeInit(DMA2_Channel3); //初始化DMA2的Channel3
DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)SPI1_DR_Address; //SPI1寄存器地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SPI1_Buffer_Tx; //发数据存储地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //从存储器读
DMA_InitStructure.DMA_BufferSize = BufferSize; //定义要传输的字节数
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //关闭外设递增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //目标缓冲区地址递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据宽度
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //存储数据宽度
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //通用模式
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //优先级最高
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //禁止两个数据互相访问
DMA_Init(DMA1_Channel3, &DMA_InitStructure); //初始化DMA1_Channel3
// DMA_ITConfig(DMA1_Channel3,DMA_IT_TC,ENABLE); //初始化DMA1全发送中断
DMA_Cmd(DMA1_Channel3, ENABLE); //使能DMA1_Channel3
/*Configure DMA2-------------*/
// DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)UART4_DR_Address; //UART4寄存器地址
// DMA_InitStructure.DMA_MemoryBaseAddr = (u32)UART4_Buffer_Rx; //收数据存储地址
// DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //从外设读
// DMA_Init(DMA2_Channel3, &DMA_InitStructure); //初始化DMA2_Channel3
// DMA_ITConfig(DMA2_Channel3,DMA_IT_TC,ENABLE); //初始化DMA2全发送中断
// DMA_Cmd(DMA2_Channel3, ENABLE); //使能DMA1_Channel3
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d
", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
此帖出自
小平头技术问答
一周热门 更多>