Kategori arşivi: MSP430

Traffic Lights and Pedestrian Way Control System with MSP430

Hi, at my this i created a system for traffic lights and pedestrian way control.

You can also watch my Youtube presentation.

Equipments that used in this project are;
+ MSP430 Chipset
+ GLCD (Nokia 5110)
+ Distance Sensor (HCSR-04)
+ Buzzer
+ LEDs
+ Buttons


Procedure of my system is like this;
I have two counters. One stars 60 to zero , another one counts
30 to zero. If we are in the first time interval ( 60 -> 0 ) cars can use their way
but pedestrians should wait until time is end. We can see the down counting from
the screen. If any person attempts to pass across the street ( from car’s road ),
the distance sensor that located edge of car’s road, will war by buzzer.

After first time interval car’s passed. Another countdown starts. (from 30 -> zero )
Cars should stop and people can pass across the street.

Third past is interrupt section. At the late hours traffic won’t be busy so we don’t need
LCD, sensors, buzzer and etc. We just need a blinking LED for warning. That also reduce
the power consupmtion.

P.s. You can find the port interrupt program and my lcd commands from early posts.

Best wishes.

Port Interrupt with MSP430

Hi, at my this post i will introduce a port interrupt to you. It is so simple.

First of all you should your interrupt pin then enable it and define it’s interrupt edge (hi to low or low low high). You can define your ISR (interrupt service routine) as you wish. At my ISR it blink a LED. Also i defined a button to get out of ISR.

Best wishes.


//P2.1 is the interrupt exit button
//P2.4 Interrupt pin
//P2.5 Led Output

#include <msp430.h>
void main(void) {


P2DIR |= BIT5; // Set P2.5 to output direction
P2IE |= 0x10; // P2.4 interrupt enabled 
P2IES |= 0x10; // P2.4 Hi/lo edge
P2OUT = 0x00;

// Port 2 interrupt service routine
#pragma vector=PORT2_VECTOR
__interrupt void Port_2(void)
P2OUT = 0x00;
while((P2IN & ~BIT1) != BIT1){ // Button at P2.1 is high active
P2OUT ^= BIT5;
P2OUT = 0x00;
P2IFG &= ~0x10; // P1.4 IFG cleared

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

1- http://u.osu.edu/ece3567/files/2014/09/AnalogToDigitalConverter_v1-14rgxe1.pdf

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