$regfile = "m8def.dat"
$crystal = 14000000

$lib "ds1307clock.lib"
Config Sda = Portc.5
Config Scl = Portc.4

Declare Sub Update()
Declare Sub Progon()
Declare Sub Setcolor
Declare Sub Checklight
Declare Sub Indicator
Declare Sub Hc595

Dim Seco As Byte
Dim Mine As Byte
Dim Hour As Byte
Dim Dat As Byte
Dim Month As Byte
Dim Year As Byte

Dim Smine As Byte
Dim Shour As Byte
Dim Sdat As Byte
Dim Smonth As Byte
Dim Syear As Byte
Dim B As Byte
Dim C As Byte
Dim I As Byte
Dim P As Byte
Dim Q As Word
Dim W As Word
Dim A As Word
Dim K As Byte
Dim Show As Bit
Dim R As Single
Dim T As Single
Dim Y As Single
Dim U As Single



Const C_r = 0.5
Const C_g = 0.35
Const C_b = 0.35
'Const C_r = 1
'Const C_g = 1
'Const C_b = 1

Dim Red_1 As Eram Byte
Dim Red As Byte

Dim Green_1 As Eram Byte
Dim Green As Byte

Dim Blue_1 As Eram Byte
Dim Blue As Byte

Dim Light_1 As Eram Byte
Dim Light As Byte

Dim Powerind_1 As Eram Byte
Dim Powerind As Byte

Red = Red_1
Green = Green_1
Blue = Blue_1
Light = Light_1
Powerind = Powerind_1




Config Portd = Input


I2cstart
I2cwbyte &HD0
I2cwbyte 7
I2cwbyte 16
I2cstop


Config Timer0 = Timer , Prescale = 64
On Timer0 Hc595:


Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down , Prescale = 8

'On Timer1 Checklight:

Config Timer2 = Pwm , Compare Pwm = Clear Up , Prescale = 8

'On Timer2 Checklight:



                                      'датчик смк pind3
Config Int0 = Falling
Config Debounce = 70
Const Debouncetime = 30

On Int0 Update




Config Portb = Output
'Set Portb.2
'Gosub Hc595
Config Portc.0 = Output
Config Portc.1 = Output
Config Portc.2 = Output





Enable Interrupts
Enable Int0
Enable Timer0
Enable Timer1
Enable Timer2


Call Progon




Do

'Call Progon
'Call Hc595
'Call Checklight

                             '?????? ?? ???????????? ???????? ??????
Loop


Hc595:

I2cstart
I2cwbyte &HD0
I2cwbyte &H00
I2cstart
I2cwbyte &HD1
I2crbyte Seco , Ack
I2crbyte Mine , Ack
I2crbyte Hour , Ack
I2crbyte Dat , Ack
I2crbyte Dat , Ack
I2crbyte Month , Ack
I2crbyte Year , Nack
I2cstop                                                     '???????????? ???????? ??????



If Show = 0 Then
                                  '???????????? ???????? ??????

Shiftout Portb.4 , Portb.0 , Seco , 3 , 8 , 3
Shiftout Portb.4 , Portb.0 , Mine , 3 , 8 , 3
Shiftout Portb.4 , Portb.0 , Hour , 3 , 8 , 3
                                                '?????????? ?????? ? ???????


Else
                                '???????????? ???????? ??????

Shiftout Portb.4 , Portb.0 , Dat , 3 , 8 , 3
Shiftout Portb.4 , Portb.0 , Month , 3 , 8 , 3
Shiftout Portb.4 , Portb.0 , Year , 3 , 8 , 3


End If


                         '?????????? ?????? ? ???????
Pulseout Portb , 5 , 5



If Hour = 0 And Mine = 0 Then
Call Progon
End If


If Hour = 18 And Mine = 0 Then
Call Progon
End If


Call Checklight

Call Indicator
                                                '??????????? ??????
Return




Indicator:

Incr K
B = K Mod Powerind
If B = 0 Then

K = 0
Set Portc.2

Else

Reset Portc.2

End If


Return








Update:

Set Portc.0
Waitms Debouncetime
Reset Portc.0







If Pind.7 = 0 Then

If Show = 0 Then

If Smine > 59 Then Smine = 0

B = Makebcd(smine)

I2cstart
I2cwbyte &HD0
I2cwbyte 0
I2cwbyte 0
I2cwbyte B
I2cstop

Incr Smine


Else

If Sdat > 31 Then Sdat = 1

B = Makebcd(sdat)

I2cstart
I2cwbyte &HD0
I2cwbyte 4
I2cwbyte B
I2cstop

Incr Sdat


End If


End If





If Pind.6 = 0 Then

If Show = 0 Then

If Shour > 23 Then Shour = 0

B = Makebcd(shour)

I2cstart
I2cwbyte &HD0
I2cwbyte 2
I2cwbyte B
I2cstop

Incr Shour


Else

If Smonth > 12 Then

Smonth = 1

If Syear > 23 Then Syear = 0

B = Makebcd(syear)

I2cstart
I2cwbyte &HD0
I2cwbyte 6
I2cwbyte B
I2cstop

Incr Syear


End If


B = Makebcd(smonth)

I2cstart
I2cwbyte &HD0
I2cwbyte 5
I2cwbyte B
I2cstop

Incr Smonth



End If


End If







If Pind.0 = 0 Then


Enable Timer0


Toggle Show


End If




If Pind.1 = 0 Then


If Show = 0 Then
Incr Light
If Light > 3 Then Light = 0
Light_1 = Light
Else
Incr Powerind
If Powerind > 8 Then Powerind = 1
Powerind_1 = Powerind
End If

End If









If Pind.5 = 0 Then

If Light = 0 Or Light = 2 Then

Disable Timer0

If Red < 251 Then Red = Red + 5 Else Red = 0


'Pwm1a = Red
Red_1 = Red
C = Red Mod 10
'C = C + 240
Shiftout Portb.4 , Portb.0 , C , 3 , 8 , 3
C = Red Mod 100
C = C / 10
'C = C + 240
Shiftout Portb.4 , Portb.0 , C , 3 , 8 , 3
C = Red / 100
'C = C + 240
Shiftout Portb.4 , Portb.0 , C , 3 , 8 , 3

Pulseout Portb , 5 , 5
'Waitms 200
Set Portc.2
Call Checklight
End If
End If






If Pind.4 = 0 Then

If Light = 0 Or Light = 2 Then

Disable Timer0

If Green < 251 Then Green = Green + 5 Else Green = 0


'Pwm1b = Green
Green_1 = Green
C = Green Mod 10
'C = C + 240
Shiftout Portb.4 , Portb.0 , C , 3 , 8 , 3
C = Green Mod 100
C = C / 10
'C = C + 240
Shiftout Portb.4 , Portb.0 , C , 3 , 8 , 3
C = Green / 100
'C = C + 240
Shiftout Portb.4 , Portb.0 , C , 3 , 8 , 3

Pulseout Portb , 5 , 5
'Waitms 200
Set Portc.2
Call Checklight
End If
End If






If Pind.3 = 0 Then

If Light = 0 Or Light = 2 Then

Disable Timer0

If Blue < 251 Then Blue = Blue + 5 Else Blue = 0


'Pwm2 = Blue
Blue_1 = Blue
C = Blue Mod 10
'C = C + 240
Shiftout Portb.4 , Portb.0 , C , 3 , 8 , 3
C = Blue Mod 100
C = C / 10
'C = C + 240
Shiftout Portb.4 , Portb.0 , C , 3 , 8 , 3
C = Blue / 100
'C = C + 240
Shiftout Portb.4 , Portb.0 , C , 3 , 8 , 3

Pulseout Portb , 5 , 5
'Waitms 200
Set Portc.2
Call Checklight
End If
End If


'Set Portc.0
'Waitms Debouncetime
'Reset Portc.0

Gifr = 64


Return




Progon:

Disable Interrupts


For I = 0 To 9
For K = 0 To 9

C = I * 17



Shiftout Portb.4 , Portb.0 , C , 3 , 8 , 5
Shiftout Portb.4 , Portb.0 , C , 3 , 8 , 5
Shiftout Portb.4 , Portb.0 , C , 3 , 8 , 5
Set Portc.2
Pulseout Portb , 5 , 5

Waitms 800



Reset Portc.2


Waitms 800

Next
Next

Enable Interrupts


Return




Sub Setcolor



Q = Makedec(hour) * 1000
W = Makedec(mine) * 16
A = Q + W
Y = A * 1.0625
Y = Y / 100
I = Round(y)



'R = Sin(e)

'R = Y * 3.14 'полусинус
'R = R / 170  'полусинус

R = Y * 3.14                                                'треуг
R = R / 170                                                 'треуг

'R = Y * 3.14                                                'синус
'R = R / 128                                                 'синус

'**********************************************************************
If I >= 0 And I < 85 Then
'***********************************************************************


'green  //////////////////////////////////////////////////////////////////


'U = R                                                       'полусинус
'T = 255 * Sin(u)                                            'полусинус

T = 3 * I                                                   '  треуг

'U = R                                                       'синус
'U = Sin(u)                                                  'синус
'U = 1 + U                                                   'синус
'T = 127 * U                                                 'синус


T = T * C_g
B = Round(t)
Pwm1b = B


'red  /////////////////////////////////////////////////////////////////


'Y = 3.14 / 2                                                'полусинус
'U = R + Y                                                   'полусинус
'T = 255 * Sin(u)                                            'полусинус

U = 85 - I                                                  ' треуг
T = 3 * U                                                   '  треуг

'U = R + 2.36                                                'синус
'U = Sin(u)                                                  'синус
'U = 1 + U                                                   'синус
'T = 127 * U                                                 'синус


T = T * C_r
B = Round(t)
Pwm1a = B


'blue /////////////////////////////////////////////////////////////////

'T = 0                                                       'полусинус

T = 0                                                       '  треуг

'U = R + 3.93                                                'синус
'U = Sin(u)                                                  'синус
'U = 1 + U                                                   'синус
'T = 127 * U                                                 'синус

T = T * C_b
B = Round(t)
Pwm2 = B


End If



'*****************************************************************
If I >= 85 And I < 170 Then
'*****************************************************************


'green /////////////////////////////////////////////////////////

'U = R                                                       'полусинус
'T = 255 * Sin(u)                                            'полусинус

U = 170 - I                                                 ' треуг
T = 3 * U                                                   '  треуг

'U = R                                                       'синус
'U = Sin(u)                                                  'синус
'U = 1 + U                                                   'синус
'T = 127 * U                                                 'синус


T = T * C_g
B = Round(t)
Pwm1b = B


'red //////////////////////////////////////////////////////////

'T = 0                                                       'полусинус

T = 0                                                       '  треуг

'U = R + 2.36                                                'синус
'U = Sin(u)                                                  'синус
'U = 1 + U                                                   'синус
'T = 127 * U                                                 'синус

T = T * C_r
B = Round(t)
Pwm1a = B


'blue  ////////////////////////////////////////////////////////

'Y = 3.14 / 2                                                'полусинус
'U = R - Y                                                   'полусинус
'T = 255 * Sin(u)                                            'полусинус

U = I - 85                                                  ' треуг
T = 3 * U                                                   '  треуг

'U = R + 3.93                                                'синус
'U = Sin(u)                                                  'синус
'U = 1 + U                                                   'синус
'T = 127 * U                                                 'синус


T = T * C_b
B = Round(t)
Pwm2 = B


End If



'**************************************************************
If I >= 170 And I <= 255 Then
'*****************************************************************

'green /////////////////////////////////////////////////////////////

'T = 0                                                       'полусинус

T = 0                                                       '  треуг


'U = R                                                       'синус
'U = Sin(u)                                                  'синус
'U = 1 + U                                                   'синус
'T = 127 * U                                                 'синус


T = T * C_g
B = Round(t)

Pwm1b = B


'red  //////////////////////////////////////////////////////////////

'U = R - 3.14                                                'полусинус
'T = 255 * Sin(u)                                            'полусинус

U = I - 170                                                 ' треуг
T = 3 * U                                                   '  треуг

'U = R + 2.36                                                'синус
'U = Sin(u)                                                  'синус
'U = 1 + U                                                   'синус
'T = 127 * U                                                 'синус

T = T * C_r
B = Round(t)
Pwm1a = B


'blue /////////////////////////////////////////////////////////////////

'Y = 3.14 / 2                                                'полусинус
'U = R - Y                                                   'полусинус
'T = 255 * Sin(u)                                            'полусинус

U = 255 - I                                                 ' треуг
T = 3 * U                                                   '  треуг

'U = R + 3.93                                                'синус
'U = Sin(u)                                                  'синус
'U = 1 + U                                                   'синус
'T = 127 * U                                                 'синус

T = T * C_b
B = Round(t)
Pwm2 = B


End If

'Pwm1a = Pwm1a * 0.5
'Pwm2 = 0
'Pwm1b = Pwm1b * 0.5                                         'red


End Sub






Sub Checklight


'Disable Interrupts

Select Case Light
  Case 0 :
   Reset Portc.1
   U = Red * C_r
   B = Round(u)
   Pwm1a = B
   U = Green * C_g
   B = Round(u)
   Pwm1b = B
   U = Blue * C_b
   B = Round(u)
   Pwm2 = B
  Case 1 :
   Reset Portc.1
   Call Setcolor
  Case 2 :
   Set Portc.1
   U = Red * C_r
   B = Round(u)
   Pwm1a = B
   U = Green * C_g
   B = Round(u)
   Pwm1b = B
   U = Blue * C_b
   B = Round(u)
   Pwm2 = B
  Case 3 :
   Set Portc.1
   Call Setcolor
End Select


'Enable Interrupts

End Sub



End