搜索
bottom↓
回复: 1

[求助](CVAVR)MEGA16与ADS1115通信获得数据为0

[复制链接]

出0入0汤圆

发表于 2011-8-30 18:07:21 | 显示全部楼层 |阅读模式
PB口连接LCD1602,PC0连SCL,PC1连SDA,PC2连READY,AIN0连待测电源,SDA、SCL、READY已连接2K上拉电阻,ADDR连地
以下是代码
/*****************************************************
Date    : 2011-8-30
Chip type           : ATmega16
Program type        : Application
Clock frequency     : 4.000000 MHz
Memory model        : Small
External RAM size   : 0
Data Stack size     : 256
*****************************************************/

#include <mega16.h>
#include <delay.h>
// I2C Bus functions
#asm
   .equ __i2c_port=0x15 ;PORTC
   .equ __sda_bit=1
   .equ __scl_bit=0
#endasm
//C0 -> SCL , C1 -> SDA , C2 -> RDY
#define RDY PORTC.2
#include <i2c.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x18 ;PORTB
#endasm
//B0 -> RS, B1 -> R/W, B2 -> E, B4-B7 -> D4-D7
#include <lcd.h>

// Declare your global variables here

#define u8 unsigned char
#define u16 unsigned int

#define ADS_address 0b10010000     //connect to ground
flash u8 table1[]="Num:";
u8 num[16];
u8 value1 , value2;

u8 time_10ms=0 , i , time_500ms=0 ;
bit time_10ms_ok=0;
bit time_500ms_ok=0;
//-----------------------------------------------------

// Timer 0 output compare interrupt service routine
interrupt [TIM0_COMP] void timer0_comp_isr(void)
{
     if(++time_10ms>=5)
     {
          time_10ms=0;
          time_10ms_ok=1;
          if(++time_500ms>=50)
          {
               time_500ms=0;
               time_500ms_ok=1;
          }
     }
}

// read 2 byte from the ADS1115
void ADS_read(u8 reg)   //just 2 bit
{
    u8 reg1=reg & 0x03;
    while(RDY==1);        
    if( i2c_start() )                              //start signal
    {
          i2c_write(ADS_address & 0);    //write slave address for writing register number
          i2c_write(reg1);                       //register number
          i2c_stop();                              //stop signal
    }
    while(RDY==1);
    if( i2c_start() )                              //start signal
    {
          i2c_write(ADS_address | 1);    //write slave address for reading
          value1=i2c_read(1);                 //get data and send ACK(low)
          value2=i2c_read(1);                 //get data and send ACK(low)
          i2c_stop();                              //stop signal
    }
}

// write 2 byte to the ADS1115
void ADS_write(u8 reg, u8 data_1, u8 data_2)
{
    u8 reg1;
    reg1=reg & 0x03;
    while(RDY==1);
    if( i2c_start() )                              //start signal
    {
          i2c_write(ADS_address & 0);    //write slave address for writing register number
          i2c_write(reg1);                       //register number
          //i2c_write(0b10000100);           //MSB of the Config register
          i2c_write(data_1);                    //write high 8 bit
          //i2c_write(0b10000011);           //LSB of the Config register
          i2c_write(data_2);                   //write low 8 bit
          i2c_stop();                               //stop signal
    }
}

void main(void)
{
// Declare your local variables here

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 62.500 kHz
// Mode: CTC top=OCR0
// OC0 output: Disconnected
TCCR0=0x0B;
TCNT0=0x00;
OCR0=0x7C;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x02;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x19;

// i2c Bus initialization
i2c_init();
ADS_write(0x01 , 0b01000100 , 0b10000000);       //initialize ADS1115 config register

// LCD module initialization
lcd_init(16);
lcd_clear();   //clear the LCD
lcd_putsf(table1);      //display the message

// Global enable interrupts
#asm("sei")

while (1)
      {
          if(time_10ms_ok==1)
          {
               if(time_500ms_ok==1)
               {
                    time_500ms_ok=0;
                    ADS_read(0x03);
                    lcd_gotoxy(0,1);    //go on the second LCD line
                    for(i=0;i<8;i++)
                         num=0x30+( (value1 & (0x80 >> i) )==(0x80 >> i) );
                    for(i=0;i<8;i++)
                         num[i+8]=0x30+( (value2 & (0x80 >> i) )==(0x80 >> i) );
                    lcd_puts(num);
               }
               time_10ms_ok=0;
          }
      };
}


LCD上显示的数值为0000000000000000……

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
头像被屏蔽

出0入0汤圆

发表于 2013-1-22 11:04:02 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-24 07:19

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表