笔记:(二)GPIO引脚驱动
Author:家有仙妻谢掌柜
Date:2021/2/26
控制GPIO引脚的主要寄存器都在下图中:
合泰的BH66F2652单片机有两种封装,对应的GPIO引脚个数也是不用的,这里以最大引脚数量来写封装函数代码。
该封装引脚没有PORTC和PB7引脚。
该封装引脚包含PORTC和PB7引脚。
一、引脚输入输出选择位
void gpioInput(char gpioPin)
{
switch(gpioPin)
{
//POARTA
case 0: _pac0 = 1; break;
case 1: _pac1 = 1; break;
case 2: _pac2 = 1; break;
case 3: _pac3 = 1; break;
case 4: _pac4 = 1; break;
case 5: _pac5 = 1; break;
case 6: _pac6 = 1; break;
case 7: _pac7 = 1; break;
//POARTB
case 8: _pbc0 = 1; break;
case 9: _pbc1 = 1; break;
case 10: _pbc2 = 1; break;
case 11: _pbc3 = 1; break;
case 12: _pbc4 = 1; break;
case 13: _pbc5 = 1; break;
case 14: _pbc6 = 1; break;
case 15: _pbc7 = 1; break;
//POARTC
case 16: _pcc0 = 1; break;
}
}
void gpioOutput(char gpioPin)
{
switch(gpioPin)
{
//POARTA
case 0: _pac0 = 0; break;
case 1: _pac1 = 0; break;
case 2: _pac2 = 0; break;
case 3: _pac3 = 0; break;
case 4: _pac4 = 0; break;
case 5: _pac5 = 0; break;
case 6: _pac6 = 0; break;
case 7: _pac7 = 0; break;
//POARTB
case 8: _pbc0 = 0; break;
case 9: _pbc1 = 0; break;
case 10: _pbc2 = 0; break;
case 11: _pbc3 = 0; break;
case 12: _pbc4 = 0; break;
case 13: _pbc5 = 0; break;
case 14: _pbc6 = 0; break;
case 15: _pbc7 = 0; break;
//POARTC
case 16: _pcc0 = 0; break;
}
}
二、引脚上拉使能除能选择位
void gpioPullUpEnable(char gpioPin)
{
switch(gpioPin)
{
//POARTA
case 0: _papu0 = 1; break;
case 1: _papu1 = 1; break;
case 2: _papu2 = 1; break;
case 3: _papu3 = 1; break;
case 4: _papu4 = 1; break;
case 5: _papu5 = 1; break;
case 6: _papu6 = 1; break;
case 7: _papu7 = 1; break;
//POARTB
case 8: _pbpu0 = 1; break;
case 9: _pbpu1 = 1; break;
case 10: _pbpu2 = 1; break;
case 11: _pbpu3 = 1; break;
case 12: _pbpu4 = 1; break;
case 13: _pbpu5 = 1; break;
case 14: _pbpu6 = 1; break;
case 15: _pbpu7 = 1; break;
//POARTC
case 16: _pcpu0 = 1; break;
}
}
void gpioPullUpDisable(char gpioPin)
{
switch(gpioPin)
{
//POARTA
case 0: _papu0 = 0; break;
case 1: _papu1 = 0; break;
case 2: _papu2 = 0; break;
case 3: _papu3 = 0; break;
case 4: _papu4 = 0; break;
case 5: _papu5 = 0; break;
case 6: _papu6 = 0; break;
case 7: _papu7 = 0; break;
//POARTB
case 8: _pbpu0 = 0; break;
case 9: _pbpu1 = 0; break;
case 10: _pbpu2 = 0; break;
case 11: _pbpu3 = 0; break;
case 12: _pbpu4 = 0; break;
case 13: _pbpu5 = 0; break;
case 14: _pbpu6 = 0; break;
case 15: _pbpu7 = 0; break;
//POARTC
case 16: _pcpu0 = 0; break;
}
}
三、引脚拉高拉低电平选择位
void gpioHigh(char gpioPin)
{
switch(gpioPin)
{
//POARTA
case 0: _pa0 = 1; break;
case 1: _pa1 = 1; break;
case 2: _pa2 = 1; break;
case 3: _pa3 = 1; break;
case 4: _pa4 = 1; break;
case 5: _pa5 = 1; break;
case 6: _pa6 = 1; break;
case 7: _pa7 = 1; break;
//POARTB
case 8: _pb0 = 1; break;
case 9: _pb1 = 1; break;
case 10: _pb2 = 1; break;
case 11: _pb3 = 1; break;
case 12: _pb4 = 1; break;
case 13: _pb5 = 1; break;
case 14: _pb6 = 1; break;
case 15: _pb7 = 1; break;
//POARTC
case 16: _pc0 = 1; break;
}
}
void gpioLow(char gpioPin)
{
switch(gpioPin)
{
//POARTA
case 0: _pa0 = 0; break;
case 1: _pa1 = 0; break;
case 2: _pa2 = 0; break;
case 3: _pa3 = 0; break;
case 4: _pa4 = 0; break;
case 5: _pa5 = 0; break;
case 6: _pa6 = 0; break;
case 7: _pa7 = 0; break;
//POARTB
case 8: _pb0 = 0; break;
case 9: _pb1 = 0; break;
case 10: _pb2 = 0; break;
case 11: _pb3 = 0; break;
case 12: _pb4 = 0; break;
case 13: _pb5 = 0; break;
case 14: _pb6 = 0; break;
case 15: _pb7 = 0; break;
//POARTC
case 16: _pc0 = 0; break;
}
}
四、引脚唤醒选择位
void gpioWakeup(char gpioPin,char enORdis)
{
switch(gpioPin)
{
//POARTA
case 0: if(!enORdis) _pawu0 = 0; else _pawu0 = 1; break;
case 1: if(!enORdis) _pawu1 = 0; else _pawu1 = 1; break;
case 2: if(!enORdis) _pawu2 = 0; else _pawu2 = 1; break;
case 3: if(!enORdis) _pawu3 = 0; else _pawu3 = 1; break;
case 4: if(!enORdis) _pawu4 = 0; else _pawu4 = 1; break;
case 5: if(!enORdis) _pawu5 = 0; else _pawu5 = 1; break;
case 6: if(!enORdis) _pawu6 = 0; else _pawu6 = 1; break;
case 7: if(!enORdis) _pawu7 = 0; else _pawu7 = 1; break;
}
}
#ifndef _GPIO_H
#define _GPIO_H
#include "BH66F2652.h"
//POARTA
#define PA0 0
#define PA1 1
#define PA2 2
#define PA3 3
#define PA4 4
#define PA5 5
#define PA6 6
#define PA7 7
//POARTB
#define PB0 8
#define PB1 9
#define PB2 10
#define PB3 11
#define PB4 12
#define PB5 13
#define PB6 14
#define PB7 15
//POARTC
#define PC0 16
extern void gpioInput(char gpioPin);
extern void gpioOutput(char gpioPin);
extern void gpioPullUpEnable(char gpioPin);
extern void gpioPullUpDisable(char gpioPin);
extern void gpioHigh(char gpioPin);
extern void gpioLow(char gpioPin);
extern void gpioWakeup(char gpioPin,char enORdis);
#endif