/***************主函数***************************************************************************************/
#include "stm32f10x.h"
#include "printf.h"
#include "stdio.h"
#include "systick.h"
#include "stdio.h"
#include "usart.h"
#include "stm32f10x_usart.h"
int main()
{
printf_init();
while(1)
{
printf("SOUTH
");
}
}
/***************中断函数***************************************************************************************/
void USART1_IRQHandler(void)
{
static u8 res;
USART_ClearFlag(USART1,USART_FLAG_TC);
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
res=USART_ReceiveData(USART1);
//res++;
USART_SendData(USART1,res);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==Bit_RESET);
}
}
/***************初始化***************************************************************************************/
#include "printf.h"
#include "stdio.h"
int fputc(int ch,FILE *p)
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
void printf_init()
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO¶Ë¿ú½á11ì嶨òå
USART_InitTypeDef USART_InitStructure; //USART′®¿ú½á11ì嶨òå
NVIC_InitTypeDef NVIC_InitStructure; //ÖD¶ÏóÅÏ輶½á11ì嶨òå
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //A¶Ë¿ú¼óê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //¸′óÃ1|Äü¼óê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //50MHzÆμÂê
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //PA.91ü½Å
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //¸′óÃíÆÃaêä3öÄ£ê½
GPIO_Init(GPIOA,&GPIO_InitStructure); //GPIO3õê¼»ˉ
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //PA.101ü½Å
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //¸¡¿ÕÄ£ê½êäè룻
GPIO_Init(GPIOA,&GPIO_InitStructure); //GPIO3õê¼»ˉ
USART_InitStructure.USART_BaudRate=9600; //2¨ìØÂêéèÖÃ
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //ò»¸öÖ¡ÖD·¢ËíμÄêy¾YμÄλêy
USART_InitStructure.USART_StopBits=USART_StopBits_1; //·¢ËíμÄí£Ö1λêyÄ¿
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl=
USART_HardwareFlowControl_None; //2»ÆôóÃó2¼tá÷
USART_InitStructure.USART_Mode=USART_Mode_Tx | USART_Mode_Rx; //·¢Ëíoí½óêÕí¬ê±ê1Äü
USART_Init(USART1,&USART_InitStructure); //USART13õê¼»ˉ
USART_Cmd(USART1,ENABLE); //USART1ê1Äü
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //ÆôóýóêÕÖD¶Ïê1Äü
USART_ClearFlag(USART1,USART_FLAG_TC); //Çå3t·¢Ëííê3é±êÖ¾
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //óÅÏ輶·Ö×é
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn; //ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; //ÇàÕ¼óÅÏ輶
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0; //Ïìó|óÅÏ輶
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //ê1Äü
NVIC_Init(&NVIC_InitStructure); //NVIC3õê¼»ˉ
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>