MAX7219
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//==================================================================================================
//**************************************************************************************************
//**** infrared emitting diode: tsal6200 ********************************************************
//**** infrared sensor: hs0038b ********************************************************
//**** display:max7219 ********************************************************
//**** mcu:at89s52 ********************************************************
//**** concact:thomas.yu@ ********************************************************
//**** finish time:2010-01-09 ********************************************************
//**** Version Control: Rev00 ********************************************************
//==================================================================================================
//========================================================================
#include
#include
//#include
//========================================================================
//common part
#define HIGH 1
#define LOW 0
#define TRUE 1
#define FALSE 0
#define ZERO 0
#define MSB 0x80
#define LSB 0x01
#define OVERH 0x0 //timer1 for speed of fan 50ms,overflow
#define OVERL 0x0 //timer1 for speed of fan 50ms,overflow
#define AVER 10
//max7219 part
#define DECODE_MODE 0x09
#define INTENSITY 0x0A
#define SCAN_LIMIT 0x0B
#define SHUT_DOWN 0x0C
#define DISPLAY_TEST 0x0F
//Timer2 Init
#define MSB_reload_value (65536-62633)/256;
#define LSB_reload_value (65536-62633)%256;
//========================================================================
//sbit RXD = P3^0; //
//sbit TXD = P3^1; //
//sbit INT0 = P3^2; //Infaread Rransmit Port ,INT0 Interrupt P3.2
//sbit INT1 = P3^3; //Infaread Teceive Port ,,INT1 Interrupt P3.3
//sbit T0 = P3^4; //Infaread Teceive Port ,,Timer0/Count0 Interrupt P3.4
//sbit T1 = P3^5; //Infaread Teceive Port ,,Timer1/Count1 Interrupt P3.5
unsigned char Overflow_Count;
unsigned char Overflow_Count_Buffer;
bit data Test_Switch = 0; //1:begin timer, 0:stop timer
sbit CLK = P2^0; //MAX7219 Load-Data Input: rising edge pin 12
sbit LOAD = P2^1; //MAX7219 Serial-Data Input: rising edge pin 1
sbit DIN = P2^2; //MAX7219 Serial-Clock Input: maximum 10MHz pin 13
unsigned int Ironic=0;
unsigned char TH1_Buffer;
unsigned char TL1_Buffer;
//////////////////////////////////////////////////////////////////////////
//========================================================================
//************Function Declare********************************************
//========================================================================
void Init_Max7219(void);
void Display_Max7219(unsigned int j);
void
Write_Max7219_byte(unsigned char temp);
void Write_Max7219(unsigned char address,unsigned char dat);
void Init_Common(void);
void Delay_X0ms(unsigned char n);
unsigned int Compute_Speed(unsigned char c,unsigned int a);
//////////////////////////////////////////////////////////////////////////
//========================================================================
//************Interrupt Service Routine***********************************
//========================================================================
void INFRARED_ISR_R (void) interrupt 0
{
TR1 = 0;
if(Test_Switch==1) //sign0=0,stop timer and count the speed
{
TL1_Buffer= TL1;
TH1_Buffer= TH1;
Overflow_Count_Buffer=Overflow_Count;
Overflow_Count=0;
Test_Switch=0;
}
else
{
Test_Switch=1;
TL1 = OVERL;
TH1 = OVERH;
TR1 = 1;
}
}
//------------------------------------------------------------------------
void It_Timer1 (void) interrupt 3
{
Overflow_Count++; // Increment the overflow count
TL0 = OVERL;
TH0 = OVERH;
TR0 = 1;
}
//////////////////////////////////////////////////////////////////////////
//==================================================================================================
//************MAIN ROGRAMME*************************************************************************
//==================================================================================================
void main (void)
{
Init_Common ();
Init_Max7219();
while(Ironic<55555) //display device self check
{
Display_Max7219(Ironic+=11111);
Delay_X0ms(20);
}
while (1)
{
Delay_X0ms(100);
EA=0;
Display_Max7219(TH1_Buffer*256+TL1_Buffer);//Compute_Speed(Overflow_Count_Buffer,TH1_Buffer*256+TL1_Buffer));
Overflow_Count_Buffer=0;
Test_Switch=0;
EA=1;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//************END MAIN ROGRAMME*********************************************************************
//==================================================================================================
//==================================================================================================
//************SUBPROGRAMME LIBRARY******************************************************************
//==================================================================================================
//========================================================================
//************Initialization_Common***************************************
//========================================================================
void Init_Common (void)
{
TMOD=0x11;//B 0000 0001; //timer0 is model 1
// Timer2 init
// TH2=MSB_reload_value; /* Init msb_value */
// TL2=LSB_reload_value; /* Init lsb_value */
// RCAP2H=MSB_reload_value; /* reload msb_value *
/
// RCAP2L=LSB_reload_value; /* reload lsb_value */
// CP_RL2=0; // 0=Reload, 1=Capture select
// C_T2 = 0; // 0=Timer, 1=Counter
// EXEN2= 0; // Timer 2 external enable
// TCLK = 0; // 0=Serial clock uses Timer 1 overflow, 1=Timer 2
// RCLK = 0; // 0=Serial clock uses Timer 1 overflow, 1=Timer 2
// EXF2 = 0; // Timer 2 external flag
// TF2 = 0; // Timer 2 overflow flag
// Interrupt Init
IT0=1; //set the trailing edge interrupt
// EX0=1; //open the external interrupt(external interrupt 0)
ET0=1; //open the timer0 interrupt
// EX1=1; //open the external interrupt
ET1=1; //open the timer1 interrupt
// ES=1; //open the serial port interrupt
// ET2=1; //open the timer2 interrupt
EA=1; //open the all interrupt
// TR2 = 1; // 0=Stop timer, 1=Start timer
}
//////////////////////////////////////////////////////////////////////////
//========================================================================
//************Compute_Speed With float************************************
//========================================================================
unsigned int Compute_Speed(unsigned char c,unsigned int a)
{
double b;
b=30000000/(c*65536+a);
a=b;
return(a);
}
//////////////////////////////////////////////////////////////////////////
//========================================================================
//************(2) Delay Time, n*10ms**************************************
//========================================================================
void Delay_X0ms(unsigned char n)
{
unsigned int f;
unsigned char g;
for(g=0;g
}
//////////////////////////////////////////////////////////////////////////
//========================================================================
//************(1) Display Max7219 with common cathode LED*****************
//========================================================================
//to diaplay bit5,bit4,bit3,bit2,bit1, total five Numbers
void Display_Max7219(unsigned int j)
{
unsigned char g,s,b,q,w;
w=j/10000;
q=(j%10000)/1000;
b=(j%1000)/100;
s=(j%100)/10;
g=j%10;
Write_Max7219(1,g);
Write_Max7219(2,s);
Write_Max7219(3,b);
Write_Max7219(4,q);
Write_Max7219(5,w);
}
//------------------------------------------------------------------------
//Display_Max7219:5,4,3,2,1,
//------------------------------------------------------------------------
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 = LOW;
Write_Max7219_byte(address);
Write_Max7219_byte(dat);
LOAD = HIGH;
}
//----------------------------
--------------------------------------------
void Init_Max7219(void)
{
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
Write_Max7219(SCAN_LIMIT, 0x04); //SCAN LIMIT 0~7 0xX0~0xX4
Write_Max7219(INTENSITY, 0x02); //Set Intensity 0xX0~0xXf
}
//////////////////////////////////////////////////////////////////////////
//==================================================================================================
//==================================================================================================
//======= E N D ====== E N D ==== E N D ====== E N D =======E N D ========