Guest User

Untitled

a guest
Apr 30th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.63 KB | None | 0 0
  1. //*****************************************************************************
  2. //
  3. // temperature_sensor.c - Example demonstrating the internal ADC temperature
  4. // sensor.
  5. //
  6. // Copyright (c) 2010-2013 Texas Instruments Incorporated.  All rights reserved.
  7. // Software License Agreement
  8. //
  9. // Texas Instruments (TI) is supplying this software for use solely and
  10. // exclusively on TI's microcontroller products. The software is owned by
  11. // TI and/or its suppliers, and is protected under applicable copyright
  12. // laws. You may not combine this software with "viral" open-source
  13. // software in order to form a larger program.
  14. //
  15. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  16. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  17. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  19. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  20. // DAMAGES, FOR ANY REASON WHATSOEVER.
  21. //
  22. // This is part of revision 10094 of the Stellaris Firmware Development Package.
  23. //
  24. //*****************************************************************************
  25.  
  26. #include "inc/hw_memmap.h"
  27. #include "inc/hw_types.h"
  28. #include "driverlib/adc.h"
  29. #include "driverlib/gpio.h"
  30. #include "driverlib/sysctl.h"
  31. #include "utils/uartstdio.h"
  32.  
  33. //*****************************************************************************
  34. //
  35. //! \addtogroup adc_examples_list
  36. //! <h1>ADC Temperature Sensor (temperature_sensor)</h1>
  37. //!
  38. //! This example shows how to setup ADC0 to read the internal temperature
  39. //! sensor.
  40. //!
  41. //! NOTE: The internal temperature sensor is not calibrated.  This example
  42. //! just takes the raw temperature sensor sample and converts it using the
  43. //! equation found in the LM3S9B96 datasheet.
  44. //!
  45. //! This example uses the following peripherals and I/O signals.  You must
  46. //! review these and change as needed for your own board:
  47. //! - ADC0 peripheral
  48. //!
  49. //! The following UART signals are configured only for displaying console
  50. //! messages for this example.  These are not required for operation of the
  51. //! ADC.
  52. //! - UART0 peripheral
  53. //! - GPIO Port A peripheral (for UART0 pins)
  54. //! - UART0RX - PA0
  55. //! - UART0TX - PA1
  56. //!
  57. //! This example uses the following interrupt handlers.  To use this example
  58. //! in your own application you must add these interrupt handlers to your
  59. //! vector table.
  60. //! - None.
  61. //
  62. //*****************************************************************************
  63.  
  64. //*****************************************************************************
  65. //
  66. // This function sets up UART0 to be used for a console to display information
  67. // as the example is running.
  68. //
  69. //*****************************************************************************
  70. void
  71. InitConsole(void)
  72. {
  73.     //
  74.     // Enable GPIO port A which is used for UART0 pins.
  75.     // TODO: change this to whichever GPIO port you are using.
  76.     //
  77.     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
  78.  
  79.     //
  80.     // Configure the pin muxing for UART0 functions on port A0 and A1.
  81.     // This step is not necessary if your part does not support pin muxing.
  82.     // TODO: change this to select the port/pin you are using.
  83.     //
  84.     GPIOPinConfigure(GPIO_PA0_U0RX);
  85.     GPIOPinConfigure(GPIO_PA1_U0TX);
  86.  
  87.     //
  88.     // Select the alternate (UART) function for these pins.
  89.     // TODO: change this to select the port/pin you are using.
  90.     //
  91.     GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
  92.  
  93.     //
  94.     // Initialize the UART for console I/O.
  95.     //
  96.     UARTStdioInit(0);
  97. }
  98.  
  99. //*****************************************************************************
  100. //
  101. // Configure ADC0 for the temperature sensor input with a single sample.  Once
  102. // the sample is done, an interrupt flag will be set, and the data will be
  103. // read then displayed on the console via UART0.
  104. //
  105. //*****************************************************************************
  106.  
  107. #define ADC_SEQUENCE_NUM 0
  108. #define VREFP 3000.0
  109. #define VREFN 0.0
  110. #define MVDIV ((VREFP-VREFN)/4096.0)
  111.  
  112. int
  113. main(void)
  114. {
  115.     //
  116.     // This array is used for storing the data read from the ADC FIFO. It
  117.     // must be as large as the FIFO for the sequencer in use.  This example
  118.     // uses sequence 3 which has a FIFO depth of 1.  If another sequence
  119.     // was used with a deeper FIFO, then the array size must be changed.
  120.     //
  121.     unsigned long ulADC0_Value[8];
  122.  
  123.     //
  124.     // These variables are used to store the temperature conversions for
  125.     // Celsius and Fahrenheit.
  126.     //
  127.     double ulTemp_ValueC;
  128.     double ulTemp_ValueF;
  129.     double externalTempC;
  130.     double externalTempF;
  131.  
  132.     //
  133.     // Set the clocking to run at 20 MHz (200 MHz / 10) using the PLL.  When
  134.     // using the ADC, you must either use the PLL or supply a 16 MHz clock
  135.     // source.
  136.     // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
  137.     // crystal on your board.
  138.     //
  139.     SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
  140.                    SYSCTL_XTAL_16MHZ);
  141.  
  142.     //
  143.     // Set up the serial console to use for displaying messages.  This is just
  144.     // for this example program and is not needed for ADC operation.
  145.     //
  146.     InitConsole();
  147.  
  148.     //
  149.     // Display the setup on the console.
  150.     //
  151.     UARTprintf("ADC ->\n");
  152.     UARTprintf("  Type: Internal Temperature Sensor\n");
  153.     UARTprintf("  Samples: One\n");
  154.     UARTprintf("  Update Rate: 250ms\n");
  155.     UARTprintf("  Input Pin: Internal temperature sensor\n\n");
  156.  
  157.     //Always enable both ADCs
  158.     SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
  159.     SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC1);
  160.  
  161.     // Select the external reference for greatest accuracy.
  162.     // TODO FIXME: Figure out if we want to use the external reference on real SEC
  163.     ADCReferenceSet(ADC0_BASE, ADC_REF_EXT_3V);
  164.     ADCReferenceSet(ADC1_BASE, ADC_REF_EXT_3V);
  165.  
  166. #if 0
  167.     // SEC board
  168. #define CHAN_EXTTEMP    ADC_CTL_CH2
  169.     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
  170.     GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_1); // External Temp
  171. #else
  172.     // EVAL board
  173. #define CHAN_EXTTEMP    ADC_CTL_CH20
  174.     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
  175.     GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_7); // External Temp
  176. #endif
  177.  
  178.     //
  179.     // Enable sample sequence 3 with a processor signal trigger.  Sequence 3
  180.     // will do a single sample when the processor sends a singal to start the
  181.     // conversion.  Each ADC module has 4 programmable sequences, sequence 0
  182.     // to sequence 3.  This example is arbitrarily using sequence 3.
  183.     //
  184.     ADCSequenceConfigure(ADC0_BASE, ADC_SEQUENCE_NUM, ADC_TRIGGER_PROCESSOR, 0);
  185.  
  186.     //
  187.     // Configure step 0 on sequence 3.  Sample the temperature sensor
  188.     // (ADC_CTL_TS) and configure the interrupt flag (ADC_CTL_IE) to be set
  189.     // when the sample is done.  Tell the ADC logic that this is the last
  190.     // conversion on sequence 3 (ADC_CTL_END).  Sequence 3 has only one
  191.     // programmable step.  Sequence 1 and 2 have 4 steps, and sequence 0 has
  192.     // 8 programmable steps.  Since we are only doing a single conversion using
  193.     // sequence 3 we will only configure step 0.  For more information on the
  194.     // ADC sequences and steps, reference the datasheet.
  195.     //
  196.     ADCSequenceStepConfigure(ADC0_BASE, ADC_SEQUENCE_NUM, 0, CHAN_EXTTEMP);
  197.     ADCSequenceStepConfigure(ADC0_BASE, ADC_SEQUENCE_NUM, 1, ADC_CTL_TS);
  198.     ADCSequenceStepConfigure(ADC0_BASE, ADC_SEQUENCE_NUM, 2, ADC_CTL_TS);
  199.     ADCSequenceStepConfigure(ADC0_BASE, ADC_SEQUENCE_NUM, 3, ADC_CTL_TS | ADC_CTL_IE | ADC_CTL_END);
  200.  
  201.     //
  202.     // Since sample sequence 3 is now configured, it must be enabled.
  203.     //
  204.     ADCSequenceEnable(ADC0_BASE, ADC_SEQUENCE_NUM);
  205.  
  206.     //
  207.     // Clear the interrupt status flag.  This is done to make sure the
  208.     // interrupt flag is cleared before we sample.
  209.     //
  210.     ADCIntClear(ADC0_BASE, ADC_SEQUENCE_NUM);
  211.  
  212.     //
  213.     // Sample the temperature sensor forever.  Display the value on the
  214.     // console.
  215.     //
  216.     while(1)
  217.     {
  218.         //
  219.         // Trigger the ADC conversion.
  220.         //
  221.         ADCProcessorTrigger(ADC0_BASE, ADC_SEQUENCE_NUM);
  222.  
  223.         //
  224.         // Wait for conversion to be completed.
  225.         //
  226.         while(!ADCIntStatus(ADC0_BASE, ADC_SEQUENCE_NUM, false))
  227.         {
  228.         }
  229.  
  230.         //
  231.         // Clear the ADC interrupt flag.
  232.         //
  233.         ADCIntClear(ADC0_BASE, ADC_SEQUENCE_NUM);
  234.  
  235.         //
  236.         // Read ADC Value.
  237.         //
  238.         ADCSequenceDataGet(ADC0_BASE, ADC_SEQUENCE_NUM, ulADC0_Value);
  239.  
  240.         //
  241.         // Use non-calibrated conversion provided in the data sheet.  Make
  242.         // sure you divide last to avoid dropout.
  243.         //
  244.         //ulTemp_ValueC = ((1475 * 1023) - (2250 * ulADC0_Value[3])) / 10230;
  245.         ulTemp_ValueC = 147.5 - (75 * (ulADC0_Value[3] * MVDIV) / 1000);
  246.  
  247.         //
  248.         // Get fahrenheit value.  Make sure you divide last to avoid dropout.
  249.         //
  250.         ulTemp_ValueF = ((ulTemp_ValueC * 9.0) + 160) / 5;
  251.  
  252.         externalTempC = (1857.6 - (ulADC0_Value[0] * MVDIV)) / 11.77;
  253.         externalTempF = ((externalTempC * 9.0) + 160) / 5;
  254.         //
  255.         // Display the temperature value on the console.
  256.         //
  257.         UARTprintf("Temperature = %d*C or %d*F ... %d*C or %d*F\r", (unsigned int)(ulTemp_ValueC * 10), (unsigned int)(ulTemp_ValueF * 10), (unsigned int)(externalTempC * 10), (unsigned int)(externalTempF * 10));
  258.  
  259.         //
  260.         // This function provides a means of generating a constant length
  261.         // delay.  The function delay (in cycles) = 3 * parameter.  Delay
  262.         // 250ms arbitrarily.
  263.         //
  264.         SysCtlDelay(SysCtlClockGet() / 12);
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment