'******************************************************************* ' Program name timer_clock ' Start Date 21st.Mar.2010 ' Author Yoshio KATO ' Description : Used MCU ATtiny2313 ' : System Clock 12.8MHz External OSC ' : Get now time from digital switch ' : Set alarm time by digital switch ' : Send time data to display unit '******************************************************************* $regfile = "attiny2313.dat" $crystal = 12800000 $baud = 38400 Dim Alarm_flg As Byte Dim Sw_no As Byte Dim Hour As Byte Dim Minute As Byte Dim Second As Byte Dim Tmcount As Byte Dim Wk As Byte Dim Tm_hour As Byte 'timer hour Dim Tm_minute As Byte 'timer minute Const Sw_m1 = 4 'PB0 Const Sw_m10 = 5 'PB1 Const Sw_h1 = 6 'PB2 Const Sw_h10 = 7 'PB3 Const Sec_led = 6 'PD6 Const Timer_sw = 3 'PD3 Const Melody = 4 'PD4 reley Const Timer_led = 5 'PD5 Const Tmconst = 100 'timer interrup counter Main: Stop Ac Config Portb = &HF0 Config Portd = &HF0 'Output Config Int0 = Falling 'PD2 hardware interrup falling edge On Int0 Timeset Enable Int0 Config Timer0 = Timer , Prescale = 1024 , Clear Timer = 1 Ocr0a = 124 On Oc0a Tmovf ' if timer0 overflow go to Tmovf interrupt routine Enable Oc0a Enable Interrupts Portb = &H0F 'PB4 to PB7 pull up Portd = &H0C 'PD2,PD3 pull up 'Time Data Clear Tmcount = 0 Second = 0 Hour = 0 Minute = 0 Alarm_flg = 0 Wk = Minute Printbin Wk 'send minute value to disp uint Wk = Hour Or &H80 '+ 128 set MSB 1 Printbin Wk 'send hour value to disp unit Set Portd.melody 'turn off relay 'evrer loop Do If Alarm_flg = 1 Then Reset Portd.melody Else Set Portd.melody End If Loop End '******************************************************************* ' Timer1 interrupt ' Interrupt ever 10ms '******************************************************************* Tmovf: Incr Tmcount If Tmcount >= 50 Then Set Portd.sec_led Else Reset Portd.sec_led End If If Pind.timer_sw = 0 Then 'timer switch on? Set Portd.timer_led Gosub Check_timer 'get timer value and compare now time Else Reset Portd.timer_led Alarm_flg = 0 End If If Tmcount >= Tmconst Then Tmcount = 0 Incr Second 'increment second If Second = 60 Then 'second over 60 ? Second = 0 Incr Minute 'increment minute If Minute >= 60 Then 'minute over 60 ? Minute = 0 'yes minute is over 60 Incr Hour 'increment hour If Hour = 24 Then 'hour over 24 ? Hour = 0 'yes hour is over 24 End If End If Wk = Minute Printbin Wk 'send minute value to disp uint Wk = Hour Or &H80 '+ 128 set MSB 1 Printbin Wk 'send hour value to disp unit End If End If Return '******************************************************************* ' Hardware interrupt routine timer set ' set hour and minute ' Clear Tmcount and Second '******************************************************************* Timeset: Gosub Get_digital_switch Minute = Tm_minute Hour = Tm_hour Tmcount = 0 Second = 0 Wk = Minute Printbin Wk 'send minute value to disp uint Wk = Hour Or &H80 '+ 128 set MSB 1 Printbin Wk 'send hour value to disp unit Return '******************************************************************* ' Get digital switch '******************************************************************* Get_digital_switch: Disable Interrupts Sw_no = Sw_m1 Gosub Read_digital_sw Tm_minute = Wk Sw_no = Sw_m10 Gosub Read_digital_sw Wk = Wk * 10 Tm_minute = Tm_minute + Wk Sw_no = Sw_h1 Gosub Read_digital_sw Tm_hour = Wk Sw_no = Sw_h10 Gosub Read_digital_sw Wk = Wk * 10 Tm_hour = Tm_hour + Wk Enable Interrupts Return '******************************************************************* ' Get timer value from digital switch '******************************************************************* Check_timer: Gosub Get_digital_switch If Tm_minute = Minute Then 'check timer value:now time If Tm_hour = Hour Then Alarm_flg = 1 'relay on End If End If Return '******************************************************************* ' Get digital switch '******************************************************************* Read_digital_sw: Set Portb.sw_m1 Set Portb.sw_m10 Set Portb.sw_h1 Set Portb.sw_h10 Waitus 50 Reset Portb.sw_no Waitus 50 Wk = Pinb Wk = Wk And &H0F Return