单片机c51读写程序.doc
93c46读写程序:/已验证可行/*-HELLO.CCopyright 1995-1999 Keil Software, Inc.-*/#include <REG52.H> /* special function register declarations */ /* for the intended 8051 derivative */#include <stdio.h> /* prototype declarations for I/O functions */#ifdef MONITOR51 /* Debugging with Monitor-51 needs */char code reserve 3 _at_ 0x23; /* space for serial interrupt if */#endif /* Stop Exection with Serial Intr. */ /* is enabled */#define uchar unsigned char#define uint unsigned intunsigned char code num10=0x18,0x7b,0x2c,0x29,0x4b,0x89,0x88,0x3b,0x08,0x09;/ 显示段码值09unsigned char code xuanze4=0x7f,0xbf,0xdf,0xef;sbit dp0= P27;sbit dp1= P26;sbit dp2= P25;sbit dp3= P24;sbit CS=P12;sbit SK=P14;sbit DI=P11;sbit DO=P15;char dpt;unsigned char dpbuf4=0,0,0,0;void delay(void)unsigned int x,y;for (x=0;x<200;x+)for (y=0;y<200;y+);void shift1(uchar num,uchar sdata) unsigned char i;for(i=0;i<num;i+) SK=0;sdata<<=1;DI=CY;SK=1;void shift2(uchar num,uchar sdata) unsigned char i;sdata<<=1;for(i=0;i<num;i+) SK=0;sdata<<=1;DI=CY;SK=1;void write_en(void) unsigned char sb_op=0x80,add=0xc0; CS=0; CS=1; shift1(3,sb_op);shift1(7,add);CS=0;CS=1;void erase_all(void) unsigned char sb_op=0x80,add=0x80; CS=0; CS=1;shift1(3,sb_op);shift1(7,add);CS=0;CS=1;while(DO=0); uchar read(uchar rd_add) unsigned char i,sb_op=0xc0,rd_data=0x00;CS=0; CS=1;shift1(3,sb_op);shift2(7,rd_add); for(i=0;i<8;i+)rd_data<<=1; SK=0;SK=1;CY=DO;if(CY=1)rd_data|=0x01;else rd_data&=0xfe;/SK=0;CS=0;CS=1;return(rd_data);void write(uchar wr_add,uchar wr_data)unsigned char sb_op=0xa0;CS=0; CS=1;shift1(3,sb_op);shift2(7,wr_add);shift1(8,wr_data);CS=0;CS=1;while(DO=0); void erase(uchar er_add) unsigned char sb_op=0xe0;CS=0;CS=1;shift1(3,sb_op);shift2(7,er_add);CS=0;CS=1;while(DO=0);void wr_all(uchar wr_data) unsigned char sb_op=0x80,wr_add=0x40;CS=0;CS=1;shift1(3,sb_op);shift1(7,wr_add);shift1(8,wr_data);CS=0;CS=1;while(DO=0);void ew_disable(void) unsigned char sb_op=0x80,add=0x00;CS=0;CS=1;shift1(3,sb_op);shift1(7,add);CS=0;delay();void main (void) int x,i;unsigned char j;unsigned char k;#ifndef MONITOR51 SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */ TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */ TH1 = 221; /* TH1: reload value for 1200 baud 16MHz */ TR1 = 1; /* TR1: timer 1 run */ TI = 1; /* TI: set TI to send first char of UART */#endif TMOD=0x01;TH0=0xee;TL0=0;TR0=1;ET0=1;EA=1;write_en();for(k=0;k<10;k+) write(k,k+11);j=read(k);dpbuf0=0;dpbuf1=k;dpbuf2=1;dpbuf3=j%10; delay(); while(1); void tmr0(void) interrupt 1 /5msTH0=0xee;TL0=0; if(dpt<3) dpt+;else dpt=0;/动态显示计数器 P0 = numdpbufdpt;if(dpt=0)dp1 = dp2 = dp3 = 1;dp0 = 0;else if(dpt=1)dp0 = dp2 = dp3 = 1;dp1 = 0; else if(dpt=2)dp0 = dp1 = dp3 = 1;dp2 = 0;/if(dpt=3)else dp0 = dp1 = dp2 = 1;dp3 = 0;