中微BAT32G137-GPIO简介
BAT32G137所有端口按类型分为 5 种,分别是类型 1~类型 5,对应情况如下:
类型 1:双向 I/O 功能
类型 2:NOD 功能,对应管脚 P60-P63
类型 3:只有输入功能,如时钟,对应管脚 P121-P124
类型 4:只有输出功能,对应管脚 P130
类型 5:RESET 功能,对应管脚 RESETB
大部分的类型是双向IO
实验功能:
循环翻转GPIO
软件实现
- /***********************************************************************************************************************
- * Copyright (C) All rights reserved.
- ***********************************************************************************************************************/
- /***********************************************************************************************************************
- * [url=home.php?mod=space&uid=1455510]@file[/url] gpio.h
- * [url=home.php?mod=space&uid=2666770]@Brief[/url] This file implements device driver for GPIO module.
- * [url=home.php?mod=space&uid=644434]@version[/url] 1.0.0
- * @date 2019/12/24
- ***********************************************************************************************************************/
- #ifndef GPIO_H
- #define GPIO_H
- #ifndef __CORE_CM0PLUS_H_GENERIC
- #include "core_cm0plus.h"
- #endif
- /***********************************************************************************************************************
- Macro definitions (Register bit)
- ***********************************************************************************************************************/
- /***********************************************************************************************************************
- Macro definitions
- ***********************************************************************************************************************/
- /***********************************************************************************************************************
- Typedef definitions
- ***********************************************************************************************************************/
- typedef enum {
- PORT0 = 0,
- PORT1,
- PORT2,
- PORT3,
- PORT4,
- PORT5,
- PORT6,
- PORT7,
- PORT8,
- PORT9,
- PORT10,
- PORT11,
- PORT12,
- PORT13,
- PORT14,
-
- }PORT_TypeDef;
- typedef enum {
- PIN0 = 0,
- PIN1,
- PIN2,
- PIN3,
- PIN4,
- PIN5,
- PIN6,
- PIN7,
-
- }PIN_TypeDef;
- typedef enum {
- INPUT = 0,
- PULLUP_INPUT,
- TTL_INPUT,
- ANALOG_INPUT,
- OUTPUT,
- OPENDRAIN_OUTPUT,
-
- }PIN_ModeDef;
- #define PIN_MASK_0 ((uint8_t)(0x01)) /*!< Pin 0 selected */
- #define PIN_MASK_1 ((uint8_t)(0x02)) /*!< Pin 1 selected */
- #define PIN_MASK_2 ((uint8_t)(0x04)) /*!< Pin 2 selected */
- #define PIN_MASK_3 ((uint8_t)(0x08)) /*!< Pin 3 selected */
- #define PIN_MASK_4 ((uint8_t)(0x10)) /*!< Pin 4 selected */
- #define PIN_MASK_5 ((uint8_t)(0x20)) /*!< Pin 5 selected */
- #define PIN_MASK_6 ((uint8_t)(0x40)) /*!< Pin 6 selected */
- #define PIN_MASK_7 ((uint8_t)(0x80)) /*!< Pin 7 selected */
- /***********************************************************************************************************************
- Global functions
- ***********************************************************************************************************************/
- void GPIO_Output_Enable(__IO uint8_t *port, uint8_t pinMsk);
- void GPIO_Input_Enable(__IO uint8_t *port, uint8_t pinMsk);
- void GPIO_PullUp_Enable(__IO uint8_t *port, uint8_t pinMsk);
- void GPIO_PullUp_Disable(__IO uint8_t *port, uint8_t pinMsk);
- void GPIO_Nch_OpenDrain(__IO uint8_t *port, uint8_t pinMsk);
- void GPIO_Ttl_Input(__IO uint8_t *port, uint8_t pinMsk);
- void GPIO_Set_Value(__IO uint8_t *port, uint8_t value);
- uint8_t GPIO_Get_Value(__IO uint8_t *port);
- void PORT_Init(PORT_TypeDef PORTx,PIN_TypeDef PINx,PIN_ModeDef MODEx);
- //void PORT_SetBit(PORT_TypeDef PORTx,PIN_TypeDef PINx);
- //void PORT_ClrBit(PORT_TypeDef PORTx,PIN_TypeDef PINx);
- //void PORT_ToggleBit(PORT_TypeDef PORTx,PIN_TypeDef PINx);
- //uint8_t PORT_GetBit(PORT_TypeDef PORTx,PIN_TypeDef PINx);
- /*****************************************************************************
- ** brief PORT_SetBit
- **
- ** param [in] PORTx : PORT_TypeDef
- ** PINx: PIN_TypeDef
- ** return none
- ** note
- *****************************************************************************/
- #define PORT_SetBit(PORTx,PINx) do{
- *((uint8_t*)((uint8_t*)&PORT->P0+PORTx)) |= (1<P0+PORTx)) &= ~(1<P0+PORTx)) ^= (1<P0+PORTx)) & (1<PMS = 0x01; /*!< Digital output level of the pin is read */
- return (*port); /*!< PL = value */
- }
- /**
- * @brief Initializes the PORTx
- * [url=home.php?mod=space&uid=3142012]@param[/url] PORTx: where x can be 0~14
- * @param PINx: where x can be 0~7
- * @param MODEx: such as INPUT,PULLUP_INPUT,TTL_INPUT,ANALOG_INPUT,OUTPUT,OPENDRAIN_OUTPUT
- *
- * @retval None
- */
- void PORT_Init(PORT_TypeDef PORTx,PIN_TypeDef PINx,PIN_ModeDef MODEx)
- {
- uint8_t mode = MODEx;
- uint8_t pos = 1<PMC0+PORTx)) &= ~pos;
- *((volatile uint8_t*)(&PORT->PM0+PORTx)) |= pos;
- *((volatile uint8_t*)(&PORT->PIM0+PORTx)) &= ~pos;
- *((volatile uint8_t*)(&PORT->POM0+PORTx)) &= ~pos;
- *((volatile uint8_t*)(&PORT->PU0+PORTx)) &= ~pos;
- break;
- case PULLUP_INPUT:
- *((volatile uint8_t*)(&PORT->PMC0+PORTx)) &= ~pos;
- *((volatile uint8_t*)(&PORT->PM0+PORTx)) |= pos;
- *((volatile uint8_t*)(&PORT->PIM0+PORTx)) &= ~pos;
- *((volatile uint8_t*)(&PORT->POM0+PORTx)) &= ~pos;
- *((volatile uint8_t*)(&PORT->PU0+PORTx)) |= pos;
- break;
- case TTL_INPUT:
- *((volatile uint8_t*)(&PORT->PMC0+PORTx)) &= ~pos;
- *((volatile uint8_t*)(&PORT->PM0+PORTx)) |= pos;
- *((volatile uint8_t*)(&PORT->PIM0+PORTx)) |= pos;
- *((volatile uint8_t*)(&PORT->POM0+PORTx)) &= ~pos;
- *((volatile uint8_t*)(&PORT->PU0+PORTx)) &= ~pos;
- break;
- case ANALOG_INPUT:
- *((volatile uint8_t*)(&PORT->PMC0+PORTx)) |= pos;
- break;
- case OUTPUT:
- *((volatile uint8_t*)(&PORT->PMC0+PORTx)) &= ~pos;
- *((volatile uint8_t*)(&PORT->PM0+PORTx)) &= ~pos;
- *((volatile uint8_t*)(&PORT->PIM0+PORTx)) &= ~pos;
- *((volatile uint8_t*)(&PORT->POM0+PORTx)) &= ~pos;
- break;
- case OPENDRAIN_OUTPUT:
- *((volatile uint8_t*)(&PORT->PMC0+PORTx)) &= ~pos;
- *((volatile uint8_t*)(&PORT->PM0+PORTx)) &= ~pos;
- *((volatile uint8_t*)(&PORT->PIM0+PORTx)) &= ~pos;
- *((volatile uint8_t*)(&PORT->POM0+PORTx)) |= pos;
- break;
- }
- }
复制代码
C文件很多函数不能应用头文件的宏
建议使用 PORT_Init这个函数,可以单独初始化GPIO引脚
- #include
- #include "BAT32G137.h"
- #include "userdefine.h"
- #include "gpio.h"
- #include "sci.h"
- /***********************************************************************************************************************
- Global variables and functions
- ***********************************************************************************************************************/
- /***********************************************************************************************************************
- Global variables and functions
- ***********************************************************************************************************************/
- volatile uint32_t g_ticks;
- void delayMS(uint32_t n)
- {
- g_ticks = n;
- while(g_ticks);
- }
- unsigned char key_scan(void)
- {
- // unsigned char key_val = 0;
- if((PORT_GetBit(PORT7,PIN0) &0x01) == 0)
- {
- delayMS(5);
- if((PORT_GetBit(PORT7,PIN0) &0x01) == 0)
- {
- while((PORT_GetBit(PORT7,PIN0) &0x01) == 0)
- return 1;
- }
- }
- return 0;
- }
- int main(void)
- {
- uint32_t msCnt; // count value of 1ms
- unsigned char data_uart = 0;
- //-----------------------------------------------------------------------
- // Systick setting
- //-----------------------------------------------------------------------
- g_ticks = 1000; // 1000ms
- SystemCoreClockUpdate();
- msCnt = SystemCoreClock / 1000;
- SysTick_Config(msCnt);
-
- UART0_Init(SystemCoreClock, 115200);
- PORT_Init(PORT7,PIN1,OUTPUT);
- PORT_Init(PORT7,PIN2,OUTPUT);
- PORT_Init(PORT7,PIN0,PULLUP_INPUT);
- while(1)
- {
- //UART0_Receive
- if(key_scan())
- {
- UART0_Send('a');
- data_uart = UART0_Receive();
- UART0_Send(data_uart);
- delayMS(250);
- PORT_SetBit(PORT7,PIN1);
- delayMS(250);
- PORT_ClrBit(PORT7,PIN1);
- delayMS(250);
- PORT_SetBit(PORT7,PIN2);
- delayMS(250);
- PORT_ClrBit(PORT7,PIN2);
- }
-
- }
- }
- /***********************************************************************************************************************
- * Function Name: SysTick Handler
- * Description : Decreament the g_ticks value
- * Arguments : None
- * Return Value : None
- ***********************************************************************************************************************/
- void SysTick_Handler(void)
- {
- WDT->WDTE = 0xAC;
- g_ticks--;
- }
复制代码
|