?? 20081001_spi.c
字號:
/****************************************************************************
程序說明:
****************************************************************************/
#include <reg52.h>
sbit SPI_DATA = P1^0;
sbit SPI_CS = P1^1;
sbit SPI_CLK = P1^2;
sbit led = P1^7;
#define uint unsigned int
#define uchar unsigned char
void delayms(uint n) //延時10×n毫秒程序
{
uint i,j;
for(i=0;i<n;i++)
for(j=0;j<2000;j++);
}
/****************************************************************************
* 名稱:HC595_SendDat()
* 功能:向74HC595發(fā)送一字節(jié)數(shù)據(jù)
* 入口參數(shù):dat 要發(fā)送的數(shù)據(jù)
* 出口參數(shù):無
* 說明:發(fā)送數(shù)據(jù)時,高位先發(fā)送。
****************************************************************************/
void HC595_SendDat(uchar dat)
{ uchar i;
SPI_CS = 0; // SPI_CS = 0
delayms(40);
led=0;
for(i=0; i<8; i++) // 發(fā)送8位數(shù)據(jù)
{ SPI_CLK = 0; // SPI_CLK = 0
delayms(40);
/* 設(shè)置SPI_DATA輸出值 */
if( (dat&0x80)!=0 ) SPI_DATA = 1;
else SPI_DATA = 0;
delayms(20);
dat <<= 1;
SPI_CLK = 1; // SPI_CLK = 1
delayms(10);
}
SPI_CS= 1; // SPI_CS = 1,輸出顯示數(shù)據(jù)
led=1;
delayms(10);
}
main()
{
uchar i;
i=1;
while(1)
{
//HC595_SendDat(~0x08);
HC595_SendDat(~i);
//i++;
//if(i==16) i=1;
//delayms(5);
//HC595_SendDat(~0x02);
//delayms(5);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -