'///////////////////////////////////////////////////////////////////////////////
'/                                                                             /
'/ ?M8?LM2576??????????                                            /
'/ ???? 0-30V                                                              /
'/ ?????? 3.0A                                                           /
'/ ???????? 0-3.0A ????                                            /
'/ ????:???                                                            /
'/         junsheng427@126.com  20081003 ????                              /
'///////////////////////////////////////////////////////////////////////////////




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

Config Lcdpin = Pin , Db4 = Portb.2 , Db5 = Portb.3 , Db6 = Portb.4 , Db7 = Portb.5 , E = Portb.0 , Rs = Portd.2
Config Lcd = 16 * 2
'??LED?16??,2?

Config Adc = Single , Prescaler = Auto , Reference = Internal
'??ADC???????,????-??,??????

Config Timer1 = Pwm , Pwm = 10 , Compare A Pwm = Clear Down , Prescale = 1
'PWM1A??,??????,

Ddrb.1 = 1                                                  'PWM1A????

'??????? ////////////////////////////////////////////////////////////////

Dim V_dc As Long , V_dc1 As Long , A_dc As Long , A_dc1 As Long

Dim V1_dc As Word , V2_dc As Word
Dim A1_dc As Word , A2_dc As Word , A As Word , C As Word , A1 As Word

Dim V1 As String * 5
Dim V2 As String * 5
Dim V3 As String * 5
Dim V4 As String * 5
Dim C1 As String * 4
Dim Va1 As Byte

Dim N As Byte , B2 As Byte , B3 As Byte , B4 As Byte , B5 As Byte

'?????/////////////////////////////////////////////////////////////////////

   Ddrd.3 = 0                                               '??PD.3~PD.7???,?????? .?????
   Ddrd.4 = 0
   Ddrd.5 = 0
   Ddrd.6 = 0
   Ddrd.7 = 0

   Portd.3 = 1
   Portd.4 = 1
   Portd.5 = 1
   Portd.6 = 1
   Portd.7 = 1


   Start Timer1
   Start Adc

   Cls
   Cursor Off

   Locate 1 , 14
   Lcd "ON-"

   A = 0
   C = 3000

   Ddrc.4 = 1                                               '???????
   Portc.4 = 0


'???/////////////////////////////////////////////////////////////////////////

Do

   Pwm1a = A

   N = 1
   V1_dc = 0
   V_dc = 0
   V2_dc = 0
   V_dc1 = 0
   A1_dc = 0
   A_dc = 0
   A2_dc = 0
   A_dc1 = 0

   For N = 1 To 60                                          'ADC??60?

      V1_dc = Getadc(0)                                     '??????
      V_dc = V_dc + V1_dc                                   'ADC??????

      A1_dc = Getadc(1)                                     '??300mA?
      A_dc = A_dc + A1_dc

      A2_dc = Getadc(2)                                     '??3A?
      A_dc1 = A_dc1 + A2_dc

      V2_dc = Getadc(3)                                     '??????
      V_dc1 = V_dc1 + V2_dc


   Next


      V_dc = V_dc / 60                                      '??????
      A_dc = A_dc / 60
      A_dc1 = A_dc1 / 60
      V_dc1 = V_dc1 / 60

      V_dc = 3040 * V_dc                                    '??3040
      V_dc = V_dc / 1023

      A_dc = 3120 * A_dc                                    '??3040
      A_dc = A_dc / 1023

      A_dc1 = 3040 * A_dc1
      A_dc1 = A_dc1 / 1023

      V_dc1 = 4025 * V_dc1
      V_dc1 = V_dc1 / 1023

   If A_dc>= C Then                                        '???????300mA?
      Waitms 500
         If A_dc>= C Then
            Portc.4 = 1
            Locate 1 , 14
            Lcd "OC "
            Gosub Mu
         End If
   End If

   If A_dc1>= C Then                                       '???????3A?
      Waitms 500
         If A_dc1>= C Then
            Portc.4 = 1
            Locate 1 , 14
            Lcd "OC "
            Gosub Mu
         End If
   End If

   If V_dc1 <= 1200 Then                                    '??????????
      Cls
      Lcd "Input U<12V"
      Portc.4 = 1
      Gosub Mu
   End If

   If V_dc1>= 3500 Then                                    '??????????
      Cls
      Lcd "Input U>35V"
      Portc.4 = 1
      Gosub Mu
   End If

   Gosub Lp_lcd
   Gosub Lp_key

Loop
End

'????///////////////////////////////////////////////////////////////////////

Lp_lcd:

   V1 = Str(v_dc)                                           '????
   Va1 = Len(v1)                                            '????
      If Va1 <= 3 Then
         V1 = Format(v1 , "0.00")                           '?????
         Else
         V1 = Format(v1 , " 0.00")
      End If

   V2 = Str(a_dc)                                           '??300mA?
   Va1 = Len(v2)
      If Va1 <= 2 Then
         V2 = Format(v2 , "0.0")
         Else
         V2 = Format(v2 , " 0.0")
      End If

   V3 = Str(a_dc1)                                          '??3A?
   V3 = Format(v3 , "0.000")

   V4 = Str(v_dc1)                                          '????
   V4 = Format(v4 , " 0")

   C1 = Str(c)                                              '????
   C1 = Format(c1 , "   0")


   Locate 1 , 8
   Lcd " "

   Locate 1 , 1
   Lcd "U:" ; V1 ; "V"

   Locate 2 , 1
   Lcd "I:"
      If A_dc> A_dc1 Then
         Locate 2 , 8
         Lcd "  "
         Locate 2 , 1
         Lcd "I:" ; V2 ; "mA"
      End If

      If A_dc < A_dc1 Then
         Locate 2 , 1
         Lcd "I:" ; V3 ; "A"
      End If

      If A_dc = A_dc1 Then
         Locate 2 , 3
         Lcd "0.0    "
      End If

   'Locate 1 , 9
   'Lcd V4

   Locate 2 , 10
   Lcd "<" ; C1 ; "mA"

Return

'????//////////////////////////////////////////////////////////////////////

Lp_key:

   If Pind.3 = 0 Then                                       '??
      Waitms 20
        If Pind.3 = 0 And Portc.4 = 1 Then
           Portc.4 = 0                                      'Portc.4??LM2576?/?
           Locate 1 , 14
           Lcd "ON "
           Waitms 250
     End If
   End If

   If Pind.3 = 0 Then                                       '??
      Waitms 20
        If Pind.3 = 0 And Portc.4 = 0 Then
           Portc.4 = 1                                      '??LM2576?/?
           Locate 1 , 14
           Lcd "OFF"
        End If
   End If



   If Pind.4 = 1 Then B2 = 0                                '??+

   If Pind.4 = 0 And Portc.4 = 0 Then
      Waitms 20
        If Pind.4 = 0 Then
          If A < 1023 Then
             A = A + 1
             B2 = B2 + 1
              If B2> 20 And A < 1013 Then
              A = A + 10
              End If
          End If
        End If
   End If

   If Pind.5 = 1 Then B3 = 0                                '??-

   If Pind.5 = 0 And Portc.4 = 0 Then
      Waitms 20
        If Pind.5 = 0 Then
          If A> 1 Then
            A = A - 1
            B3 = B3 + 1
             If B3> 20 And A> 10 Then
             A = A - 10
             End If
          End If
        End If
   End If

   If Pind.6 = 1 Then B4 = 0                                '??+

   If Pind.6 = 0 Then
      Waitms 20
        If Pind.6 = 0 Then
          If C < 3000 Then
            C = C + 1
            B4 = B4 + 1
             If B4> 20 And C < 2980 Then
             C = C + 20
             End If
          End If
        End If
   End If

   If Pind.7 = 1 Then B5 = 0                                '??-

   If Pind.7 = 0 Then
      Waitms 20
         If Pind.7 = 0 Then
           If C> 1 Then
             C = C - 1
             B5 = B5 + 1
             If B5> 20 And C> 20 Then
                C = C - 20
             End If
           End If
         End If
   End If

Return

'????///////////////////////////////////////////////////////////////////////

Mu:

   Sound Portc.5 , 100 , 1000
      Waitms 20
   Sound Portc.5 , 100 , 1000
      Waitms 20
   Sound Portc.5 , 100 , 1000

Return

'/////////////////////////////////////////////////////////////////////////////// 