如何自制51封装库代码

//GPIO.H
#ifndef _GPIO_H_
#define _GPIO_H_ enum pn{ p0 = 0, p1 = 1, p2 = 2, p3 = 3, }; void gao(unsigned char pn, unsigned char pin); void di(unsigned char pn, unsigned char pin); #endif
//gpio.c
#include <reg52.h>
#include <GPIO.h>

void gao(unsigned char pn,
    unsigned char pin)
    {
        unsigned char i = 1;
        i = i<<pin;
        switch(pn){
                case 0:
                P0 |= i;
                break;
                case 1:
                P1 |= i;
                break;
                case 2:
                P2 |= i;
                break;
                case 3:
                P3 |= i;
                break;
        }
    }
    void di(unsigned char pn,
    unsigned char pin)
    {
        unsigned char i=1;
        i = i<<pin;
        switch(pn){
                case 0:
                P0 &= ~i;
                break;
                case 1:
                P1 &= ~i;
                break;
                case 2:
                P2 &= ~i;
                break;
                case 3:
                P3 &= ~i;
                break;
        }
    }
//main.c


#include <reg52.h>
#include <GPIO.h>
void delay(int n){
    int i,j;
    for(i=0;i<n;i++)
    for(j=0;j<1000;j++);
}
void main(){
    int j;
    while(1){
        for(j=0;j<8;j++){
            gao(p2,j);
            delay(100);
            di(p2,j);
            delay(100);
        }
    }
}

 

posted on 2017-09-22 16:13  梁锦杰  阅读(676)  评论(0编辑  收藏  举报

导航

欢迎进入我的博客!