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

pic18f4431 with adc

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



Joined: 19 Oct 2013
Posts: 19
Location: salam

View user's profile Send private message

pic18f4431 with adc
PostPosted: Wed Sep 10, 2014 11:28 pm     Reply with quote

dear all, i am using pic18f4431 controller. I am using adc channel pin a0. I got 255 only in serial window through hardware crystal value 20mhz. Pls help me dear all

i initialized like
Code:
void main()
{
unsigned int valadc=0;

set_tris_a(0x1f);
setup_adc_ports(sAN0|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0|ADC_WHEN_INT0|ADC_INT_EVERY_OTHER);
setup_timer_0(RTCC_INTERNAL);

set_adc_channel(0);

  while(true)
    {
     delay_ms(100);
     valadc = read_adc();
     valadc=valadc*0.0197;
     printf("\n\r");
     printf("%u",valadc);
    }
}

Code:
/*************header file***************/

#include <18F4431.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV27                   //Brownout reset at 2.7V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                      //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOWINEN                  //WDT Timer Window Disabled
#FUSES T1LOWPOWER               //Timer1 low power operation when in sleep
#FUSES HPOL_HIGH                //High-Side Transistors Polarity is Active-High (PWM 1,3,5 and 7)
   //PWM module high side output pins have active high output polarity
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES LPOL_HIGH                //Low-Side Transistors Polarity is Active-High (PWM 0,2,4 and 6)
   //PWM module low side output pins have active high output polar
#FUSES PWMPIN                   //PWM outputs disabled upon Reset
#FUSES MCLR                     //Master Clear pin enabled
#FUSES FLTAC1                   //FLTA input is multiplexed with RC1
#FUSES SSP_RC                   //SCK/SCL=RC5, SDA/SDI=RC4, SDO=RC7
#FUSES PWM4B5                   //PWM4 output is multiplexed on RB5
#FUSES EXCLKC3                  //TMR0/T5CKI external clock input is muliplexed with RC3

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

Rolling Eyes
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Thu Sep 11, 2014 12:22 am     Reply with quote

Three reasons one after the other:

adc=8

tells the compiler to only read the 8bit value from the adc....

Then:

unsigned int valadc=0;

How big is an int?.

Then:

printf("%u",valadc);

What size variables can %u handle?. Look at what %Lu handles instead.

Each of these will stop you getting the 10bit value.
deenadayalan



Joined: 19 Oct 2013
Posts: 19
Location: salam

View user's profile Send private message

PostPosted: Thu Sep 11, 2014 1:32 am     Reply with quote

8 bit adc enough this application sir , i used int but simulation showed negative values ,if i use unsigned int and %u simulation showed 0 to 255
properly ,i checked in a0 pin during the adc input,voltage reaching in a0 but i got 255 only in output window

this is my problem sir
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Thu Sep 11, 2014 1:45 am     Reply with quote

Er....

An 8bit value, has 256 values. 0 to 255. 255, is the biggest value that the ADC can return in 8bit mode, and the biggest value that an int8 can hold.
deenadayalan



Joined: 19 Oct 2013
Posts: 19
Location: salam

View user's profile Send private message

PostPosted: Thu Sep 11, 2014 2:32 am     Reply with quote

even i am connecting adc to gnd result is 255 sir didn't show 0
deenadayalan



Joined: 19 Oct 2013
Posts: 19
Location: salam

View user's profile Send private message

PostPosted: Thu Sep 11, 2014 2:38 am     Reply with quote

this code also showed same result,result 03ff didnt change any analog input


Code:
#include <18F4431.h>
#device adc=10
#fuses HS,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=20000000) 
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//=================================
void main()
{
int16 result;

setup_adc_ports(sAN0);
setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(0);
delay_us(15);

while(1)
  { 
   result = read_adc();
   printf("%LX \n\r", result);
   delay_ms(500);
  }

}
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

PostPosted: Thu Sep 11, 2014 2:39 am     Reply with quote

Valadc = valadc * 0,0197
The right side of the equation returns a floating point since the 0,0197 is such format.
Then you asaign it to an unint. It is uncorrect.
_________________
A person who never made a mistake never tried anything new.
deenadayalan



Joined: 19 Oct 2013
Posts: 19
Location: salam

View user's profile Send private message

PostPosted: Thu Sep 11, 2014 2:45 am     Reply with quote

thank you dear rikotech8 , i am getting same problem another attached example. i couldnt find my mistake
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Thu Sep 11, 2014 3:04 am     Reply with quote

Well you are telling the ADC to sample on an interrupt....

Just look in the forum, at code people use for the ADC. Your setup, is _not_ how to use the adc.

An int, is unsigned by default.

What changes how it is displayed, is the format in the printf statement. %d, prints it as a signed value, %u prints it as an unsigned.

Your maths is completely screwed. The int, can't hold the result in any meaningful manner.....
Code:

void main()
{
   int8 valadc;

   setup_adc_ports(sAN0|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL); //OK for 8bit, but not recommended
   set_adc_channel(0); //voltage input on pin2.

   while(true)
   {
      delay_ms(100);
      valadc = read_adc();
      valadc=(valadc * 10L) / 51; //at this point the multiply needs 16bit. 'L'
      printf("\n\r%3.1w volts",valadc); //display as x.x volts
    }
}


As a general comment, add 'ERRORS' to your RS232 declaration.

ERRORS, is _required_ when using hardware RS232, unless _you_ handle the error conditions yourself. It is a problem waiting to catch you, unless you use it.
deenadayalan



Joined: 19 Oct 2013
Posts: 19
Location: salam

View user's profile Send private message

PostPosted: Thu Sep 11, 2014 3:35 am     Reply with quote

thank you dear Ttelmah i got continuously 5.0 volts, i used your code, again thank you for your valuable code, i used 20mhz crystal 22pf capacitor in my hardware circuit.
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Thu Sep 11, 2014 3:47 am     Reply with quote

Seriously I'd bet pin 2 is shorted high.
deenadayalan



Joined: 19 Oct 2013
Posts: 19
Location: salam

View user's profile Send private message

PostPosted: Thu Sep 11, 2014 4:01 am     Reply with quote

sir i checked normally it has 0.8 v
when connected variable resistor for adc input

adc v --------------- controller pin (v)

starting -------------- 0.004

middle --------------- 2.505

ending ------------ 5
deenadayalan



Joined: 19 Oct 2013
Posts: 19
Location: salam

View user's profile Send private message

PostPosted: Thu Sep 11, 2014 6:50 am     Reply with quote

dear all i changed crystal value (16mhz) . i got perfect output thank you very much sir................ Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy
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