Friday, February 22, 2008

PIC C Programming Video Tutorial (ADC)

< << PREVIOUS TUTORIAL


Now we shall Jump into PIC ADC Programming using C .As I told you in the previous post we are gonna use MIKROC and The simulator that we will be using is OSHONSOFT PIC Simulator(download link ) .

Watch this video tutorial to get started with PIC16F877A programming and set up using the MikroC compiler and Oshonsoft PIC Simulator .



PIC C programming is just like normal C programming that you do for you computer .The differences being you have to declare the ports set the registers and there is no printf and scanf of course .

Here is the small program that I teach you in the video .It simply reads the value from the 2 nd ADC channel and outputs it to the PORTB .And also I've configured the ADC ports as 8 bits and all analog inputs .If you not sure about the register settings read my previous tutorial .
So look at the table that I've given in my previous tutorial and configure ADCON1 .

unsigned short adc_val ;
void main ()
{
ADCON1 = 0x00;
TRISA = 0xff ;
TRISB = 0x00 ;
while (1)
{
adc_val = adc_read(2) ;
portb =adc_val ;
}
}

After setting ADCON1 .The TRIS registers have to be set .The TRIS registers configures the port as either input or output .Just suffix the TRIS register with the PORT name .
TRISA is the register for PORTA ,TRISB is the register for PORTB .Here we are going to use PORTA so I've to set the TRISA register as inputs .So how do we do this ?
I am going to use hexadecimal to configure the registers and ports .You can also use binary to configure the ports which is good and easy for beginners .
So to use hexadecimal you've to add the suffix 0x (0xFF) and to use the binary you've to use the suffix ob (0b11111111) . Looking at my program I've used

TRISA = 0xFF which can also be written as
TRISA = 0b11111111 in biinary .

The next thing is setting the port as input and outputs .Let us take PORTB as example .
TRISB = 0x00 ;
TRISB = 0b00000000 ; configures the port b as outputs .

So I guess you must have learnt how to set a port as input or output by now .
TRISX = 0x00 makes the port as inputs and TRISX = 0xff makes the port as inputs .
(Substitute the port name in the place of X ) A combination of input and output can also be done .
for ex

TRISB = 0xF0
TRISB=0x11110000

sets half of the upper ports as input and the other half as outputs .
After setting the ports we shall get into the main program .
You must have noticed an unsatisfied while loop .Yes there must be at least one unsatisfied loop inside your program ,because your microcontroller doesn't have a operating system there has to be atleast one unsatisfied loop inside the program to make it run continuously.

MikroC has lots of inbuilt libraries . For reading the analog inputs
adc_read(unsigned int channel number ) is used .

Getting back to my program what I do is I get the input from the channel and output it to the PORT B .

So I hope this tutorial has helped you .
Please leave your comments if you have any suggestions .


Thursday, February 21, 2008

Getting Started :PIC 16f877A Analog to Digital conversion tutorial

What is Analog to digital conversion ?

My first post in the series of "Mastering PIC microcontroller Programming " is gonna be about ADC(Analog to Digital Converter).
Well what is A2D first of all ?
We all have done analog to digital conversion (A2D) .You dont beleive me ? .Remember the 5th grade science experiment of finding the boiling point of water .We take down the readings of the thermometer every 30 seconds until it reaches a contant .Although the temperature varies continously we only note the reading between specified time interval ie You are sampling the continuous data into a sampled data and reconstructing the continuous curve based on the data we have .So we all have done the basis of A2D in our lives .

PIC Microcontrollers have 10 BIT ADC's .That means the range is from 0-1023 or 0-(2^10 - 1).If we set the reference voltage at 5 volts then 0 - 5 volts can be represented as 0 - 1023 in the digital format .ie 5 v would represent 1023 2.5 v would represent 512(rounded).That is the microcontroller would understand 2.5 v as 50% of 2.5 .
The formula to calculate the digital value is

x= (Vin/Vfullscale )(2^n-1)

following this formula

x=(2.5/5)*1023 =511.5 (512 approx)

Then the next question arises about the a2d resolution .To put it simply what is the minimum voltage that the microcontroller would be sensitive .The resolution of 10 bit microcontroller with 5 v as reference is

5/1023 =4.88 mv .

The microcontroller would measure a minimum change of 4.8 mv below which it will be insensitive .
The general formula for resolution would be

V res = V Fullscale /2^n-1

Vfullscake = What you set as reference
n= No of bits (10 bit in PIC16F877A)

I assume that you might have understood the basics of A2D and move on to the next step .
The next tutorial is gonna be about the A2D Registers in PIC16F877A . NEXT TUTORIAL>>>>>>

PIC Analog to digital conversion registers

<<<Previous tutorial
Next Tutorial >>>
There are totally four registers for A2d conversion in PIC16f877 and PIC16f877A .They are the following .
(NOTE : Almost all the PIC series of IC's have this same register configuration .So knowing this is enough to program all the PIC series IC's )
















































registers for analogue inputs.
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
ADRESHA2D Result Register - High Byte
ADRESLA2D Result Register - Low Byte
ADCON0ADCS1ADCS0CHS2CHS1CHS0GO/DONE-ADON
ADCON1ADFM---PCFG3PCFG2PCFG1PCFG0




ADCON0 Configuration
Since PIC has 10 bit A2D and its a 8 bit microcontroller it needs an extra register to store the extra 2 bits resulting in the A2D conversion .Thats the job the first two registers ADRESH and ADRESL name itself is self explanatory .
ADRESH:Stores the higher 8 bits .
ADRESL:Stores the lower 8 bits .
So now we are done with the first two registers .

The next register that we are gonna see in PIC16F877A is ADCONO .
The first two bits ADCS1 and ADCS0 is required to select the A2D clock .The table for clock selection is given below .If I am using 5 MHZ I will select 01 thats fosc/8 .



































ADCS1ADCS0A/D Conversion Clock
Select bits.
Max. Clock Freq.
00Fosc/21.25MHz
01Fosc/85MHz
10FOsc/3220MHz
11Frc (Internal A2D RC Osc.)Typ. 4uS




The next set of bits are channel select .YOu can select the appropriate channels by selecting the appropriate bits .So now I am using 5 MHZ and selecting RA2 for example , now bit selection will be 01010xxx .The bits that are named as X are still to be selected .

The next bit is GO/DONE .Setting this bit high starts A2D conversion and it is automatically cleared when it is complete .So monitoring the status of this bit can tell us about the status of Analog to digital conversion .So we have to set it high to start conversion .So now our bit configuration will be 010101xx .

If you look at the first table you will see bit1 is useless .So put a zero there .So now our bit configuration becomes 0101010x .
The last bit is ADON .When the bit is set it turns on the A2D when cleared it turns off .So we are setting 1 there .Now it becomes
01010101 .This is the bit configuration to select 5 MHZ clock channel AN2 .The next three bits are useless .




































































CHS2CHS1CHS0ChannelPin
000Channel0RA0/AN0
001Channel1RA1/AN1
010Channel2RA2/AN2
011Channel3RA3/AN3
100Channel4RA5/AN4
101Channel5RE0/AN5
110Channel6RE1/AN6
111Channel7RE2/AN7




ADCON1 Configuration
The next channel is ADCON1 . The bit7 ADFM(Analog to digital conversion format ) is used to select the format .Setting this bit high makes it 10 bit converter .And clearing it makes it 8 bit ie the result only appears in ADRESH and ADRESL is ignored .
Now we are entering thre most complicated part of the register selection .Please look at the following table to select the configuration you need .

Visit WINPIC PROG to learn more in detail about the register selection .


































































































































































































































PCFG3:

PCFG0
AN7

RE2
AN6

RE1
AN5

RE0
AN4

RA5
AN3

RA3
AN2

RA2
AN1

RA1
AN0

RA0
Vref+Vref-
0000AAAAAAAAVddVss
0001AAAAVref+AAARA3Vss
0010DDDAAAAAVddVss
0011DDDAVref+AAARA3Vss
0100DDDDADAAVddVss
0101DDDDVref+DAARA3Vss
0110DDDDDDDDVddVss
0111DDDDDDDDVddVss
1000AAAAVref+Vref-AARA3RA2
1001DDAAAAAAVddVss
1010DDAAVref+AAARA3Vss
1011DDAAVref+Vref-AARA3RA2
1100DDDAVref+Vref-AARA3RA2
1101DDDDVref+Vref-AARA3RA2
1110DDDDDDDAVddVss
1111DDDDVref+Vref-DARA3RA2




in the next tutorial we are going to kick start with PIC16F877A programming in C .I am gonna use mikroc compiler .

Previous

Tuesday, February 19, 2008

Mastering PIC16f877A programming .

I bunked college today to do a lot of work that was left to do but ended up seeing cricket match instead .But I did a small portion of the shopping cart for the site that I am building .And my 2nd review is also nearing .But I'vent finished any considerable amount of work except for fabricating a few parts for my robot .

So what am I gonna do now ?.
I am going to dedicate this week for "Mastering PIC16F877A Programming " .
Although I've programmed a lot of robots using C .I always had difficulty with programming .And moreover I always used simple techniques .In fact I've never used TIMERS and Interrupts which has many functionalities.So I am planning to learn all the pheriperals of the PIC like

TIMERS
INTERRUPTS
USART
CCP Modules .


And I also dont know assembly language which I consider as a huge drawback .Although PIC is based on RISC architecture and has only 35 instructions I never managed(not even tried)to learn it .And I am gonna post a new tutorial every day about programming the PIC .So lets see what happens this week .

* Tutorial 1 Programing ADC's
* Tutorial 1 Programming Interrupts

Monday, February 18, 2008

How to Post HTML Tables in Blogger without the big gap ?


Have you ever tried posting tables in blogger ?

If you have done so ,you will see a huge gap in between the table and the text above it .Heres an example .














































NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
ADRESHA2D Result Register - High Byte
ADRESLA2D Result Register - Low Byte
ADCON0ADCS1ADCS0CHS2CHS1CHS0GO/DONE-ADON
ADCON1ADFM---PCFG3PCFG2PCFG1PCFG0

So how do you get rid of the space between ?
First let me tell you why there is a huge gap when you post a table on blogger.Blogger inserts a line break tag tag every time you hit enter that is why you see a huge gap .
To get rid of this you will have to insert a small piece of CSS code which will solve the problem .

<style type="text/css">.nobrtable br { display: none }</style>


Paste this on the top of your post and use the div tag to reference it


<div class="nobrtable">

Post your table here -------------------

</div>


Heres the table after inserting the code .














































registers for analogue inputs.
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
ADRESHA2D Result Register - High Byte
ADRESLA2D Result Register - Low Byte
ADCON0ADCS1ADCS0CHS2CHS1CHS0GO/DONE-ADON
ADCON1ADFM---PCFG3PCFG2PCFG1PCFG0





YOu can see the table is correctly formatted .

Thursday, February 14, 2008

PCB designing Video Tutorial and Ebooks

Easy to use PCB designing Tutorial

PCB designing is no more a hard core designer’s job , even cons like us can design the PCB using this brilliant software “PCB WIZARD”. You need not be a wizard when you Have PCB wizard in hand.

Unlike most of the PCB designing software’s PCB WIZARD is very user friendly.
(I am telling this keeping in mind that I‘ve tried many other designing software’s).
.The coolest thing that I found in this software is you don’t have to design the PCB at all. It makes PCB for you. How cool is that?

You’ve to just draw the circuit (schematic diagram)



then select the option convert
Design to Printed circuit board



The software makes the PCB for you .This is done by auto routing .They even provide manual design mode for advanced users.


Autorouting in action watch the video .



No more you have to be a master knowing each and every spec of the electronic components the pins diameter the size of drill bit .Nothing nothing this software does it for you.


COMPONENT LIBRARY

But there are a few draw backs as well .When you choose the automatic conversion option It lets you down on the aesthetics part .But I don’t mind as far as it is done at the hobby level and if I can design PCB so easily why care about aesthetics.
And the component library is also not adequate .Especially L298 is missing .It also lacks SMD library.

They even have tutorials about etching PCB‘s .And more importantly they don’t have restriction on the size of the PCB.

Download it from here
If you are looking for Ebooks about designing here is the link .


Ebook-Tutorial-1
Ebook-Tutorial-2
Ebook-Tutorial-3
Ebook-Tutorial-4


Final designed PCB

You can select different view modes like prototype,solderside layout ,component side layout and silk screen layout .If you want to design a PCB to replace all your annoying soldered circuits boards which gives you head ache "PCB WIZARD " is the software for you .

Wednesday, February 13, 2008

How to remove the blogger bar on the top ?

REMOVE BLOGGER BAR ON THE TOP
I personally don't like the blogger bar at the top of blogger templates .Its good as well as bad .Its good because it provides a search box to search my blog .And bad because it gives an option to flag my blog for objectionable content which I guess you wont like if you blog about some "objectionable content" ;) .But Moreover who needs their search bar when you get cool google search box to search your site .

So how do you remove it ? .Heres a small piece of CSS code which will do the job .Go to
Layout
Edit HTML
Before doing any changes always copy the html code and save it in a text file in your PC .So that
if you mess up something you can always copy paste the code and get back your original template .Or you can download the templete in XML format as a backup .
Paste the following code inside the head tag inbetween the outer-wrapper and header-wrapper definitions

#b-navbar
{
height: 0px;
visibility: hidden;
display: none;}

#navbar-iframe
{
height: 0px;
visibility: hidden;
display: none;
}

<>

#outer-wrapper {
font: $bodyFont;
}
/* Header
----------------------------------------------- */
copy paste the above code exactly here
here goes the code
#b-navbar
{
height: 0px;
visibility: hidden;
display: none;}

#navbar-iframe
{
height: 0px;
visibility: hidden;
display: none;
}

#header-wrapper {
margin:0;
padding: 0;

< / head>
Preview to see if the bar has disappeared and then save the template and view your blog the blogger bar must have disappeared .

Tuesday, February 5, 2008

How to couple a robot's wheel with motor ?

COUPLING MOTORS AND WHEELS

When you have motors and wheels the question arises how do I couple them ?.As far as I've experienced they never fit together .So you have to make a coupler to couple both so that it can be removed and used later in any other robot and for easy dismantling .I've written the steps for fabricating it elaborately .You can use either ms(Mild steel ) or Brass for fabricating it .

STEPS OF FABRICATION

  • First measure the diameter of the motor shaft.
  • Then measure the internal diameter of the axis the wheel.
  • If both match then you can fix it directly.
  • Otherwise choose a rod that is at least 4 mm greater in diameter than the motor shaft.(so that you can use a miimum size of 2mm screw to secure it)
  • Step turn one side of the rod slightly larger than the internal diameter of the axis of wheel to give a tight fit.
  • Then on the other side drill a hole with a drill bit that is little larger than diameter of the motor shaft in the rod along its axis .This side is given a loose fit since motor can be reused .(Note this is a drill to insert the motor)
  • Drill until a depth that is little shorter than the height of the motor shaft.
  • Then grind the motor shaft with grinding machine to make a flat surface along the axis if your motors shaft is cylindrical. Some motors shaft will be like half cylindrical .This step is not needed if the motor is of this type.
  • Then drill a hole perpendicular to the axis of the coupling shaft matching the place where you ground the motors shaft, then tap the hole to form thread and use a bolt to tightly secure the coupling shaft with motor.
  • Before doing the above step don’t forget to fix the wheel.

Wheels fitted to the motors with the coupler .


MOTORS AND SPEED CALCULATION

MOTORS AND SPEED CALCULATION

When you start designing wheeled robots start from the motors .
Decide the type of motor you are going to use .

According to gear arrangement DC motors can be broadly classified into

1. Motors with Internal gear's
2. Notors with External gear's

For beginners I would recommend using motors with internal gear box motor ,since they are easy to use(they eliminate the whole process of designing and fabricating the gear box ) .Internal gear box are available from a range of anywhere between 5 to 5000 RPM .And their speed can be varied either by

1.Varying the supply voltage or
2.Pulse width modulation (PWM )


After you choose the motor you can calculate the speed of the robot to get a rough idea about how fast your robot will go .(Never forget to do this while designing a robot ,otherwise you will end with a robot that is either too slow or too fast) .
The speed calculation doesn't involve big mathematical formulas .
Measure the diameter of the wheel (in meters) that you are going to use .Then multiply it with PI(pi = 3.14159265) .This gives you the circumference of the wheel .So you get a rough idea about how far the robot moves on one rotation of the wheel .

Circumference = Diameter X PI

Most cheap motors availabe never work to the specifics .The best way to find the RPM is trial and error .Apply the required voltage(remember to connect the wheel before applying the voltage) and start a stop watch for (say) 10 seconds and see how many time it rotates and then multiply it with 6 .This gives u the approx speed of your motor in RPM .And also mark a position on the wheel to make it easier to track it visually .
After getting the RPM of your motor multiply it with the circumference of your wheel .This gives u the approx speed of your robot in meters per minute.

Speed of your robot = RPM of motor x Circumference of your wheel (meters/min)

Sunday, February 3, 2008

My Final Year Project

Hi Everyone very long time since I blogged ...
I guess I will blog regularly in blogger .

From now on I will post about my final year project .Which I am doing in CVRDE .Its a R & D center for Ministry of defence India .

My project title is
"AUTONOMOUS NAVIGATION OF A SURVEILLANCE ROBOT IN STRUCTURED ENVIRONMENT AND TELEOPERATION THROUGH INTERNET"

I am planning to build a tracked robot (click here for my tracked robot tutorial)which has basically two modes of operation .

a.Manual teleoperated mode through the internet .
b.Autonomous mapping mode .

Here is the basic block diagram of how the robot operates .



SCOPE OF WORK
Design and fabrication of tracked robot .
Setting up wireless communication between the remote computer and the robot
Dynamic mapping of the given area.
Obstacle avoidance using triangulation principle.
Setting up of the image acquisition device to relay back the images to the remote location.

This is what I am trying to accomplish .Hope I finish it with in time .

Starting off for SHAASTRA 2007

Its pretty interesting that I overcame my laziness for another post here .I promised to myself that I will post regularly (see my previous post) .I was also inspired my Ganesh my friend and also my school mate who has renewed his old blog (meet him here) to give it a new look .And I also grew a little jealous by the fact that he was getting about 30 hits a day :D .Inspired by all these factors I am back here again .

I am starting off for shaastra 2007 . I am planning to compete in all the three robotics event .
This time I am pairing up with Prassana my class mate and his cousin and his friend Siddarth for the mission mars event.

Mission Mars
We are planning to start building the robot for mission maPost Optionsrs event .The task of the robot is to autonomously navigate a surface similar to mars .We had long discussions regarding the design of the robot .
Finally we made the following conclusions about the design
1.Track drive for the robot .
Since this the best available simple method to navigate a rugged terrain and also we have the belts ,sprockets and boggie wheels for construction which they used for their staircase climber in shaastra 2006.

2.Distance measurement sensors
We planned to implement a real time distance measurement and comparison algorithm to navigate the terrain .Sharp IR sensors seemed to be a good choice .But calibrating them is
surely gonna be a challenge .As the ADC pin give a o/p ranging from - 0.3 to 0.3 volts .I have
to Google and learn a lot to do that .

3.Image processing
Since Siddarth had previously worked in Image Processing .We have also planned to implement
this with image processing method with a TMS32054x Processor .But I guess the problem here lies with connecting the camera to the chip .We have to figure out a way to do that .

Because I have CAT this week .Oh well please don't think CAT that I have mentioned as the common aptitude test for Gods sake ,I am not a MBA freak.This CAT is called as Continuous Assessment Test the great method my college uses to find out how a student
is performing and award him marks accordingly which is accompanied with partiality and grudge . Well I guess I am deviating off topic .Getting back ......I wont be working on it until Saturday .

wish me good luck .I want to win this event .

Some things that I want to remind myself to do it this week
Finish eeemaniacs .(The site that I am building for my class mates.)
Get the 3.2 CC engine running .For the hovercraft event.
And study something for CAT exams .