Srccodetst

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
 
<source lang="bascomavr">
 
<source lang="bascomavr">
'--------------------------------------------------------------
 
'                  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
+
' real time measurement of preignition angle for two stroke engine
Dim Over_limit As Bit , Limit As Integer
+
' made by Zsolt Fazekas (fazekaszs at hotmail.com)
Dim Volt_temp As String * 1 , Volt_string As String * 3
+
' Copyright reserved
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
+
$regfile = "89s52.dat"
Start Adc
+
$crystal = 22118400
 +
$map
 +
$baud = 19200
  
'Inputs
+
Dim E As Word                                                'time elapsed from interruption until upper dead point
'PC.0 Light sensor, analog value
+
Dim F As Word                                                'time elapsed from upper dead point until interruption
'PC.1 Threshold up
+
Dim K As Byte                                                'secondary variable counting the number of measures made by timer2
'PC.2 Threshold down
+
'PC.3 Over/under threshold
+
  
'Outputs
+
Dim Fh As Byte                                                'Fh, Fl, Eh, El - upper and lower bytes of F and E
'PB.0 LED-display digit 1
+
Dim Fl As Byte
'PB.1 LED-display digit 2
+
Dim Eh As Byte
'PB.2 LED-display digit 3
+
Dim El As Byte
'PB.3 Output constant
+
'PB.4 Output pulse
+
'PortD LED display
+
  
'get eeprom values
+
Reset F
Limit = Eeprom_limit
+
Reset E
 +
Reset K
  
Portd = 64
+
Reset Fh
Portb.0 = 1
+
Reset Fl
Portb.1 = 0
+
Reset Eh
Portb.2 = 0
+
Reset El
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
+
Set Tcon.0                                                    'INT0 falling edge
 +
Set Tcon.2                                                    'INT1 falling edge
  
'program start
+
Enable Interrupts
Main:
+
Enable Timer2
'read light
+
Enable Timer1
If Read_timer = 0 Then
+
Disable Serial
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
+
Config Timer2 = Counter , Gate = Internal , Mode = 0         'not the capture mode, however simple counter mode
If Pinc.2 = 1 Then
+
Counter2 = 0                                                 'reset counter (driven by a 200KHz outside clock sign coming in through port P1.0)
  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
+
Config Timer1 = Timer , Gate = Internal , Mode = 2            'auto reload for transmit
If Limit < 0 Then Limit = 0
+
Load Timer1 , 3
If Limit > 999 Then Limit = 999
+
If Volt > 999 Then Volt = 999
+
  
'check limit and set conditions
+
Declare Sub Kuld(f As Word , E As Word , Fh As Byte , Fl As Byte , Eh As Byte , El As Byte)
Lowlimit = Limit - 25
+
Highlimit = Limit + 25
+
  
If Pinc.3 = 0 Then
+
On Int0 Fordulatszam                                          'falling edge interrupt coming in through port P3.2 (at upper dead point)
  If Volt > Highlimit Then Over_limit = 1
+
On Int1 Elogyujtas                                            'falling edge interrupt coming in through port P3.3 (at the point of ignition)
  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
+
Start Timer1
If Set_timer = 0 Then Volt_string = Str(volt)
+
Start Timer2
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
+
Do
Portb.1 = 0
+
Enable Int0
Portb.2 = 0
+
Enable Int1
 +
Loop
 +
End
  
'digit 2
+
Fordulatszam:                                                'INT0 interrupt, saving E
If Len(volt_string) = 2 Then Volt_temp = Left(volt_string , 1)
+
Stop Timer2                                                  'stop timer 2 for reading
If Len(volt_string) > 2 Then Volt_temp = Mid(volt_string , 2 , 1)
+
Reset Tcon.1                                                 'deleting the interrupt flag
If Len(volt_string) < 2 Then Volt_temp = "0"
+
K = K + 1
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
+
If T2con.7 = 1 Then                                          'overflow flag is 1, (f. e. because crankshaft halts)
Portb.1 = 0
+
Counter2 = 0
Portb.2 = 0
+
Reset T2con.7                                                'overflow flag is deleted
 +
Start Timer2
 +
F = 0
 +
E = 0
 +
K = 0
 +
Else
 +
  Eh = Th2
 +
  El = Tl2
 +
  E = Makeint(el , Eh)
 +
        If K >= 3 Then                                      'after enabling interrupt one has to wait until K=3
 +
        Disable Int0
 +
        Disable Int1
 +
        Call Kuld
 +
        K = 0                                                'the value of K is reset after serial transmission
 +
        End If
 +
  Counter2 = 0
 +
  Start Timer2
 +
End If
 +
Return
  
'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
+
Sub Kuld(f As Word , E As Word , Fh As Byte , Fl As Byte , Eh As Byte , El As Byte)
Portb.1 = 0
+
F = F + 6                                                    '6 and 4: time ellapsing between stopping and starting Timer2
Portb.2 = 0
+
E = E + 4                                                    'this should be added to times measured for correction
 +
Fh = High(f)
 +
Fl = Low(f)
 +
Eh = High(e)
 +
El = Low(e)
 +
If Fl = 255 And Fh = 255 Then                                'the higest value should be avoided because of sincronisation
 +
Fl = 254
 +
End If
 +
If El = 255 And Eh = 255 Then                                'the higest value should be avouded because of sincronisation
 +
El = 254
 +
End If
 +
If P2.1 = 1 Then                                              'switch
 +
Scon = &H40
 +
Enable Serial
 +
Sbuf = 255                                                    'for sincronisation
 +
Bitwait Scon.1 , Set
 +
Reset Scon.1
 +
Sbuf = 255                                                    'for sincronisation
 +
Bitwait Scon.1 , Set
 +
Reset Scon.1
 +
Sbuf = Fl
 +
Bitwait Scon.1 , Set
 +
Reset Scon.1
 +
Sbuf = Fh
 +
Bitwait Scon.1 , Set
 +
Reset Scon.1
 +
Sbuf = El
 +
Bitwait Scon.1 , Set
 +
Reset Scon.1
 +
Sbuf = Eh
 +
Bitwait Scon.1 , Set
 +
Reset Scon.1
 +
Disable Serial
 +
End If
 +
End Sub
  
'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
+
Elogyujtas:                                                  'INT1 interrupt, saving F
If Action_timer > 0 Then
+
Stop Timer2                                                  'stop timer 2 for reading
  Decr Action_timer
+
Reset Tcon.3                                                  'deleting the interrupt flag
  Portb.4 = 1
+
K = K + 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
+
  
 +
If T2con.7 = 1 Then                                          'overflow flag is 1, (f. e. because crankshaft halts)
 +
Counter2 = 0
 +
Reset T2con.7                                                'overflow flag is deleted
 +
Start Timer2
 +
K = 0
 +
E = 0
 +
F = 0
 +
Else
 +
      Fh = Th2
 +
      Fl = Tl2
 +
      F = Makeint(fl , Fh)
 +
      Counter2 = 0
 +
      Start Timer2
 +
End If
 +
Return
  
 
</source>
 
</source>

Revision as of 14:50, 30 July 2019

'--------------------------------------------------------------------------------------
' real time measurement of preignition angle for two stroke engine
' made by Zsolt Fazekas (fazekaszs at hotmail.com)
' Copyright reserved
'--------------------------------------------------------------------------------------
 
$regfile = "89s52.dat"
$crystal = 22118400
$map
$baud = 19200
 
Dim E As Word                                                 'time elapsed from interruption until upper dead point
Dim F As Word                                                 'time elapsed from upper dead point until interruption
Dim K As Byte                                                 'secondary variable counting the number of measures made by timer2
 
Dim Fh As Byte                                                'Fh, Fl, Eh, El - upper and lower bytes of F and E
Dim Fl As Byte
Dim Eh As Byte
Dim El As Byte
 
Reset F
Reset E
Reset K
 
Reset Fh
Reset Fl
Reset Eh
Reset El
 
Set Tcon.0                                                    'INT0 falling edge
Set Tcon.2                                                    'INT1 falling edge
 
Enable Interrupts
Enable Timer2
Enable Timer1
Disable Serial
 
 
Config Timer2 = Counter , Gate = Internal , Mode = 0          'not the capture mode, however simple counter mode
Counter2 = 0                                                  'reset counter (driven by a 200KHz outside clock sign coming in through port P1.0)
 
Config Timer1 = Timer , Gate = Internal , Mode = 2            'auto reload for transmit
Load Timer1 , 3
 
Declare Sub Kuld(f As Word , E As Word , Fh As Byte , Fl As Byte , Eh As Byte , El As Byte)
 
On Int0 Fordulatszam                                          'falling edge interrupt coming in through port P3.2 (at upper dead point)
On Int1 Elogyujtas                                            'falling edge interrupt coming in through port P3.3 (at the point of ignition)
 
Start Timer1
Start Timer2
 
 
Do
Enable Int0
Enable Int1
Loop
End
 
Fordulatszam:                                                 'INT0 interrupt, saving E 
Stop Timer2                                                   'stop timer 2 for reading
Reset Tcon.1                                                  'deleting the interrupt flag
K = K + 1
 
If T2con.7 = 1 Then                                           'overflow flag is 1, (f. e. because crankshaft halts)
Counter2 = 0
Reset T2con.7                                                 'overflow flag is deleted
Start Timer2
F = 0
E = 0
K = 0
Else
   Eh = Th2
   El = Tl2
   E = Makeint(el , Eh)
         If K >= 3 Then                                       'after enabling interrupt one has to wait until K=3
         Disable Int0
         Disable Int1
         Call Kuld
         K = 0                                                'the value of K is reset after serial transmission
         End If
   Counter2 = 0
   Start Timer2
End If
Return
 
 
Sub Kuld(f As Word , E As Word , Fh As Byte , Fl As Byte , Eh As Byte , El As Byte)
F = F + 6                                                     '6 and 4: time ellapsing between stopping and starting Timer2
E = E + 4                                                     'this should be added to times measured for correction
Fh = High(f)
Fl = Low(f)
Eh = High(e)
El = Low(e)
If Fl = 255 And Fh = 255 Then                                 'the higest value should be avoided because of sincronisation
Fl = 254
End If
If El = 255 And Eh = 255 Then                                 'the higest value should be avouded because of sincronisation
El = 254
End If
If P2.1 = 1 Then                                              'switch
Scon = &H40
Enable Serial
Sbuf = 255                                                    'for sincronisation
Bitwait Scon.1 , Set
Reset Scon.1
Sbuf = 255                                                    'for sincronisation
Bitwait Scon.1 , Set
Reset Scon.1
Sbuf = Fl
Bitwait Scon.1 , Set
Reset Scon.1
Sbuf = Fh
Bitwait Scon.1 , Set
Reset Scon.1
Sbuf = El
Bitwait Scon.1 , Set
Reset Scon.1
Sbuf = Eh
Bitwait Scon.1 , Set
Reset Scon.1
Disable Serial
End If
End Sub
 
 
Elogyujtas:                                                   'INT1 interrupt, saving F
Stop Timer2                                                   'stop timer 2 for reading
Reset Tcon.3                                                  'deleting the interrupt flag
K = K + 1
 
If T2con.7 = 1 Then                                           'overflow flag is 1, (f. e. because crankshaft halts)
Counter2 = 0
Reset T2con.7                                                 'overflow flag is deleted
Start Timer2
K = 0
E = 0
F = 0
Else
      Fh = Th2
      Fl = Tl2
      F = Makeint(fl , Fh)
      Counter2 = 0
      Start Timer2
End If
Return
Personal tools
Namespaces
Variants
Actions
Navigation
Language