$map ' Obtain a map of the distribution of memory
$regfile = "m328pdef.dat" ' Used controller
$crystal = 16000000
$framesize = 64 ' The size of the area used and needed to convert
$hwstack = 64 ' The size of the hardware stack
$swstack = 64 ' The size of the software stack
$baud = 38400
'Ucsr0a.1 = 1 ' Baud = Baud * 2
'===============================================================================
' CONFIG SPI
'===============================================================================
Const Const_realization_spi = 0 ' If "0" - implementation of hardware (hardware terminals), if "1" - implementation of software (any conclusions)
#if Const_realization_spi = 0
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = High , Phase = 0 , Clockrate = 4 , Noss = 0
#endif
#if Const_realization_spi = 1
Config Spi = Soft , Din = Pinb.4 , Dout = Portb.3 , Clock = Portb.5 , Ss = None
#endif
Cs_max7219 Alias Portb.0 : Config Cs_max7219 = Output
Spsr.0 = 1 ' F_spi = F_osc / 2; Double Speed (CK/2) Master SPI Mode
Spiinit
Waitms 50
'===============================================================================
' CONSTANTS
'===============================================================================
Const Const_amount_max7219 = 10 ' Количество чипов MAX7219
Const Const_decode_mode = &H09 ' Выбор режим управления сегментами индикатора
Const Const_intensity = &H0A ' Уровень яркости свечения сегментов (0-минимум, 15-максимум)
Const Const_scan_limit = &H0B ' Число используемых разрядов (счет от 0)
Const Const_shutdown = &H0C ' Режим Shutdown (0-дисплей выключен, 1-нормальная работа)
Const Const_display_test = &H0F ' Тестовый режим (0-выключен, 1-включен)
'===============================================================================
' VARIABLE
'===============================================================================
Dim Reg_max7219 As Byte
Dim Led_max7219 As Byte
Dim Data_max7219 As Byte
Dim Chip_max7219 As Byte
Dim Count_max7219 As Byte
Dim Buffer_led_max7219(8) As Byte
Dim Symbol_byte As Byte
Dim Count_digit_1 As Byte , Count_digit_2 As Byte , Len_string As Byte , Symbolic_place As Byte
Dim Str_max7219 As String * 10 , Symbol_str As String * 1
'===============================================================================
' SUBROUTINES
'===============================================================================
Declare Sub Init_max7219()
Declare Sub Write_data_max7219()
Declare Sub Write_register_max7219()
Declare Sub Skip_chip(byval Copy_skip_chip_max7219 As Byte)
Declare Sub On_off_max7219(byval Copy_chip_max7219 As Byte , Byval On_off_max7219 As Byte)
Declare Sub Bright_max7219(byval Copy_chip_max7219 As Byte , Byval Bright_max7219 As Byte)
Declare Sub Write_data_reg_max7219(byval Copy_reg_max7219 As Byte , Byval Copy_data_max7219 As Byte , Byval Copy_chip_max7219 As Byte)
Declare Sub Print_max7219(byval Copy_str_max7219 As String * 10 , Byval Copy_chip_max7219 As Byte , Byval Copy_led_max7219 As Byte)
Init_max7219 ' Initialization MAX7219
'===============================================================================
' HOW TO USE THE DRIVER
'===============================================================================
' +------------- A string variable or a string value. A value greater than 4 characters (without dots) is cut
' | +------- On a MAX7219 chip pass. The offset is 1
' | | +--- 1 - output 1; 2; 3; 4 bits, 2 - appears on the 5, 6, 7, 8 bits
' /|\ | |
' Print_max7219 "Abcd" , 1 , 2
'------------------ Variables for the test output ----------------------------
Dim Flag As Byte
Dim A As Word : A = 0
Dim B As Word : B = 10000
Dim Count As Byte
Dim Buffer_led_max7219_str As String * 10
Dim 1_buffer_led_max7219_str As String * 10
Dim 2_buffer_led_max7219_str As String * 10
'===============================================================================
Main:
'===============================================================================
Incr A
Buffer_led_max7219_str = Str(a)
Buffer_led_max7219_str = Format(buffer_led_max7219_str , " 0")
Print_max7219 Buffer_led_max7219_str , 2 , 1
Decr B
Buffer_led_max7219_str = Str(b)
Buffer_led_max7219_str = Format(buffer_led_max7219_str , " 0")
Print_max7219 Buffer_led_max7219_str , 2 , 2
Waitms 400
'===============================================================================
Goto Main
End
'===============================================================================
'===============================================================================
' HOW TO USE THE DRIVER
'===============================================================================
' +------------- A string variable or a string value. A value greater than 4 characters (without dots) is cut
' | +------- On a MAX7219 chip pass. The offset is 1
' | | +--- 1 - output 1; 2; 3; 4 bits, 2 - appears on the 5, 6, 7, 8 bits
' /|\ | |
' Print_max7219 "Abcd" , 1 , 2
'===============================================================================
Sub Print_max7219(byval Copy_str_max7219 As String * 10 , Byval Copy_chip_max7219 As Byte , Byval Copy_led_max7219 As Byte)
'-------------------------------------------------------------------------------
Str_max7219 = Copy_str_max7219 ' Taking the output variable
If Copy_led_max7219 = 1 Then ' Exhibiting counter depending on the output quartet of numbers
Symbolic_place = 0
Else
Symbolic_place = 4
End If
Len_string = Len(str_max7219) ' Learn the length of the string variable
If Len_string > 4 Then ' Prune unnecessary
Len_string = 5
End If ' If Len_string > 4 Then
For Count_digit_1 = 1 To Len_string ' Pour into a string variable codes for 7-seg led indicator
Symbolic_place = Symbolic_place + 1 ' Array of bytes read
Symbol_str = Mid(str_max7219 , Count_digit_1 , 1) ' Taking the variable of one character
Symbol_byte = Asc(symbol_str) ' Obtain the character code in ASCII
If Symbol_byte = 46 Then ' If the point ".", Adding points in the previous discharge
Symbolic_place = Symbolic_place - 1 ' Derive "symbol" point "" in !!! previous discharge !!!
Buffer_led_max7219(symbolic_place) = Buffer_led_max7219(symbolic_place) Xor &B_1000_0000 ' Derive "symbol" point "" in !!! previous discharge !!!
Elseif Symbol_byte = 176 Then ' If the symbol "°", icon degree Celso
Buffer_led_max7219(symbolic_place) = &H63 ' Derive the symbol "°", icon Celso degrees for indicators with OK
Else ' Everything else
Symbol_byte = Symbol_byte - 32
Buffer_led_max7219(symbolic_place) = Lookup(symbol_byte , Coding_7seg_led_max7219_ok)
End If
Next Count_digit_1 ' Loop through all the characters
If Copy_led_max7219 = 1 Then
For Count_digit_2 = 1 To 4 ' Throws in the MAX7219
Write_data_reg_max7219 Count_digit_2 , Buffer_led_max7219(count_digit_2) , Copy_chip_max7219
Next Count_digit_2
Else
For Count_digit_2 = 5 To 8 ' Throws in the MAX7219
Write_data_reg_max7219 Count_digit_2 , Buffer_led_max7219(count_digit_2) , Copy_chip_max7219
Next Count_digit_2
End If ' If Copy_led_max7219 = 1 Then
'-------------------------------------------------------------------------------
End Sub
'===============================================================================
'===============================================================================
Sub On_off_max7219(byval Copy_chip_max7219 As Byte , Byval On_off_max7219 As Byte)
'-------------------------------------------------------------------------------
Write_data_reg_max7219 Const_shutdown , On_off_max7219 , Copy_chip_max7219
'-------------------------------------------------------------------------------
End Sub
'===============================================================================
'===============================================================================
Sub Bright_max7219(byval Copy_chip_max7219 As Byte , Byval Bright_max7219 As Byte)
'-------------------------------------------------------------------------------
Write_data_reg_max7219 Const_intensity , Bright_max7219 , Copy_chip_max7219
'-------------------------------------------------------------------------------
End Sub
'===============================================================================
'===============================================================================
Sub Write_data_reg_max7219(byval Copy_reg_max7219 As Byte , Byval Copy_data_max7219 As Byte , Byval Copy_chip_max7219 As Byte)
'-------------------------------------------------------------------------------
Local Skip_chip_max7219 As Byte , Temp_skip_chip_max7219 As Byte
Skip_chip_max7219 = Copy_chip_max7219 - 1 ' For transfer to the format of understanding MAX7219_7221
Cs_max7219 = 0
Spiout Copy_reg_max7219 , 1
Spiout Copy_data_max7219 , 1
If Skip_chip_max7219 > 0 Then
Temp_skip_chip_max7219 = 0 ' You need to pass two zero bytes (16 bits of zeros)
Skip_chip_max7219 = Skip_chip_max7219 * 2 ' These skip the chip is 2 bytes
Spiout Temp_skip_chip_max7219 , Skip_chip_max7219
End If
Cs_max7219 = 1
'-------------------------------------------------------------------------------
End Sub
'===============================================================================
'===============================================================================
Sub Init_max7219() ' Presets MAX7219_7221
'-------------------------------------------------------------------------------
For Count_max7219 = 1 To Const_amount_max7219
Write_data_reg_max7219 Const_shutdown , 1 , Count_max7219 ' Mode Shutdown (0 screen is turned off, 1-normal)
Write_data_reg_max7219 Const_decode_mode , 0 , Count_max7219 ' Selecting the operating mode indicator segments, decoding hardware or software
Write_data_reg_max7219 Const_intensity , 15 , Count_max7219 ' The level of luminescence segments (minimum 0, maximum 15)
Write_data_reg_max7219 Const_scan_limit , 7 , Count_max7219 ' The number of bits used (score of 0)
Write_data_reg_max7219 Const_display_test , 0 , Count_max7219 ' Test mode (0-off, 1-on)
Next Count_max7219
'-------------------------------------------------------------------------------
End Sub
'===============================================================================
'===============================================================================
Coding_7seg_led_max7219_ok:
'-------------------------------------------------------------------------------
$asm
.db &H00 ' 32 - "space"
.db &H00 ' 33 - "!"
.db &H00 ' 34 - "top quotes"
.db &H00 ' 35 - "#"
.db &H00 ' 36 - "$"
.db &H00 ' 37 - "%"
.db &H00 ' 38 - "&"
.db &H00 ' 39 - "'"
.db &H00 ' 40 - "("
.db &H00 ' 41 - ")"
.db &H00 ' 42 - "*"
.db &H00 ' 43 - "+"
.db &H00 ' 44 - ","
.db &H01 ' 45 - "-"
.db &H80 ' 46 - "."
.db &H00 ' 47 - "/"
.db &H7E ' 48 - "0"
.db &H30 ' 49 - "1"
.db &H6D ' 50 - "2"
.db &H79 ' 51 - "3"
.db &H33 ' 52 - "4"
.db &H5B ' 53 - "5"
.db &H5F ' 54 - "6"
.db &H70 ' 55 - "7"
.db &H7F ' 56 - "8"
.db &H7B ' 57 - "9"
.db &H00 ' 58 - ":"
.db &H00 ' 59 - ";"
.db &H00 ' 60 - "<"
.db &H00 ' 61 - "="
.db &H00 ' 62 - ">"
.db &H00 ' 63 - "?"
.db &H00 ' 64 - "@"
.db &H77 ' 65 - "A"
.db &H00 ' 66 - "B"
.db &H4E ' 67 - "C"
.db &H00 ' 68 - "D"
.db &H4F ' 69 - "E"
.db &H47 ' 70 - "F"
.db &H5E ' 71 - "G"
.db &H37 ' 72 - "H"
.db &H06 ' 73 - "I"
.db &H38 ' 74 - "J"
.db &H00 ' 75 - "K"
.db &H0E ' 76 - "L"
.db &H00 ' 77 - "M"
.db &H00 ' 78 - "N"
.db &H7E ' 79 - "O"
.db &H67 ' 80 - "P"
.db &H00 ' 81 - "Q"
.db &H00 ' 82 - "R"
.db &H5B ' 83 - "S"
.db &H00 ' 84 - "T"
.db &H3E ' 85 - "U"
.db &H00 ' 86 - "V"
.db &H00 ' 87 - "W"
.db &H00 ' 88 - "X"
.db &H38 ' 89 - "Y"
.db &H00 ' 90 - "Z"
.db &H4E ' 91 - "["
.db &H00 ' 92 - "\"
.db &H78 ' 93 - "]"
.db &H00 ' 94 - "^"
.db &H08 ' 95 - "_" - underscore
.db &H00 ' 96 - "`"
.db &H00 ' 97 - "a"
.db &H1F ' 98 - "b"
.db &H0D ' 99 - "c"
.db &H3D ' 100 - "d"
.db &H00 ' 101 - "e"
.db &H00 ' 102 - "f"
.db &H00 ' 103 - "g"
.db &H17 ' 104 - "h"
.db &H00 ' 105 - "i"
.db &H00 ' 106 - "j"
.db &H00 ' 107 - "k"
.db &H00 ' 108 - "l"
.db &H00 ' 109 - "m"
.db &H15 ' 110 - "n"
.db &H1D ' 111 - "o"
.db &H00 ' 112 - "p"
.db &H00 ' 113 - "q"
.db &H05 ' 114 - "r"
.db &H00 ' 115 - "s"
.db &H0F ' 116 - "t"
.db &H1C ' 117 - "u"
$end Asm
'===============================================================================