请教一个问题,关于c51的程序
程序代码:
#include <./Atmel/REG51.H> #include "tm1618.h" #define uchar unsigned char //255 #define uint unsigned int //65535 unsigned char code tab[]={ 0x3F // 0 ,0x06 // 1 ,0x5B // 2 ,0x4F // 3 ,0x66 // 4 ,0x6D // 5 ,0x7D // 6 ,0x07 // 7 ,0x7F // 8 ,0x6F // 9 ,0x77 // A ,0x7C // b ,0x39 // C ,0x5E // d ,0x79 // E ,0x71 // F ,0x40 // - ,0x80 // . ,0 // 全灭 }; void delay100ms(void) //误差 0us { unsigned char a,b,c; for(c=19;c>0;c--) for(b=20;b>0;b--) for(a=130;a>0;a--); } main() { uchar i = 0; uchar num[4] = {0}; init_TM1618(); for(i=0;i<18;i++) { Write_num(0,tab[i]); Write_num(2,tab[i]); Write_num(4,tab[i]); Write_num(6,tab[i]); delay100ms(); } while(1) { i = Read_key(); while(Read_key() == i)//等待按键释放 { } num[i]++; if(num[i] >15) { num[i]=0; } Write_num(i*2,tab[num[i]]); } }这是主函数
程序代码:
#ifndef _TM1618_H #define _TM1618_H #include <./Atmel/REG51.H> #define BYTE unsigned char //引脚定义 sbit DIO=P2^0; sbit CLK=P2^1; sbit STB=P2^2; void TM1618_WriteBYTE(unsigned char DATA) //写一个字节 { unsigned char i; for(i=0;i<8;i++) { CLK=0; if(DATA&0X01) DIO=1; else DIO=0; DATA>>=1; CLK=1; } } unsigned char TM1618_Read(void) //读数据函数 { unsigned char i; unsigned char temp=0; DIO=1; //设置为输入 for(i=0;i<8;i++) { temp>>=1; CLK=0; CLK=1; if(DIO) temp|=0X80; } return temp; } void Write_command(unsigned char cmd) //发送命令字 { STB=0; TM1618_WriteBYTE(cmd); STB=1; } unsigned char Read_key(void) //读按键 { unsigned char a=0, b=0, c=0; STB=0; TM1618_WriteBYTE(0x42); a = TM1618_Read(); b = TM1618_Read(); c = TM1618_Read(); STB=1; if((a & 0x02) != 0) { return 0; } if((a & 0x10) != 0) { return 1; } if((b & 0x02) != 0) { return 2; } if((b & 0x10) != 0) { return 3; } return 255; } void Write_num(BYTE add, BYTE DATA) { Write_command(0x44); STB=0; TM1618_WriteBYTE(0xc0|add); TM1618_WriteBYTE(DATA&0x1F);//seg1~seg5 键扫描 STB=1; STB=0; TM1618_WriteBYTE(0xc0|add+1); TM1618_WriteBYTE((DATA&0xE0)>>2);//seg12~seg14 STB=1; } void init_TM1618(void) { unsigned char i; Write_command(0x00);//4位8段模式 Write_command(0x8b);//亮度 Write_command(0x40);//地址自动+1模式 STB=0; TM1618_WriteBYTE(0xc0); for(i=0;i<16;i++) TM1618_WriteBYTE(0x00); STB=1; } #endif这是调用函数等等。程序的实现功能是四个按键控制四个数码管,按一下加一,怎么样修改才能实现四个按键功能分别为:控制小数点,加以键,减一键,确认键