// 2012-2 by naga lee
#include <reg51.h>
#include <intrins.h> //_nop_();
sbit Max7129_DIN = P1^3; // Serial-Data Input: rising edge
sbit Max7129_LOAD = P1^5; // Load-Data Input: rising edge
sbit Max7129_CLK = P1^4; // Serial-Clock Input: maximum 10MHz
unsigned char k1,k2,k3,k4,k5,k6,k7=0x00,k8=0x00;
unsigned int cnt=0;
#define MSB 0x80
#define LSB 0x01
#define HIGH 1
#define LOW 0
// max7129 暫存器說明
#define DECODE_MODE 0x09 // 解碼方式
#define INTENSITY 0x0A // 顯示亮度
#define SCAN_LIMIT 0x0B // 掃描位數
#define SHUT_DOWN 0x0C // 掉電模式
#define DISPLAY_TEST 0x0F // 顯示測試
// x是後 y是前
unsigned char code buf_x[10][8]={ {0x00,0x83,0x03,0x03,0x03,0x03,0x03,0x83},//0
{0x00,0x83,0x80,0x80,0x80,0x80,0x82,0x80},//1
{0x00,0x83,0x02,0x02,0x83,0x01,0x01,0x83},//2
{0x00,0x83,0x01,0x01,0x83,0x01,0x01,0x83},//3
{0x00,0x01,0x01,0x01,0x83,0x03,0x03,0x03},//4
{0x00,0x83,0x01,0x01,0x83,0x02,0x02,0x83},//5
{0x00,0x83,0x03,0x03,0x83,0x02,0x02,0x83},//6
{0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x83},//7
{0x00,0x83,0x03,0x03,0x83,0x03,0x03,0x83},//8
{0x00,0x83,0x01,0x01,0x83,0x03,0x03,0x83} //9
};
unsigned char code buf_y[10][8]={ {0x00,0x2c,0x0c,0x0c,0x0c,0x0c,0x0c,0x2c},//0-
{0x00,0x2c,0x20,0x20,0x20,0x20,0x24,0x20},//1-
{0x00,0x2c,0x04,0x04,0x2c,0x08,0x08,0x2c},//2-
{0x00,0x2c,0x08,0x08,0x2c,0x08,0x08,0x2c},//3-
{0x00,0x08,0x08,0x08,0x2c,0x0c,0x0c,0x0c},//4-
{0x00,0x2c,0x08,0x08,0x2c,0x04,0x04,0x2c},//5-
{0x00,0x2c,0x0c,0x0c,0x2c,0x04,0x04,0x2c},//6-
{0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x2c},//7-
{0x00,0x2c,0x0c,0x0c,0x2c,0x0c,0x0c,0x2c},//8-
{0x00,0x2c,0x08,0x08,0x2c,0x0c,0x0c,0x2c} //9-
};
//
void Max7219_WriteByte(unsigned char byte);
void Max7219_WriteData_01(unsigned char addr, unsigned char dat0,unsigned char dat1,unsigned char dat2,unsigned char dat3);
void Max7219_Init(void);
void Init_IRQ(void);
void Init_IRQ()
{
ET1 = 0x01; //Enable Timer Interrupt 1
TMOD = 0X10 ;
TH1 = 0X00 ;
TL1 = 0XA0 ;
EA = 0x01 ; //Enable All Interrupt
TR1 = 0X01 ; //START TIMER1
}
// 送出1byte:
// 上升沿發送數據
// MSB first
void Max7219_WriteByte(unsigned char byte)
{
unsigned char i;
for (i=0; i<8; i++)
{
_nop_();
Max7129_CLK = LOW;
Max7129_DIN = (bit)(byte & MSB);//運算結果=0即輸出0,不是0即輸出1
byte <<= 1;
Max7129_CLK = HIGH;
}
}
// write a byte to register
// 先address 後data
void Max7219_WriteData_01(unsigned char addr, unsigned char dat0,unsigned char dat1,unsigned char dat2,unsigned char dat3)
{
Max7129_LOAD = HIGH; // 使Max7129_LOAD HI
_nop_();
Max7129_LOAD = LOW; // 使Max7129_LOAD LOW 串列資料傳送ENABLE
_nop_();
Max7219_WriteByte(addr);
Max7219_WriteByte(dat0);
Max7219_WriteByte(addr);
Max7219_WriteByte(dat1);
Max7219_WriteByte(addr);
Max7219_WriteByte(dat2);
Max7219_WriteByte(addr);
Max7219_WriteByte(dat3);
_nop_();
Max7129_LOAD = HIGH; // 使Max7129_LOAD HI 串列資料傳送DISABLE
_nop_();
_nop_();
_nop_();
}
void OUT_0(void)
{
Max7219_WriteData_01(1, buf_x[k1][0]|buf_y[k2][0],buf_x[k3][0]|buf_y[k4][0],buf_x[k5][0]|buf_y[k6][0],buf_x[k7][0]|buf_y[k8][0]);
Max7219_WriteData_01(2, buf_x[k1][1]|buf_y[k2][1],buf_x[k3][1]|buf_y[k4][1],buf_x[k5][1]|buf_y[k6][1],buf_x[k7][1]|buf_y[k8][1]);
Max7219_WriteData_01(3, buf_x[k1][2]|buf_y[k2][2],buf_x[k3][2]|buf_y[k4][2],buf_x[k5][2]|buf_y[k6][2],buf_x[k7][2]|buf_y[k8][2]);
Max7219_WriteData_01(4, buf_x[k1][3]|buf_y[k2][3],buf_x[k3][3]|buf_y[k4][3],buf_x[k5][3]|buf_y[k6][3],buf_x[k7][3]|buf_y[k8][3]);
Max7219_WriteData_01(5, buf_x[k1][4]|buf_y[k2][4],buf_x[k3][4]|buf_y[k4][4],buf_x[k5][4]|buf_y[k6][4],buf_x[k7][4]|buf_y[k8][4]);
Max7219_WriteData_01(6, buf_x[k1][5]|buf_y[k2][5],buf_x[k3][5]|buf_y[k4][5],buf_x[k5][5]|buf_y[k6][5],buf_x[k7][5]|buf_y[k8][5]);
Max7219_WriteData_01(7, buf_x[k1][6]|buf_y[k2][6],buf_x[k3][6]|buf_y[k4][6],buf_x[k5][6]|buf_y[k6][6],buf_x[k7][6]|buf_y[k8][6]);
Max7219_WriteData_01(8, buf_x[k1][7]|buf_y[k2][7],buf_x[k3][7]|buf_y[k4][7],buf_x[k5][7]|buf_y[k6][7],buf_x[k7][7]|buf_y[k8][7]);
}
void Max7219_Init(void)
{
Max7219_WriteData_01(SHUT_DOWN, 0x01,0X01,0X01,0X01); // 掉電模式 : 1, 正常操作; 0, 睡眠模式
Max7219_WriteData_01(DISPLAY_TEST, 0x00,0X00,0X00,0X00); // 顯示測試 : 0, 正常模式; 1, 測試模式
Max7219_WriteData_01(DECODE_MODE, 0x00,0X00,0X00,0X00); // 解碼方式:0, 不解碼; 1, BCD解碼
Max7219_WriteData_01(SCAN_LIMIT, 0x07,0X07,0x07,0X07); // 掃描位數 : 0 ~ 7 最多支援8位數字
Max7219_WriteData_01(INTENSITY, 0x01,0X01,0X01,0X01); // 顯示亮度: 0 ~ F F最亮
}
void Timer_Interrupt_1() interrupt 3
{
cnt++;
TH1 = 0X01 ; //a8 ;
TL1 = 0X88 ;
}
void main(void)
{
Max7129_LOAD = HIGH; // 使Max7129_LOAD HI
Max7129_CLK = HIGH; // 使Max7129_CLK HI
Max7219_Init();
_nop_();
Init_IRQ();
while(1)
{
k1 = cnt % 10 ;
k2 = (cnt/10)%10 ;
k3 = (cnt/100)%10 ;
k4 = (cnt/1000)%10 ;
k5 = (cnt/10000)%10 ;
k6 = (cnt/100000)%10 ;
OUT_0();
}
}