'******************************************************************* ' Program name talking clock ' Start Date 28th.Mar.2010 ' Author Yoshio KATO ' Description : Used MCU ATtiny2313 ' : System Clock 12.8MHz External OSC ' : '******************************************************************* $regfile = "attiny2313.dat" $crystal = 12800000 '4915200 $baud = 38400 Dim Err_flg As Bit Dim I As Byte Dim J As Byte Dim K As Byte Dim Sw_no As Byte Dim Hour As Byte Dim Minute As Byte Dim Temp_hour As Byte Dim Temp_minute As Byte Dim Timer_hour As Byte Dim Timer_minute As Byte Dim Second As Byte Dim Tmcount As Byte Dim Wk As Byte Dim Msg As String * 3 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 Tmconst = 100 'timer interrup counter Const Sound_off = 4 'just hour off Const Ir_sensor = 5 'infrared sensor input Const Alarm_sw = 3 'Alarm set switch PD3 Main: Stop Ac 'off analog comparater power Config Portb = &HF0 Config Portd = &HC0 Config Int0 = Falling 'PD2 hardware interrup falling edge INT0 On Int0 Timeset Enable Int0 Config Int1 = Falling 'PD3 hardware interrup falling edge INT1 On Int1 Alarm_set 'hard ware interrup, alarm time set routine Enable Int1 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 = &H3C 'PD2,PD3 pull up Do 'evrer loop If Pind.ir_sensor = 0 Then Gosub Time_talk 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 Tmcount >= Tmconst Then 'past 1 secod ? Tmcount = 0 If Pind.alarm_sw = 0 Then 'alarm set switch on? Wk = Second Mod 5 'if alarm time,call alarm_talk every 5 second If Timer_hour = Hour And Timer_minute = Minute And Wk = 0 Then 'alarm time? Gosub Alarm_talk 'call alarm time coming End If End If 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 Pind.sound_off = 0 Then Gosub Just_hour_talk End If If Hour = 24 Then 'hour over 24 ? Hour = 0 'yes hour is over 24 End If End If End If End If Return '******************************************************************* ' Hardware interrupt routine timer set ' set hour and minute ' Clear Tmcount and Second '******************************************************************* Timeset: Gosub Get_digital_switch If Err_flg = 0 Then Tmcount = 0 Second = 0 Hour = Temp_hour Minute = Temp_minute Gosub Time_set_talk End If Return '******************************************************************* ' Get digital switch '******************************************************************* Get_digital_switch: Disable Interrupts Sw_no = Sw_m1 Gosub Read_digital_sw Temp_minute = Wk Sw_no = Sw_m10 Gosub Read_digital_sw Wk = Wk * 10 Temp_minute = Temp_minute + Wk Sw_no = Sw_h1 Gosub Read_digital_sw Temp_hour = Wk Sw_no = Sw_h10 Gosub Read_digital_sw Wk = Wk * 10 Temp_hour = Temp_hour + Wk If Temp_hour > 23 Or Temp_minute > 59 Then Gosub Error_msg_talk Err_flg = 1 Else Err_flg = 0 End If Enable Interrupts 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 '******************************************************************* ' Send message to Micro Talk '******************************************************************* Send_data: Print "MSG"; Send_data1: Read J For I = 1 To J Read K Printbin K Next I Return '******************************************************************* ' Talk hour and minute '******************************************************************* Time_talk: Restore Time_talk1: Gosub Send_data Msg = Str(hour) Print Msg; Restore Time_talk2: Gosub Send_data1 Msg = Str(minute) Print Msg; Restore Time_talk3: Gosub Send_data1 Return '******************************************************************* ' Talk time set hour and minute '******************************************************************* Time_set_talk: Restore Time_set1 Gosub Send_data Msg = Str(hour) Print Msg; Restore Time_set2: Gosub Send_data1 Msg = Str(minute) Print Msg; Restore Time_set3: Gosub Send_data1 Return '******************************************************************* ' Talk only hour '******************************************************************* Just_hour_talk: Restore Just_hour1: Gosub Send_data Msg = Str(hour) Print Msg; Restore Just_hour2: Gosub Send_data1 Return '******************************************************************* ' Talk error message '******************************************************************* Error_msg_talk: Restore Error_msg: Gosub Send_data Return '******************************************************************* ' Talk "Jikandesu" '******************************************************************* Alarm_talk: Restore Alarm: Gosub Send_data Return '******************************************************************* ' Alarm time set '******************************************************************* Alarm_set: Gosub Get_digital_switch If Err_flg = 0 Then Timer_hour = Temp_hour Timer_minute = Temp_minute Gosub Alarm_set_talk End If Return '******************************************************************* ' Talk alarm hour and minute '******************************************************************* Alarm_set_talk: Restore Alarm_set_msg: Gosub Send_data Msg = Str(timer_hour) Print Msg; Restore Time_set2: Gosub Send_data1 Msg = Str(timer_minute) Print Msg; Restore Time_set3: Gosub Send_data1 Return '******************************************************************* ' Message data ' Top byte is length of message '******************************************************************* Time_talk1: Data 34, Data &H82 , &HB0 , &H27 , &H82 , &HF1 , &H82 , &HB4 , &H82 , &HA2 , &H82 , Data &HCC , &H81 , &H41 , &H82 , &HB6 , &H27 , &H82 , &HB1 , &H82 , &HAD , Data &H82 , &HED , &H81 , &H41 , &H3C , &H4E , &H55 , &H4D , &H4B , &H20 , Data &H56 , &H41 , &H4C , &H3D Time_talk2: Data 23 , Data &H20 , &H43 , &H4F , &H55 , &H4E , &H54 , &H45 , &H52 , &H3D , &H82 , Data &HB6 , &H3E , &H3B , &H3C , &H4E , &H55 , &H4D , &H4B , &H20 , &H56 , Data &H41 , &H4C , &H3D Time_talk3: Data 23, Data &H20 , &H43 , &H4F , &H55 , &H4E , &H54 , &H45 , &H52 , &H3D , &H82 , Data &HD3 , &H82 , &HF1 , &H3E , &H2F , &H82 , &HC5 , &H27 , &H82 , &HB7 , Data &H81 , &H42 , &H0D Time_set1: 'talk time set Data 21 , Data &H82 , &HB6 , &H27 , &H82 , &HB1 , &H82 , &HAD , &H82 , &HF0 , &H81 , Data &H41 , &H3C , &H4E , &H55 , &H4D , &H4B , &H20 , &H56 , &H41 , &H4C , Data &H3D Time_set2: Data 23 , Data &H20 , &H43 , &H4F , &H55 , &H4E , &H54 , &H45 , &H52 , &H3D , &H82 , Data &HB6 , &H3E , &H3B , &H3C , &H4E , &H55 , &H4D , &H4B , &H20 , &H56 , Data &H41 , &H4C , &H3D , Time_set3: Data 37 , Data &H20 , &H43 , &H4F , &H55 , &H4E , &H54 , &H45 , &H52 , &H3D , &H82 , Data &HD3 , &H82 , &HF1 , &H3E , &H2F , &H82 , &HC9 , &H82 , &HB9 , &H82 , Data &HC1 , &H82 , &HC4 , &H82 , &HA2 , &H82 , &HB5 , &H82 , &HDC , &H82 , Data &HB5 , &H27 , &H82 , &HBD , &H81 , &H42 , &H0D Just_hour1: 'talk every one hour Data 10 , Data &H3C , &H4E , &H55 , &H4D , &H4B , &H20 , &H56 , &H41 , &H4C , &H3D Just_hour2: Data 21 , Data &H20 , &H43 , &H4F , &H55 , &H4E , &H54 , &H45 , &H52 , &H3D , &H82 , Data &HB6 , &H3E , &H2F , &H82 , &HC5 , &H27 , &H82 , &HB7 , &H81 , &H42 , Data &H0D Error_msg: Data 23 , Data &H82 , &HB9 , &H82 , &HC1 , &H82 , &HC4 , &H82 , &HA2 , &H2F , &H82 , Data &HA6 , &H27 , &H82 , &HE7 , &H81 , &H5B , &H82 , &HC5 , &H82 , &HB7 , Data &H81 , &H42 , &H0D Alarm: Data 38 , Data &H82 , &HB9 , &H82 , &HC1 , &H82 , &HC4 , &H82 , &HA2 , &H82 , &HB5 , Data &H82 , &HBD , &H81 , &H41 , &H82 , &HB6 , &H27 , &H82 , &HB1 , &H82 , Data &HAD , &H82 , &HC9 , &H2F , &H82 , &HC8 , &H82 , &HE8 , &H82 , &HDC , Data &H82 , &HB5 , &H27 , &H82 , &HBD , &H81 , &H42 , &H0D Alarm_set_msg: Data 29 , Data &H82 , &HA0 , &H82 , &HE7 , &H81 , &H5B , &H82 , &HDE , &H82 , &HB6 , Data &H27 , &H82 , &HB1 , &H82 , &HAD , &H82 , &HF0 , &H81 , &H41 , &H3C , Data &H4E , &H55 , &H4D , &H4B , &H20 , &H56 , &H41 , &H4C , &H3D