SIM 300/900 (GSM Module)

INTRODUCTION

SIM 300 is a GSM modem with a simple serial interface. SIM 300 modem can accept any GSM network operator SIM card and act just like a mobile phone with its own unique phone number. With this module one can send/receive sms, connect to internet via GPRS and receive calls. The modem can either be connected to PC serial port directly or to any microcontroller. When purchasing purchase the entire board. As it comes with RS232 to TTL converter and ethernet port. Also do check the module by calling a few times when in the shop.

You can purchase this module online. Some of the sites are listed below:

  1. http://www.nskelectronics.com/sim300_modem_with_rs232.html
  2. http://robokart.com/wireless-modules/gsm-gprs/sim-900a-gsm-gprs-modem.html

SIM300_INTERFACE_MODULE_RS232_TTL-500x500

Fig.1 SIM300 Module

There are two LEDs on the board. One is power LED and the other is the network LED. When you insert your SIM card into the slot and power ON the device the power LED will be turned ON. After few seconds the network LED will start blinking after an interval of 3 seconds. If this happens it means signal is proper but if it is blinking faster it means that there is no network. If your mobile phone has network then this module should have network at the same location(provided the antenna is connected.) Make a call and it should ring. Do it a couple of times before purchasing from a store.

AT commands

These are the Haye’s command set also called AT commands. AT stands for attention. These commands are used to control the modem. Using these commands the modem can be operated. There are different commands for sending/reading sms etc. For further information about the history you can read the Wikipedia article.

The AT command set can be downloaded here.

at_commands

Above table lists few of the commands. The most basic command is AT and the response is OK. If you get OK then it means that everything is working fine.

exteneded_at

Now to test out the commands or for direct interfacing with the PC or laptop you can use USB to RS232 adapter.

USBRS232Cable_1

Fig. 2 USB to RS232 Adapter

You will need to install prolific drivers. These will be included in a small CD that accompanies the adapter or you can download here.

Once you do that there will be a COM port available now. Some of the basic commands are explained in the following video.

Advertisements

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/

PWM in microcontrollers

What is PWM? Why do we need it?

Well those of you who know about principles of communication PWM stands for pulse width modulation. Now modulation refers to change in output signal in proportion to the input signal. Now in communication systems in order to transmit data over antenna  we modulate the signal and translate it to a higher frequency. But how does PWM help in microcontrollers? Lets consider an LED which is connected to the port pin of your microcontroller. Suppose the high logic level corresponds to 3 Volts and low logic level corresponds to 0 Volt. So when you make the port pin ON the LED will get 3Volts. But suppose the LED is too bright for your application you want to change the brightness somehow. One way is to use a potentiometer to vary the voltage. But that would require an external hardware as well as manpower(you have to manually adjust the pot value). Suppose that you want to use the software and the timer hardware inbuilt in your microcontroller to change this voltage, then what would you do? The answer my friend is PWM. We give pules of varying duty cycle at the output of port pin. This in turn will give us varying average DC values.

How varying voltage is obtained using PWM?

261120131291

Thus we can vary the duty cycle and in turn vary the dc voltage available at the port pin. When we vary the duty cycle we are in turn varying the pulse width and doing pulse width modulation. This PWM can be used to vary the speed of DC motors without using a DAC(if you don’t want precise speed control you can use this.) Now about the frequency of pulses. The time delay should be small enough that there are no jerks in the motor. One other application is dimming effect in LED’s. So if you want to make LED ON in an expensive way(I’ll tell expensive why very soon) i.e by making a subtle transition from the OFF state to ON state you use PWM. Just change the duty cycle of the pulses gradually from 0 to 100%. This will give you the elegant transition. Now coming to the expensive part. Suppose you own a car and there is this interior light system which uses a proximity sensor or any other sensor to detect your presence and stars the lights. What would look more elegant to you the lights abruptly turning ON or the slow and smooth transition? Well I would like the cool effect. Now this will raise the cost of that system drastically. So these are two applications of this PWM concept.

Persistence of vision and dimming effect

Well you might know about the persistence of vision concept. If the change is taking place in less than tenth of a second then human eye is unable to distinguish the change. This concept is used in motion pictures. Now suppose your PWM frequency is 1Hz i.e 1 cycle per second. You will clearly see the LED’s turning on and OFF. So the trick is to make the LED’s toggle at a rate or frequency that gives the human eyes the illusion that LED is  continuously ON . So that is the small part that one needs to take care while using PWM for the dimming effect. But lower the frequency lower the power dissipated, so use a frequency that is just enough to make the LED’s not seem flickering.

Timer A and PWM

Well now we have a background knowledge of PWM, so let’s learn how to achieve PWM using Timer A of msp430. I had covered how to use Timer A in up mode. So we’ll learn how to obtain PWM in up mode and most importantly get the output on a port pin and know which pin it is.

Well for Timer A initializing please refer my earlier post. Now we’ll focus on OUT modes.

out_modes

Well the description given is clear enough. But still one example will aid to the understanding of the modes.

example_out_mode_in_up_mode

The Output modes are explained in terms of Timer A up mode. We’ll be using the TA0CCR0 and TA0CCR1 register for this particular experiment. Since we are using TA0CCR1 we have to see the output at the OUT1 of Timer A0. Now since this is a special output, we have to select the port special function mode. This is given in the data sheet.

port_functions

Look at the P1.2 functions. We are interested in TA0.1. So we need to make the port pin as output and then select the function 2 i.e set the P1SEL BIT2. With that we cover the PWM and its generation.

Code

/*
* main.c
* Created on    : 26-Nov-2013 1:52:12 AM
* Author        : Manpreet Singh Minhas
* Website       : https://learningmsp430.wordpress.com
*/

// Aim : To create dimming effect using PWM.

#include "msp430g2553.h";
void main(void) {
WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer

P1DIR |= BIT2;               // Initialize port for Output signal
P1SEL |= BIT2;
P1SEL2 &= ~BIT2;
TA0CCR0 = 0xFF;              // Give the time delay small enough to avoid visible flickering
TA0CCR1 = 0xFF;              // Give initial duty cycle 0
TA0CCTL1 = OUTMOD_2;
TA0CTL |= TASSEL_1|ID_0|MC_1;
int i;
int j;
for(;;)
{
for(j=0;j<0xFE;j++){
for(i =0;i<3000;i++){}
--TA0CCR1;                   // Increase the duty cycle slowly
}
for(j=0;j<0xFE;j++)

{
for(i =0;i<3000;i++){}
++TA0CCR1;                   // Decrease the duty cycle slowly
}
TA0CCR1=0xFF; // Give initial count again
}
}

Alternate which involves LPM and interrupt

/*
* main.c
* Created on     : 27-Nov-2013 3:52:12 PM
* Author         : Manpreet Singh Minhas
* Website        : https://learningmsp430.wordpress.com
*/
#include <msp430g2553.h>
int i=1;
void main(void) {
WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer

P1OUT = BIT0;
P1DIR |= BIT0|BIT6|BIT2;
P1SEL |= BIT2;
P1SEL2 &= ~BIT2;
TA0CCR0 = 0x7FF;
TA0CCR1 = 0x7FF;
TA0CCTL1 = OUTMOD_2;
TA0CTL |= TASSEL_2|ID_0|MC_1|TAIE;
_BIS_SR(LPM0_bits|GIE);
}

#pragma vector=TIMER0_A1_VECTOR
__interrupt void TimerA(void)
{
switch(i){
case 1:
--TA0CCR1;
if(TA0CCR1==0)
{
i=2;
}
break;
case 2:
++TA0CCR1;
if(TA0CCR1==0x7FF){i=1;TA0CCR1 = 0x7FF;}
break;
}
P1OUT ^= BIT0|BIT6;
TA0CTL &= ~TAIFG;

}

Alternate version

/*
* main.c
* Created on     : 26-Nov-2013 1:52:12 AM
* Author         : Manpreet Singh Minhas
* Website        : https://learningmsp430.wordpress.com
* Aim            : Well this is nothing but the same program written differently.
*                       In this the dimming effect will take place once when you push
*                       the button connected to P1.1.
*/
#include <msp430g2553.h>
int i;
int j;
void main(void) {
WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer

P1OUT = BIT0;
P1DIR |= BIT0|BIT6|BIT2;
P1IE |= BIT1;
P1DIR &= ~BIT1;
P1REN |= BIT1;
P1OUT |= BIT1;
P1IES |= BIT1;
P1SEL |= BIT2;
P1SEL2 &= ~BIT2;
TA0CCR0 = 0xFF;
TA0CCR1 = 0xFF;
TA0CCTL1 = OUTMOD_2;
//TA0CTL |= TASSEL_2|ID_0|MC_1|TAIE;
TA0CTL |= TASSEL_2|ID_0|MC_1;
_BIS_SR(LPM0_bits|GIE);
}
#pragma vector=PORT1_VECTOR
__interrupt void Port1(void){
P1OUT ^= BIT0|BIT6;
for(j=0;j<0xFE;j++){
for(i =0;i<3000;i++){}

--TA0CCR1;
}
for(j=0;j<0xFE;j++){
for(i =0;i<3000;i++){}
++TA0CCR1;
}
TA0CCR1=0xFF;
P1IFG &= ~BIT1;
}

Effect Video

You can see the effect in this video.

Assembly Language Programming Inception

Introduction

Well I really wanted to write about assembly language programming, so here I am writing about it. I first came across the term assembly language when I was learning about Intel’s 8085 in my junior college. There we were introduced to this concept. When we talk with other humans we use sentences to communicate. Now these sentences are made up of words and the words from alphabets. One more thing is language, the letters are specific to the language being used. If I say ”ਤੁਹਾਡਾ ਨਾਮ ਕੀ ਹੈ” you would be able to understand this only if you know Punjabi language. So the interpretation is done by the brain in normal conversations. We come across translators who translate one language to another. Now suppose that I know only Punjabi and you know only English, then we would need a translator. This enables us to use the language we know without having to learn a new language. Now correlating this example to processors, processor only understands binary language i.e. 1’s and 0’s. We understand English(well since this is an international language I’m stating this) so we can’t write 11011110000 directly. We need some English like language for talking to the controller. This is where assembly language comes into picture. The instructions are given certain English like terms so that we humans can easily write programs and understand the written programs. Now each instruction in assembly language has an opcode which is nothing but a hex value. This hex or hexadecimal value is what the processor understands. When we were learning 8085, we used to do manual assembly. We wrote the code in assembly language, then using the opcode sheet wrote the corresponding opcodes down. Now these lines in hexadecimal numbers would be our code. This code was written into the FLASH/ROM memory. Since we manually used to write the hex values into the program memory this is called manual assembly. We talk of modern microcontrollers now. Today we get IDE’s and compilers and assemblers for all the modern microcontrollers be it msp430 or Arduino or Atmel. We write the program in English like language aka assembly language. The compiler will convert this assembly program into hexadecimal values and the assembler will burn this code in the flash memory. So the compiler generates the hex file (Now the name hex file makes sense,doesn’t it??!). This hex file is burnt into the flash memory i.e assembled into the memory  by the assembler. Now in this post I won’t be going into the details of how the program is burnt. We shall focus on how to write the code for now.

Starting with msp430 assembly language

Now that we know what assembly language is, let’s study msp430 assembly language. First thing is to learn the instructions. For that please refer the user guider  msp430x2xx for this. The section 3.4 is instruction set. The instructions are so beautifully explained there that I feel no need to explain each and everything again here. In order for anyone to write in assembly language one should know the instruction set thoroughly. Now I assume you know the instruction set from this point on!!!(If you do not understand any instruction you can comment on my blog. I’ll do the best I can to help you.) . Along with instruction set please read the addressing mode section as well. It is required as well.

CPU Registers

One of the most important resources of CPU is its registers. Most of the instructions involve them as one of the operands. Following picture shows all the registers.

registers

As we can see there are 16 registers in total. With 4 of them being special purpose registers and rest 12 general purpose registers. I’ll tell you about the PC,SP and SR for now.

PC

PC stands for program counter. This register contains the memory location of current instruction under execution. When the chip is reset,  program counter (PC) is loaded with address contained at reset vector location (0FFFEh). So when you reset the chip, [reset vector]–> PC. The reset vector address for mspx2xx is 0FFFEh. This feature is important while writing assembly language programs. I’ll talk about this when we write our first assembly language program.

SP

SP stands for stack pointer. Now this stack pointer points to top of stack. Stack is like a scratch pad where we store temporary data. We can push data onto stack. We use push and pop instructions while dealing with stack. Stack is nothing but RAM.

SR

SR stands for status register. This contains information about the result obtained after arithmetic or logical operations. Then SR bits are used to configure the low power modes in msp430. Also you can enable interrupts by setting GIE bit of SR. All in all SR is an important special register and has multiple applications.

Software

I’ll be using IAR Embedded Workbench for assembly language programs. The only reason for this is that the directives used in IAR are similar to KEIL which I learnt as a part of my curriculum. Directives are instructions used by the compiler to understand what to do with the code. You can use Code Composer Studio if you want. Note that for IAR the assembly language file is saved with .s43 extension unlike the usual .asm .

First Assembly Language program

;Name             : Manpreet Singh Minhas
;Date               : 23 Nov 2013
;Website         : https://learningmsp430.wordpress.com/
;Software        : IAR Embedded Workbench
\#include “msp430g2553.h”                                                            ;Include the header file
ORG 0FFFEh                                                                                     ;Reset vector address
DW MAIN
ORG 0C000h                                                                                     ;Flash memory starting address
MAIN:                    mov.w #WDTPW|WDTHOLD,&WDTCTL       ;Stop the watchdog timer
mov.b #BIT0|BIT6,&P1DIR                                ;Make P1.0 and P1.6 as output
mov.b #BIT0,&P1OUT                                        ;Make LED1 ON
REPEAT:             mov.w #0FFFFh,R7                                             ;Put value in counter for delay
UP:                       dec.w R7                                                               ;Decrement counter till 0
jnz UP                                                                    ;Repeat this 0FFFFh times
xor.b #BIT0|BIT6,&P1OUT                                 ;Toggle the LED1 and LED2
jmp REPEAT                                                        ;Infinite loop
END

Let’s understand what we have done in this program. I’ve written the hello world program again. This is LED blinking program in assembly language. I shall explain the basic directives now.

ORG

This directive tells the compiler that where it has to write the opcodes in memory. So ORG 0FFFFh tells the compiler that the next statement’s hex equivalent has to be stored in memory location 0FFFFh.

DW

This directive tells the compiler that we have defined a word and it is stored in memory which is given by ORG directive. So the code ORG 0FFFEh then DW 0C000h will store the word 0C000h in 0FFFEh memory location.

END

This directive tells the compiler that the code is over, whatever is written beyond this point is not relevant to the program.

Concept of LABELS

MAIN,REPEAT and UP are labels in the above program. Now these labels are nothing but the memory location of the statement where they are written. So MAIN is nothing but 0C000h, thus we could have very well written DW 0C000h instead of MAIN.

Numbers in assembly language

While writing numbers in assembly language you need to write 0 before a hexadecimal number starting with an alphabet. This is to make the compiler understand that the code is a number and not a label. I’ve developed a practice to write 0 before every number. But you can follow whatever you like. Thus FFFFh is not valid but 0FFFFh is valid. (and the 0 is zero and not the alphabet O)

Now we understand the code. The first two lines writes the starting address of the main program code at the reset vector address i.e. 0FFFEh. As I had discussed earlier that when the controller is reset the data at the reset vector memory location is loaded into the program counter, thus we write our main program starting address there. Then we write the main program in flash memory. Now for msp40g2553 flash starts at 0C000h, so I’ve written the main code there. You’ll have to change these addresses as per the data sheet of the controller you are using.

Then I’ve used simple mov instructions to initialize the watchdog timer and the ports. I’m giving this program for starting your assembly language coding. So you may want to use the timer section to give delay. I’ll write a code for that as well. Also if you have any doubts at all feel free to ask me. I’ll definitely share what I know.

Hope this post was informative. Thank you for reading this.

P.S:

Here are two more assembly language programs just for practice.

;Name             : Manpreet Singh Minhas
;Date                : 23 Nov 2013
;Website         : https://learningmsp430.wordpress.com/
;Software        : IAR Embedded Workbench
;This is a program to generate the well known Fibonacci series.
#include “msp430g2553.h”
ORG 0FFFEH                       ;RESET VECTOR
DW MAIN                        ; GOTO MAIN LABEL
ORG 0C000H
MAIN:        MOV.W #WDTPW|WDTHOLD,&WDTCTL    ; STOP THE WATCHDOG TIMER
MOV.W #03FFH,SP            ; INITIALIZE STACK POINTER
MOV.W #00H,R4            ; FIRST ELEMENT
MOV.W #01H,R5            ; SECOND ELEMENT
MOV.W #00H,R6            ; AUXILLARY POINTER
MOV.W #010D,R7            ; NUMBER OF ELEMENTS
MOV.W R4,0x200(R6)
INCD.W R6
MOV.W R5,0x200(R6)
UP:        INCD.W R6
CLRC
DADD.W R4,R5
MOV.W R5,0x200(R6)
DECD.W  R6
MOV.W 0x200(R6),R4
INCD.W R6
DEC.W R7
JNZ UP
JMP $
END

;Name             : Manpreet Singh Minhas
;Date                 : 23 Nov 2013
;Website        : https://learningmsp430.wordpress.com/
;Software        : IAR Embedded Workbench
;This program transfers data from one memory location to other.
#include “msp430g2553.h”
ORG 0FFFEH  ; This is the reset vector address.
DW MAIN
ORG 0C000H ; Starting of flash memory.
MAIN:   mov.w #WDTPW|WDTHOLD,WDTCTL ; stop the watchdog timer
mov.w #03ffh,SP            ; initialize the stack pointer to top of ram
mov.w #0200h,r4            ; initialize the destination reference pointer
mov.w #0e000h,r5           ; initialize the source pointer
mov.b #010d,r6
up:     mov.w @r5+,0(r4)
incd.w r4
dec.w r6
jnz up
jmp $
ORG 0E000H
DB “MANPREETSINGHMINHAS”
END

For both these you can use the IAR emulator. I’ll cover how to use that later.

Hardware Interrupts it is!!!!!! ;)

interrrupt_logoIn this project we’ll be dealing with the hardware interrupt functionality of msp430. An interrupt is a specific signal that the processor identifies. Interrupt triggering can be either rising edge or falling edge or level triggered. falling-edge rising-edge

So whenever an interrupt of desired from occurs, it triggers the processor. Just a brief idea about Interrupts. Whenever an interrupt occurs the processor completes the task at hand, then context saving takes place and then context switching. Context saving is nothing but the processor stores the current content of the program counter to the stack. Stack is like a scratch pad where you write data whenever you require. Only difference is that this stack cleans up once you pop the data or take the data back. The vector address (which is nothing but an address that is a hex number) is loaded into the program counter. Each interrupt has a unique vector address for interrupt subroutine(ISR). Now this ISR is executed and once it executes the return instruction(has to be written by programmer in ISR) it goes back into the main program. This is done by taking back the address of the program counter that was stored onto the stack.

Now we’ll talk about msp430 and hardware interrupts. In msp430 you can configure each and every pin of all ports as interrupt enabled. So one can have various interrupts and various parts in ISR based on that. For port 0 there is one vector address and for port 1 and so on. So irrespective of which pin the interrupt occurs the control will go to the same ISR. In order to distinguish which port pin has triggered the processor there is a flag register in msp430 called PxIFG which is port x interrupt flag. Each bit corresponds to a the port pin of that register. So whenever there is an interrupt the m pin of port x then the mth bit of PxIFG goes high. We have to clear that bit ourselves, compiler won’t do that for us.

Now coming back to the project, in this project we’ll write the led pattern code in the main program so that there are some patterns in normal state. We’ll be using 6 push buttons to give interrupts. And we have programmed the controller in such a way that led corresponding to the port pin that gives interrupt will glow. So if P2.3 gives the interrupt I’ve programmed this such that P1.3 led will glow(same port pin number just for tracking purpose.)

Note you can easily vary this program to write a code to display the pin number on a 7- segment display. All you need is a BCD to seven segment decoder and a seven segment display. I got this idea after returning home when I had purchased the push buttons. I really didn’t want to go to the shop so I stuck to this led concept.

Hope you gained some knowledge about the wonderful world of interrupts. This is a really important part of any controller and how a programmer writes code for this is also important. Thank you for reading this blog. Please do come again for some new concepts or entries.

P.S: For a couple of weeks I’ll be using grace for initializing all peripherals. Later I’ll be covering assembly language.

111020131216

The above circuit diagram shows the connections.

Tutorial:

Project :