- #include
- #pragma config JTAGEN =OFF
- char Led[]={0x42, 0xf3, 0x86, 0xa2, 0x33, 0x2a, 0x0a, 0xf2, 0x02, 0x22, 0x40, 0xf1, 0x84, 0xa0, 0x31, 0x28, 0x08, 0xf0, 0x00, 0x20, 0x1e, 0x0e, 0x0f, 0xbf, 0x23, 0x9b, 0x8b};
- //led字庫
- void spiout(char image[],int len)
- {
- int i;
- PORTClearBits(IOPORT_B, BIT_9);
- for (i = 0; i < len; i++)
- {
- SpiChnPutC(2, image[i]);
- }
- for(i=0;i<2;i++);//延時使數據傳輸完后鎖存,通常數碼管亂碼都是這導致的。
- PORTSetBits(IOPORT_B, BIT_9);//數據鎖存
- }
- int main()
- {
- SpiOpenFlags oFlags = SPI_OPEN_MSTEN | SPI_OPEN_CKP_HIGH | SPI_OPEN_MODE8 | SPI_OPEN_ON;//作為主機, ,8位數據模式,SPI使能
- PORTSetPinsDigitalOut(IOPORT_B, BIT_9);//外部移位寄存器數據鎖存,1鎖存,0開放
- SpiChnOpen(2, oFlags, 6);//打開通道2即SDO2,配置SPI,fpbDiv(2~1024).波特率BR=Fpb/fpbDiv
- PPSOutput(2, RPB8, SDO2);//輸出針腳組2中,查表將針腳RPB8,作為數據輸出2口SDO2.實際連線也是如此
- spiout(Led,4);
- }
-
- //PPSOutput(2,RPB5,SDO1);//輸出針腳組2中,查表將針腳RPB5,作為數據輸出1口SDO1.
- //也可查數據手冊用另一種,以后再說
- //查找頭文件PPSOUT.h在C:\Program Files (x86)\Microchip\xc32\v1.21\pic32-libs\include\peripheral
- {
- Output Pin Group 2
- #define OUT_PIN_PPS2_RPA1 RPA1Rbits.RPA1R
- #define OUT_PIN_PPS2_RPB5 RPB5Rbits.RPB5R
- #define OUT_PIN_PPS2_RPB1 RPB1Rbits.RPB1R
- #define OUT_PIN_PPS2_RPB11 RPB11Rbits.RPB11R
- #define OUT_PIN_PPS2_RPB8 RPB8Rbits.RPB8R
- #define OUT_PIN_PPS2_RPA8 RPA8Rbits.RPA8R
- #define OUT_PIN_PPS2_RPC8 RPC8Rbits.RPC8R
- #define OUT_PIN_PPS2_RPA9 RPA9Rbits.RPA9R
- //#define OUT_FN_PPS2_SDO1 3 // RPn tied to SDO1
- //#define OUT_FN_PPS2_SDO2 4 // RPn tied to SDO2
- //#define OUT_FN_PPS2_OC2 5 // RPn tied to OC2
- }
-
- 秒表
- #include
- #pragma config JTAGEN=OFF
- #pragma config FPLLIDIV = DIV_2 //振蕩器配置
- #pragma config FPLLMUL = MUL_24
- #pragma config FPLLODIV = DIV_2
- #pragma config FNOSC = FRCPLL
- #pragma config FPBDIV = DIV_1 //8MHZ/2*24/2/1=48MHZ
- #pragma config FWDTEN = OFF
- #pragma POSCMOD = OFF
- #define PERIOD 48000 //t=ps*period/48000000=0.001s
- int n=0,sta=0,ledflag=1,butflag=0,i=0,b=0,bit1=0,bit2=0;;
- char t[4],Led[]={0x42, 0xf3, 0x86, 0xa2, 0x33, 0x2a, 0x0a, 0xf2, 0x02, 0x22, 0x40, 0xf1, 0x84, 0xa0, 0x31, 0x28, 0x08, 0xf0, 0x00, 0x20, 0x1e, 0x0e, 0x0f, 0xbf, 0x23, 0x9b, 0x8b},image[]={0x42,0x42,0x42,0x42};
- //Led 字庫
- void timer1init()//時鐘源1初始化,配置中斷等
- {
- OpenTimer1(T1_ON|T1_SOURCE_INT|T1_PS_1_1,PERIOD);
- ConfigIntTimer1(T1_INT_ON|T1_INT_PRIOR_2|T1_INT_SUB_PRIOR_0);
- INTEnableSystemMultiVectoredInt();
- }
- void spiinit()//SPI初始化
- {
- SpiOpenFlags oFlags = SPI_OPEN_MSTEN | SPI_OPEN_CKP_HIGH | SPI_OPEN_MODE8 | SPI_OPEN_ON;//作為主機,,8位數據模式,SPI使能
- SpiChnOpen(2, oFlags, 6);//打開通道2即SDO2,配置SPI,fpbDiv(2~1024).波特率BR=Fpb/fpbDiv
- PPSOutput(2, RPB8, SDO2);//輸出針腳組2中,查表將針腳RPB8,作為數據輸出2口SDO2.實際連線也是如此
- PORTSetPinsDigitalOut(IOPORT_B,BIT_9);//外部移位寄存器SLCK數據鎖存,1鎖存,0開放
- PORTSetBits(IOPORT_B, BIT_9);//先置一鎖存數據
- }
- void __ISR(_TIMER_1_VECTOR,ipl2) timer(void)//中斷函數0.001s一次中斷
- {
- INTClearFlag(INT_T1);
- n++;
- if(n>100)//0.1s
- {
- n=0;
- ledflag=1;
- // PORTBINV=(1<<7); RB7口led燈與按鍵AN1的RA0并聯沖突,不能同時使用
- }
-
- }
- void buttoninit()//按鍵初始化
- {
- // ANSELAbits.ANSA0 = 0; //Button1
- // ANSELAbits.ANSA1 = 0; //Button2
- PORTSetPinsDigitalIn(IOPORT_A,BIT_0|BIT_1);//按鍵電位檢測讀數據
- PORTSetPinsDigitalIn(IOPORT_B,BIT_3|BIT_14);//需將對應的按鍵針腳位設為數字輸入
- }
- void spiout(char im[])//SPI數據輸出函數
- {
- PORTClearBits(IOPORT_B, BIT_9);
- for(i=0;i<4;i++)
- {
- SpiChnPutC(2,im[i]);//2號通道依次傳輸數據
- }
- for(i=0;i<2;i++);//延時使數據傳輸完后鎖存,通常數碼管亂碼都是這導致的。
- PORTSetBits(IOPORT_B, BIT_9);//數據鎖存
- }
- void led(char imag[3])//秒表換算函數
- {
- t[0]++;
- if(t[0]>9)
- {
- t[0]=0;
- t[1]++;
- if(t[1]>19)
- {
- t[1]=10;
- t[2]++;
- if(t[2]>9)
- {
- t[2]=0;
- t[3]++;
- if(t[3]>9)
- t[3]=0;
- }
- }
- }
- image[0]=Led[t[3]];
- image[1]=Led[t[2]];
- image[2]=Led[t[1]];
- image[3]=Led[t[0]];
- spiout(image); //四個數據相互變換
- }
-
- void main()//主函數
- {
-
- timer1init();
- spiinit();
- buttoninit();
- PORTSetPinsDigitalOut(IOPORT_B,BIT_13);
- PORTSetBits(IOPORT_B,BIT_13);
- while(1)//死循環檢測按鍵狀態和不斷投影數碼管
- {
-
- if(PORTAbits.RA0==0)
- {
- bit1++;
- if(bit1==5)//防止按鍵顫動,一種狀態只運行一次(非常巧妙)
- {
- sta=0;
- PORTBINV=(1<<13);
- }
- }
- else bit1=0;
- if(PORTAbits.RA1==0)
- {
- bit2++;
- if(bit2==5)//防止按鍵顫動,一種狀態只運行一次(非常巧妙)
- {
- if(sta==1)
- sta=2;
- else
- sta=1;
- }
- }
- else bit2=0;
- switch(sta)//不同狀態運行不同函數
- {
- case 0:
- for(i=0;i<4;i++)
- t[i]=0;
- t[1]=10;
- image[0]=Led[t[3]];
- image[1]=Led[t[2]];
- image[2]=Led[t[1]];
- image[3]=Led[t[0]];
- spiout(image);break; //初始化重置數碼管
- case 1:
- if(ledflag==1)
- {
- ledflag=0;
- led(image);
- }
- break; //秒表開始
- case 2:
- break; //暫停
- }
-
- }
-
- }
- // }
-
復制代碼
|