PIC单片机内部SPI通信接口的使用方法
扫描二维码
随时随地手机看文章
#include #define uchar unsigned char void write_enable() void write(uchar add,uchar wdata) uchar read(uchar add) void didi(uchar num) void disp(uchar num1,uchar num2,uchar num3,uchar num4,uchar num5,uchar num6)
#define uint unsigned int
#define cs RC2
#define dout RC4
#define nop() asm("nop")
__CONFIG(0x3B31);
const uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar da
void delay(uint x);
void init();
void didi(uchar num);
void disp(uchar num1,uchar num2,uchar num3,uchar num4,uchar num5,uchar num6);
void comm(uchar da
uchar read(uchar add);
void write(uchar add,uchar wdata);
void write_enable();
void main()
{
uchar a1,a2,a3,a4,a5,a6,i;
init();
//write_enable();
//for(i=6;i>0;i--)
//{
//write_enable();
//write(i,i);
//}
a1=read(1);
a2=read(2);
a3=read(3);
a4=read(4);
a5=read(5);
a6=read(6);
while(1)
{
disp(a1,a2,a3,a4,a5,a6);
}
}
void delay(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void init()
{
TRISD=0;
TRISA=0;
TRISC=0x10;//00010000
TRISE0=0;
SSPSTAT=0x80;
SSPCON=0x32;
cs=0;
}
{
cs=1;
nop();
comm(0x02);
comm(0x60);
cs=0;nop();nop();
}
{
cs=1;
nop();
comm(0x02);
comm(add|0x80);
comm(wdata);
cs=0;
nop();nop();
cs=1;nop();nop();
while(!dout);
cs=0;nop();nop();
}
{
uchar rtemp;
cs=1;
nop();
comm(0x03);
comm(add);
comm(0);
cs=0;
rtemp=da
return (rtemp);
}
void comm(uchar da
{
SSPBUF=da
while(!SSPIF);
SSPIF=0;
da
}
{
uchar di_num;
for(di_num=num;di_num>0;di_num--)
{
RE0=1;
delay(50);
RE0=0;
delay(20);
}
}
{
PORTD=table[num1];//显示第一个数码管
PORTA=0x20;//0010 0000
delay(2);
PORTD=table[num2];//显示第二个数码管
PORTA=0x10;//0001 0000
delay(2);
PORTD=table[num3];//显示第三个数码管
PORTA=0x08;//0000 1000
delay(2);
PORTD=table[num4];//显示第四个数码管
PORTA=0x04;//0000 0100
delay(2);
PORTD=table[num5];//显示第五个数码管
PORTA=0x02;//0000 0010
delay(2);
PORTD=table[num6];//显示第六个数码管
PORTA=0x01;//0000 0001
delay(2);
}