#include <stc15f2k60s2.h>
#include <intrins.h>
#define NOP() _nop_()
//74HC595 IO
sbit HC595_SI = P1^2;
sbit HC595_RCK = P1^3;
sbit HC595_SCK = P1^4;
sbit HC595_OE = P0^0; //¿ØÖÆ595Ëø´æÆ÷ʹÄܶˣ¬´Ó¶øʵÏÖÄ¿±êÇ°½ø£¬µ«µÆ²»ÁÁµÄ״̬
void Delay1ms(void);
void Delay_ms(unsigned int i);
void HC595SendData(unsigned int Val);
void Uart_Service(void);
void Uar
tinit(void);
unsigned int Fly_Val = 0xfffe; //595³õÖµ
unsigned char Flag_Time0 = 1; //¶¨Ê±Æ÷0ÑÓʱµ½Î»ºó±êÖ¾±äÁ¿
unsigned char Blink_Times = 0; //ÉÁ˸´ÎÊý
unsigned int Delay_Time = 0; //¶¨Ê±Æ÷0ÑÓʱÀÛ¼Ó±äÁ¿
unsigned int COM = 0; //Êý¾ÝºÏ³ÉÓñäÁ¿
unsigned char Uart_Command[7] = {0}; //
unsigned char Uart_Long = 0; //
void main()
{
UartInit();
HC595_OE = 0;
HC595SendData(0x5555);
Delay_ms(1000);
HC595SendData(0xaaaa);
while(1)
{
if(0 == Flag_Time0)
{
Flag_Time0 = 1;
HC595SendData(Fly_Val);
// Fly_Val = Fly_Val;
// COM = COM*2+1;
Fly_Val = (Fly_Val << 1) | (COM*2 + 1);
}
Uart_Service();
}
}
void Uart_Service(void)
{
if(Uart_Long == 7)
{
if((0xaa==Uart_Command[0])&&(0xf1==Uart_Command[1])&&(0xeb==Uart_Command[6]))
{
TR0 = 0;
}
if((0xaa==Uart_Command[0])&&(0xf2==Uart_Command[1])&&(0xeb==Uart_Command[6]))
{
TR0 = 1;
}
Uart_Long = 0;
}
}
void Ser_int(void) interrupt 4
{
ES = 0;
if (RI)
{
RI = 0; //Çå³ýRIλ
Uart_Command[Uart_Long++] = SBUF;
SBUF = Uart_Command[Uart_Long-1];
while(TI==1)
{
TI = 0;
}
}
ES = 1;
}
void Time0_int(void) interrupt 1
{
TF0 = 0;
TR0 = 0;
Delay_Time ++;
if(175 == Delay_Time)
{
Flag_Time0 = 0;
Delay_Time = 0;
Blink_Times ++;
}
if(Blink_Times == 16)
{
Blink_Times = 0;
Fly_Val = 0xfffe;
COM = 0;
}
TR0 = 1;
}
void Delay1ms() //@18.432MHz
{
unsigned char i, j;
i = 18;
j = 235;
do
{
while (--j);
} while (--i);
}
void Delay_ms(unsigned int i)
{
while(i--)
{
Delay1ms();
}
}
void HC595SendData(unsigned int Val)
{
unsigned char i;
for(i=0;i<16;i++)
{
if((Val<<i)&0x8000) HC595_SI = 1;
else HC595_SI = 0;
HC595_SCK=0;
NOP();
NOP();
HC595_SCK= 1;
}
HC595_RCK = 0;
NOP();
NOP();
HC595_RCK = 1;
}
void UartInit(void) //9600bps@18.432MHz
{
SCON = 0x50; //UART1:8λÊý¾Ý,¿É±ä²¨ÌØÂÊ
AUXR |= 0x40; //¶¨Ê±Æ÷1ʱÖÓΪFosc,¼´1T
AUXR &= 0xFE; //´®¿Ú1Ñ¡Ôñ¶¨Ê±Æ÷1Ϊ²¨ÌØÂÊ·¢ÉúÆ÷
TMOD &= 0x0F; //É趨¶¨Ê±Æ÷1Ϊ16λ×Ô¶¯ÖØ×°·½Ê½
TL1 = 0x20; //É趨¶¨Ê±³õÖµ
TH1 = 0xFE; //É趨¶¨Ê±Æ÷ÖØ×°Öµ
ET1 = 0; //½ûÖ¹¶¨Ê±Æ÷1ÖжÏ
TR1 = 1; //Æô¶¯¶¨Ê±Æ÷1
S2CON = 0x50; //UART2:8λÊý¾Ý,¿É±ä²¨ÌØÂÊ
AUXR |= 0x04; //¶¨Ê±Æ÷2ʱÖÓΪFosc,¼´1T
T2L = 0x20; //É趨¶¨Ê±³õÖµ
T2H = 0xFE; //É趨¶¨Ê±³õÖµ
AUXR |= 0x10; //Æô¶¯¶¨Ê±Æ÷2
AUXR |= 0X80; //¶¨Ê±Æ÷0¹¤×÷ÔÚ1T
TMOD &= 0XF0; //É趨¶¨Ê±Æ÷1Ϊ16λ×Ô¶¯ÖØ×°·½Ê½
TH0 = 0XF8; //¶¨Ê±100΢Ãë
TL0 = 0XCD; //¶¨Ê±100΢Ãë
ET1 = 1;
TF0 = 0; //Çå³ýTF0±êÖ¾
TR0 = 1;
IE = 0X82; //¿ªÆôÖжÏ
EA = 1;
ES = 1;
}
51单片机都是要用定时器的。
一周热门 更多>