一个pic单片机iic例子(未检验正确性)
2019-04-15 13:00发布
生成海报
#include
#define uchar unsigned char
#define uint unsigned int
#define add 0xaa
__CONFIG(0x3B31);
const uchar ee_data[]={1,2,3,4,5,6};
uchar read_data[6];
const uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
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 write();
void read();
void main()
{
init();
write();
delay(100);
read();
while(1)
{
disp(read_data[0],read_data[1],read_data[2],read_data[3],read_data[4],read_data[5]);
}
}
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;
TRISE0=0;
// ADCON1=0x07;
RE0=0;
PORTD=0;
PORTA=0;
TRISC=0xff;
SSPSTAT=0x80;
SSPCON=0x38;
SSPCON2=0;
SSPADD=0x09;
}
void write()
{
uchar i;
SSPIF=0;
SEN=1;
while(!SSPIF);
SSPIF=0;
SSPBUF=0xA0;
while(!SSPIF);
SSPIF=0;
SSPBUF=add;
while(!SSPIF);
SSPIF=0;
for(i=0;i<6;i++)
{
SSPBUF=ee_data[i];
while(!SSPIF);
SSPIF=0;
}
PEN=1;
while(!SSPIF);
SSPIF=0;
}
void read()
{
uchar i;
SSPIF=0;
SEN=1;
while(!SSPIF);
SSPIF=0;
SSPBUF=0xA0;
while(!SSPIF);
SSPIF=0;
SSPBUF=add;
while(!SSPIF);
SSPIF=0;
SSPIF=0;
RSEN=1;
while(!SSPIF);
SSPIF=0;
SSPBUF=0xA1;
while(!SSPIF);
SSPIF=0;
for(i=0;i<6;i++)
{
RCEN=1;
while(!SSPIF);
read_data[i]=SSPBUF;
while(!SSPIF);
SSPIF=0;
if(i>=5)
{
ACKDT=1;
}
else
{
ACKDT=0;
}
ACKEN=1;
while(!SSPIF);
SSPIF=0;
}
PEN=1;
while(!SSPIF);
SSPIF=0;
}
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);
}
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮