例1
点亮数码管
#include
#pragma config FPLLIDIV = DIV_2 // PLL Input Divider (2x Divider)
#pragma config FPLLMUL = MUL_24 // PLL Multiplier (24x Multiplier)
#pragma config FPLLODIV = DIV_2 // System PLL Output Clock Divider (PLL Divide by 2)
#pragma config FNOSC = FRCPLL // Oscillator Selection Bits (Fast RC Osc with PLL)
#pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
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<60;i++); //等待数据存储成功
PORTSetBits(IOPORT_B, BIT_9); //锁存数据显示
}
int main()
{
PPSOutput(2, RPB8, SDO2);
//输出针脚组2中,查表将针脚RPB8,作为数据输出2口SDO2.实际连线也是如此
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
PORTSetPinsDigitalOut(IOPORT_B, BIT_9);//外部移位寄存器数据锁存,1锁存,0开放
// SPI2CONCLR=(1<<15); SPI2CONCLR=0X8000; 多种操作
spiout(Led,4);
}
|