Sei sulla pagina 1di 15

Implementation of alarm clock controller using embedded microcontroller Trainer

Table of contents

1. Introduction 2. Technical Specifications 3. Front Panel Controls 4. Operating Instructions

INTRODUCTION

This alarm clock is implemented using PIC Microcontroller 16F877A of Microchip. It has the function of showing Time and alarm set time in 16X2 LCD Display. It uses DS1307 Real time clock timer and interfaced to pic microcontroller. we can set time in HH: MM:SS format and set alarm in HH:MM format.For alarm sound buzzer is used. Alarm clock has five keys to edit and set time and alarm.

Technical specification;
1. Based on PIC Microcontroller 16F877A. 2. Real time clock using DS1307 3. For display 16x2 LCD display is used. 4. For alarm sound Buzzer is connected to port b0. 5. Five keys provided to edit time and alarm set. 6. Build in regulated 5v powr supply with ON/OFF switch. 7.Build in ICSP ( In-circuit serial programming) terminals provided.

Front Panel control


1.On/Off switch 2. LCD Display 3. Five keys. SET, INC, DEC, OK ALARM ON/OFF 4. BUZZER with led. 5. On board PIC 16F877A 6. Real time clock DS1307.

Circuit Description
1. 16X2 LCD is connected on port D. 2. Five key switches connected on port C, RC3, RC4, RC5,RC6 and RC7 with pull down sistor. 3. Buzzer on port B7 with driving transistor and LED. 4. IC DS1307 is interfaced to PIC port RB1, RB2 and RB3. Other circuit details refer circuit diagram shown. re-

OPERATING INSTRUCTION
1. Switch on the power supply 2. The clock will shows default time 00:00:00. on first line of LCD.and clock run. 3. The second line shows Alarm set time and alarm mode is ONor OFF. 4. For setting or editing TIME, press SET key, it ask for TIME SET, if you want to set time, press OK key, 5. Now TIME 00:00 is shown and hour digits will blink, now you can increase or decrease the hour by pressing INC or DEC keys. and for minuate setting press SET key, then MM format will blink and you can increase or decrease the minuate by pressing INC or DEC keys. Once time is edited, then press OK key. Now the clock will run from this new time. 6. Or if you want set alarm time, then press SET key two times, it ask for alarm time set, press OK key. 7. Now ALARM TIME 00:00 is shown and hour digits will blink, now you can increase or decrease the alarm hour by pressing INC or DEC keys. and for minuate setting press SET key, then MM format will blink and you can increase or decrease the minuate by pressing INC or DEC keys. Once time is edited, then press OK key. 8. Now Clock time shown in first line of LCD and alarm time on second line of LCD display. 9. And ALARM mode which is ONor OFF shown in the second line. It can be set ON or OFF by pressing ALARM ON/OFF Key. It will toggle. 10. Once the clock time reaches alarm set time and alarm mode is set in ON, then the buzzer will sound. It can be disabled by pressing alarm ON/OFF key.

software
#include <16F877A.h> #fuses HS,NOWDT,PROTECT,NOBROWNOUT,PUT,NOLVP #use delay(clock=20000000) #define SET_SELECT_SW PIN_C7 #define INC_SW PIN_C3 #define DEC_SW PIN_C4 #define OK_SW PIN_C5 #define ALARM_SW PIN_C6 #define ALARM_OUT PIN_B7 #ifndef RTC_SCLK #define RTC_SCLK PIN_B1 #define RTC_IO PIN_B3 #define RTC_RST PIN_B2 #endif //#define BL_ON PIN_E1 //#define BUZZER PIN_E2 // PCB //#define MOTOR_ON PIN_B7 //#define DRY_RUN_RST_TMR_LED PIN_B6 //#define DRY_RUN PIN_B5 //#define TANK_FULL PIN_B4 //#define SUMP_EMPTY PIN_B3 //#define HIGH_VOL PIN_B2 //#define LOW_VOL PIN_B1 //#define BL_SW PIN_B0 //#define OVER_LOAD PIN_E0

unsigned char alarm_hr=0,alarm_min=0,alarm_status=0; unsigned char hr=0,min=0,sec=0,mode=0; //unsigned char i=0; struct lcd_pin_map { BOOLEAN rs; BOOLEAN rw; BOOLEAN enable; BOOLEAN unused; int data : 4; } lcd; #byte lcd = 0x8 //0xF83 #define set_tris_lcd(x) set_tris_d(x) #define lcd_type 2 #define lcd_line_two 0x40 BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6}; struct lcd_pin_map const LCD_WRITE = {0,0,0,0,0}; struct lcd_pin_map const LCD_READ = {0,0,0,0,15};

BYTE lcd_read_byte() { BYTE low,high; set_tris_lcd(LCD_READ); lcd.rw = 1; delay_cycles(1); lcd.enable = 1; delay_cycles(1); high = lcd.data; lcd.enable = 0; delay_cycles(1); lcd.enable = 1; delay_us(1); low = lcd.data; lcd.enable = 0; set_tris_lcd(LCD_WRITE); return( (high<<4) | low); } void lcd_send_nibble( BYTE n ) { lcd.data = n; delay_cycles(1); lcd.enable = 1; delay_us(2); lcd.enable = 0; } void lcd_send_byte( BYTE address, BYTE n ) { lcd.rs = 0; while ( bit_test(lcd_read_byte(),7) ) ; lcd.rs = address; delay_cycles(1); lcd.rw = 0; delay_cycles(1); lcd.enable = 0; lcd_send_nibble(n >> 4); lcd_send_nibble(n & 0xf); } void lcd_init() { BYTE i; set_tris_lcd(LCD_WRITE); lcd.rs = 0; lcd.rw = 0; lcd.enable = 0; delay_ms(15); for(i=1;i<=3;++i) { lcd_send_nibble(3); delay_ms(5); } lcd_send_nibble(2); for(i=0;i<=3;++i) lcd_send_byte(0,LCD_INIT_STRING[i]); }

ALPHA

Manual for

Alarm clock controller using embedded microcontroller Trainer

( Sr. No:............................)

void lcd_gotoxy( BYTE x, BYTE y) { BYTE address; if(y!=1) address=lcd_line_two; else address=0; address+=x-1; lcd_send_byte(0,0x80|address); } void lcd_putc( char c) { switch (c) { case '\f' : lcd_send_byte(0,1); delay_ms(2); break; case '\1' : lcd_gotoxy(1,1); break; case '\2' : lcd_gotoxy(1,2); break; case '\b' : lcd_send_byte(0,0x10); break; default : lcd_send_byte(1,c); break; } } char lcd_getc( BYTE x, BYTE y) { char value; lcd_gotoxy(x,y); while ( bit_test(lcd_read_byte(),7) ); // wait until busy flag is low lcd.rs=1; value = lcd_read_byte(); lcd.rs=0; return(value); } void write_ds1302_byte(BYTE cmd) { BYTE i; for(i=0;i<=7;++i) { output_bit(RTC_IO, shift_right(&cmd,1,0) ); output_high(RTC_SCLK); output_low(RTC_SCLK); } } void write_ds1302(BYTE cmd, BYTE data) { output_high(RTC_RST); write_ds1302_byte(cmd); write_ds1302_byte(data); output_low(RTC_RST); }

BYTE read_ds1302(BYTE cmd) { BYTE i,data; output_high(RTC_RST); write_ds1302_byte(cmd); for(i=0;i<=7;++i) { shift_right(&data,1,input(RTC_IO)); output_high(RTC_SCLK); delay_us(2); output_low(RTC_SCLK); delay_us(2); } output_low(RTC_RST); return(data); } void rtc_init() { BYTE x; output_low(RTC_RST); delay_us(2); output_low(RTC_SCLK); write_ds1302(0x8e,0); write_ds1302(0x90,0xa6); x=read_ds1302(0x81); if((x & 0x80)!=0) write_ds1302(0x80,0); } int get_bcd(BYTE data) { int nibh; int nibl; nibh=data/10; nibl=data-(nibh*10); return((nibh<<4)|nibl); } int rm_bcd(BYTE data) { int i; i=data; data=(i>>4)*10; data=data+(i<<4>>4); return (data); } void rtc_set_datetime(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min) { write_ds1302(0x86,get_bcd(day)); write_ds1302(0x88,get_bcd(mth)); write_ds1302(0x8c,get_bcd(year)); write_ds1302(0x8a,get_bcd(dow)); write_ds1302(0x84,get_bcd(hr)); write_ds1302(0x82,get_bcd(min)); write_ds1302(0x80,get_bcd(0)); }

void rtc_get_date(BYTE& day, BYTE& mth, BYTE& year, BYTE& dow) { day = rm_bcd(read_ds1302(0x87)); mth = rm_bcd(read_ds1302(0x89)); year = rm_bcd(read_ds1302(0x8d)); dow = rm_bcd(read_ds1302(0x8b)); } void rtc_get_time(BYTE& hr, BYTE& min, BYTE& sec) { hr = rm_bcd(read_ds1302(0x85)); min =rm_bcd( read_ds1302(0x83)); sec =rm_bcd( read_ds1302(0x81)); } void rtc_write_nvr(BYTE address, BYTE data) { write_ds1302(address|0xc0,data); } BYTE rtc_read_nvr(BYTE address) { return(read_ds1302(address|0xc1)); } #define INTS_PER_SECOND 76 // (20000000/(4*256*256)) BYTE seconds=0; BYTE int_count; byte buzzer_flag=0; #int_rtcc void clock_isr() { if(--int_count==0) { int_count=INTS_PER_SECOND; seconds++; if(seconds>1) seconds=0; if(buzzer_flag==1) { if(seconds==0) output_high(ALARM_OUT); else output_low(ALARM_OUT); } }

void main() { set_tris_c(0xff); // ext_int_edge(L_TO_H); // init interrupts // enable_interrupts(INT_EXT); // enable_interrupts(GLOBAL); lcd_init(); int_count=INTS_PER_SECOND; set_timer0(0); setup_counters( RTCC_INTERNAL, RTCC_DIV_256 | RTCC_8_BIT); enable_interrupts(INT_RTCC); enable_interrupts(GLOBAL); rtc_init(); printf(lcd_putc,"\fALARM CLOCK BY\2EMBEDDED SYSTEM "); delay_ms(2000); output_low(ALARM_OUT); while(TRUE) { while(mode==0) { hr = rm_bcd(read_ds1302(0x85)); min =rm_bcd( read_ds1302(0x83)); sec =rm_bcd( read_ds1302(0x81)); printf(lcd_putc,"\fTIME :%02d:%02d:%02d\2ALARM:%02d:%02d",hr,min,sec,alarm_hr,alarm_min); if(alarm_status==0) { printf(lcd_putc," OFF"); output_low(ALARM_OUT); } else { printf(lcd_putc," ON"); } if(input(ALARM_SW)) { if (alarm_status==0) alarm_status=1; else { alarm_status=0; buzzer_flag=0; seconds=0; output_low(ALARM_OUT); } }

if(input(SET_SELECT_SW)) { mode=1; printf(lcd_putc,"\fSET TIME ?"); } if((alarm_hr==hr)&&(alarm_min==min)&&(alarm_status==1)) { buzzer_flag=1; } delay_ms(250); } while(mode==1) { if(input(SET_SELECT_SW)) { mode=4; printf(lcd_putc,"\fSET ALARM ?"); delay_ms(250); } if(input(OK_SW)) { mode=2; printf(lcd_putc,"\fSET TIME :%02d:%02d",hr,min); //lcd_send_byte(0,0x8A); lcd_send_byte(0,0x0D); lcd_send_byte(0,0x8B); delay_ms(250); } } while(mode==2) { if(input(SET_SELECT_SW)) { mode=3; printf(lcd_putc,"\fSET TIME :%02d:%02d",hr,min); //lcd_send_byte(0,0x8D); lcd_send_byte(0,0x0D); lcd_send_byte(0,0x8E); delay_ms(250); } if(input(INC_SW)) { hr++; if(hr==24) hr=0; printf(lcd_putc,"\1SET TIME :%02d:%02d",hr,min); //lcd_send_byte(0,0x8A); lcd_send_byte(0,0x0D); lcd_send_byte(0,0x8B); delay_ms(250); }

if(input(DEC_SW)) { if(hr!=0) hr--; else hr=23; printf(lcd_putc,"\1SET TIME :%02d:%02d",hr,min); //lcd_send_byte(0,0x8A); lcd_send_byte(0,0x0D); lcd_send_byte(0,0x8B); delay_ms(250); } if(input(OK_SW)) { mode=0; lcd_send_byte(0,0x0C); write_ds1302(0x84,get_bcd(hr)); write_ds1302(0x82,get_bcd(min)); } } while(mode==3) { if(input(SET_SELECT_SW)) { mode=2; printf(lcd_putc,"\1SET TIME :%02d:%02d",hr,min); lcd_send_byte(0,0x0D); lcd_send_byte(0,0x8B); delay_ms(250); } if(input(INC_SW)) { min++; if(min==60) min=0; printf(lcd_putc,"\1SET TIME :%02d:%02d",hr,min); //lcd_send_byte(0,0x8D); lcd_send_byte(0,0x0D); lcd_send_byte(0,0x8E); delay_ms(250); } if(input(DEC_SW)) { if(min!=0) min--; else min=59; printf(lcd_putc,"\1SET TIME :%02d:%02d",hr,min); // lcd_send_byte(0,0x8D); lcd_send_byte(0,0x0D); lcd_send_byte(0,0x8E); delay_ms(250); } if(input(OK_SW)) { mode=0; lcd_send_byte(0,0x0C); write_ds1302(0x84,get_bcd(hr)); write_ds1302(0x82,get_bcd(min)); } }

while(mode==4) { if(input(SET_SELECT_SW)) { mode=1; printf(lcd_putc,"\fSET TIME ?"); delay_ms(250); } if(input(OK_SW)) { mode=5; printf(lcd_putc,"\fSET ALARM:%02d:%02d",alarm_hr,alarm_min); lcd_send_byte(0,0x0D); lcd_send_byte(0,0x8B); delay_ms(250); } } while(mode==5) { if(input(SET_SELECT_SW)) { mode=6; printf(lcd_putc,"\fSET ALARM:%02d:%02d",alarm_hr,alarm_min); lcd_send_byte(0,0x0D); lcd_send_byte(0,0x8E); delay_ms(250); } if(input(INC_SW)) { alarm_hr++; if(alarm_hr==24) alarm_hr=0; printf(lcd_putc,"\1SET ALARM:%02d:%02d",alarm_hr,alarm_min); lcd_send_byte(0,0x0D); lcd_send_byte(0,0x8B); delay_ms(250); } if(input(DEC_SW)) { if(alarm_hr!=0) alarm_hr--; else alarm_hr=23; printf(lcd_putc,"\1SET ALARM:%02d:%02d",alarm_hr,alarm_min); lcd_send_byte(0,0x0D); lcd_send_byte(0,0x8B); delay_ms(250); } if(input(OK_SW)) { mode=0; lcd_send_byte(0,0x0c); } } while(mode==6) { if(input(SET_SELECT_SW)) { mode=5; printf(lcd_putc,"\fSET ALARM:%02d:%02d",alarm_hr,alarm_min); lcd_send_byte(0,0x0D); lcd_send_byte(0,0x8B); delay_ms(250); } } }

} }

Potrebbero piacerti anche