# include "uart. H"
# include "string. H"
# include "stdlib. H"
//////////////////////////////////////////////////////////////////////////////////
//if you use the ucos, include the header file for the below.
If # 1
# pragma import (__use_no_semihosting)
//the standard library needs the support of function
Struct __FILE
{
Int handle;
};
The FILE __stdout;
//define _sys_exit () to avoid using half a host mode
Void _sys_exit (int x)
{
X=x;
}
//redefine fputc function
Int fputc (int ch, FILE * f)
{
While ((USART1 - & gt; ISR& 0 x40)==0);//cycle to send, until completion of the send
USART1 - & gt; The RDR=(uint8_t) ch;
Return ch;
}
# endif
//serial port interrupt service routine 1
//note that reads USARTx - & gt; The SR can avoid the puzzling error
Uint8_t USART_RX_BUF [USART_REC_LEN];//receive buffer, the biggest USART_REC_LEN bytes.
//receiving state
//bit15, receive complete sign
//bit14, receiving x0d 0
//bit13 ~ 0, the number of significant byte received
Uint16_t USART_RX_STA=0;//receiving state tag
//initialize the IO serial port 1
//bound: baud rate
Void uart_init (uint32_t bound) {
//set the GPIO port
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd (RCC_AHBPeriph_GPIOB, ENABLE);//can make GPIOA clock
RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1, ENABLE);//can make USART1 clock
Reuse//serial port 1 corresponding pin map
GPIO_PinAFConfig (GPIOB, GPIO_PinSource6 GPIO_AF_1);//GPIOB6 reuse for USART1
GPIO_PinAFConfig (GPIOB, GPIO_PinSource7 GPIO_AF_1);//GPIOB7 reuse for USART1
//USART1 port configuration
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_6 | GPIO_Pin_7;//GPIOB6 GPIOB7
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;//multiplexing function
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//speed 50 MHZ
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;//push-pull multiplexing output
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;//pull
GPIO_Init (GPIOB, & amp; GPIO_InitStructure);//initialize PB6, PB7
//Usart1 NVIC configuration
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;//serial port interrupt channel 1
NVIC_InitStructure. NVIC_IRQChannelPriority=0;
NVIC_InitStructure. NVIC_IRQChannelCmd=ENABLE;//IRQ channel can make
NVIC_Init (& amp; NVIC_InitStructure);//register according to the specified parameters initialization VIC,
//USART1 initialization setting
USART_InitStructure. USART_BaudRate=bound;//baud rate setting
USART_InitStructure. USART_WordLength=USART_WordLength_8b;//word length of 8 bits of data format
USART_InitStructure. USART_StopBits=USART_StopBits_1;//a stop bit
USART_InitStructure. USART_Parity=USART_Parity_No;//and white parity bit
USART_InitStructure. USART_HardwareFlowControl=USART_HardwareFlowControl_None;//no hardware flow control
USART_InitStructure. USART_Mode=USART_Mode_Rx | USART_Mode_Tx;//transceiver model
USART_Init (USART1, & amp; USART_InitStructure);//initialize serial port 1,
USART_ITConfig (USART1, USART_IT_RXNE, ENABLE);
USART_Cmd (USART1, ENABLE);//can make a serial port 1
//USART_ClearFlag (USART1, USART_FLAG_TC);
}
Void USART1_IRQHandler (void)//serial port 1 interrupt service routines
{
Uint8_t Res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) !=RESET)//receiving interrupt (the received data must be 0 x0d 0 x0a end)
{
Res=USART_ReceiveData (USART1);//(USART1 - & gt; DR);//read the received data
If ((USART_RX_STA & amp; 0 x8000)==0)//receiving unfinished
{
If (USART_RX_STA & amp; 0 x4000)//received 0 x0d
{
if(Res!=0x0a)USART_RX_STA=0;//receiving errors, start
The else USART_RX_STA |=0 x8000;//receive completed
}
Haven't received 0 x0d else//
{
If (Res==0 x0d) USART_RX_STA |=0 x4000;
The else
{
USART_RX_BUF x3fff] [USART_RX_STA & amp; 0=Res;
USART_RX_STA + +;
If (USART_RX_STA & gt; USART_RX_STA (USART_REC_LEN - 1))=0;//receiving data errors, start receiving
}
}
}
}
}
//////////////////////////////////////////////
//the main function
# include "stm32f0xx. H"
# include "delay. H"
# include "uart. H"
# include "PWM h"
Int n=300;//duty ratio, the absolute maximum 500
Int main (void)
{
Delay_init (48);
Uart_init (115200);
//TIM_PWM_Config (aiaa 999-1);//5 KHZ frequency
////pwmout (v);
//TIM_SetCompare1 (TIM1, v);
//TIM_SetCompare2 (TIM1, v);
While (1)
{
//delay_ms (500);
//printf (" aa \ r \ n ");
USART_SendData (USART1, 0 x31);
}
}
CodePudding user response:
Redefine fputc function has a mistake, the RDR should be changed to TDR