startup.c
#include "stm32f10x.h"
#include "usart.h"
void usart_init(void) //´®¿Ú³õʼ»¯
{
GPIO_InitTypeDef GPIO_initstructure;
USART_InitTypeDef USART_initstructure;
NVIC_InitTypeDef Nvic_Init;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÅäÖÃÖжÏ×éΪÖжÏ×é2 misc.h
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //ÅäÖÃÖжϸ´ÓÃʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
//ÅäÖÃPA9 TX
GPIO_initstructure.GPIO_Mode =GPIO_Mode_AF_PP; //ÍÆÍìÊä³ö
GPIO_initstructure.GPIO_Pin =GPIO_Pin_9;
GPIO_initstructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_initstructure);
//ÅäÖÃPA10 RX
GPIO_initstructure.GPIO_Mode =GPIO_Mode_IN_FLOATING; //¸¡¿ÕÊäÈë
GPIO_initstructure.GPIO_Pin =GPIO_Pin_10;
GPIO_Init(GPIOA,&GPIO_initstructure);
//´®¿Ú1´ò¿ª
USART_initstructure.USART_BaudRate=115200; //²¨ÌØÂÊ
USART_initstructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_initstructure.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;
USART_initstructure.USART_Parity=USART_Parity_No; //УÑéλ
USART_initstructure.USART_StopBits= USART_StopBits_1; //ֹͣλ
USART_initstructure.USART_WordLength= USART_WordLength_8b; //ÓÐЧ×Ö½Ú³¤¶È 8λ
USART_Init(USART1,&USART_initstructure);
//ʹÄÜ´®¿Ú1
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //´®¿ÚÖжÏÅäÖÃ
USART_Cmd(USART1,ENABLE);
//ÅäÖÃÖжÏ
Nvic_Init.NVIC_IRQChannel =USART1_IRQn; //ÉèÖÃÖжÏͨµÀ stm32f10x.h
Nvic_Init.NVIC_IRQChannelCmd =ENABLE; //¿ØÖÆÊÇ·ñʹÄÜ
Nvic_Init.NVIC_IRQChannelPreemptionPriority =1; //ÉèÖÃÇÀÕ¼ÓÅÏȼ¶
Nvic_Init.NVIC_IRQChannelSubPriority =1; //ÉèÖÃ×ÓÓÅÏȼ¶
NVIC_Init(&Nvic_Init);
}
//·¢Ë͵¥¸ö×Ö·û µ÷Ó÷½·¨ USARTSendByte(USART1,'o');
void USARTSendByte(USART_TypeDef* USARTx, uint16_t Data){
USART_SendData(USART1,Data);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET); //ÅжÏÊÇ·ñ·¢ËÍÍê³É
}
//·¢ËÍ×Ö·û´® //ÔÀí×Ö·û·¢ËÍ×îºóһλÊÇ\0ÅжÏÊÇ·ñ×îºóһλ\0¾ÍÄÜ·¢ËÍ×Ö·û´® µ÷Ó÷½·¨ USARTSendStr(USART1,"ruanzebin");
void USARTSendStr(USART_TypeDef* USARTx, char *str){
uint16_t i=0; //ÎÞ·ûºÅÕûÐÎÊ®Áùλ
do{
USARTSendByte(USARTx, *(str+i));
i++;
}while(*(str+i)!='\0');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET); //ÅжÏÊÇ·ñ·¢ËÍÍê³É
}
//printf º¯Êý putcharº¯Êý(Êä³öµ¥¸ö×Ö·û) ͨ¹ýÖض¨Ïò Êä³öÊýÖµ
int fputc(int ch ,FILE *f){
USART_SendData(USART1,(uint8_t)ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET); //ÅжÏÊÇ·ñ·¢ËÍÍê³É
return (ch);
}
//scanf() getcharº¯Êý
int fgetc(FILE *f)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE)==RESET); //ÅжÏÊÇ·ñ½ÓÊճɹ¦
return (int)USART_ReceiveData(USART1);
}
|