📄 matrix.c
字号:
#include <AT89S8253.H>
#include <stdio.h>
// ==== declare subroutine
void delay(unsigned long);
void init_timer0_m1();
// ==== declare global variables
int student_id[] = { 1, 3, 5, 7, 2, 4, 6, 8 }; // Your student ID
int temp = 0 ;
int temp_1 = 0 ;
unsigned char idata row_count = 0x01; //the row number of 8*8 LED martrix
// unsigned char idata mData[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; // store column data for 8*8 LED martrix
unsigned char idata mData[] = {0x81, 0x82, 0x84, 0x88, 0x10, 0x20, 0x40, 0x80};
//unsigned char idata stid_x[] = { 0,0,0,0,0,0,0,0 } ;
unsigned char idata lastkey = 0x00; //store which is the last key was pressed
unsigned char idata address; // chips address
unsigned char data LCDCont = 0x00; //00100000, [CS1] [CS2] [write] [command]
unsigned char data rotate_left;
unsigned char data rotate_right;
unsigned char data stop_rotate;
unsigned char data rotate_left1;
unsigned char data rotate_right1;
unsigned char data stop_rotate1;
int m;
int n ;
// 5x7 digits from 0 to 9
unsigned char code d0[] = { 0, 0x7c, 0xa2, 0x92, 0x8a, 0x7c, 0, 0 };
unsigned char code d1[] = { 0, 0 , 0x02, 0xFE, 0x42, 0, 0, 0 };
unsigned char code d2[] = { 0, 0x62, 0x92, 0x8a, 0x86, 0x42, 0, 0 };
unsigned char code d3[] = { 0, 0x8c, 0xd2, 0xa2, 0x82, 0x82, 0, 0 };
unsigned char code d4[] = { 0, 0x08, 0xFE, 0x48, 0x28, 0x18, 0, 0 };
unsigned char code d5[] = { 0, 0x9c, 0xa2, 0xa2, 0xa2, 0xe4, 0, 0 };
unsigned char code d6[] = { 0, 0x0c, 0x92, 0x92, 0x52, 0x3c, 0, 0 };
unsigned char code d7[] = { 0, 0xc0, 0xa0, 0x90, 0x8e, 0x80, 0, 0 };
unsigned char code d8[] = { 0, 0x6c, 0x92, 0x92, 0x92, 0x6c, 0, 0 };
unsigned char code d9[] = { 0, 0x78, 0x94, 0x92, 0x92, 0x60, 0, 0 };
// Assume student ID is 13572468
unsigned char code stid_r[] = { 0, 0, 0x02, 0xFE, 0x42, 0, 0, 0,
0, 0x8c, 0xd2, 0xa2, 0x82, 0x82, 0, 0,
0, 0x9c, 0xa2, 0xa2, 0xa2, 0xe4, 0, 0,
0, 0xc0, 0xa0, 0x90, 0x8e, 0x80, 0, 0,
0, 0x62, 0x92, 0x8a, 0x86, 0x42, 0, 0,
0, 0x08, 0xFE, 0x48, 0x28, 0x18, 0, 0 ,
0, 0x0c, 0x92, 0x92, 0x52, 0x3c, 0, 0 ,
0, 0x6c, 0x92, 0x92, 0x92, 0x6c, 0, 0,
0, 0, 0x02, 0xFE, 0x42, 0, 0, 0};
unsigned char code stid_l[] = { 0, 0, 0, 0x42, 0xFE, 0x02, 0, 0,
0, 0, 0x82, 0x82, 0xa2, 0xd2, 0x8c, 0,
0, 0, 0xe4, 0xa2, 0xa2, 0xa2, 0x9c, 0,
0, 0, 0x80, 0x8e, 0x90, 0xa0, 0xc0, 0,
0, 0, 0x42, 0x86, 0x8a, 0x92, 0x62, 0,
0, 0, 0x18, 0x28, 0x48, 0xFE, 0x08, 0 ,
0, 0, 0x3c, 0x52, 0x92, 0x92, 0x0c, 0 ,
0, 0, 0x6c, 0x92, 0x92, 0x92, 0x6c, 0,
0, 0, 0, 0x42, 0xFE};
//=== Main program start here=======================================================================
void main()
{
//=== initialize =====================
address = 0xF6; //none of devices connected to address 6
init_timer0_m1(); // timer0, mode 1(16-bit counter)
EA = 1; // Eanble selected interrup
TR0 = 1; // turn on Timer0, Timer0 for dot matrix LED
stop_rotate1 = 0 ;
for ( temp = 0 ; temp < 8 ; temp++ )
mData[temp] = stid_r[temp] ;
while( 1 )
{
// when RESET or P2_5 pressed
if (stop_rotate1 == 0)
{
rotate_left = P2 & 0x10; // P2_4
rotate_left1 = rotate_left ;
rotate_right = P2 & 0x40 ; // P2_6
rotate_right1 = rotate_right;
for ( temp = 0 ; temp < 8 ; temp++ )
{
switch(student_id[temp])
{
case 0 : for( temp_1=0; temp_1<8; temp_1++ ) mData[temp_1] = d0[temp_1] ; break ;
case 1 : for( temp_1=0; temp_1<8; temp_1++ ) mData[temp_1] = d1[temp_1] ; break ;
case 2 : for( temp_1=0; temp_1<8; temp_1++ ) mData[temp_1] = d2[temp_1] ; break ;
case 3 : for( temp_1=0; temp_1<8; temp_1++ ) mData[temp_1] = d3[temp_1] ; break ;
case 4 : for( temp_1=0; temp_1<8; temp_1++ ) mData[temp_1] = d4[temp_1] ; break ;
case 5 : for( temp_1=0; temp_1<8; temp_1++ ) mData[temp_1] = d5[temp_1] ; break ;
case 6 : for( temp_1=0; temp_1<8; temp_1++ ) mData[temp_1] = d6[temp_1] ; break ;
case 7 : for( temp_1=0; temp_1<8; temp_1++ ) mData[temp_1] = d7[temp_1] ; break ;
case 8 : for( temp_1=0; temp_1<8; temp_1++ ) mData[temp_1] = d8[temp_1] ; break ;
case 9 : for( temp_1=0; temp_1<8; temp_1++ ) mData[temp_1] = d9[temp_1] ;
}
for (n=0 ; n<20 ; n++)
{
delay(10000) ;
rotate_left = P2 & 0x10; // P2_4
rotate_left1 = rotate_left ;
rotate_right = P2 & 0x40 ; // P2_6
rotate_right1 = rotate_right;
if ( rotate_left1 == 0 | rotate_right1 == 0)
{
for ( temp = 0 ; temp < 8 ; temp++ )
mData[temp] = 0xff ;
break ;
}
}
}
}
if ( rotate_right1 == 0 )
{
for ( n = 0 ; n <65 ; n++ )
{
for ( temp = 0 ; temp < 8 ; temp++ )
mData[temp] = stid_r[temp+n] ;
delay(100000);
stop_rotate = P2 & 0x20; // check P2_5
stop_rotate1 = stop_rotate ;
if ( stop_rotate1 == 0 | rotate_left1 == 0)
{
break ;
}
}
} // end of while loop
else if (rotate_left1 == 0)
{
for (n=0 ; n<8 ; n++)
mData[n] = stid_r[n] ;
for ( n = 8 ; n <64 ; n++ )
{
for(temp = 7 ; temp > 0 ; temp-- )
mData[temp] = mData[temp-1];
mData[0] = stid_l[n] ;
delay(100000);
stop_rotate = P2 & 0x20; // check P2_5
stop_rotate1 = stop_rotate ;
if ( stop_rotate1 == 0 | rotate_right1 == 0 )
{
break ;
}
}
for (n=0 ; n<8 ; n++)
{
for( temp=7; temp>0; temp-- )
mData[temp] = mData[temp-1];
mData[0] = stid_l[n] ;
delay(100000);
stop_rotate = P2 & 0x20; // check P2_5
stop_rotate1 = stop_rotate ;
if ( stop_rotate1 == 0 | rotate_right1 == 0)
{
break ;
}
}
}
} // end of while loop
} //<<<<<<< end of "Main" >>>>>>>>>>
//=== Interrupt subroutine start here
// interrupt routine for timer 0, (8x8 Dot matrix LED)
void timer0() interrupt 1 using 1
{
TR0 = 0; // temporary suspend timer0, and restore data
row_count++;
if (row_count==8) row_count=0;
//P0 = mData[row_count] ; //put data to matrix column
P0 = mData[row_count] ;
P2 = (address & 0xF8) | 0x0D; // select matrix column
P2 = address; // unselect matrix column and restore default address bus value
P0 = row_count | LCDCont; //put dat to matrix row
P2 = (address & 0xF8) | 0x0E; // select matrix row
P2 = address; // unselect matrix row and restore default address bus value
TL0=0; //reload counter for next row
TH0=240; //margin value is 235
TF0=0; // clear the flag of timer0 overflow
TR0 = 1; // resume timer0 for counting
} /* end of ----interrupt routine for timer 0, (8x8 Dot matrix LED) */
// <<<<<< end of "Interrupt subroutine"
//== subroutine start here ==========
/* init Timer0 mode1 (16-bit counter) , user have to turn on TR0 */
void init_timer0_m1()
{
TR0 = 0; // set timer_0 run control 'STOP'
TMOD = (TMOD & 0xF0) | 0x01; //mode_1
ET0 = 1; //set tiemr0 interrupt ON
}
/* delay */
void delay(unsigned long x)
{
while (x>0) x--;
}
// <<<<< end of "subrotuine"
// <<<<<<<<<<< end of file >>>>>>>>>>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -