Thursday, August 15, 2013

Controlling servo motor using MSP430 Launchpad 2 ( introduction to timer )

This is the second post about controlling a servo motor with MSP430. In the last post I explained what is PWM and how to use it to control a servo motor.

Now in this post, I'm going to explain how a timer works in MSP430 and how you can use it for generating PWM.

First of all, between all the MCUs that I have worked with, MSP430 has the easiest setting to activate a timer. To generate PWM, the only thing you need to do is set two number for total time and the duty cycle.

 basically the code will be like this:


#include <msp430.h>


/* using Timer A and generate PWM

* code by godman

*/

unsigned int DutyCycle=1650;

void main(void) {

WDTCTL = 0X5A80; // or we can use WDTCTL = WDTPW + WDTHOL


P1DIR = BIT6; //Set P1.6 Output
    
P1SEL = BIT6;   //Set Peripheral Function which is TA1



// set timer value

TACTL = 0x0210; // TA register set, TASSEL_2+ID_0+MC_1+TACLR_0+TAIE_0+TAIFG_0

TACCR0 = 22000; // MCU_FREQ/SERVO_FREQ=1100000/50=22000
TACCTL1 = OUTMOD_6;  // set the timer to generating PWM
TACCR1 = DutyCycle; // set duty cycle of PWM 0=550 180=2750

while(1)
                {

                 DutyCycle = 1250;     // it set the servo to 90 degree ( my servo works between 0 to 180)
                 }

}


 I will add explanation later, sorry for now

don't forget to connect all ground together, if you want to try this code on MSP development board or LunchPad.

Saturday, July 20, 2013

Controlling servo motor using MSP430 Launchpad 1 ( introduction to PWM )

Servos are widely use in many application and knowing how to control them is an essential knowledge. Servo motors are simple to use and relatively easy to control.

Servo motors are controlled by PWM (Pulse Width Module). most of Servo motors works on 50Hz in frequency. The time duration for PWM can be calculated as below:

Time=1/Frequency
T=1/50
T=20ms
 
 
So to control a servo motor we need to have a 50Hz or 20ms PWM.
After setting the duration we need to set the necessary duty cycle of PWM. This duty cycle will control the position of the servo motor.
 
For example the servo that I use works in 50Hz and duty cycle between 0.5ms to 1.5ms. lets see what is that means.
 
For example we want to have a PWM with 50Hz duration and 50% duty cycle. That means we need signal duration of 20ms which is 10ms on and 10ms off.
 
 
Now my servo motor need 50Hz  and a range between 0.5ms for zero degrees and 1.5ms for 180 degrees.
 

Any other position that we want to reach has a linear relation to these numbers. if you put your desired position into this equation you will get the duty cycle in ms;
 
DutyCycle=((1/180)*degree)+0.5
 
in the next post I will explain how to use a timer and generate PWM in MSP430
 

 
 





Sunday, June 9, 2013

Which MCU and Which compiler?

Hi again.

Lets start with a simple program with PIC MCU.

I choose PIC16F690 because  it's very use full and have lots of functionality like TIMER, PWM, ADC and so on. It also has 18 pin General Purpose Input/Output (GPIO).

What is GPIO and what is the difference?

When a MCU has GPIO, it means each pin on the mcu has different configuration, as you can see in picture blow:


figure from PIC16F690 datasheet


for example pin 14 in this chip has 3 functionality, it is PIN3 of POTR C , also it's Analog PIN6  moreover it's PWM PIN1D.

As you already know I don't want to go to detailes. Let's see some real action ;)

For working with PIC MCU you need to download a programing software named MPLAB.

It's free and easy to download from this link:

link to download MPLABX IDE


Download latest version.

Download latest version. With this software you can program all Microchip products with assembly language. But, it’s hard to understand assembly language for beginners.

 
It's easier to use a high level programming language like C/C++. Therefore, we must use a C/C++ compiler such as hi-tech C or XC8/XC16/XC32 compiler.

You can download hi-tech C from here.
 


Link to download CX compiler is here. This link is same as MPLAB IDE at the end of the page or at the left side you can see the links for direct download.

Both of these software have free version both during installation don't forget to choose free version.

Now we have enough tools to start our first code in the next post.




 









Friday, June 7, 2013

What is MCU?

What is MCU? And how does it works.

  MCU or Micro Controller Unit is the brain of any embedded systems.

  There are many MCUs in the market and all of them clam that their MCU is the best, but really it depends on application.

 Some of the well known MCUs are:


 Microchip PIC family like PIC16 and PIC18 series.

Texas Instruments MPS430
 




When you chose a MCU for your project, you need to consider what you want to do. There is no multipurpose MCU also, there is no good and bad MCU. It all depends on applications.
 
 All MCUs contain three main part.
  1. ALU
  2. Memory
  3. I/O
These are the essential part that any MCU has.
 
I don't want to continue on this technical issue. It's really boring to know about all these staff :D . there fore I leave it and in the next post will tell you how to start your very first code and simulate it in a simulator.
 
 

 
 
 
 
 
 
 

 

Tuesday, June 4, 2013

Greeting

Hey every body.

I wish you well.

First,let me introduce my self. I'm Rahim and I'm a mechatronic student. I like robots and machine learning systems.

I start this weblog because, during my study I find so many difficulties to find out simple solutions for my simple questions. When a problem come out every time I need to search and register for some forums that I never use them again :D.

So I decided to make it easy for other people who have the same problem.

I'm going to post all my project and other ideas about my field of study with enough explanation on this weblog.

So anyone can visit, and if it's useful for them, just use it.

don't forget to mention my name :)

farewell!

Rahim