You are on page 1of 7

' This Program demonstrates the use of the MAX7219, seven segment

decoder driver
' It also incorporates placing of Decimal point.
' A PICmicro with on-board eeprom

Device 16F876A
Include "Modedefs.bas"
XTAL = 20 ' Set Xtal
Frequency
' ** Declare Pins Used **
Symbol Clk = PORTB.1 ' Data is clocked on
rising edge of this pin
Symbol Dta = PORTB.2 ' Bits are
shifted out of this pin
Symbol Load = PORTB.3
Symbol RecEnable = PORTC.2
Symbol SWS = PORTB.0 ' start switch
Symbol Sol_Low = PORTB.4 'Low soleniod
Symbol Sol_Mid = PORTB.5 ' medium solenoid
Symbol Sol_Hig = PORTB.6 ' High sole
' ** Declare Constants **
Dim Kg_grams1 As DWord
' DIM Address as BYTE
' Dim AD_Raw as Word ' 10-bit result
of A/D conversion
' Dim AD_Result as Word ' Quantasized ADC
result
' Dim Average as Word ' Build up the
average result
' Dim Samples as Byte
Dim Rate As 3129
' Rate1 var word
Dim Flow_rate As Word
Dim Kg As DWord
Dim Kg1 As DWord
Dim Amount As DWord
' DIM Kgc as Dword
Dim Kgdis As DWord
Dim L_Kg As DWord
Dim L_Sale As DWord
Dim Totalizer As DWord
Dim Rate2 As Word
Dim cal_fact As Byte
LTMSW var Word
MTHSW var Word
Dim Low_flow_cut As Word
Dim cons1 As 10
Dim cons2 As 1000
Dim cons3 As 10000
Dim cons4 As 10000000
Dim conav As 30
Dim i As Byte
Digit Var Byte
R_Val Var Byte
' Flow_rate var word
Disp_Bot var Word
register3 var Word
register4 var Word
register5 var Word
register6 var Word
Kg_grams var Word
register8 var Word
Dim Kg_grams_old As Word
Dim Flow_rate_old As Word
' Totalizer var word
Dim LastSale As DWord
Dim LSale As DWord
Dim Flag As Byte
Dim Lcd_top[8] As Word
Dim Lcd_mid[8] As Word
Dim Lcd_bot[8] As Word
Dim Seg[8] As Word
Dim SegA[8] As Word
Dim SegB[8] As Word
Dim Disp_Patt As Word
Dim CoilTx[8] As Byte
Dim CoilRx[8] As Byte

a var Byte
c var Byte
d var Byte
Dim DataTX[8] As Byte
Dim RX[23] As Byte
CRC16 var Word
Disp_top var DWord
Disp_mid var DWord
' Disp_bot var word
SEG1 var Byte
SEG2 var Byte
SEG3 var Byte
SEG4 var Byte
SEG5 var Byte
SEG6 var Byte
SEG7 var Byte
SEG8 var Byte
SEG9 var Byte
SEG10 var Byte
SEG11 var Byte
SEG12 var Byte
SEG13 var Byte
SEG14 var Byte
SEG15 var Byte
SEG16 var Byte
rat_flag var Byte
TRISA = %00000011 ' Configure AN0 (PORTA.0) as
an input
TRISB = %00000001
TRISC = %10000000
ADCON1 = %10000100
CoilTx[0]=$01
CoilTx[1]=$05
CoilTx[2]=$00
CoilTx[3]=$02
CoilTx[4]=$FF
CoilTx[5]=$00
CoilTx[6]=$2D
CoilTx[7]=$FA
DataTX[0]=$01
DataTX[1]=$03
DataTX[2]=$00
DataTX[3]=$01
DataTX[4]=$00
DataTX[5]=$09
DataTX[6]=$D4
DataTX[7]=$0C
PortB=0
Portc=0
PortA=0
'EWRITE 100, [100]
'EWRITE 105, [400]
rat_flag=0
DelayMS 2000
SHOut Dta,Clk,msbfirst,
[0,119,88,109,121,120,0,0,118,89,121,120,0,0,0,0]
High Load
Low Load
DelayMS 5000

' ***** MAIN PROGRAM *****


L_Sale=ERead 100
L_Kg=ERead 105
' Rate=EREAD 115
Disp_top=L_Sale
Disp_mid=L_Kg
Disp_bot=Rate
GoSub Transfer
DelayMS 1000
' Totalizer=EREAD 110
Flow_rate=0
Kg=0
LTMSW=90
MTHSW=60
Low_flow_cut=15
cal_fact=100
LastSale=0
Kgdis=0
Kg_grams=0
Kg_grams1=0
Amount=0
a=0
rat_flag=1
' AD_Raw=0
High RecEnable
SerOut portc.6,34,[$01,$06,$00,$26,$00,$46,$E9,$F3]
DelayMS 500
Main: Flag=0
a=0
If SWS= 0 Then GoTo coil
Low Sol_Low
Low Sol_Mid
Low Sol_Hig
' IF LastSale=0 then goto Main
' Gosub Total
GoTo Main
coil:
High RecEnable
SerOut portc.6,34,
[CoilTx[0],CoilTx[1],CoilTx[2],CoilTx[3],CoilTx[4],CoilTx[5],CoilTx[6],C
oilTx[7]]
' Delayms 500
Low RecEnable
SerIn portc.7,34,5,Coil_Error,
[CoilRx[0],CoilRx[1],CoilRx[2],CoilRx[3],CoilRx[4],CoilRx[5],CoilRx[6],C
oilRx[7]]
DelayMS 200
If CoilRx[0]=CoilTx[0]And CoilRx[1]=CoilTx[1]And
CoilRx[2]=CoilTx[2]And CoilRx[3]=CoilTx[3]And CoilRx[4]=CoilTx[4]And
CoilRx[5]=CoilTx[5]And CoilRx[6]=CoilTx[6]And CoilRx[7]=CoilTx[7] Then
GoTo Again
Else
GoTo Coil_Error
EndIf
Again: High RecEnable
SerOut portc.6,34,
[DataTX[0],DataTX[1],DataTX[2],DataTX[3],DataTX[4],DataTX[5],DataTX[6],D
ataTX[7]]
Low RecEnable
SerIn portc.7,34,5,RS_485_Error,
[RX[0],RX[1],RX[2],RX[3],RX[4],RX[5],RX[6],RX[7],RX[8],RX[9],RX[10],RX[1
1],RX[12],RX[13],RX[14],RX[15],RX[16],RX[17],RX[18],RX[19],RX[20],RX[21]
,RX[22]]
If RX[1]<>$03 Then
GoTo Again
EndIf
GoSub CRC16_Calculate
If CRC16.LowByte=RX[21] And CRC16.HighByte=RX[22] Then
Flow_rate.HighByte=RX[3]
Flow_rate.LowByte=RX[4]
Kg_grams.HighByte=RX[15]
Kg_grams.LowByte=RX[16]
Totalizer.HighByte=RX[19]
Totalizer.LowByte=RX[20]
Kg_grams1=Kg_grams
If Flow_rate > 500 Then Flow_rate=Flow_rate_old
Flow_rate_old = Flow_rate
If Kg_grams > 50000 Then Kg_grams=Kg_grams_old
Kg_grams_old=Kg_grams
Else GoTo CRC_Error
EndIf
If Flag=0 Then GoTo LowSol
If a<=10 Then GoTo Start_fil
If Flag=1 Then GoTo MED
GoTo Again
LowSol: High Sol_Low 'Low soleniod open
Low Sol_Mid ' medium solenoid
Low Sol_Hig 'high sol
GoTo Start_fil
MED: If Flow_rate > LTMSW Then GoTo Start_fil
If Flow_rate < LTMSW And Flow_rate > MTHSW Then GoTo MEDSol
If Flow_rate < MTHSW And Flow_rate > Low_flow_cut Then GoTo
HiSOl
If Flow_rate < Low_flow_cut Then GoTo cutof
GoTo Again
MEDSol:
Low Sol_Low 'Low soleniod open
High Sol_Mid ' medium solenoid
Low Sol_Hig
GoTo Start_fil
HiSOl:
Low Sol_Low 'Low soleniod open
Low Sol_Mid ' medium solenoid
High Sol_Hig
GoTo Start_fil
cutof:
Low Sol_Low 'Low soleniod open
Low Sol_Mid ' medium solenoid
Low Sol_Hig
DelayMS 200
If SWS = 1 Then GoTo Main
GoTo cutof
Start_fil: 'If SWS= 1 then goto Total
'IF Flow_rate < 1 then goto Again
' If Flow_rate > 1 then goto con2
' goto Again
con2: Kg_grams1=Kg_grams1*cal_fact/100
Amount = (Kg_grams1* Rate)/100000
Flag=1
LastSale=Amount
a=a+1
Totalizer=Totalizer+ Kg_grams1
Disp_top=Amount
Disp_mid=Kg_grams1/10
Disp_bot=Rate
GoSub Transfer
EWrite 100 , [ LastSale ]
EWrite 105 , [ Kg_grams1/10 ]
EWrite 110 , [ Totalizer ]
GoTo Again

** Subroutines **
' Display the Value held in the Variable "MAX_DISP" on the four LED's
' The value held in "MAX_DP" places the decimal point on that LED (0-3)
' Sending the value 15 blanks the display, this allows Zero suppression
' By setting bit-7 of the value sent to the individual LED displays,
the decimal point
' Is illuminated

Transfer:
For i = 0 To 5
Lcd_top[i]=Disp_top Dig i
LookUpL Lcd_top[i],[95,6,59,47,102,109,124,7,127,103,0],Disp_Patt
Seg[i]=Disp_Patt
Next i
For i = 0 To 5
Lcd_mid[i]=Disp_mid Dig i
LookUpL Lcd_mid[i],[95,6,59,47,102,109,124,7,127,103,0],Disp_Patt
SegA[i]=Disp_Patt
Next i
'If rat_flag=1 then goto display_1
For i = 0 To 3
Lcd_bot[i]=Disp_bot Dig i
LookUpL Lcd_bot[i],[95,6,59,47,102,109,124,7,127,103,0],Disp_Patt
SegB[i]=Disp_Patt
Next i
display_1: SEG1=Seg[0]
SEG2=Seg[1]
SEG3=Seg[2]
SEG4=Seg[3]
SEG5=Seg[4]
SEG6=Seg[5]
SEG7=SegA[0]
SEG8=SegA[1]
SEG9=SegA[2]
SEG10=SegA[3]
SEG11=SegA[4]
SEG12=SegA[5]
SEG13=SegB[0]
SEG14=SegB[1]
SEG15=SegB[2]
SEG16=SegB[3]
SHOut Dta,Clk,msbfirst,
[SEG1,SEG2,SEG3,SEG4,SEG5,SEG6,SEG7,SEG8,SEG9,SEG10,SEG11,SEG12,SEG13,SE
G14,SEG15,SEG16]
High Load
pause 50
Return
'Delayms 20
CRC16_Calculate:
CRC16=$ffff
For c=0 To 20
CRC16=CRC16^RX[c]
For d=1 To 8
If CRC16.0=1 Then
CRC16=$A001^(CRC16>>1)
Else
CRC16=CRC16>>1
EndIf
Next d
Next c
Return
Total: DelayMS 1000
Totalizer=Totalizer+ Kg_grams1
EWrite 110 , [ Totalizer ]
DelayMS 500
If SWS= 0 Then GoTo Again
GoTo Main

Coil_Error: SHOut Dta,Clk,msbfirst,


[0,0,88,6,60,89,0,48,60,48,48,121,0,0,0,0]
High Load
Low Load
DelayMS 5000
GoTo coil

RS_485_Error: SHOut Dta,Clk,msbfirst,


[0,0,119,120,119,62,0,48,60,48,48,121,0,0,0,0]
High Load
Low Load
DelayMS 5000
GoTo Again

CRC_Error: SHOut Dta,Clk,msbfirst,


[0,0,0,89,48,89,0,48,60,48,48,121,0,0,0,0]
High Load
Low Load
DelayMS 5000
GoTo Again

You might also like