- #include "n32g43x.h" // Device header
- #include "systick.h"
- #include <stdio.h>
- void GPIO_LED_Init()
- {
- GPIO_InitType GPIO_InitStruct;
-
- RCC_EnableAPB2PeriphClk(RCC_APB2PCLKEN_IOPAEN, ENABLE);
-
- GPIO_InitStruct.Pin = GPIO_PIN_8;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD;
- GPIO_InitStruct.GPIO_Current = GPIO_DC_2mA;
-
- GPIO_InitPeripheral(GPIOA, &GPIO_InitStruct);
- }
- void USART1_init()
- {
- GPIO_InitType GPIO_InitStruct_Tx;
- GPIO_InitType GPIO_InitStruct_Rx;
-
-
- // /* --------------- USART1 默认端口的配置 配置开始 --------------- */
- // RCC_EnableAPB2PeriphClk(RCC_APB2PCLKEN_IOPAEN, ENABLE);
- //
- // // Tx PA9 复用输出推挽
- // GPIO_InitStruct_Tx.Pin = GPIO_PIN_9;
- // GPIO_InitStruct_Tx.GPIO_Mode = GPIO_Mode_AF_PP;
- // GPIO_InitStruct_Tx.GPIO_Alternate = GPIO_AF4_USART1;
- //
- // GPIO_InitPeripheral(GPIOA, &GPIO_InitStruct_Tx);
- // // Rx PA10 输入浮空输入上拉
- // GPIO_InitStruct_Rx.Pin = GPIO_PIN_10;
- // GPIO_InitStruct_Rx.GPIO_Pull = GPIO_Pull_Up;
- // GPIO_InitStruct_Rx.GPIO_Alternate = GPIO_AF4_USART1;
- //
- // GPIO_InitPeripheral(GPIOA, &GPIO_InitStruct_Rx);
- // /* --------------- USART1 默认端口的配置 配置结束 --------------- */
-
-
-
- /* 如果PA9和PA10端口被占用,则可以通过配置重映射端口 */
- RCC_EnableAPB2PeriphClk(RCC_APB2PCLKEN_AFIOEN, ENABLE); // 使能AFIO模块的时钟
-
- //配置重映射寄存器,使能重映射功能
- // GPIO_ConfigPinRemap(GPIOB_PORT_SOURCE, GPIO_PIN_SOURCE6, GPIO_AF1_USART1);
- // GPIO_ConfigPinRemap(GPIOB_PORT_SOURCE, GPIO_PIN_SOURCE7, GPIO_AF1_USART1);
-
- RCC_EnableAPB2PeriphClk(RCC_APB2PCLKEN_IOPBEN, ENABLE);
- // Tx PB6 复用输出推挽
- GPIO_InitStruct_Tx.Pin = GPIO_PIN_6;
- GPIO_InitStruct_Tx.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStruct_Tx.GPIO_Alternate = GPIO_AF4_USART1;
-
- GPIO_InitPeripheral(GPIOB, &GPIO_InitStruct_Tx);
- // Rx PB7 输入浮空输入上拉
- GPIO_InitStruct_Rx.Pin = GPIO_PIN_7;
- GPIO_InitStruct_Rx.GPIO_Mode = GPIO_Mode_Input;
- GPIO_InitStruct_Rx.GPIO_Pull = GPIO_Pull_Up;
- GPIO_InitStruct_Tx.GPIO_Alternate = GPIO_AF4_USART1;
-
- GPIO_InitPeripheral(GPIOB, &GPIO_InitStruct_Rx);
- /* --------------- 端口映射配置结束 --------------- */
- USART_InitType USART_InitStruct;
-
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_USART1, ENABLE); // 开启时钟
-
- USART_InitStruct.BaudRate = 115200; // 波特率115200
- USART_InitStruct.Mode = USART_MODE_TX | USART_MODE_RX; // 双向
- USART_InitStruct.WordLength = 8; // 8位数据位
- USART_InitStruct.StopBits = USART_STPB_1; // 1位停止位
- USART_InitStruct.Parity = USART_PE_NO; // 无校验
-
- USART_Init(USART1, &USART_InitStruct);
-
- USART_Enable(USART1, ENABLE); // 使能USART1 总开关
- }
- void MY_USART_SendBytes(USART_Module* USARTx, uint8_t *pData, uint16_t Size)
- {
- for (uint16_t i = 0; i < Size; i++)
- {
- // #1. 等待发送数据寄存器空
- while(USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET);
- // #2. 将要发送的数据写入到发送数据寄存器
- USART_SendData(USARTx, pData[i]);
- }
-
- // #3. 等待数据发送完成
- while(USART_GetFlagStatus(USARTx, USART_FLAG_TXC) == RESET);
- }
- /* retarget the C library printf function to the USART */
- int fputc(int ch, FILE* f)
- {
- // #1. 等待发送数据寄存器空
- while (USART_GetFlagStatus(USART1, USART_FLAG_TXDE) == RESET);
- // #2. 写入发数据寄存器当中
- USART_SendData(USART1, (uint8_t)ch);
- ;
- return (ch);
- }
- int main(void)
- {
- RCC_ClocksType RCC_ClocksStruct;
- RCC_GetClocksFreqValue(&RCC_ClocksStruct);
-
- systick_config();
-
- USART1_init();
- GPIO_LED_Init();
-
-
- // uint8_t byTwoSend[] = {1,2,3,4,5};
- // MY_USART_SendBytes(USART1, byTwoSend, 5);
-
-
- printf("\r\nCK_SYS is %d", RCC_ClocksStruct.SysclkFreq); // 打印当前的系统时钟
-
-
- while(1)
- {
- // #1. 等待接收数据寄存器RDR非空
- while (USART_GetFlagStatus(USART1, USART_FLAG_RXDNE) == RESET);
- // #2. 读取数据
- uint8_t byte_rcvd = USART_ReceiveData(USART1);
- // #3. 对数据进行处理
- if (byte_rcvd == '0')
- GPIO_WriteBit(GPIOA, GPIO_PIN_8, Bit_RESET);
- else if (byte_rcvd == '1')
- GPIO_WriteBit(GPIOA, GPIO_PIN_8, Bit_SET);
- }
- }