Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. unsigned long loopInterval = 1500;
  2. unsigned long loopTimer = 0;
  3. int temp = 0;
  4. int tempArray[5];
  5.  
  6. const int numReadings = 5;
  7.  
  8. int temp = 0; // temporary temp reading
  9. int readings[numReadings]; // the temp readings
  10. int readIndex = 0; // the index of the current reading
  11. int total = 0; // the running total
  12. int average = 0; // the average
  13.  
  14. void setup()
  15. {
  16. P1DIR |= BIT6 | BIT0; // set GREEN led and RED led as output
  17. P1OUT &= ~(BIT6 | BIT0); // turn off led if its on
  18. // Temp sensor formula Vtemp = 0.00355*(TEMPc)+0.986
  19. // ADC value for voltage Nadc = 1023*((Vin-(Vr-))/((Vr+)-(Vr-)))
  20. // Isvesta formule: TEMPc = ((27069L * Nadc) - 18169625L) >> 16;
  21. //--- START OF ADC10 SETUP -------------------------------------//
  22. // ENC must be 0 to be able to set parameters, 1 to be able to start conversion
  23. ADC10CTL0 = 0; // clear all configurations
  24. ADC10CTL0 |= ADC10IE; // Enable temp conversion done interrupt
  25. ADC10CTL0 |= SREF_1; // Select reference
  26. ADC10CTL0 |= ADC10SHT_3; // sample 64 values before conversion
  27. ADC10CTL0 |= MSC; // multiple sample and conversion support
  28. ADC10CTL0 |= ADC10ON; // turn of converter
  29. ADC10CTL0 |= REFON; // Enable internal reference
  30.  
  31. ADC10CTL1 = INCH_10; // select 10th chanel for temp sensor
  32. ADC10CTL1 |= ADC10DIV_3; // Set clock divider
  33. ADC10CTL1 |= CONSEQ_2; // set ADC to repeated temp sensor sampling and conversion
  34. //--- END OF ADC10 SETUP ---------------------------------------//
  35. //--- START OF Timer_A SETUP -----------------------------------//
  36. TACCR0 = 0x6D60; // 4 000 000 * 0.007
  37. TACCTL0 = CCIE;
  38. TACTL |= TACLR | TASSEL_2 | ID_3 | MC_1;
  39. // TACLR resets CTAR, selects THE SMCLK (runs 1MHz), ID_3 selects 8x divider MC_1 selects UPMODE
  40. // 7 ms = 142 hz 125khz / 142hz
  41. //--- END OF Timer_A SETUP -------------------------------------//
  42.  
  43. Serial.begin(9600);
  44. }
  45. void loop()
  46. {
  47. if( millis() - loopTimer > loopInterval )
  48. {
  49. ADC10CTL0 |= ~ENC;
  50. LPM0; // turn off cpu and wait then it will be on
  51.  
  52. // subtract the last reading:
  53. total = total - readings[readIndex];
  54. // set temperature
  55. readings[readIndex] = temp;
  56. // add the reading to the total:
  57. total = total + readings[readIndex];
  58. // advance to the next position in the array:
  59. readIndex = readIndex + 1;
  60.  
  61. // if we're at the end of the array...
  62. if (readIndex >= numReadings) {
  63. // ...wrap around to the beginning:
  64. readIndex = 0;
  65. }
  66. // calculate the average:
  67. average = total / numReadings;
  68.  
  69.  
  70. Serial.println(average);
  71. loopTimer = millis();
  72. }
  73. }
  74.  
  75. __attribute__((interrupt(TIMER0_A0_VECTOR)))
  76. void Timer_A (void)
  77. {
  78. TACCTL0 &= ~CCIE; // Turn off timer events
  79. P1OUT |= BIT6; // put green led on then timer starts ADC
  80. ADC10CTL0 |= ENC + ADC10SC; // start the conversion
  81. }
  82. __attribute__((interrupt(ADC10_VECTOR)))
  83. void ADC_interrupt (void)
  84. {
  85. temp = ((27069L * ADC10MEM) - 18169625L) >> 16;
  86. LPM0_EXIT; // turn on CPU
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement