Srccodetst

(Difference between revisions)
Jump to: navigation, search
Line 3: Line 3:
 
'                  Thomas Jensen | uCtrl.net
 
'                  Thomas Jensen | uCtrl.net
 
'--------------------------------------------------------------
 
'--------------------------------------------------------------
'  file: KEYPAD_v.1.0
+
'  file: AVR_LIGHT_SENSOR_v1.0
'  date: 17/11/2007
+
'  date: 18/03/2007
 
'--------------------------------------------------------------
 
'--------------------------------------------------------------
$regfile = "attiny2313.dat"
+
$regfile = "m8def.dat"
$crystal = 8000000
+
$crystal = 1000000
Config Portd = Input
+
Config Portb = Output
+
 
Config Watchdog = 1024
 
Config Watchdog = 1024
 +
Config Portb = Output
 +
Config Portd = Output
 +
Config Portc = Input
 +
 +
Dim W As Word , Volt As Word , Read_timer As Integer
 +
Dim C_limit1 As Integer , C_limit2 As Integer
 +
Dim Over_limit As Bit , Limit As Integer
 +
Dim Volt_temp As String * 1 , Volt_string As String * 3
 +
Dim Volt_number As Integer , Set_timer As Integer
 +
Dim Over_limit_set As Bit , Action_timer As Integer
 +
Dim Up_counter As Integer , Down_counter As Integer
 +
Dim Eeprom_limit As Eram Integer , Eeprom_save As Integer
 +
Dim Lowlimit As Integer , Highlimit As Integer
  
Dim Lifesignal As Byte , Number As Byte , Row As Byte , Button As Byte
+
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Dim Code1 As Integer , Code2 As Integer , Code3 As Byte , Code4 As Byte , Nr As Byte
+
Start Adc
Dim Redled As Byte , Greenled As Byte , Amberled As Byte , Code As Integer
+
Dim Code_p As Integer , Kanal1 As Byte , Kanal2 As Byte , Timeout As Byte , Feil As Integer
+
Dim Eeprom As Eram Integer , Program As Byte , Feil_a As Byte
+
  
 
'Inputs
 
'Inputs
'PD2 Keypad R1
+
'PC.0 Light sensor, analog value
'PD3 Keypad R2
+
'PC.1 Threshold up
'PD4 Keypad R3
+
'PC.2 Threshold down
'PD5 Keypad R4
+
'PC.3 Over/under threshold
  
 
'Outputs
 
'Outputs
'PB0 Keypad K1
+
'PB.0 LED-display digit 1
'PB1 Keypad K2
+
'PB.1 LED-display digit 2
'PB2 Keypad K3
+
'PB.2 LED-display digit 3
'PB3 Mode 1 signal
+
'PB.3 Output constant
'PB4 Mode 2 signal
+
'PB.4 Output pulse
'PB5 Green LED indicator
+
'PortD LED display
'PB6 Red LED indicator
+
'PB7 Lifesignal
+
  
Code_p = Eeprom                                            'Get stored code
+
'get eeprom values
If Code_p < 0 Then Code_p = 1234                            'If no code use default
+
Limit = Eeprom_limit
Number = 255
+
  
'Boot
+
Portd = 64
Portb = 0
+
Portb.0 = 1
 +
Portb.1 = 0
 +
Portb.2 = 0
 +
Waitms 500
 +
Portb.0 = 0
 +
Portb.1 = 1
 +
Waitms 500
 +
Portb.1 = 0
 +
Portb.2 = 1
 +
Waitms 500
 +
Portb.2 = 0
 +
Waitms 500
  
 
Start Watchdog
 
Start Watchdog
  
 +
'program start
 
Main:
 
Main:
'Read keypad
+
'read light
If Button = 0 And Feil = 0 And Number = 255 Then
+
If Read_timer = 0 Then
 +
W = Getadc(0)
 +
Volt = W
 +
Read_timer = 666
 +
End If
  
If Row = 0 Then                                            'Keyboard row 1
+
'set limit up
  Portb.0 = 1
+
If Pinc.1 = 1 Then
  If Pind.5 = 1 Then Number = 10
+
   C_limit1 = 0
  If Pind.4 = 1 Then Number = 7
+
   Up_counter = 0
   If Pind.3 = 1 Then Number = 4
+
   If Pind.2 = 1 Then Number = 1
+
  Portb.0 = 0
+
 
   End If
 
   End If
 
+
If Pinc.1 = 0 Then
If Row = 1 Then                                            'Keyboard row 2
+
   If C_limit1 = 0 Then Incr Limit
  Portb.1 = 1
+
   C_limit1 = 1
   If Pind.5 = 1 Then Number = 0
+
   Incr Up_counter
   If Pind.4 = 1 Then Number = 8
+
   Eeprom_save = 20000
   If Pind.3 = 1 Then Number = 5
+
   Set_timer = 133
   If Pind.2 = 1 Then Number = 2
+
   Portb.1 = 0
+
 
   End If
 
   End If
  
If Row = 2 Then                                            'Keyboard row 3
+
'set limit down
  Portb.2 = 1
+
If Pinc.2 = 1 Then
   If Pind.5 = 1 Then Number = 11
+
   C_limit2 = 0
   If Pind.4 = 1 Then Number = 9
+
  Down_counter = 0
   If Pind.3 = 1 Then Number = 6
+
   End If
   If Pind.2 = 1 Then Number = 3
+
If Pinc.2 = 0 Then
   Portb.2 = 0
+
   If C_limit2 = 0 Then Decr Limit
 +
   C_limit2 = 1
 +
  Incr Down_counter
 +
  Eeprom_save = 20000
 +
   Set_timer = 133
 
   End If
 
   End If
  
Row = Row + 1                                               'Next row
+
'correct if value out of range
If Row > 2 Then Row = 0
+
If Limit < 0 Then Limit = 0
 +
If Limit > 999 Then Limit = 999
 +
If Volt > 999 Then Volt = 999
 +
 
 +
'check limit and set conditions
 +
Lowlimit = Limit - 25
 +
Highlimit = Limit + 25
 +
 
 +
If Pinc.3 = 0 Then
 +
  If Volt > Highlimit Then Over_limit = 1
 +
  If Volt < Lowlimit Then Over_limit = 0
 +
  Else
 +
  If Volt < Lowlimit Then Over_limit = 1
 +
  If Volt > Highlimit Then Over_limit = 0
 
End If
 
End If
  
'Reset button status
+
'show light level/threshold
 +
If Set_timer = 0 Then Volt_string = Str(volt)
 +
If Set_timer > 0 Then Volt_string = Str(limit)
 +
 
 +
'digit 1
 +
If Len(volt_string) > 2 Then Volt_temp = Left(volt_string , 1) Else Volt_temp = "0"
 +
Volt_number = Val(volt_temp)
 +
Portd = Lookup(volt_number , Table)
 +
If Set_timer > 0 Then Portd.7 = 1
 +
If Len(volt_string) > 3 Then Portd = 121
 
Portb.0 = 1
 
Portb.0 = 1
 +
Portb.1 = 0
 +
Portb.2 = 0
 +
Waitms 5
 +
 +
Portb.0 = 0
 +
Portb.1 = 0
 +
Portb.2 = 0
 +
 +
'digit 2
 +
If Len(volt_string) = 2 Then Volt_temp = Left(volt_string , 1)
 +
If Len(volt_string) > 2 Then Volt_temp = Mid(volt_string , 2 , 1)
 +
If Len(volt_string) < 2 Then Volt_temp = "0"
 +
Volt_number = Val(volt_temp)
 +
Portd = Lookup(volt_number , Table)
 +
If Over_limit = 1 Then Portd.7 = 1
 +
If Len(volt_string) > 3 Then Portd = 80
 +
Portb.0 = 0
 
Portb.1 = 1
 
Portb.1 = 1
Portb.2 = 1
+
Portb.2 = 0
If Pind.2 = 0 And Pind.3 = 0 And Pind.4 = 0 And Pind.5 = 0 Then Button = 0
+
Waitms 5
 +
 
 
Portb.0 = 0
 
Portb.0 = 0
 
Portb.1 = 0
 
Portb.1 = 0
 
Portb.2 = 0
 
Portb.2 = 0
  
'Get code
+
'digit 3
If Number <> 255 Then
+
Volt_temp = Right(volt_string , 1)
Button = 1
+
Volt_number = Val(volt_temp)
Select Case Nr
+
Portd = Lookup(volt_number , Table)
  Case 0                                                  'Digit 1
+
If Read_timer > 600 And Eeprom_save = 0 Then Portd.7 = 1
  Code1 = Number
+
If Read_timer < 600 And Eeprom_save > 0 Then Portd.7 = 1
  Code1 = Code1 * 1000
+
If Len(volt_string) > 3 Then Portd = 80
  Code = Code1 + Code
+
Portb.0 = 0
  Amberled = 4
+
Portb.1 = 0
  Case 1                                                   'Digit 2
+
Portb.2 = 1
  Code2 = Number
+
Waitms 5
  Code2 = Code2 * 100
+
  Code = Code2 + Code
+
  Amberled = 4
+
  Case 2                                                   'Digit 3
+
  Code3 = Number
+
  Code3 = Code3 * 10
+
  Code = Code3 + Code
+
  Amberled = 4
+
  Case 3                                                  'Digit 4
+
  Code4 = Number
+
  Code = Code4 + Code
+
  Amberled = 4
+
  
  Case 4
+
Portb.0 = 0
  If Program = 1 Then                                      'Program new code
+
Portb.1 = 0
      Select Case Number
+
Portb.2 = 0
        Case 11                                            'Accept
+
        Code_p = Code
+
        Eeprom = Code_p
+
        Greenled = 60
+
        Case Else                                          'Reject
+
        Redled = 30
+
        End Select
+
      Number = 255
+
      Program = 0
+
      Gosub Reset_code_read
+
      Goto Main
+
      End If
+
  If Code = Code_p Then                                    'Code correct
+
  Feil_a = 0
+
      Select Case Number
+
        Case 0                                            'New code
+
        Program = 1
+
        Amberled = 40
+
        Case 10                                            'Channel 1
+
        Kanal1 = 20
+
        Greenled = 20
+
        Case 11                                            'Channel 2
+
        Kanal2 = 20
+
        Greenled = 40
+
        Case Else                                          'Reject
+
        Redled = 30
+
        End Select
+
  Else                                                    'Code failed
+
      Redled = 30
+
      Feil_a = Feil_a + 1
+
  End If
+
  Timeout = 0
+
  Number = 255
+
  Gosub Reset_code_read
+
  Goto Main
+
End Select
+
Nr = Nr + 1
+
Number = 255
+
End If
+
  
'Timeout when entering code
+
'set outputs
If Nr > 0 And Timeout = 0 Then Timeout = 201
+
If Over_limit = 1 And Over_limit_set = 0 Then
If Timeout > 0 Then Timeout = Timeout - 1
+
  Over_limit_set = 1
If Timeout = 1 And Nr <> 0 Then
+
  Portb.3 = 1
  Gosub Reset_code_read
+
   Action_timer = 33
  Number = 255
+
   Redled = 30
+
 
   End If
 
   End If
 
+
If Over_limit = 0 Then
'Error handling, 3 wrong codes
+
   If Over_limit_set = 1 Then Action_timer = 100
If Feil_a > 3 And Feil = 0 Then
+
   Over_limit_set = 0
   Feil = 1200
+
  Portb.3 = 0
   Feil_a = 0
+
 
   End If
 
   End If
If Feil > 0 And Redled = 0 Then Redled = 4
 
If Feil > 0 Then Feil = Feil - 1
 
  
'AmberLED
+
'pulse output
If Amberled > 0 Then
+
If Action_timer > 0 Then
   Greenled = Amberled
+
   Decr Action_timer
   Redled = Amberled
+
   Portb.4 = 1
  Amberled = 0
+
 
   End If
 
   End If
 +
If Action_timer = 0 Then Portb.4 = 0
  
'GreenLED
+
'timers
If Greenled > 2 Then Portb.5 = 1
+
If Set_timer > 0 Then Decr Set_timer
If Greenled = 2 Then Portb.5 = 0
+
If Read_timer > 0 Then Decr Read_timer
If Greenled > 0 Then Greenled = Greenled - 1
+
  
'RedLED
+
'fast threshold adjust
If Redled > 2 Then Portb.6 = 1
+
If Up_counter > 66 Then Incr Limit
If Redled = 2 Then Portb.6 = 0
+
If Down_counter > 66 Then Decr Limit
If Redled > 0 Then Redled = Redled - 1
+
  
'Channel1
+
'save eeprom values
If Kanal1 = 20 Then Portb.3 = 1
+
If Eeprom_save > 0 Then Decr Eeprom_save
If Kanal1 = 1 Then Portb.3 = 0
+
If Eeprom_save = 1 Then Eeprom_limit = Limit
If Kanal1 > 0 Then Kanal1 = Kanal1 - 1
+
 
+
'Channel2
+
If Kanal2 = 20 Then Portb.4 = 1
+
If Kanal2 = 1 Then Portb.4 = 0
+
If Kanal2 > 0 Then Kanal2 = Kanal2 - 1
+
 
+
'Lifesignal
+
If Lifesignal > 0 Then Lifesignal = Lifesignal - 1
+
If Lifesignal = 12 Then Portb.7 = 1
+
If Lifesignal = 1 Then Portb.7 = 0
+
If Lifesignal = 0 Then Lifesignal = 41
+
  
 +
'loop
 
Reset Watchdog
 
Reset Watchdog
Waitms 50
+
Goto Main
Goto Main                                                   'Loop cycle
+
 
End
 
End
  
Reset_code_read:                                            'Reset code param.
+
'---- data for correct display of numbers on LED display ------
  Nr = 0
+
Table:
  Code = 0
+
Data 63 , 6 , 91 , 79 , 102 , 109 , 125 , 7 , 127 , 111 , 128
  Code1 = 0
+
'    0    1  2   3   4    5    6    7  8    9    dp
   Code2 = 0
+
   Code3 = 0
+
   Code4 = 0
+
Return
+
End
+
  
  
 
</source>
 
</source>

Revision as of 10:49, 9 March 2016

'--------------------------------------------------------------
'                   Thomas Jensen | uCtrl.net
'--------------------------------------------------------------
'  file: AVR_LIGHT_SENSOR_v1.0
'  date: 18/03/2007
'--------------------------------------------------------------
$regfile = "m8def.dat"
$crystal = 1000000
Config Watchdog = 1024
Config Portb = Output
Config Portd = Output
Config Portc = Input
 
Dim W As Word , Volt As Word , Read_timer As Integer
Dim C_limit1 As Integer , C_limit2 As Integer
Dim Over_limit As Bit , Limit As Integer
Dim Volt_temp As String * 1 , Volt_string As String * 3
Dim Volt_number As Integer , Set_timer As Integer
Dim Over_limit_set As Bit , Action_timer As Integer
Dim Up_counter As Integer , Down_counter As Integer
Dim Eeprom_limit As Eram Integer , Eeprom_save As Integer
Dim Lowlimit As Integer , Highlimit As Integer
 
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Start Adc
 
'Inputs
'PC.0 Light sensor, analog value
'PC.1 Threshold up
'PC.2 Threshold down
'PC.3 Over/under threshold
 
'Outputs
'PB.0 LED-display digit 1
'PB.1 LED-display digit 2
'PB.2 LED-display digit 3
'PB.3 Output constant
'PB.4 Output pulse
'PortD LED display
 
'get eeprom values
Limit = Eeprom_limit
 
Portd = 64
Portb.0 = 1
Portb.1 = 0
Portb.2 = 0
Waitms 500
Portb.0 = 0
Portb.1 = 1
Waitms 500
Portb.1 = 0
Portb.2 = 1
Waitms 500
Portb.2 = 0
Waitms 500
 
Start Watchdog
 
'program start
Main:
'read light
If Read_timer = 0 Then
W = Getadc(0)
Volt = W
Read_timer = 666
End If
 
'set limit up
If Pinc.1 = 1 Then
   C_limit1 = 0
   Up_counter = 0
   End If
If Pinc.1 = 0 Then
   If C_limit1 = 0 Then Incr Limit
   C_limit1 = 1
   Incr Up_counter
   Eeprom_save = 20000
   Set_timer = 133
   End If
 
'set limit down
If Pinc.2 = 1 Then
   C_limit2 = 0
   Down_counter = 0
   End If
If Pinc.2 = 0 Then
   If C_limit2 = 0 Then Decr Limit
   C_limit2 = 1
   Incr Down_counter
   Eeprom_save = 20000
   Set_timer = 133
   End If
 
'correct if value out of range
If Limit < 0 Then Limit = 0
If Limit > 999 Then Limit = 999
If Volt > 999 Then Volt = 999
 
'check limit and set conditions
Lowlimit = Limit - 25
Highlimit = Limit + 25
 
If Pinc.3 = 0 Then
   If Volt > Highlimit Then Over_limit = 1
   If Volt < Lowlimit Then Over_limit = 0
   Else
   If Volt < Lowlimit Then Over_limit = 1
   If Volt > Highlimit Then Over_limit = 0
End If
 
'show light level/threshold
If Set_timer = 0 Then Volt_string = Str(volt)
If Set_timer > 0 Then Volt_string = Str(limit)
 
'digit 1
If Len(volt_string) > 2 Then Volt_temp = Left(volt_string , 1) Else Volt_temp = "0"
Volt_number = Val(volt_temp)
Portd = Lookup(volt_number , Table)
If Set_timer > 0 Then Portd.7 = 1
If Len(volt_string) > 3 Then Portd = 121
Portb.0 = 1
Portb.1 = 0
Portb.2 = 0
Waitms 5
 
Portb.0 = 0
Portb.1 = 0
Portb.2 = 0
 
'digit 2
If Len(volt_string) = 2 Then Volt_temp = Left(volt_string , 1)
If Len(volt_string) > 2 Then Volt_temp = Mid(volt_string , 2 , 1)
If Len(volt_string) < 2 Then Volt_temp = "0"
Volt_number = Val(volt_temp)
Portd = Lookup(volt_number , Table)
If Over_limit = 1 Then Portd.7 = 1
If Len(volt_string) > 3 Then Portd = 80
Portb.0 = 0
Portb.1 = 1
Portb.2 = 0
Waitms 5
 
Portb.0 = 0
Portb.1 = 0
Portb.2 = 0
 
'digit 3
Volt_temp = Right(volt_string , 1)
Volt_number = Val(volt_temp)
Portd = Lookup(volt_number , Table)
If Read_timer > 600 And Eeprom_save = 0 Then Portd.7 = 1
If Read_timer < 600 And Eeprom_save > 0 Then Portd.7 = 1
If Len(volt_string) > 3 Then Portd = 80
Portb.0 = 0
Portb.1 = 0
Portb.2 = 1
Waitms 5
 
Portb.0 = 0
Portb.1 = 0
Portb.2 = 0
 
'set outputs
If Over_limit = 1 And Over_limit_set = 0 Then
   Over_limit_set = 1
   Portb.3 = 1
   Action_timer = 33
   End If
If Over_limit = 0 Then
   If Over_limit_set = 1 Then Action_timer = 100
   Over_limit_set = 0
   Portb.3 = 0
   End If
 
'pulse output
If Action_timer > 0 Then
   Decr Action_timer
   Portb.4 = 1
   End If
If Action_timer = 0 Then Portb.4 = 0
 
'timers
If Set_timer > 0 Then Decr Set_timer
If Read_timer > 0 Then Decr Read_timer
 
'fast threshold adjust
If Up_counter > 66 Then Incr Limit
If Down_counter > 66 Then Decr Limit
 
'save eeprom values
If Eeprom_save > 0 Then Decr Eeprom_save
If Eeprom_save = 1 Then Eeprom_limit = Limit
 
'loop
Reset Watchdog
Goto Main
End
 
'---- data for correct display of numbers on LED display ------
Table:
Data 63 , 6 , 91 , 79 , 102 , 109 , 125 , 7 , 127 , 111 , 128
'     0    1   2    3    4     5     6     7   8     9     dp
Personal tools
Namespaces
Variants
Actions
Navigation
Language