SHARE
TWEET

Untitled

a guest Oct 14th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //#include <studio.h>
  2. #include <msp430.h>
  3. #include "project.h"
  4. #include "ports.h"
  5. #include "timer.h"
  6. #include "adc.h"
  7. #include "uart.h"
  8.  
  9.  
  10. int gADCLastVal = 0;
  11. char buf[25];
  12.  
  13. void ADC_Init() {
  14.  
  15.     ADC10CTL0 &= ~ENC;
  16.    
  17.     ADC10CTL0 = ADC10ON /* + MSC */ + ADC10SHT_3;
  18.  
  19.     ADC10CTL1 = INCH_10 + ADC10SSEL_3 + ADC10DIV_4; //Channel 10, ADC10CLK/3
  20.     //ADC10CTL0 = SREF_0 + REFON + ADC10SHT_3 + ADC10ON ;
  21.     // Vcc & Vss as reference, Sample and hold for 64 Clock Cyccles, ADC on
  22.     //ADC10CTL1 = INCH_10 + ADC10DIV_3;
  23.  
  24.     //ADC10AE0      //ADC input enable P1.3
  25.  
  26. }
  27.  
  28. void ADC_Read() {
  29.     int count = 32000;
  30.     ADC10CTL0 |= ENC + ADC10SC; //Start new conversion
  31.  
  32.     while ((ADC10CTL1 & ADC10BUSY) && --count);
  33.     if (!count) UART_Write("ADC Timeout error");
  34.  
  35.     gADCLastVal = ADC10MEM; // Left shift
  36.     ADC10CTL0 &= ~ENC;
  37.  
  38. }
  39.  
  40. inline int ADC_GetValue() {
  41.     return gADCLastVal;
  42. }
  43.  
  44. void ADC_Write() {
  45.  
  46.     int i;
  47.     int val = ADC_GetValue();
  48.     int q, c = 0;
  49.  
  50.     //snprintf(buf, 25, "Val: %\n", val);
  51.  
  52.     for(i=10000; i>0; i/=10){
  53.         q = val/i;
  54.         buf[c++] = q + '0';
  55.         val -= q*i;
  56.     }
  57.     buf[c++] = '\n';
  58.     buf[c++] = '\r';
  59.  
  60.     buf[c] = '\0';
  61.  
  62.     UART_Write(buf);
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top