經(jīng)典常用的單片機(jī)c程序
void main(void) {
unsigned char disp,flash,temp,cpu;
EA=1;
SCON=0xd8;PCON=0x80;
TMOD=0x21;
TL1=TH1=0xff;TR1=1;
TH0=-100;ET0=TR0=1;
flash=0x80;
while (1) {
temp=command();
switch (temp) {
case 0:
case 1: //89c51
case 2: //89C52
case 3: //80f51
case 4: //80F52
case 5: //87F51
case 6:cpu=temp;SBUF=temp;break;//87f52
case e:SBUF=temp;earsure(cpu);break; //erasure
case p:SBUF=temp;program(cpu);break; //program
case l:lock(cpu);SBUF=temp;break; //lock
default:SBUF=temp;break;
}
b_break=0;
if ((++disp)>flash) {disp=0;led=!led;}
}
}
//HT1380實時時鐘驅(qū)動程序
sbit clock_dat=P0^1;
sbit clock_clk=P0^2;
sbit clock_rst=P0^3;
sbit a0=ACC^0;
sbit a1=ACC^1;
sbit a2=ACC^2;
sbit a3=ACC^3;
sbit a4=ACC^4;
sbit a5=ACC^5;
sbit a6=ACC^6;
sbit a7=ACC^7;
void clock_out(unsigned char dd) {
ACC=dd;
clock_dat=a0;clock_clk=1;clock_clk=0;
clock_dat=a1;clock_clk=1;clock_clk=0;
clock_dat=a2;clock_clk=1;clock_clk=0;
clock_dat=a3;clock_clk=1;clock_clk=0;
clock_dat=a4;clock_clk=1;clock_clk=0;
clock_dat=a5;clock_clk=1;clock_clk=0;
clock_dat=a6;clock_clk=1;clock_clk=0;
clock_dat=a7;clock_clk=1;clock_clk=0;
}
unsigned char clock_in(void) {
clock_dat=1;
a0=clock_dat;
clock_clk=1;clock_clk=0;a1=clock_dat;
clock_clk=1;clock_clk=0;a2=clock_dat;
clock_clk=1;clock_clk=0;a3=clock_dat;
clock_clk=1;clock_clk=0;a4=clock_dat;
clock_clk=1;clock_clk=0;a5=clock_dat;
clock_clk=1;clock_clk=0;a6=clock_dat;
clock_clk=1;clock_clk=0;a7=clock_dat;
return(ACC);
}
unsigned char read_clock(unsigned char ord) {
unsigned char dd=0;
clock_clk=0;
clock_rst=0;
clock_rst=1;
clock_out(ord);
dd=clock_in();
clock_rst=0;
clock_clk=1;
return(dd);
}
void write_clock(unsigned char ord,unsigned char dd) {
clock_clk=0;
clock_rst=0;
clock_rst=1;
clock_out(ord);
clock_out(dd);
clock_rst=0;
clock_clk=1;
}
/*單個漢字庫字摸提取程序,tc2.0編譯*/
#i nclude stdio.h
#i nclude dos.h
#i nclude process.h
#i nclude string.h
void main(void) {
long int num_bytes,qm,wm;
unsigned char d,i,j,k,a[132],b[132];
unsigned char * data;
unsigned char * hz;
static unsigned char dd[103];
FILE *fp;
if ((fp=fopen(hzk16f,rb))==NULL) {
printf(can't open hzk16n);
exit(1);
}
clrscr();
while (1) {
data=(unsigned char *) malloc(33);
printf(please input:n);
scanf(%s,dd); /*輸入一個漢字*/
qm=* dd; /*通過區(qū)位碼計算其在hzk16f文件中的偏移地址*/
qm=qm-161;
if (qm>87) exit(0);
wm=* (dd+1);
wm=wm-161;
if (wm>94) exit(0);
num_bytes=((long)qm*94+wm)*32;
fseek(fp,num_bytes,SEEK_SET);
fgets(data,33,fp);
for (i=0;i32;i++) b[i]=* data++;
for (i=0;i32;i+=2) a[i/2]=b[i];
for (i=0;i32;i+=2) a[i/2+16]=b[i+1];
for (i=8;i16;i++) b[i]=a[i];
for (i=8;i16;i++) a[i]=a[i+8];
for (i=8;i16;i++) a[i+8]=b[i];
/*轉(zhuǎn)換,hzf16f在電腦的儲存格式是以行為字節(jié)計算的,一般的lcd都采用以列為字節(jié)計算*/
for (k=0;k32;k+=8) {
for (j=0;j8;j++) {
d=0;
for (i=0;i8;i++) {
if (a[i+k]>=0x80) {
switch (i) {
case 0:d+=1;break;
case 1:d+=2;break;
case 2:d+=4;break;
case 3:d+=8;break;
case 4:d+=0x10;break;
case 5:d+=0x20;break;
case 6:d+=0x40;break;
case 7:d+=0x80;break;
}
}
}
for (i=0;i8;i++) a[i+k]=1;
b[j+k]=d;
}
}
clrscr();
printf(/*%s:*/n,dd); /*輸出0x00格式的16進(jìn)制數(shù)*/
for (k=0;k32;k+=8) {
for (j=0;j8;j++) printf(0x%x,,b[j+k]);
printf(n);
}
getch();
}
}
//按鍵掃描驅(qū)動程序
unsigned char key,key_h,kpush;
unsigned int key_l;
//按鍵連接到p1.0、p1.1、p1.2
void int_t0(void) interrupt 1 {
unsigned char dd,i;
TL0=TL0+30;TH0=0xfb; //800
/* 按鍵判別 */
if ((P10x7)==0x7) {
if ((key_l>30)(key_l800)(key_h>30)) {
//釋放按鍵,如果之前按鍵時間少于1秒,讀入鍵值
key=kpush;
}
if ((++key_h)>200) key_h=200;
key_l=0;
if (key>=0x80) key=0;
//如果之前的按鍵為長按1秒,清除鍵值
} else {
kpush=P10x7;
key_l++;
if ((key_l>800)(key_h>30)) {
//如果按鍵超過1秒,鍵值加0x80標(biāo)志長按鍵
key=kpush|0x80;
key_h=0;
key_l=0;
}
}
}
評論