时钟七:在LCD1602上显示时钟,并且用按键修改时间

主函数文件:

#include 
#include "./delay/delay.h"

sbit RS = P2^4;
sbit RW = P2^5;
sbit E  = P2^6;
sbit beem = P3^2;

#define LCDPORT P0
#define KEYPORT P1
#define LCD_WRITE_DATA 1
#define LCD_WRITE_COM  0

unsigned char key;
unsigned char time = 0;
unsigned char shi = 0;
unsigned char fen = 0;
unsigned char miao = 0;
unsigned char Dshi = 1;
unsigned char Dfen = 0;
unsigned char Dmiao = 30;

unsigned char code SONG_TONE[]={239,239,212,189,159,159,189,159,159,142,126,120,120,159,120,120,142,159,189,142,159,189,239,212,189,159,142,159,189,212,239};
unsigned char code SONG_LONG[]={6,6,9,3,6,6,12,6,6,6,6,6,6,12,6,6,9,3,6,6,9,3,6,3,3,6,3,3,6,6,9};

unsigned char key_scan(void)
{
	  unsigned char keyvalue;
	  KEYPORT = 0xf0;
	  if(0xf0 != KEYPORT)
		{
			  delay_ms(10);
			  if(0xf0 != KEYPORT)
				{
					  KEYPORT = 0xfe;
					  if(0xfe != KEYPORT)
						{
							  keyvalue = KEYPORT;
							  keyvalue &= 0xf0;
							  keyvalue += 0x0e;
							  while(0xfe != KEYPORT);
						    delay_ms(10);
								while(0xfe != KEYPORT);
							  return keyvalue;
						}
						KEYPORT = 0xfd;
					  if(0xfd != KEYPORT)
						{
							  keyvalue = KEYPORT;
							  keyvalue &= 0xf0;
							  keyvalue += 0x0d;
							  while(0xfd != KEYPORT);
						    delay_ms(10);
								while(0xfd != KEYPORT);
							  return keyvalue;
						}
				}
		}
		return 0xff;
}

void PlayMusic(void)
{
		unsigned char q=0;
	
	  unsigned char m;
	  unsigned char k;
		while(SONG_LONG[q]!=0||SONG_TONE[q]!=0)
		{
				for(m=0;m



延时函数文件:

void delay_us(unsigned char t)
{
    while(--t);
}

void delay_ms(unsigned char t)
{
    while(t--)
		{
        delay_us(245);
			  delay_us(245);
    }
}

void delay_s(unsigned char t)
{
    while(t--)
    {
		    delay_ms(200);	
			  delay_ms(200);
			  delay_ms(200);
			  delay_ms(200);
			  delay_ms(200);
    }
}



你可能感兴趣的:(单片机)