ATY_LIB V2_102_230218
ATY_LIB for general devices or ALGO
 
Loading...
Searching...
No Matches
max7219_8x8led_4in1_stc8g1k08.c
Go to the documentation of this file.
1// Copyright 2021 IOsetting <iosetting(at)outlook.com>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
50#include "fw_hal.h"
51
52#define MAX7219_CS P55
53#define MAX7219_BLOCKS 4
54
55#define DECODE_MODE 0x09
56#define INTENSITY 0x0A
57#define SCAN_LIMIT 0x0B
58#define SHUT_DOWN 0x0C
59#define DISPLAY_TEST 0x0F
60
62 0x3e,0x63,0x63,0x7f,0x63,0x63,0x63,0x63, //A
63 0x7e,0x63,0x63,0x7e,0x63,0x63,0x63,0x7e, //B
64 0x3e,0x63,0x63,0x60,0x60,0x63,0x63,0x3e, //C
65 0x7e,0x63,0x63,0x63,0x63,0x63,0x63,0x7e, //D
66 0x7f,0x60,0x60,0x7f,0x60,0x60,0x60,0x7f, //E
67 0x7f,0x60,0x60,0x7e,0x60,0x60,0x60,0x60, //F
68 0x3e,0x63,0x63,0x60,0x67,0x63,0x63,0x3e, //G
69 0x63,0x63,0x63,0x7f,0x63,0x63,0x63,0x63, //H
70 0x3f,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x3f, //I
71 0x1f,0x06,0x06,0x06,0x06,0x66,0x66,0x3c, //J
72 0x63,0x66,0x6c,0x78,0x6c,0x66,0x63,0x61, //K
73 0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x7f, //L
74 0x63,0x77,0x7f,0x6b,0x63,0x63,0x63,0x63, //M
75 0x63,0x63,0x73,0x7b,0x6f,0x67,0x63,0x63, //N
76 0x3e,0x63,0x63,0x63,0x63,0x63,0x63,0x3e, //O
77 0x7e,0x63,0x63,0x63,0x7e,0x60,0x60,0x60, //P
78 0x3c,0x66,0x66,0x66,0x66,0x6e,0x66,0x3f, //Q
79 0x7e,0x63,0x63,0x63,0x7e,0x6c,0x66,0x63, //R
80 0x3e,0x63,0x63,0x60,0x3e,0x03,0x63,0x3e, //S
81 0x3f,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, //T
82 0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x3e, //U
83 0x63,0x63,0x63,0x63,0x63,0x36,0x1c,0x08, //V
84 0x63,0x63,0x63,0x63,0x6b,0x7f,0x77,0x63, //W
85 0x63,0x63,0x36,0x1c,0x1c,0x36,0x63,0x63, //X
86 0x33,0x33,0x33,0x33,0x1e,0x0c,0x0c,0x0c, //Y
87 0x7f,0x03,0x06,0x0c,0x18,0x30,0x60,0x7f, //Z
88 0x3e,0x63,0x73,0x6b,0x67,0x63,0x63,0x3e, //0
89 0x0c,0x1c,0x3c,0x0c,0x0c,0x0c,0x0c,0x3f, //1
90 0x3e,0x63,0x63,0x06,0x0c,0x18,0x30,0x7f, //2
91 0x3e,0x63,0x63,0x0e,0x03,0x63,0x63,0x3e, //3
92 0x06,0x0e,0x1e,0x36,0x66,0x7f,0x06,0x06, //4
93 0x7f,0x60,0x60,0x7e,0x03,0x03,0x03,0x7e, //5
94 0x3e,0x63,0x60,0x7e,0x63,0x63,0x63,0x3e, //6
95 0x7f,0x03,0x03,0x06,0x0c,0x18,0x18,0x18, //7
96 0x3e,0x63,0x63,0x3e,0x63,0x63,0x63,0x3e, //8
97 0x3e,0x63,0x63,0x63,0x3f,0x03,0x63,0x3e, //9
98};
99
102
104{
105 index = index % MAX7219_BLOCKS;
106 MAX7219_CS = 0;
107 SPI_TxRx(addr);
108 SPI_TxRx(dat);
109 while(index--)
110 {
111 SPI_TxRx(addr);
112 SPI_TxRx(dat);
113 }
114 MAX7219_CS = 1;
115}
116
118{
119 MAX7219_CS = 0;
120 while (len--)
121 {
122 SPI_TxRx(addr);
123 SPI_TxRx(*dat++);
124 }
125 MAX7219_CS = 1;
126}
127
128void MAX7219_init(void)
129{
130 for (uint8_t i = 0; i < MAX7219_BLOCKS; i++)
131 {
132 MAX7219_singeWrite(i, SHUT_DOWN, 0x01); // 0x00:shutdown, 0x01:normal
133 MAX7219_singeWrite(i, DECODE_MODE, 0x00); // Bypass code B decoder, no-decode operation
134 MAX7219_singeWrite(i, SCAN_LIMIT, 0x07); // Scan-limit, 0:1-digit, 1:2-digits, ..., 7:8-digits
135 MAX7219_singeWrite(i, INTENSITY, 0x00); // 0x00:min, 0xFF:max
136 MAX7219_singeWrite(i, DISPLAY_TEST, 0x00); // 0x00:normal, 0x01:test mode
137 }
138}
139
140void SPI_init(void)
141{
142 // MAX7219 can work with frequency up to 20MHz
144 // Clock idles low
146 // Data transfer is driven by lower SS pin
148 // MSB first
150 // Define the output pins
152 // Ignore SS pin, use MSTR to swith between master/slave mode
154 // Master mode
156 // Start SPI
158}
159
160void main()
161{
171 uint16_t pos = 0, lpos = 0;
172 uint8_t i, j, cpos = 0, bpos = 0, tcpos = 0;
173
174 SYS_SetClock();
175 // Configure GPIO pins before SPI and device
176 // SCLK(P32), CS(P55)
179 // MOSI(P54)
181 // Configure SPI and device
182 SPI_init();
183 MAX7219_init();
184
185 while(1)
186 {
187 lpos = pos + sizeof(numbers) - MAX7219_BLOCKS * 8;
188 cpos = lpos / 8; // char position
189 bpos = lpos % 8; // bit position
190 for (i = 0; i < 8; i++) // fill each line
191 {
192 for (j = 0; j < MAX7219_BLOCKS; j++)
193 {
194 tcpos = (cpos + j) % character_len;
195 val[j] = numbers[tcpos * 8 + i] << bpos;
196 tcpos = (cpos + j + 1) % character_len;
197 val[j] |= numbers[tcpos * 8 + i] >> (8 - bpos);
198 }
200 }
201 pos = (pos + 1) % sizeof(numbers);
202 SYS_Delay(100);
203 }
204}
uint8_t pos
__CODE int8_t dat[20]
#define GPIO_P5_SetMode(__PINS__, __MODE__)
Definition: fw_gpio.h:99
#define GPIO_P3_SetMode(__PINS__, __MODE__)
Definition: fw_gpio.h:89
@ GPIO_Pin_5
Definition: fw_gpio.h:50
@ GPIO_Pin_2
Definition: fw_gpio.h:47
@ GPIO_Pin_4
Definition: fw_gpio.h:49
@ GPIO_Mode_Output_PP
Definition: fw_gpio.h:24
@ GPIO_Mode_InOut_QBD
Definition: fw_gpio.h:23
#define SPI_SetClockPrescaler(__PRE_SCALER__)
Definition: fw_spi.h:88
#define SPI_SetPort(__ALTER_PORT__)
Definition: fw_spi.h:92
#define SPI_SetDataOrder(__ORDER__)
Definition: fw_spi.h:70
@ SPI_ClockPreScaler_4
Definition: fw_spi.h:45
#define SPI_SetEnabled(__STATE__)
Definition: fw_spi.h:69
#define SPI_SetClockPolarity(__STATE__)
Definition: fw_spi.h:78
uint8_t SPI_TxRx(uint8_t dat)
Definition: fw_spi.c:20
#define SPI_SetClockPhase(__PHASE__)
Definition: fw_spi.h:84
@ SPI_ClockPhase_LeadingEdge
Definition: fw_spi.h:53
#define SPI_IgnoreSlaveSelect(__STATE__)
Definition: fw_spi.h:68
#define SPI_SetMasterMode(__STATE__)
Definition: fw_spi.h:71
@ SPI_AlterPort_P12P54_P13_P14_P15
Definition: fw_spi.h:36
@ SPI_DataOrder_MSB
Definition: fw_spi.h:59
void SYS_SetClock(void)
Definition: fw_sys.c:40
void SYS_Delay(uint16_t t)
Definition: fw_sys.c:65
unsigned short uint16_t
Definition: fw_types.h:19
@ HAL_State_OFF
Definition: fw_types.h:70
@ HAL_State_ON
Definition: fw_types.h:71
unsigned char uint8_t
Definition: fw_types.h:18
uint8_t addr[8]
Definition: main.c:28
#define DECODE_MODE
void MAX7219_multiWrite(uint8_t addr, uint8_t len, uint8_t *dat)
#define SCAN_LIMIT
#define SHUT_DOWN
#define INTENSITY
#define DISPLAY_TEST
void MAX7219_init(void)
const uint8_t numbers[]
uint8_t character_len
#define MAX7219_CS
void MAX7219_singeWrite(uint8_t index, uint8_t addr, uint8_t dat)
#define MAX7219_BLOCKS
uint8_t val[MAX7219_BLOCKS]
void SPI_init(void)
uint8_t __XDATA i