久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2140|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

打鈴系統(tǒng)仿真有問題,哪位大神有空來幫幫我啊,跪求

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:256738 發(fā)表于 2018-4-23 11:46 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
100黑幣
每次仿真時日期和星期都不對,有時直接出現(xiàn)亂碼,哪位單片機(jī)大佬可以幫忙改改嗎,不勝感激。
#include <AT89X52.h>
#include <stdio.h>     
bit bclassFlg = 0;
sbit FMQ = P3 ^ 7;

unsigned char stringtemp[10]="";

struct time
{
        unsigned char        second;
        unsigned char        minute;
        unsigned char        hour;
        unsigned char        day;
        unsigned char        weekday;
        unsigned char        month;
        unsigned int        year;
} time;

sbit tim_sda=P2^6;
sbit tim_scl=P2^7;

sbit        lcd16rs        =        P2^0;
sbit        lcd16rw        =        P2^1;
sbit        lcd16en        =        P2^2;

unsigned char        lcd16_1[16];
unsigned char        lcd16_2[16] = {"welcome"};


bit         tim_ack;                // i2c slave ack.
bit         tim_err;
unsigned char tim_rd_buffer[16];
unsigned char tim_wr_buffer[16];

/*****************11us延時函數(shù)*************************/
void delay11us(unsigned char t)
{
  for (;t>0;t--);
}



void delay(unsigned int x)
{
        unsigned int a,b;
        for(a=x;a>5;a--);
                for(b=10;b>0;b--);
}



//寫指令
void write_com(unsigned char com)
{
        P1=com;// 將指令值寫到數(shù)據(jù)線上
        lcd16rs=0;//寫指令
        lcd16en=0;//給一個LCD 使能脈沖
        delay(10);
        lcd16en=1;
        delay(10);
        lcd16en=0;
       
}

//寫數(shù)據(jù)
void write_date(unsigned char date)
{
        P1=date;    //將數(shù)據(jù)寫到數(shù)據(jù)線上
        lcd16rs=1;    //寫數(shù)據(jù)
        lcd16en=0;    //給一個LCD 使能脈沖
        delay(10);
        lcd16en=1;
        delay(10);
        lcd16en=0;
       
}


//初始化LCD
void lcd_init()
{
        lcd16rw = 0;
        write_com(0x38);//顯示模式設(shè)置
        delay(20);
        write_com(0x0f);//開顯示,顯示光標(biāo)、光標(biāo)閃爍
        delay(20);
        write_com(0x06);//光標(biāo)和顯示指針加一,屏幕不移動
        delay(20);
        write_com(0x01);//清顯示屏
        delay(20);       
}


//在第N行顯示第M個字
void lcd16_n_line_n_byte(unsigned char n_line,unsigned char n_byte)
{
        unsigned char j;
        unsigned char com;
        unsigned char *dat1;
        unsigned char *dat2;

        dat1 = lcd16_1;
        dat2 = lcd16_2;

        if(n_line==0)                                   //顯示第一行。
        {
                for(j=0;j<n_byte;j++)
                {
                        com = 0x80+j;
                        write_com(com);
                        write_date(*dat1);
                        dat1++;
                }       
        }
        if(n_line==1)                           //顯示第二行。
        {
                for(j=0;j<n_byte;j++)
                {
                        com = 0xc0+j;
                        write_com(com);
                        write_date(*dat2);
                        dat2++;
                }       
        }
}

// i2c start.
void PCF8563_start()        //start flag is when tim_scl 1, tim_sdafrom 1 to 0.
{
        tim_sda = 1;
        delay11us(1);
        tim_scl = 1;
        delay11us(1);
        tim_sda = 0;
        delay11us(1);
}

// i2c stop.
void PCF8563_stop()                         //start flag is when tim_scl 1, tim_sdafrom 0 to 1.
{
        tim_sda = 0;
        delay11us(1);
        tim_scl = 1;
        delay11us(1);
        tim_sda = 0;
}

//i2c slave respons.
void PCF8563_respons()
{
        unsigned char i;
        tim_scl = 1;
        delay11us(1);
        while((tim_sda)&&(i<250))  i++;
        if(!tim_sda)
                tim_ack = 1;
        else
                tim_ack = 0;
        tim_scl = 0;
        delay11us(1);
}

// i2c master respons.
void PCF8563_master_respons(bit master_ack)
{
        tim_scl = 0;
        delay11us(1);
        tim_sda = ~master_ack;
        delay11us(1);
        tim_scl = 1;
        delay11us(1);
        tim_scl = 0;
        delay11us(1);
}

//i2c init.
void PCF8563_init()
{
        tim_sda = 1;
        delay11us(1);
        tim_scl = 1;
        delay11us(1);
}

// i2c write n byte.
void PCF8563_wr_byte(unsigned char dat)
{
        unsigned char i , temp;
        temp = dat;
        for(i=0;i<8;i++)
        {
                tim_scl = 0;
                delay11us(1);

               
                if(temp&0x80)
                        tim_sda = 1;
                else
                        tim_sda = 0;
                temp = temp<<1;
               
                delay11us(1);
                tim_scl = 1;
                delay11us(1);

        }
        tim_scl = 0;
        delay11us(1);
        tim_sda = 1;                       
        delay11us(1);
}

unsigned char PCF8563_rd_byte()
{
        unsigned char i,k;
        tim_scl = 0;
        delay11us(1);
        tim_sda = 1;
        delay11us(1);
        for(i=0;i<8;i++)
        {
                tim_scl = 1;
                delay11us(1);
                k = (k<<1)|tim_sda;
                tim_scl = 0;
                delay11us(1);
        }
        return k;
}

void PCF8563_write_n_byte(unsigned char add,unsigned char com,unsigned char *dat,unsigned char n_byte)   //
{
        unsigned char i = 0;
        unsigned char temp;

        temp = *dat;                                  // send to begin address to 'temp'.

        add = add<<1;
        add = 0xa0+add+0x00;                       

        PCF8563_start();
        PCF8563_wr_byte(add);                 //send EEPROM address, bit 0 = 0 is write.
        PCF8563_respons();
        if(!tim_ack)                                //if i2c respons.
        {
                tim_err = 1;
                return;
        }
       
        PCF8563_wr_byte(com);                        //send EEPROM beginning address.
        PCF8563_respons();
        if(!tim_ack)                                //if i2c respons.
        {
                tim_err = 1;
                return;
        }
       
        while(n_byte)                           //write n byte.
        {
                PCF8563_wr_byte(temp);
                PCF8563_respons();
                if(!tim_ack)                               
                {
                        tim_err = 1;
                        PCF8563_stop();       
                        return;
                }
                temp++;
                n_byte--;
        }
       
        PCF8563_stop();       
}

void PCF8563_read_n_byte(unsigned char add,unsigned char com,unsigned char n_byte)
{
        unsigned char *temp;

        temp = tim_rd_buffer;

        add = add<<1;
        add = 0xa0+add+0x00;                       

        PCF8563_start();

        PCF8563_wr_byte(add);                                 //send address + wr.
        PCF8563_respons();

        PCF8563_wr_byte(com);                                // first byte address.
        PCF8563_respons();

        add = add+0x01;                       

        PCF8563_start();
        PCF8563_wr_byte(add);                           //send address + rd.
        PCF8563_respons();

        while(n_byte)                                   // rd n byte .
        {
                *temp = PCF8563_rd_byte();
                if(n_byte != 1)                                //if i2c respons.
                {
                        PCF8563_master_respons(1);        //if the number is not the last one, master send a ack bit.
                }
                temp++;
                n_byte--;
        }
        PCF8563_stop();
}

void PCF8563_wr_time()
{
        tim_wr_buffer[0] = 0x00;        //second
        tim_wr_buffer[1] = 0x11;        //minute
        tim_wr_buffer[2] = 0x22;        //hour

        tim_wr_buffer[3] = 0x10;        //day
        tim_wr_buffer[4] = 0x03;        //weekday
        tim_wr_buffer[5] = 0x06;        //month
        tim_wr_buffer[6] = 0x15;        //year

       
        PCF8563_write_n_byte(1,2,tim_wr_buffer,8);
}

//延時眼熟
void DelayFM(unsigned int x)  
{
        unsigned char t;
        while(x--)
        {
                 for(t=0;t<120;t++);
        }
}
//蜂鳴器驅(qū)動函數(shù),參數(shù)為發(fā)聲聲調(diào)
void FM(unsigned char x)
{
        unsigned char i;
        for(i=0;i<100;i++)
        {
                 FMQ = ~FMQ;
                DelayFM(x);
        }
        FMQ = 0;
}

void PCF8563_rd_time()
{
        unsigned char        temp;


        PCF8563_read_n_byte(1,2,9);

        time.second = ((tim_rd_buffer[1]&0x70)>>4)*10 + (tim_rd_buffer[1]&0x0f);        //second                        

        time.minute = ((tim_rd_buffer[2]&0x70)>>4)*10 + (tim_rd_buffer[2]&0x0f);        //minute

        time.hour = ((tim_rd_buffer[3]&0x30)>>4)*10 + (tim_rd_buffer[3]&0x0f);                //hour
        time.day = ((tim_rd_buffer[4]&0x30)>>4)*10 + (tim_rd_buffer[4]&0x0f);                //day
        time.weekday = tim_rd_buffer[5]&0x07;                                                                                //weekday
        time.month = ((tim_rd_buffer[6]&0x10)>>4)*10 + (tim_rd_buffer[6]&0x0f);                //month

  if(time.second == 0)
  {
    if(bclassFlg == 0 )
    {
      sprintf(stringtemp,"%x%x",time.hour,time.minute);
//      printf("%s\n",stringtemp);
      printf("it's time to have class\n"); //上課
      bclassFlg = 1;
      FM(3);
    }
  }
  else if(time.second == 45) //如果到了45分鐘,開始下課鈴
  {
    if(bclassFlg == 1)
    {
      sprintf(stringtemp,"%x%x",time.hour,time.minute);
//      printf("%s\n",stringtemp);
      printf("it's time to finish class\n");
      bclassFlg = 0;
      FM(7);
    }
  }
  else
  {
  }

        temp = (tim_rd_buffer[4]&0x80)>>7;
        time.year = (temp+20)*100+((tim_rd_buffer[7]&0x70)>>4)*10 + (tim_rd_buffer[7]&0x0f);           //year
                lcd16_1[0] = time.hour/10+0x30;          //hour
                lcd16_1[1] = time.hour%10+0x30;
                lcd16_1[2] = '-';
                lcd16_1[3] = time.minute/10+0x30;          //minute
                lcd16_1[4] = time.minute%10+0x30;
                lcd16_1[5] = '-';
                lcd16_1[6] = time.second/10+0x30;          //second
                lcd16_1[7] = time.second%10+0x30;

                lcd16_2[0] = time.year/1000+0x30;          //year
                lcd16_2[1] = (time.year%1000)/100+0x30;
                lcd16_2[2] = (time.year%100)/10+0x30;
                lcd16_2[3] = time.year%10+0x30;
                lcd16_2[4] = '-';
                lcd16_2[5] = time.month/10+0x30;  //mon
                lcd16_2[6] = time.month%10+0x30;
                lcd16_2[7] = '-';
                lcd16_2[8] = time.day/10+0x30;          //day
                lcd16_2[9] = time.day%10+0x30;

                lcd16_2[10] = 'w';
                lcd16_2[11] = 'e';
                lcd16_2[12] = 'e';
                lcd16_2[13] = 'k';
                lcd16_2[14] = ':';
                lcd16_2[15] = time.weekday+0x30;

                lcd16_n_line_n_byte(0,16);
                lcd16_n_line_n_byte(1,16);
}




void DelayMS(unsigned int ms)
{
        unsigned char i;
        while(ms--)
        {
                 for(i=0;i<120;i++);
        }
}

void InitUart(void)
{
        SCON = 0x50;                                  //工作方式1
        TMOD = 0x21;
        PCON = 0x00;
        TH1 = 0xfd;                                   //使用T1作為波特率發(fā)生器
        TL1 = 0xfd;
        TI = 1;
        TR1 = 1;                                      //啟動T1                                                                  //啟動T1  
}

void main()
{
  PCF8563_init();
//        PCF8563_wr_time();
        lcd_init();
  InitUart();
        while(1)
        {
                PCF8563_rd_time();
    DelayMS(250);
        }
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 国产一区二区三区四区五区加勒比 | 国产高清在线观看 | 亚洲经典一区 | 一区二区成人 | www.一区二区三区 | 欧美极品少妇xxxxⅹ免费视频 | 人妖一区 | 亚洲高清在线免费观看 | 九九热最新视频 | av毛片免费| a级黄色片视频 | av成年人网站| 亚洲精品68久久久一区 | 亚洲视频一区在线观看 | 91精品国产高清一区二区三区 | 国产福利在线视频 | 在线观看视频亚洲 | 欧美在线观看一区 | 国产成人综合在线 | 在线观看国产 | 久久精品无码一区二区三区 | 特一级毛片 | 国产成人精品一区二区三区网站观看 | 久久视频精品 | 国产精品一区二区三区四区 | 日韩精品视频一区二区三区 | 九九九久久国产免费 | 99视频免费看 | 欧美九九| 韩日精品一区 | 欧美成人一级视频 | 狠狠色综合欧美激情 | hitomi一区二区三区精品 | 国产美女视频黄a视频免费 国产精品福利视频 | 国产精品美女久久久久久久久久久 | 91精品国产综合久久香蕉922 | 国产人成在线观看 | 成人在线视频网址 | 成人午夜精品一区二区三区 | 99精品欧美一区二区三区综合在线 | 美女毛片免费看 |