您的位置 首页 观点

PIC单片机SPI通讯的规划

PIC单片机SPI通信的设计-const uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,

0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

uchar data_temp;

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 data);

uchar read(uchar add);

void write(uchar add,uchar wdata);

void write_enable();

void main()

#include

#define uchar unsigned char

#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 data_temp;

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 data);

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;

}


P%&&&&&%单片机SPI通讯的规划

void write_enable()

{

cs=1;

nop();

comm(0x02);

comm(0x60);

cs=0;nop();nop();

}

void write(uchar add,uchar wdata)

{

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 read(uchar add)

{

uchar rtemp;

cs=1;

nop();

comm(0x03);

comm(add);

comm(0);

cs=0;

rtemp=data_temp;

return (rtemp);

}

void comm(uchar data)

{

SSPBUF=data;

while(!SSPIF);

SSPIF=0;

data_temp=SSPBUF;

}

void didi(uchar num)

{

uchar di_num;

for(di_num=num;di_num》0;di_num–)

{

RE0=1;

delay(50);

RE0=0;

delay(20);

}

}

void disp(uchar num1,uchar num2,uchar num3,uchar num4,uchar num5,uchar num6)

{

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);

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/guandian/345758.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部