/************************************************************************************************** * * Title : AVRY Demo * Version : 1.0 * Last updated : Jul 02 2004 * Target : ATMega16 * File : test.c * Author : Gaurang Sinha * * * DESCRIPTION * Menu driven program which can display all the features of the * AVRY board. ***************************************************************************************************/ #include #include #include #include #include #include "lcd_io.h" #include "i2cmaster.h" #include "avrlibtypes.h" #include #define MAX_MENU 8 //Always one less than total coz it starts from 0 #define UP 6 #define DOWN 7 #define OK 2 #define CANCEL 3 #define DevDS1307 0xD0 #define SET_TIME 0xAA #define ok_pressed bit_is_clear(PIND, OK) #define cancel_pressed bit_is_clear(PIND, CANCEL) #define up_pressed bit_is_clear(PINC, UP) #define down_pressed bit_is_clear(PINC, DOWN) typedef struct cTime_Format { u08 sec; u08 min; u08 hour; u08 day; u08 date; u08 month; u08 year; } Time_Format; /*------------------------------ FUNCTION PROTOTYPES ------------------------------*/ void ADC_Display(u08 pin); //Interpret all ADC component values and display void Buzzer(void); //Enables and disables the onboard buzzer void LED_Count(u08 dir); //Used to display flashing or counting LED's on PORT A void Serial_Comm(void); //Function used to control the board from the computer void EEPROM_Data(void); //Function used to display & modify EEPROM data void RTC_menu(void); //Display another submenu of all features of RTC void Delay(void); // void Splash_Screen(void); //Display's the splash screen at the start of the application void RTC_Make_Ready(void); //Make RTC ready to be Read to Written to u08 ReadTimeFromDS1307(Time_Format *); //Read time from RTC u08 InitDS1307(void); //Initialize RTC (IC DS1307) void WriteTimeToDisplay(Time_Format *); //Display time on LCD and USART u08 WriteTimeToDS1307(Time_Format *); //Write time to RTC void Enable_RTC_Osc(void); // void Stop_Watch(u08 Display); //Implements a stop watch using RTC void Get_All_Time(Time_Format *time); //Ask user from all values in the Time_Format structure u08 LCD_Accept(u08 MAX, u08 MIN, PGM_P sStr,u08 ConvertToBCD); //Accepts single value in range provided void USART_NewLine(void); //Sends newline character to USART u08 USART_GetTime(Time_Format *time,u08 Type); //Ask user from all values in the Time_Format structure void USART_Send(u08 *str,u08 size); //Send a string of a specified size on USART void USART_Send_P(const unsigned char *progmem_str); //Send program space string on USART void USART_Send_Val(u16 Value,u08 Point,u08 Length); //Send value on USART of specified size u08 USART_Receive(void); //Receive a character from USART void Enable_USART_TimeOut(void); //Enbales USART to timeout after 8.3 seconds /*------------------------------- Global Variables -------------------------------*/ PGM_P k[MAX_MENU + 2]; //Points To Main Selection Menu PGM_P CancelAbort; //Points To "Cancel To Abort" PGM_P RTC_mnu[8]; //Points To the RTC Selection Menu PGM_P OkToContinue; //Points To "(OK) To Continue" PGM_P Choice; //Points To "Choice : " PGM_P InValid; //Points To "InValid Choice" u08 USART_Timeout; //Used to indicate weather the USART has Timed Out ! u08 RTC_Timer_Enable; //Tells weather to enable down count for timer u08 Timer_Hour; //Stores the hours the timer should count down for u08 Timer_Min; //Stores the minutes the timer should count down for u08 Timer_Sec; //Stores the seconds the timer should count down for u08 Alarm_Check = 0; //Stores the count for the Timer/Counter0 to check alarm u08 Milliseconds; //Stores the milliseconds value for the stop watch u08 USART_Display_Enable=0; //Tells individual function weather to output values to USART u08 Time_Check; /* Used to store previous value of seconds so that function will display only when there is a change */ Time_Format Alarm; //Variable To Store The Alarm Time unsigned char UNESOLS_LOGO[]__attribute__((progmem)) = { 0x1F,0x00,0x11,0x11,0x11,0x0E,0x00,0x1F, //U 0x1F,0x00,0x11,0x19,0x15,0x13,0x00,0x1F, //N 0x1F,0x00,0x1F,0x10,0x1C,0x1F,0x00,0x1F, //E 0x1F,0x00,0x07,0x08,0x02,0x1C,0x00,0x1F, //S 0x1F,0x00,0x0E,0x11,0x11,0x0E,0x00,0x1F, //O 0x1F,0x00,0x10,0x10,0x10,0x1F,0x00,0x1F, //L 0x1F,0x00,0x07,0x08,0x02,0x1C,0x00,0x1F, //S 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; /* ---------------------------------------------- MAIN FUNCTION. PROGRAM STARTS EXECUTING HERE!! ---------------------------------------------- */ int main(void) { unsigned char p; CancelAbort = PSTR("Cancel To Abort"); OkToContinue = PSTR("(OK) To Continue"); Choice = PSTR("Choice : "); InValid = PSTR("InValid Choice"); k[0] = PSTR(" Light Sensor \n"); k[1] = PSTR(" Time - RTC \n"); k[2] = PSTR(" Buzzer \n"); k[3] = PSTR(" Counting Led \n"); k[4] = PSTR(" Blinking Lights\n"); k[5] = PSTR(" Temp Sensor \n"); k[6] = PSTR(" Volt Meter \n"); k[7] = PSTR(" Serial Comm \n"); k[8] = PSTR(" EEPROM Data \n"); k[9] = PSTR(" \n"); RTC_mnu[0] = PSTR(" Current Time \n"); RTC_mnu[1] = PSTR(" Set Time \n"); RTC_mnu[2] = PSTR(" Set Alarm \n"); RTC_mnu[3] = PSTR(" View Alarm \n"); RTC_mnu[4] = PSTR(" Set Timer \n"); RTC_mnu[5] = PSTR(" View Timer \n"); RTC_mnu[6] = PSTR(" Stop Watch \n"); RTC_mnu[7] = PSTR(" \n"); lcd_init(); //Incrementing Counter In EEPROM eeprom_busy_wait(); p=eeprom_read_byte((u08 *)0x01); p++; eeprom_busy_wait(); eeprom_write_byte ((u08 *)0x01, p); sbi(PORTD, OK); Splash_Screen(); p = 0; //Enable Global Interrupts sei(); for(;;) { lcd_gotoxy(0,0); lcd_puts_p(k[p]); lcd_puts_p(k[p+1]); lcd_gotoxy(0,0); lcd_putc('>'); sbi(PORTC, UP); sbi(PORTC, DOWN); sbi(PORTD, OK); sbi(PORTD, CANCEL); while(up_pressed) { //UP if(p > 0) { lcd_gotoxy(0,0); lcd_puts_p(k[--p]); lcd_puts_p(k[p-1]); } break; } while(down_pressed) { //DOWN if(p 7) { test=0; } //Delay for(j=0;j<255;j++) { for(k=0;k<30;k++) { ; } } } lcd_puts_p(PSTR("\nHello ")); lcd_puts_e((u08 *)0x02); lcd_gotoxy(0,0); test++; if(test > 7) { test=0; } } //Make All User Defined Characters blank for(i = 0 ;i < 8; i++ ){ lcd_putc_cgram(UNESOLS_LOGO[7],i); } Delay(); } void Buzzer(void) { /* Buzzer is Connected to PD4 therefore sending a low signal on PortD Pin 4 */ lcd_clrscr(); lcd_gotoxy(0, 0); DDRD = 0xF0; lcd_puts_p(PSTR(" Buzzer ON !!!\n")); lcd_puts_p(CancelAbort); while(!cancel_pressed) { cbi(PORTD, 4); //Activating Buzzer Delay(); sbi(PORTD, 4); //Deactivating Buzzer Delay(); if(USART_Display_Enable) { if(bit_is_set(UCSRA, RXC)) { u08 tmp; tmp = UDR; return; } } } return; } void LED_Count(u08 dir) { u08 num; lcd_clrscr(); lcd_gotoxy(0, 0); num = PORTA; DDRA = 0xF0; PORTA = 0xA0; for(;;){ if(dir == 0){ lcd_puts_p(k[4]); lcd_puts_p(PSTR("LED - PORT A ")); Delay(); Delay(); PORTA = (0xF0 ^ PORTA); while(cancel_pressed) { PORTA = (0x0F & num); return; } if(USART_Display_Enable) { if(bit_is_set(UCSRA, RXC)) { dir = UDR; return; } } } else { lcd_puts_p(k[3]); lcd_puts_p(PSTR("LED - PORT A\n")); PORTA = (0x0F & num); while(PORTA < 0xFF) { PORTA = num; num = num + 0x10; Delay(); while(cancel_pressed) { PORTA = (0x0F & num); return; } if(USART_Display_Enable){ if(bit_is_set(UCSRA, RXC)) { dir = UDR; return; } } } } } } /************************************************************************************************** * ADMUX - Multiplexer Selection Register * 7 6 5 4 3 2 1 0 * REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0 * * REFS1 REFS0 * 0 0 - AREF, Internal Vref Turned off * 0 1 - AVCC with external capacitor at AREF pin * 1 0 - Reserved * 1 1 - Internal 2.56V voltage reference with external capacitor at AREF pin * * ADLAR - Affects the presentation of the ADC conversion result 0=Right Adjusted, 1=Left Adjusted * MUX4 to MUX0 - Analog channel and Gain selection bits * Our value is 0100XXXX i.e. 0x4X ************************************************************************************************** * ADCSR - ADC Control & Status Register * 7 6 5 4 3 2 1 0 * ADEN ADSC ADATE ADIF ADIE ADSP2 ADSP1 ADSP0 * * ADEN - ADC Enable * ADSC - ADC Start Of Conversion * ADATE - ADC Auto Trigger Enable (on +ive edge) * ADIF - ADC End Of Conversion (set to High on EOC) * ADIE - ADC Interrupt Enable * ADSP2 to ADSP0 - Division Factor between XTAL and internal clock of ADC * :. our contorl word is 11000000 i.e. 0xC0 **************************************************************************************************/ void ADC_Display(u08 adc_pin) { union _adc_data { u08 dt[2]; u16 data; } adc_data; u08 bar=0,i; lcd_clrscr(); ADMUX = (0x40 + adc_pin); for(;;) { ADCSR = 0xC0; //Control Word And SOC (using divisor of 128) loop_until_bit_is_set ( ADCSR, ADIF ); //Wait For EOC or while(ADIF==0) adc_data.dt[0] = ADCL; //Read Lower Byte adc_data.dt[1] = ADCH; //Read Higher Byte if(adc_pin == 0 || adc_pin == 2) { adc_data.data = adc_data.data * 48; } if(USART_Display_Enable == 1) { USART_NewLine(); } lcd_gotoxy(0, 0); if(adc_pin == 0) { lcd_puts_p(PSTR("Temperature\n")); lcd_put_i(adc_data.data, 2, 5); if(USART_Display_Enable) { USART_Send((u08 *)0x13, 1); //Carriage Return USART_Send_Val(adc_data.data, 2, 4); USART_Send((u08 *)" C", 2); } lcd_putc(' '); lcd_putc(0xDF); lcd_putc('C'); } else if(adc_pin == 1) { lcd_puts_p(PSTR("Light Sensor\n")); lcd_put_i(adc_data.data, 0, 4); if(USART_Display_Enable) { USART_Send((u08 *)0x13, 1); //Carriage Return USART_Send_Val(adc_data.data, 0, 4); } adc_data.data = 1024 - adc_data.data; lcd_putc(' '); bar = 0; while(adc_data.data > 102) { bar++; adc_data.data -= 102; } for(i = 0; i < 10; i++) { if(bar) { lcd_putc(0xFF); bar--; } else { lcd_putc(' '); } } } else if(adc_pin == 2){ lcd_puts_p(PSTR("Volt Meter\n")); lcd_put_i(adc_data.data, 4, 5); if(USART_Display_Enable) { USART_Send((u08 *)0x13,1); //Carriage Return USART_Send_Val(adc_data.data,0,4); USART_Send((u08 *)" V",2); } lcd_putc('V'); } else { lcd_puts_p(PSTR("Unknow Device")); } if(USART_Display_Enable) { if(bit_is_set(UCSRA, RXC)) { adc_data.dt[0] = UDR; return; } } Delay(); while(cancel_pressed) { //Wait For Cancel Button To Be Pressed return; } } } void USART_NewLine(void) { loop_until_bit_is_set(UCSRA, UDRE); UDR = 10; loop_until_bit_is_set(UCSRA, UDRE); UDR = 13; } void USART_Send_Val(u16 Value,u08 Point_Pos,u08 Length) { u08 *temp,i; utoa(Value,temp,10); for(i=0;i0x41) Name[i]--; else Name[i]=0x5A; } lcd_gotoxy(0,0); lcd_puts(Name); Delay(); } Delay(); } Name[7]='\0'; lcd_gotoxy(0,0); lcd_puts_p(P_Name); lcd_puts(Name); lcd_putc('\n'); //wait till EEPROM is not busy eeprom_busy_wait(); //Write 8 bytes to EEPROM from 0x02 eeprom_write_block(Name, (u08 *)0x02,8); lcd_puts_p(PSTR("New Name Saved")); loop_until_bit_is_clear(PIND, CANCEL); } } return; } void RTC_Make_Ready() { DDRB = 0xFF; PORTB = 0xFF; InitDS1307(); Enable_RTC_Osc(); } void RTC_menu(void) { u08 s=0; lcd_gotoxy(0,0); lcd_clrscr(); Delay(); while(!cancel_pressed){ while(!ok_pressed){ lcd_gotoxy(0,0); lcd_puts_p(RTC_mnu[s]); lcd_puts_p(RTC_mnu[s+1]); lcd_gotoxy(0,0); lcd_putc('>'); if(down_pressed){ if(s<6) s++; } if(up_pressed){ if(s>0) s--; } if(cancel_pressed){ return; } Delay(); } switch(s){ case 0: { Time_Format TimeStamp; RTC_Make_Ready(); lcd_clrscr(); while(!cancel_pressed){ ReadTimeFromDS1307(&TimeStamp); lcd_gotoxy(0,0); WriteTimeToDisplay(&TimeStamp); Delay(); } } break; case 1: { Time_Format NewTime; lcd_clrscr(); //Get All Values From The User Get_All_Time(&NewTime); //Write New Time To RTC lcd_clrscr(); RTC_Make_Ready(); WriteTimeToDS1307(&NewTime); while(!cancel_pressed){ ReadTimeFromDS1307(&NewTime); lcd_gotoxy(0,0); WriteTimeToDisplay(&NewTime); } } break; case 2: lcd_clrscr(); Delay(); //Get All Time Values From User Get_All_Time(&Alarm); /****************************************************************************** * Timer/Counter Contorl Register - TCCR0 * 7 6 5 4 3 2 1 0 * FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00 * * FOC0 - Force Output Compare * WGM01:0 - Waveform Generation Mode * COM01:0 - Compare Match Output Mode * CS02:0 - Clock Select * * :. 00000101 i.e. 0x05 using prescalar of 1024 ****************************************************************************** * Timer/Counter Register - TCNT0 * 7 6 5 4 3 2 1 0 * R/w R/W R/W R/W R/W R/W R/W R/W ****************************************************************************** * Timer/Counter Interrupt Mask - TIMSK * 7 6 5 4 3 2 1 0 * - - - - - - OCIE0 TOIE0 * * OCIE0 - Timer/Counter2 Output Compare Match Interrupt * TOIE0 - Timer/Counter2 Overflow Interrupt Enable ******************************************************************************/ //Set Timer to Zero TCNT0=0x00; //Enable Overflow Interrupt timer_enable_int(_BV(TOIE0)); //Initialize Alarm Check Counter to zero Alarm_Check = 0; //Start Timer with prescalar of 1024 TCCR0 = 0x05; //Display Alarm Set Screen lcd_clrscr(); lcd_gotoxy(0,0); WriteTimeToDisplay(&Alarm); loop_until_bit_is_clear(PIND, CANCEL); break; case 3: lcd_clrscr(); lcd_gotoxy(0,0); WriteTimeToDisplay(&Alarm); loop_until_bit_is_clear(PIND, CANCEL); break; case 4: lcd_clrscr(); Delay(); Timer_Sec = LCD_Accept(59, 0, PSTR("Enter Sec"), 0); Delay(); Timer_Min = LCD_Accept(59, 0, PSTR("Enter Min"), 0); Delay(); Timer_Hour = LCD_Accept(23, 0, PSTR("Enter Hour"), 0); Delay(); if(Timer_Sec || Timer_Min || Timer_Hour){ Milliseconds = 0; RTC_Timer_Enable = 1; //Make Timer Down Count Enable //Enable Timer 2 TCNT2 = 0x4F; //Set Count for 0.01 seconds timer_enable_int(_BV(TOIE2)); //Enable Overflow Interrupt TCCR2 = 0x07; //Prescalar Of 1024 & SOC lcd_clrscr(); lcd_puts_p(PSTR("Timer Set\n")); lcd_puts_p(OkToContinue); loop_until_bit_is_clear(PIND, OK); Delay(); } break; case 5: lcd_clrscr(); if(Timer_Hour || Timer_Min || Timer_Sec) { while(!cancel_pressed) { lcd_gotoxy(0, 0); lcd_put_i(Timer_Hour, 0, 2); lcd_putc(':'); lcd_put_i(Timer_Min, 0, 2); lcd_putc(':'); lcd_put_i(Timer_Sec, 0, 2); lcd_putc('\n'); lcd_puts_p(CancelAbort); } } else { lcd_clrscr(); lcd_gotoxy(0, 0); lcd_puts_p(PSTR("Timer Not Set\n")); lcd_puts_p(CancelAbort); loop_until_bit_is_clear(PIND, CANCEL); } break; case 6: Stop_Watch(0); break; } Delay(); } } void Get_All_Time(Time_Format *time) { //For Seconds time->sec = LCD_Accept(59, 0, PSTR("Enter Sec"), 1); Delay(); //For Minutes time->min = LCD_Accept(59, 0, PSTR("Enter Min"), 1); Delay(); //For Hour time->hour = LCD_Accept(23, 0, PSTR("Enter Hour"), 1); Delay(); //For Date time->date = LCD_Accept(31, 1, PSTR("Enter Date"), 1); Delay(); //For Month time->month = LCD_Accept(12, 1, PSTR("Enter Mnth"), 1); Delay(); //For Year time->year = LCD_Accept(99, 0, PSTR("Enter Year"), 1); Delay(); } u08 LCD_Accept(u08 MAX, u08 MIN, PGM_P sStr,u08 ConvertToBCD) { u08 SetTime=MIN; while(!ok_pressed){ if(up_pressed){ if(SetTimeMIN) SetTime--; else SetTime = MAX; } lcd_gotoxy(0,0); lcd_put_i(SetTime,0,2); lcd_putc('\n'); lcd_puts_p(sStr); Delay(); } if(ConvertToBCD){ //Converting To BCD MAX = MIN = SetTime; MAX = MAX / 10; MIN = MIN % 10; SetTime = ((MAX&0x0F)<<4) + (MIN&0x0F); } return SetTime; } void Stop_Watch(u08 Display) { //Declare Variables u08 Sec=0,Prev,Min=0,Hour=0; Time_Format Temp; //Display Start Screen lcd_clrscr(); lcd_gotoxy(0,0); lcd_puts_p(PSTR("(UP) To Start\n")); lcd_puts_p(PSTR("00:00:00.00")); //For USART if(Display){ USART_NewLine(); USART_Send_P(PSTR("Press Any Key To Start")); } //Wait For UP or USART char for(;;){ if(up_pressed) break; if(USART_Display_Enable){ if(bit_is_set(UCSRA, RXC)){ Prev=UDR; break; } } } //Show Stop Watch Screen lcd_clrscr(); //Read Current Time From RTC RTC_Make_Ready(); ReadTimeFromDS1307(&Temp); Prev = Temp.sec; while(!down_pressed){ ReadTimeFromDS1307(&Temp); if(Prev!=Temp.sec){ if(!RTC_Timer_Enable){ Milliseconds = 0; TCNT2=0x4F; //Set Count for 0.01 seconds timer_enable_int(_BV(TOIE2)); //Enable Overflow Interrupt TCCR2 = 0x07; //Prescalar Of 1024 & SOC } Prev = Temp.sec; Sec++; if(Sec == 60){ Sec = 00; Min++; } if(Min == 60){ Min = 00; Hour++; } } lcd_gotoxy(0, 0); lcd_put_i(Hour, 0, 2); lcd_putc(':'); lcd_put_i(Min, 0, 2); lcd_putc(':'); lcd_put_i(Sec, 0, 2); lcd_putc('\n'); lcd_puts_p(PSTR("(DOWN) To Stop")); if((Display) && (Sec != Time_Check)) { Time_Check = Sec; USART_Send_Val(Hour, 0, 2); USART_Send_P(PSTR(" : ")); USART_Send_Val(Min, 0, 2); USART_Send_P(PSTR(" : ")); USART_Send_Val(Sec, 0, 2); USART_NewLine(); if(bit_is_set(UCSRA, RXC)) { Prev = UDR; break; } } } lcd_clrscr(); lcd_gotoxy(0, 0); lcd_put_i(Hour, 0, 2); lcd_putc(':'); lcd_put_i(Min, 0, 2); lcd_putc(':'); lcd_put_i(Sec, 0, 2); lcd_putc('.'); lcd_put_i(Milliseconds, 0, 2); lcd_putc('\n'); lcd_puts_p(PSTR("Timer Stopped ")); if(Display){ USART_Send_Val(Hour, 0, 2); USART_Send_P(PSTR(" : ")); USART_Send_Val(Min, 0, 2); USART_Send_P(PSTR(" : ")); USART_Send_Val(Sec, 0, 2); USART_Send_P(PSTR(".")); USART_Send_Val(Milliseconds, 0, 2); USART_NewLine(); loop_until_bit_is_set(UCSRA, RXC); Prev=UDR; return; } loop_until_bit_is_clear(PIND, CANCEL); } u08 USART_GetTime(Time_Format *time,u08 Type) { u08 i; USART_Send_P(PSTR("Set Time 24hr Format")); USART_NewLine(); //Seconds USART_Send_P(PSTR("Seconds : ")); /* Setting TimeOut On Timer1 16bit Timer */ USART_Timeout=0; Enable_USART_TimeOut(); // while(USART_Timeout!=1){ if(bit_is_set(UCSRA, RXC)){ TCCR1B=0x00; break; } } if(USART_Timeout==1) { /* UART Timeout is hit*/ USART_NewLine(); USART_Send_P(PSTR("TimeOut !!!")); return 1; //Error Timed Out } i = USART_Receive(); time->sec = (i&0x0F); time->sec = (time->sec<<4); //Put Number in MSB i = USART_Receive(); time->sec = time->sec + (i&0x0F); USART_NewLine(); //Minutes USART_Send_P(PSTR("Minutes : ")); i = USART_Receive(); time->min = (i&0x0F); time->min = (time->min<<4); i = USART_Receive(); time->min = time->min + (i&0x0F); USART_NewLine(); //Hours USART_Send_P(PSTR("Hours : ")); i = USART_Receive(); time->hour = (i&0x0F); time->hour = (time->hour<<4); i = USART_Receive(); time->hour = time->hour + (i&0x0F); USART_NewLine(); if(Type){ //Date USART_Send_P(PSTR("Date : ")); i = USART_Receive(); time->date = (i&0x0F); time->date = (time->date<<4); i = USART_Receive(); time->date = time->date + (i&0x0F); USART_NewLine(); //Month USART_Send_P(PSTR("Month : ")); i = USART_Receive(); time->month = (i&0x0F); time->month = (time->month<<4); i = USART_Receive(); time->month = time->month + (i&0x0F); USART_NewLine(); //Year USART_Send_P(PSTR("Year : ")); i = USART_Receive(); time->year = (i&0x0F); time->year = (time->year<<4); i = USART_Receive(); time->year = time->year + (i&0x0F); USART_NewLine(); USART_Send_P(PSTR("Writing To RTC")); USART_NewLine(); } return 0; } u08 USART_Receive(void) { u08 i; loop_until_bit_is_set(UCSRA, RXC); //Wait Until Byte Is Received i=UDR; if((bit_is_set(UCSRA,FE))||(bit_is_set(UCSRA,DOR))||(bit_is_set(UCSRA,PE))){ USART_NewLine(); USART_Send_P(PSTR("!!! Error !!!")); lcd_gotoxy(1,0); lcd_puts_p(PSTR("Error During Rx")); loop_until_bit_is_clear(PIND, CANCEL); return 0; } return i; } u08 InitDS1307() { u08 ret; i2c_init(); // init I2C interface ret = i2c_start(DevDS1307+I2C_WRITE); // set device address and write mode if (ret) { /* failed to issue start condition, possibly no device found */ i2c_stop(); return(0); } else { i2c_stop(); return(1); } } u08 ReadTimeFromDS1307(Time_Format *time) { i2c_start_wait(DevDS1307+I2C_WRITE); i2c_write(0x00); i2c_rep_start(DevDS1307+I2C_READ); time->sec = i2c_readAck(); time->min = i2c_readAck(); time->hour = i2c_readAck(); time->day = i2c_readAck(); time->date = i2c_readAck(); time->month = i2c_readAck(); time->year = i2c_readNak(); i2c_stop(); // set stop conditon = release bus return(1); } u08 WriteTimeToDS1307(Time_Format *time) { i2c_start_wait(DevDS1307+I2C_WRITE); i2c_write(0x00); i2c_write(time->sec); i2c_write(time->min); i2c_write(time->hour); i2c_write(time->day); i2c_write(time->date); i2c_write(time->month); i2c_write(time->year); i2c_stop(); // set stop conditon = release bus return(1); } void WriteTimeToDisplay(Time_Format *time) { u08 TimeBuffer[10]; u08 DateBuffer[10]; TimeBuffer[0] = ((time->hour>>4)&0x03) + 0x30; TimeBuffer[1] = (time->hour&0x0F) + 0x30; TimeBuffer[2] = ':'; TimeBuffer[3] = ((time->min>>4)&0x07) + 0x30; TimeBuffer[4] = (time->min&0x0F) + 0x30; TimeBuffer[5] = ':'; TimeBuffer[6] = ((time->sec>>4)&0x07) + 0x30; TimeBuffer[7] = (time->sec&0x0F) + 0x30; TimeBuffer[8] = ' '; TimeBuffer[9] = '\0'; DateBuffer[0] = ((time->date>>4)&0x03) + 0x30; DateBuffer[1] = (time->date&0x0F) + 0x30; DateBuffer[2] = '/'; DateBuffer[3] = ((time->month>>4)&0x01) + 0x30; DateBuffer[4] = (time->month&0x0F) + 0x30; DateBuffer[5] = '/'; DateBuffer[6] = ((time->year>>4)&0x0F) + 0x30; DateBuffer[7] = (time->year&0x0F) + 0x30; DateBuffer[8] = ' '; DateBuffer[9] = '\0'; lcd_puts(TimeBuffer); lcd_putc('\n'); lcd_puts(DateBuffer); if((USART_Display_Enable)&&(Time_Check!=time->sec)){ //If Time Needs To Be Sent On The USART Time_Check = time->sec; USART_Send(TimeBuffer,9); USART_Send(DateBuffer,8); USART_NewLine(); } } void Enable_RTC_Osc(void) { u08 sec; i2c_start_wait(DevDS1307+I2C_WRITE); i2c_write(0x00); //Go to read mode i2c_rep_start(DevDS1307+I2C_READ); //Get the time! sec = i2c_readNak(); i2c_stop(); //Keep the seconds as is just force bit 7 to low to enable the osc. //Check DS1307 sheet for explanations. sec = sec & 0x7F; //Write this byte to the DS1307 i2c_start_wait(DevDS1307+I2C_WRITE); //Write address i2c_write(0x00); //Write the byte i2c_write(sec); i2c_stop(); }//End of Enable_RTC_Osc /************************************************************************************************** * Timer/Counter1 Control Register A - TCCR1A * 7 6 5 4 3 2 1 0 * COM1A1 COM1A0 COM1B1 COM1B0 FOC1A FOC1B WGM11 WGM10 * * COM1A1:0 - Compare Output Mode For Channel A * COM1B1:0 - Compare Output Mode For Channel B * FOC1A - Force Output Compare for Channel A * FOC1B - Force Output Compare For Channel B * WGM11:0 - Waveform Generation Mode * * :. 00001100 - 0x0C ************************************************************************************************** * Timer/Counter1 Control Register B - TCCR1B * 7 8 5 4 3 2 1 0 * ICNC1 ICES1 - WGM13 WGM12 CS12 CS11 CS10 * * ICNC1 - Input capture Noise Canceler * ICES1 - Input Capture Edge Select * WGM13:2 - Waveform Generation Mode * CS12:0 - Clock Select * * :. 00000101 - 0x05 ************************************************************************************************** * Timer/Counter1 - TCNT1H & TCNT1L * 7 6 5 4 3 2 1 0 * - - - - - - - - TCNT1H * - - - - - - - - TCNT1L ************************************************************************************************** * Timer/Counter Interrupt Mask Register - TIMSK (Showing Only Bits Relavent To 16bit Timers) * 7 6 5 4 3 2 1 0 * - - TICIE1 OCIE1A OCIE1B TOIE1 - - * * TICIE1 - Timer/Counter1 Input Capture Interrupt Enable * OCIE1A - Timer/Counter1 Output Compare A Match Interrupt Enable * OCIE1B - Timer/Counter1 Output Compare B Match Interrupt Enable * TOIE1 - Timer/Counter1 Overflow Interrupt Enable * ************************************************************************************************** * Timer/Counter Interrupt Flag Register - TIFR (Showing Only Bits Relavent To 16bit Timers) * 7 6 5 4 3 2 1 0 * - - ICF1 0CF1A OCF1B TOV1 - - * * ICF1 - Timer/Counter1 Input Capture Flag * OCF1A - Timer/Counter1, Output Compare A Match Flag * OCF1B - Timer/Counter1, Output Compare B Match Flag * TOV1 - Timer/Counter1, Overflow Flag **************************************************************************************************/ void Enable_USART_TimeOut(void) { u08 val; TCNT1=0x0000; val = TIMSK; val += 0x04; TIMSK = val; //timer_enable_int(_BV(TOIE1)); TCCR1B = 0x05; } SIGNAL(SIG_OVERFLOW1) //Using 16 bit Timer for USART TimeOut { //Disable Interrupt & Timer if(USART_Timeout==0){ TCCR1B = 0x00; USART_Timeout = 1; } } SIGNAL(SIG_OVERFLOW2) //Using 8 bit Timer for Milliseconds in SW & Timer { Milliseconds++; if(RTC_Timer_Enable) { if(50 == Milliseconds) { Milliseconds = 0; if(!Timer_Sec) { if(Timer_Min) { Timer_Min--; Timer_Sec = 59; } } else{ Timer_Sec--; } if(!Timer_Min) { if(Timer_Hour) { Timer_Hour--; Timer_Min = 59; } } if((!Timer_Hour) && (!Timer_Min) && (!Timer_Sec)){ lcd_clrscr(); lcd_gotoxy(0, 0); lcd_puts_p(PSTR("Timer TimeOut\n")); lcd_puts_p(OkToContinue); if(USART_Display_Enable){ USART_NewLine(); USART_NewLine(); USART_Send_P(PSTR("!!! Timer TimeOut !!!")); USART_NewLine(); USART_NewLine(); } while(!ok_pressed){ if((USART_Display_Enable)&&(bit_is_set(UCSRA, RXC))){ RTC_Timer_Enable=UDR; RTC_Timer_Enable = 0; lcd_clrscr(); TCCR2 = 0x00; return; } } TCCR2 = 0x00; RTC_Timer_Enable = 0; //Disable Timer lcd_clrscr(); } } else { //Enable Timer 2 TCNT2 = 0x4F; //Set Count for 0.01 seconds timer_enable_int(_BV(TOIE2)); //Enable Overflow Interrupt TCCR2 = 0x07; //Prescalar Of 1024 & SOC } } } SIGNAL(SIG_OVERFLOW0) //Using 8 bit Timer for Alarm Check { Alarm_Check++; if(Alarm_Check>60){ Time_Format Test; RTC_Make_Ready(); //Read Current Time From RTC ReadTimeFromDS1307(&Test); if((Alarm.year==Test.year)&&(Alarm.month==Test.month)&&(Alarm.date==Test.date)&&(Alarm.hour==Test.hour)&&(Alarm.min==Test.min)){ lcd_clrscr(); lcd_gotoxy(0,0); lcd_puts_p(PSTR("Alarm Timeout\n")); if(USART_Display_Enable){ USART_NewLine(); USART_NewLine(); USART_Send_P(PSTR("!!! Alarm Time Out !!!")); USART_NewLine(); USART_NewLine(); } Alarm.year = Alarm.month = Alarm.date = Alarm.hour = Alarm.min = Alarm.sec = 0; lcd_puts_p(OkToContinue); cbi(PORTD, 4); while(!ok_pressed){ if((USART_Display_Enable)&&(bit_is_set(UCSRA, RXC))){ Alarm_Check=UDR; sbi(PORTD, 4); lcd_clrscr(); return; } } sbi(PORTD, 4); lcd_clrscr(); } else{ //Initialize Alarm Check Counter to zero Alarm_Check = 0; //Set Timer to Zero TCNT0=0x00; //Enable Overflow Interrupt timer_enable_int(_BV(TOIE0)); //Start Timer with prescalar of 1024 TCCR0 = 0x05; } } else{ //Set Timer to Zero TCNT0=0x00; //Enable Overflow Interrupt timer_enable_int(_BV(TOIE0)); //Start Timer with prescalar of 1024 TCCR0 = 0x05; } } void Delay() { u08 i, j; for (i = 0; i < 255; i++ ) { for (j = 0; j < 255; j++ ) { ; } } }