本帖最后由 RISCVLAR 于 2021-8-19 10:55 编辑
CH32V103应用教程——PD0、1引脚的复用 本章教程主要讲述如何进行PD0、1引脚的复用。正常情况下,PD0、1引脚是作为外部晶振引脚使用的,但是某些情况下,我们需要使用重映射功能,把其复用为普通引脚作为输入输出引脚使用。
1、GPIO简介 由数据手册引脚描述可知,外部晶振两个引脚可重映射为PD0、1引脚使用。
2、硬件设计 可使用杜邦线将PD0、1引脚与测试板LED灯连接,测试其输出功能,或与VCC或者GND连接测试其输入功能。
3、软件设计 本次实验具体程序如下,由于代码量较小,均放在main函数,main函数如下: Main.c文件 - /********************************** (C) COPYRIGHT *******************************
- * File Name : main.c
- * Author : WCH
- * Version : V1.0.0
- * Date : 2020/04/30
- * Description : Main program body.
- *******************************************************************************/
- /*
- *@Note
- GPIO例程:
- PA0推挽输出。
-
- */
- #include "debug.h"
- #define in 1
- //#define out 0
- /*******************************************************************************
- * Function Name : GPIO_Toggle_INIT
- * Description : Initializes GPIOA.0
- * Input : None
- * Return : None
- *******************************************************************************/
- void GPIO_Toggle_INIT(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
- GPIO_PinRemapConfig(GPIO_Remap_PD01,ENABLE);
- #ifdef out
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- GPIO_SetBits(GPIOD, GPIO_Pin_0);
- GPIO_SetBits(GPIOD, GPIO_Pin_1);
- #elif in
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- #endif
- }
- /*******************************************************************************
- * Function Name : main
- * Description : Main program.
- * Input : None
- * Return : None
- *******************************************************************************/
- int main(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- Delay_Init();
- USART_Printf_Init(115200);
- printf("SystemClk:%d\r\n",SystemCoreClock);
- printf("GPIO Toggle TEST\r\n");
- GPIO_Toggle_INIT();
- while(1)
- {
- #ifdef out
- Delay_Ms(500);
- GPIO_ResetBits(GPIOD, GPIO_Pin_0);
- GPIO_ResetBits(GPIOD, GPIO_Pin_1);
- printf("111111\r\n");
- Delay_Ms(500);
- GPIO_SetBits(GPIOD, GPIO_Pin_0);
- GPIO_SetBits(GPIOD, GPIO_Pin_1);
- printf("222222\r\n");
- Delay_Ms(500);
- #elif in
- if((GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_0)==1)||(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_1)==1))
- {
- printf("up\r\n");
- Delay_Ms(500);
- }
- else
- {
- printf("down\r\n");
- Delay_Ms(500);
- }
- #endif
- }
- }
Main.c文件主要包含两个函数:GPIO_Toggle_INIT函数和main函数,GPIO_Toggle_INIT函数主要进行PD0、1引脚的配置,注意作为重映射功能要使用GPIO_PinRemapConfig(GPIO_Remap_PD01,ENABLE)函数。main函数就是主函数,进行输入输出的运行。
4、下载验证 将编译好的程序下载到开发板并复位,当作为输出时,可看到测试板LED灯闪烁,同时串口打印如下: 当作为输入使用时,当将PD0、1引脚接GND或者VCC时,串口打印如下:
|