|
#include "usart1.h"
#define RX_BUFER_SIZE 32
u8 rx_buffer[RX_BUFER_SIZE] = {0};
u8 *rx_bufferP = rx_buffer;
u8 Usart_RecvIng = 0;
u8 Usart_RecvEnd = 0;
u8 Usart_Monitor = 0;
void My_USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PA端口時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能USART1端口時鐘
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA_Pin_9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA_Pin_10
//設置NVIC中斷分組
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設置NVIC中斷分組2:2位搶占優先級,2位響應優先級0-3;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //搶占優先級1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子優先級1
NVIC_Init(&NVIC_InitStructure); //設置中斷優先級,根據指定的參數初始化VIC寄存器
USART_InitStructure.USART_BaudRate = 512000;
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(USART1, &USART_InitStructure); //初始化串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //開啟串口接收中斷
USART_Cmd(USART1, ENABLE); //使能USART1
}
void USART1_SendChar(u8 ch)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); //must have
USART_SendData(USART1, ch);
}
void USART1_SendStr(u8* str)
{
while(*str)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); //must have
USART_SendData(USART1, *str++);
}
}
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
if(!Usart_RecvEnd)
{
Usart_Monitor = 1;
*rx_bufferP++ = USART_ReceiveData(USART1);
*rx_bufferP = '\0'; //沒有此語句也可以
Usart_RecvIng = 1;
}
}
if(USART_GetFlagStatus(USART1,USART_FLAG_ORE) == SET) //溢出處理
{
USART_ReceiveData(USART1);
USART_ClearFlag(USART1,USART_FLAG_ORE);
}
USART_ClearFlag(USART1,USART_IT_RXNE); //一定要清除接收中斷
}
|
評分
-
查看全部評分
|