本帖最后由 jack.king 于 2009-9-10 18:08 编辑
以下程序可以直接用于51系列MCU控制IIC但是不知道为什么不能用于PIC16F77的MCU,有 那位能不指点下。若想用于51控制IIC的可以直接用
说明:PIC16F77的晶振为20MHZ
#define SCL TRISC3
#define SDA TRISC4
const uchar eeprom_data[256];//eeprom
void nop(void)
{
#asm
nop;
nop;
nop;
#endasm
}
uchar eeprom_position = 0x00;
/***************************
** function:DelayShortms
** input: uchar ms
** ouput: no
** description: delay some ms
****************************/
void DelayShortms(uchar ms)
{
uchar i,j;
for(i=0;i<ms;i++)
for(j=0;j<120;j++)
;
}
/***************************
** function:IIC_Delay
** input: no
** ouput: no
** description: delay some nop
****************************/
void IIC_Delay(void)
{
nop();
nop();
nop();
nop();
nop();
nop();
}
/**************************
** function:IIC_Init
** input: no
** output:no
** descripition: init the IIC device
***************************/
void IIC_Init(void)
{
SDA=1;
SCL=1;
}
/**************************
** function:IIC_Start
** input: no
** output:no
** descripition: Start the IIC device
***************************/
void IIC_Start(void)
{
SDA=1;
SCL=1;
IIC_Delay();
SDA=0;
IIC_Delay();
SCL=0;
}
/**************************
** function:IIC_Stop
** input: no
** output:no
** descripition:Stop the IIC device
***************************/
void IIC_Stop(void)
{
SDA=0;
IIC_Delay();
SCL=1;
IIC_Delay();
SDA=1;
}
bit ack;
/**************************
** function:IIC_Send
** input: uchar Send_Data
** output: bit ack
** descripition: MCU send a data to IIC device
***************************/
bit IIC_Send(uchar Send_Dat)
{
uchar i;
for(i = 0;i < 8; i++)
{
SDA=(bit)(Send_Dat&0x80);
Send_Dat<<=1;
IIC_Delay();
SCL=1;
IIC_Delay();
SCL=0;
IIC_Delay();
}
SDA=1;
IIC_Delay();
SCL=1;
IIC_Delay();
ack=SDA;
SCL=0;
IIC_Delay();
return ack;
}
/**************************
** function:IIC_Receive
** input: no
** output: uchar Receive_Dat
** descripition: MCU receive a data from IIC device
***************************/
uchar IIC_Receive(void)
{
uchar i,Receive_Dat=0;
for(i=0;i<8;i++)
{
SCL=1;
IIC_Delay();
Receive_Dat<<=1;
Receive_Dat|=SDA;
SCL=0;
IIC_Delay();
}
return Receive_Dat;
}
/***********************************
** function:WrAddDat
** input: uchar Add
** output: uchar Dat
** descripition:write a data to IIC device
***********************************/
void WrAddDat(uchar Add,uchar Dat)
{
IIC_Start();
IIC_Send(0xA0);
IIC_Send(Add);
IIC_Send(Dat);
IIC_Stop();
DelayShortms(10);
}
/*******************************
** function:WrAddRdDat
** input: uchar address
** output:Dat
** decripition:read data from IIC device
********************************/
uchar WrAddRdDat(uchar address)
{
uchar Dat;
IIC_Start();
IIC_Send(0xA0);
IIC_Send(address);
IIC_Start();
IIC_Send(0xA1);
Dat=IIC_Receive();
IIC_Stop();
return Dat;
}
/********************************
** name:main
** input: no
** output:no
** function:
**********************************/
void main (void)
{
// uchar i;
init_timer();
// i = WrAddRdDat(0);
ADCON1 = 0xff;
nop();
TRISE = 0;
nop();
TRISB = 0xff;//Rb3,Rb4,Rb5,Rb6,RB7,is in .the data direction registe
nop();
TRISD = 0x00;//the dircetion of outing
nop();
TRISA = 0x00;//RA0,RA1,RA2,RA3,is out .the data direction register.
nop();
PORTA = 0x08;
nop();
PORTD = 0xff;
nop();
TRISC = 0x00; //C口设为输出 RC3为SCL线,RC4为SDA线。
PORTC = 0x00;
for(;;)
{
//scan_key();
display();
WrAddDat(eeprom_position,eeprom_data[eeprom_position]);
if(eeprom_position != 0xff)
eeprom_position++;
else
break;
}
} |