Hello everyone, I'm trying to read the temperature of two LM35 sensors and send them to my phone using HC06 bluetooth module. I've successfully received the temperature of one the sensors on my phone, but when I added the second sensor I received only 0. I checked my circuit connections and tried different adc channels, but still I keep receiving 0 from the second sensor. Interestingly when I use only one adc channel I receive the correct temperature, but when I use 2 adc channels I receive the correct temperature from one sensor and 0 from the other one. Moreover, in debug mode I receive 0 from one of the adc channels, so clearly the problem is with adc and not uart part of the code, I think
Thanks in advance
#include "Hardware.h" #include "lpc17xx_gpio.h" #include "lpc17xx_adc.h" #include "lpc17xx_pinsel.h" #include "lpc17xx_nvic.h" #include "Delay_RIT.h" #include "lpc17xx_uart.h" uint16_t voltage1,voltage2, realTemp1=0,realTemp2=0,localTemp; uint32_t total1=0,total2=0; uint16_t countOfSum1=0,countOfSum2=0; volatile uint8_t flagOfTemp =0; float calcVolt1,calcVolt2; void ADC_IRQHandler() { if(ADC_ChannelGetStatus(LPC_ADC,ADC_CHANNEL_1,ADC_DATA_DONE)==0) { uint16_t temp1; voltage1=ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_1); calcVolt1=(voltage1*3.48)/4096; temp1=calcVolt1*100; total1+=temp1; countOfSum1++; if (countOfSum1==100) { countOfSum1=0; realTemp1=total1/100; total1=0; flagOfTemp=1; } else ADC_StartCmd(LPC_ADC,ADC_START_NOW); } if(ADC_ChannelGetStatus(LPC_ADC,ADC_CHANNEL_0,ADC_DATA_DONE)==0) { uint16_t temp2; voltage2=ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_0); calcVolt2=(voltage2*3.48)/4096; temp2=calcVolt2*100; total2+=temp2; countOfSum2++; if (countOfSum2==100) { countOfSum2=0; realTemp2=total2/100; total2=0; flagOfTemp=1; } else ADC_StartCmd(LPC_ADC,ADC_START_NOW); } } uint16_t HW_ADC_Read1() { flagOfTemp=0; ADC_StartCmd(LPC_ADC,ADC_START_NOW); while(flagOfTemp==0); return realTemp1; } uint16_t HW_ADC_Read2() { flagOfTemp=0; ADC_StartCmd(LPC_ADC,ADC_START_NOW); while(flagOfTemp==0); return realTemp2; } void HW_Init() { PINSEL_CFG_Type adcpinsel; PINSEL_CFG_Type uartpinsel; UART_CFG_Type uartConfig; NVIC_SetPriorityGrouping(0x07); adcpinsel.Funcnum=PINSEL_FUNC_1; adcpinsel.OpenDrain=PINSEL_PINMODE_NORMAL; adcpinsel.Pinmode=PINSEL_PINMODE_TRISTATE; adcpinsel.Pinnum=24; adcpinsel.Portnum=0; PINSEL_ConfigPin(&adcpinsel); adcpinsel.Pinnum=23; PINSEL_ConfigPin(&adcpinsel); ADC_Init(LPC_ADC,200000); ADC_IntConfig(LPC_ADC,ADC_ADINTEN1,ENABLE); ADC_IntConfig(LPC_ADC,ADC_ADINTEN0,ENABLE); ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_1,ENABLE); ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_0,ENABLE); NVIC_SetPriority(ADC_IRQn,NVIC_EncodePriority(0x07,0,1)); NVIC_EnableIRQ(ADC_IRQn); Delay_RIT_Init(); uartpinsel.Funcnum=PINSEL_FUNC_1; uartpinsel.OpenDrain=PINSEL_PINMODE_NORMAL; uartpinsel.Pinmode=PINSEL_PINMODE_TRISTATE; uartpinsel.Pinnum=2; uartpinsel.Portnum=0; PINSEL_ConfigPin(&uartpinsel); uartpinsel.Pinnum=3; PINSEL_ConfigPin(&uartpinsel); uartConfig.Baud_rate=38400; uartConfig.Databits=UART_DATABIT_8; uartConfig.Parity=UART_PARITY_NONE; uartConfig.Stopbits=UART_STOPBIT_1; UART_Init(LPC_UART0,&uartConfig); UART_TxCmd(LPC_UART0,ENABLE); } //main.c file #include <stdio.h> #include "lpc17xx_gpio.h" #include "lpc17xx_pinsel.h" #include "lpc17XX_uart.h" #include "lpc17XX_adc.h" #include "Hardware.h" #include "Delay_RIT.h" int main() { uint8_t buffstr [20]; HW_Init(); while(1) { uint16_t temp1 = HW_ADC_Read1(); uint16_t temp2 = HW_ADC_Read2(); uint8_t len= sprintf( (char*)buffstr,"%u\n %u\n ",temp1,temp2); UART_Send(LPC_UART0,buffstr,len,NONE_BLOCKING); //send Delay_RIT_ms(1000); } }
I'm not familiar with this MCU so I skipped reading your code, but it sounds like you haven't enabled the scanning of the ADC channels. There should be something like scanning of the ADC channels that you might have forgotten to enable it. Read the reference manual carefully. Find a register that sets the ADC to get the data for all channels.
Thank you, I'll give that a try
Ali_Kermani said: I keep receiving 0 from the second sensor
It's an analogue sensor - are you saying that it's giving a zero output?
Or, as SillFsxa suggests, do you mean that you are getting a zero reading from the ADC?
Or, if the ADC is reading OK, maybe it's getting "lost" somewhere in the Bluetooth transmission or the phone app?
It's really important to learn to think about these things, and track down what is happening, and where.
How To Debug.
Thanks for the answer. As I said I realized in the debug mode that I receive zero from one of the ADC channels when they are working together before receiving them by my phone app. So I believe that I receive zero from the ADC
The sensor by the way gives the correct voltage and I swapped those two sensors, so I believe the sensors work fine too