天天看點

HC89S003F4管腳快速設定函數GPIO,友善懶人

不用記那麼多寄存器的名稱了,直接輸入管腳号和管腳狀态就可以了。例子在最下面幾行

變量 P 被廠家用了,隻能使用GPIO了。

本函數占用約200位元組,地方不夠的慎用

#define P00  0

#define P01  1

#define P02 2

#define P03 3

#define P04 4

#define P05 5 

#define P06 6

#define P07 7 

#define P20 20

#define P21 21

#define P22 22

#define P23 23

#define P24 24

#define P25 25

#define P26 26

#define P27 27 

#define P10 10 

#define P11 11

#define IO_IN_FL_NO_SMT 0x00   //!<0000輸入(無SMT)

#define IO_IN_PD_NO_SMT 0x01   //!<0001帶下拉輸入(無SMT) P2.3/P2.4/P2.5/P2.7不支援此功能

#define IO_IN_PU_NO_SMT 0x02   //!<0010帶上拉輸入(無SMT) P2.3/P2.4/P2.5/P2.7不支援此功能

#define IO_IN_AN  0x03   //!<0011帶模拟輸入

#define IO_IN_SMT  0x04   //!<0100輸入(SMT)

#define IO_IN_PD_SMT  0x05   //!<0101帶下拉輸入(SMT)

#define IO_IN_PU_SMT  0x06   //!<0110帶上拉輸入(SMT)

#define IO_IN_AN_PU_PD 0x07   //!<0111帶上下拉模拟輸入  僅P2.3/P2.4/P2.5/P2.7支援此功能

#define IO_OUT_PP  0x08   //!<1x00推挽輸出

#define IO_OUT_OD  0x09   //!<1x01開漏輸出

#define IO_OUT_PU  0x0A   //!<1x10帶上拉開漏輸出

//P2M1 = P2M1&0x0F|0x80; //P23設定為推挽輸出

//GPIO(P23,IO_OUT_PP);

void GPIO(u8 m,u8 n)  //HC89S003F4管腳快速設定函數by abin 。 m為管腳 P0.1=01  P1.0=10 等,n為管腳狀态,見上面或資料手冊說明

{

switch (m)

{

case 00:P0M0 = P0M0&0xF0|n;break;

case 01:P0M0 = P0M0&0x0F|(n*16);break;

case 02:P0M1 = P0M1&0xF0|n;break;

case 03:P0M1 = P0M1&0x0F|(n*16);break;

case 04:P0M2 = P0M2&0xF0|n;break;

case 05:P0M2 = P0M2&0x0F|(n*16);break;

case 06:P0M3 = P0M3&0xF0|n;break;

case 07:P0M3 = P0M3&0x0F|(n*16);break;

case 20:P2M0 = P2M0&0xF0|n;break;

case 21:P2M0 = P2M0&0x0F|(n*16);break;

case 22:P2M1 = P2M1&0xF0|n;break;

case 23:P2M1 = P2M1&0x0F|(n*16);break;

case 24:P2M2 = P2M2&0xF0|n;break;

case 25:P2M2 = P2M2&0x0F|(n*16);break;

case 26:P2M3 = P2M3&0xF0|n;break;

case 27:P2M3 = P2M3&0x0F|(n*16);break;

case 10:P1M0 = P1M0&0xF0|n;break;

case 11:P1M0 = P1M0&0x0F|(n*16);break;

default:break;

}

}

        P2M0 = P2M0&0x0F|0x80;                                //P21設定為推挽輸出

        GPIO(P21,IO_OUT_PP);

        P0M1 = P0M1&0x0F|0x20;                                //P03設定為上拉輸入

        GPIO(P03,IO_IN_PU_NO_SMT);

繼續閱讀