?? 串行e2prom24c01擴展.c
字號:
/* 文件名:(C13.c) */
#pragma code debug small
#include <reg51.h>
#include <intrins.h>
#include <absacc.h>
#include <string.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
sbit SCL=P1^6;
sbit SDA=P1^7;
sbit K0=P1^3;
uchar E24_State;
#define COM XBYTE[0xe100] /*命令口*/
#define OUTBIT XBYTE[0xe101] /* 位控制口 */
#define CLK164 XBYTE[0xe102] /* 段控制口(接164時鐘位) */
#define DAT164 XBYTE[0xe102] /* 段控制口(接164數據位) */
#define IN XBYTE[0xe103] /* 鍵盤讀入口 */
uchar buf[6]; /* 顯示緩沖 */
uchar code tab[17]={ /* 八段管顯示碼 */
0x3f, 0x6, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x7,
0x7f, 0x6f,0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71,0
};
void sdelay() /* 延時子程序 */
{ uint x=200;
while(--x);
}
void time10ms(char num)
{ uint lp;
uchar i;
for(i=0;i<=num;i++)
{ lp=200; while(--lp); }
}
void start()
{ SCL=0;SDA=1; SCL=1; SDA=0; SCL=0; }
void endd()
{ SCL=0; SDA=0; SCL=1; SDA=1;}
char rbyte(char ask_flag)
{ uchar x,lp;
x=0;
for(lp=0;lp<8;lp++)
{ SCL=0;
SDA=1; /* set SDA input */
SCL=1;
x=(x<<1)|SDA;
}
SCL=0;
if(ask_flag) SDA=0; /* master ask */
else SDA=1; /* master no ask */
SCL=1;;
return(x);
}
void wbyte(char p)
{ uchar i;
for(i=0;i<8;i++)
{ SCL=0;
SDA=p&0x80;
p<<=1;
SCL=1;
}
SCL=0; /* extra 9th pulse */
SDA=1; /* set SDA input */
SCL=1;
i=255;
while(SDA && --i) /* wait for slave ack */
if(!i)
E24_State|=0x1;
SCL=0;
}
char read_byte(char adr)
{ uchar x;
start();
wbyte(0xa0);
wbyte(adr);
start();
wbyte(0xa1);
x=rbyte(0);
endd();
return(x);
}
void write_byte(char adr,char num)
{ start();
wbyte(0xa0);
wbyte(adr);
wbyte(num);
endd();
time10ms(0);
}
void disp()
{ static uchar temp;
uchar i,j,shift=0x20,x; /* 從左邊開始顯示 */
for(i=0;i<6;i++) /* 共6個八段管 */
{ OUTBIT=00; /* 關所有八段管 */
temp=0;
x=tab[buf[i]];
for(j=0;j<8;j++) /* 送164 */
{ if( x&0x80) temp|=1;
else temp&=0xfe;
DAT164=temp;
temp|=2; /* CLK='1' */
CLK164=temp;
temp&=0xfd;; /* CLK='0' */
CLK164=temp;
x<<=1;
}
OUTBIT=shift; /* 顯示一位八段管 */
shift>>=1; /* 顯示下一位 */
sdelay();
}
}
main()
{ uchar i;
SP=0x60;
PSW=0;
COM=0x3; /* 定義8155 PA,PB口 輸出,PC口輸入*/
while(1)
{if(K0)
{ write_byte(0x10,0x41); /*向0x10,0x11,0x12單元分別寫入0x41,0x21,0x86*/
write_byte(0x11,0x21);
write_byte(0x12,0x86);
memset(buf,0,6); /*設置顯示"000000" */
}
else
{ i=read_byte(0x10); /*讀出0x10,0x11,0x12,三個單元內容*/
buf[0]=(i&0xf0)>>4;
buf[1]=i&0xf;
i=read_byte(0x11);
buf[2]=(i&0xf0)>>4;
buf[3]=i&0xf;
i=read_byte(0x12);
buf[4]=(i&0xf0)>>4;
buf[5]=i&0xf;
}
for(i=0;i<20;i++)
disp(); /* 顯示 */
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -