Blog Arşivleri

Analog to Digital Conversion Application with MSP430

Hi, now we will try to use analog to digital converter of MSP430. Analog to Digilal 
Converter is a module that convert an analog input to digital signal. By this way 
we can process oursignal easily. In MSP430, i used ADC10. ADC10 means 10 bits. 
My input signal will be value between 0 to 2^N, N is my bit number. 
In ADC10 it is ten bits so resoponse will be 0 to 1023. 

My digital value will be stored in ADC10MEM. It is 10 bits register between 
0x0000 - 0x03FF 

After that we can process our digitilazied signal and use it. At the program below 
i used internal temperature sensor. It is inside of our chip. After that i tried 
an analog input as P1.0 via an potentiometer. 

Able to use ADC we had to set some register. You can find details at (1)

Best Wishes.

// 07.10,2014 ADC10 example
// - A LED is driven by P1.4
// - LED will be ON when my analog input reaches the levet that i decided
// - Internal temperature sensor
#include <msp430g2553.h>
long sample;

void main(void) {
P1DIR |= BIT4;
WDTCTL = WDTPW + WDTHOLD;                             // Stop WDT
ADC10CTL1 = INCH_10 + ADC10DIV_3;                     // Temp Sensor, ADC10CLK/4
ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON;    // Ref voltage/sample and hold time

while(1) {                                            // Sampling and conversion start
ADC10CTL0 |= ENC + ADC10SC; //enable conversion, start conversion
while ( ADC10CTL1 & ADC10BUSY );                       // Wait for ADC to complete
sample = ADC10MEM;                                     // Read ADC sample
if (sample &amp;amp;lt;= 0x02F3)
P1OUT = 0xFF;
P1OUT = 0x00;
If you want to use an external analog input as potentiometer just change one line 

ADC10CTL1 = INCH_10 + ADC10DIV_3; 
ADC10CTL1 = INCH_0 + ADC10DIV_3; // use P1.0 As analog input
and add this code to program
ADC10AE0 = BIT0; // enable P1.0 As analog input


Distance Sensor Application with MSP430

Hi, at my this project i made an sensor application. In the program sensor measures the distance then if it is in the range of mine, it reacts as a LED ON. My sensor is HC-SR04 that has approximately 4.5 meters distance long. It has one input (echo),one output(trigger) and the supply voltages on it. Working princible of sensor (HC-SR04) is like that; we are sending an output signal from echo pin about 20us then set trig as LOW. After that we are waiting the return signal from echo pin. By the way we are measuring the time between sended and recieved. After that we are calculating the time by using speed of the light from the measured time. Last step is converting the time to distance. That is the whole working princible. You can find the code at below. Good Luck.

Sensor Application With MSP430

Sensor Application With MSP430

// Ultrasonic rangefinder example for MSP430G2553
// - An LED is driven by P1.3
// - The trigger pulse is sent to the rangefinder via P1.2
// - The echo pulse is read from the rangefinder via P1.1
#include <msp430.h>

void main(void) {
int distance_cm;
// Disable watchdog timer
TA1CTL = TASSEL_2 + ID_0 + MC_2;
P1DIR = 0x03;

while(1) {
distance_cm = read_distance_sensor_cm();
if (distance_cm < 50) P1OUT |= BIT3; // LED on
else P1OUT &= ~BIT3; // LED off

int read_distance_sensor_cm()
int echo_pulse_duration; // time in us
int distance; // distance in cm
// Send a 20us trigger pulse
P1OUT |= BIT2; // trigger high
__delay_cycles(20); // 20us delay
P1OUT &= ~BIT2; // trigger low
// Measure duration of echo pulse
while ((P1IN & BIT1) == 0); // Wait for start of echo pulse
TA1R = 0; // Reset timer at start of pulse
while ((P1IN & BIT1) > 0); // Wait for end of echo pulse
echo_pulse_duration = TA1R; // Current timer value is pulse length
distance = 0.017 * echo_pulse_duration; // Convert from us to cm
return distance; // Return distance in cm to calling function

//Special thank to Ali Gökoğlu

A Small Step Through MSP430


MSP430 LaunchPad

MSP430 LaunchPad

I have been using Arduino for along time. For one of my lessons, i have to use MSP430 Launchpad so i ordered from It cost me about $10 includes with shipping. i thinks that is so cheap, in the package it comes with 2 processors, data cable and sticker.

At my first project I wrote an code for blinking a LED. It was can be an easy beginning for you but it is entertaining.
that photo is from my workshop.


The MSP-EXP430G2 LaunchPad is an easy-to-use flash programmer and debugging tool for the MSP430G2xx Value Line microcontrollers. It features everything you need to start developing on an MSP430 microcontroller device. It has on-board emulation for programming and debugging and features a 14/20-pin DIP socket, on-board buttons and LEDs & BoosterPack-compatible pinouts that support a wide range of plug-in modules for added functionality such as wireless, displays & more.

My Workshop at MSP430 LaunchPad

My Workshop at MSP430 LaunchPad

#include <MSP430.h>;

unsigned short wGecikme;
void main()
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1SEL &amp;= (~BIT0); // Set P1.0 SEL for GPIO
P1DIR |= BIT0; // Set P1.0 as Output
P1OUT = 0x01; // Set P1.0 HIGH
P1OUT = 0x00; // Set P1.0 HIGH