输出
#ifndef C51_GPIO_H
#define C51_GPIO_H
#include "reg52.h"
typedef enum
{
P_0 = 0,
P_1,
P_2,
P_3
}GPIO_TypeDef;
typedef enum
{
GPIO_Pin_0 = 0x00,
GPIO_Pin_1 = 0x01,
GPIO_Pin_2 = 0x02,
GPIO_Pin_3 = 0x03,
GPIO_Pin_4 = 0x04,
GPIO_Pin_5 = 0x05,
GPIO_Pin_6 = 0x06,
GPIO_Pin_7 = 0x07,
}GPIO_PinNum_t;
typedef enum
{
Bit_RESET = 0,
Bit_SET
}BitAction;
void GPIO_Write(GPIO_TypeDef P_x, GPIO_PinNum_t GPIO_Pin, BitAction BitVal);
unsigned char GPIO_ReadInputDataBit(GPIO_TypeDef P_x, GPIO_PinNum_t GPIO_Pin);
#endif
输入
#include "c51_gpio.h"
void GPIO_Write(GPIO_TypeDef P_x, GPIO_PinNum_t GPIO_Pin, BitAction BitVal)
{
switch(P_x)
{
case P_0:
P0 = P0 & (~(0x01 << GPIO_Pin));
P0 = P0 | (BitVal << GPIO_Pin);
break;
case P_1:
P1 = P1 & (~(0x01 << GPIO_Pin));
P1 = P1 | (BitVal << GPIO_Pin);
break;
case P_2:
P2 = P2 & (~(0x01 << GPIO_Pin));
P2 = P2 | (BitVal << GPIO_Pin);
break;
case P_3:
P3 = P3 & (~(0x01 << GPIO_Pin));
P3 = P3 | (BitVal << GPIO_Pin);
break;
default:
break;
}
}
unsigned char GPIO_ReadInputDataBit(GPIO_TypeDef P_x, GPIO_PinNum_t GPIO_Pin)
{
unsigned char bitstatus = 0x00;
switch(P_x)
{
case P_0:
bitstatus = P0 & GPIO_Pin;
break;
case P_1:
bitstatus = P1 & GPIO_Pin;
break;
case P_2:
bitstatus = P2 & GPIO_Pin;
break;
case P_3:
bitstatus = P3 & GPIO_Pin;
break;
default:
break;
}
if(bitstatus != Bit_RESET)
{
bitstatus = Bit_SET;
}
return bitstatus;
}