Showing posts with label TexasInstruments. Show all posts
Showing posts with label TexasInstruments. Show all posts

Sunday, 19 November 2017

MSP430 with Bluetooth Low Energy RN4020 (BLE)

In this post, we'll see how to use a Bluetooth Low Energy (BLE) module, microchip's RN4020 with the MSP430 launchpad. I have used the MSP430G2553 microcontroller for this purpose. There is a lot of documentation available about BLE on the official website (Bluetooth.com) and just typing the term in google will lead you to several other sources. I may cover certain topics related to BLE in a separate post!

You'll need to know how UART works for this tutorial, I would suggest having a look at this URL:UART
Before connecting the BLE module to the micro-controller, we need to configure RN4020 as a peripheral and also create a private service and its characteristics. The steps required to do the same are as follows:

Connect RN4020 to an FTDI adapter (USB to TTL converter) like the one below:


Connect 3.3 V Vcc to RN4020 WAKE and Vcc respectively, GND to grounds and RX, TX to TX, and RX respectively.

Open Tera term and select the serial port, enable Local echo and select CR+Linefeed for Transmit and Receive from setup, and also set the correct baud rate under Serial Port option, i.e. 115200.

Now type the following commands, after the execution of each command, an ACK or acknowledgment signal will be received on the terminal.

SF,1 //factory default
SB, 4 // baud rate set as 115200
SS, C0000001 //set device information and battery service
PZ //clears all settings of the private service and characteristics
PS, 11223344556677889900AABBCCDDEEFF //define private service with UUID
PC,010203040506070809000A0B0C0D0E0F, 08, 02 //the specified characteristic with write property is added, size is 2 bytes
SR, 20000000 //auto advertise
R, 1 //reboot

Check the list of services by typing LS, the UUID of Device Information, Battery and our private service and its characteristics with their UUIDs and handles will be displayed.

The characteristic that we created will be used to turn the LED ON/OFF via an android application. On typing 00, the LED will turn OFF, if it was ON and on typing 01, the LED will turn ON (if it was OFF). You may create additional characteristics as per your requirement, refer the RN4020 user-guide to know more about different AT commands used to configure the module.

Now download BLE Scanner Application from the Android store: BLE Scanner App

Connect with the RN4020 module from the list of devices and enter 00 or 01 as the value of the private characteristic, do select byte array and not Text to enter the value successfully. After entering the same, you can observe that tera terminal shows a command WC, handle, value. Here the value is either 00 or 01.

If on connecting the MSP430 micro-controller, you are able to read the value, you would then be able to make a decision. Have a look at the code which is loaded on MSP430G2553 which causes an interrupt to be generated whenever any value is transmitted by the BLE module. The code is self-explanatory.


#include <msp430.h>

#include<stdio.h>

char buffer[20]; //buffer to receive

volatile unsigned int receive = 0;

//configure UART

void Configure_UART()

{

P1SEL = BIT1 + BIT2;

P1SEL2 = BIT1 + BIT2;

UCA0CTL1 |= UCSSEL_2;

UCA0BR0 = 0X08;

UCA0BR1 = 0X00;

UCA0MCTL = UCBRS2 + UCBRS0;

UCA0CTL1 &= ~UCSWRST;

}
//configure pins

void Configure_Pins()

{

P1DIR |= BIT0; //Pin 0, LED connection

P1OUT &= ~BIT0;

&nbsp;

}
//main loop

void main(void)

{

WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

BCSCTL1 = CALBC1_1MHZ; // Set range

DCOCTL = CALDCO_1MHZ;

Configure_UART();

Configure_Pins();

UC0IE |= UCA0RXIE;

__bis_SR_register(CPUOFF + GIE); //set cpuoff and enable interrupts
}

//cpu is turned on whenever interrupt occurs and in main routine its off(stack restored)
#pragma vector = USCIAB0RX_VECTOR

__interrupt void USCI0RX_ISR(void)

{

P1OUT &=~BIT6;

buffer[receive++] = UCA0RXBUF;

if(UCA0RXBUF == '\n')

{
receive = 0;

__bic_SR_register(GIE); //disable interrupt as we want to make a decision now

char conn = buffer[0];

if(conn != 'C')

{

char c = buffer[9]; //decide number to be accessed

switch(c)

{

case '1': P1OUT |= BIT0;

break;

case '0': P1OUT &= ~BIT0;

break;

}

}

__bis_SR_register(GIE); //okay to talk again

}

}

Finally, the demo can be found on Youtube: Demo Video

Saturday, 18 November 2017

MSP430G2553 with a Light Dependent Resistor

In this tutorial, we will connect a Light Dependent Resistor to an MSP430 and turn ON the LED connected to Port 1, pin 0 if the light is below a threshold value else we will turn it OFF.

The LDR is connected to Port 1, pin 7 (configured to act as input channel for the ADC). The SAR ADC on MSP430 converts the analog values supplied by the LDR to digital values.

As displayed in the video (link given below), the LDR is connected in a voltage divider circuit with a 10K Ohms resistor. The detailed theory about the operation of the voltage divider circuit and the formula for calculating the voltage due to variation in resistance of the LDR can be found on: http://robotics.hobbizine.com/ldrlaunch.html

Youtube video link: Demo Video

#include <msp430.h>

unsigned int value = 0;

void ConfigureADC(void)

{
ADC10CTL1 = INCH_7 + ADC10DIV_0;
ADC10CTL0 = SREF_0 + ADC10SHT_2 + ADC10ON + ADC10IE;
ADC10AE0 |= BIT7;
}

void ConfigurePins(void)
{
P1OUT = 0;
P1DIR |= BIT0;
}

void main(void) {

WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

BCSCTL1 = CALBC1_1MHZ;

DCOCTL = CALDCO_1MHZ;

ConfigurePins();

ConfigureADC();

while(1)

{

ADC10CTL0 |= ENC + ADC10SC;

__bis_SR_register(CPUOFF + GIE);

value = ADC10MEM;

if(value <= 511)

{

P1OUT |= BIT0; //turn ON the Light

}

else

{

P1OUT &= ~BIT0; //turn if OFF

}

}

}

#pragma vector = ADC10_VECTOR

__interrupt void ADC10_ISR(void)

{

__bic_SR_register_on_exit(CPUOFF);

}

You may use this circuit with a bulb and power the circuit with a solar panel during day time and use the saved power to turn ON the bulb after evening. Another interesting project along similar lines is: Solar Powered Project using MSP430
Kindly comment if you want to know anything specific about the H/W or S/W part!
Next post will be on an MSP430 + Bluetooth low energy module!

MSP430G2553 - Toggle LED with a switch

In this post, we'll learn to use a switch to toggle an LED using MSP430G2553 μcontroller.

The LED and switch are connected to Port 1, pin 0 and pin 3 respectively. Clicking the switch generates an interrupt. Initially, the LED is in OFF state, if the switch is pressed, the LED starts blinking till next interrupt occurrence/switch press.

If pull mode is enabled by setting PxREN register then either the pull up or pull down option can be selected by setting or clearing the corresponding bit in PxOUT register. One can either select high to low or low to high transition as the trigger for interrupt by setting/clearing the corresponding bit in PxIES register. Detailed information regarding the port settings can be found in the datasheet.
The state of the switch is tracked by a variable named "blink".

Do check the demonstration video: MSP430 with a switch and an LED

#include<msp430.h>

unsigned int blink = 0;

void main(void)

{

 WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

 P1DIR |= BIT0; // Set P1.0 to output direction

 P1OUT &= ~BIT0; //clear pin

 P1REN |= BIT3; //enable pull mode 

 P1OUT |= BIT3; //pull up

 P1IES |= BIT3; //triggers when button pressed, high to low 

 P1IE |= BIT3; //enable interrupt 

__enable_interrupt(); //global/general interrupts enabled
for(;;) 

 { 

if (blink > 0)
{
P1OUT ^= BIT0; //toggle LED
__delay_cycles(100000); //delay of 10000 cycles at 1MHz
  }

 }
}
//Port 1 Interrupt Service Routine

#pragma vector = PORT1_VECTOR

__interrupt void Port_1(void)
{
blink ^= 0x01;
P1IFG &= ~BIT3; // P1.3 interrupt flag cleared
P1OUT &= ~BIT0; // clear LED to start in off state
}

MSP430G2553 – LED/Blinky

Blinking LED is similar to "Hello World" for an embedded engineer. I started using launchpad as a part of my MSc. project and have been experimenting with it since then. As there are many blogs and websites that assist a beginner or a pro alike, I'll mention all that I referred (wherever relevant).

The best blog to get started with MSP430 would be : MSPSCI Blog

This blog covers all the basics required to get started with the development process. I have used MSP430G2553 microcontroller on a launchpad board for this project.

Hardware: Although, there is an on-board LED, I have used an external one and have connected everything on a breadboard. (Picture will be added soon).

Code: There is a default project called Blink.c (CCS), you may use that or the below mentioned code.
 
#include<msp430.h>

void main(void)

{

WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

P1DIR |= BIT0; // Set P1.0 to output direction

for(;;)

{

volatile unsigned int i; // volatile to prevent optimization

P1OUT ^= BIT0; // Toggle P1.0 using exclusive-OR

__delay_cycles(100000); //delay of 10000 cycles at 1MHz

}

}