打印

我写了个PIC控制EEPROM的程序但是好象错了那位能不能纠正下

[复制链接]
1695|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
jack.king|  楼主 | 2009-9-10 18:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 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;
  }
}

相关帖子

沙发
zhaoyu2005| | 2009-9-11 08:30 | 只看该作者
bit IIC_Send(uchar Send_Dat)
下面的SDA=(bit)(Send_Dat&0x80);
在PIC上不行,PIC执行这个语句,SDA永远等于0,因为PIC的编译器取得是最低位,不像51的,取对应位

使用特权

评论回复
板凳
alamo2009| | 2009-9-11 08:57 | 只看该作者
要先移位再取,pic取得是最低位
SDA=(bit)((Send_Dat>>7)&0x01);

使用特权

评论回复
地板
t86964988| | 2009-12-30 09:15 | 只看该作者
void IIC_Start(void)
{
SDA=1;
SCL=1;
IIC_Delay();
SDA=0;
IIC_Delay();
SCL=0;
在这里加个IIC_Delay();
} 还有就是ls提到的

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

488

主题

2158

帖子

1

粉丝