略略略
Posted liumengyue
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了略略略相关的知识,希望对你有一定的参考价值。
#include <lcd.h> void delay(uint x) { uint i, j; for (i = x; i > 0; i--) for (j = 100; j > 0; j--); } void LcdWriteCom(uchar com) { LCD1602_E = 0; LCD1602_RS = 0; LCD1602_RW = 0; LCD1602_DATAPINS = com; delay(1); LCD1602_E = 1; delay(5); LCD1602_E = 0; } void LcdWriteData(uchar dat) { LCD1602_E = 0; LCD1602_RS = 1; LCD1602_RW = 0; LCD1602_DATAPINS = dat; delay(1); LCD1602_E = 1; delay(5); LCD1602_E = 0; } void LcdInit() { LcdWriteCom(0x38); LcdWriteCom(0x0c); LcdWriteCom(0x05); LcdWriteCom(0x01); // LcdWriteCom(0x80); LcdWriteCom(0x90); }
#ifndef __LCD_H_ #define __LCD_H_ #include <reg51.h> #ifndef uchar #define uchar unsigned char #endif #ifndef uint #define uint unsigned int #endif #define LCD1602_DATAPINS P0 sbit LCD1602_E = P2^7; sbit LCD1602_RW = P2^5; sbit LCD1602_RS = P2^6; void delay(uint); void LcdWriteCom(uchar); void LcdWriteData(uchar); void LcdInit(); #endif
/* S16 µÈÓÚ S4 ÍËλ S8¸´Î» S15 K1~K4¼Ó¼õ³Ë³ý */ #include <reg51.h> #include <lcd.h> #define uchar unsigned char #define uint unsigned int sbit k1 = P3^1; sbit k2 = P3^0; sbit k3 = P3^2; sbit k4 = P3^3; uchar Ans[10]; uchar code todig[] = { 7,8,9,0,4,5,6,0,1,2,3,0,0,0,0,0 }; uchar Disp[50]; uchar flag, MKValue, cnt, i, ope; int ans1, ans2; uchar MatrixKeyScan(void); uchar KeyScan(void); double getdig(int); void DisplayAns(); void Cal(void); double oper(double, double, char); int main() { LcdInit(); while (1) { flag = 0; flag = MatrixKeyScan(); KeyScan(); if (flag == 1)//Êý×Ö¼ü { Disp[++cnt] = todig[MKValue]+‘0‘; LcdWriteData(Disp[cnt]); } if (flag == 2)//µÈÓÚ { LcdWriteData(‘=‘); Disp[++cnt] = ‘=‘; //¼ÆËã Cal(); } if (flag == 3)//ÍËλ¼ü { cnt--; LcdWriteCom(0x01); LcdWriteCom(0x90); for (i = 1; i <= cnt; i++) LcdWriteData(Disp[i]); } if (flag == 4)//¸´Î»¼ü { LcdWriteCom(0x01); LcdWriteCom(0x90); cnt = 0; } if (flag == 5) { LcdWriteData(‘.‘); Disp[++cnt] = ‘.‘; } } } void Cal(void) { uchar i = 1; while (Disp[i] >= ‘0‘ && Disp[i] <= ‘9‘) { ans1 = ans1 * 10 + Disp[i] - ‘0‘; i++; } while (Disp[i] != ‘=‘)//¼ì²âµ½"="Ìø³öÑ»· { ope = Disp[i];//´æ´¢·ûºÅλ i++; while (Disp[i] >= ‘0‘ && Disp[i] <= ‘9‘) { ans2 = ans2 * 10 + Disp[i] - ‘0‘; i++; } switch (ope) { case(‘+‘): ans1 = ans1+ans2;break; case(‘-‘): ans1 = ans1-ans2;break; case(‘*‘): ans1 = ans1*ans2;break; case(‘/‘): ans1 = ans1/ans2;break; } ans2 = 0; } DisplayAns(); } void DisplayAns()//´¦Àí½á¹û²¢ÏÔʾ { int z = 0, i;//z λÊý if (ans1 < 0) { LcdWriteData(‘-‘); ans1 *= -1; } if(ans1 == 0) Ans[++z] = 0; else while (ans1) { Ans[++z] = ans1%10; ans1 /= 10; } for (i = z; i > 0; i--) { LcdWriteData(Ans[i]+48); } } uchar MatrixKeyscan() { uchar a = 0; P1 = 0x0f; if (P1 != 0x0f) { delay(10); if (P1 != 0x0f) { P1 = 0x0f; switch (P1) { case (0x07): MKValue = 0; break; case (0x0b): MKValue = 1; break; case (0x0d): MKValue = 2; break; case (0x0e): MKValue = 3; break; } P1 = 0xf0; switch (P1) { case (0xb0): MKValue += 4; break; case (0xd0): MKValue += 8; break; case (0xe0): MKValue += 12; break; } } while ((a < 50)&&(P1 != 0xf0)) { delay(10); a++; } if ((MKValue==0)||(MKValue==1)||(MKValue==2)||(MKValue==4)||(MKValue==5)|| (MKValue==6)||(MKValue==8)||(MKValue==9)||(MKValue==10)||(MKValue==13))//°´ÏÂÊý×Ö¼ü return 1; if (MKValue == 15)//µÈÓÚ return 2; if (MKValue == 3)//ÍËλ¼ü return 3; if (MKValue == 7)//¸´Î»¼ü return 4; if (MKValue == 14)//СÊýµã return 5; } return 0;//ÎÞЧ¼ü } uchar KeyScan() { if (k1 == 0) { delay (10); if (k1 == 0) { while (!k1); LcdWriteData(‘+‘); Disp[++cnt] = ‘+‘; } } if (k2 == 0) { delay (10); if (k2 == 0) { while (!k2); LcdWriteData(‘-‘); Disp[++cnt] = ‘-‘; } } if (k3 == 0) { delay (10); if (k3 == 0) { while (!k3); LcdWriteData(‘*‘); Disp[++cnt] = ‘*‘; } } if (k4 == 0) { delay (10); if (k4 == 0) { while (!k4); LcdWriteData(‘/‘); Disp[++cnt] = ‘/‘; } } return 0; }
/* k1k2 ·¢Çò k3k4½ÓÇò */ #include <reg52.h> #include <intrins.h> #define uint unsigned int #define uchar unsigned char sbit LSA = P2^2; sbit LSB = P2^3; sbit LSC = P2^4; sbit k1 = P3^1; sbit k2 = P3^0; sbit k3 = P3^2; sbit k4 = P3^3; sbit beep = P1^5; uchar code table[] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f }; uchar score1 = 0, score2 = 0; uchar flag = 0, i, a, dire;// dire = 1 l->r dire = 2 r->l uint v = 300, v0; void delay(uint); void Display(void); void Led(uchar); void Int0Init(void); void Int1Init(void); void Beep(void); int main() { Int0Init(); Int1Init(); while (1) { if (dire != 0) { flag = 0; Led(dire); if (flag == 0)//½ÓÇòʧ°Ü { Beep();//·äÃùÆ÷Ïì if (dire == 1) score1++; else score2++; dire = 0; a = 1000; while(a--) Display(); } } else //·¢Çò { if (k1 == 0) { delay(10); if (k1 == 0) { dire = 1; } while (!k1); } if (k2 == 0) { delay(10); if (k2 == 0) { dire = 2; } while (!k2); } } } } /*·äÃùÆ÷*/ void Beep(void) { uchar i = 100; while (i--) { beep = ~beep; delay(5); } delay(5); } /*Á÷Ë®µÆº¯Êý*/ void Led(uchar x) { if (x == 1) { P2 = 0x7f; delay(v); for (i = 0; i < 7; i++) { P2 = _cror_(P2,1); delay(v); } } if (x == 2) { P2 = 0xfe; delay(v); for (i = 0; i < 7; i++) { P2 = _crol_(P2,1); delay(v); } } } /*ÊýÂë¹ÜÏÔʾº¯Êý*/ void Display() { LSA = 0; LSB = LSC = 1; P0 = table[score1 / 10];delay(1);P0 = 0x00; LSA = LSC = 1; LSB = 0; P0 = table[score1 % 10];delay(1);P0 = 0x00; LSA = LSB = 0; LSC = 1; P0 = 0x40;delay(1);P0 = 0x00; LSA = LSB = 1; LSC = 0; P0 = 0x40;delay(1);P0 = 0x00; LSA = LSC = 0; LSB = 1; P0 = table[score2 / 10];delay(1);P0 = 0x00; LSA = 1; LSC = LSB = 0; P0 = table[score2 % 10];delay(1);P0 = 0x00; } /*INT0³õʼ»¯º¯Êý*/ void Int0Init(void) { IT0 = 1;//ϽµÑØÓÐЧ EX0 = 1;//´ò¿ªINT0ÖжÏÔÊÐí EA = 1; } void Int0() interrupt 0 { delay(10); if (k3 == 0) { if (P2 == 0x7f)//½ÓÇò³É¹¦ { P0 = 0x00; dire = 1; flag = 1; } } while (!k3); } /*INT1³õʼ»¯º¯Êý*/ void Int1Init(void) { IT1 = 1;//ϽµÑØÓÐЧ EX1 = 1;//´ò¿ªINT0ÖжÏÔÊÐí EA = 1; } void Int1() interrupt 2 { delay(10); if (k4 == 0) { if (P2 == 0xfe)//½ÓÇò³É¹¦ { P0 = 0x00; dire = 2; flag = 1; } } while (!k4); } /*ÑÓʱº¯Êý xms*/ void delay(uint x) { uint i, j; for (i = x; i > 0; i--) for (j = 100; j > 0; j--); }
/*Åжϲ»ÔÚͬһÐС¢Í¬Ò»ÁеĶà¸ö¼ü°´ÏÂ*/ #include <reg52.h> #include <intrins.h> #define uint unsigned int #define uchar unsigned char sbit LSA = P2^2; sbit LSB = P2^3; sbit LSC = P2^4; uchar code table[] = { 0x00,0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71 }; uint MKValue, a, flag, flag1, DValue = 0, P1s,MKValue2; void delay(uint); void MatrixKeyScan(void); int main() { LSA = LSB = LSC = 1; while (1) { MatrixKeyScan(); if (flag == 1) P0 = table[MKValue]; } } void delay(uint x) { uint j, k; for (j = x; j > 0; j--) for (k = 110; k > 0; k--); } void MatrixKeyScan(void) { flag = 0; P1 = 0x0f; if (P1 != 0x0f) { delay(10); if (P1 != 0x0f) { P1 = 0x0f; switch (P1)//? { case(0x07): MKValue = 1; break; case(0x0b): MKValue = 2; break; case(0x0d): MKValue = 3; break; case(0x0e): MKValue = 4; break; } P1 = 0xf0; switch (P1)//? { case(0xb0): MKValue += 4; break; case(0xd0): MKValue += 8; break; case(0xe0): MKValue += 12; break; } P1s = P1; while ( P1 != 0xf0)//P1ľÓÐËÉ¿ª { if (P1 != P1s)//¼ì²âµ½µÚ¶þ¸ö¼ü°´Ï { delay(10); if (P1 != P1s) { P1 = 0x0f; switch (P1)//DValue ´æ´¢×éºÏ¼üµÄÖµ { case(0x03): DValue = 1; break; case(0x05): DValue = 2; break; case(0x06): DValue = 3; break; case(0x09): DValue = 4; break; case(0x0a): DValue = 5; break; case(0x0c): DValue = 6; break; } P1 = 0xf0; switch (P1)//? { case(0x50): DValue += 6; break; case(0x60): DValue += 12; break; case(0x90): DValue += 18; break; case(0xa0): DValue += 24; break; case(0xc0): DValue += 30; break; } switch (DValue)//¼ÆËãÁíÒ»¸ö¼üµÄÖµ´æÈëMKV2ÖÐ { case(1):if (MKValue == 1) MKValue2 = 6; if (MKValue == 2) MKValue2 = 5; if (MKValue == 5) MKValue2 = 2; if (MKValue == 6) MKValue2 = 1;//-->7-MKV while(P1 == 0x30);//µÚ¶þ¸ö¼üËÉ¿ª P0 = table[MKValue2]; break; case(2):MKValue2 = 8 - MKValue; while(P1 == 0x30); P0 = table[MKValue2]; break; case(3): case(4):MKValue2 = 9 - MKValue; while(P1 == 0x30); P0 = table[MKValue2]; break; case(5):MKValue2 = 10 - MKValue; while(P1 == 0x30); P0 = table[MKValue2]; break; case(6):MKValue2 = 11 - MKValue; while(P1 == 0x30); P0 = table[MKValue2]; break; case(7):MKValue2 = 11 - MKValue; while(P1 == 0x50); P0 = table[MKValue2]; break; case(8):MKValue2 = 12 - MKValue; while(P1 == 0x50); P0 = table[MKValue2]; break; case(9): case(10):MKValue2 = 13 - MKValue; while(P1 == 0x50); P0 = table[MKValue2]; break; case(11):MKValue2 = 14 - MKValue; while(P1 == 0x50); P0 = table[MKValue2]; break; case(12):MKValue2 = 15 - MKValue; while(P1 == 0x50); P0 = table[MKValue2]; break; case(13):MKValue2 = 15 - MKValue; while(P1 == 0x60); P0 = table[MKValue2]; break; case(14):MKValue2 = 16 - MKValue; while(P1 == 0x60); P0 = table[MKValue2]; break; case(15): case(16):MKValue2 = 17 - MKValue; while(P1 == 0x60); P0 = table[MKValue2]; break; case(17):MKValue2 = 18 - MKValue; while(P1 == 0x60); P0 = table[MKValue2]; break; case(18):MKValue2 = 19 - MKValue; while(P1 == 0x60); P0 = table[MKValue2]; break; case(19):MKValue2 = 15 - MKValue; while(P1 == 0x90); P0 = table[MKValue2]; break; case(20):MKValue2 = 16 - MKValue; while(P1 == 0x90); P0 = table[MKValue2]; break; case(21): case(22):MKValue2 = 17 - MKValue; while(P1 == 0x90); P0 = table[MKValue2]; break; case(23):MKValue2 = 18 - MKValue; while(P1 == 0x90); P0 = table[MKValue2]; break; case(24):MKValue2 = 19 - MKValue; while(P1 == 0x90); P0 = table[MKValue2]; break; case(25):MKValue2 = 19 - MKValue; while(P1 == 0xa0); P0 = table[MKValue2]; break; case(26):MKValue2 = 20 - MKValue; while(P1 == 0xa0); P0 = table[MKValue2]; break; case(27): case(28):MKValue2 = 21 - MKValue; while(P1 == 0xa0); P0 = table[MKValue2]; break; case(29):MKValue2 = 22 - MKValue; while(P1 == 0xa0); P0 = table[MKValue2]; break; case(30):MKValue2 = 23 - MKValue; while(P1 == 0xa0); P0 = table[MKValue2]; break; case(31):MKValue2 = 23 - MKValue; while(P1 == 0xc0); P0 = table[MKValue2]; break; case(32):MKValue2 = 24 - MKValue; while(P1 == 0xc0); P0 = table[MKValue2]; break; case(33): case(34):MKValue2 = 25 - MKValue; while(P1 == 0xc0); P0 = table[MKValue2]; break; case(35):MKValue2 = 26 - MKValue; while(P1 == 0xc0); P0 = table[MKValue2]; break; case(36):MKValue2 = 27 - MKValue; while(P1 == 0xc0); P0 = table[MKValue2]; break; } DValue = 0; } } } flag = 1; } } }
/*k1 ¼õËÙ k2 ¼ÓËÙ k3 ÔÝÍ£/¼ÌÐø k4 ºôÎüµÆ*/ #include <reg52.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int sbit k0 = P3^1; sbit k1 = P3^0; sbit k2 = P3^2; sbit k3 = P3^3; uchar flag = 0, flag1 = 0, P2ram;//ÔÝÍ£flag ºôÎüµÆflag1 uint PWM_Low, Clock = 500, i; uint v; void KeyScan(void); void delay1ms(uint); void delay10us(uint); void PWMLed(void); void Timer0Init(void); int main() { Timer0Init(); P2 = 0xfe; while(1) { KeyScan(); if (flag1 == 1) { PWMLed(); } delay1ms(100+v); } } void Timer0Init() { TMOD |= 0x01; TH0 = (65535-50000) / 256; TL0 = (65535-50000) % 256; ET0 = 1; EA = 1; TR0 = 1; } void Timer0() interrupt 1 { TH0 = (65535-50000) / 256;//50ms TL0 = (65535-50000) % 256; i ++; if (i*50>v+100) { i = 0; if (flag != 1) P2 = _crol_(P2,1); else P2ram = _crol_(P2ram,1);//P2Ϊ0xffʱ ÓÃP2ram´úÌæP2ÒÆλ } } void KeyScan(void) { if (k0 == 0) { delay1ms(10); if (k0 == 0) { while (!k0); v -= 30; } } if (k1 == 0) { delay1ms(10); if (k1 == 0) { while (!k1); v += 30; } } if (k2 == 0) { delay1ms(10); if (k2 == 0) { while (!k2); TR0 = ~TR0; } } if (k3 == 0) { delay1ms(10); if (k3 == 0) { while (!k3); flag1 ^= 1;//ºôÎüµÆ¿ª¹Øflag } } } void PWMLed() { for (PWM_Low = Clock; PWM_Low > 0; PWM_Low --) { KeyScan(); if (flag1 == 0) break; delay10us(PWM_Low);//µÍµçƽdelay P2ram = P2; P2 = 0xff; flag = 1;//¶¨Ê±Æ÷P2ÒÆλflag flag = 1ÓÉP2ram´úÌæP2ÒÆλ delay10us(Clock-PWM_Low);//¸ßµçƽdelay P2 = P2ram; flag = 0; } } void delay1ms(uint x) { uint i, j; for (i = x; i > 0; i--) for (j = 100; j > 0; j--); } void delay10us(uint x) { while (x--); }
/* k3-ÔÝÍ£ k4-ÒÆλ k1 +1 */ #include <reg52.h> #define uchar unsigned char #define uint unsigned int sbit LSA = P2^2; sbit LSB = P2^3; sbit LSC = P2^4; sbit k1 = P3^1; sbit k2 = P3^0; sbit k3 = P3^2; sbit k4 = P3^3; uchar code table[] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f }; uchar tab[10]; uchar i, p = 1;//pÖ¸Ïò¸Ä±äλ void Timer0Init(void); void Int0Init(void); void Display(void); void delay(uint); void KeyPros(void); int main() { Timer0Init(); Int0Init(); while (1) { KeyPros(); Display(); } } void KeyPros() { if (k1 == 0) { delay(10); if (k1 == 0) { tab[p]++; if ((p%2 == 1) && (tab[p] == 6)) tab[p] = 0; if ((p%2 == 0) && (tab[p] == 10)) tab[p] = 0; } while (!k1); } if (k2 == 0) { delay(10); if (k2 == 0) { } while (!k2); } if (k4 == 0) { delay(10); if (k4 == 0) { p++; if (p > 6) p = 1; } while (!k4); } } void Display() { LSA = LSB = LSC = 1; P0 = table[tab[1]];delay(1);P0 = 0x00; LSA = 0; LSB = LSC = 1; P0 = table[tab[2]];delay(1);P0 = 0x00; LSA = LSC = 1; LSB = 0; P0 = 0x40;delay(1);P0 = 0x00; LSA = LSB = 0; LSC = 1; P0 = table[tab[3]];delay(1);P0 = 0x00; LSA = LSB = 1; LSC = 0; P0 = table[tab[4]];delay(1);P0 = 0x00; LSA = LSC = 0; LSB = 1; P0 = 0x40;delay(1);P0 = 0x00; LSA = 1; LSC = LSB = 0; P0 = table[tab[5]];delay(1);P0 = 0x00; LSA = LSB = LSC = 0; P0 = table[tab[6]];delay(1);P0 = 0x00; } void Int0Init(void) { IT0 = 1;//ϽµÑØÓÐЧ EX0 = 1;//´ò¿ªINT0ÖжÏÔÊÐí EA = 1; } void Int0() interrupt 0 { delay(10); if (k3 == 0) TR0 = ~TR0; } void Timer0Init(void) { TMOD |= 0x01; TH0 = (65535 - 50000) / 256; TL0 = (65535 - 50000) % 256; ET0 = 1; TR0 = 1; EA = 1; } void Timer0() interrupt 1 { TH0 = (65535 - 50000) / 256; TL0 = (65535 - 50000) % 256; i++; if (i == 20) { i = 0; tab[6] ++; if (tab[6] == 10)//ÒÔÏ´¦Àí½øλ { tab[6] = 0; if (tab[5] != 5) tab[5] ++; else { tab[5] = 0; tab[4] ++; if (tab[4] == 10) { tab[4] = 0; if(tab[3] != 5) tab[3] ++; else { tab[3] = 0; tab[2] ++; if (tab[2] == 10) { tab[2] = 0; if (tab[1] != 5) tab[1]++; else tab[1] = 0; } } } } } } } void delay(uint x) { uint i, j; for (i = x; i > 0; i--) for (j = 100; j > 0; j--); }
/* k1ÏÔʾÃû×Ö k2ÏÔʾÉúÈÕ */ #include <reg51.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int #define COMMONPORTS P0 sbit SER = P3^4; sbit RCLK = P3^5; sbit SRCLK = P3^6; sbit k0 = P3^1; sbit k1 = P3^0; /*ÁÐѡͨ¿ØÖÆ*/ uchar code TAB[] = {0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe}; //µãÕó×ÖÂë(ÁÐ) uchar code lattice[4][8] = { {0x31,0xaa,0x64,0x3a,0x21,0x18,0x01,0x7f}, {0x50,0x61,0xf6,0x6a,0x5b,0x6e,0xf0,0x60}, {0x38,0x00,0xff,0x40,0xbf,0x68,0x6f,0xb9}, {0x00,0x07,0x05,0x77,0x00,0x00,0x00,0x77} }; void delay(uint); void Hc595SendByte(uchar); uint KeyScan(void); int main() { uint i, j, k, flag; while(1) { flag = KeyScan(); if (flag == 1) { for (j = 0; j < 3; j++) { for (k = 0; k < 50; k++) //Á½¸ö×ÖÖ®¼äµÄɨÃè¼ä¸ôʱ¼ä { for (i = 0; i < 8; i++) { Hc595SendByte(0x00); COMMONPORTS = TAB[i]; Hc595SendByte(lattice[j][i]); delay(2); } } } Hc595SendByte(0x00);//ÏûÓ° } else if (flag == 2) { for (k = 0; k < 50; k++) //Á½¸ö×ÖÖ®¼äµÄɨÃè¼ä¸ôʱ¼ä { for (i = 0; i < 8; i++) { Hc595SendByte(0x00); COMMONPORTS = TAB[i]; Hc595SendByte(lattice[3][i]); delay(2); } } Hc595SendByte(0x00);//ÏûÓ° } } } /*ÑÓʱº¯Êý*/ void delay(uint x) { uint i, j; for (i = x; i > 0; i--) for (j = 110; j > 0; j--); } /*ÏòHc595·¢ËÍÒ»¸ö×Ö½ÚµÄÊý¾Ý*/ void Hc595SendByte(uchar dat) { uchar i; SRCLK = 0; RCLK = 0; for (i = 0; i < 8; i++) { SER = dat >> 7; dat <<= 1; SRCLK = 1; _nop_(); _nop_(); SRCLK = 0; } RCLK = 1; _nop_(); _nop_(); RCLK = 0; } /*°´¼üɨÃ躯Êý*/ uint KeyScan(void) { if (k0 == 0) { delay(10); if (k0 == 0) { while (!k0); return 1; } } if (k1 == 0) { delay(10); if (k1 == 0) { while (!k1); return 2; } } }
/* S8 ¸´Î» S12 ÔÝÍ£ S16È·¶¨ S1~3 5~7 9~11 14 Êý×Ö¼ü */ #include <reg52.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int sbit LSA = P2^2; sbit LSB = P2^3; sbit LSC = P2^4; sbit beep = P1^5; uchar code table[] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f }; uchar code dig[] = { 7,8,9,0,4,5,6,0,1,2,3,0,0,0,0,0 }; uchar tab[10];//´æ´¢¼üÈëÊý±í uchar MKValue, flag = 0, cnt = 0, cnt0 = 0, p = 0,a = 0; uint num = 0, num0; uchar i, t; uchar MatrixKeyScan(void); void delay(uint); void Display(void); void Timer0Init(void); void Led(void); void Beep(void); int main() { Timer0Init(); while (1) { flag = MatrixKeyScan(); if (flag == 1) { tab[++cnt] = dig[MKValue]; num = num*10 + dig[MKValue]; } if (flag == 2)//¼Æʱ¿ªÊ¼~ { TR0 = 1; } if (flag == 3) { TR0 = ~TR0; } if (flag == 4) { num = 0; for (i = 1; i < cnt; i ++) tab[i] = 0; cnt = 0; } flag = 0; Display(); } } /*·äÃùÆ÷Ïì*/ void Beep(void) { uchar i = 100; while (i--) { beep = ~beep; delay(5); } delay(5); } /*Á÷Ë®µÆº¯Êý*/ void Led(void) { uchar i = 16; P2 = 0xfe; while (i--) { P2 = _crol_(P2,1); delay(50); } P2 = 0xff; } /*¼ÆʱÆ÷0³õʼ»¯º¯Êý*/ void Timer0Init(void) { TMOD |= 0x01; TH0 = (65535 - 45872) / 256;//50ms TL0 = (65535 - 35872) % 256; ET0 = 1; EA = 1; } void Timer0() interrupt 1 { TH0 = (65535 - 45872) / 256; TL0 = (65535 - 35872) % 256; a++; if (a == 20) { a = 0; p = 0; cnt0 = 0;//³õʼ»¯£¡£¡£¡ num --; num0 = num; while (num0)//´¦ÀíÍËλ ×¢Òânum == 0 ÌØÊâÇé¿ö { num0 /= 10; cnt0++; } num0 = num; if ((cnt > cnt0) && (num != 0)) cnt--; for (i = cnt; i >= 1; i--) { tab[i] = num0 % 10; num0 /= 10; } if (num == 0)//ʱ¼äµ½ { Led(); Beep();Beep();Beep();Beep(); TR0 = 0; } } } void Display(void) { char pos = cnt-1; pos = cnt - 1; while (pos >= 0 && num != 0) { LSC = pos / 4; LSB = (pos%4) / 2; LSA = (pos%2); P0 = table[tab[cnt - pos]]; pos --; delay(2); P0 = 0x00; } if (num == 0)//²»Ó°ÏìÁ÷Ë®µÆ { LSA = LSB = LSC = 1; P0 = 0x00; } } uchar MatrixKeyscan() { uchar a = 0; P1 = 0x0f; if (P1 != 0x0f) { delay(10); if (P1 != 0x0f) { P1 = 0x0f; switch (P1) { case (0x07): MKValue = 0; break; case (0x0b): MKValue = 1; break; case (0x0d): MKValue = 2; break; case (0x0e): MKValue = 3; break; } P1 = 0xf0; switch (P1) { case (0xb0): MKValue += 4; break; case (0xd0): MKValue += 8; break; case (0xe0): MKValue += 12; break; } } while ((a < 50)&&(P1 != 0xf0)) { delay(10); a++; } if ((MKValue==0)||(MKValue==1)||(MKValue==2)||(MKValue==4)||(MKValue==5)|| (MKValue==6)||(MKValue==8)||(MKValue==9)||(MKValue==10)||(MKValue==13))//°´ÏÂÊý×Ö¼ü return 1; if (MKValue == 15)//È·¶¨¼ü return 2; if (MKValue == 11)//ÔÝÍ£¼ü return 3; if (MKValue == 7)//¸´Î»¼ü return 4; } return 0;//ÎÞЧ¼ü } void delay(uint x) { uint i, j; for (i = x; i > 0; i--) for (j = 100; j > 0; j--); }
/* S1~S3 S7~S7 S9~S11 S14Êý×Ö¼ü S4ÇåÁã S12Í˸ñ S8 ±£´æ S13 ÏÔʾÉϴα£´æÊý¾Ý S16 È·ÈÏ */ #include <reg52.h> #include <intrins.h> #include <i2c.h> #define uchar unsigned char #define uint unsigned int sbit LSA = P2^2; sbit LSB = P2^3; sbit LSC = P2^4; sbit beep = P1^5; uchar code table[] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f }; uchar code dig[] = { 7,8,9,0,4,5,6,0,1,2,3,0,0,0,0,0 }; uchar code SongTone[]= { 212,212,106,126,159,169,190,119,119,126,159,142,159,0 }; uchar code SongLong[]= { 9,3,12,12,12,12,12,9,3,12,12,12,24,0 }; uchar tab[20]; uchar Password[20]; uchar MKValue, flag = 0, cnt = 0, cnt0 = 0, cnt1 = 0, Useflag = 0; uint num = 0; uchar i, t,WrongTime; uchar MatrixKeyScan(void); void delay(uint); void Display(void); void Timer0Init(void); void Led(void); void Beep(void); void Use(void); uint Check(void); void EnterPassword(void); void UsartInit(void); void Sing(void); int main() { UsartInit(); Timer0Init(); while (1) { flag = MatrixKeyScan(); if (flag == 1) { tab[++cnt] = dig[MKValue]; num = num*10 + dig[MKValue]; } if (flag == 2)//½øÈëʹÓÃģʽ { Use(); } if (flag == 3)//Í˸ñ { cnt --; } if (flag == 4)//Çå³ý { num = 0; for (i = 1; i <= cnt; i ++) tab[i] = 0; cnt = 0; } if (flag == 5)//±£´æµ±Ç°ÏÔʾµÄÃÜÂë { At24c02Write(20,cnt); delay(20); for (i = 1; i <= cnt; i++) { At24c02Write(i,tab[i]); delay(20); } } if (flag == 6)//ÏÔʾÉϴα£´æµÄÃÜÂë { cnt = At24c02Read(20); delay(20); for (i = 1; i <= cnt; i++) { tab[i] = At24c02Read(i); delay(20); } } flag = 0; Display(); } } /******************************************************************************* * º¯ÊýÃû :UsartInit() * º¯Êý¹¦ÄÜ :ÉèÖô®¿Ú * ÊäÈë £ºÎÞ * ·µ»Ø £ºÎÞ *******************************************************************************/ void UsartInit(void) { SCON = 0x50; // ÉèÖÃΪ¹¤×÷·½Ê½1 TMOD = 0x20; //ÉèÖö¨Ê±Æ÷1¹¤×÷·½Ê½2 PCON = 0x80; //²¨ÌØÂʼӱ¶ TH1 = 0xF3; //²¨ÌØÂÊ4800 TL1 = 0xF3; ES = 1; //´®¿ÚÖжÏÔÊÐí EA = 1; //¿ª×ÜÖÐ¶Ï TR1 = 1; //¿ª¶¨Ê±Æ÷ÖÐ¶Ï } /******************************************************************************* * º¯ÊýÃû :Usart() * º¯Êý¹¦ÄÜ :´®¿ÚÖжϺ¯Êý * ÊäÈë £ºÎÞ * ·µ»Ø £ºÎÞ *******************************************************************************/ void Usart() interrupt 4 { uchar receiveData; //receivedata 8λ 0~255 receiveData = SBUF; //½ÓÊÕÊý¾Ý tab[++cnt] = receiveData-48; //ASCIIÂëACSIIÂëASCIIÂë num = num*10 + (receiveData-48); RI = 0; //Çå³ý½ÓÊÕÖжϱêÖ¾ } /******************************************************************************* * º¯ÊýÃû :Use() * º¯Êý¹¦ÄÜ :ʹÓÃģʽ * ÊäÈë £ºÎÞ * ·µ»Ø £ºÎÞ *******************************************************************************/ void Use(void) { Useflag = 1; WrongTime = 0; while(1) { cnt1 = 0; EnterPassword(); if (Check()) { Led(); Sing(); Use(); } else { WrongTime++; if (WrongTime >= 3) { Beep(); Use(); } } } Useflag = 0; } /******************************************************************************* * º¯ÊýÃû :Check() * º¯Êý¹¦ÄÜ :¼ì²éÃÜÂëÕýÎó * ÊäÈë £ºÎÞ * ·µ»Ø £º1/0 *******************************************************************************/ uint Check(void) { uint i; if (cnt != cnt1) return 0; for (i = 0; i < cnt; i++) if (Password[i] != tab[i]) return 0; return 1; } /******************************************************************************* * º¯ÊýÃû :EnterPassword() * º¯Êý¹¦ÄÜ :ÊäÈëÒ»×éÃÜÂë * ÊäÈë £ºÎÞ * ·µ»Ø £ºÎÞ *******************************************************************************/ void EnterPassword(void) { char pos; while(1) { if(MatrixKeyScan()) { if (MKValue == 15)//È·ÈÏ return; if (MKValue == 11)//Í˸ñ cnt1 --; else { Password[++cnt1] = dig[MKValue]; } } Display(); } } /******************************************************************************* * º¯ÊýÃû :Beep() * º¯Êý¹¦ÄÜ :·äÃùÆ÷±¨¾¯ * ÊäÈë £ºÎÞ * ·µ»Ø £ºÎÞ *******************************************************************************/ void Beep(void) { uchar i = 100; while (i--) { beep = ~beep; delay(5); } delay(5); } /******************************************************************************* * º¯ÊýÃû :Sing() * º¯Êý¹¦ÄÜ :·äÃùÆ÷³ª¸è * ÊäÈë £ºÎÞ * ·µ»Ø £ºÎÞ *******************************************************************************/ void Sing() { uint i=0,j,k; while(SongLong[i]!=0||SongTone[i]!=0) { for(j=0;j<SongLong[i]*20;j++) { beep=~beep; for(k=0;k<SongTone[i]/5;k++); } delay(50); i++; } } /******************************************************************************* * º¯ÊýÃû :Led() * º¯Êý¹¦ÄÜ :Á÷Ë®µÆº¯Êý * ÊäÈë £ºÎÞ * ·µ»Ø £ºÎÞ *******************************************************************************/ void Led(void) { uchar i = 16; P2 = 0xfe; while (i--) { P2 = _crol_(P2,1); delay(50); } P2 = 0xff; } /******************************************************************************* * º¯ÊýÃû :Timer0Init() * º¯Êý¹¦ÄÜ :¼ÆʱÆ÷0³õʼ»¯º¯Êý * ÊäÈë £ºÎÞ * ·µ»Ø £ºÎÞ *******************************************************************************/ void Timer0Init(void) { TMOD |= 0x01; TH0 = (65535 - 45872) / 256;//50ms TL0 = (65535 - 35872) % 256; ET0 = 1; EA = 1; } /******************************************************************************* * º¯ÊýÃû :Timer0() * º¯Êý¹¦ÄÜ :¼ÆʱÆ÷0¹¦Äܺ¯Êý * ÊäÈë £ºÎÞ * ·µ»Ø £ºÎÞ *******************************************************************************/ void Timer0() interrupt 1 { } /******************************************************************************* * º¯ÊýÃû :Display() * º¯Êý¹¦ÄÜ :ÊýÂë¹ÜÏÔʾÊý×Ö * ÊäÈë £ºÎÞ * ·µ»Ø £ºÎÞ *******************************************************************************/ void Display(void) { char pos = cnt-1; if (Useflag == 0) { if (pos >= 8) pos = 7; while (pos >= 0) { LSC = pos / 4; LSB = (pos%4) / 2; LSA = (pos%2); P0 = table[tab[cnt - pos]]; pos --; delay(2); P0 = 0x00; } } else { pos = cnt1-1; while (pos >= 0) { LSC = pos / 4; LSB = (pos%4) / 2; LSA = (pos%2); P0 = 0x40; pos --; delay(2); P0 = 0x00; } } } /******************************************************************************* * º¯ÊýÃû :MatrixKeyscan() * º¯Êý¹¦ÄÜ :ɨÃè¼üÅÌ * ÊäÈë :ÎÞ * ·µ»Ø :°´¼üÀàÐÍ *******************************************************************************/ uchar MatrixKeyscan() { uchar a = 0; P1 = 0x0f; if (P1 != 0x0f) { delay(10); if (P1 != 0x0f) { P1 = 0x0f; switch (P1) { case (0x07): MKValue = 0; break; case (0x0b): MKValue = 1; break; case (0x0d): MKValue = 2; break; case (0x0e): MKValue = 3; break; } P1 = 0xf0; switch (P1) { case (0xb0): MKValue += 4; break; case (0xd0): MKValue += 8; break; case (0xe0): MKValue += 12; break; } } while ((a < 50)&&(P1 != 0xf0)) { delay(10); a++; } if ((MKValue==0)||(MKValue==1)||(MKValue==2)||(MKValue==4)||(MKValue==5)|| (MKValue==6)||(MKValue==8)||(MKValue==9)||(MKValue==10)||(MKValue==13))//°´ÏÂÊý×Ö¼ü return 1; if (MKValue == 15)//È·¶¨¼ü return 2; if (MKValue == 11)//Í˸ñ¼ü return 3; if (MKValue == 3)//¸´Î»¼ü return 4; if (MKValue == 7)//±£´æÃÜÂë return 5; if (MKValue == 12)//ÏÔʾÉϴα£´æµÄÃÜÂë return 6; } return 0;//ÎÞЧ¼ü } /******************************************************************************* * º¯ÊýÃû :Delay() * º¯Êý¹¦ÄÜ :ÑÓʱxms *******************************************************************************/ void delay(uint x) { uint i, j; for (i = x; i > 0; i--) for (j = 100; j > 0; j--); }
#include <i2c.h> #define uchar unsigned char #define uint unsigned int /******************************************************************************* * 函数名 :Delay10us() * 函数功能 :延时10us *******************************************************************************/ void Delay10us(void) { unsigned char a, b; for (b = 1; b > 0; b--) for (a = 2; a > 0; a--); } /******************************************************************************* * 函数名 :I2cStart() * 函数功能 :起始信号,在SCL时钟信号在高电平期间SDA信号产生一个下降沿 * 输入 :无 * 输出 :无 * 备注 :起始之后SDA和SCL都为0 *******************************************************************************/ void I2cStart() { SDA = 1; Delay10us(); SCL = 1; Delay10us(); SDA = 0; Delay10us(); SCL = 0; Delay10us(); } /******************************************************************************* * 函数名 :I2cStop() * 函数功能 :终止信号,在SCL时钟信号在高电平期间SDA信号产生一个上升沿 * 输入 :无 * 输出 :无 * 备注 :结束之后SDA和SCL都为1,表示总线空闲 *******************************************************************************/ void I2cStop() { SDA = 0; Delay10us(); SCL = 1; Delay10us(); SDA = 1; Delay10us(); } /******************************************************************************* * 函数名 :I2cSendByte() * 函数功能 :通过IIC发送一个字节。在SCL时钟信号高电平期间,保持发送信号SDA稳定 * 输入 :待发送字节num * 输出 :0或1 表示发送成功或失败 * 备注 :发送完一个字节SCL = 0,SDA = 1 *******************************************************************************/ uchar I2cSendByte(uchar dat) { uchar a = 0, b = 0; for (a = 0; a < 8; a++)//先取最高位,再左移一位令次高位成为最高位 { SDA = dat>>7;//起始信号后SCL = 0,此时可以直接改变SDA的值 dat <<= 1; Delay10us(); SCL = 1;//时钟信号高电平,SDA稳定 Delay10us(); SCL = 0;//时钟信号低电平,等待下一位读入 Delay10us(); } SDA = 1; Delay10us(); SCL = 1;//SDA SCL都拉高 等待应答 while(SDA)//如果应答 SDA为低电平 跳出循环 { b++; if (b > 200)//如果超过2000us没有应答则接收结束 { SCL = 0; Delay10us(); return 0; } } SCL = 0;//SCL置0以发送/读取下一字节 Delay10us(); return 1; } /******************************************************************************* * 函数名 :I2cReadByte() * 函数功能 :通过IIC读取一个字节。在SCL时钟信号高电平期间,保持发送信号SDA稳定 * 输入 :无 * 输出 :读取字节dat * 备注 :接收完一个字节SCL = 0,SDA = 1 *******************************************************************************/ uchar I2cReadByte() { uchar a = 0, dat = 0; SDA = 1;//起始或发送字节后SCL都为0 Delay10us(); for (a = 0; a < 8; a++)//接收8个字节 { SCL = 1;//SDA稳定 Delay10us(); dat <<= 1; dat |= SDA; Delay10us(); SCL = 0; Delay10us(); } return dat; } /******************************************************************************* * 函数名 :At24c02Write(uchar,uchar) * 函数功能 :向24c02中写入一个数据 * 输入 :要写入的内存地址 写入的数据 * 输出 :无 *******************************************************************************/ void At24c02Write(uchar addr, uchar dat) { I2cStart(); //起始信号 I2cSendByte(0xa0);//发送写器件地址 I2cSendByte(addr);//发送写入内存地址 I2cSendByte(dat);//发送数据 I2cStop();//终止信号 } /******************************************************************************* * 函数名 :At24c02Read(uchar) * 函数功能 :读取24c02一个地址的一个数据 * 输入 :读取内存地址 * 输出 :读取字节num *******************************************************************************/ uchar At24c02Read(uchar addr) { uchar num; I2cStart();//起始信号 I2cSendByte(0xa0);//发送写器件地址 I2cSendByte(addr);//发送写入内存地址 I2cStart();//起始信号 I2cSendByte(0xa1);//发送读器件地址 num = I2cReadByte();//读取数据 I2cStop(); return num; }
#ifndef _I2C_H #define _I2C_H #include <reg52.h> sbit SCL = P2^1; sbit SDA = P2^0; void At24c02Write(unsigned char, unsigned char); unsigned char At24c02Read(unsigned char); #endif
#include <reg52.h> #include <ds1302.h> #define uint unsigned int #define uchar unsigned char sbit LSA = P2^2; sbit LSB = P2^3; sbit LSC = P2^4; uchar code table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; uchar DisplayDataA[8]; uchar DisplayDataB[8]; void DataPros(void); void DigDisplay(uchar); void delay(uchar); int main() { uchar i; Ds1302Init(); while(1) { i = 10000000; while(i --) { DataPros(); DigDisplay(1); } i = 5000000; while(i --) { DataPros(); DigDisplay(2); } } } void DataPros() { Ds1302ReadTime(); DisplayDataA[0] = table[TIME[2] / 16]; DisplayDataA[1] = table[TIME[2] % 16]; DisplayDataA[2] = 0x40; DisplayDataA[3] = table[TIME[1] / 16]; DisplayDataA[4] = table[TIME[1] % 16]; DisplayDataA[5] = 0x40; DisplayDataA[6] = table[TIME[0] / 16]; DisplayDataA[7] = table[TIME[0] % 16]; DisplayDataB[0] = table[TIME[6] / 16]; DisplayDataB[1] = table[TIME[6] % 16]; DisplayDataB[2] = 0x40; DisplayDataB[3] = table[TIME[4] / 16]; DisplayDataB[4] = table[TIME[4] % 16]; DisplayDataB[5] = 0x40; DisplayDataB[6] = table[TIME[3] / 16]; DisplayDataB[7] = table[TIME[3] % 16]; } void DigDisplay(uchar x) { uchar i; for (i = 0; i < 8; i ++) { LSC = i / 4; LSB = i%4 / 2; LSA = i%2; if (x == 1) P0 = DisplayDataA[7-i]; else P0 = DisplayDataB[7-i]; delay(1); P0 = 0x00; } } void delay(uchar x) { uint i, j; for (i = 100; i > 0; i--) for (j = x; j > 0; j--); }
#include <ds1302.h> #define uchar unsigned char #define uint unsigned int uchar code READ_ADDR[7] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; uchar code WRITE_ADDR[7] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; uchar TIME[7] = {0x00,0x35,0x21,0x20,0x02,0x03,0x19}; void Ds1302Write(uchar addr, uchar dat) { uchar i; RST = 0; _nop_(); SCLK = 0; _nop_(); RST = 1; _nop_(); for (i = 0; i < 8; i++) { DSIO = addr & 0x01; addr >>= 1; SCLK = 1; _nop_(); SCLK = 0; _nop_(); } for (i = 0; i < 8; i++) { DSIO = dat & 0x01; dat >>= 1; SCLK = 1; _nop_(); SCLK = 0; _nop_(); } RST = 0; _nop_(); } uchar Ds1302Read(uchar addr) { uchar i, dat = 0, dat1; RST = 0; _nop_(); SCLK = 0; _nop_(); RST = 1; _nop_(); for (i = 0; i < 8; i++) { DSIO = addr & 0x01; addr >>= 1; SCLK = 1; _nop_(); SCLK = 0; _nop_(); } _nop_(); for (i = 0; i < 8; i++) { dat1 = DSIO; dat = (dat >> 1)|(dat1 << 7); SCLK = 1; _nop_(); SCLK = 0; _nop_(); } RST = 0; _nop_(); SCLK = 1;//? _nop_(); DSIO = 0; _nop_(); DSIO = 1; _nop_(); return dat; } void Ds1302Init() { uchar i; Ds1302Write(0x8e,0x00); for (i = 0; i < 7; i++) { Ds1302Write(WRITE_ADDR[i], TIME[i]); } Ds1302Write(0x8e,0x80); } void Ds1302ReadTime() { uchar i; for (i = 0; i < 7; i++) { TIME[i] = Ds1302Read(READ_ADDR[i]); } }
#ifndef __DS1302_H_ #define __DS1302_H_ #include <reg52.h> #include <intrins.h> #ifndef uchar #define uchar unsigned char #endif #ifndef uint #define uint unsigned int #endif sbit DSIO = P3^4; sbit SCLK = P3^6; sbit RST = P3^5; void Ds1302Write(uchar addr, uchar dat); uchar Ds1302Read(uchar addr); void Ds1302Init(); void Ds1302ReadTime(); extern uchar TIME[7]; #endif
以上是关于略略略的主要内容,如果未能解决你的问题,请参考以下文章