PIC32单片机的I2C编程 [英] I2C programming for PIC32 microcontroller

查看:155
本文介绍了PIC32单片机的I2C编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...我对pic32微控制器的C编程有问题.我在使用PIC32进行I2C编程时遇到问题.我需要对I2C进行编程以与ADC芯片AD9883A进行通信.我可以在发送字节时写入芯片.但是我在发送多个字节时遇到问题.这是我的代码..我希望有人可以帮助我更正或有更好的代码来帮助我.谢谢...

Hi...I have a problem with c programming for pic32 microcontroller. i have problem programming for I2C using PIC32. i need to program I2C to communicate with ADC chip AD9883A. I can write to the chip in sending a byte. But i have problem sending in multiple byte.This is my codes.. i hope someone can help me correct or have better codes to help me. Thanks...

#include <plib.h>

#define SYS_FREQ    (80000000)
#define PBCLK       (SYS)_FREQ/2)
#define Fsck        375000
#define BRG_VAL     (PBCLK/2/Fsck)
#define DELAY       1024*1024

unsigned char SlaveAddress;
int DataSz;
unsigned char i2cData[4] = {0x98,0x01,0xAA};
unsigned char i2cData2[4] = {0xAA,0xAB,0xAC,0xAD};


void i2c_writemany ( unsigned char i, unsigned char k )
{
    while(1)
    {
        unsigned char a, b, c;
        a = 0x01;
        b = *i2cData2;
        for ( ; a<i; a++ )
        {
            i2cData[0] = 0x98;
            i2cData[1] = a;
            i2cData[2] = b;
            int Index = 0;
            DataSz = 3;
            StartI2C1();//Send the Start Bit
            IdleI2C1();//Wait to complete
            while ( DataSz )
            {
                MasterWriteI2C1( i2cData[Index++] );
                IdleI2C1();//Wait to complete

                DataSz--;

                if( I2C1STATbits.ACKSTAT )
                break;
            }
            StopI2C1();//Send the Stop condition
            IdleI2C1();//Wait to complete
            b++;
        }
    }
}

int main (void)
{
    SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
    mOSCSetPBDIV ( OSC_PB_DIV_8 );  //Set Periph
    OpenI2C1( I2C_EN, BRG_VAL );    //Enable I2C channel
    i2c_write(i2cData);
    /*while(1)
    {
        i2c_writemany(6,i2cData);
        DELAY;
    }
}



[edit]改进的标题,使用代码块保留格式OriginalGriff [/edit]



[edit]Improved title, used code block to preserve formatting OriginalGriff[/edit]

推荐答案

本人未尝试I2C,但Microchip拥有一个很好的代码示例软件库: 此处的I2C示例 [ ^ ]
始终值得首先检查Microchip网站,因为这个问题对于一般的编程网站来说有点专家.
Not tried I2C myself, but Microchip have a good software library of code examples: I2C examples here[^]
It is always worth checking the Microchip site first, as this question is a bit specialist for a general programming site.


在测试
之后是否休息一下?
Does the break after your test of
limwy写道:

if(I2C1STATbits.ACKSTAT)

if( I2C1STATbits.ACKSTAT )

使您在收到第一个Ack之后退出while循环吗? > 发送地址并接收到Ack后的多字节传输应该是发送8位并在第9个时钟脉冲上等待一个Ack,然后发送另外8位并在第9个时钟脉冲上再次等待一个Ack,依此类推,直到Ack之后您发送一个停止位.
我还看到它假定"I2C"只是在一切顺利时发送.但这还包括处理错误和问题.例如,发送方可以取消部分传输并告知
从机返回到再次等待启动.
我看到的另一个问题是在输出缓冲区中放置了一个字节以进行传输,但没有移出.

尝试在时钟和数据线上放置sccope,它将显示所有内容.

take you out of the while loop after the first Ack is recieved?
Multibyte transmission after sending the adress and receiving an Ack should be to send 8 bits and wait for an Ack on the 9th clock pulse then either send another 8 bits and wait for an Ack again on the 9th clock pulse and so on until after an Ack you send a Stop bit.
I''ve also seen it assumed that ''I2C'' is just involves sending when all goes well. But it also includes, dealing with errors and problems. For example, the sender being able to cancel a part transmission and tell
the slave to return to waiting for a START again.
Another problem I''ve seen is a byte being places in the output buffer for transmission but not shifted out.

Try putting a sccope on the clock and data lines it will reveal everything.




我快速浏览了一下您的代码,我认为您需要整理一下-那里有一些不一致之处.如果这样做,将会很容易理解.

例如

Hi,

I have had a quick look at your code and I think you need to tidy it up a bit - there a few inconsitencies. If you do that it will be alot easier to follow.

For example,

unsigned char i2cData[4] = {0x98,0x01,0xAA};



此数组有4个元素,但仅加载了3个元素,然后在代码中将其覆盖,然后再也不会更改.



This array has 4 elements, but only 3 are loaded, then it is overwritten in the code and then never changed again.

b = *i2cData2;



这似乎是 i2cData2 的唯一用途,那么它的作用是什么?您是否要获取它的地址然后通过它递增,我不确定?

DataSz是全局声明的,但仅在"writemany"函数中使用,它是否在其他任何地方使用(或损坏)?可以是本地的(会更好)吗?

"a"已初始化但是否曾经使用过?

另外,如先前所建议的,Microchip有很棒的论坛,请看一看.

http://www.microchip.com/forums/Default.aspx? [ ^ ]

第一次使这些东西正常工作始终是最困难的事情:)

祝你好运:thumbsup:



This seems to be the only use of i2cData2, so what is it for? Were you trying to get the address of it then increment through it, I''m not sure?

DataSz is declared globally, but is only used in the ''writemany'' function, is it used (or corrupted) anywhere else? Could it be local (that would be better)?

''a'' is initialised but is it ever used?

Also, as previously suggested Microchip have a great forums, give it a look.

http://www.microchip.com/forums/Default.aspx?[^]

Getting these things working the first time is always the hardest bit :)

Good Luck :thumbsup:


这篇关于PIC32单片机的I2C编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆