Bit set, reset, toggle and masking in Microcontrollers

Well I would like your concepts on bit set and reset operations to be clear. No matter which language you use be it assembly or embedded c, you will always require to set and reset register bits.

Now whether you want to initialize some peripheral or poll something you will require these three operations.

Bit set operation

This means making a particular bit in an register as 1 or setting it. Now let us see this property of logical OR.

x OR 1 = 1

So whatever be the value of x if you OR it with 1 the result will be 1. This is what we want right? I’ll be taking one example after explaining bit reset.

Bit reset operation

This means that you want to make a particular bit of a register 0 or reset it. We make use of the property of logical AND:

x AND 0 = 0

So whatever be the value of x if you AND it with 0 the result is 0. This is what we wanted so bit reset is done by logical AND operation.

Masking

Often you are interested only in a particular bits of a register. This may be the input of some sensor or something else. Now you want to use masking in this scenario. So masking means you reset all the bits that are not required and let the bits under consideration as it is. Again we make use of logical AND properties.

x AND 0 = 0

x AND 1 = x

So you make the masking pattern by making the bits you want as 1 and the ones you don’t want  as 0. Convert this value to hex and logical AND that mask value with the register.

Bit toggle

Bit toggle is also useful. So for this we make use of the property of ex-or as follows.

x ex-or 1 = complement of x or x-bar

Thus if you ex-or the bit with 1 the bit will be toggled.

Example

130620141602

R &= 0x2081 // This is the masking operation in embedded c

R |= 0x2081 // This will set the bits 0,7,13

R ^= 0x2081 // This will toggle bits 0,7,13

So as you can see this example covers masking. Similarly you need to make the bit patterns for bit set, reset and toggle operation.

If you want more examples comment here. Please like the posts if you like what I have written. And do subscribe to get notifications of my new posts.

Advertisements

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.

A taste of Image Processing

Image processing involves extracting information from images and using the information so obtained for various operations and tasks. Don’t confuse image processing image processing with image manipulation that involves adjusting the images. Photoshop and similar software are used for image manipulation.

Application areas

  • Medical Applications
  • Industrial Applications
  • Military Applications: Some of the most challenging and performance-critical scenarios for image processing solutions have been developed for military needs, ranging from detection of soldiers or vehicles to missile guidance and object recognition and reconnaissance tasks using unmanned aerial vehicles (UAVs). In addition, military applications often require the use of different imaging sensors, such as range cameras and thermo-graphic forward-looking infrared (FLIR) cameras.
  • Law Enforcement and Security: Surveillance applications have become one of the most intensely researched areas within the video processing community. Biometric techniques (e.g., fingerprint, face, iris, and hand recognition), which have been the subject of image processing research for more than a decade, have recently become commercially available.
  • Consumer Electronics
  • The World Wide Web

So as we can see image processing has wide areas of application.

That being said you need some software or programming language for making this image processing possible. There are various ways by which this can be done. You can use c, NI LabVIEW, MATLAB etc. for image processing. For this post I’ll be using MATLAB. Now we did this just so as to understand the concepts of image processing. So if you feel that its meagre you can build on this.

Code

clc;
close all;
clear all;
video = videoinput('winvideo');%Create video variable

set(video,'FramesPerTrigger',1); % Setting frames per trigger
preview(video);%Preview the video
rgb_image = getsnapshot(video); % Storing Image in an array variable
[y x c]= size(rgb_image); % Determining the size of the captured frame.
x1 = (x/2)-(0.2*x);
x2 = (x/2)+(0.2*x);
y1 = (y/2)-(0.25*y);
y2 = (y/2)+(0.25*y);
global s;
s = serial('COM4')
fopen(s)
while(1)
image = getsnapshot(video);
fR = image(:,:,1);
fG = image(:,:,2);
fB = image(:,:,3);
I = fR>200;
se=strel('disk',5);
B=imopen(I,se);
final=imclose(B,se);
[L,n]=bwlabel(final);
for k=1:n
    [r,c]=find(L==k);
    rbar=mean(r);
    cbar=mean(c);
end
rbar
cbar
if x1<cbar<x2 &&  rbar<y1
    disp('Move forward');
    global s;
    fwrite(s,'w')
elseif cbar<x1 && y1<rbar<y2
    disp('Move right');
    global s;
    fwrite(s,'d')
elseif cbar>x2 && y1<rbar<y2
    disp('Move left');
    global s;
    fwrite(s,'a')
elseif x1<cbar<x2 && rbar>y2
    disp('Move back');    
    global s;
    fwrite(s,'s')
elseif x1<cbar<x2 && y1<rbar<y2
    disp('Move stop');
    global s;
    fwrite(s,'f')
end
end

Now this is the code. I am thinking I’ll just explain the logic and then you can use MATLAB help for the rest. Seriously the help provides is simply awesome. You can understand how the functions word using the help and then there is the world wide net. So I am assuming that those of you who has interest will read further.

Now let’s begin understanding the code.

video = videoinput(‘winvideo’);

This will create a video object from the available cameras. You can check the available ones by using imaqhwinfo

image

So for the windows winvideo is installed adapter.

image

Next give winvideo as the agrument to the imaqhwinfo(). Now if you have an external webcam connected to your computer you will se two device ID’s. So now suppose you want to know about the device with ID=1 all you need to do is pass device ID as second argument.

image

You can see the properties you the webcam.

So just use the videoinput() for creating a variable attached to the particular webcam in MATLAB. In our case video is the variable.

Next you set the frames per trigger i.e. whenever you give the capture commands how many frames will be captured every time.

Then you see a preview of the video feed so that you come to know what exactly is the camera viewing.

Next up you take a sample shot so as to determine the dimensions of the camera.

You will get a 3D matrix of the image. Extract the x and y resolution.

Now for this application what we will do is divide the webcam field into 9 quadrants and take decision depending on position of the image of the object to be detected.

Now since we wanted to learn the basic we used white light such as a torch as the source. If you have a proper webcam and good lighting conditions you can detect normal coloured balls using this program. All you need to do is some thresholding.

Now as usual open the serial port and send a particular character depending upon the quadrant in which the object lies.

So you basically take a snapshot again and again and manipulate that snap. So each image is made of 3 components red, blue and green. Since we are using white light as the object it does not matter which component you choose. But if there is a particular colour that the object has take that particular matrix. Now do the thresholding so that you get only the torch circle on the screen.

Then you remove the noise and coalesce the remaining parts to form a single body.

Then you calculate the centroid and take decision.

image

This is a sample of what the image will look like after thresholding. So as you can see it lies in the middle quadrant.

We had written the code such that it will transmit w,a,s,d,f depending on the quadrant. Do top middle corresponds to w and so on you can figure that yourself.

On the controller side we manipulate the data to control the bot.

I’ll be uploading the video soon. Thank you for reading. Hope this was useful.

GUI controlled bot

Well in this post I’ll be covering how to create a GUI for controlling a bot using MATLAB. Now this concept can be readily extended to various other stuff. As after learning alphabets one can make words and sentences and essays so can one make various applications using basics. So let’s start, shall we?

To begin with you need to read this post on creating GUI because I’ll be assuming you have read that. So we know how to make buttons now. So this tutorial will focus on using this GUI along with serial class of MATLAB.

Serial Communication in MATLAB

Well we all know man is a social animal. We need someone or the other to communicate with others. We may use words or gestures or expressions etc. Similarly MATLAB needs to communicate with the outside world to get data in order to manipulate the data and send the processed result back. The way to communicate is via RS232 protocol.

There is a serial class in MATLAB that lets you to create objects and manipulate its properties.

Syntax:

obj = serial(‘port’)

Here port is the COM name and obj is the object name. You can change the baud rate and other properties like number of party bits and stop bits etc. I won’t be covering that in this post. If you have further interest you may refer to their website.

Now that we have a object for the serial COM port we need to open that port for serial communication. Now the syntax for the same is as follows.

fopen(obj)

This will open the COM port for communication. Similarly there is fclose(obj) that will close the port.

Now that we know how to open and close the port might as well learn how to write data to the port.

fwrite(obj,data)

Here obj is the object created and data is 1 byte data that you want to send/write on the port.

For example :

fwrite(s,’a’)

Well that takes care of the serial part that is required for our task. Remember that internet is a universe of knowledge and you can travel it to unravel new mysteries whenever you like.

Now many of you out there who are programmers are already aware of the concept of global and local variables. We’ll require this concept as well. For creating a global object just type global keyword before the object. Now whenever you want to access the global version of the variable just type global before the object or variable.

Now in the GUI code you need to create a global object, open the port. Then the logic we’ll be using for making this GUI work is as follows:

1) Write code in the button callback function to send a specific character. For example send ‘W’ for forward and so on.

2) Now the controller comes into picture wherein you have to receive the character and take some action depending on the character received.

So basically you need to use the UART module of your controller whether it be msp430 or PIC or 51 or arduino or atmel or and processor for that matter. The logic is simple if you are using embedded c or assembly language you can write a simple ISR that will check the character received serially and send some data to port pins that will drive the motor via the motor driver.

I think this much explanation is enough. If you have any queries you can always mail me or comment, I’ll get back to you as soon as possible.

Code

/*
 * serial_reception.c
 *
 *  Created on: 30-Mar-2014
 *      Author: MANPREET
 *     Website: https://learningmsp430.wordpress.com/
 */

#include <msp430g2553.h>
#include "serial.h"

void main(void)
{
	WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
	uart_init(); // Refer the UART post for the functions
	P1DIR |= BIT3|BIT4|BIT5|BIT6;
	P1OUT = 0;
	 IE2 |= UCA0RXIE;     // Enable USCI_A0 RX interrupt
	 __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
}

#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
 if(UCA0RXBUF=='W' | UCA0RXBUF == 'w')
 {
	 P1OUT |= BIT3|BIT5;
	 P1OUT &= ~(BIT4|BIT6);// Send 1010 for forward
 }
 if(UCA0RXBUF=='S' | UCA0RXBUF == 's')
 {
	 P1OUT |= BIT4|BIT6;
	 P1OUT &= ~(BIT3|BIT5);// Send 0101 for backward
 }
 if(UCA0RXBUF=='A' | UCA0RXBUF == 'a')
 {
	 P1OUT |= BIT4|BIT5;
	 P1OUT &= ~(BIT3|BIT6);// Send 0110 for left
 }
 if(UCA0RXBUF=='D' | UCA0RXBUF == 'd')
 {
	 P1OUT |= BIT3|BIT6;
	 P1OUT &= ~(BIT4|BIT5);// Send 1001 for right
 }
 if(UCA0RXBUF=='F' | UCA0RXBUF == 'f')
 {
	 P1OUT &= ~(BIT3|BIT6|BIT4|BIT5);// Send 1001 for right
 }
}

MATLAB Code link:

https://www.dropbox.com/s/iyddas4ynf93rz5/project_bot.fig

https://www.dropbox.com/s/78fzdopyci2g5km/project_bot.m

GUI:

GUI

 

Note :

The connections are obvious if you read the code. You have to use a driver like L293d of l298. If you face any difficulties doing this feel free to contact me. I’m happy to help.

Introduction to creating GUI using MATLAB

In this post I’ll be talking about MathWorks MATLAB software and how one can create GUI’s using the software. This GUI creation has been made really simple in MATLAB.

Introduction

MATLAB® is a high-level language and interactive environment for numerical computation, visualization, and programming. Using MATLAB, you can analyse data, develop algorithms, and create models and applications. The language, tools, and built-in math functions enable you to explore multiple approaches and reach a solution faster than with spread sheets or traditional programming languages, such as C/C++ or Java. You can use MATLAB for a range of applications, including signal processing and communications, image and video processing, control systems, test and measurement, computational finance, and computational biology. More than a million engineers and scientists in industry and academia use MATLAB, the language of technical computing.

This paragraph is what is given on their website. So MATLAB is a very versatile software. One can use it for many applications. We learnt MATLAB as a part of our curriculum. We learnt how to make graphs, make simple programs, analyse LTI systems using MATLAB. But recently I learnt how to make GUI using MATLAB because my project needed this. So here I am sharing my knowledge with you all.

GUI

GUI stands for a graphical user interface. This is nothing but a graphical utility that is used to make the life of a common user easier. A graphical user interface takes inputs as mouse clicks and others. Let me give you an example. Those of you who are familiar with present day LINUX operating system have seen the windows/apple like interface. But earlier there used to be a command prompt like terminal in LINUX. Here one had to type in list of commands for performing any task. So if you want to see the calendar you needed to type in the command related to the same. But now there is a GUI available for it. Behind the scene there is extensive coding done so that the user does not have to be a coder in order to use the system. So this was an example of GUI’s making our life easier.

Now let us discuss the scope of GUI’s in our micro controller based projects. There is a huge scope for GUI’s here. For example you can make a GUI that could have just two buttons one Red LED and the other Green LED. You could program it in such a way that whenever you pressed this button the corresponding LED on Launchpad will glow. This is just a simple example you can take this to the next levels by making real time graphs using MATLAB. For example there is a sensor that is collecting data and sending it serially to your computer. You can plot real time graphs of that data and do manipulations on that data. This is the signal processing part. This can be easily done using MATLAB.

Steps for creating a GUI

Step 1: Open MATLAB

Step 2 : New—> Graphical User Interface

Step 3 : Select blank GUI

Step 4 : Create your own GUI using the palette available on the left hand side (for more details view the video tutorial.)

Step 5 : Save the file as ‘.fig’. After this step the ‘.m’ file of your GUI will automatically open in front of you.

Video

For more details refer the documentation provided in MATLAB and http://www.mathworks.in/