Classic and commonly used single chip microcomputer C program

Publisher:andyliow1980Latest update time:2018-03-12 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

//Hexadecimal<->decimal conversion program


 unsigned char d[10]; //10-bit display buffer for display

 //==========================================================
    //Hexadecimal to decimal output subroutine: display data, start bit, end bit, whether there is a decimal point
 //==============================================================
 void output(unsigned long dd,unsigned char s,unsigned char 
 e,unsigned char  DIP ) {
    unsigned long div;
    unsigned char tm[8],i,j;
    div=10000000;
    for (i=0;i<8;i++) {
        tm[i]=dd/div;
        dd%=div;
        div/=10;
    }
    for (i=0;i<6;i++) {
        if (tm[i]!=0) break;
        tm[i]=nul;
    }
    tm[5]|=dip; //Decimal point control, please refer to "Serial LED Digital Tube Display Driver"
    j=7;
    for (i=s;i        d[i]=tm[j];
        j--;
    }
 }

 //Convert the decimal number of display digits 5-9 into hexadecimal number
 unsigned int input(void) {
    unsigned int dd,dat;
    dd=10000;dat=0;
    for (i=5;i<10;i++) {
        dat+=dd*temp;
        dd/=10;
    }
    return(dat);
 }
 /* 89C51 series CPU programmer receives CPU program*/

 #i nclude
 #i nclude
 #i nclude

 #define e 8
 #define p 9
 #define l 10

 sbit led=P3^2;
 sbit p27=P2^7;
 sbit p26=P2^6;
 sbit p36=P3^6;
 sbit p37=P3^7;
 sbit rst=P3^3;
 sbit ale=P3^5;
 sbit vpp=P3^4;

 bit b_break;
 unsigned int adds;


 //    13.8mS
 void int_t0(void) interrupt 1 {
    TH0=-100;
    b_break=1;
 }

 void wait(unsigned char w) {
    unsigned int t=w*184;
    b_break=0;
    TL0=-t%256-1;TH0=-t/256;
    while (b_break==0) {}
 }

 void nop(void) {
    _nop_();_nop_();_nop_();_nop_();
    _nop_();_nop_();_nop_();_nop_();
    _nop_();_nop_();_nop_();_nop_();
    _nop_();_nop_();_nop_();_nop_();
    _nop_();_nop_();_nop_();_nop_();
 }

 unsigned char command(void) {
    TH0=-100;b_break=0;
    while (RI==0) {if (b_break==1) return(0);}
    RI=0;
    return(SBUF);
 }

 void earsure(unsigned char cpu) {
    switch (cpu) {
    case 1:        //89C51
    case 2:rst=p26=1;p27=p36=p37=0;nop();vpp=1;nop();
        ale=0;wait(110);ale=1;nop();
        break;
    case 3:
    case 4:break;
    case 5:
    case 6:break;
    }
 }

 void program(unsigned char cpu) {
    unsigned int bdata adds=0;
    unsigned char d;
    switch (cpu) {
    case 1:    //89C51
    case 2:
        p36=p37=1;rst=1;
        while (1) {
            TH0=-100;b_break=0;
            while (RI==0) {if (b_break==1) return;}
            RI=0;
            d=SBUF;
            //address
            P0=adds%256;
            P2=adds/256;
            p27=1;
            //data
            P1=d;
            nop();    //48clcl
            //vpp
            vpp=1;
            nop();    //48clcl
            //ale
            ale=0;
            wait(1);//100uS
            ale=1;
            nop();    //10uS
            vpp=0;
            nop();    //48clcl
            p27=0;
            nop();    //48clcl
            P1=0xff;
            TH0=-100;b_break=0;
            while (d!=P1) {if (b_break==1) return;}    //data 
polling
            SBUF=d;
            adds++;
        }
        break;
    case 3:
    case 4:
    case 5:
    case 6:break;
    }
 }

 void LOCk(unsigned char cpu) {
    unsigned char i;
    switch (cpu) {
    case 1:    //89c51
    case 2:
        //lock 1
        rst=p26=p36=p27=p37=1;nop();
        vpp=1;
        nop();
        ale=0;
    //    for (i=0;i<6;i++) wait(100);
        wait(1);
        ale=1;
        nop();
        vpp=0;
        nop();
    
        //lock 2
        rst=p26=p27=1;p36=p37=0;nop();
        vpp=1;
        nop();
        ale=0;
    //    for (i=0;i<6;i++) wait(100);
        wait(1);
        ale=1;
        nop();
        vpp=0;
        nop();
        
        //lock 3
        rst=p26=p36=1;p27=p37=0;nop();
        vpp=1;
        nop();
        ale=0;
    //    for (i=0;i<6;i++) wait(100);
        wait(1);
        ale=1;
        nop();
        vpp=0;
        nop();
        break;
    case 3:
    case 4:
    case 5:
    case 6:break;
    }
 }

 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实时时钟驱动程序

 sbit clock_dat=P0^1;
 sbit clock_clk=P0^2;
 sbit clock_rst=P0^3;

 sbit a0=A CC ^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); /*Input a Chinese character*/

         qm=* dd;        /*通过区位码计算其在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;i<32;i++) b[i]=* data++;
         for (i=0;i<32;i+=2) a[i/2]=b[i];
         for (i=0;i<32;i+=2) a[i/2+16]=b[i+1];
         for (i=8;i<16;i++) b[i]=a[i];
         for (i=8;i<16;i++) a[i]=a[i+8];
         for (i=8;i<16;i++) a[i+8]=b[i];

         /*Conversion, the storage format of hzf16f in the computer is calculated by row bytes, and general LCDs use column bytes*/
         for (k=0;k<32;k+=8) {
             for (j=0;j<8;j++) {
                 d=0;
                 for (i=0;i<8;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;i<8;i++) a[i+k]<<=1;
                 b[j+k]=d;
             }
         }
         clrscr();
         printf("/*%s:*/n",dd); /*Output hexadecimal number in 0x00 format*/
         for (k=0;k<32;k+=8) {
             for (j=0;j<8;j++) printf("0x%x,",b[j+k]);
             printf("n");
         }
         getch();
     }
 }
 //Key scanning driver

 unsigned char key,key_h,kpush;
 unsigned int key_l;

 //Buttons connected to p1.0, p1.1, p1.2

 void int_t0(void) interrupt 1 {
     unsigned char dd,i;
     TL0=TL0+30;TH0=0xfb; //800
     /* key identification */
     if ((P1&0x7)==0x7) {
         if ((key_l>30)&&(key_l<800)&&(key_h>30)) {        
 //Release the key. If the previous key press time is less than 1 second, read the key value
             key=kpush;
         }
         if ((++key_h)>200) key_h=200;
         key_l=0;
         if (key>=0x80) key=0;                            
 //If the previous key was pressed for 1 second, clear the key value
     } else {
         kpush=P1&0x7;
         key_l++;
         if ((key_l>800)&&(key_h>30)) {                    
 //If the key is pressed for more than 1 second, add 0x80 to the key value to indicate a long press
             key=kpush|0x80;
             key_h=0;
             key_l=0;
         }
     }
 }

 void main(void) {
     TMOD=0x1;TR0=1;ET0=1;EA=1;
     while (1) {
         while (!key) {}
         switch (key) {
         case 1:break;
         case 2:break;
         }
     }
 //Serial drive LED display,
 //One 74hc595 shift register drives the transistor to drive the LED bit,
 //Two 74hc595 drive the LED segment, mode bit 5 bit x 8 segment x 2 = 10 digital tubes
 //5 frequency division , each scan time bit 1.25ms



 //Define special symbols

 #define nul 0xf
 #define qc 0xc
 #define qb 0xb
 #define q_ 0xa
 #define q__ 0xd
 #define q___ 0xe
 #define qp 0x10
 #define qe 0x11
 #define qj 0x12
 #define qn 0x13
 #define qf 0x14
 #define qa 0x15
 #define qr 0x16
 #define qd 0x17
 #define qu 0x18
 #define ql 0x19
 #define qh 0x1a
 #define qwen 0x1b
 #define qt 0x1c
 #define qla 0x1d
 #define qlb 0x1e
 #define qlc 0x1f
 #define qld 0x20
 #define qle 0x21
 #define qlf 0x22
 #define qlg 0x23
 #define qldp 0x24


 //Display segment information. The segment information of different LED combinations only needs to change 8 values.
 //Therefore, this definition is universal.

 //    显示    
 //        -d 20
 //    |c 40    |e 10
 //        - g        80        
 //    |b 2    |f 4
 //        _a1    .dp 8
 #define pa 1
 #define pb 2
 #define pc 0x40
 #define pd 0x20
 #define pe 0x10
 #define pf 4 
 #define pg 0x80
 #define PDP 8

 //--------------
 #define l0 PDP+pg
 #define l1 255-pf-pe
 #define l2 PDP+pc+pf
 #define l3 pdp+pc+pb
 #define l4 pdp+pa+pb+pd
 #define l5 pdp+pb+pe
 #define l6 pdp+pe
 #define l7 pdp+pc+pg+pb+pa
 #define l8 pdp
 #define l9 pdp+pb
 #define la pdp+pa
 #define lb pdp+pd+pe
 #define lc pdp+pg+pe+pf
 #define ld pdp+pc+pd
 #define le pdp+pe+pf
 #define lf pdp+pe+pf+pa
 #define l_ 255-pg
 #define lnul 255
 #define ll pdp+pg+pd+pf+pe
 #define lp pdp+pa+pf
 #define lt pdp+pd+pe+pf
 #define lr pdp+pe+pf+pg+pa
 #define ln pdp+pg+pa
 #define lh pdp+pd+pe+pa
 #define ly pdp+pb+pd
 #define lu pdp+pg+pd
 #define l__ pdp+pg+pb+pc+pe+pf
 #define l___ l__-pg
 #define l_1 255-pa
 #define l_2 255-pa-pg
 #define lj 255-(pe+pf+pa)
 #define lwen 255-(pd+pe+pg+pb)
 #define lall 0


 #define lla 255-pa
 #define llb 255-pb
 #define llc 255-pc
 #define lld 255-pd
 #define lle 255-pe
 #define llf 255-pf
 #define llg 255-pg
 #define lldp 255-pdp


 //The bit information sent serially is currently a 10-bit LED display.
 unsigned char code un_dig[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb};
 //The short message sent serially.
 unsigned char code 
 un_disp[]={l0,l1,l2,l3,l4,l5,l6,l7,l8,l9,l_,lb,lc,l__,l___,lnul,lp,le,lj,ln,lf,la,lr,ld,lu, ll,
 lh,lwen,lt,lla,llb,llc,lld,lle,llf,llg,lldp,lnul};


 sbit d_clk=P0^0; //Shift clock
 sbit d_dat=P0^1; //Shift data
 sbit d_st=P0^2; //Shift lock


 unsigned char dig; //bit scan counter
 unsigned char d[10]; //display buffer

 //Send 8-bit serial data
 void out_disp(unsigned char dd) {
     unsigned char i;
     for (i=0;i<8;i++) {
         if (dd&1) d_dat=1; else d_dat=0;
         d_clk=0;
         dd>>=1;
         d_clk=1;
     }
 }
 //Control the decimal point and flashing. Display data|0x040 means there is a decimal point; display data|0x80 means flashing.
 void out_displ(unsigned char dd) {
     if (dd>=0x80) {
         if (s001>flash_time) {out_disp(0xff);return;}
     }
     dd&=0x7f;
     if (dd>=0x40) {
         dd=un_disp[dd&0x3f]^pdp;
     } else dd=un_disp[dd];
     out_disp(dd);
 }

 unsigned int s001;    //闪烁时间参考
 void int_t0(void) interrupt 1 {
     unsigned char dd;
     TL0=TL0+30;TH0=0xfb;    //800
     time++;
     if ((++s001)>=800) s001=0;
     //    显示    
     if ((++dig)>4) dig=0;
     d_st=0;
     dd=d[dig+5];
     out_displ(dd);
     dd=d[dig];
     out_displ(dd);
     out_disp(un_dig[dig]);
     d_st=1;
 }
 void main(void) {
     unsigned char i;
     TMOD=0x1;
     TR0=ET0=1;
     EA=1;
     for (i=0;i<10;i++) d[i]=i;    //display test
     while (1) {}
 }


Keywords:MCU Reference address:Classic and commonly used single chip microcomputer C program

Previous article:Program for displaying matrix keyboard scan values ​​using digital tubes
Next article:Single chip computer dynamic scanning interface and program design

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号