Thông báo

Collapse
No announcement yet.

các bác giúp e cái code đồng hồ led ma trận này với,

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • các bác giúp e cái code đồng hồ led ma trận này với,

    #include <REGX52.H>
    sbit DATA1 = P3^0;
    sbit SCK1 = P3^1;
    sbit LACH1 = P3^2;
    sbit set=P3^4;
    sbit up=P3^5;
    sbit down=P3^6;
    void delay(unsigned char t)
    {
    unsigned char x;
    unsigned int y;
    for(x=0;x<t;x++)
    {
    for(y=0;y<100;y++);
    }
    }
    unsigned char hour, minute, second,c;
    unsigned char code max0[]={
    0xff,0xFF,0x81,0x7A,0x76,0x6E,0x81,0xFF,
    0xff,0xFF,0x7B,0x7D,0x00,0x7F,0x7F,0xFF,
    0xff,0xFF,0x3D,0x5E,0x6E,0x76,0x79,0xFF,
    0xff,0xFF,0xBD,0x7E,0x6E,0x66,0x99,0xFF,
    0xff,0xEF,0xE7,0xEB,0xED,0x00,0xEF,0xFF,
    0xff,0xFF,0xB0,0x76,0x76,0x76,0x8E,0xFF,
    0xff,0xFF,0x81,0x6E,0x76,0x76,0x8D,0xFF,
    0xff,0xFF,0xFE,0x0E,0xF6,0xFA,0xFC,0xFF,
    0xff,0xFF,0x89,0x76,0x76,0x76,0x89,0xFF,
    0xff,0xFF,0xB1,0x6E,0x6E,0x76,0x81,0xFF };
    unsigned char code ma0x[]={ 0xff,0xff,0xff,0x81,0x7A,0x76,0x6E,0x81,
    0xff,0xff,0xff,0x7b,0x7D,0x00,0x7F,0x7F,
    0xff,0xff,0xff,0x39,0x5E,0x6E,0x76,0x79,
    0xff,0xff,0xff,0xBD,0x7E,0x6E,0x66,0x99,
    0xff,0xff,0xEF,0xE7,0xEB,0xED,0x00,0xEF,
    0xff,0xff,0xff,0xB0,0x76,0x76,0x76,0x8E,
    0xff,0xff,0xff,0x81,0x6E,0x76,0x76,0x8D,
    0xff,0xff,0xff,0xFE,0x0E,0xF6,0xFA,0xFC,
    0xff,0xff,0xff,0x89,0x76,0x76,0x76,0x89,
    0xff,0xff,0xff,0xB1,0x6E,0x6E,0x76,0x81 };
    unsigned char code magiay[]={
    0xFF,0x07,0x77,0x07,0xFF,0x07,0x77,0x07,
    0xFF,0x07,0x77,0x07,0xFF,0x07,0xFF,0xFF,
    0xFF,0x07,0x77,0x07,0xFF,0x17,0x57,0x47,
    0xFF,0x07,0x77,0x07,0xFF,0x57,0x57,0x07,
    0xFF,0x07,0x77,0x07,0xFF,0xC7,0xDF,0x07,
    0xFF,0x07,0x77,0x07,0xFF,0x47,0x57,0x17,
    0xFF,0x07,0x77,0x07,0xFF,0x07,0x57,0x17,
    0xFF,0x07,0x77,0x07,0xFF,0xF7,0xF7,0x07,
    0xFF,0x07,0x77,0x07,0xFF,0x07,0x57,0x07,
    0xFF,0x07,0x77,0x07,0xFF,0x47,0x57,0x07,
    0xFF,0xFF,0xFF,0x07,0xFF,0x07,0x77,0x07,
    0xFF,0xFF,0xFF,0x07,0xFF,0x07,0xFF,0xFF,
    0xFF,0xFF,0xFF,0x07,0xFF,0x17,0x57,0x47,
    0xFF,0xFF,0xFF,0x07,0xFF,0x57,0x57,0x07,
    0xFF,0xFF,0xFF,0x07,0xFF,0xC7,0xDF,0x07,
    0xFF,0xFF,0xFF,0x07,0xFF,0x47,0x57,0x17,
    0xFF,0xFF,0xFF,0x07,0xFF,0x07,0x57,0x17,
    0xFF,0xFF,0xFF,0x07,0xFF,0xF7,0xF7,0x07,
    0xFF,0xFF,0xFF,0x07,0xFF,0x07,0x57,0x07,
    0xFF,0xFF,0xFF,0x07,0xFF,0x47,0x57,0x07,
    0xFF,0x17,0x57,0x47,0xFF,0x07,0x77,0x07 ,
    0xFF,0x17,0x57,0x47,0xFF,0x07,0xFF,0xFF,
    0xFF,0x17,0x57,0x47,0xFF,0x17,0x57,0x47,
    0xFF,0x17,0x57,0x47,0xFF,0x57,0x57,0x07,
    0xFF,0x17,0x57,0x47,0xFF,0xC7,0xDF,0x07,
    0xFF,0x17,0x57,0x47,0xFF,0x47,0x57,0x17,
    0xFF,0x17,0x57,0x47,0xFF,0x07,0x57,0x17,
    0xFF,0x17,0x57,0x47,0xFF,0xF7,0xF7,0x07,
    0xFF,0x17,0x57,0x47,0xFF,0x07,0x57,0x07,
    0xFF,0x17,0x57,0x47,0xFF,0x47,0x57,0x07 ,
    0xFF,0x57,0x57,0x07,0xFF,0x07,0x77,0x07 ,
    0xFF,0x57,0x57,0x07,0xFF,0x07,0xFF,0xFF ,
    0xFF,0x57,0x57,0x07,0xFF,0x17,0x57,0x47 ,
    0xFF,0x57,0x57,0x07,0xFF,0x57,0x57,0x07 ,
    0xFF,0x57,0x57,0x07,0xFF,0xC7,0xDF,0x07 ,
    0xFF,0x57,0x57,0x07,0xFF,0x47,0x57,0x17 ,
    0xFF,0x57,0x57,0x07,0xFF,0x07,0x57,0x17 ,
    0xFF,0x57,0x57,0x07,0xFF,0xF7,0xF7,0x07 ,
    0xFF,0x57,0x57,0x07,0xFF,0x07,0x57,0x07 ,
    0xFF,0x57,0x57,0x07,0xFF,0x47,0x57,0x07 ,
    0xFF,0xC7,0xDF,0x07,0xFF,0x07,0x77,0x07 ,
    0xFF,0xC7,0xDF,0x07,0xFF,0x07,0xFF,0xFF ,
    0xFF,0xC7,0xDF,0x07,0xFF,0x17,0x57,0x47 ,
    0xFF,0xC7,0xDF,0x07,0xFF,0x57,0x57,0x07 ,
    0xFF,0xC7,0xDF,0x07,0xFF,0xC7,0xDF,0x07 ,
    0xFF,0xC7,0xDF,0x07,0xFF,0x47,0x57,0x17 ,
    0xFF,0xC7,0xDF,0x07,0xFF,0x07,0x57,0x17 ,
    0xFF,0xC7,0xDF,0x07,0xFF,0xF7,0xF7,0x07 ,
    0xFF,0xC7,0xDF,0x07,0xFF,0x07,0x57,0x07 ,
    0xFF,0x47,0x57,0x17,0xFF,0x07,0x77,0x07 ,
    0xFF,0x47,0x57,0x17,0xFF,0x07,0xFF,0xFF ,
    0xFF,0x47,0x57,0x17,0xFF,0x17,0x57,0x47,
    0xFF,0x47,0x57,0x17,0xFF,0x57,0x57,0x07,
    0xFF,0x47,0x57,0x17,0xFF,0xC7,0xDF,0x07 ,
    0xFF,0x47,0x57,0x17,0xFF,0x47,0x57,0x17,
    0xFF,0x47,0x57,0x17,0xFF,0x07,0x57,0x17,
    0xFF,0x47,0x57,0x17,0xFF,0xF7,0xF7,0x07,
    0xFF,0x47,0x57,0x17,0xFF,0x07,0x57,0x07,
    0xFF,0x47,0x57,0x17,0xFF,0x47,0x57,0x07 };
    unsigned char cot[]={0x01,0x02,0x04,0x08,0x10,0x2,0x40,0x80};
    unsigned char kt=0,a=0,i,k,x=0;
    bit set0=1,set1=1,up1=1,up0=1,down0=1,down1=1;
    sbit SCL = P1^0;
    sbit SDA = P1^1;


    //-----------------------------------------
    void SCL_high(){
    SCL = 1;
    delay(1);
    }

    //------------------------------------------
    void SCL_low(){
    SCL = 0;
    delay(1);
    }

    //-------------------------------------------
    void I2C_Start(){
    SDA = 1;
    SCL = 1;
    SDA = 0;
    delay(1);
    SCL = 0;
    SDA = 1;
    }

    //--------------------------------------------
    void I2C_Stop(){
    SDA = 0;
    SCL_high();
    SDA = 1;
    }

    //----------------------------------------------
    bit I2C_Write(unsigned char dat){
    unsigned char i;
    bit outbit;
    for (i = 1; i <= 8; i++){
    outbit=dat&0x80;
    SDA = outbit;
    dat = dat << 1;
    SCL_high();
    SCL_low();
    }
    SDA = 1;
    SCL_high();
    outbit = SDA;
    SCL_low();
    return(outbit);
    }

    //----------------------------------------------------
    unsigned char I2C_Read(bit ack){
    unsigned char i, dat;
    bit inbit;

    dat = 0;
    for(i=1;i<=8;i++) {
    SCL_high();
    inbit = SDA;
    dat = dat << 1;
    dat = dat | inbit;
    SCL_low();
    }
    if (ack) SDA = 0;
    else SDA = 1;
    SCL_high();
    SCL = 0;
    SDA = 1;
    delay(1);
    return(dat);
    }

    //-------------------------------------------------------------
    void rtc_write(unsigned char add, unsigned char dat){
    I2C_Start();
    I2C_Write(0xd0);
    I2C_Write(add);
    I2C_Write(((dat/10)<<4)|(dat%10));
    I2C_Stop();
    }

    unsigned char rtc_read(unsigned char add){
    unsigned char dat;
    I2C_Start();
    I2C_Write(0xd0);
    I2C_Write(add);
    I2C_Start();
    I2C_Write(0xd1);
    dat = I2C_Read(0);
    I2C_Stop();
    dat = (dat & 0x0f) + (dat>>4)*10;
    return (dat);
    }
    void writeds(){
    rtc_write(0x00, second);
    rtc_write(0x01, minute);
    rtc_write(0x02, hour);
    }

    void readds()
    {
    if(kt==0)
    {
    if(a==1)
    {writeds();a=0;
    }
    second = rtc_read(0x00);
    minute = rtc_read(0x01);
    hour = rtc_read(0x02);
    }
    }

    void hienthi(unsigned char x)
    {
    unsigned int i,temp;
    for(i=0;i<8;i++)
    {
    temp=x;
    temp=temp&0x80;
    if(temp==0x80)
    DATA1=1;
    else
    DATA1=0;
    x*=2;
    SCK1=0;
    SCK1=1;
    }
    }
    void quetled()
    {

    if(kt==0)
    {
    for(i=0;i<8;i++){
    P2=cot[i];
    hienthi(magiay[second]);
    hienthi(ma0x[minute%10]);
    hienthi(max0[minute/10]);
    hienthi(ma0x[hour%10]);
    hienthi(max0[hour/10]);
    LACH1=0;
    LACH1=1;
    delay(3);
    }}
    if(kt==1)
    {for(i=0;i<8;i++){
    P2=cot[i];
    hienthi(0xff);
    hienthi(0xff);
    hienthi(0xff);
    hienthi(ma0x[hour%10]);
    hienthi(max0[hour/10]);
    LACH1=0;
    LACH1=1;

    delay(3);
    }
    }
    if(kt==2)
    { for(i=0;i<8;i++){
    P2=cot[i];

    hienthi(0xff);
    hienthi(ma0x[minute%10]);
    hienthi(max0[minute/10]);
    hienthi(0xff);
    hienthi(0xff);
    LACH1=0;
    LACH1=1;

    delay(3);
    }
    }
    if(kt==3)
    {
    for(i=0;i<8;i++){
    P2=cot[i];
    hienthi(magiay[second/1]);
    hienthi(0xff);
    hienthi(0xff);
    hienthi(0xff);
    hienthi(0xff);
    LACH1=0;
    LACH1=1;

    delay(3);
    }
    }
    }
    void ktphim()
    {
    set0=set1;set1=set;
    if((set0==1)&&(set1==0))
    {
    kt++;
    if(kt>3)kt=0;
    }
    switch(kt)
    {
    case 0:break;
    case 1:
    {
    up0=up1;up1=up; a=1;
    if((up0==1)&&(up1==0))
    {
    hour++;
    if(hour>23)hour=0;
    }
    down0=down1;down1=down;
    if((down0==1)&&(down1==0))
    {
    hour--;
    if(hour==-1)
    hour=23;
    }
    break;
    }
    case 2:
    {
    up0=up1;up1=up; a=1;
    if((up0==1)&&(up1==0))
    {
    minute++;
    if(minute>59)
    minute=0;
    }
    down0=down1;down1=down;
    if((down0==1)&&(down1==0))
    {
    minute--;
    if(minute==-1)
    minute=59;
    }
    break;
    }

    case 3:
    {
    up0=up1;up1=up;a=1;
    if((up0==1)&&(up1==0))
    {
    second++;
    if(second>59)
    second++;
    }
    down0=down1;down1=down;
    if((down0==1)&&(down1==0))
    {
    second=0;
    }
    break;
    }
    }
    }

    void main()
    {
    I2C_Start();
    I2C_Write(0xD0);
    I2C_Write(0x07);
    I2C_Write(0x10);
    I2C_Stop();
    while(1)
    {
    ktphim();
    readds();
    quetled();
    }
    }

  • #2
    Bạn phải nói rõ chứ, tự dưng ném lên đây một mớ ai biết gì mà giúp bạn

    Comment

    Về tác giả

    Collapse

    codientu95 Tìm hiểu thêm về codientu95

    Bài viết mới nhất

    Collapse

    Đang tải...
    X