⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.c

📁 利用热释红外传感器、12C887和PIC单片机制作的智能电子时钟
💻 C
字号:
#include <pic.h>
#include "lcd.h";
#include "delay.h";
//#include "12c887.h";
/////////////   SUNROUTINE DECLARE   /////////////
extern  _12c887_write(unsigned char ADDRESS,unsigned char DATA);
extern  _12c887_read(unsigned char ADDRESS);
void mcu_init(void);
void ReadKey(void);
void GetTimeDate(void);
//////////// SUBSTITUTION DEFINITIONS  ///////////
        // input substitution
#define Human_in	 RB2
#define Menu_in      RB3
#define Up_in        RB4
#define Down_in      RB5
        // output substitution
#define AlarmOn		 RB2=1
#define AlarmOff	 RB2=0
#define LightOn		 RB1=0
#define LightOff	 RB1=1


////////////   VARIABLE DEFINITIONS   ////////////
unsigned static char second,minute,minute_alarm,hour,hour_alarm,weekday,monthday,month,year;
extern SECOND,SECOND_ALARM,MINUTE,MINUTE_ALARM,HOUR,HOUR_ALARM,DayOfWeek,DayOfMonth,MONTH,YEAR,CENTURY,REGA,REGB,REGC,REGD;
unsigned static char TimeShow[11];
unsigned static char DateShow[11];
unsigned static char AlarmShow[5];
unsigned static char DelayTimer;
unsigned static char PWMTimer;
static bit LightDelay;
        // input,used for signal switch or operation key  read
static unsigned char   Menu_Value;
static unsigned char   Up_Value;
static unsigned char   Down_Value;
static unsigned char   Human_Value;
static bit             Menu;
static bit             Menu_USED;
static bit             Up;
static bit             Up_USED;
static bit             Down;
static bit             Down_USED;
static bit             Human;
static bit             Human_USED;
		// system state word
static unsigned char SSW;
static bit             AlarmAllow;
/////////////////// SUBROUTINE ///////////////////
void mcu_init()
{
    // initialize I/O port
    ADCON1=0x07;
    TRISA=0x00;         // PORTA as DO
    RBPU=0x00;          // enable portB weak up
    TRISB=0b11111001;   // PORTB as DI,expect RB0,RB2
    TRISC=0x00;         // PORTC as D0
    TRISD=0x00;         // PORTD as D0
    PORTA=0x00;         // clear port A,B,C
    PORTB=0b00000011;
    PORTC=0x00;
    PORTD=0x00;
    // initialize timer1
    T1CON=0x01;         // Prescale value is 1:1; internal clock,start TMR1
    TMR1IE=1;           // enable TMR1 interrupt
    // initialize general interrupt
    GIE=1;              // enables all unmasked interrupts
    PEIE=1;             // enables all unmasked peripheral interrupts
}

void interrupt TMR1_INT(void)
        // TMR1 or INT interrupt
{

    if  (TMR1IF&&TMR1IE)
        {
                // TMR1 interrupt
            TMR1IF=0;
            TMR1H=216;                                  // 10ms per TMR1 interrupt
            TMR1ON=1;                                   // TMR1 start to work
            ReadKey();                                  // read the key of operation
            DelayTimer++;
        }
}


void ReadKey(void)
        // read keys
{
    Menu_Value<<=1;     // read menu key
    if (Menu_in==0) Menu_Value=Menu_Value|0x01;
    Menu_Value=Menu_Value&0x07;
    if (Menu_Value==0x07) Menu=1;
        else if (Menu_Value==0x00) Menu=0,Menu_USED=0;

    Up_Value<<=1;     // read up key
    if (Up_in==0) Up_Value=Up_Value|0x01;
    Up_Value=Up_Value&0x07;
    if (Up_Value==0x07) Up=1;
        else if (Up_Value==0x00) Up=0,Up_USED=0;

    Down_Value<<=1;     // read down key
    if (Down_in==0) Down_Value=Down_Value|0x01;
    Down_Value=Down_Value&0x07;
    if (Down_Value==0x07) Down=1;
        else if (Down_Value==0x00) Down=0,Down_USED=0;

    Human_Value<<=1;     // read human sensor
    if (Human_in==1) Human_Value=Human_Value|0x01;
    Human_Value=Human_Value&0x07;
    if (Human_Value==0x07) Human=1;
        else if (Human_Value==0x00) Human=0,Human_USED=0;
}

void GetTimeDate(void)
{
    second=_12c887_read(0x00);
    minute=_12c887_read(0x02);
	minute_alarm=_12c887_read(0x03);
    hour=_12c887_read(0x04);
	hour_alarm=_12c887_read(0x05);
    weekday=_12c887_read(0x06);
    monthday=_12c887_read(0x07);
    month=_12c887_read(0x08);
    year=_12c887_read(0x09);
}

void Change(void)
{
    TimeShow[0]=((hour>>4)+0x30);
    TimeShow[1]=((hour&0x0f)+0x30);
    TimeShow[2]=':';
    TimeShow[3]=' ';
    TimeShow[4]=((minute>>4)+0x30);
    TimeShow[5]=((minute&0x0f)+0x30);
    TimeShow[6]=':';
    TimeShow[7]=' ';
    TimeShow[8]=((second>>4)+0x30);
    TimeShow[9]=((second&0x0f)+0x30);
    TimeShow[10]=' ';

    DateShow[0]=((year>>4)+0x30);
    DateShow[1]=((year&0x0f)+0x30);
    DateShow[2]='.';
    DateShow[3]=((month>>4)+0x30);
    DateShow[4]=((month&0x0f)+0x30);
    DateShow[5]='.';
    DateShow[6]=((monthday>>4)+0x30);
    DateShow[7]=((monthday&0x0f)+0x30);
    DateShow[8]=',';
    DateShow[9]=((weekday&0x0f)+0x30);
    DateShow[10]=' ';

	AlarmShow[0]=((hour_alarm>>4)+0x30);
	AlarmShow[1]=((hour_alarm&0x0f)+0x30);
	AlarmShow[2]=':';
	AlarmShow[3]=((minute_alarm>>4)+0x30);
	AlarmShow[4]=((minute_alarm&0x0f)+0x30);
}

void task0(void)
	// idle state
{
    lcd_goto(0x80);
    lcd_puts("TIME ");
    lcd_goto(0xc0);
    lcd_puts("DATE ");
	GetTimeDate();
    Change();
    if (AlarmAllow==1) TimeShow[10]='~';
	else TimeShow[10]=' ';
    lcd_goto(0x85);
    lcd_puts(TimeShow);
    lcd_goto(0xc5);
    lcd_puts(DateShow);

	if (Up==1&&Up_USED==0&&Down==1&&Down_USED==0) 
		{
			Up_USED=1;
			Down_USED=1;
			AlarmAllow=!AlarmAllow;
			LightOn;
			if (AlarmAllow==1)
				{
				 lcd_goto(0x80);
				 lcd_puts("Alarm Time:");
				 lcd_goto(0x8b);
				 lcd_puts(AlarmShow);
				 lcd_goto(0xc0);
				 lcd_puts("   Alarm On    ");
				 asm("nop");
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 lcd_goto(0xc0);
				 lcd_puts("  Fighting !!!  ");
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 LightOff;}
			else 
				{
				 lcd_goto(0x80);
				 lcd_puts("Alarm Time:");
				 lcd_goto(0x8b);
				 lcd_puts(AlarmShow);
				 lcd_goto(0xc0);
				 lcd_puts("   Alarm Off   ");
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 lcd_goto(0xc0);
				 lcd_puts(" Have Nice Day !");
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 DelayMs(250);
				 LightOff;}
		}
	else {;}

	if (Menu==1&&Menu_USED==0&&LightDelay==0) {Menu_USED=1;LightDelay=1;LightOn;DelayTimer=0;}
	else {;}
	if (LightDelay==1)
		{ LightOn;
		  DelayMs(10);
		  if (DelayTimer>250) 	{LightDelay=0;LightOff;}
		  else if (Menu==1&&Menu_USED==0&&LightDelay==1) {Menu_USED=1;SSW=1;}
			  }
	else {;}
}


void task1(void)
	// time rectify
{
	LightOn;
	lcd_goto(0x80);
    lcd_puts("  rectify menu  ");
    lcd_goto(0xc0);
    lcd_puts("Time:UP,Alarm:DN");

	if (Up==1&&Up_USED==0) {Up_USED=1;SSW=2;}
	else if (Down==1&&Down_USED==0) {Down_USED=1;SSW=3;}
	else if (Menu==1&&Menu_USED==0) {Menu_USED=1;LightOff;SSW=0;}
}




void task2(void)
	// time rectify
{
	LightOn;
	lcd_goto(0x80);
    lcd_puts("  time rectify  ");
	lcd_goto(0xc0);
    lcd_puts("TIME ");
	GetTimeDate();
	Change();
    lcd_goto(0xc5);
    lcd_puts(TimeShow);

	if (Up==1&&Up_USED==0)
		{
			Up_USED=1;
			if (minute==0x59)
				{
				_12c887_write(0x02,0x00);
				}
			else
				{
				minute=_12c887_read(0x02);
				minute++;
				_12c887_write(0x02,minute);
				}
		}
	else if (Down==1&&Down_USED==0)
		{
			Down_USED=1;
			if (minute==0x00)
				{
				_12c887_write(0x02,0x59);
				}
			else
				{
				minute=_12c887_read(0x02);
				minute--;
				_12c887_write(0x02,minute);
				}
		}

	if (Menu==1&&Menu_USED==0) {Menu_USED=1;LightOff;SSW=0;}
	else {;}
}

void task3(void)
	// alarm rectify
{
	LightOn;
	lcd_goto(0x80);
    lcd_puts(" alarm rectify");
	lcd_goto(0xc0);
    lcd_puts("Alarm Time  ");
	GetTimeDate();
	Change();
	lcd_goto(0xcb);
	lcd_puts(AlarmShow);

	if (Up==1&&Up_USED==0)
		{
			Up_USED=1;
			if (hour_alarm==0x23)
				{
				_12c887_write(0x05,0x00);
				}
			else
				{
				hour_alarm=_12c887_read(0x05);
				hour_alarm++;
				_12c887_write(0x05,hour_alarm);
				}
		}
	else if (Down==1&&Down_USED==0)
		{
			Down_USED=1;
			if (minute_alarm==0x59)
				{
				_12c887_write(0x03,0x00);
				}
			else
				{
				minute_alarm=_12c887_read(0x03);
				minute_alarm++;
				_12c887_write(0x03,minute_alarm);
				}
		}

	if (Menu==1&&Menu_USED==0) {Menu_USED=1;LightOff;SSW=0;}
	else {;}
}


main()
{
    mcu_init();                     // initialize MCU
    AlarmAllow=1;
	AlarmOn;
	LightOn;
	DelayMs(100);
	AlarmOff;
	LightOff;
    //_12c887_init();               	// initialize RTC 12c887
    lcd_init();                     // initialize LCD
    lcd_puts("    WELCOME!    ");
    lcd_goto(0xc0);
    lcd_puts("     G.H.Wu    ");
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	while(1)
	{
		GetTimeDate();
		if (AlarmAllow==1&&hour==hour_alarm&&minute==minute_alarm&&(second==1||second==3||second==5||second==7||second==9)) {LightOn;AlarmOn;}
		else
			{
				if (hour<=0x22&&hour>=0x07)
				{
		    		if (minute==0x29&&second==0x59) {DelayMs(250);DelayMs(250);DelayMs(250);DelayMs(150);LightOn;AlarmOn;DelayMs(100);LightOff;AlarmOff;}
    				else if (minute==0x00&&second==0x00) {LightOn;AlarmOn;}
	    			else {LightOff;AlarmOff;}
	     		}
	     		else {LightOff;AlarmOff;}
	     	}

		switch(SSW)
        {
        	case 0: task0();break;
        	case 1: task1();break;
        	case 2: task2();break;
        	case 3: task3();break;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -