初中生
最后登录1970-1-1
在线时间 小时
注册时间2019-7-4
|
===代码如下:
#include "stm32f10x.h"
#include <stdio.h>
#include <string.h>
//PC2 LED1
//PC3 LED2
//PA0 KEY1
//PA9 UART_TX
//PA10 UART_RX
void USART1_Configuration(void);
void RCC_Configuration(void);
void GPIO_Configuration(void);
int fputc(int ch, FILE *f);
void DMA_Configuration(void);
uint8_t SrcBuf[12] = "xxxxxxxxxxx";
//UART重定向
int fputc(int ch, FILE *f)
{
if (ch == '\n')
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, '\r');
}
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, ch);
return ch;
}
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}
//
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_Stucturue;
GPIO_StructInit(&GPIO_Stucturue); //
GPIO_Stucturue.GPIO_Pin = GPIO_Pin_9; //usart1 TX
GPIO_Stucturue.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_Stucturue);
GPIO_Stucturue.GPIO_Pin = GPIO_Pin_10; //RX
GPIO_Stucturue.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入//复用开漏输入
GPIO_Init(GPIOA, &GPIO_Stucturue);
}
void USART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;//DR地址,
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SrcBuf;//数组名即为数组首地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//从外设读取数据
DMA_InitStructure.DMA_BufferSize = 11;
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_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//存储器到存储器ENABLE,存储器到外设DISABLE
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
}
int main()
{
uint8_t flag = 1;
RCC_Configuration();
GPIO_Configuration();
USART1_Configuration();
//存储器到存储器DMA
DMA_Configuration();
//发送第一位数据之前,先清除TC标志,否则容易造成第一位发送不出去
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
DMA_Cmd(DMA1_Channel5, ENABLE);
printf("hello\n");
while (flag)
{
if (strstr(SrcBuf, "LED")) //uint8_t SrcBuf[12] = "xxxxxxxxxxx";
{
printf(SrcBuf);
flag = 0;
}
printf("NO");
flag = 0;
}
}
===串口调试助手发送LED+回车,无返回值;
===求大哥们帮忙分析一下;小弟不才,在此谢过
|
|