Skip to content

Instantly share code, notes, and snippets.

@LongClipeus
Last active January 15, 2019 05:37
Show Gist options
  • Select an option

  • Save LongClipeus/983ca38476182cf351fd717a0f6d7548 to your computer and use it in GitHub Desktop.

Select an option

Save LongClipeus/983ca38476182cf351fd717a0f6d7548 to your computer and use it in GitHub Desktop.
Library led matrix
#include "BTL_ViXuLy.h"
#include "LedMatrix.h"
#include "Librarie.h"
/* ------------------ Config ------------------ */
#define ST PORTCbits.RC0 // CHAN ST
#define SH PORTCbits.RC1 // CHAN SH
#define DS0 PORTCbits.RC2 // CHAN D
#define DS1 PORTCbits.RC3 // CHAN C
#define LED_ROW_NUMBER 1
#define LED_COL_NUMBER 4
/* ------------------ Config ------------------ */
#define TEMP_MAX 40 // ngưỡng cảnh báo: nhiệt độ quá cao
#define TEMP_MIN 10 // ngưỡng cảnh báo: nhiệt độ quá thấp
#define LIGHT_TIME_TEMP 1
#define LIGHT_TIME_FLICKER_TEMP 10
#define SLEEP_TIME_TEMP 100
#define NUMBER_FLASH 1
#define SPEED_RUN_TEMP 1
#define LOOP_RUN_TEMP 10
/* -------------------------------------------- */
const unsigned int LedRow = LED_ROW_NUMBER << 3;
const unsigned int LedCol = LED_COL_NUMBER << 3;
const unsigned int MaxLenStr = (LED_COL_NUMBER << 2) / 3;
const char FontD[] = {0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE};
const char FontC[][8] = {
{0x7E, 0x85, 0x89, 0x91, 0x7E, 0x00, 0x00, 0x00}, /*0*/
{0x00, 0x21, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00}, /*1*/
{0x43, 0x85, 0x89, 0x91, 0x61, 0x00, 0x00, 0x00}, /*2*/
{0x82, 0x81, 0x91, 0xA9, 0xC6, 0x00, 0x00, 0x00}, /*3*/
{0x18, 0x28, 0x48, 0xFF, 0x08, 0x00, 0x00, 0x00}, /*4*/
{0xF2, 0x91, 0x91, 0x91, 0x8E, 0x00, 0x00, 0x00}, /*5*/
{0x3E, 0x51, 0x91, 0x91, 0x0E, 0x00, 0x00, 0x00}, /*6*/
{0x80, 0x80, 0x8F, 0x90, 0xE0, 0x00, 0x00, 0x00}, /*7*/
{0x6E, 0x91, 0x91, 0x91, 0x6E, 0x00, 0x00, 0x00}, /*8*/
{0x60, 0x91, 0x91, 0x92, 0x7C, 0x00, 0x00, 0x00}, /*9*/
{0x7F, 0x90, 0x90, 0x90, 0x7F, 0x00, 0x00, 0x00}, /*A ==> 10*/
{0xFF, 0x91, 0x91, 0x91, 0x6E, 0x00, 0x00, 0x00}, /*B ==> 11*/
{0x7E, 0x81, 0x81, 0x81, 0x42, 0x00, 0x00, 0x00}, /*C ==> 12*/
{0xFF, 0x81, 0x81, 0x42, 0x3C, 0x00, 0x00, 0x00}, /*D ==> 13*/
{0xFF, 0x91, 0x91, 0x91, 0x81, 0x00, 0x00, 0x00}, /*E ==> 14*/
{0xFF, 0x90, 0x90, 0x90, 0x80, 0x00, 0x00, 0x00}, /*F ==> 15*/
{0x7E, 0x81, 0x89, 0x8F, 0x08, 0x00, 0x00, 0x00}, /*G ==> 16*/
{0xFF, 0x10, 0x10, 0x10, 0xFF, 0x00, 0x00, 0x00}, /*H ==> 17*/
{0x81, 0x81, 0xFF, 0x81, 0x81, 0x00, 0x00, 0x00}, /*I ==> 18*/
{0x82, 0x81, 0xFE, 0x80, 0x80, 0x00, 0x00, 0x00}, /*J ==> 19*/
{0xFF, 0x18, 0x24, 0x42, 0x81, 0x00, 0x00, 0x00}, /*K ==> 20*/
{0xFF, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00}, /*L ==> 21*/
{0xFF, 0x20, 0x10, 0x20, 0xFF, 0x00, 0x00, 0x00}, /*M ==> 22*/
{0xFF, 0x20, 0x10, 0x08, 0xFF, 0x00, 0x00, 0x00}, /*N ==> 23*/
{0x7E, 0x81, 0x81, 0x81, 0x7E, 0x00, 0x00, 0x00}, /*O ==> 24*/
{0xFF, 0x90, 0x90, 0x90, 0x60, 0x00, 0x00, 0x00}, /*P ==> 25*/
{0x7E, 0x81, 0x85, 0x82, 0x7D, 0x00, 0x00, 0x00}, /*Q ==> 26*/
{0xFF, 0x98, 0x94, 0x92, 0x61, 0x00, 0x00, 0x00}, /*R ==> 27*/
{0x62, 0x91, 0x91, 0x91, 0x4E, 0x00, 0x00, 0x00}, /*S ==> 28*/
{0x80, 0x80, 0xFF, 0x80, 0x80, 0x00, 0x00, 0x00}, /*T ==> 29*/
{0xFE, 0x01, 0x01, 0x01, 0xFE, 0x00, 0x00, 0x00}, /*U ==> 30*/
{0xFC, 0x02, 0x01, 0x02, 0xFC, 0x00, 0x00, 0x00}, /*V ==> 31*/
{0xFF, 0x04, 0x08, 0x04, 0xFF, 0x00, 0x00, 0x00}, /*W ==> 32*/
{0xC7, 0x28, 0x10, 0x28, 0xC7, 0x00, 0x00, 0x00}, /*X ==> 33*/
{0xE0, 0x10, 0x0F, 0x10, 0xE0, 0x00, 0x00, 0x00}, /*Y ==> 34*/
{0x87, 0x89, 0x91, 0xA1, 0xC1, 0x00, 0x00, 0x00}, /*Z ==> 35*/
{0x02, 0x15, 0x15, 0x15, 0x0F, 0x00, 0x00, 0x00}, /*a ==> 36*/
{0xFF, 0x11, 0x11, 0x11, 0x0E, 0x00, 0x00, 0x00}, /*b ==> 37*/
{0x0E, 0x11, 0x11, 0x11, 0x02, 0x00, 0x00, 0x00}, /*c ==> 38*/
{0x0E, 0x11, 0x11, 0x0A, 0xFF, 0x00, 0x00, 0x00}, /*d ==> 39*/
{0x0E, 0x15, 0x15, 0x15, 0x0C, 0x00, 0x00, 0x00}, /*e ==> 40*/
{0x08, 0x3F, 0x48, 0x40, 0x20, 0x00, 0x00, 0x00}, /*f ==> 41*/
{0x18, 0x25, 0x25, 0x25, 0x3E, 0x00, 0x00, 0x00}, /*g ==> 42*/
{0x7F, 0x04, 0x08, 0x08, 0x07, 0x00, 0x00, 0x00}, /*h ==> 43*/
{0x00, 0x00, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00}, /*i ==> 44*/
{0x00, 0x02, 0x01, 0x11, 0x5E, 0x00, 0x00, 0x00}, /*j ==> 45*/
{0x00, 0x7F, 0x04, 0x0A, 0x11, 0x00, 0x00, 0x00}, /*k ==> 46*/
{0x00, 0x41, 0x7F, 0x01, 0x00, 0x00, 0x00, 0x00}, /*l ==> 47*/
{0x1F, 0x10, 0x08, 0x10, 0x0F, 0x00, 0x00, 0x00}, /*m ==> 48*/
{0x1F, 0x08, 0x10, 0x10, 0x0F, 0x00, 0x00, 0x00}, /*n ==> 49*/
{0x0E, 0x11, 0x11, 0x11, 0x0E, 0x00, 0x00, 0x00}, /*o ==> 50*/
{0x3F, 0x24, 0x24, 0x18, 0x00, 0x00, 0x00, 0x00}, /*p ==> 51*/
{0x18, 0x24, 0x24, 0x3F, 0x00, 0x00, 0x00, 0x00}, /*q ==> 52*/
{0x1F, 0x08, 0x10, 0x08, 0x00, 0x00, 0x00, 0x00}, /*r ==> 53*/
{0x09, 0x15, 0x15, 0x12, 0x00, 0x00, 0x00, 0x00}, /*s ==> 54*/
{0x10, 0x7E, 0x11, 0x01, 0x02, 0x00, 0x00, 0x00}, /*t ==> 55*/
{0x1E, 0x01, 0x01, 0x02, 0x1F, 0x00, 0x00, 0x00}, /*u ==> 56*/
{0x1C, 0x02, 0x01, 0x02, 0x1C, 0x00, 0x00, 0x00}, /*v ==> 57*/
{0x1E, 0x01, 0x02, 0x01, 0x1E, 0x00, 0x00, 0x00}, /*w ==> 58*/
{0x11, 0x0A, 0x04, 0x0A, 0x11, 0x00, 0x00, 0x00}, /*x ==> 59*/
{0x18, 0x05, 0x05, 0x05, 0x1E, 0x00, 0x00, 0x00}, /*y ==> 60*/
{0x11, 0x13, 0x15, 0x19, 0x11, 0x00, 0x00, 0x00}, /*z ==> 61*/
{0x70, 0x88, 0x88, 0x88, 0x70, 0x00, 0x00, 0x00}, /*oC ==> 62*/
{0xFF, 0xA9, 0x91, 0xA9, 0xFF, 0x00, 0x00, 0x00}, /*[x] ==> 63*/
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /*null == > 64*/
{0x00, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00}}; /*- == > 65*/
inline void pop() {
SH ^= SH;
SH ^= 1;
}
inline void push() {
ST = 1;
ST ^= SH;
}
/**
* resetLed: Tắt hết tất cả các led của led matrix
*/
void resetLed() {
DS0 ^= DS0;
DS1 ^= DS1;
unsigned int cnt = LedCol;
while (cnt--) {
pop();
}
push();
}
/**
* @param X: Số hàng chục của nhiệt độ cần hiển thị
* @param Y: Số hàng đơn vị của nhiệt độ cần hiển thị
* ShowTemperature(): Hiển thị nhiệt độ X, Y được truyền vào
*/
void ShowTemperature(char temp, char run) {
char tempArr[4];
if (temp & 0x80) { // temp < 0
// convert negative integer to integers
--temp;
temp ^= 0xff;
tempArr[0] = 65;
if (temp < 10)
tempArr[1] = temp;
else
tempArr[1] = 9;
} else {
if (temp < 10) {
tempArr[0] = 64;
tempArr[1] = temp;
} else if (10 <= temp && temp < 100) {
tempArr[0] = (temp / 5) >> 1;
tempArr[1] = temp % 10;
} else {
tempArr[0] = 9;
tempArr[1] = 9;
}
}
tempArr[2] = 62;
tempArr[3] = 12;
if (TEMP_MIN < temp && temp < TEMP_MAX) {
if (run) {
ShowRunLetter(tempArr, 4, SPEED_RUN_TEMP, LOOP_RUN_TEMP);
} else {
ShowLetter(tempArr, 4, LIGHT_TIME_TEMP);
}
} else {
FlickerLed(tempArr, 4, NUMBER_FLASH, LIGHT_TIME_FLICKER_TEMP, SLEEP_TIME_TEMP);
}
}
/**
* @param string: Chuối truyền vào cần hiển thị
* @param length: Độ dài của chuỗi StringShow
* @param cycleShow: Số lần chớp (tắt)
* FlickerLed(): Hiển thị hớp tắt chuỗi string được truyền vào
*/
void FlickerLed(char *string, int length, int cycleShow, int lightTime, int sleepTime) {
while (cycleShow--) {
ShowLetter(string, length, lightTime);
resetLed();
Timer_ms(sleepTime);
}
}
/**
* @param * string: Chuỗi truyền vào cần chuyển đổi
* @param length: Độ dài chuỗi cần chuyển đổi
* ChangeTheFont(): Chuyển đổi chuỗi truyền vào về dạng số tương ứng của ký tự đó trong bảng mã FontC
*/
void ChangeTheFont(char *string, int length) {
while (length--) {
if (47 < string[length] && string[length] < 58)
string[length] -= 48;
else if (64 < string[length] && string[length] < 91)
string[length] -= 55;
else if (96 < string[length] && string[length] < 123)
string[length] -= 61;
else if (string[length] == 32)
string[length] = 64;
else
string[length] = 63;
}
}
/**
* @param StringShow: Chuỗi truyền vào cần hiển thị
* @param length: Độ dài chuỗi StringShow truyền vào
* @param cycle_speed: Tốc độ hoặc chu kỳ cần hiển thị chuỗi StringShow
* @param number: Số lần chạy chữ ứng với trường hợp length của chuỗi StringShow > 5
* ShowLed(): Hàm quyết định hiển thị chuỗi StringShow theo dạng chạy chữ hay dạng hiển thị bình thường
*/
void ShowLed(char *string, int length, int cycle_speed, int loop) {
if (length < MaxLenStr)
ShowLetter(string, length, cycle_speed);
else
ShowRunLetter(string, length, cycle_speed, loop);
}
/**
* @param StringShow: Chuỗi truyền vào cần hiển thị
* @param length: Độ dài chuỗi được truyền vào
* @param cycleShow: Thời gian hiển thị chuỗi StringShow, được tính bằng số vòng lặp for
* ShowLetter(): Hiển thị chuỗi StringShow trong một khoảng thời gian là số (cycleShow) vòng lặp for
*/
void ShowLetter(char *string, int length, int lightTime) {
char posC, posD, bitC;
unsigned int row = LedRow, col;
while (lightTime--) {
posC = 0x80;
for (row = 0; row < LedRow; ++row) {
D = FontD[row];
posD = 0x80;
for (col = 0; col < LedCol; ++col) {
if (col > 23) {
DS0 = ((D & posD) ? 1 : 0); /*Lay bit dua vao chan DS0 hay còn gọi là láy bit vào chân D*/
posD >>= 1;
}
/*set font vàn chân C*/
if (col < 6)
bitC = FontC[string[0]][col];
else if (length > 1 && col < 12)
bitC = FontC[string[1]][col - 6];
else if (length > 2 && col < 18)
bitC = FontC[string[2]][col - 12];
else if (length > 3 && col < 24)
bitC = FontC[string[3]][col - 18];
else if (length == 5 && col < 30)
bitC = FontC[string[4]][col - 24];
else
bitC = FontC[64][0];
DS1 = ((bitC & posC) ? 1 : 0); /*Lay bit dua vao chan DS1 hay còn gọi là lấy bit vào chân C*/
pop();
}
push();
posC >>= 1; /*quét 8 hàng led*/
}
}
resetLed();
}
/**
* @param StringShow: Chuỗi truyền vào cần hiển thị
* @param length: Độ dài chuỗi truyền vào
* @param SpeedLed: Tốc độ chạy chữ
* @param number: Số lần lặp lại chạy chữ
* ShowRunLetter(): Hiển thị chạy chữ chuỗi StringShow được truyền vòa với tốc độ là SpeedLed và với số lần lặp lại là number
*/
void ShowRunLetter(char *StringShow, int length, int speedLed, int loop) {
int i, j;
char posC, posD, fontC;
int scanFont = 0;
int cntCol, cntRow, cntSpeed;
int lenCol = LedCol - 8;
int scanFL = 0;
length <<= 3;
while (loop) {
scanFL = LedCol - scanFont;
for (cntSpeed = 0; cntSpeed < speedLed; ++cntSpeed) {
posC = 0x80;
for (cntRow = 0; cntRow < LedRow; ++cntRow) {
/*lấy font vào chân D*/
D = FontD[cntRow];
posD = 0x80;
for (cntCol = 0; cntCol < lenCol; ++cntCol) {
if (cntCol < scanFL || cntCol > scanFL - 1 + length) { /*lấy font null*/
fontC = FontC[64][0];
} else { /*lấy font chữ*/
i = cntCol - scanFL;
j = i & 0x07; /*tmp2 = tmp1 % 8;*/
i >>= 3; /*tmp1 /= 8;*/
fontC = FontC[StringShow[i]][j];
}
DS1 = ((fontC & posC) ? 1 : 0);
pop();
}
while(cntCol < LedCol) {
DS0 = ((D & posD) ? 1 : 0);
posD >>= 1;
if (cntCol < scanFL || cntCol > scanFL - 1 + length) { /*lấy font null*/
fontC = FontC[64][0];
} else { /*lấy font chữ*/
i = cntCol - scanFL;
j = i & 0x07; /*tmp2 = tmp1 % 8;*/
i >>= 3; /*tmp1 /= 8;*/
fontC = FontC[StringShow[i]][j];
}
DS1 = ((fontC & posC) ? 1 : 0);
pop();
++cntCol;
}
push();
posC >>= 1; /*quét 8 hàng led*/
}
}
if (scanFont > LedCol + length) {
scanFont = 0;
--loop;
} else
++scanFont;
}
resetLed();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment