登录 免费注册 首页 | 行业黑名单 | 帮助
维库电子市场网
技术交流 | 电路欣赏 | 工控天地 | 数字广电 | 通信技术 | 电源技术 | 测控之家 | EMC技术 | ARM技术 | EDA技术 | PCB技术 | 嵌入式系统
驱动编程 | 集成电路 | 器件替换 | 模拟技术 | 新手园地 | 单 片 机 | DSP技术 | MCU技术 | IC 设计 | IC 产业 | CAN-bus/DeviceNe

请帮我看看我写的dsPIC30F6014 I2C bus 读写程序

作者:wnch 栏目:单片机
请帮我看看我写的DSPIC30F6014 I2C bus 读写程序
小弟刚开始用DSPIC30F6014搞项目,负责部分程序的编写,其中一部分是关于I2C的读写的,全部硬件有我老大设计,在DSPIC30F6014的I2C bus上接了两个PCF8574,DSPIC30F6014为单主器件,两个PCF8574作为从器件,作为开关量的输入和输出,但SDA AND SCL 没有接上拉电阻,现小弟看完DSPIC30F6014中I2C接口的E文介绍后,写了一段初始化和读写程序,但提交给老大后,老大说测试到SDA AND SCL一直都是处于低电平状态,初始化后连高电平(即空闲状态)都没有出现,但老大测试过,如果把SDA AND SCL作为普通I/O输出,是可以输出高电平的,肯定我的程序有问题,我一时却找不出来,请各位帮我看看程序吧:
/* it indicate the i2c bus operation error when I2C_ERROR=0x55 *
* it means that the result is not avail                       */
extern unsigned CHAR I2C_ERROR
void initI2 ( void )
{   
     unsigned int i,j,k;
     //I2CCONbits.I2CEN=1,turn on the I2C MODULE
     //I2CCONbits.I2CSIDL=0,continute MODULE operation in  idle mode
     //I2CCONbits.SCLREL=1,release the clock
     //I2CCONbits.IPMIEN=0,disable the IPMI mode
     //I2CCONbits.A10M=0,I2CADD is a 7-bit slave address
     //I2CCONbits.DISSLW=1,disable the slew rate
     //I2CCONbits.SMEN=0,disable the SMbus
     //I2CCONbits.GCEN=0,disable the general call interrupt
     //I2CCONbits.STREN=0,disable the clock stretch
     //I2CCONbits.ACKDT=0,sending the ACK during acknowledge
     //I2CCONbits.ACKEN=0,disable the ACK
     //I2CCONbits.RCEN=0,disable the receiven the data for I2C bus
     //I2CCONbits.PEN=0,disable initiate the stop condition on the   SDA and SCL
     //I2CCONbits.RSEN=0,disable repeat the start conditon
     //I2CCONbits.SEN=0,disable start condition on the SDA and SCL bus

     I2CCON=0x9200;

     //transmission rate is 100kbps(Fscl),I2CBRG=INT(Fcy/Fscl)-1==159

    I2CBRG=0x9f;

     //I2CSTAT
     //I2CSTAT=0X00C0;
     //turn on the I2C MODULE
     //I2CCONbits.I2CEN=1;

     I2C_ERROR=0xaa;

}
/* generate the start condition            *
*  it is called by I2C_read and I2C_write */
void I2C_start(void)
{
     //clear the collision flag
     I2CSTATbits.IWCOL=0;
     //ensure the i2c bus is on the idle mode
     if (I2CSTATbits.s==1&&I2CSTATbits.P==0)
     {
        //clear the I2CCON<4:0>
        I2CCON&=0xffe0
        //initiate the stop condition
        I2CCONbits.PEN=1;
        //ensure the stop condition is complete
        while (I2CCONbits.PEN==1);
     }
     //clear the I2CCON<4:0>
     I2CCON&=0xffe0
     //initiate the START condition
     I2CCONbits.SEN=1;
     //ensure the START condition is completion
     while (I2CCONbits.SEN==1);          
}

/* use polling method to get a byte data from the slave *
* slave_address_read=0x43 ,yx or                       *
* slave_address_read=0x41 ,yk and led                  */
unsigned CHAR I2C_read(unsigned CHAR slave_address_read)
{
     unsigned CHAR i;
     unsigned int I2C_COUNTER;
     //clear the I2C_COUNTER
     I2C_COUNTER=0;   
     //initiate the start condition
     I2C_start();     
     //transmission the slave address
     I2CTRN=slave_address_read;
     //wait for the slave address's transmission was compled
     while (I2CSTATbits.TBF==1);
     //wait for the acknowledge
     while (I2CSTATbits.ACKSTAT==1)
     {
            I2C_COUNTER++;
            if(I2C_COUNTER>0x2c)
            {
                I2C_ERROR=0x55;
                break;
            }
     }
     if (I2C_ERROR==0xaa)
     {
        //ensure the acknowledge is complete
        while (I2CSTATbits.TRSTAT==1);
        //clear the I2CCON<4:0>
        I2CCON&=0xffe0
        //enable the receive
        I2CCONbits.RCEN=1;
        //clear the I2C_COUNTER
        I2C_COUNTER=0;
        //wait for data
        while (I2CSTATbits.RBF==0)
        {
              I2C_COUNTER++;
              if (I2C_COUNTER>0x0160)
              {
                    I2C_ERROR=0x55;
                  break;
              }    
        }
        if (I2C_ERROR==0xaa)
        {
           //get the data from slave
           i=I2CRCV;
    &
2楼: >>参与讨论
wnch
自己顶,希望各位能帮助下第一次用MICROCHIP的人,期待中!
 
3楼: >>参与讨论
xieyuanbin
有没有搞错,仿真器也一人独占?
这叫什么部门?怎么合作?
程序太长,没法看,自己用软仿真做一下?

4楼: >>参与讨论
兰天白云
请老大吃一次饭,顺便讨教
 
5楼: >>参与讨论
rad.zhu
用软件仿真测试吧
 
6楼: >>参与讨论
wnch
先多谢各位的关注
DSPIC30F6014是我们公司第一次使用MICROCHIP的单片机,对PIC的单片机,我们全部是新手,我老大也不懂,但他认为,肯定是我程序的问题。其实程序不长,因为里面很多是注悉,但是老大要我这样放置注悉,我也没有办法。在初始化I2C bus 时,只需要设置它的控制寄存器和波特率触发器寄存器就可以了吧?

7楼: >>参与讨论
jason

不一定是程序问题,sda和scl也有可能是被PCF8574拉底的。最好加个上拉看看吧

 

参与讨论
昵称:
讨论内容:
 
 
相关帖子
串口送数据
SOS    PIC10F200最低功耗
请教各位:初学PIC,哪一款比较合适?
仿真器的作用
怎么检测步进电机精确位置?
免费注册为维库电子开发网会员,参与电子工程师社区讨论,点此进入


Copyright © 1998-2006 www.dzsc.com 浙ICP证030469号