1
完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
实现效果:
/* * @Author: Guoquan Wei * @Date: 2018-01-23 11:03:32 * @Last Modified by: Guoquan Wei * @Last Modified time: 2018-01-23 11:12:22 */ #include #define uchar unsigned char #define uint unsigned int uchar datable[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71}; //定义段选 uchar watable[] = {0x3f, 0x1f, 0x2f, 0x37, 0x3b, 0x3d, 0x3e}; //定义位选,位选六位,最高两位初始化定为零 uchar lovetable[] = {5, 2, 0, 1, 3, 1, 4}; uchar *start = lovetable; //只是记录lovetable首地址,它不会改变 uchar *p = lovetable; // 指针 uchar *q; ***it dula = P2 ^ 6; ***it wela = P2 ^ 7; #define KEY P3 int truenum, totalnum; uchar flag; void delayms(int x) { int i, j; for (i = 0; i < x; i++) for (j = 0; j < 110; j++) ; } void display(uchar numdis) //numdis是要显示的数字 { uchar shi, ge; shi = numdis / 10; ge = numdis % 10; dula = 1; P0 = datable[shi]; dula = 0; P0 = 0xff; wela = 1; P0 = watable[2]; wela = 0; delayms(1); dula = 1; P0 = datable[ge]; dula = 0; P0 = 0xff; wela = 1; P0 = watable[1]; wela = 0; delayms(1); } void display1(uchar numdis) //numdis是要显示的数字 { uchar shi, ge; shi = numdis / 10; ge = numdis % 10; dula = 1; P0 = datable[shi]; dula = 0; P0 = 0xff; wela = 1; P0 = watable[6]; wela = 0; delayms(1); dula = 1; P0 = datable[ge]; dula = 0; P0 = 0xff; wela = 1; P0 = watable[5]; wela = 0; delayms(1); } //------------------------------------------- unsigned char Keyboard(void) { unsigned char x, y, z = 0, jz; KEY = 0xf0; //低四位置低,即行接低 if (KEY != 0xf0) //是否有按键按下 { delayms(100); //去抖 if (KEY != 0xf0) //确实有按键按下 { x = KEY; //读取按键所在的行 KEY = 0x0f; //高四位置低,即列接低 y = KEY; //读取按键所在的列 z = x | y; //按键的坐标为行+列 while (KEY != 0x0f) ; //等待按键抬起 } } switch (z) { case 0xee: { jz = 0; break; } //按键值显示为0 case 0xde: { jz = 1; break; } //按键值显示为1 case 0xbe: { jz = 2; break; } case 0x7e: { jz = 3; break; } case 0xed: { jz = 4; break; } case 0xdd: { jz = 5; break; } case 0xbd: { jz = 6; break; } case 0x7d: { jz = 7; break; } case 0xeb: { jz = 8; break; } case 0xdb: { jz = 9; break; } case 0xbb: { jz = 10; break; } case 0x7b: { jz = 11; break; } case 0xe7: { jz = 12; break; } case 0xd7: { jz = 13; break; } case 0xb7: { jz = 14; break; } case 0x77: { jz = 15; break; } default: { jz = 16; break; } // 没有按键按下,返回16 } return jz; } /*if (key == 0) //答题正确 { truenum++; totalnum++; flag = 1; } if (key == 1) //答题错误 { totalnum++; flag = 2; } if (key == 2) { if (flag == 1) { if (totalnum > 0) { truenum--; totalnum--; } } if (flag == 2) { if (totalnum > 0) { totalnum--; } } flag = 0; //表明本次执行的是按键3 }*/ int main() { uchar key; int i, j, k; while (totalnum < 3) { display(truenum); display1(totalnum); key = Keyboard(); if (key == 0) //答题正确 { truenum++; totalnum++; flag = 1; } if (key == 1) //答题错误 { totalnum++; flag = 2; } if (key == 2) { if (flag == 1) { if (totalnum > 0) { truenum--; totalnum--; } } if (flag == 2) { if (totalnum > 0) { totalnum--; } } flag = 0; //表明本次执行的是按键3 } } /*while (1) { dula = 1; P0 = datable[5]; dula = 0; P0 = 0xff; wela = 1; P0 = watable[3]; wela = 0; delayms(1); dula = 1; P0 = datable[2]; dula = 0; P0 = 0xff; wela = 1; P0 = watable[2]; wela = 0; delayms(1); dula = 1; P0 = datable[0]; dula = 0; P0 = 0xff; wela = 1; P0 = watable[1]; wela = 0; delayms(1); }*/ while (1) { //start存储lovetable的首地址,且一直都不变;q是一直存储着start+k的地址,在内层的for循环里是不变的,q其实表示最左边数码管在一段时间内要显示的数对应的lovetable的地址;只有p一直在变,它用来显示段选 for (k = 0; k < 7; k++) for (i = 1, j = 0, q = p = start + k; j < 300; i++, j++) { if (i >= 7) { i = 1; } dula = 1; P0 = datable[*p++]; if (p >= start + 7)//p指向lovetable末尾的下一位时,把p移到start { p = start; } if (p - q == 6 || q - p == 1)//因为数码管是6位,而要显示7位,所以只能选取其中的六位。当p在q的后面时,p-q==6时,就要归位了;当q在p的后面时,则q-p==1时归位。 { p = q; } dula = 0; P0 = 0xff; P0 = 0xff; P0 = 0xff; P0 = 0xff; P0 = 0xff; wela = 1; P0 = watable[7 - i]; wela = 0; P0 = 0xff; P0 = 0xff; delayms(5); } } } |
|
|
|
只有小组成员才能发言,加入小组>>
3310 浏览 9 评论
2991 浏览 16 评论
3492 浏览 1 评论
9055 浏览 16 评论
4086 浏览 18 评论
1174浏览 3评论
603浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
596浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2333浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1894浏览 2评论
小黑屋| 手机版| Archiver| 德赢Vwin官网 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-21 16:03 , Processed in 1.077895 second(s), Total 48, Slave 39 queries .
Powered by 德赢Vwin官网 网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
德赢Vwin官网 观察
版权所有 © 湖南华秋数字科技有限公司
德赢Vwin官网 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号