View previous topic :: View next topic |
Author |
Message |
thanhandaithanh
Joined: 30 Oct 2017 Posts: 6
|
ADC with PIC18f4580 |
Posted: Mon Oct 30, 2017 9:27 pm |
|
|
I have a problem with read_adc(). I cannot read adc value from AN0 of this Pic
This is my code:
Code: |
#include <18f458.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device adc=8
#use delay(clock=20000000)
int8 data=0;
void main(void)
{
set_tris_a(0xff);
set_tris_b(0x00);
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
delay_ms(100);
while(1){
data = read_adc();
delay_ms(100);
OUTPUT_B(data);
delay_ms(1000);
}
} |
Last edited by thanhandaithanh on Tue Oct 31, 2017 8:14 am; edited 2 times in total |
|
|
srikrishna
Joined: 06 Sep 2017 Posts: 82
|
Re: ADC with PIC18f4580 |
Posted: Mon Oct 30, 2017 10:43 pm |
|
|
modify according to your settings
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 30, 2017 10:46 pm |
|
|
What PIC do you really have ? Your title says 18F4580, but your program
says 18F458. Those are different PICs.
What is your CCS compiler version ? |
|
|
thanhandaithanh
Joined: 30 Oct 2017 Posts: 6
|
Re: ADC with PIC18f4580 |
Posted: Mon Oct 30, 2017 10:47 pm |
|
|
srikrishna wrote: |
modify according to your settings
|
I don't know what is problem in my code ? |
|
|
thanhandaithanh
Joined: 30 Oct 2017 Posts: 6
|
|
Posted: Mon Oct 30, 2017 10:48 pm |
|
|
PCM programmer wrote: | What PIC do you really have ? Your title says 18F4580, but your program
says 18F458. Those are different PICs.
What is your CCS compiler version ? |
I tried both of them, they don't work |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Oct 31, 2017 5:25 am |
|
|
Comments...
The speed used in setup_adc() is wrong. Ther's a table (21-1 ?) in the ADC section of the PIC datasheet that shows what's valid for various PIC clock rates.
The clock option might not compute ? 20,000000 I'd use 20,000,000 or 20MHz and recompile.
There's an extra, in the use RS232(...options...). BTW you need to add ERRORS to that.
The variable value only needs to be unsigned interger 16.
Hopefully you've got VDD and GND connected, though NOT shown in the Proteus schematic, they are required.
That is a 5 volt PIC, most BT modules are 3 volts, so be careful !!
Does a 1Hz LED program work ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Oct 31, 2017 7:32 am |
|
|
Unfortunately, some comments are being addressed to code that was a reply to the original post.
Lets just look at the original:
First comment. Have you verified that the PIC is actually running?.
Simple 'flash an LED' test. :
Code: |
#include <18f458.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device adc=8
#use delay(crystal=20MHz)
void main(void)
{
SETUP_PSP(PSP_DISABLED);
SETUP_COMPARATOR(NC_NC_NC_NC);
SETUP_CCP1(CCP_OFF);
SETUP_CCP2(CCP_OFF);
//several of these peripherals use pins on the ports being used
//ensure these are off.
while(1)
{
output_toggle(PIN_B0);
delay_ms(1000);
}
}
|
Does pin B0 toggle every second?.
If not, then the chip is not running. MCLR pulled up?. Crystal not working?. Power connections correct?.
If it does, then move on to the ADC.
As already pointed out the ADC clock is not recommended at this CPU clock rate.
Code: |
#include <18f458.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device adc=8
#use delay(crystal=20MHz)
int8 data=0;
void main(void)
{
SETUP_PSP(PSP_DISABLED);
SETUP_COMPARATOR(NC_NC_NC_NC);
SETUP_CCP1(CCP_OFF);
SETUP_CCP2(CCP_OFF);
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(0);
while(TRUE)
{
delay_ms(1000);
data = read_adc();
OUTPUT_B(data);
}
}
|
|
|
|
thanhandaithanh
Joined: 30 Oct 2017 Posts: 6
|
|
Posted: Tue Oct 31, 2017 7:52 am |
|
|
Ttelmah wrote: | Unfortunately, some comments are being addressed to code that was a reply to the original post.
Lets just look at the original:
First comment. Have you verified that the PIC is actually running?.
Simple 'flash an LED' test. :
Code: |
#include <18f458.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device adc=8
#use delay(crystal=20MHz)
void main(void)
{
SETUP_PSP(PSP_DISABLED);
SETUP_COMPARATOR(NC_NC_NC_NC);
SETUP_CCP1(CCP_OFF);
SETUP_CCP2(CCP_OFF);
//several of these peripherals use pins on the ports being used
//ensure these are off.
while(1)
{
output_toggle(PIN_B0);
delay_ms(1000);
}
}
|
Does pin B0 toggle every second?.
If not, then the chip is not running. MCLR pulled up?. Crystal not working?. Power connections correct?.
If it does, then move on to the ADC.
As already pointed out the ADC clock is not recommended at this CPU clock rate.
Code: |
#include <18f458.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device adc=8
#use delay(crystal=20MHz)
int8 data=0;
void main(void)
{
SETUP_PSP(PSP_DISABLED);
SETUP_COMPARATOR(NC_NC_NC_NC);
SETUP_CCP1(CCP_OFF);
SETUP_CCP2(CCP_OFF);
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(0);
while(TRUE)
{
delay_ms(1000);
data = read_adc();
OUTPUT_B(data);
}
}
|
|
Firstly, I'm sure that my Chip is good. I tried your first ex, and pin B0 toggles every second. But I still cannot read adc and export it to portB. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Oct 31, 2017 7:58 am |
|
|
Even with the second version as posted?.
What is on port B?. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Oct 31, 2017 8:06 am |
|
|
I'm assuming you have 8 LEDs and Rs connected to PortB.
Have you confirmed the AN0 pin of the PIC is connected to wiper of pot and that it does get 0v, 5v, 2.5v, etc.?
If so what do you see on the LEDs ?
I would add a 'test LED and 470r' on an unused I/O pin to toggle every time in main(). That way you KNOW the program is running..
the concept....
main()....
loop
toggle led
read adc
display adc
delay
loop again
end of main()...
Jay |
|
|
thanhandaithanh
Joined: 30 Oct 2017 Posts: 6
|
|
Posted: Tue Oct 31, 2017 8:08 am |
|
|
Ttelmah wrote: | Even with the second version as posted?.
What is on port B?. |
Yes, even with your second version
Nothing on port B
[/img] |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Oct 31, 2017 8:10 am |
|
|
You are sure you are connecting the voltage to the right pin?.
I have just tested this with a 4580 at 8MHz, and it merrily outputs the value on the port. |
|
|
thanhandaithanh
Joined: 30 Oct 2017 Posts: 6
|
|
Posted: Tue Oct 31, 2017 8:15 am |
|
|
Ttelmah wrote: | You are sure you are connecting the voltage to the right pin?.
I have just tested this with a 4580 at 8MHz, and it merrily outputs the value on the port. |
Can you upload the image of ISIS file ? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Oct 31, 2017 8:16 am |
|
|
I can't decide WHICH PIC he's using
18F458
18F4580
18F2550
sigh.... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Oct 31, 2017 8:57 am |
|
|
thanhandaithanh wrote: | Ttelmah wrote: | You are sure you are connecting the voltage to the right pin?.
I have just tested this with a 4580 at 8MHz, and it merrily outputs the value on the port. |
Can you upload the image of ISIS file ? |
No Isis file. Just a real PIC.
Read the sticky at the top of the forum about Proteus/Isis.
If you are basing your 'it does not work' on these, then you are misleading yourself.
Proteus is a great PCB package, but if you try to develop code using Isis, you will end up doing the job twice. Once to get it working in Isis, and then a second time when you go to the real PIC.... |
|
|
|