ldf1987 发表于 2010-3-14 22:56:59

本人是新手,请问pic16f917与mcp3421之间的I2C通信问题

pic16f917与mcp3421之间的通信时采用I2C方式,但是两者之间的数据究竟是如何传输的,有些搞不清楚,资料也看了些。希望哪位大哥能指点一下,小弟也是刚学习pic单片机的,谢谢

zbm2007 发表于 2012-4-27 09:41:54

这位仁兄你现在会用了吗?在下正好也想用用试试,也是看不大懂

yklstudent 发表于 2012-4-30 10:58:31

用CCSC编译器,程序很容易写。。。。。

surf_131 发表于 2012-5-2 10:22:39

我贴过MCP3425型号的驱动实例.......

另外,最近发的串行口通信实例一帖里面有对LM75A通信的部分,就是完整的IIC例子

surf_131 发表于 2012-5-2 10:23:13

我贴过MCP3425型号的驱动实例.......

另外,最近发的串行口通信实例一帖里面有对LM75A通信的部分,就是完整的IIC例子

zbm2007 发表于 2012-5-2 15:34:16

yklstudent 发表于 2012-4-30 10:58 static/image/common/back.gif
用CCSC编译器,程序很容易写。。。。。

程序不会写啊大哥

yklstudent 发表于 2012-5-3 09:46:01


#include<16F917.h>
#device ADC=10
#include<math.h>

#define MCP3421_SDAPIN_C7
#define MCP3421_SCLPIN_C6
#define CS PIN_C0
#define CLK PIN_C1
#define DIN PIN_C2
#define addo (2.048/32767.0)

#fuses INTRC_IO , NOWDT , PROTECT , NOPUT
#use delay(clock=1000000,restart_wdt)
#use i2c(master , sda=MCP3421_SDA , scl=MCP3421_SCL)

int8 LedNum[] = {1,2,3,4,5,6,7,8};

int32 MCP3421_READ()
{
        int read_value_a,read_value_b,read_value_c,read_value_d,read_value_e,ERROR;
        int32value_adc=0x00;
        output_float(MCP3421_SCL);
        output_float(MCP3421_SDA);
        i2c_start();
        if (i2c_write(0B11010000)==0)   
        if (i2c_write(0B10001000)==0)
        i2c_stop();
        delay_ms(10);
        read_value_a=0;
        read_value_b=0;
        read_value_c=0;
        read_value_d=0;
        read_value_e=0;
        i2c_start();
        if (i2c_write(0B11010001)==0)   
    {
                read_value_a=i2c_read(1);
                value_adc=value_adc+read_value_a;
                value_adc=value_adc<<8;
                read_value_b=i2c_read(1);
                value_adc=value_adc+read_value_b;
                read_value_c=i2c_read(1);               
                read_value_d=i2c_read(1);
                read_value_e=i2c_read(0);
                i2c_stop();
                read_value_a=ERROR;
        }
        return(value_adc);
}

void Port_Initial(void)
{
        set_tris_c(0xF0);
}

void WriteByte(int8 dat)
{
        int8 i;
        for(i=0;i<8;i++)
        {
                if(((dat<<i)&0x80))
                {
                        output_high(DIN);
                }
                else
                {
                        output_low(DIN);
                }
                output_low(CLK);
                #asm
                nop
                #endasm
                output_high(CLK);
                #asm
                nop
                #endasm
        }
}

void MAX7221_WRITE(int8 addr,int8 dat)
{
        output_low(CS);
        WriteByte(addr);
        WriteByte(dat);
        output_high(CS);
}

void MAX7221_Initial(void)
{
        MAX7221_WRITE(0x0A,0x07);
        MAX7221_WRITE(0x0B,0x07);
        MAX7221_WRITE(0x0C,0x01);
        MAX7221_WRITE(0x0D,0x00);
        MAX7221_WRITE(0x09,0xFF);
}

void Display(int8 *str)
{
        int8 i;
        for(i=0;i<8;i++)
        {
                MAX7221_WRITE(i+1,str);
        }
}

void HEXTOBCD(void)
{
        int32 temp;
        temp = (int32)(MCP3421_READ()*addo*1000);

        if(temp<=1864)
        {
                Temp = (int32)(sqrt(((1.8639-(MCP3421_READ()*addo))/3.88)+2.1962)*1000-1481.96);
                LedNum = temp/1000;
                LedNum = temp%1000/100;
                LedNum = temp%100/10;
                LedNum = (temp%10)|0x80;
                LedNum = 0;
                LedNum = 15;
                LedNum = 15;
                LedNum = 15;
        }
        else
        {
                temp = (int32)(-sqrt(2.1962-(((MCP3421_READ()*addo)-1.8639)/3.88))*1000+1481.96);
                LedNum = 10;
                LedNum = temp/1000;
                LedNum = temp%1000/100;
                LedNum = temp%100/10;
                LedNum = (temp%10)|0x80;
                LedNum = 0;
                LedNum = 15;
                LedNum = 15;
        }

        /*
        LedNum = (temp/1000)|0x80;
        LedNum = temp%1000/100;
        LedNum = temp%100/10;
        LedNum = temp%10;
        LedNum = 15;
        LedNum = 15;
        LedNum = 15;
        LedNum = 15;
        */
}

void main()
{
        Port_Initial();
        delay_us(10);
        MAX7221_Initial();
        delay_us(10);
        while(1)
        {
                HEXTOBCD();
                delay_ms(20);
                Display(LedNum);
                delay_ms(20);
        }
}

               
               

yklstudent 发表于 2012-5-3 09:46:54

有用的就这么点而已
int32 MCP3421_READ()
{
         int read_value_a,read_value_b,read_value_c,read_value_d,read_value_e,ERROR;
         int32value_adc=0x00;
         output_float(MCP3421_SCL);
         output_float(MCP3421_SDA);
         i2c_start();
         if (i2c_write(0B11010000)==0)   
         if (i2c_write(0B10001000)==0)
         i2c_stop();
      delay_ms(10);
         read_value_a=0;
         read_value_b=0;
         read_value_c=0;
         read_value_d=0;
         read_value_e=0;
         i2c_start();
      if (i2c_write(0B11010001)==0)   
   {
               read_value_a=i2c_read(1);
               value_adc=value_adc+read_value_a;
               value_adc=value_adc<<8;
               read_value_b=i2c_read(1);
               value_adc=value_adc+read_value_b;
               read_value_c=i2c_read(1);               
                read_value_d=i2c_read(1);
               read_value_e=i2c_read(0);
               i2c_stop();
               read_value_a=ERROR;
         }
         return(value_adc);
}

zbm2007 发表于 2012-5-4 17:01:58

yklstudent 发表于 2012-5-3 09:46 static/image/common/back.gif
有用的就这么点而已
int32 MCP3421_READ()
{


用51的可以实现吗   我没用过PIC呢

zbm2007 发表于 2012-5-5 07:37:34

yklstudent 发表于 2012-5-3 09:46 static/image/common/back.gif
有用的就这么点而已
int32 MCP3421_READ()
{


我用的STC90C516RD+程序怎么改呢   谢谢大侠!!!

yklstudent 发表于 2012-5-5 10:15:31

自己先去把I2C通信学学吧

f117_2r 发表于 2013-2-25 23:17:05

yklstudent 发表于 2012-5-5 10:15 static/image/common/back.gif
自己先去把I2C通信学学吧

你的头像真的太好看了,对了MCP3421零卖一般多少一个,他的稳定性怎么样,比ADS1110如何

fengxin32 发表于 2013-2-26 17:25:54

taobao上有卖4.6的
我也对这个芯片感兴趣,拿来做多路隔离的万用表O(∩_∩)O哈哈~

sweet_136 发表于 2017-5-25 10:35:03

yklstudent 发表于 2012-5-3 09:46
有用的就这么点而已
int32 MCP3421_READ()
{


已经用到. 感谢.
页: [1]
查看完整版本: 本人是新手,请问pic16f917与mcp3421之间的I2C通信问题