Homemade 51 MCU package library

Publisher:EtherealHeartLatest update time:2024-08-14 Source: cnblogs Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The following contains: three files, main.c, gpoi.c, gpio.h


1 /*

2 * Main function

3 */

4

5 #include

6 #include

7

8 void main()

9

10 {

11 P2 = 0;

12 setBit(GPIO_2,3); //GPIO_2 means select P2 port, 3 means select P2.3 bit

13 while(1);

14 }


1 /*

2 *gpio.c file

3 */

4

5

6 #include

7 #include

8

9 void setBit(unsigned char gpio, unsigned char pin)

10 {

11 unsigned char i = 1;//0000 0001

12 i = i<

13 switch(gpio) //Select P0 P1 P2 P3

14 {

15 case 0:

16 P0 |=i;

17 break;

18 case 1:

19 P1 |=i;

20 break;

21 case 2:

22 P2 |=i;

23 break;

24 case 3:

25 P3 |=i;

26 break;

27

28 }

29 }

30 void resetBit(unsigned char gpio, unsigned char pin)

31 {

32 unsigned char i = 1; //0000 0001

33 i = i<

34 i = ~i;//reverse 1111 1011

35 switch (gpio)

36 {

37 case 0:

38 P0 &=i; //Bitwise AND operation requires two 1s to be 1 1111 1011 So the third bit is definitely 0, output low level

39 break;

40 case 1:

41 P1 &=i;

42 break;

43 case 2:

44 P2 &=i;

45 break;

46 case 3:

47 P3 &=i;

48 break;

49

50 }

51 }


1 /*

2 *gpio.h file

3 */

4

5 #ifndef _GPIO_H_

6 #define _GPIO_H_

7 // Enumeration 0 1 2 3 represents P0 P2 P3 P4

8 enum gpio{

9 GPIO_0 = 0, GPIO_1 = 1,

10 GPIO_2 = 2, GPIO_3 = 3

11 };

12 /*

13 *Set a pin to high level

14 *@param gpio: enumeration constant gpio

15 *@param pin: the pin number corresponding to each IO register

16 */

17

18 void etBit(

19 unsigned char gpio,

20 unsigned char pin);

twenty one

22 void resetBit(

23 unsigned char gpio,

24 unsigned char pin);

25 #endif


Reference address:Homemade 51 MCU package library

Previous article:Flashing LED lights
Next article:Customize the reg52.h header file (key knowledge points of microcontroller learning)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号