这是关于蓝桥杯备赛路上练习的小程序,蓝桥杯的竞赛路上希望大家互相勉励。 祝大家有个好成绩。 以下代码是我利用手头上小红板(JW-51)训练的小程序。 希望大家一同进步。 #include<reg52.h> //只在第一个数码管显示0 typedef unsigned int u16; typedef unsigned char u8; unsigned char code smgduan[]={0x3f, 0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; unsigned char code smgwei[]={0xfe, 0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; sbit rclk=P2^5; sbit srclk=P2^6; sbit ser=P2^7; void smg_dis(); voidsend_595(u8 n); void delay(u16 n) { u8 i ; while (n--) for(i=0;i<125;i++); } void main() { while(1) { smg_dis(); } } void smg_dis() { send_595(smgwei[0]); send_595(smgduan[0]); rclk=0; rclk=1; delay(10); } voidsend_595(u8 n) { u8 i; for(i=0;i<8;i++) { if(n&0x80 ) ser=1; else ser=0; srclk=0; srclk=1; n=n<<1; } } #include<reg52.h> //八个数码管一起间隔0.5s循环显示0-9 typedef unsigned int u16; typedef unsigned char u8; unsigned char code smgduan[]={0x3f, 0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; unsigned char code smgwei[]={0xfe, 0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; sbit rclk=P2^5; sbit srclk=P2^6; sbit ser=P2^7; void smg_dis(); voidsend_595(u8 n); void delay(u16 n) { u8 i ; while (n--) for(i=0;i<125;i++); } void main() { while(1) { smg_dis(); } } void smg_dis() { u8 i; for(i=0;i<10;i++) { send_595(0x00); send_595(smgduan[i]); rclk=0; rclk=1; delay(500); }} voidsend_595(u8 n) { u8 i; for(i=0;i<8;i++) { if(n&0x80 ) ser=1; else ser=0; srclk=0; srclk=1; n=n<<1; } } #include<reg52.h> //八个数码管显示1-8 typedef unsigned int u16; typedef unsigned char u8; unsigned char code smgduan[]={0x3f, 0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; unsigned char code smgwei[]={0xfe, 0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; sbit rclk=P2^5; sbit srclk=P2^6; sbit ser=P2^7; void smg_dis(); voidsend_595(u8 n); void delay(u16 n) { u8 i ; while (n--) for(i=0;i<125;i++); } void main() { while(1) { smg_dis(); } } void smg_dis() { u8 i,n=500; while(n--){ for(i=1;i<9;i++) { send_595(smgwei[i-1]); send_595(smgduan[i]); rclk=0; rclk=1; delay(1); }}} voidsend_595(u8 n) { u8 i; for(i=0;i<8;i++) { if(n&0x80 ) ser=1; else ser=0; srclk=0; srclk=1; n=n<<1; } } #include<reg52.h> //第一个数码管显示0 独立按键 按下第一个使数字+1 直到9循环到1,按下第 typedef unsigned int u16;//二个按键使数字-1 ,到0循环至9 typedef unsigned char u8; unsigned char code smgduan[]={0x3f, 0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; unsigned char code smgwei[]={0xfe, 0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; sbit rclk=P2^5; sbit srclk=P2^6; sbit ser=P2^7; sbit key=P3^0; sbit key1=P3^1; u8 a=1; void smg_dis(); voidsend_595(u8 n); void delay(u16 n) { u8 i ; while (n--) for(i=0;i<125;i++); } void main() { while(1) { if(key==0) { delay(100); if(key==0) { a++; if(a==11) a=1; } } if(key1==0) { delay(100); if(key1==0) { a--; if(a==0) a=10; } } smg_dis(); } } void smg_dis() { send_595(smgwei[0]); send_595(smgduan[a-1]); rclk=0; rclk=1; delay(1); } voidsend_595(u8 n) { u8 i; for(i=0;i<8;i++) { if(n&0x80 ) ser=1; else ser=0; srclk=0; srclk=1; n=n<<1; } } #include<reg52.h>//矩阵键盘数码管显示相应键码(16进制显示) typedef unsigned char u8; typedef unsigned int u16; sbit ser=P2^7; sbit srclk=P2^6; sbit rclk=P2^5; unsigned char code smgduan[]={0x3f, 0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f, 0x77,0x7c,0x39,0x5e,0x79,0x71}; unsigned char code smgwei[]={0xfe, 0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; unsigned char code colu[]={0x7f,0xbf,0xdf,0xef}; void DelayMs(u16 n); void DispSmg(); unsigned char keyscan(); void send_595(u8 dat); void main() { while(1) { DispSmg(); } } void DelayMs(u16 n) { u8 i; while(n--) for(i=0;i<113;i++); } void send_595(u8 dat) { u8 i; for(i=0;i<8;i++) { if(dat&0x80) ser=1; else ser=0; srclk=0; srclk=1; dat=dat<<1; } } unsigned char keyscan() { u8 i,temp,row,column; P3=0x0f; temp=P3&0x0f; if(temp!=0x0f) { DelayMs(20); temp=P3&0x0f; if(temp!=0x0f) { switch(temp) { case 0x0e:row=3;break; case 0x0d:row=2;break; case 0x0b:row=1;break; case 0x07:row=0;break; default:break; } for(i=0;i<4;i++) { P3=colu[i]; temp=P3&0x0f; if(temp!=0x0f) {column=i; break; } } return(row*4+column); } } } void DispSmg() { u8 n,a; n=500; a=keyscan(); while(n--) { send_595(smgwei[0]); send_595(smgduan[a]); rclk=0; rclk=1; DelayMs(1); } } #include<reg52.h>//1602 第一行显示“^_^”第二行显示“Hellow,everyone”,接着在第一行显示 typedef unsigned char u8;//“Wish You” 第二行显示“STUDY MCU WELL” typedef unsigned int u16; sbit RS=P3^5; sbit RW=P3^6; sbit EN=P3^4; u8 code StrCode[]="Hellow,everyone" ; u8 code StrCode1[]="Wish You" ; u8 code StrCode2[]="STUDY MCU WELL" ; void delayms(u16 n); void W_cmd(u8 cmd); void W_dat(u8 dat); void init(); void main() { u8 j; while(1){ init(); W_cmd(0x80+6); W_dat(0x5e); W_cmd(0x80+7); W_dat(0x5f); W_cmd(0x80+8); W_dat(0x5e); for(j=0;j<15;j++){ W_cmd(0xc0+j+1); W_dat(StrCode[j]); delayms(1000); } init(); for(j=0;j<8;j++){ W_cmd(0x80+j+4); W_dat(StrCode1[j]); delayms(1000); } for(j=0;j<14;j++){ W_cmd(0xc0+j); W_dat(StrCode2[j]); delayms(1000); } } } void delayms(u16 n) { u8 i; while(n--) for(i=0;i<113;i++); } void W_cmd(u8 cmd) { EN=0; RS=0; RW=0; EN=1; P0=cmd; delayms(2); EN=0; } void W_dat(u8 dat) { EN=0; RS=1; RW=0; EN=1; P0=dat; delayms(2); EN=0; } void init() { W_cmd(0x38); W_cmd(0x0f); W_cmd(0x06); W_cmd(0x01); }
/*课程设计自动计数器 数码管显示 一个按键每次按下使数字+1*/ /*同时LED灯闪一次,另一个按键按下后清零同时蜂鸣器响一声*/ #include<reg52.h> typedef unsigned char u8; typedef unsigned int u16; sbit ser=P2^7; //定义数码管端口 sbit srclk=P2^6; sbit rclk=P2^5; sbit key1=P3^0; //定义独立键盘端口 sbit key2=P3^1; sbit key3=P2^3; //定义蜂鸣器端口 unsigned char code smgduan[]={0x3f, 0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f, 0x77,0x7c,0x39,0x5e,0x79,0x71}; //段选数组 unsigned char code smgwei[]={0xfe, 0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //位选数组 void DelayMs(u16 n); //延时函数 u8 DispSmg(u8 n); //数码管显示函数 void send_595(u8 dat); //输送段选位选数据函数 void main() //主函数 { u16 n; while(1) { DispSmg(n); if(key2==0) //若key2按下 { DelayMs(100); if(key2==0) //消抖 确认key2被按下 { n=0; //n被赋值为0 key3=0; DelayMs(20); key3=1; //蜂鸣器响0.02秒后灭 } } if(key1==0) { DelayMs(100); if(key1==0) //消抖 确认key1被按下 { n++; //n每次在原来的基础上+1 P1=0xfe; DelayMs(100); P1=0xff; //发光二极管亮0.1秒后灭 } } }} void DelayMs(u16 n) { u8 i; while(n--) for(i=0;i<113;i++); } u8 DispSmg(u8 n) { send_595(smgwei[5]); //输送位码数据 提供显示位置 send_595(smgduan[n/100]); //输送段码数据 提供显示内容 rclk=0; rclk=1; //上升沿有效 说明输送数据到数码管 send_595(smgwei[6]); send_595(smgduan[n/10%10]); rclk=0; rclk=1; send_595(smgwei[7]); send_595(smgduan[n%10]); rclk=0; rclk=1; } void send_595(u8 dat) { u8 i; for(i=0;i<8;i++) //比较8次 每次提交一位数据 一共八位数据 { if(dat&0x80) ser=1; else ser=0; srclk=0; srclk=1; //上升沿有效 说明输送数据到数码管 dat=dat<<1; 被输送的数据左移一位 } }