本帖最后由 cnhysc 于 2013-1-20 19:48 编辑
上代码,改天有空的时候整理一份原理图。。。
#include <at89x051.h>
//common part
#define HIGH 1
#define LOW 0
#define TRUE 1
#define FALSE 0
#define ZERO 0
#define MSB 0x80
#define LSB 0x01
//max7219 part
#define DECODE_MODE 0x09
#define INTENSITY 0x0A
#define SCAN_LIMIT 0x0B
#define SHUT_DOWN 0x0C
#define DISPLAY_TEST 0x0F
sbit LOAD = P1^2; //MAX7219 Load-Data Input: rising edge pin 12
sbit DIN = P1^3; //MAX7219 Serial-Data Input: rising edge pin 1
sbit CLK = P3^7; //MAX7219 Serial-Clock Input: maximum 10MHz pin 13
void delayms(unsigned int z)//延时函数
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void Write_Max7219_byte(unsigned char temp) //发送一个字节的子程序, 上升沿发送数据
{
unsigned char i;
for (i=0; i<8; i++)
{
CLK = LOW;
DIN = (bit)(temp&MSB);
temp <<=1;
CLK = HIGH;
}
}
void Write_Max7219(unsigned char address,unsigned char dat) //向寄存器中写入一个数据先写地址,后写数据load 上升沿锁存数据
{
LOAD = LOW;
Write_Max7219_byte(address);
Write_Max7219_byte(dat);
LOAD = HIGH;
}
void Init_Max7219(void) //初始化max7219 子函数 设置工作寄存器 需要查看芯片手册
{
Write_Max7219(SHUT_DOWN,0x01); //Normal Operation XXXXXXX1 Shutdown Mode XXXXXXXX0
Write_Max7219(DISPLAY_TEST,0x00); //Normal Operation XXXXXXX0 Display Test Mode XXXXXXXX1
Write_Max7219(DECODE_MODE,0xff); //Decode Mode Select D7~D0 1 B decode 0 No decode在非BCD译码态输入,高四位为8,9,A,B,C,D,E,F等数显示“.”低四位为正常数码管字型码。
// Write_Max7219(SCAN_LIMIT,0x07); //SCAN LIMIT 0~7 0xX0~0xX7
Write_Max7219(SCAN_LIMIT,0x04); //SCAN LIMIT 0~4 0xX0~0xX4
Write_Max7219(INTENSITY,0x04); //Set Intensity 0xX0~0xXf 强度调节
}
void main(void) //左移方式显示0~9数字
{
unsigned char i;
unsigned char j;
i=j=0;
Init_Max7219();
while(TRUE)
{
j = i;
Write_Max7219(1,0x80+j);
j++;
if(j>=10)
j=0;
Write_Max7219(2,0x80+j);
j++;
if(j>=10)
j=0;
Write_Max7219(3,0x80+j);
j++;
if(j>=10)
j=0;
Write_Max7219(4,0x80+j);
j++;
if(j>=10)
j=0;
Write_Max7219(5,0x80+j);
j++;
if(j>=10)
j=0;
delayms(500);
i++;
if(i>=10)
i=0;
}
}
|