单片机实例——I LOVE U

#include
#include

typedef unsigned int u16;
typedef unsigned char u8;

sbit butt = P3^2;  //独立按键,连接了外部中断0

sbit RCLK = P3^5;
sbit SRCLK = P3^6;
sbit SER = P3^4;  //HC595芯片

u8 code led_H[] = {0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//点阵的列
u8 code ledL_I[] = {0x00,0x81,0x81,0xff,0xff,0x81,0x81,0x00}; //I的行的集合
u8 code ledL_LOVE[] = {0x30,0x78,0xfe,0x7f,0x7f,0xfe,0x78,0x30};//爱心的集合
u8 code ledL_U[] = {0x00,0xff,0xff,0x03,0x03,0xff,0xff,0x00};    //U的集合

u8 flag=0;//    标志位flag = 0;


void delay(u16 i)   //延时函数
{
    while(i--);
}

void HC595(u8 dat)  //HC595芯片
{
    u8 a;
    RCLK = 0;
    SRCLK = 0;         
    for(a=0;a<8;a++){
        SER = dat>>7;
        dat <<= 1;
        SRCLK = 1;
        _nop_();
        _nop_();
        SRCLK = 0;      
    }
    RCLK = 1;
    _nop_();
    _nop_();
    RCLK = 0;
}

void Int0_init() //提前把外部中断0的参数准备好
{
    EA = 1;
    IT0 = 1;
    EX0 = 1;
}

void main() //主函数
{   
    Int0_init();
    while(1){
        u8 j;
        switch(flag){  //switch语句
              case 0:for(j=0;j<8;j++){
                       HC595(ledL_I[j]);
                       P0 = led_H[j];
                       delay(100);
                       HC595(0x00);
                       };break;
              case 1:for(j=0;j<8;j++){
                        HC595(ledL_LOVE[j]);
                        P0 = led_H[j];
                        delay(100);
                        HC595(0x00);
                        };break;
              case 2:for(j=0;j<8;j++){
                        HC595(ledL_U[j]);
                        P0 = led_H[j];
                        delay(100);
                        HC595(0x00);
                        };break;
        }
    }
}

void INT() interrupt 0
{   
    if(butt==0){
        delay(1000);
        if(butt==0){//按键按下
            if(flag<2) flag++;
            else flag=0;
        
        }
    }
}

//状态:I LOVE U(每按一下点阵就出来一个)  

你可能感兴趣的:(单片机实例——I LOVE U)