⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bascom-plc.bas

📁 pic 单片机 的简易PLC labplc.zip
💻 BAS
字号:
'-----------------------------------------------------------------
'                1999 Mirko Bjelica, Yugoslavia
'
'                   Contact: mikanb@EUnet.yu
'
'                           Lab-PLC
'                           -------
'  This program is a part of whole laboratory PLC project which
'I realized as my final exam on Polytechnical Engeneering College.
'Lab-PLC has 4 inputs, 4 outputs and one timer and it is created
'for educational purposes. There is also 10 memory markers used
'as temporary bits between gates. At this moment Lab-PLC is installed
'at the college laboratory to controll one 3 phase electromotor. I used
'ATMEL's microcontroller AT89C2051 because of its simplicity and because it
'had enough I/O pins for this project. Project also includes software
'written in Visual Basic for DOS, which is used to load RAM memory of
'AT89C2051, PCB, Schematic and a user's manual.
'NOTE:
' Timer - Timer in PLC Device
'uTimer - Timer in AT89C2051 microcontroller
'Input_1, Input_2 - Two inputs of single logic gate or Timer (Input_1)
'Output_ - Output of logic gate or Timer
'-----------------------------------------------------------------

'Variable dimensioning
Dim Output_ As Bit , Input_1 As Bit , Input_2 As Bit , Temporary As Bit , Jump As Bit

Dim Working As Bit , Ctrlbit As Bit , Edge As Bit , Tmpmem As Byte , Interval As Byte

Dim K As Byte , Pc_input As Byte , Countr As Integer , Ir As Bit

Dim M0 As Bit : Dim M1 As Bit : Dim M2 As Bit : Dim M3 As Bit : Dim M4 As Bit

Dim M5 As Bit : Dim M6 As Bit : Dim M7 As Bit : Dim M8 As Bit : Dim M9 As Bit

O1 Alias P1.0 : O2 Alias P1.1 : O3 Alias P1.2 : O4 Alias P1.3

I1 Alias P1.4 : I2 Alias P1.5 : I3 Alias P1.6 : I4 Alias P1.7

'Initialization of uTimer
Config Timer0 = Timer , Gate = Internal , Mode = 2

On Timer0 Timer0_interapt

Load Timer0 , 250

Enable Interrupts

Enable Timer0

Priority Set Timer0

'Definition of Baud Rate for RS232 communication between PC and PLC
$baud = 4800
$crystal = 12000000

Set P3.7                                                      'Turn led "RECEIVED DATA" OFF

Reset P3.3                                                    'Led "READY FOR RECEIVING" starts to blink

P1 = &B11111111                                               'Clear all inputs and outputs

K = 67                                                        'RAM Memory address from where starts writing PLC program

Do                                                            'Routine for loading RAM memory
   Pc_input = Waitkey                                         'Wait for data
   mov R0,{k}                                                 'generating indirect address
   Acc = Pc_input                                             'proceed data to accumulator
   mov @R0,A                                                  'Write received data to indirectly addressed memory location
   inc k
Loop Until Pc_input = 99                                      'Code for "End Of Transfer"


Reset P3.7                                                    'Turn led "RECEIVED DATA" ON

Set P3.3                                                      'Led "READY FOR RECEIVING" stops to blink

Tmpmem = 0                                                    '{

Countr = 0                                                    '{

Jump = 0                                                      '{Initialization of used variables

Edge = 0                                                      '{

Ctrlbit = 0                                                   '{

Prgstart:                                                     'Inception

K = 66                                                        'address-1 because of increment in next subroutine

Gosub Readram                                                 'Go and read RAM memory

'Program
Nastavi:
'Detection of logic function and timer
Select Case Tmpmem
             Case 10 : Gosub Inputs                           'Take logic conditions of input1 and input2
                       Output_ = Input_1 And Input_2            'AND
                       Gosub Outp                             'Set output of current logic gate
                       Goto Checkend
             Case 11 : Gosub Inputs                           'Take logic conditions of input1 and input2
                       Output_ = Input_1 Or Input_2           'OR
                       Gosub Outp                             'Set output of current logic gate
                       Goto Checkend
             Case 12 : Gosub Inputs                           'Take logic conditions of input1 and input2
                       Output_ = Input_1
                       Output_ = Not Output_                  'NOT
                       Gosub Outp                             'Set output of current logic gate
                       Goto Checkend
             Case 13 : Gosub Inputs                           'Take logic conditions of input1 and input2
                       Output_ = Input_1 And Input_2
                       Output_ = Not Output_                  'NAND
                       Gosub Outp                             'Set output of current logic gate
                       Goto Checkend
             Case 14 : Gosub Inputs                           'Take logic conditions of input1 and input2
                       Output_ = Input_1 Or Input_2
                       Output_ = Not Output_                  'NOR
                       Gosub Outp                             'Set output of current logic gate
                       Goto Checkend
             Case 15 : Gosub Inputs                           'Take logic conditions of input1 and input2
                       Output_ = Input_1 Xor Input_2          'XOR
                       Gosub Outp                             'Set output of current logic gate
                       Goto Checkend
             Case 24 : Gosub Tajmer                           'TIMER
                       Gosub Outp                             'Set output of Timer
                       Goto Checkend

End Select

'Input detection
Inputs:
Gosub Readram                                                 ' /
Gosub Inpt                                                    '| Find first input of current log.gate
Input_1 = Temporary                                           ' \

Gosub Readram                                                 ' /
Gosub Inpt                                                    '| Find second input of current log.gate
Input_2 = Temporary                                           ' \
Return                                                        'Return from subroutine Inputs

'Finding if input in current log.gate is phisical input,phisical output or memory marker
Inpt:
Select Case Tmpmem
                Case 16 : Temporary = I1
                Case 17 : Temporary = I2
                Case 18 : Temporary = I3
                Case 19 : Temporary = I4
                Case 20 : Temporary = O1
                          Temporary = Not Temporary
                Case 21 : Temporary = O2
                          Temporary = Not Temporary
                Case 22 : Temporary = O3
                          Temporary = Not Temporary
                Case 23 : Temporary = O4
                          Temporary = Not Temporary
                Case 30 : Temporary = M0
                Case 31 : Temporary = M1
                Case 32 : Temporary = M2
                Case 33 : Temporary = M3
                Case 34 : Temporary = M4
                Case 35 : Temporary = M5
                Case 36 : Temporary = M6
                Case 37 : Temporary = M7
                Case 38 : Temporary = M8
                Case 39 : Temporary = M9
End Select
Return

'Finding if output from current log.gate is phisical output or memory marker
Outp:
Gosub Readram
Select Case Tmpmem
                Case 20 : Output_ = Not Output_
                          O1 = Output_
                Case 21 : Output_ = Not Output_
                          O2 = Output_
                Case 22 : Output_ = Not Output_
                          O3 = Output_
                Case 23 : Output_ = Not Output_
                          O4 = Output_
                Case 30 : M0 = Output_
                Case 31 : M1 = Output_
                Case 32 : M2 = Output_
                Case 33 : M3 = Output_
                Case 34 : M4 = Output_
                Case 35 : M5 = Output_
                Case 36 : M6 = Output_
                Case 37 : M7 = Output_
                Case 38 : M8 = Output_
                Case 39 : M9 = Output_
End Select
Return

'Timer routine
Tajmer:
Gosub Readram                                                 ' /
Gosub Inpt                                                    '| Find input of Timer
Input_1 = Temporary                                                 ' \

If Jump = 1 Then                                              'Control bit for bypassing Timer input scan when Timer Workings
               Goto L1
End If
Edge = Input_1
If Edge = 0 Then                                              'If input Timer input is low SET control bit
               Set Jump
End If
L1:
If Input_1 = 1 Then                                             'These are 3 conditions which have to be accomplished to start uTimer
               If Edge = 0 Then
                              If Working = 0 Then
                                          Start Timer0
                                          Set Working
                              End If
                              Goto L2
               End If
End If

Stop Timer0
Countr = 0                                                    'Reset elapsed time counter
Working = 0                                                   'Reset Timer condition bit
Output_ = 0                                                   'Reset output of Timer
Ctrlbit = 0

L2:
If Ctrlbit = 1 Then
               K = K + 1
          Else
               Gosub Readram
               Interval = Tmpmem
               Ctrlbit = 1
End If

If Interval = 0 Then
                     Stop Timer0
                     Countr = 0
                     Reset Output_
                     Jump = 0
                     Reset Working
                     Ctrlbit = 0
                Else
                If Working = 1 Then
                                 Set Output_
                            Else
                                 Reset Output_
                End If

End If

Return

'Checking if it is end of PLC program and if it is, go again from start
Checkend:
Gosub Readram
If Tmpmem = 99 Then
   Goto Prgstart
Else
   Goto Nastavi
End If

'Interrupt routine for Timer interval
Timer0_interapt:
Countr = Countr + 1                                           'Elapsed time counter
     If Countr = 4000 Then                                    '4000x250=1s
     Interval = Interval - 1                                  'Previously set interval by user
     Countr = 0                                               'Reset elapsed time counter
     End If
Return

'Routine for reading RAM memory in Execution mode
Readram:
        inc k                             'Increment address value
        mov R0,{k}                        'Move value to indirect address register
        mov A,@R0                         'Move value from addressed location to accumulator
        Tmpmem = Acc                                          'Store this value in temporary register because-
                                          'leaving data in accumulator can cause loss-
                                          'of that data while accumulator is used for other purposes
Return
End

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -