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 .


16 comments:

Anonymous said...

Thank you thank you thank you.
I searched for hours and hours to understand what is 0x00, 0bxxxxx and all that stuff.

PIC said...

I want to learn programming PIC using C with u

Anonymous said...

Greetings woman. I am only I start to study c programs
arrays c program.
I have visited a site http://program.v3host.be/?cat=204.
Much to our regret I have very little found out about it.
You can probably tell to me a good site about c program library.
I will be very glad.
Thansk

Anonymous said...

Hello woman. I am only I start to study c program file.
I have visited a site http://program.v3host.be/?cat=204.
Much to our regret I have very little found out about it.
You can probably tell to me a good site about c language program.
I will be very glad.
Thansk

Anonymous said...

Can someone explain me what is the difference between TRISB and PORTB?Why instead of using portb we are not using trisb?

Anonymous said...

Tiffany's is one of the most well known [U]tiffany jewellery uk[/U] brand names and design houses in the jewelry [B]tiffany co[/B] business. The quality of service, customer satisfaction and the fine designs offered by [U][B]tiffany bracelets[/U][/B] the company are one of the best in their times. The company is primarily [B][URL=http://www.tiffanysfree.com]tiffany london[/URL][/B] famous for its superb quality and artisanship.

However, as this amount of beauty and quality comes at [B]tiffany co[/B] a price, there are many jewelry lovers who are just not able to pay [U][B]tiffany[/U][/B] the high price and look for suitable replicas instead. This is the [U][B]buyers of tiffany jewelry sets[/U][/B] reason there are special [U]tiffany engagement rings][/U] designers that now offer beautifully made Replica Tiffany Jewelry and that too, at a much [B][URL=http://www.tiffanysfree.com]tiffany necklaces[/URL][/B] lower price.

The Tiffany's is one [B]fake tiffany jewelry[/B] of those brands that actually [B]buyers of tiffany jewelry sets[/B] mark the onset of a classic style and design on its own. Some of the designs of this [B]tiffany[/B] brand name have become so common and popular, that they [B]tiffany jewelry store[/B] are bound to be copied by artisans all over. These replicas are created either by [U][B]tiffany london[/U][/B] using a mass production technique or by an individual jewelry designer who is striving to [U]tiffany co][/U] make a name for his services and capitalize on the original designs. These [U]cheap tiffany and co jewelry[/U] kinds of procedures have made the replica business [B]pandora bracelet[/B] a very lucrative one, with scores of skilled designers [U]tiffany engagement rings][/U] paying close attention to these designs and producing them effortlessly [U][B]tiffany jewelry store[/U][/B] with same style and appeal.

These designers also have full knowledge of the materials, metals [U]pandora bangles[/U] and stones to be used to imitate the look of the real jewelry. It is a very common [B]pandora bangles[/B] sight to find paste gems with [U][B]fake tiffany jewelry[/U][/B] plastic bezels or pendant pieces. Often, one [U]fake tiffany jewelry[/U] also finds jewelry that uses a [U][B]pandora bracelets[/U][/B] quartz crystal of a diamond or even silver [U][B]tiffany[/U][/B] instead of platinum. Such [B]tiffany london[/B] techniques are extremely useful and come handy when preparing [U]buyers of tiffany jewelry sets[/U] replica Tiffany jewelry.

Anonymous said...

Jewelry is women's best friend. It is surely a [B][URL=http://www.tiffanysfree.com]tiffany charms[/URL][/B] long lasting gift to your loved ones and enhances ones beauty [U]replica tiffany jewelry[/U] with its attention getting charm. So, while buying such beautiful jewelry for [B]fake tiffany jewelry[/B] your precious ones, you need to make [B]baltimore tiffany jewelry[/B] sure that you get them from trust worthy places. Pandora bracelets, Links of London, Tiffany jewelry [U][B]buyers of tiffany jewelry sets[/U][/B] offer you top edge designs and high quality jewelry at affordable prices. They are known for their [U][B]cheap tiffany and co jewelry[/U][/B] elegant yet bright designs that could match any occasion. Jewelry these brands offer has a specific [B][URL=http://www.etiffanystore.com]tiffany necklaces[/URL][/B] meaning that expresses many feelings.

Silver bangles and bracelets of Tiffany jewelry [B][URL=http://www.etiffanystore.com]tiffany necklaces[/URL][/B] are known for their delicate knots and symbols which express unity and harmony. Their heart shaped lockets which express love make [U][B]best website to buy discounted tiffany jewelry[/U][/B] their partner feels that they are precious [B]cheap tiffany and co jewelry[/B] and wonderful.

These jewelries are quite famous [B]best website to buy discounted tiffany jewelry[/B] among couples who say that these unique and simple [U]tiffany company jewelry[/U] designs of Tiffany jewelry are long-lasting and incomparable. Some [U][B]3tiffany & co jewellery[/U][/B] designs in Tiffany's jewelry date back to decades ago, which make them [B][URL=http://www.tiffanysfree.com]tiffany co[/URL][/B] a perfect gift as a heirloom, so that it could be passed from one generation to another, this explains the timeless beauty [U]tiffany company jewelry[/U] of the jewelry and still be in fashion even after many generations

Pandora bracelets are exquisite and its [U][B]knockoff tiffany jewelry[/U][/B] lovely charm casts a spell on every one who sees it. It is irresistible to stop thinking about [B]authentic tiffany jewelry[/B] the wonderful piece of art once you have seen it. Jewelry of the line of Pandora bracelets are designed with [U]best website to buy discounted tiffany jewelry[/U] great care keeping in mind of tastes of millions of people, not to mention about [U][B]knockoff tiffany jewelry[/U][/B] the current fashion too. They would make a perfect choice for [U]tiffany jewellery uk[/U] all those modern women to whom [U][B]tiffany jewellery uk[/U][/B] fashion is everything.

Ganesh Mogu said...

Thanks man ur tutorial is fantastic...

Ganesh Mogu said...
This comment has been removed by the author.
Unknown said...

thank you very much..pls add some more tutorials..

Unknown said...

thank you very much..pls add some more tutorials..

nass_sare said...

This is wonderful. Thank you for the entry.

I wonder if I want to use 10 bit conversion. which port to use and how to program them.

Smh said...

I read the PIC16F877 digital port on hyper terminal but it gives asii value instead of actual value of the port. How I convert ascii to integer in mikroC?

Arun Vijay said...

http://www.thinkage.ca/english/gcos/expl/c/lib/atoi.html

You could just get it from Google !

Unknown said...

thanks

manish said...

Nice blog thanks for sharing this tutorials. For more robotics tutorials and learning visit Robotics tutorials