/*********************************************************
電機轉同時點陣顯示
********************************************************/
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit da =P2^7;
sbit shcp=P2^5;
sbit stcp=P2^6;
uint led_array_count,anode_next;
bit motor_flag=0,led_array_flag;
uchar anode,negative;
uchar a,b;
////////////////////////////////////////////
void delays(uchar ms)
{
uint a;
while(ms--) for(a=0;a<100;a++);
}
////////////////////////////////////////初始化定時器
void initial_timer0()
{
TMOD=0X02;
TH0=0x38; //100us
TL0=0x38;
TR0=1;
EA=1;
ET0=1;
}
///////////////////////////////////////////////////////////////////////
uchar code motor_run[]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09}; //最高頻率250us
/////////////////////////////////////////////////////////////////////
uchar code led_array_anode[]={ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xC1,0x01,0x00,0x00,0x00,0x00,0x80,0x83,
0x60,0x01,0x01,0x00,0x00,0x80,0x80,0x06,
0x30,0x00,0x01,0x01,0x80,0x80,0x00,0x0C,
0x18,0x00,0x00,0x81,0x81,0x00,0x00,0x18,
0x0C,0x00,0x80,0x80,0x01,0x01,0x00,0x30,
0x06,0x80,0x80,0x00,0x01,0x01,0x01,0x60,
0x83,0x80,0x00,0x00,0x00,0x00,0x01,0xC1,
0xE1,0x01,0x01,0x00,0x00,0x80,0x80,0x87,
0x62,0xC3,0x01,0x00,0x00,0x80,0xC3,0x46,
0x30,0x62,0x03,0x01,0x80,0xC0,0x46,0x0C,
0x08,0x14,0x22,0x41,0x82,0x44,0x28,0x10,
0x00,0x08,0x14,0x22,0x44,0x28,0x10,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,
0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xE0, //16
0x00,0x00,0x00,0x00,0x80,0xC0,0xE0,0xF0,
0x00,0x00,0x00,0x80,0xC0,0xE0,0xF0,0xF8,
0x00,0x00,0x80,0xC0,0xE0,0xF0,0xF8,0xFC,
0x00,0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,
0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xFF,
0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xFF,0xFF,
0xE0,0xF0,0xF8,0xFC,0xFE,0xFF,0xFF,0xFF,
0xF0,0xF8,0xFC,0xFE,0xFF,0xFF,0xFF,0xFF,
0xF8,0xFC,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFC,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0x00,0x7E,0x7E,0x7E,0x7E,0x7E,0x7E,0x00,
0x00,0x00,0x3C,0x3C,0x3C,0x3C,0x00,0x00,
0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00, //31
};
///////////////////////////////////////////////////////////////////
uchar led_array_negative[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; //負極
////////////////////////////////////// //595送數據
void input595(uchar dat)
{
uchar b;
shcp=0;
for(b=0;b<8;b++)
{
da=dat&0x80;
dat=dat<<1;
shcp=1;
shcp=0;
}
}
//////////////////////////////////// //595輸出數據
void output595()
{
stcp=0;
stcp=1;
}
//////////////////////////////////////////////////////////////////
void motor_foreward()
{
uint motor_time;
if(!led_array_flag)
{
motor_time++;
if(motor_time==5)
{
motor_time=0;
P3=motor_run[a];
a++;
if(a>=8)
{
a=0;
}
}
}
}
//////////////////////////////////////////////////////////////////////
void refresh_led_array() //刷新點陣
{
if(!led_array_flag)
{
anode_next++;
if(anode_next==100) //1s
{
anode_next=0;
anode=anode+8;
if(anode>240)
{
anode=0;
}
}
for(negative=0;negative<8;negative++)
{
input595(led_array_anode[anode+negative]);
input595(led_array_negative[negative]);
output595();
}
}
}
///////////////////////////////////////
void main()
{
initial_timer0();
// initial_timer1();
while(1)
{
refresh_led_array();
motor_foreward();
}
}
/////////////////////////////////////
void timer0_interrupt()interrupt 1
{
led_array_count++;
if(led_array_count==10) //1ms
{
led_array_count=0;
!led_array_flag;
}
}
|