这个是利用iic驱动的oled屏幕显示,与数码管显示会有不同,但主要的程序还是一样的,希望可以给大家提供一些资料
单片机源程序如下:
- #include "reg52.h"
- #include "intrins.h"
- #include "codetab.h"
- #include "LQ12864.h"
- #include<stdio.h>
- #define uchar unsigned char
- sbit dula=P2^6;
- sbit wela=P2^7;
- sbit beep=P2^3;
- uchar key,j,k,temp,wei;
- daojishi=5;
- bit begin,end,clear;
- int a0,b0;c0;d0;e0;f0=5;
- unsigned int pp;
- uchar diss[20]={0};
- void delay1(int i) //延时函数
- {
- for(j=i;j>0;j--)
- for(k=125;k>0;k--);
- }
- void keyscan() //按键扫描
- {
- P3=0xfe;
- temp=P3;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay1(10);
- if(temp!=0xf0)
- {
- temp=P3;
- switch(temp)
- {
- case 0xee:
- key=1;
- wei++;
- break;
- case 0xde:
- key=2;
- wei++;
- break;
- case 0xbe:
- key=3;
- wei++;
- break;
- case 0x7e:
- key=4;
- wei++;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P3;
- temp=temp&0xf0;
- beep=0;
- }
- beep=1;
- }
- }
- P3=0xfd;
- temp=P3;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- if(temp!=0xf0)
- {
- temp=P3;
- switch(temp)
- {
- case 0xed:
- key=5;
- wei++;
- break;
- case 0xdd:
- key=6;
- wei++;
- break;
- case 0xbd:
- clear=1;
- break;
- case 0x7d:
- begin=1;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P3;
- temp=temp&0xf0;
- beep=0;
- }
- beep=1;
- }
- }
- }
- void kaiji(void)
- {
- OLED_Init(); //OLED初始化
- OLED_P16x16Ch(0,0,1); //初始显示的字符
- OLED_P16x16Ch(16,0,2);
- OLED_P16x16Ch(33,0,3);
- OLED_P16x16Ch(0,3,4);
- OLED_P16x16Ch(16,3,5);
- OLED_P16x16Ch(32,3,6);
- OLED_P16x16Ch(0,6,7);
- OLED_P16x16Ch(16,6,8);
- OLED_P16x16Ch(32,6,9);
- }
- void fangui()
- {
- if(key!=0)
- {
- switch(wei) //传递字符,显示
- {
- case 1: a0=key;
- sprintf((int*)diss,"%d",a0);
- OLED_P6x8Str(40,0,diss);
- beep=0;
- break;
- case 2: b0=key;
- sprintf((int*)diss,"%d",b0);
- OLED_P6x8Str(40,0,diss);
- beep=0;
- break;
- case 3: c0=key;
- sprintf((int*)diss,"%d",c0);
- OLED_P6x8Str(40,0,diss);
- beep=0;
- break;
- case 4: d0=key;
- sprintf((int*)diss,"%d",d0);
- OLED_P6x8Str(40,0,diss);
- beep=0;
- break;
- case 5: e0=key;
- sprintf((int*)diss,"%d",e0);
- OLED_P6x8Str(40,0,diss);
- beep=0;
- break;
- case 6: f0=key;
- sprintf((int*)diss,"%d",f0);
- OLED_P6x8Str(40,0,diss);
- beep=0;
- break;
- }
- }
- }
- void main(void)
- {
- kaiji();
- TMOD=0x01;
- TH0=(65536-46080)/256;
- TL0=(65536-46080)%256;//46080的来历,为50000*11.0592/12
- ET0=1;
- EA=1;
- while(1)
- {
- keyscan();
- if(begin) //开始计时并抢答
- {
- if(!end)
- {
- TR0=1; //中断开始
- if(pp==20) //中断次数
- {
- pp=0;
- daojishi--;
- }
- sprintf((int*)diss,"%d",daojishi);
- OLED_P6x8Str(40,6,diss);
- if(key!=0)
- {
- a0=key; //倒计时结束
- sprintf((int*)diss,"%d",a0);
- OLED_P6x8Str(40,3,diss);
- }
- if(!daojishi|a0)
- {
- end=1;
- TR0=0; //中断暂停
- pp=0;
- }
- }
- }
- else
- {
- fangui();
- }
- if(clear)
- {
- a0=0;key=0;begin=0;end=0;beep=1;daojishi=5;f0=5;clear=0; //数据清除
- OLED_CLS();//清屏
- kaiji();
- }
- }
- }
- void time0() interrupt 1 //中断函数
- { TH0=(65536-46080)/256;
- TL0=(65536-46080)%256;
- pp++;
- }
复制代码
所有资料51hei提供下载:
单片机课设.zip
(43.62 KB, 下载次数: 7)
|