PIC16F877A開(kāi)發(fā)板 SPI總線(xiàn)驅(qū)動(dòng)74595實(shí)驗(yàn)
掃描二維碼
隨時(shí)隨地手機(jī)看文章
//*****************電子園PIC16F877A開(kāi)發(fā)板 SPI總線(xiàn)驅(qū)動(dòng)74595實(shí)驗(yàn)***************////CKP=0; 空閑為低電平//CKP=1; 空閑為高電平//STAT_CKE=0; SDO后沿采樣//STAT_CKE=1; SDO前沿采樣////STAT_SMP=0; //在數(shù)據(jù)輸出時(shí)間的中間采樣輸入數(shù)據(jù)//STAT_SMP=1; //在數(shù)據(jù)輸出時(shí)間的末尾采樣輸入數(shù)據(jù)////RC3/SCK 接74595 移位時(shí)鐘11腳//RC5/SDO 接74595 數(shù)據(jù)輸入14腳//RC4/SDI 接74595 鎖存時(shí)鐘12腳////mcu: PIC16F877A 4MHz//2010年7月10日16:11:08//***************************************************************************#include __CONFIG(HS&WDTDIS&LVPDIS&PWRTEN);//HS振蕩,禁止看門(mén)狗,低壓編程關(guān)閉,啟動(dòng)延時(shí)定時(shí)器constuchar smg_text[]={0xbf,0xfe,0xfb,0xef,0xdf,0x7f,0xfd,0xf7,};constuchar smg[]={0x0a,0xfa,0x8c,0xa8,0x78,0x29,0x09,0xba,0x08,0x28,0x00,};// 0 1 2 3 4 5 6 7 8 9 allconstuchar smg_bit[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0xff,};uchar c;uchar d;//********************函數(shù)定義*********************voiddelay1ms(uint DelayTime);uchar spi_write_read(uchar dd);//*************** 主程序 ********************voidmain(void){SSPM3=0;SSPM2=0;SSPM1=0;SSPM0=0; //spi主控模式,時(shí)鐘為Fosc/4CKP=0; //空閑為低電平STAT_CKE=1; //SDO前沿采樣STAT_SMP=0; //在數(shù)據(jù)輸出時(shí)間的中間采樣輸入數(shù)據(jù)TRISC3=0; //RC3/SCK為輸出TRISC5=0; //RC5/SDO為輸出TRISC4=0; //RC4/SDI為輸出(不使用SDI引腳時(shí),可設(shè)置為輸出,作普通io用)RC4=0;SSPEN=1; //使能MSSP模塊,啟用spi或iicdelay1ms(10);RC4=0; //產(chǎn)生鎖存時(shí)鐘spi_write_read(0x00);spi_write_read(0x00);RC4=1;delay1ms(500);for(d=0;d<8;d++){for(c=0;c<8;c++){RC4=0;spi_write_read(smg_bit[d]);spi_write_read(smg_text[c]);RC4=1;delay1ms(50);}}while(1){for(c=0;c<11;c++){RC4=0;spi_write_read(smg_bit[8]);spi_write_read(smg[c]);RC4=1;delay1ms(50);}}}//****************spi總線(xiàn)發(fā)送接收數(shù)據(jù)***************uchar spi_write_read(uchar dd){uchar buf;SSPBUF=dd;while(!STAT_BF); //等待數(shù)據(jù)發(fā)送接收完畢buf=SSPBUF;return(buf); //返回接收到的數(shù)據(jù)}//*****************延時(shí) n*1ms 12MHz*****************voiddelay1ms(uint DelayTime){uint temp;for(;DelayTime>0;DelayTime--){for(temp=0;temp<270;temp++){;}}}