本程序是基于PIC18F458控制LED數(shù)碼管顯示1~8數(shù)字的C語言程序,筆者也是剛剛學(xué)習(xí)PIC單片機(jī),也是和大家一起學(xué)習(xí)交流吧!不多說了,先貼程序:#include "p18f458.h"static volatile int table[20]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0XD8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x7f,0xbf,0x89,0xff};//段碼volatile unsigned char data;/*初始化子程序*/void initial(){TRISA=0x00; /*A口設(shè)置為輸出*/TRISC=0x00; /*SDO引腳為輸出,SCK引腳為輸出*/INTCON=0x00; /*關(guān)閉所有中斷*/SSPCON1=0x30; /*SSPEN=1;CKP=1,F(xiàn)OSC/4*/SSPSTAT=0xC0; /*時(shí)鐘下降沿發(fā)送數(shù)據(jù)*/PIR1=0; /*清除SSPIF標(biāo)志*/}/*顯示子程序*/void SPILED(char data){PORTAbits.RA5=0;/*LACK送低電平,為鎖存做準(zhǔn)備*/SSPBUF=data; /*啟動(dòng)發(fā)送*/do{;}while(PIR1bits.SSPIF==0); /*等待發(fā)送完畢*/PIR1bits.SSPIF=0; /*清除SSPIF標(biāo)志*/PORTAbits.RA5=1; /*最后給鎖存信號(hào),代表顯示任務(wù)完成*/}/*主程序*/main(){unsigned i;initial(); /*系統(tǒng)初始化*/for(i=8;i>0;i--) /*連續(xù)發(fā)送8個(gè)數(shù)據(jù)*/{data=table[i]; /*通過數(shù)組的轉(zhuǎn)換獲得待顯示的段碼*/SPILED(data); /*發(fā)送顯示段碼顯示*/ }for(;;)//無限循環(huán){}}
評(píng)論