- 單片機C語言程序設計實訓100例
- 彭偉編著
- 477字
- 2018-12-27 05:12:05
3.15 K1~K4控制數碼管加減演示
上一個案例所顯示的最大數字為8,本例重新編寫程序,在數碼管上顯示000~255范圍內的任意整數,而且,在不松開按鍵時,數字會按一定時間間隔持續遞增或遞減。
程序設計調試與實訓:
本例要點在于函數Show_Counts_ON_DSY的編寫,000~255之間的整數要顯示在3位數碼管上時,需要先將待顯示的3位整數分解為3個數位,然后根據它們的值提取對應段碼。
本例固定顯示3個數位,讀者可修改程序,不顯示高位無效的0,如002僅顯示2,039僅顯示39。
源程序代碼:
//----------------------------------------------------------------- // 名稱: K1~K4 控制數碼管加減演示 //----------------------------------------------------------------- // 說明: K1按下后加1計數, // K2按下后減1計數, // K3按下后清零。 //----------------------------------------------------------------- #include <reg51.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int //0~9的數碼管段碼,最后一個為關閉 uchar code DSY_CODE[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xff}; //待顯示的3位數 uchar Num_Buffer[] = {0,0,0}; //按鍵代碼,按鍵計數 uchar Key_Code,Key_Counts=0; //----------------------------------------------------------------- // 延時 //----------------------------------------------------------------- void DelayMS(uint x) { uchar t; while(x--) for(t = 0; t < 120; t++); } //----------------------------------------------------------------- // 顯示子程序 //----------------------------------------------------------------- void Show_Counts_ON_DSY() { uchar i,j = 0x01; Num_Buffer[2] = Key_Counts / 100; Num_Buffer[1] = Key_Counts / 10 % 10; Num_Buffer[0] = Key_Counts % 10; for (i = 0;i < 3; i++) { j = _cror_(j,1); P2 = j; P0 = DSY_CODE[Num_Buffer[i]] ; DelayMS(1); } } //----------------------------------------------------------------- // 主程序 //----------------------------------------------------------------- void main() { uchar i; P0 = 0xFF; P1 = 0xFF; P2 = 0x00; Key_Code = 0xFF; while(1) { Show_Counts_ON_DSY(); P1 = 0xFF; Key_Code = P1; //有鍵按下時,數碼管刷新顯示30次,該行代碼同時起到了延時作用 if (Key_Code != 0xFF) for(i = 0; i < 30; i++) Show_Counts_ON_DSY(); switch(Key_Code) { case 0xFE: if (Key_Counts < 255) Key_Counts++; break; case 0xFD: if (Key_Counts > 0) Key_Counts--; break; case 0xFB: Key_Counts = 0; } Key_Code = 0xFF; } }
推薦閱讀
- DevOps:軟件架構師行動指南
- Learn TypeScript 3 by Building Web Applications
- Practical Internet of Things Security
- Linux網絡程序設計:基于龍芯平臺
- Windows Presentation Foundation Development Cookbook
- C語言程序設計案例式教程
- Getting Started with SQL Server 2012 Cube Development
- Apache Mesos Essentials
- Python:Master the Art of Design Patterns
- PHP 7+MySQL 8動態網站開發從入門到精通(視頻教學版)
- Odoo 10 Implementation Cookbook
- Python商務數據分析(微課版)
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐
- SCRATCH編程課:我的游戲我做主
- Java Web應用開發