|
|
View previous topic :: View next topic |
Author |
Message |
th3gr8
Joined: 16 Jul 2013 Posts: 12 Location: Pakistan
|
Help regarding ADC |
Posted: Tue Jul 16, 2013 5:37 am |
|
|
Hello
I am new to PIC programming and i am using CCS compiler.
I am now working with PIC's built-in ADCs.
I am using PIC 16F877A and i am little confused about somethings.
I read that read_adc() function gives Digital output of ADC which is in the form of step (for 8-bit ADC, there are 256 steps), but when I view the ouput of read_adc() on LCD it gives value like 994, 101, -1014,-888 etc etc, what are these values?? and what they represents?
Coding is:
Code: |
#include <16F877A.h>
#device adc=8
#Fuses HS, NOWDT, NOLVP, PROTECT
#use delay (clock=12Mhz)
#include <lcd.c>
void main()
{
float a;
int b;
set_tris_a(0b000001);
setup_adc_ports(all_analog);
setup_adc(adc_clock_internal);
set_adc_channel(0);
delay_ms(50);
lcd_init();
delay_ms(20);
while(1)
{
b=read_adc();
delay_ms(100);
a=b*(0.01953125);
lcd_gotoxy(1,1);
printf(lcd_putc,"%d",b);
lcd_gotoxy(1,2);
printf(lcd_putc,"%f",a);
}
} |
Secondly, i want to ask that as we know there are other ICs used as ADC like an Op-amp 741, can i use my PIC microcontroller in the same way like 741? I mean can i configure it in a way that when the applied voltage is below Vref, it gives VEE and when it is greater than Vref, it gives VCC ?? If yes, then how??
Regards. _________________ A learner |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19541
|
|
Posted: Tue Jul 16, 2013 5:58 am |
|
|
First, learn to use the code buttons.
Second, don't use 'PROTECT', when developing code. If this is selected, then to even just change one byte in the chip, a full erase has to be performed. The ROM has a limited write life, and you are potentially 'wasting' these in places that don't need to be reprogrammed each time.
Protect should only ever be turned on, when code is complete and being shipped (if then).
Third, if you are displaying number like -1014, you have the output format being used for the print, set incorrectly. An 'int' can only hold values from 0 to 255....
Code: |
while(TRUE)
{
b=read_adc();
lcd_gotoxy(1,1);
printf(lcd_putc,"%u ",b);
delay_ms(250);
}
|
Now, note two things - %u, is the format to show an unsigned value. You were trying to print the unsigned value as signed.... Then second, note the spaces. Problem is you are never clearing the screen, so if it prints (say) -127, and then 23 on the next loop, the display would end up showing 2327, as one value overwrites the other.
Fourth, a 741, is not an ADC, and can never make an ADC (without a lot of other stuff around it). You seem possibly to be talking about using it as a comparator. If so, then this is 'possible', but the 16F877A, has a comparator module in it. Use this. |
|
|
th3gr8
Joined: 16 Jul 2013 Posts: 12 Location: Pakistan
|
Sweet !! |
Posted: Tue Jul 16, 2013 6:12 am |
|
|
Thanks alot Ttelmah.
As you mentioned, i was making mistakes at two places:
1) The screen was not getting cleared, due to which the text was getting over-written.
2) The format specifier i used (%d) was incorrect. By using %u, it fine now.
Thanks _________________ A learner |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|