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

Please help. I do not read 18F4520 ADC correctly.

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



Joined: 19 Jun 2015
Posts: 21
Location: Banned - Spammer

View user's profile Send private message

Please help. I do not read 18F4520 ADC correctly.
PostPosted: Sat Apr 07, 2018 2:32 am     Reply with quote

Hi All
I am writing a program about reading pic18f4520 ADC and display it 7 segment display. Program is doing wrong reading from ADC. and then continue. I have trouble with this. program is below.
anybody help me for solution.
thanks.
Code:

#include <18f4520.h>
#device ADC=10 //ICD=TRUE
#include <c:\inc\defs_4520.h>
#FUSES INTRC_IO,NOFCMEN,PUT,NOCPB
//#FUSES NOPROTECT,NOWDT,MCLR,NOCPD,NOBROWNOUT,NOWRTC,NOWRTB,NOWRTD,NOEBTR,NOEBTRB
#use delay(CLOCK=8000000)

#use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_C7)

void main(){

   delay_ms(300);
   set_tris_a(0x01);
   set_tris_b(0x40);
   set_tris_c(0x00);
   set_tris_d(0x00);
   set_tris_e(0x00);
   
   output_a(0x01);
   output_b(0x40);
   output_c(0x00);
   output_d(0x00);
   output_e(0x00);

   setup_adc_ports(AN0|VSS_VDD);   
   setup_timer_0(T0_INTERNAL|T0_8_BIT|T0_DIV_256);
   setup_adc(ADC_CLOCK_INTERNAL); 

   set_adc_channel(0);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   while (true){
      DAC = read_adc();
      delay_us(30);
      //DAC=(DAC/1024)*5;
      printf("A/D value = %3lu", DAC);
      printf("\n\r");
      delay_ms(100);
   }
}
temtronic



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

View user's profile Send private message

PostPosted: Sat Apr 07, 2018 5:36 am     Reply with quote

I didn't look at all the code as

this line...
setup_adc(ADC_CLOCK_INTERNAL);

is incorrect.

Typically the ..internal option is used when the PIC reads the ADC while asleep.
There is a table ( 19-1) which shows what are valid ADC clock setups for PIC clock speeds. There are 2 options for you for <-8MHz. which is best for you ,depends on how you need to use the ADC. I'd choose one option, run say 1000 sample, then choose the other option, run 1000 samples again. Compare the results to what you expect and choose the option that gives the best results.

Now there may be other code wrong but this one is the FIRST I would correct as according to the datasheet,using ...internal WILL give 'bad numbers'.

Jay
amcasi



Joined: 19 Jun 2015
Posts: 21
Location: Banned - Spammer

View user's profile Send private message

PostPosted: Sat Apr 07, 2018 7:21 am     Reply with quote

in 18f4520.h file there are these constants.
Code:

// Constants used for SETUP_ADC() are:
#define ADC_OFF               0           // ADC Off
#define ADC_CLOCK_DIV_2   0x100
#define ADC_CLOCK_DIV_4    0x04
#define ADC_CLOCK_DIV_8    0x01
#define ADC_CLOCK_DIV_16   0x05
#define ADC_CLOCK_DIV_32   0x02
#define ADC_CLOCK_DIV_64   0x06
#define ADC_CLOCK_INTERNAL 0x07           // Internal 2-6us

Regards
temtronic



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

View user's profile Send private message

PostPosted: Sat Apr 07, 2018 8:02 am     Reply with quote

Please note that just because they are listed does not mean you can use them without reading the ADC section of the PIC's datasheet. There are specific rules that must followed to properly use the PIC's ADC subsection.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Please help. I do not read 18F4520 ADC correctly.
PostPosted: Sat Apr 07, 2018 9:23 am     Reply with quote

amcasi wrote:
Hi All
void main(){

delay_ms(300);
set_tris_a(0x01);
set_tris_b(0x40);
set_tris_c(0x00);
set_tris_d(0x00);
set_tris_e(0x00);

output_a(0x01);
output_b(0x40);
output_c(0x00);
output_d(0x00);
output_e(0x00);

The line in bold above is over-writing the TRIS for PortA. That line makes
PortA into all output pins. Look at the .LST file. It's setting TRISA = 0x00.
Quote:
..... output_a(0x01);
015A: CLRF TRISA
015C: MOVLW 01
015E: MOVWF LATA

The A/D conversion on pin A0 requires that it must be set as an input pin.
So this is the problem that must be fixed.

Since you are setting the TRIS for all ports with set_tris_x() statements,
it would be best if you switch the compiler to fast_io mode for all i/o ports.
Add the statement shown in bold below, in that exact location:
Quote:
#include <18f4520.h>
#device ADC=10
#FUSES INTRC_IO,NOFCMEN,PUT,NOCPB
#use delay(CLOCK=8000000)

#use fast_io(all)

#use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_C7)

Now when you do output_a(), it doesn't over-ride your TRISA settings.
It only writes to the latch for PortA. This is what you want.
Look at the .LST file to see this:
Quote:
..... output_a(0x01);
015A: MOVLW 01
015C: MOVWF LATA
amcasi



Joined: 19 Jun 2015
Posts: 21
Location: Banned - Spammer

View user's profile Send private message

Problem Solved
PostPosted: Tue Apr 10, 2018 2:03 am     Reply with quote

Thanks friend,
I solved problem. There is short circuit Pin_A0 and PIN_A1. I corrected and it works. I have a one A/D input, others and Digital out. Problem was there.
Thanks for everybody.
Huseyin
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