forked from WCH-Templates/CH32V00x
Updated example program to be less useless
This commit is contained in:
parent
58b4d4951d
commit
d306a7ea53
1 changed files with 26 additions and 56 deletions
82
User/main.c
82
User/main.c
|
@ -7,19 +7,6 @@
|
||||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
/*
|
|
||||||
*@Note
|
|
||||||
轮询收发模式例程:
|
|
||||||
Master:USART1_Tx(PD5)、USART1_Rx(PD6)。
|
|
||||||
本例程演示 USART1 收到CH341发送的数据取反发出(波特率 115200)。
|
|
||||||
|
|
||||||
注:
|
|
||||||
硬件连线:PD5 —— Rx
|
|
||||||
PD6 —— Tx
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
|
|
||||||
|
@ -27,40 +14,23 @@
|
||||||
|
|
||||||
|
|
||||||
/* Global Variable */
|
/* Global Variable */
|
||||||
vu8 val;
|
|
||||||
|
|
||||||
/*********************************************************************
|
/*********************************************************************
|
||||||
* @fn USARTx_CFG
|
* @fn GPIO_Toggle_INIT
|
||||||
*
|
*
|
||||||
* @brief Initializes the USART2 & USART3 peripheral.
|
* @brief Initializes GPIOD.4
|
||||||
*
|
*
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
void USARTx_CFG(void)
|
void GPIO_LED_Init(void) {
|
||||||
{
|
GPIO_InitTypeDef GPIO_InitStructure = {0};
|
||||||
GPIO_InitTypeDef GPIO_InitStructure = {0};
|
|
||||||
USART_InitTypeDef USART_InitStructure = {0};
|
|
||||||
|
|
||||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_USART1, ENABLE);
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
|
||||||
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
|
||||||
/* USART1 TX-->D.5 RX-->D.6 */
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
|
||||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
||||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
||||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
||||||
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
||||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
|
|
||||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
||||||
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
|
||||||
|
|
||||||
USART_InitStructure.USART_BaudRate = 115200;
|
|
||||||
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
|
||||||
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
|
||||||
USART_InitStructure.USART_Parity = USART_Parity_No;
|
|
||||||
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
|
||||||
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
|
|
||||||
|
|
||||||
USART_Init(USART1, &USART_InitStructure);
|
|
||||||
USART_Cmd(USART1, ENABLE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*********************************************************************
|
/*********************************************************************
|
||||||
|
@ -70,27 +40,27 @@ void USARTx_CFG(void)
|
||||||
*
|
*
|
||||||
* @return none
|
* @return none
|
||||||
*/
|
*/
|
||||||
int main(void)
|
int main(void) {
|
||||||
{
|
|
||||||
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
|
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
|
||||||
|
SystemCoreClockUpdate();
|
||||||
Delay_Init();
|
Delay_Init();
|
||||||
|
#if (SDI_PRINT == SDI_PR_OPEN)
|
||||||
|
SDI_Printf_Enable();
|
||||||
|
#else
|
||||||
USART_Printf_Init(115200);
|
USART_Printf_Init(115200);
|
||||||
printf("SystemClk:%d\r\n",SystemCoreClock);
|
#endif
|
||||||
|
printf("SystemClk: %ld\r\n", SystemCoreClock);
|
||||||
|
printf("DeviceID: %08x\r\n", DBGMCU_GetDEVID());
|
||||||
|
printf("ChipID: %08x\r\n", DBGMCU_GetCHIPID());
|
||||||
|
|
||||||
USARTx_CFG();
|
GPIO_LED_Init();
|
||||||
|
|
||||||
while(1)
|
while(1) {
|
||||||
{
|
Delay_Ms(1000);
|
||||||
|
printf("On\r\n");
|
||||||
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
|
GPIO_WriteBit(GPIOD, GPIO_Pin_4, Bit_SET);
|
||||||
{
|
Delay_Ms(1000);
|
||||||
/* waiting for receiving finish */
|
printf("Off\r\n");
|
||||||
}
|
GPIO_WriteBit(GPIOD, GPIO_Pin_4, Bit_RESET);
|
||||||
val = (USART_ReceiveData(USART1));
|
|
||||||
USART_SendData(USART1, ~val);
|
|
||||||
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
|
|
||||||
{
|
|
||||||
/* waiting for sending finish */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue