GD32晶片的USART1初始化程式碼
阿新 • • 發佈:2019-01-09
void usart_init(void) { /* Enable USART2 APB clock */ RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_USART1, ENABLE); /* USART1 Pins configuration **************************************************/ { /* Configure the GPIO ports */ GPIO_InitPara GPIO_InitStructure; /* Connect pin to Periph */ GPIO_PinAFConfig(GPIOA , GPIO_PINSOURCE9, GPIO_AF_1); GPIO_PinAFConfig(GPIOA , GPIO_PINSOURCE10, GPIO_AF_1); /* Configure pins as AF pushpull */ GPIO_InitStructure.GPIO_Pin = GPIO_PIN_9 | GPIO_PIN_10; GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF; GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ; GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL; GPIO_Init(GPIOA , &GPIO_InitStructure); } //GPIO setting { USART_InitPara USART_InitStructure; USART_DeInit(USART1); USART_InitStructure.USART_BRR =9600; USART_InitStructure.USART_WL = USART_WL_8B; USART_InitStructure.USART_STBits = USART_STBITS_1; USART_InitStructure.USART_Parity = USART_PARITY_RESET; USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE; USART_InitStructure.USART_RxorTx = USART_RXORTX_RX | USART_RXORTX_TX; USART_Init(USART1, &USART_InitStructure); } { NVIC_InitPara NVIC_InitStructure; /* Configure the NVIC Preemption Priority Bits */ /* Configure the preemption priority and subpriority: - 1 bits for pre-emption priority: possible value are 0 or 1 - 3 bits for subpriority: possible value are 0..7 - Lower values gives higher priority */ /* Re-configure and enable USART interrupt to have the higher priority */ NVIC_InitStructure.NVIC_IRQ = USART1_IRQn; NVIC_InitStructure.NVIC_IRQPreemptPriority = 5; NVIC_InitStructure.NVIC_IRQSubPriority = 1; NVIC_InitStructure.NVIC_IRQEnable = ENABLE; NVIC_Init(&NVIC_InitStructure); } //NVIC usart2 setting /* Enable the USART1 Receive interrupt */ USART_INT_Set(USART1, USART_INT_RBNE, ENABLE); /* USART enable */ USART_Enable(USART1, ENABLE); }
//傳送單位元組
void usart_transmit( unsigned char data )
{
USART_ClearBitState (USART1,USART_FLAG_TC);
USART_DataSend(USART1,data);
while(USART_GetBitState(USART1,USART_FLAG_TC)==RESET);
USART_ClearBitState (USART1,USART_FLAG_TC);
}
//傳送字串 void usart_send_string(char *str) { while(*str) { usart_transmit(*str) ; str++; } }