|
該程序主要運(yùn)用了鍵盤掃描技術(shù)和EEROM存儲器編程,做到把鍵盤值輸入到存儲器中,不怕掉電。基于WICN單片機(jī)開發(fā)板設(shè)計(jì)。
主程序
- //--------------------- 半自動繞線機(jī)程序 ----------------------
- //-----------------芯片STC89C52RC 晶振11.0592M ----------------
- //---------------------計(jì)數(shù)值范圍1~22圈-----------------------
- #include<reg52.h>
- #include"24c01.c"
- #include"stopgo.c"
- #include"jishuan.c"
- #include"lcd1602.c"
- #include"startgo.c"
- //-------------------------------------------------------------
- #define uchar unsigned char
- #define uint unsigned int
- //-------------------------------------------------------------
-
- sbit K1=P0^0; //數(shù)字輸入鍵個位數(shù)
- sbit K2=P0^1; //數(shù)字輸入鍵十位數(shù)
- sbit K3=P0^2; //輸入功能并以IIC存入AT24C01中保存
- sbit K4=P0^3; //確認(rèn)存入數(shù)據(jù)鍵
- sbit K5=P0^4; //停止鍵
- sbit beep=P3^3;
- sbit CLK=P1^2; //光電感應(yīng)脈沖(用該開關(guān)模仿)
- sbit LE_LED=P3^4;
- sbit LED=P1^5;
- //-------------------------------------------------------------
- //-------------------------------------------------------------
- uchar tablee[2];
-
- //------按鍵延時-----------------------------------------------
- void delayx(uint i)
- {
- uint w;
- for(w=0;w<i;w++);
- }
- //-------------------------------------------------------------
- void start_ggo(uchar b) //打線執(zhí)行程序
- {
- uchar d=0;
- uchar n=0;
- while(n<=b)
- {
- LCD1602(n); //顯示
- if(!CLK)
- {
- delayx(500);
- while(CLK==0);
- n++;
- LED=0;
- beep=0;
- delayx(10000);
- LED=1;
- beep=1;
- }
- }
- }
- //------------------------------------------------------------
- //-------------------------------------------------------------
- //------------------數(shù)據(jù)鍵盤按鍵掃描---------------------------
- void main()
- {
- uchar h,f;
- uchar e=30;
- uchar id0=0;
- uchar id1=0;
- LE_LED=0;
- while(1)
- {
- if(!K1) //P0.0 K1=3
- {
- delayx(500);
- while(K1==0);
- beep=0;
- delayx(5000);
- beep=1;
- tablee[0]=3;
- if(id0==1)
- {
- tablee[1]=3;
- id0=0;
- }
- id0++;
- if(id0==2)id0=0;
- }
- //--------------------
- if(!K2) //P0.1 K2=5
- {
- delayx(500);
- while(K1==0);
- beep=0;
- delayx(5000);
- beep=1;
- tablee[1]=5;
- if(id1==1)
- {
- tablee[0]=5;
- id0=0;
- }
- id1++;
- if(id1==2)id1=0;
- }
- //===========================================================
- if(!K3)//確認(rèn)輸入到AT24C01中
- {
- delayx(500);
- while(K3==0); //P0.2
- beep=0;
- delayx(5000);
- beep=1;
- jishuan(tablee[0],tablee[1]);
- f=jishuan_ok();
- init(); //24c01初始化
- W_AT24C01(e,f);//寫數(shù)據(jù)到AT24C01中,0x30是器件地址內(nèi)存單元
- }
- if(!K4)//啟動機(jī)器打線 P0.3
- {
- delayx(500);
- while(K4==0);
- beep=0;
- delayx(5000);
- beep=1;
- init(); //24c01初始化
- h=R_AT24C01(e); //讀AT24C01中0x30的數(shù)據(jù)
- LCD1602(h); //顯示
- //start_go(h); //啟動執(zhí)行打線功能并計(jì)數(shù)
- start_ggo(h);
-
- if(!K5) //如果停機(jī)鍵按下則執(zhí)行停機(jī)功能
- stop_go();
- }
- }
- }
-
-
- //---IIC AT24C01存儲器讀寫操作程序-----------
- #include <reg52.h>
- #define uchar unsigned char
- sbit SDA=P3^6;
- sbit SCL=P3^7;
- ///------------------------------------------
- void delay()
- { ; ; }
- ///////////////////////
- void start()//起始信號
- {
- SDA=1;
- delay();
- SCL=1;
- delay();
- SDA=0;
- delay();
- }
- ///////////////////////
- void stop()//終止信號
- {
- SDA=0;
- delay();
- SCL=1;
- delay();
- SDA=1;
- delay();
- }
- //////////////////////////////////////////////////
- //////////////////////////////////////////////////
- void respons()//應(yīng)答信號,等待或者發(fā)送一個應(yīng)答信號
- {
- uchar i;
- SCL=1;
- delay();
- while((SDA==1)&&(i<250))i++;
- SCL=0;
- delay();
- }
- /*************************************************/
- void init()//初始化
- {
- SDA=1;
- SCL=1;
- }
- //////////////////////////////////////////////////
- void write_byte(uchar date)//寫一字節(jié)
- {
- uchar i;
- SCL=0;
- for(i=0;i<8;i++)
- {
- date=date<<1;
- SDA=CY;
- SCL=1;
- delay();
- SCL=0;
- delay();
- }
- }
- //------------------------------------------------
- uchar read_byte()//讀數(shù)據(jù)(也就是接收從器件傳來的數(shù)據(jù))
- {
- uchar i,k;
- for(i=0;i<8;i++)
- {
- SCL=1;
- delay();
- k=(k<<1)|SDA;
- SCL=0;
- delay();
- }
- return k;
- }
- //////////////////////////////////////////////////
- //=============AT24C01寫操作=====================//
- void W_AT24C01(uchar address,uchar date)//寫數(shù)據(jù)
- {
- start();
- write_byte(0xa0); //器件地址
- respons();
- write_byte(address);//器件內(nèi)存地址 address
- respons();
- write_byte(date);//數(shù)據(jù) date
- respons();
- stop();
- }
- //////////////////////////////////////////////////
- //==============AT24C01讀操作====================//
- uchar R_AT24C01(uchar address)
- {
- uchar date;
- start();
- write_byte(0xa0);//寫器件地址和寫指令0XA0=10100000B最后一個零表示寫,低電平有效
- respons();
- write_byte(address);////器件內(nèi)存地址address
- respons();
- start();
- write_byte(0xa1);//寫器件地址和讀指令0XA1=10100001B,最后一個1表示讀,高電平有效
- respons();
- date=read_byte();
- stop();
- return date;
- }
-
-
-
- //------------------LCD1602顯示程序(模塊)--------------------
- #include <reg52.h>
- #define uchar unsigned char
- ///////////////LCD1602/////////////
- sbit RS=P0^5;
- sbit RW=P0^6;
- sbit YE=P0^7;
- /////////////////////////////////////////////////////////////
- uchar code table[]={0xc0,0xf9,0xa4,0xb0,//0,1,2,3
- 0x99,0x92,0x82,0xf8,//4,5,6,7
- 0x80,0x90,0xfe};//8,9,.,段碼表
- /////////////////////////////////////////////////////////////
- uchar code table1[] ={"0123456789. -"};
- uchar code table2[] ={"Temperature"};
- uchar code table3[] ={" 123333 "};
- /////////////////////////////////////////////////////////////
- void delayy(unsigned int us)
- {
- while(us--);
- }
- ////////////////////////////////////////////
- void delay2()
- {
- RS=0;
- RW=0;
- YE=0;
- delayy(5);
- YE=1;
- }
- /******************************LCD1602液晶顯示****************************/
- void LCD1602(uchar dd)//dd是整數(shù)
- {
- uchar e,f,i,nem;
- e=dd/10;
- f=dd%10;
- //////////////////////////////////////LCD屏初始化
- P2=0x01; //清屏
- delay2();
- P2=0x38; //顯示設(shè)定兩行
- delay2();
- P2=0x0f; //打開顯示和光標(biāo)
- delay2();
- P2=0x06; //光標(biāo)右移
- delay2();
- P2=0x80; //光標(biāo)回到第一行的開頭
- delay2();
- //////////////////////////////////////////
- for(nem=0;nem<11;nem++)////////////溫度英文字顯示
- {
- P2=table2[nem];
- RS=1;
- RW=0;
- YE=0;
- delay2();
- }
- ////////////////////////
- P2=table1[e];
- RS=1;
- RW=0;
- YE=0;
- P2=0x06; //光標(biāo)右移顯示整數(shù)十位
- delay2();
- ////////////////////////
- P2=table1[f];
- RS=1;
- RW=0;
- YE=0;
- P2=0x06; //光標(biāo)右移顯示整數(shù)個位
- delay2();
- /////////////////////////////////////////////////
- P2=0xc0;//換行顯示
- delay2();
- for(i=0;i<16;i++)
- {
- P2=0xc0;
- delay2();
- P2=table3[i];
- RS=1;
- RW=0;
- YE=0;
- }
- }
-
-
-
- //-----------數(shù)據(jù)計(jì)算程序--------------
- #define uchar unsigned char
- static uchar j=0;
- ///----------------------------------------------------
- void jishuan(uchar i,uchar e) //i為十位,e為個位
- {
- j=((i*10)+e);
- }
- ///-----------------------------------------------------
- uchar jishuan_ok()
- {
- return (j);
- }
復(fù)制代碼
|
|