Capacitive Accelerometer Interfacing

Well in this post I’ll be telling briefly what is an accelerometer and how to interface it with a microcontroller. To be honest I woke up in the middle of the night and couldn’t go back to sleep, so I decided to write this post which I was planning on writing for some time now.

Accelerometer

It is basically a device which is used for measuring acceleration or change in motion. You use one more often than you know. There is an inbuilt accelerometer in your cellular mobile phones and tablets. So now you know what is used for detecting the tilts in your phone. Next time you play temple run or similar game you would know that all this fun is possible due to a technology called accelerometer and other stuff. Then modern laptop hard disks have accelerometers to detect fall. If fall is detected the writing head in retraced so that the disk is not damages and there are no scratches. There are numerous other applications and examples. I just gave food for thought you can explore the rest on your own.

Types of Accelerometer

Well as you may have already guessed there are various types of accelerometers.

  • Capacitive Accelerometer
  • Magneto-resistive Accelerometer
  • Piezo-electric Accelerometer

There are various other types these three being examples.This is a video one of my instructors showed me. The guy explains the working and construction quite well.

This is an article having good information about accelerometers

http://www.engineersgarage.com/articles/accelerometer?page=1

Data from accelerometers

Now that you know how accelerometers work. Let’s come to the topic at hand i.e. using one with your microcontroller.

Well there are different types of accelerometers depending on the type and method of obtaining data. While the data acquisition may be different but processing part is same once you get the reading. So the data may be available as an analog signal or may be it may be available inside the accelerometer in a register which you need to access via a protocol like SPI etc..

ADXL 335

So I’ll be talking about this accelerometer. You can look at the datasheet before deciding to use it.

So this accelerometer gives the output as three analog signals. There are three pins x,y,z for the three axes. Then there is Vcc and GND.

For actually using this signals you need to convert them into digital form. For this you use the inbuilt 12bit ADC that is available in msp430g2553.(If your controller does not have an inbuilt ADC, which won’t be the case, you can use an external ADC or if your application requires faster conversion and better precision and stability then you can use external ADC.) So once you have the data in digital form, next step would be calibration of the accelerometer.

Calibration

Now you have the data in digital form but what to do with it and how to see it? The answer is you send the digital reading serially and observe it on a serial monitor. So you make variables and store the digital reading in those and view the numbers on screen. Now you will make a table for these variables and decide the limits. Suppose you want to detect forward tilt, you can note what are the range of values that the accelerometer gives for the gesture and the using a simple if statement write whatever you want your application to do on a forward tilt.

Position Digital Range of X Digital Range of Y
Forward N.A <658
Backward N.A >705
Left >497 N.A
Right <460 N.A
Stop 470 to 485 695 to 705

 

The above code is an example of finding the ranges. You can then use basic if else for this. If you want a video showing the calibration process do tell me.

Code for gesture controlled bot

void setup()
{
  pinMode(P2_0,OUTPUT);
  pinMode(P2_1,OUTPUT);
  pinMode(P2_2,OUTPUT);
  pinMode(P2_3,OUTPUT);
  pinMode(A0,INPUT);//X
  pinMode(A3,INPUT);//Y
  pinMode(P1_4,INPUT);
  //pinMode(A2,INPUT);//Z
  Serial.begin(9600);
  Serial.println("Start");
}
void loop()
{
  int x = analogRead(A0);
  int y = analogRead(A3);
  int m = digitalRead(P1_4);
  //Serial.print(x);
  //Serial.print(','); //use these lines for calliberation
  //Serial.println(y);
   if(y>520)
  {
  digitalWrite(P2_0,HIGH);
  digitalWrite(P2_1,LOW);
  digitalWrite(P2_2,HIGH);
  digitalWrite(P2_3,LOW);
  Serial.println("BACKWARD");
  //delay(100);
  }
  if(y<460)
  {
  digitalWrite(P2_0,LOW);
  digitalWrite(P2_1,HIGH);
  digitalWrite(P2_2,LOW);
  digitalWrite(P2_3,HIGH);
  Serial.println("FORWARD");
  //delay(100);
  }
  if(x>445)
  {
  digitalWrite(P2_0,LOW);
  digitalWrite(P2_1,LOW);
  digitalWrite(P2_2,LOW);
  digitalWrite(P2_3,HIGH);
  Serial.println("LEFT");
  //delay(100);
  }
  if(x<430)
  {
  digitalWrite(P2_0,LOW);
  digitalWrite(P2_1,HIGH);
  digitalWrite(P2_2,LOW);
  digitalWrite(P2_3,LOW);
  Serial.println("RIGHT");
 // delay(100);
  }
  if(x>430 && x< 445 && y>460 && y<500)
  {
  digitalWrite(P2_0,LOW);
  digitalWrite(P2_1,LOW);
  digitalWrite(P2_2,LOW);
  digitalWrite(P2_3,LOW);
  Serial.println("STOP");
  //delay(100);
  }  
}

(If you would like the embedded c code email me.)

P.S.

If you like my articles do like them. Well just want to say a little appreciation goes a long way. Thank you for reading the post.

Advertisements

16×2 LCD interfacing in 4 bit mode

Hello there!! After trying many times I finally managed to interface LCD in 4 bit mode. I’m writing this post as soon as I got the output and understood what was going wrong. First things first, if you are reading this article directly without knowing about 8 bit mode interfacing then you should read my blog post on 8 bit LCD interfacing. Assuming that you know what 8 bit mode is, let’s begin with the 4 bit mode interfacing.

Need for 4 bit mode

Well for interfacing anything with any processor we need system-bus (data-bus, address-bus and control-bus). In our case for 8 bit mode the 8 data pins (D0-D7) are the data and address bus while the 3 control pins(RS, R/W and E) are the control bus. Thus using these we can control the peripheral that we are interfacing. We are greedy so we want to interface as many peripherals as possible with the same microcontroller. This requires either large number of ports or we need to be smart and utilize what we have to the fullest. Thus first thing is we try to do is reduce the number of pins required for controlling the peripheral. Here comes the need for 4 bit mode. Thus we reduce the port pins required from 11 to 7. It might not seem much, but for a small microcontroller like msp430g2553 with limited port pins this is really a big amount. Now coming to the other method. Maybe we can use demultiplexing , in this way we can use ‘n’ lines to share the system bus with ‘2^n’ devices. I got one tip that we can use SIPO shift register for sending data. Now this will require only 5 port pins. Three control pins and two for serial data and clock.

4 bit mode working

In 4 bit mode we send the data nibble by nibble, first upper nibble and then lower nibble. For those of you who don’t know what a nibble is: a nibble is a group of four bits, so the lower four bits (D0-D3) of a byte form the lower nibble while the upper four bits (D4-D7) of a byte form the higher nibble. This enables us to send 8 bit data be it the ASCII code or the command code by using 4 pins instead of 8. The connections remain identical. The only change is that the lower nibble pins of LCD are unused.

Initializing the LCD in 4 bit mode

This is perhaps the most tricky part of this interfacing. When LCD is powered ON it is by default in 8 bit mode.

reset

This is the command code format for function set operation. Using this we form the command code for 4 bit mode operation.

upper part

mode_set

The function set bits are explained in the following section.

functions_mode_set_bits

We need 4 bit mode so make DL ‘0’. Thus the upper nibble of the command code is 0010b which is 0x02. If we need 2 lines we set N and the normal font so F is 0 thus lower nibble comes out to be 1000b i.e 0x08. Thus total command code is 0x28.

Before sending this 0x28 we need to perform a specific initialization.

initialization

So we send 0x33 as the command code. This will do the initial 8 bit mode starting of LCD. Now after this we need to send 0x32.(Note : We’re using the nibble method so what will go for 0x33 is 3 followed by 3 and for 0x32 is 3 followed by 2.) I was not able to do the initialization. But then I suddenly remembered about a workshop that I had attended on msp430. The professor had taught LCD interfacing in 4 bit mode but due to shortage of time could not go into the details. He had just told about the nibble sending part and that we need to send command codes to initialize. So I opened his file and saw this 0x33 and 0x32 and tried sending it. And to my surprise I got my name on the LCD. So this credit goes to him. His name is Gurjeet Singh Gill.

Now coming back to the topic at hand. So now we have initialized the LCD in 4 bit mode. All we need to learn is how to send the value nibble by nibble in c and without affecting other port pins except the ones that we use for sending data. (In assembly its just the rotate instruction.)

Nibble sending logic

P1OUT = (P1OUT & 0xF0)|((data>>4) & 0x0F); // send higher nibble

In this we make use of masking concept and logical shift operation in c. Now in my program I’m using P1.0 – P1.3 pins as the data pins. Thus I’ll explain the logic accordingly. Here data is the parameter that is passed in the function. I shift the data right by four bits, thus bringing the higher nibble in the lower nibble location. Mask the upper part. Then I make the P1OUT lower nibble 0 so that ‘or’ing with nibble data will give nibble data on P1OUT pin. (x and 0 is 0 ; x and 1 is x; x or 1 is 1 and x or 0 is x—-> (x&0)|(data_bit)= 0|data_bit= data_bit. Thus we get data bit on the port pin without affecting P1.4-P1.7) This takes care of upper nibble.

P1OUT = (P1OUT & 0xF0)|(data & 0x0F); // send lower nibble

Now lower nibble involves the same operations except the shifting operation.

Code

Header file :

// Author : Manpreet Singh Minhas
// This file is for 4 bit mode LCD interfacing with msp430g2553 chip
// 16x2 LCD is used
#include &lt;msp430g2553.h&gt;
#define DR P1OUT = P1OUT | BIT4 // define RS high
#define CWR P1OUT = P1OUT &amp; (~BIT4) // define RS low
#define READ P1OUT = P1OUT | BIT5 
// define Read signal R/W = 1 for reading
#define WRITE P1OUT = P1OUT &amp; (~BIT5) 
// define Write signal R/W = 0 for writing
#define ENABLE_HIGH P1OUT = P1OUT | BIT6 
// define Enable high signal
#define ENABLE_LOW P1OUT = P1OUT &amp; (~BIT6) 
// define Enable Low signal
unsigned int i;
unsigned int j;
void delay(unsigned int k)
{
for(j=0;j&lt;=k;j++)
{
for(i=0;i&lt;100;i++);

}

}
void data_write(void)
{
ENABLE_HIGH;
delay(2);
ENABLE_LOW;
}

void data_read(void)
{
ENABLE_LOW;
delay(2);
ENABLE_HIGH;
}

void check_busy(void)
{
P1DIR &amp;= ~(BIT3); // make P1.3 as input
while((P1IN&amp;BIT3)==1)
{
data_read();
}
P1DIR |= BIT3; // make P1.3 as output
}

void send_command(unsigned char cmd)
{

check_busy();
WRITE;
CWR;
P1OUT = (P1OUT &amp; 0xF0)|((cmd&gt;&gt;4) &amp; 0x0F); // send higher nibble
data_write(); // give enable trigger
P1OUT = (P1OUT &amp; 0xF0)|(cmd &amp; 0x0F); // send lower nibble
data_write(); // give enable trigger

}

void send_data(unsigned char data)
{
check_busy();
WRITE;
DR;
P1OUT = (P1OUT &amp; 0xF0)|((data&gt;&gt;4) &amp; 0x0F); // send higher nibble
data_write(); // give enable trigger
P1OUT = (P1OUT &amp; 0xF0)|(data &amp; 0x0F); // send lower nibble
data_write(); // give enable trigger
}

void send_string(char *s)
{
while(*s)
{
send_data(*s);
s++;
}
}

void lcd_init(void)
{
P1DIR |= 0xFF;
P1OUT &amp;= 0x00;
send_command(0x33);
send_command(0x32);
send_command(0x28); // 4 bit mode
send_command(0x0E); // clear the screen
send_command(0x01); // display on cursor on
send_command(0x06); // increment cursor
send_command(0x80); // row 1 column 1
}

Main Code:

/* LCD_own.c
* Created on: 12-Nov-2013
* Author: Manpreet
* In this program we interface the lcd in 4 bit mode. We send strings and display it on the screen.
*/
#include "lcd.h"

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // stop watchdog timer
lcd_init();
send_string("Manpreet Singh");
send_command(0xC0);
send_string("Minhas");
while(1){}
}

P1.0 – D4            Pin11

P1.1 – D5             Pin12

P1.2 – D6             Pin13

P1.3 – D7             Pin14

P1.4 – RS             Pin4

P1.5 – R/W         Pin5

P1.6 – E                 Pin6

16×2 LCD Interfacing in 8bit mode

lcd_real

Hello there!! In this post I’ll tell you about 16×2 LCD’s and their interfacing in 8 bit mode. As you all know LCD stands for liquid crystal display. Now earlier we used to use 7 segment displays for display purposes, but now LCD’s are preferred. The main reason is we need less number of databus lines for interfacing LCD’s as compared to 7 segment displays. Other reason is we can print various characters on the screen. Now the basic characters are already saved inside CGROM(Character Generator ROM). So you need to send only the ASCII values in order to display the character on screen. So let us see what a 16×2 LCD reakky means. It has 2 rows and 16 columns. So basically a 16×2 LCD has 32 blocks where one can display data. Each block has certain number of pixels. You can draw your own character by saving the pattern of pixels.

Ok then let’s begin. First things first lets get the datasheet. The link is : Datasheet Link

Please download the datasheet because a datasheet tells you everything there is to know the electrical parameters, command registers, pin-outs and so on. Assuming you have the datasheet with you, let’s go further.

Pin-out:

Lcd16x2Now there is a protruding rectangular portion on this LCD. This will help you identify which pin is which. Now let us see what each pin does exactly. But then they have also printed 16 and 1 on the back of LCD, so no need to worry about connecting the pins inverted.

lcd_pinoutThe pin features are explained in the table. The contrast adjust input is nothing but output taken from a pot.contrast-input

So basically when you vary the pot , you get different values of voltage from the voltage divider network. And thus you can change the contrast to suit your visual needs. (Caution: Do not give the LCD voltage greater than 5 Volts. Your LCD may get damaged. By more I’m not talking about 5.1 Volts but 6 V and beyond.)

RS, R/W,E are the control signals of LCD. DB0 to DB7 are the databus lines. You send the command word as well as the data to be written on this bus.

Let’s see a little about the control signals first.

RS : This stands for register select. The two registers in LCD are the data register and the command word/code register. In order to tell LCD that the bits on databus are for which register we make use of RS control signal via the RS pin. When you make this pin high you select data register, where you’ll send the ASCII values to be displayed on screen. When you make RS low you select the command word register where you’ll be sending all the commands for configuring and initializing the LCD.

RS = 1 —–> Data Register

RS = 0 —–> Command Code Register

R/W : This stands for read or write. The read is active high signal and write is active low. Thus when you want to read from the LCD you make the signal on this pin high and when you want to write you make the signal on this pin low.

R/W = 1 —-> Read Operation

R/W = 0 —-> Write Operation.

(For those who are wondering why W has no bar on its top indicating an active low signal, there should be one. Its just that I don’t know how to type W bar!!)

E : This stands for enable. This is a edge triggering signal which is used while writing or reading data to/from LCD respectively. E line is negative edge triggered for write while it is positive edge triggered for the read. The timing diagram given in datasheet tells about the minimum delay between the level transitions.

E = high to low / negative edge triggered —-> Write

E = low to high / positive edge triggered  —-> Read

Busy Flag :  The concept of busy flag is beautiful. Now the LCD internal processor takes time to latch and make the necessary adjustments as per the command word. While the LCD’s internal processor is busy this flag is set. So one should check the status of this flag before sending the next command word or data. D7 is the busy flag pin. You’ll have to configure the port pin connected to D7 pin as input while checking the flag condition. Along with this we need to make RS = 0 and R/W = 1 , since this is read operation and busy flag is given by command code register mode.

Busy Flag = 1 —-> LCD Busy

Busy Flag = 0 —-> LCD can take next data/command

Well you can give delays also for LCD to finish work, but this is better way if you have enough port pins. Because for reading busy flag status you need R/W signal and thus a port pin.

List of LCD Instructions

commands

Using the above table you can make any command byte. For example we’ll be using this LCD in 8 bit mode so make DL = 1, N = 1 and F =0 respectively. The hexadecimal value that we get is 0x38/038h. This is the command word that we must send to the LCD to initialize it in 8 bit mode and use 2 lines with 5×7 dots.

Command Codes

So these are few of the instruction codes that you come across frequently. Of course you can make these on your own by using the command code syntax table.

DDRAM address:

Display data random access memory. This is where the data you send to data register is stored. And it so happens that you can send the address of block to the command code register to position the cursor at that particular block. For example you want to position the cursor at row 2 column 10 , just send 0CAh to the command code register. So that is about the DDRAM and positioning the cursor.

ddram

Connection Diagram:

interfacing121120131277

The connections are shown in the above pictures.

Code:

/*  8bit_lcd.h
 *  Created on: 12-Nov-2013
 *  Author: Manpreet
 */


#include <msp430g2553.h>;
#define DR		  	P2OUT = P2OUT | BIT0 		// define RS high
#define CWR		   	P2OUT = P2OUT &amp; (~BIT0)	// define RS low
#define READ     	P2OUT = P2OUT | BIT1  	// define Read signal R/W = 1 for reading
#define WRITE    	P2OUT = P2OUT &amp; (~BIT1) 	// define Write signal R/W = 0 for writing
#define ENABLE_HIGH P2OUT = P2OUT | BIT2		// define Enable high signal
#define ENABLE_LOW  P2OUT = P2OUT &amp; (~BIT2)		// define Enable Low signal
unsigned int i;
unsigned int j;
void delay(unsigned int k)
{
	for(j=0;j&lt;=k;j++)
	{
		for(i=0;i&lt;100;i++);

	}

}
void data_write(void)
{
	ENABLE_HIGH;
	delay(2);
	ENABLE_LOW;
}

void data_read(void)
{
	ENABLE_LOW;
	delay(2);
	ENABLE_HIGH;
}

void check_busy(void)
{
	P1DIR &amp;= ~(BIT7); // make P1.7 as input
	while((P1IN&amp;BIT7)==1)
	{
		data_read();
	}
	P1DIR |= BIT7;  // make P1.7 as output
}

void send_command(unsigned char cmd)
{
		check_busy();
		WRITE;
		CWR;
		P1OUT = (P1OUT &amp; 0x00)|(cmd);
		data_write();								// give enable trigger

}

void send_data(unsigned char data)
{
		check_busy();
		WRITE;
		DR;
		P1OUT = (P1OUT &amp; 0x00)|(data);
		data_write();								// give enable trigger
}

void send_string(char *s)
{
	while(*s)
	{
		send_data(*s);
		s++;
	}
}

void lcd_init(void)
{
		P2DIR |= 0xFF;
		P1DIR |= 0xFF;
		P2OUT &amp;= 0x00;
		P1OUT &amp;= 0x00;
		send_command(0x38); // 8 bit mode
		send_command(0x0E); // clear the screen
		send_command(0x01); // display on cursor on
		send_command(0x06);// increment cursor
		send_command(0x80);// cursor position
}

Sample Code

/* LCD_own.c
* Created on: 12-Nov-2013
* Author: Manpreet
* In this program we interface the lcd in 8 bit mode. We send strings and display it on the screen.
*/
#include "8bit_lcd.h"

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // stop watchdog timer
lcd_init();
send_string("Manpreet Singh");
send_command(0xC0);
send_string("Minhas");
while(1){}
}

For code explanation watch:

Output:

output

This is all regarding the 8 bit mode interfacing. I’ll be writing  about 4 bit mode and creating custom character if I successfully learn and perform the same. Thank you for reading this post. Hope it was useful and informative.

References :

The 8051 Microcontroller and Embedded Systems using Assembly and C by Mazidi (ISBN-978-81-317-1026-5)

HD44780U Datasheet (Download Link)