N32是Cortex-M4内核,看用户手册发现N32G457是有位带别名区的
手册中有这样两段话:
对于N32的库中有关GPIO有GPIO_Module结构体,其中POD和PID为控制十六个管脚的输出与输入寄存器,使用位带别名区可以将单个管脚1bit的控制位扩展到32位,实现类似PA0 = 1;代码操作IO输出。
typedef struct
{
__IO uint32_t PL_CFG;
__IO uint32_t PH_CFG;
__IO uint32_t PID;
__IO uint32_t POD;
__IO uint32_t PBSC;
__IO uint32_t PBC;
__IO uint32_t PLOCK_CFG;
uint32_t RESERVED0;
__IO uint32_t DS_CFG;
__IO uint32_t SR_CFG;
} GPIO_Module;
实现手册内的公式,最终代码如下
#ifndef _ALIAS_REG_H
#define _ALIAS_REG_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include "n32g45x.h"
#define MCU_PERIPH_BASE PERIPH_BASE
#define MCU_PERIPH_BB_BASE PERIPH_BB_BASE
#define MEM_ADDR(addr) (*((volatile unsigned long *)(addr)))
#define DIFF_ADDR(addr) ((uint32_t)(addr) - (uint32_t)MCU_PERIPH_BASE)
#define BIT_BAND(addr) ((MCU_PERIPH_BB_BASE + (DIFF_ADDR(addr) << 5)))
#define BIT_REG(reg, bit) ((uint32_t *)BIT_BAND(&(reg)))[bit]
#define PA ((uint32_t *)BIT_BAND(&(GPIOA->POD)))
#define PB ((uint32_t *)BIT_BAND(&(GPIOB->POD)))
#define PC ((uint32_t *)BIT_BAND(&(GPIOC->POD)))
#define PD ((uint32_t *)BIT_BAND(&(GPIOD->POD)))
#define PE ((uint32_t *)BIT_BAND(&(GPIOE->POD)))
#define PF ((uint32_t *)BIT_BAND(&(GPIOF->POD)))
#define PG ((uint32_t *)BIT_BAND(&(GPIOG->POD)))
#define PAout(n) BIT_REG(GPIOA->POD, n)
#define PBout(n) BIT_REG(GPIOB->POD, n)
#define PCout(n) BIT_REG(GPIOC->POD, n)
#define PDout(n) BIT_REG(GPIOD->POD, n)
#define PEout(n) BIT_REG(GPIOE->POD, n)
#define PFout(n) BIT_REG(GPIOF->POD, n)
#define PGout(n) BIT_REG(GPIOG->POD, n)
#define PAin(n) BIT_REG(GPIOA->PID, n)
#define PBin(n) BIT_REG(GPIOB->PID, n)
#define PCin(n) BIT_REG(GPIOC->PID, n)
#define PDin(n) BIT_REG(GPIOD->PID, n)
#define PEin(n) BIT_REG(GPIOE->PID, n)
#define PFin(n) BIT_REG(GPIOF->PID, n)
#define PGin(n) BIT_REG(GPIOG->PID, n)
#ifdef __cplusplus
}
#endif
#endif
用位带别名区控制闪灯
while (1) {
delay_ms(500);
PA[8] ^= 1;
PB[4] ^= 1;
PB[5] ^= 1;
}
功能正常
|