- #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; //暂停
- }
-
- }
-
- }
- // }
-
复制代码
|