Part Number:MSP430FR4133
Tool/software: Code Composer Studio
Hi everyone,
I have this code block for ADC. My application is reading voltage from ADC pin. I have some conditions, like if voltage is between 0.8V and 1.2V, turn on the led. There is no problem about debugging also. Please check the code.
#include <msp430.h> #define ENABLE_PINS 0xFFFE // Enables inputs and outputs void ADC_SETUP(void); // Used to setup ADC12 peripheral main(){ WDTCTL = WDTPW | WDTHOLD; // Stop WDT PM5CTL0 = ENABLE_PINS; // Enable inputs and outputs P1DIR = BIT0; // Set RED LED to output SYSCFG2 |= ADCPCTL9; ADC_SETUP(); // Sets up ADC peripheral while(1){ ADCCTL0 = ADCCTL0 | ADCENC; // Enable conversion ADCCTL0 = ADCCTL0 | ADCSC; // Start conversion if ((ADCMEM0 >= 0x3C0)&&(ADCMEM0<=0x5C0)){ // 0.8V<Voltage<1.2V HIGH P1OUT = BIT0; // Turn on red LED } else { // Else P1OUT = 0x00; // Turn off red LED } } // end while(1) } // end main() void ADC_SETUP(void) { #define ADC_SHT_16 0x0200 // 16 clock cycles for sample and hold #define ADC_ON 0x0010 // Used to turn ADC12 peripheral on #define ADC_SHT_SRC_SEL 0x0200 // Selects source for sample & hold #define ADC_12BIT 0x0020 // Selects 12-bits of resolution #define ADC_P81 0x0009 // Use input P8.1 for analog input --->A9 ADCCTL0 = ADC_SHT_16 | ADC_ON ; // Turn on, set sample & hold time ADCCTL1 = ADC_SHT_SRC_SEL; // Specify sample & hold clock source ADCCTL2 = ADC_12BIT; // 12-bit conversion results ADCMCTL0 = ADC_P81; // P8.1 is analog input --->A9 }
Thanks,