# ADC and PWM help (MSP430FG4618)

**URL:** <https://community.sparkfun.com/t/adc-and-pwm-help-msp430fg4618/17101>\
**Category:** MicroMod\
**Tags:** msp\
**Created:** [August 23, 2010, 9:45pm UTC](https://community.sparkfun.com/t/adc-and-pwm-help-msp430fg4618/17101 "2010-08-23T21:45:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![err0r](https://avatars.discourse-cdn.com/v4/letter/e/58f4c7/32.png) [@err0r](https://community.sparkfun.com/u/err0r)\
**Post date:** [August 23, 2010, 9:45pm UTC](https://community.sparkfun.com/t/adc-and-pwm-help-msp430fg4618/17101/1 "2010-08-23T21:45:22Z")

</div>

Hello all. I was just wondering if someone can help post some tips for my project. My project involves a flex sensor (potentiometer that ranges from 60k to 100k ohms). Using external circuitry to send various voltage levels (from changes in the potentiometer) to the micro. I want to perform ADC on these voltage readings (0-2V). Then based on these voltage readings I want to implement PWM to drive a servo. The servo can function between 600us to 2400us.

I want to set it up such that:

- - 100k ohms (no flex) will drive the servo at 600us.

- - 85k ohms (mid flex) will drive the servo at 1500us
- - 60k ohms (max flex) will drive the servo at 2400us

In addition to these settings, I want every resistance in-between (the entire range of 60k-100k). One of my main problems is that I don’t know how to direct the reading from ADC (what port does that reading comes in on?) to begin PWM? Also, I’m not sure how to setup PWM. I know it involves timers but I don’t know much more than that.

Any/all help is much appreciated.

Also, below is my setup code for ADC. Comments appreciated on this as well.

```auto
#include <MSP430xg46x.h>
#include "intrinsics.h"
#include "io430.h"
#include "header_file.h"

//--- MAIN ---//
int main (void)
{
  WDTCTL = WDTPW | WDTHOLD; // Stop watchdog
  
//--- ADC --//
  ADC12CTL0 = ADC12ON | REFON | REF2_5V | SHT0_7; // ADC12ON = enables ADC12
                                                                               // REFON = enables reference voltage (for testing purposes)
                                                                               // REF2_5V = 2.5 reference voltage
                                                                               // SHT0_7 = 2^7 = 128 cycles
  
  ADC12CTL1 = SHS0 | SHP | ADC12DIV0 | ADC12SSEL0 | CONSEQ0 ;       
                                                                               // SHS0 = ADC12OSC internal timer bit
                                                                               // SHP = Sample/Hold Pulse Mode  
                                                                               // ADC12DIV0 = No clock divisions
                                                                               // ADC12SSEL0 = ADC12OSC as source
                                                                               // CONSEQ0 = Single-channel single-conversion
  
  ADC12IE = BIT0; // Enable interrupt on bit0
  ADC12MEM0 = ZERO; // Clear mem
  
  asm (" EINT"); // Enable global interrupts
  
}

```
