CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

ADC with PIC18f4580

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
thanhandaithanh



Joined: 30 Oct 2017
Posts: 6

View user's profile Send private message

ADC with PIC18f4580
PostPosted: Mon Oct 30, 2017 9:27 pm     Reply with quote

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

View user's profile Send private message

Re: ADC with PIC18f4580
PostPosted: Mon Oct 30, 2017 10:43 pm     Reply with quote



modify according to your settings

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 30, 2017 10:46 pm     Reply with quote

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

View user's profile Send private message

Re: ADC with PIC18f4580
PostPosted: Mon Oct 30, 2017 10:47 pm     Reply with quote

srikrishna wrote:


modify according to your settings



I don't know what is problem in my code ?
thanhandaithanh



Joined: 30 Oct 2017
Posts: 6

View user's profile Send private message

PostPosted: Mon Oct 30, 2017 10:48 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 31, 2017 5:25 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 31, 2017 7:32 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 31, 2017 7:52 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 31, 2017 7:58 am     Reply with quote

Even with the second version as posted?.

What is on port B?.
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Oct 31, 2017 8:06 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 31, 2017 8:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 31, 2017 8:10 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 31, 2017 8:15 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 31, 2017 8:16 am     Reply with quote

I can't decide WHICH PIC he's using

18F458

18F4580

18F2550

sigh....
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Tue Oct 31, 2017 8:57 am     Reply with quote

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....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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