'******************************************************************* ' Program name simple_clock ' Start Date 28th.Feb.2010 ' Author Yoshio KATO ' Description : Used MCU ATtiny2313 ' : System Clock 12.8MHz External OSC ' : Used 7 segments LED display HH:MM '******************************************************************* $regfile = "attiny2313.dat" $crystal = 12800000 Dim Hour As Byte Dim Minute As Byte Dim Second As Byte Dim Tmcount As Byte Dim Tmconst As Byte Dim Segdata(10) As Byte Dim Digit As Byte Dim Wk As Byte Const Digit1 = 3 'PD3 Const Digit2 = 4 'PD4 Const Digit3 = 5 'PD5 Const Digit4 = 6 Const Min_adj_sw = 0 'PD0 Const Hour_adj_sw = 1 'PD1 Main: Stop Ac 'analog comparater power off Config Portb = Output Config Portd = &B11111000 'Output PD3-PD7 Config Int0 = Falling 'PD2 hardware interrup falling edge On Int0 Zeroset 'hardware interrupt routine 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 Portd = &B11111111 'PD0,PD1,PD2 pull up '7segment data Segdata(1) = &HC0 ' 0 a,b,c,d,e,f Segdata(2) = &HF9 ' 1 b,c Segdata(3) = &HA4 ' 2 a,b,d,e,g Segdata(4) = &HB0 ' 3 a,b,c,d,g Segdata(5) = &H99 ' 4 b,c,f,g Segdata(6) = &H92 ' 5 a,c,d,f,g Segdata(7) = &H82 ' 6 a,c,d,e,f,g Segdata(8) = &HD8 ' 7 a,b,c,f Segdata(9) = &H80 ' 8 a,b,c,d,e,f,g Segdata(10) = &H90 ' 9 a,b,c,d,f,g 'all segment off Portb = &HFF Set Portd.digit1 Set Portd.digit2 Set Portd.digit3 Set Portd.digit4 'parameter clear Tmcount = 0 Second = 0 Minute = 0 Hour = 0 Tmconst = 100 'ever loop Do Wk = Minute Mod 10 Digit = Digit1 Gosub Dispdata 'PORTB = Segdata(wk + 1) Wk = Minute / 10 Digit = Digit2 Gosub Dispdata 'PORTB = Segdata(wk + 1) Wk = Hour Mod 10 Digit = Digit3 Gosub Dispdata 'PORTB = Segdata(wk + 1) Wk = Hour / 10 Digit = Digit4 Gosub Dispdata 'PORTB = Segdata(wk + 1) Loop End '******************************************************************* ' Out 7 segments data to PORTD ' Select digit by digit data '******************************************************************* Dispdata: Set Portd.digit1 Set Portd.digit2 Set Portd.digit3 Set Portd.digit4 Waitus 100 If Tmcount <= 50 Then 'second LED on off Portb = Segdata(wk + 1) And &H7F 'second LED off Else Portb = Segdata(wk + 1) Or &H80 'second LED on End If Reset Portd.digit Waitms 5 Return '******************************************************************* ' Timer0 interrupt ' Interrupt ever 10ms '******************************************************************* Tmovf: Incr Tmcount If Pind.min_adj_sw = 0 Or Pind.hour_adj_sw = 0 Then 'time adjust sw pushed ? Tmconst = 50 'yes time adjust sw was pushed Else Tmconst = 100 'normal count End If If Tmcount >= Tmconst Then Tmcount = 0 If Pind.min_adj_sw = 0 Then 'minute adjust sw pushed? Incr Minute 'increment minute If Minute = 60 Then 'minute over 60minute ? Minute = 0 End If Else If Pind.hour_adj_sw = 0 Then 'hour adjust sw pushed ? Incr Hour 'increment hour If Hour = 24 Then 'hor over 24hour ? Hour = 0 End If Else Incr Second End If '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 End If End If End If Return '******************************************************************* ' Hardware interrupt routine zero set ' Zero adjust sw was pushed ' Clear Tmcount and Second '******************************************************************* Zeroset: Tmcount = 0 'clear tmcount value Second = 0 'clear second value Return