Jump to content
Bernard

Reading LM4F120 internal temp sensor

Recommended Posts

Hello,

 

Here is a ino file that allow to display temperature  from LM4F120 internal sensor.

 

It is based on example from TI workbook , ported a la mode Energia.

 

Nothing spectacular. I am slowly learning ARM and I wanted to share.

 

/* Read internal temperature sensor of LM4F120 
 Stellaris Launchpad
 Example from TI LM4F120 workbook
 A la mode Energia
 */

#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"
#include "Energia.h"

unsigned long ulADC0Value[4];
volatile unsigned long ulTempAvg;
volatile unsigned long ulTempValueC;
volatile unsigned long ulTempValueF;

void  setup()
{
  Serial.begin(9600);

  SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
  SysCtlADCSpeedSet(SYSCTL_ADCSPEED_125KSPS); // 250
  ADCSequenceDisable(ADC0_BASE, 1);
  ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);
  ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADC_CTL_TS);
  ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADC_CTL_TS);
  ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADC_CTL_TS);
  ADCSequenceStepConfigure(ADC0_BASE, 1, 3, ADC_CTL_TS | ADC_CTL_IE | ADC_CTL_END);
  ADCSequenceEnable(ADC0_BASE, 1);
}
void loop()
{  
  ADCIntClear(ADC0_BASE, 1);
  ADCProcessorTrigger(ADC0_BASE, 1);
  
  while(!ADCIntStatus(ADC0_BASE, 1, false))
  {    
  }

  ADCSequenceDataGet(ADC0_BASE, 1, ulADC0Value);
  ulTempAvg = (ulADC0Value[0] + ulADC0Value[1] + ulADC0Value[2] + ulADC0Value[3] + 2)/4;
  ulTempValueC = (1475 - ((2475 * ulTempAvg)) / 4096)/10;
  ulTempValueF = ((ulTempValueC * 9) + 160) / 5;
  Serial.println(ulTempValueC);
  delay(300);
}

Salutations.

 

Bernard

 

 

 

 

 

 

 

 

Share this post


Link to post
Share on other sites

it is working on the first try.. thanks a lot.. 

i have a stellaris launchpad as well now ^_^

and i want to explore its abilites (especially pros against msp430's)

..

now i will test it on raspberry pi, writing a simple python code.. yay^_^

Share this post


Link to post
Share on other sites

If you are using Energia 11 and get something like the below then move #include "Energia.h" to the top as the first include. The reason is that the new driverlib depends on stdint.h and stdbool.h. Energia.h includes these. As an alternative, you can also include stdbool.h and stdint.h before any of the drivelib includes.

In file included from sketch_feb01a.ino:10:0:
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/debug.h:49:41: error: 'uint32_t' has not been declared
In file included from sketch_feb01a.ino:11:0:
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/sysctl.h:614:8: error: 'uint32_t' does not name a type
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/sysctl.h:615:8: error: 'uint32_t' does not name a type
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/sysctl.h:616:8: error: 'uint32_t' does not name a type
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/sysctl.h:617:37: warning: 'SysCtlPeripheralPresent' initialized and declared 'extern' [enabled by default]
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/sysctl.h:617:37: error: 'uint32_t' was not declared in this scope
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/sysctl.h:618:35: warning: 'SysCtlPeripheralReady' initialized and declared 'extern' [enabled by default]
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/sysctl.h:618:35: error: 'uint32_t' was not declared in this scope
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/sysctl.h:619:37: error: variable or field 'SysCtlPeripheralPowerOn' declared void
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/sysctl.h:619:37: error: 'uint32_t' was not declared in this scope
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/sysctl.h:620:38: error: variable or field 'SysCtlPeripheralPowerOff' declared void
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/sysctl.h:620:38: error: 'uint32_t' was not declared in this scope
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/sysctl.h:621:35: error: variable or field 'SysCtlPeripheralReset' declared void
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/sysctl.h:621:35: error: 'uint32_t' was not declared in this scope
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/sysctl.h:622:36: error: variable or field 'SysCtlPeripheralEnable' declared void
/Applications/Energia.app/Contents/Resources/Java/hardware/lm4f/cores/lm4f/driverlib/sysctl.h:622:36: error: 'uint32_t' was not declared in this scope
........
sketch_feb01a.ino: In function 'void setup()':
sketch_feb01a.ino:24:44: error: 'SysCtlPeripheralEnable' was not declared in this scope
sketch_feb01a.ino:25:44: error: 'SysCtlADCSpeedSet' was not declared in this scope
sketch_feb01a.ino:26:34: error: 'ADCSequenceDisable' was not declared in this scope
sketch_feb01a.ino:27:62: error: 'ADCSequenceConfigure' was not declared in this scope
sketch_feb01a.ino:28:55: error: 'ADCSequenceStepConfigure' was not declared in this scope
sketch_feb01a.ino:32:33: error: 'ADCSequenceEnable' was not declared in this scope
sketch_feb01a.ino: In function 'void loop()':
sketch_feb01a.ino:36:27: error: 'ADCIntClear' was not declared in this scope
sketch_feb01a.ino:37:35: error: 'ADCProcessorTrigger' was not declared in this scope
sketch_feb01a.ino:39:42: error: 'ADCIntStatus' was not declared in this scope
sketch_feb01a.ino:43:47: error: 'ADCSequenceDataGet' was not declared in this scope

Share this post


Link to post
Share on other sites
The code below works in Energia 10,compiles in 11 but does nothing.Can anyone tell why?

 

 

 


/* Read internal temperature sensor of LM4F120 

 Stellaris Launchpad
 Example from TI LM4F120 workbook
 A la mode Energia
 */
#include "Energia.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"




unsigned long ulADC0Value[4];
volatile unsigned long ulTempAvg;
volatile unsigned long ulTempValueC;
volatile unsigned long ulTempValueF;
volatile unsigned long tt;
volatile unsigned long tc;
void  setup()
{
  Serial.begin(9600);
  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(BLUE_LED, OUTPUT);


  SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
  SysCtlADCSpeedSet(SYSCTL_ADCSPEED_125KSPS); // 250
  ADCSequenceDisable(ADC0_BASE, 1);
  ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);
  ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADC_CTL_TS);
  ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADC_CTL_TS);
  ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADC_CTL_TS);
  ADCSequenceStepConfigure(ADC0_BASE, 1, 3, ADC_CTL_TS | ADC_CTL_IE | ADC_CTL_END);
  ADCSequenceEnable(ADC0_BASE, 1);
}
void loop()
{  
  ADCIntClear(ADC0_BASE, 1);
  ADCProcessorTrigger(ADC0_BASE, 1);


  while(!ADCIntStatus(ADC0_BASE, 1, false))
  {    
  }


  ADCSequenceDataGet(ADC0_BASE, 1, ulADC0Value);
  ulTempAvg = (ulADC0Value[0] + ulADC0Value[1] + ulADC0Value[2] + ulADC0Value[3] + 2)/4;
  ulTempValueC = (1475 - ((2475 * ulTempAvg)) / 4096);
  ulTempValueF = ((ulTempValueC * 9) + 160) / 5;
  Serial.println(ulTempValueC);
  for (int tc =1;tc <=ulTempValueC/100;tc ++)
    dot();


  tt=ulTempValueC%100;


  for (int ttt =1;ttt <=abs(tt/10);ttt ++)
    gdot();
  for (int tc =1;tc <=ulTempValueC%10;tc ++)
    bdot();
  delay(3000);


}


void dot()
{
  digitalWrite(RED_LED, HIGH);
  delay(250);
  digitalWrite(RED_LED, LOW);
  delay(250);
}
void gdot()
{
  digitalWrite(GREEN_LED, HIGH);
  delay(250);
  digitalWrite(GREEN_LED, LOW);
  delay(250);
}
void bdot()
{
  digitalWrite(BLUE_LED, HIGH);
  delay(250);
  digitalWrite(BLUE_LED, LOW);
  delay(250);
}

Share this post


Link to post
Share on other sites

I added this:

 

    SYSCTL_RCGC0_R |= SYSCTL_RCGC0_ADC0;  

    ROM_ADCHardwareOversampleConfigure(ADC0_BASE64);

 

Before the ADCSequenceDisable line and it works ok.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...