PIC -IIC硬件驱动程序
2010-05-17 00:08
#include
#define uchar unsigned char
#define uint unsigned int
__CONFIG(0x3B31);
const uchar ee_data[]={1,2,3,4,5,6};
uchar read_data[6];
unsigned char duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char wei[]={0x3e,0x3d,0x3b,0x37,0x2f,0x1f};
const uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void FBI(uint x);
void init();
void didi(uchar num);
void disp(uchar num1,uchar num2,uchar num3,uchar num4,uchar num5,uchar num6);
void write(uchar add, uchar value);
int read(uchar r_add);
uchar a1,a2,a3,a4,a5,a6;
int vv;
void main()
{
uchar i;
init();
/* for(i=3;i<9;i++)
{
write(i,i);// 对应地址写入数据
}*/
FBI(1000);
while(1)
{
a1=read(3);// 对应地址读出
a2=read(4);// 对应地址读出
a3=read(5);// 对应地址读出
a4=read(6);// 对应地址读出
a5=read(7);// 对应地址读出
a6=read(8);
PORTA=wei[0];
PORTD=duan[a1];
FBI(1);
PORTA=wei[1];
PORTD=duan[a2];
FBI(1);
PORTA=wei[2];
PORTD=duan[a3];
FBI(1);
PORTA=wei[3];
PORTD=duan[a4];
FBI(1);
PORTA=wei[4];
PORTD=duan[a5];
FBI(1);
PORTA=wei[5];
PORTD=duan[a6];
FBI(1);
}
}
void FBI(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;
RC2=0;
PORTD=0;
PORTA=0;
TRISC=0x00;
SSPSTAT=0x80;
SSPCON=0x38;
SSPCON2=0;
SSPADD=0x09;
}
void write(uchar add ,uchar value)
{
uchar i;
SSPIF=0;
SEN=1;
while(!SSPIF);
SSPIF=0;
SSPBUF=0XA0;
while(!SSPIF);
SSPIF=0;
SSPBUF=add;
while(!SSPIF);
SSPIF=0;
SSPBUF=value;
while(!SSPIF);
SSPIF=0;
PEN=1;
while(!SSPIF);
SSPIF=0;
didi(1);
}
int read( uchar r_add)
{
uchar i;
SSPIF=0;
SEN=1;
while(!SSPIF);
SSPIF=0;
SSPBUF=0xA0;
while(!SSPIF);
SSPIF=0;
SSPBUF=r_add;
while(!SSPIF);
SSPIF=0;
SSPIF=0;
RSEN=1;
while(!SSPIF);
SSPIF=0;
SSPBUF=0xA1;
while(!SSPIF);
SSPIF=0;
RCEN=1;
while(!SSPIF);
vv=SSPBUF;
while(!SSPIF);
SSPIF=0;
ACKDT=1;
ACKEN=1;
while(!SSPIF);
SSPIF=0;
PEN=1;
while(!SSPIF);
SSPIF=0;
return vv;
}
void didi(uchar num)
{
uchar di_num;
for(di_num=num;di_num>0;di_num--)
{
RC2=1;
FBI(50);
RC2=0;
FBI(20);
}
}