'####################################################################################
'# PICAXE 28X1 fw A.2, SAA1064 7 Seg Driver #
'####################################################################################
'
#picaxe 28x1
SetFreq m8
'
'####################################################################################
'# EEPROM use - 0 to 255 available 'STATIC VALUES #
'####################################################################################
'
'RPM Bit Patterns - I/O Exp 0
' 7 Segment Display
' b0
eeprom 0,(%00111111) '0 RPM
eeprom 1,(%00000110) '1 RPM
eeprom 2,(%01011011) '2 RPM
eeprom 3,(%01001111) '3 RPM
eeprom 4,(%01100110) '4 RPM
eeprom 5,(%01101101) '5 RPM
eeprom 6,(%01111101) '6 RPM
eeprom 7,(%00000111) '7 RPM
eeprom 8,(%01111111) '8 RPM
eeprom 9,(%01101111) '9 RPM
' .gfedcba
'
eeprom 10,(%10111111) '0 RPM + DP
eeprom 11,(%10000110) '1 RPM + DP
eeprom 12,(%11011011) '2 RPM + DP
eeprom 13,(%11001111) '3 RPM + DP
eeprom 14,(%11100110) '4 RPM + DP
eeprom 15,(%11101101) '5 RPM + DP
eeprom 16,(%11111101) '6 RPM + DP
eeprom 17,(%10000111) '7 RPM + DP
eeprom 18,(%11111111) '8 RPM + DP
eeprom 19,(%11101111) '9 RPM + DP
' .gfedcba
eeprom 20,(%00000000) 'all digits OFF
'
'####################################################################################
'# SYMBOL ASSIGNMENTS #
'####################################################################################
'
symbol SAA1064 = %01110000 '7Seg display - SAA1064 7Seg LED Driver i2c WRITE address
symbol L_Count = 1 'in1 == Lathe count input
symbol Count_In = w2 ;Input Count value
symbol FIFO_1 = w3 'FIFO accumulator
symbol FIFO_2 = w4 'FIFO accumulator
symbol FIFO_3 = w5 'FIFO accumulator
symbol FIFO_4 = w6 'FIFO accumulator
symbol FIFOAVE = w7 'Average of FIFO Accumulator
symbol ThoD = b16 'w8 Thousands Digit
symbol HunD = b17 'w8 Hundreds Digit
symbol TenD = b18 'w9 Tens Digit
symbol OneD = b19 'w9 Ones Digit
symbol RPM = w10 'Average from Accumulator converted to RPM
'
'####################################################################################
'# Initialise #
'####################################################################################
'
Init:
'
'SAA1064
hi2csetup i2cmaster,SAA1064,i2cslow_16,i2cbyte
hi2cout (%00000000,%00101111)
' write control , 0,0,6mA,0,Test,NoBlank,NoBlank,Dynamic
' Instruction , Control
pause 500
hi2cout (%00000000,%00100111)
' write control , 0,0,6mA,0,NoTest,NoBlank,NoBlank,Dynamic
' Instruction , Control
pause 500
hi2cout (%00000000,%00101111)
' write control , 0,0,6mA,0,Test,NoBlank,NoBlank,Dynamic
' Instruction , Control
pause 500
hi2cout (%00000000,%00100111)
' write control , 0,0,6mA,0,NoTest,NoBlank,NoBlank,Dynamic
' Instruction , Control
hi2cout (%00000001,%00000000,%00000000,%00000000,%00000000)
' write Digit1, all off , all off , all off , all off
' Instruction , Digit 1 , Digit 2 , Digit 3 , Digit 4
'
'
'#################################################################################
Main:
'#################################################################################
'
'
Count L_Count, 1000, Count_In ;Period 1000 = 1/2 second at 8MHz clock
gosub Accumulator
If Count_In < 2 then ;remove spurious counts when stationary
RPM = 0
Else
RPM = FIFOAVE * 5 / 12 '*5 for 500 counts == 2500rpm
' /12 as FIFOAVE = 12 times actual count
EndIf
'
;RPM represents range from 0 to ~ 2500 RPM.
If RPM > 999 then
ThoD = RPM DIG 3 + 10 ;Thousands +10 to show dp as 1000s indicator
HunD = RPM DIG 2 ;Hundreds
TenD = RPM DIG 1 ;Tens
OneD = RPM DIG 0 ;Ones
ElseIf RPM > 99 Then
ThoD = 20 ;Thousands == Blank
HunD = RPM DIG 2 ;Hundreds
TenD = RPM DIG 1 ;Tens
OneD = RPM DIG 0 ;Ones
ElseIf RPM > 9 Then
ThoD = 20 ;Thousands == Blank
HunD = 20 ;Hundreds == Blank
TenD = RPM DIG 1 ;Tens
OneD = RPM DIG 0 ;Ones
Else
ThoD = 20 ;Thousands == Blank
HunD = 20 ;Hundreds == Blank
TenD = 20 ;Tens == Blank
OneD = RPM DIG 0 ;Ones
EndIf
Read ThoD, ThoD ;get bit pattern for LED from EEPROM
Read HunD, HunD ;get bit pattern for LED from EEPROM
Read TenD, TenD ;get bit pattern for LED from EEPROM
Read OneD, OneD ;get bit pattern for LED from EEPROM
hi2cout (%00000001,ThoD,HunD,TenD,OneD) 'SAA1064 Digit one = MSD= Left side
Goto Main 'Loop for continuous display
End
'
Accumulator:
'Sum the last 4 Count values to smooth output
'* 3 to improve resolution
FIFO_1 = FIFO_2
FIFO_2 = FIFO_3
FIFO_3 = FIFO_4
FIFO_4 = Count_In * 3
FIFOAVE = FIFO_1 + FIFO_2 + FIFO_3 + FIFO_4 '12 * Average last 4 Readings
'
Return
'