單片機SPI模塊+74LS164驅(qū)動數(shù)碼管
#include
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000
#endif
const unsigned char seg_comAnode[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f};//共陽
const unsigned char seg_comCathode[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
0x87,0xff,0xef}; //共陰
/*同時發(fā)送和接收一個字節(jié),74LS164吸電流大于拉電流,數(shù)碼管接成上拉形式*/
unsigned char BUFF;
void init(){
TRISC=0B11010111;/*RC4/SDI引腳為輸出,RC5/SDO引腳為輸出,從模式RC3/SCK引腳為輸入*/
SSPSTAT=0b00000000;/* 在結(jié)束時采樣,在上升沿數(shù)據(jù)移位*/
SSPCON=0b00110010;/* SSPEN=1;CKP=1 , FOSC/64,*/
TRISD=0X00;
PORTD=0;
}
void In_74LS164(unsigned char datas){
SSPBUF=datas;
do
{
;
}while(SSPIF==0);/*等待發(fā)送完畢*/
SSPIF=0;/*清除SSPIF標志*/
//BUFF=SSPBUF;/*騰空緩沖器*/
}
void main()
{unsigned char b,a,i,s=0;unsigned char m=0x01;
init();
while(1){
for(i=0;i<4;i++)
{
PORTD=~(m<
for(s=0;s<9;s++){
In_74LS164( seg_comCathode[s]);
__delay_ms(100);__delay_ms(100);
}
}
for(i=1,a=m<<4;i<4;i++)
{
PORTD=~(a>>i);
for(s=0;s<9;s++){
In_74LS164( seg_comCathode[s]);
__delay_ms(100);__delay_ms(100);
}
}
}
}
評論