具体程序如下:
main函数:
#include "stm32f10x.h"
#include "stm32f10x_sdio.h"
#include "GPIO_config.h"
#include "USART_Config.h"
#include <stdio.h>
#define CMD_STRING_SIZE 128
int fputc(int ch,FILE *f)
{
//USART_ClearFlag(USART2,USART_FLAG_TC);
USART_SendData(USART2,ch);
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
return(ch);
}
//uint32_t SerialKeyPressed(uint8_t *key)
//{
// if(USART_GetFlagStatus(USART2,USART_FLAG_RXNE)!=RESET)
// {
// *key = (uint8_t)USART2->DR;
// return 1;
// }
// else
// {
// return 0;
// }
//}
//
//uint8_t GetKey(void)
//{
// uint8_t key = 0;
// while(1)
// {
// if(SerialKeyPressed((uint8_t*)&key)) break;
//
// }
// return key;
//}
//void GetInputString(uint8_t *buffp)
//{
// uint32_t bytes_read = 0;
// uint8_t c = 0;
// do
// {
// c=GetKey();
// if(c== '\r')
// break;
//
// if(bytes_read >= (CMD_STRING_SIZE))
// {
// printf("Command string size overflow\r\n");
// }
// if(c >= 0x20 &&c<=0x7E)
// {
//
// buffp[bytes_read++] = c;
//
// USART_ClearFlag(USART1,USART_FLAG_TC);
// USART_SendData(USART1,c);
// while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
// }
// }
// while(1);
// if(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=RESET)
// {
// USART_ClearFlag(USART1,USART_FLAG_TC);
// USART_SendData(USART1,"\n\r");
// buffp[bytes_read]='\0';
// //不确定是否要加结束符
// }
//}
int main(void)
{
uint8_t inputstr[CMD_STRING_SIZE];
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
USART_Config( );
GPIO_Config( );
printf("请输入:");
GPIOA->BRR = GPIO_Pin_8;
GPIOA->BRR = GPIO_Pin_11;
GPIOA->BRR = GPIO_Pin_12;
printf("请输入:");
while(1) ;
// {
//
// GetInputString(inputstr);
// }
}
USART配置
#include "stm32f10x.h"
void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
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_Mode_Rx;;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
gpio配置
#include"stm32f10x.h"
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
USART_InitStructure.USART_BaudRate = 9600;
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_Mode_Rx;;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
} |