I keep receiving 0 from one of lm35 sensors

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

I keep receiving 0 from one of lm35 sensors

Jump to solution
1,084 Views
Alex_2221
Contributor IV

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 of 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

Here is my code:

 

#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);

}

}

Labels (1)
0 Kudos
Reply
1 Solution
1,065 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Alexander,

I just reviewed your code, how about just having the ADC to sample only one channel for each uint16_t HW_ADC_Read1()/uint16_t HW_ADC_Read2(), so you can just change code like :

uint16_t HW_ADC_Read1()
{
flagOfTemp=0;

ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_1,DISABLE);
ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_0,ENABLE);
ADC_StartCmd(LPC_ADC,ADC_START_NOW);

while(flagOfTemp==0);

return realTemp1;
}

 

uint16_t HW_ADC_Read2()
{
flagOfTemp=0;

ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_1,ENABLE);
ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_0,DISABLE);
ADC_StartCmd(LPC_ADC,ADC_START_NOW);

while(flagOfTemp==0);

return realTemp1;
}

Frankly speaking, I do not know why you START the ADC conversion in ISR void ADC_IRQHandler(). can you delete the code in ISR?

else
ADC_StartCmd(LPC_ADC,ADC_START_NOW);

}

 

I suggest you use a Timer to generate an interrupt for example RIT module, in the Timer ISR, you can call the HW_ADC_Read1() and HW_ADC_Read2() so that you can control the sampling rate.

Frankly speaking, I do not understand your code structure, if I am wrong, I am sorry.

BR

XiangJun Rong

View solution in original post

0 Kudos
Reply
2 Replies
1,059 Views
Alex_2221
Contributor IV

Thank you so much for your reply. Actually, I used BRUST mode and it solved my problem. I enabled BRUST mode in void HW_Init() and disabled it in void HW_ADC_Read1 and HW_ADC_Read2. I started ADC in ISR mode because I wanted ADC to be enabled while the average temperature is being calculated. Anyways what you suggested to me seems to be very helpful and feasible. I'll give that a try and compare the results, cause I need to add more ADC channels later.

0 Kudos
Reply
1,066 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Alexander,

I just reviewed your code, how about just having the ADC to sample only one channel for each uint16_t HW_ADC_Read1()/uint16_t HW_ADC_Read2(), so you can just change code like :

uint16_t HW_ADC_Read1()
{
flagOfTemp=0;

ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_1,DISABLE);
ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_0,ENABLE);
ADC_StartCmd(LPC_ADC,ADC_START_NOW);

while(flagOfTemp==0);

return realTemp1;
}

 

uint16_t HW_ADC_Read2()
{
flagOfTemp=0;

ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_1,ENABLE);
ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_0,DISABLE);
ADC_StartCmd(LPC_ADC,ADC_START_NOW);

while(flagOfTemp==0);

return realTemp1;
}

Frankly speaking, I do not know why you START the ADC conversion in ISR void ADC_IRQHandler(). can you delete the code in ISR?

else
ADC_StartCmd(LPC_ADC,ADC_START_NOW);

}

 

I suggest you use a Timer to generate an interrupt for example RIT module, in the Timer ISR, you can call the HW_ADC_Read1() and HW_ADC_Read2() so that you can control the sampling rate.

Frankly speaking, I do not understand your code structure, if I am wrong, I am sorry.

BR

XiangJun Rong

0 Kudos
Reply