Advertisement
igendel

MSP430G2553 ADC Demo - External

May 13th, 2022
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include <msp430.h>
  2. #include <stdint.h>
  3.  
  4. void setOnboardLEDs(const uint8_t greenOn, const uint8_t redOn) {
  5.  
  6.     uint8_t outVal = 0;
  7.     if (greenOn) outVal |= 0b01000000;
  8.     if (redOn)   outVal |= 0b00000001;
  9.     P1OUT = outVal;
  10.  
  11. }
  12.  
  13. int main(void) {
  14.  
  15.     uint16_t aVal;
  16.     volatile uint16_t x;
  17.  
  18.     WDTCTL = WDTPW | WDTHOLD;   // stop watchdog timer
  19.     P1DIR = 0b01000001; // Pin P1.6 (green LED) and P1.0 (red LED) are Outputs
  20.    
  21.     ADC10CTL1 = INCH_4; // Direct ADC to channel 4 (pin 1.4)
  22.     ADC10AE0 = 0b00010000; // Enable analog input for pin 1.4
  23.     ADC10CTL0 = ADC10ON + ENC; // Turn the ADC10 module on and enable it
  24.  
  25.  
  26.     for ( ; ; ) {
  27.  
  28.         ADC10CTL0 |= ADC10SC; // Start conversion
  29.         while (0 == ADC10CTL0 & ADC10IFG) {}; // Wait for it to end
  30.         aVal = ADC10MEM; // Read conversion result
  31.  
  32.         setOnboardLEDs(aVal > 768, aVal < 256);
  33.  
  34.     }
  35.  
  36.     return 0;
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement