#include "reg51.h"
#include "intrins.h"
// QA1~8接交通燈
sbit SER = P3^4; // 74HC595串行數據
sbit RCK = P3^5; // 74HC595存儲寄存器時鐘
sbit SCK = P3^6; // 74HC595移位寄存器時鐘
typedef unsigned char u8;
// 行是高點平,列是低電平
void HC595(u8 colHigh, u8 colLow, u8 rowHigh, u8 rowLow)
{
int i;
u8 tempData;
RCK = 0;
SCK = 0;
// 先傳高位
for (i = 7; i >= 0; --i)
{
tempData = colHigh >> i;
SER = tempData & 0x01;
_nop_();
SCK = 1; // 上升沿移位
_nop_();
SCK = 0;
_nop_();
}
for (i = 7; i >= 0; --i)
{
tempData = colLow >> i;
SER = tempData & 0x01;
_nop_();
SCK = 1; // 上升沿移位
_nop_();
SCK = 0;
_nop_();
}
for (i = 7; i >= 0; --i)
{
tempData = rowHigh >> i;
SER = tempData & 0x01;
_nop_();
SCK = 1; // 上升沿移位
_nop_();
SCK = 0;
}
for (i = 7; i >= 0; --i)
{
tempData = rowLow >> i;
SER = tempData & 0x01;
_nop_();
SCK = 1; // 上升沿移位
_nop_();
SCK = 0;
_nop_();
}
_nop_();
RCK = 1; // 上升沿4個74HC595並口一起發出8位數據
_nop_();
}
void Delay(int i)
{
while (--i);
}
void main()
{
while (1)
{
int i;
for (i = 0; i < 8; ++i)
{
HC595(0xff, 0xfe, 0x00, 1 << i);
Delay(5000 * 10);
}
}
}