//#include "usartself.h"
#include "stm32f10x.h"
#include "stdio.h"
#include "usartself.h"
int fputc(int ch, FILE *f)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
USART_SendData(USART1,(uint8_t)ch);
return ch;
}
//bound:?¨????
void uart1_init(u32 bound){
//GPIO?????è??
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //????USART1??GPIOA?±??
USART_DeInit(USART1); //????????1
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //???????ì????
GPIO_Init(GPIOA, &GPIO_InitStructure); //??????PA9
//USART1_RX    A.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????????
GPIO_Init(GPIOA, &GPIO_InitStructure); //??????PA10
//USART ???????è??
USART_InitStructure.USART_BaudRate = bound;//??°??è????9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×??¤??8??????????
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_Rx | USART_Mode_Tx; //??·?????
USART_Init(USART1, &USART_InitStructure); //??????????
#if EN_USART1_RX //??????????????
//Usart1 NVIC ????
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//??????????3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×???????3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??????
NVIC_Init(&NVIC_InitStructure); //?ù?????¨????????????VIC?????÷
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//????????
#endif
USART_Cmd(USART1, ENABLE); //????????
}
void Usart2_Initial()
{
//******GPIO Initial****
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//***********RCC Initial
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOD |RCC_APB2Periph_AFIO ,ENABLE );
RCC_APB1PeriphClockCmd (RCC_APB1Periph_USART2 , ENABLE );
//******USART 1***
GPIO_PinRemapConfig (GPIO_Remap_USART2 , ENABLE);
GPIO_InitStructure .GPIO_Mode = GPIO_Mode_AF_PP ;
GPIO_InitStructure .GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure .GPIO_Speed = GPIO_Speed_50MHz ;
GPIO_Init (GPIOD , &GPIO_InitStructure );
GPIO_InitStructure .GPIO_Mode = GPIO_Mode_IN_FLOATING ;
GPIO_InitStructure .GPIO_Pin = GPIO_Pin_6;
GPIO_Init (GPIOD , &GPIO_InitStructure );
//**********USART Initail
USART_InitStructure .USART_BaudRate = 9600;
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 ;
USART_Init (USART2 ,&USART_InitStructure );
USART_ITConfig (USART2,USART_IT_RXNE ,ENABLE );
USART_Cmd (USART2 , ENABLE );
}
#ifndef _USARTSELF_H_
#define _USARTSELF_H_
void uart1_init(u32 bound);
void Usart2_Initial();
int fputc(int ch, FILE *f);
#endif
|