#include <stc8a8k.h>
#include <intrins.h>
#include <stdlib.h>
#include <stdio.h>
unsigned int Global_System_Tim0_Ms=0;
#define LongToBin(n) \
(\
((n >> 21) & 0x80) | \
((n >> 18) & 0x40) | \
((n >> 15) & 0x20) | \
((n >> 12) & 0x10) | \
((n >> 9) & 0x08) | \
((n >> 6) & 0x04) | \
((n >> 3) & 0x02) | \
((n ) & 0x01) \
)
#define B(n) LongToBin(0x##n##l)
#define lowByte(w) ((w) & 0xff)
#define highByte(w) ((w) >> 8)
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bit(b) (1 << (b))
sbit LED=P1^7;
sbit HC595_LATCH = P1^0;
sbit HC595_OE = P1^1;
void portmode();
void SendTo595( unsigned char byteData);
void SpiTransfer(unsigned char transferbyteTemp);
void Timer0Init(void) ;
unsigned int Get_millis(void);
void Delay10us();
void Delay30us();
void Delay500ms();
UART1 发送串口数据,字符串
void UartSendStr(char *p);
void UART1_SendData(char dat);
void Uart1_Init(void);
bit busy;
void main()
{
unsigned char c;
long starttime=0;
busy = 0;
portmode();
LED=0;Delay500ms();
LED=1;Delay500ms();
LED=0;Delay500ms();
LED=1;Delay500ms();
SPCTL = 0x50;
SPSTAT = 0xc0;
IE2 = ESPI;
EA = 1;
HC595_LATCH =1;
HC595_OE =0;
Timer0Init();
Uart1_Init();
EA=1;
TR0=0;
while(1)
{
SpiTransfer(B(00000000));
Global_System_Tim0_Ms=0;
TR0=1;
while(Global_System_Tim0_Ms <100)
{
}
TR0=0;
printf("time0\r\n");
SpiTransfer(B(00000001));
Global_System_Tim0_Ms=0;
TR0=1;
while(Global_System_Tim0_Ms <100)
{
}
TR0=0;
printf("time1\r\n");
SpiTransfer(B(00000010));
Global_System_Tim0_Ms=0;
TR0=1;
while(Global_System_Tim0_Ms <100)
{
}
TR0=0;
printf("time2\r\n");
SpiTransfer(B(00000001));
Global_System_Tim0_Ms=0;
TR0=1;
while(Global_System_Tim0_Ms <100)
{
}
TR0=0;
printf("time3\r\n");
SpiTransfer(0xaa);
Global_System_Tim0_Ms=0;
TR0=1;
while(Global_System_Tim0_Ms <100)
{
}
TR0=0;
printf("aaaa\r\n");
SpiTransfer(0xbb);
Global_System_Tim0_Ms=0;
TR0=1;
while(Global_System_Tim0_Ms <100)
{
}
TR0=0;
printf("bb\r\n");
SpiTransfer(0xcc);
Global_System_Tim0_Ms=0;
TR0=1;
while(Global_System_Tim0_Ms <100)
{
}
TR0=0;
printf("cc\r\n");
SpiTransfer(0xdd);
Global_System_Tim0_Ms=0;
TR0=1;
while(Global_System_Tim0_Ms <100)
{
}
TR0=0;
printf("dd\r\n");
SpiTransfer(0xee);
Global_System_Tim0_Ms=0;
TR0=1;
while(Global_System_Tim0_Ms <100)
{
}
TR0=0;
printf("ee\r\n");
SpiTransfer(0xff);
Global_System_Tim0_Ms=0;
TR0=1;
while(Global_System_Tim0_Ms <100)
{
}
TR0=0;
printf("ff\r\n");
}
}
void portmode()
{
P0M0=0x00;P0M1=0x00;
P1M0=0x00;P1M1=0x00;
P2M0=0x00;P2M1=0x00;
P3M0=0x00;P3M1=0x00;
P4M0=0x00;P4M1=0x00;
P5M0=0x00;P5M1=0x00;
P6M0=0x00;P6M1=0x00;
P7M0=0x00;P7M1=0x00;
}
void SPI_Isr() interrupt 9
{
SPSTAT = 0xc0;
HC595_LATCH = 1;
busy = 0;
}
void SpiTransfer(unsigned char transferbyteTemp)
{
busy = 1;
HC595_LATCH = 0;
SPDAT = transferbyteTemp;
while (busy);
}
void Timer0Init(void)
{
AUXR &= 0x7F;
TMOD &= 0xF0;
TL0 = 0x00;
TH0 = 0xB8;
TF0 = 0;
TR0 = 0;
ET0=1;
}
void TM0_Isr() interrupt 1
{
Global_System_Tim0_Ms++;
if(Global_System_Tim0_Ms>10000)
{
Global_System_Tim0_Ms=0;
}
}
unsigned int Get_millis(void)
{
return Global_System_Tim0_Ms;
}
void Uart1_Init(void)
{
SCON = 0x50;
AUXR |= 0x01;
AUXR &= 0xFB;
T2L = 0xFC;
T2H = 0xFF;
AUXR |= 0x10;
}
char putchar(char c)
{
UART1_SendData(c);
return c;
}
void UART1_SendData(char dat)
{
ES=0;
SBUF=dat;
while(TI!=1);
TI=0;
ES=1;
}
void UartSendStr(char *p)
{
while (*p)
{
UART1_SendData(*p++);
}
}
void Delay10us()
{
unsigned char i;
i = 71;
while (--i);
}
void Delay500ms()
{
unsigned char i, j, k;
i = 57;
j = 27;
k = 112;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay30us()
{
unsigned char i;
i = 219;
while (--i);
}
