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

pic18f67k90 problem adc

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



Joined: 03 Oct 2024
Posts: 10

View user's profile Send private message

pic18f67k90 problem adc
PostPosted: Tue Oct 08, 2024 6:52 am     Reply with quote

hello everyone
i am a beginner in programming, my problem is: to acquire a value from 0 to 5V on ANA0 and send it to my screen with a pic18f67K90 or a pic18f67k22
the principle works but the problem is the linearity of the reading
attached is a raised curve, I think I forgot something!!
i have already made an identical assembly with a pic26k22 and I did not have this problem, the 67k90 or 67k22 must be special??
if someone could tell me where my mistake is, thank you in advance
cordially

I can't insert an image, sorry, explanation: input voltage
AN0 = 0v --> screen reading or debugger 234
AN0 = 1.2V --> screen reading or debugger 255
AN0 = 1.21V --> screen reading or debugger 0
An0 = 1.21v -> 4.9 v screen reading or linear debugger
An0= 5v --> screen reading or maximum debugger 188

/* compilateur CCS V5.116
PIC18F67K90 ( idem si pic18F67k22 )
*/



#include "main.h"

void main()
{

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32|RTCC_8_BIT); //2,0 ms overflow
setup_timer_2(T2_DIV_BY_16,250,10); //1,0 ms overflow, 10,0 ms interrupt


disable_interrupts(INT_TIMER0);
disable_interrupts(INT_TIMER2);


disable_interrupts(GLOBAL);

setup_adc_ports(sAN0, VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL | ADC_TAD_MUL_20);

set_adc_channel(0);
delay_us(20);

while(TRUE){

bouton = input(PIN_E7);
if(bouton == 1){

potard1= read_adc(adc_start_and_read) ;
delay_us(100);


//////// tacho metre
fputc(0XAA,PORT1);
fputc(0X3D,PORT1); // 3D -> N16 write / 42 -> str write
/// adresse ////
fputc(0X00,PORT1);
fputc(0X08,PORT1);
fputc(0X00,PORT1);

fputc((potard1 & FILTRE_INT16_H ) >>8,PORT1) ;
fputc(potard1 & FILTRE_INT16_L ,PORT1);

//Copie suffixe
fputc(0XCC,PORT1);
fputc(0X33,PORT1);
fputc(0XC3,PORT1);
fputc(0X3C,PORT1);

delay_ms(5);

output_high(PIN_B3);
}
else
{
output_low(PIN_B3);


} // while true

}
Code:


#include <18F67K90.h>
#device ADC=8


#FUSES NOWDT //No Watch Dog Timer
#FUSES VREGSLEEP //Ultra low-power regulator is disabled
#FUSES SOSC_LOW //Low-power SOSC circuit is selected
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV18 //Brownout reset at 1.8V
#FUSES ZPBORM //Zero-Power BOR
#FUSES RTCOSC_T1 //RTCC uses Secondary Oscillator as reference source
#FUSES MSSPMSK7 //MSSP uses 7 bit Masking mode
#FUSES MCLR //Master Clear pin enabled
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES BBSIZ2K //2K words Boot Block size
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOWRT //Program memory not write protected
#FUSES NOEBTR //Memory not protected from table reads


#use delay(clock=64MHz,crystal=16MHz)



#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT1) // ecran
#use rs232(baud=9600,parity=N,xmit=PIN_G1,rcv=PIN_G2,bits=8,stream=PORT2)

//Filtres
#DEFINE FILTRE_INT16_H 0xFF00
#DEFINE FILTRE_INT16_L 0x00FF

#DEFINE FILTRE_HH 0xFF000000
#DEFINE FILTRE_HL 0x00FF0000
#DEFINE FILTRE_LH 0x0000FF00
#DEFINE FILTRE_LL 0x000000FF

//#include <stdio.h>
//#include <stdlib.h>

int1 bouton ;
unsigned int8 potard1;

[/code][img][/img]
temtronic



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

View user's profile Send private message

PostPosted: Tue Oct 08, 2024 11:35 am     Reply with quote

quick comments
start putting comments at the end of most lines of code ! It costs nothing yet allows EVERYONE to understand what should be happening. It'll really help 3 weeks from now !

As a basic test, I'd JUST send the RAW data from the pot position.
This allows you to confirm PIC does read the pot and send the correct data.
Since it is a pot, I suggest a small (.1-1mfd ) cap on the wiper to ground as a 'filter' and sample at say a 1Hz rate

also add 'errors' to the USE RS232 (.... options...... )
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Tue Oct 08, 2024 11:45 am     Reply with quote

I'm very puzzled why you are trying to access the upper 8 bits of an 8bit
value?..
However key difference is that the ADC on this chip is a differential ADC.
The top bit is a sign bit.
alain-debutant



Joined: 03 Oct 2024
Posts: 10

View user's profile Send private message

pic18f67k90 problem adc
PostPosted: Fri Oct 11, 2024 2:41 am     Reply with quote

hello
thank you Ttelmah for your clear answer, I suspected that this chip was special but I would not have imagined that it was a differential input, sorry to abuse your knowledge how to do the code for this kind of application? for information I also put a capacitor for filtering on this input and the signal is very clean, I am starting with chip programming by passion and I have no training as a programmer!
thank you again for your help, you are a genius
alain-debutant



Joined: 03 Oct 2024
Posts: 10

View user's profile Send private message

pic18f67k90 problem adc
PostPosted: Fri Oct 11, 2024 2:48 am     Reply with quote

for Mr Ttelmah: originally I am on a 16 bit value, but as I had the same problem I wanted to simplify my real value that I would like to use is indeed in 16 bit

thanks
alain-debutant



Joined: 03 Oct 2024
Posts: 10

View user's profile Send private message

PostPosted: Fri Oct 11, 2024 5:37 am     Reply with quote

Mr Ttelmah
how to configure this chip in single channel mode?? I looked at the datacheet page 373 but I don't know how to do it, or how to use it to have a linear scale from 0 to 4095??
thanks again for your help
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Fri Oct 11, 2024 7:07 am     Reply with quote

The point is it is always a differential ADC module. Even when you run it
so Vss is the second input. The top bit is always ADSGN. The result is
12bits plus sign. Because you were trying to select 8 bit mode, this switches
to returning the high 7 bits plus sign. If you just switch to 12bit mode,
and read the low twelve bits of the value returned, if you want 8 bits, just
rotate this right 4 bits.
alain-debutant



Joined: 03 Oct 2024
Posts: 10

View user's profile Send private message

pic18f67k90 problem adc
PostPosted: Sat Oct 12, 2024 2:48 am     Reply with quote

thank you very much Ttelmah, I will do like that.
good luck on the forum you are great
best regards
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