ADC Library and ADCON1

General discussion on mikroBasic.
Post Reply
Author
Message
atdsm
Posts: 12
Joined: 15 Dec 2004 02:58
Contact:

ADC Library and ADCON1

#1 Post by atdsm » 15 Dec 2004 03:07

Hi, I'm working with mikroBasic and a PIC18F452. I want to be able to left-justify the results of an analog to digital conversion, but the ADC_Read function seems to override whatever I set to ADCON1.

I've modified the ADC_on_Leds example program to demonstrate my problem. Here's what I have:

Code: Select all

program ADC_on_Leds

dim temp_res as word

main:
  ADCON1 = $80  ' configure analog inputs and Vref
  TRISA  = $FF  ' designate PORTA as input
  TRISB  = 0  ' designate PORTB as output
  TRISD  = $0   ' designate PORTD as output
  while true

      temp_res = ADC_read(2)
      'now you can use temp_res ...
      PORTB = temp_res ' send lower 8 bits to PORTB
      PORTD = word(temp_res >> 8)
      ' send higer bits to PORTD
  wend
end.
Up to here everything behaves properly.

To set the output to left justified, I change this line:

Code: Select all

ADCON1 = $80  ' configure analog inputs and Vref
To this:

Code: Select all

ADCON1 = $00  ' configure analog inputs and Vref
Recompile and download, but the output on the LED's looks the same as before.

Any ideas as to why my custom settings are being overwritten?

Steve

p.s. in the meantime, I'll amuse myself trying to write my own ADC routine...
There are 10 kinds of people in the world... those who can read binary and those who can't.

Charlie
Posts: 2744
Joined: 01 Dec 2004 22:29

#2 Post by Charlie » 15 Dec 2004 23:09

Hi atdsm,

I don't know what programmer you are using,but one of the ones that I have I have to erase the pic first,then reprogram it with the new code.
This might not be the case with you,but it may be worth a try.




Regards Charlie

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

Re: ADC Library and ADCON1

#3 Post by zristic » 15 Dec 2004 23:30

Yes, the function "ADC_Read" right justifies the result. Here is the code for reading AD conversion result with 18F452 (taken from Microchip's PDF and tested with mikroPascal):

Code: Select all

//This is how to read ad conversion from <channel>
  ADCON0 := 0;
  ADCON0 := ADCON0 or $C0; // rc clock source
  SetBit(ADCON1,6);        // to make code compatibile with p16f87xA series
                           // and upward compatibile with some 18 series
  SetBit(ADCON1, ADFM);    // right justified
  ADCON0 := ADCON0 or byte(channel shl 3);  // select channel
  SetBit(ADCON0, ADON);     //turn on ADC modul
  delay_us(25);                        // wait required acquisition time
  SetBit(ADCON0, GO_DONE);   //start conversion
  while TestBit(ADCON0,GO_DONE) = 1 do       // wait for conversion to complete
    nop;
  // after GO_DONE bit is set low the ADC result is stored in ADRESH and ADRESL
  // you can use ADRESH and ADRESL here to read conversion value
  ClearBit(ADCON0, ADON);   //turn off ADC modul if necessary
It can be easily ported to mikroBasic, i just did not have time, sorry.

atdsm
Posts: 12
Joined: 15 Dec 2004 02:58
Contact:

#4 Post by atdsm » 07 Jan 2005 03:22

Thanks for that tip... I read up on this stuff over break. I'll probably write my own code for the (slightly different but not very different) ADC on the PIC18F4620. (It has three registers instead of two because it supports more than 8 channels.)

Would anyone be interested in me posting the code once I finish it?
There are 10 kinds of people in the world... those who can read binary and those who can't.

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

#5 Post by zristic » 07 Jan 2005 11:03

atdsm wrote:Would anyone be interested in me posting the code once I finish it?
It would be useful to have the code snippet here on forum.

atdsm
Posts: 12
Joined: 15 Dec 2004 02:58
Contact:

#6 Post by atdsm » 08 Jan 2005 21:21

I have written the code, but I can't test it yet because the PICFlash programmer doesn't support the 18F4620 devices... Aparently that support is supposed to come out soon? As soon as it does I'll test my code and then post it on here.
There are 10 kinds of people in the world... those who can read binary and those who can't.

Post Reply

Return to “mikroBasic General”