|
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周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|