Read-Sensor-Resistances/ADS1115_driver/main.c

111 lines
3.5 KiB
C
Raw Normal View History

2023-01-08 21:50:38 +00:00
[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "lcd.h"
#include "key.h"
#include "ads1115.h"
float aa;
int main(void)
{
float t1;
u16 t,result;
u8 key;
u16 i=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// Set interrupt priority group 2
delay_init();
uart_init(9600); // The serial port is initialized to 9600
LED_Init();
//LCD_Init();
KEY_Init();
//IIC初始化
ADS1115_Init();
while(1)
{
key=KEY_Scan(0);
if(key==WKUP_PRES)
{
result=lvbo(0xeb,0x82); // A0 A1 is the differential input test terminal, low eight bits + high eight bits 1111 1011, 1000 0010
if(result >= 0x8000 && result <= 0xffff)
result = 0xffff - result; //If the difference is negative, take the absolute value, so that A0 and A1 can be connected positively and negatively.
else if(result >= 0xffff)
result = 0;
t1=4.096*2*result/65535; //converted to voltage
printf("The range is 4.096V, the voltage between A0-A1 = %f V\r\n",t1);//print
if(result == 0x7fff || result == 0x8000)
{
printf("over range \r\n\r\n");
}
else
{
printf("Read OK \r\n\r\n");
}
}
if(key==KEY0_PRES)
{
result=lvbo(0xe3,0xb2); //A2 A3 is the differential input test terminal, low eight bits + high eight bits 1111 0011,1011 0010
if(result >= 0x8000)
result = 0xffff - result; //If the difference is negative, take the absolute value, so that A2 and A3 can be connected positively and negatively.
t1=4.096*2*result/65535; //converted to voltage
printf("The range is 4.096V, the voltage between A2-A3 = %f V\r\n",t1); //print
if(result == 0x7fff || result == 0x8000) // exceeds the maximum value or falls below the minimum value
{
printf("over range \r\n\r\n");
}
else
{
printf("over range\r\n\r\n");
}
}
if(key == KEY1_PRES)
{
result=lvbo(0xe3,0xb4); //A2 A3 is the differential input test terminal, low eight bits + high eight bits 1111 0011,1011 0100
if((result >= 0x8000) && (result <= 0xffff))
result = 0xffff - result; //If the difference is negative, take the absolute value, so that A0 and A1 can be connected positively and negatively.
else if(result >= 0xffff)
result = 0;
t1=2.048*2*result/65535; //converted to voltage
printf("The range is 2.048V, the voltage between A0-A1 = %f V\r\n",t1);//打印
if(result == 0x7fff || result == 0x8000)
{
printf("over range \r\n\r\n");
}
else
{
printf("over range \r\n\r\n");
}
}
i++;
delay_ms(10);
if(i==20)
{
LED0=!LED0;//Prompt system is running
i=0;
}
}
}