|
//端口初始化
void UART1_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0);
//UART1_TX GPIOB.6
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//TX
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//UART1_RX GPIOB.7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
//--------------------------------------------------
void UART1_9Bit_Init(u32 baudrate)
{
UART_InitTypeDef UART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_UART1, ENABLE);
//UART1 NVIC
NVIC_InitStructure.NVIC_IRQChannel = UART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//Baud rate
UART_StructInit(&UART_InitStructure);
UART_InitStructure.BaudRate = baudrate;
//The word length is in 8-bit data format.
UART_InitStructure.WordLength = UART_WordLength_8b;
UART_InitStructure.StopBits = UART_StopBits_1;
//No even check bit.
UART_InitStructure.Parity = UART_Parity_No;
//No hardware data flow control.
UART_InitStructure.HWFlowControl = UART_HWFlowControl_None;
UART_InitStructure.Mode = UART_Mode_Rx | UART_Mode_Tx;
UART_Init(UART1, &UART_InitStructure);
UART_ClearITPendingBit( UART1, UART_IT_RXIEN);//上電清除一次標志位
UART_ITConfig(UART1, UART_IT_RXIEN, ENABLE);
UART_Enable9bit(UART1, ENABLE);
UART_Set9bitLevel(UART1, DISABLE);
UART_Set9bitAutomaticToggle(UART1, ENABLE);
// UART_Set9bitPolarity(UART1,ENABLE);
UART_Cmd(UART1, ENABLE);
UART1_GPIO_Init();
}
//----------------------------------------
extern u16 MODE[10];//狀態存儲
u8 USART_RX_BUF[USART_REC_LEN]={0x00}; //接收緩沖,最大USART_REC_LEN個字節.末字節為換行符
u8 USART_RX_STA=0; //接收狀態標記
void UART1_IRQHandler(void)
{
u8 res;
if(UART_GetITStatus(UART1, UART_IT_RXIEN)!= RESET)
{
//Receiving interrupts (data received must end at 0x0D 0x0a)
//read receive data.
res = UART_ReceiveData(UART1);
USART_RX_BUF[USART_RX_STA]=res ;
if(USART_RX_STA<(USART_REC_LEN-1))
{
USART_RX_STA++;
}
else
{
USART_RX_STA=0;
}
UART_ClearITPendingBit(UART1, UART_IT_RXIEN);
}
}
//----------------H文件--------------------
#ifndef __UART_H
#define __UART_H
// Files includes
#include "mm32_device.h"
#include "stdio.h"
#include "hal_conf.h"
#include "string.h"
#include "hal_uart.h"
////////////////////////////////////////////////////////////////////////////////
#define USART_REC_LEN 10 //定義最大接收字節數 10
extern u8 USART_RX_BUF[USART_REC_LEN]; //接收緩沖,最大USART_REC_LEN個字節.末字節為換行符
extern u8 USART_RX_STA; //接收狀態標記
void UART1_GPIO_Init(void);
void UART1_9Bit_Init(u32 baudrate);
void UART1_Send_Byte(u8 dat);
void UART1_Send_Group(u8* buf, u16 len);
/// @}
/// @}
/// @}
////////////////////////////////////////////////////////////////////////////////
#endif
|
|