< << 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 .
Friday, February 22, 2008
PIC C Programming Video Tutorial (ADC)
Thursday, February 21, 2008
Getting Started :PIC 16f877A Analog to Digital conversion tutorial
What is Analog to digital conversion ?
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>>>>>>
Subscribe to:
Comments (Atom)