ANSEL PIC16F684

General discussion on mikroC.
Post Reply
Author
Message
Storic
Posts: 393
Joined: 01 Oct 2004 00:20
Location: Australia (S.A.)

ANSEL PIC16F684

#1 Post by Storic » 01 Sep 2005 11:06

Hi,
I am having a couple of problems with seting up an analog port on the the above micro

How do you set the input to an analog input then use the Adc_Read() to reference the value. I have been going around in circle's :? :roll: :?

Code: Select all

  CMCON0 = 7;
  PORTC = 0;
  TRISC = 0;
  PORTA = 0xff;
  TRISA = 0; 	// Set RA5 as an output //temp RA0 as CPU
  
  ANSEL = 0x10;         // Configure AN2,AN3,AN4, AN5 and AN7 as analog
  ADCON0.VCFG = 0;	// Use Vdd as Ref
  ADCON0.ADFM = 1;	// Right justified A/D result

  TRISA.ANS3 = 1;	// pin ANS3 as input
.....

.....
 ISR_Value = Adc_Read(ANS3);
Andrew

briphi
Posts: 19
Joined: 13 Aug 2005 02:29

#2 Post by briphi » 02 Sep 2005 00:33

I'm not familiar with the 16f684, but i recently had a lot of trouble with the pic16f916 and the ADC. Apparently, the library ADC_Read did not work on the 916 and may (or may not) be the same problem that you're having. What I did was rewrite the ADC_Read routine myself and I use that now instead. Its kinda a pain in the ass, but you do what you gotta.

Storic
Posts: 393
Joined: 01 Oct 2004 00:20
Location: Australia (S.A.)

#3 Post by Storic » 02 Sep 2005 09:20

Yes,
I have been thinking along the same lines, can you show me where to start with doing this, I am not very good with ASM. :?

Andrew

User avatar
rajkovic
mikroElektronika team
Posts: 694
Joined: 16 Aug 2004 12:40

#4 Post by rajkovic » 02 Sep 2005 16:17

Storic wrote:Yes,
I have been thinking along the same lines, can you show me where to start with doing this, I am not very good with ASM. :?
Andrew
Will check as soon as possible.
If there are errors we will correct source
and send you .mcl.

We are working on optimization and some other improvments
:wink: .

Storic
Posts: 393
Joined: 01 Oct 2004 00:20
Location: Australia (S.A.)

#5 Post by Storic » 03 Sep 2005 11:30

Hi,
I went back to basic and found that by removing interupt on timer 0 and remove the reference to ANSEL, (a rewrite of the 12F675 flashing led program) I got it to work. :?: :? :?: :?:

Still need some work however it may seem that the Adc_Read() does work, however not so well with the interupt. :?

Code: Select all

#define CLEARWDOG asm  CLRWDT
#define CPU_Led   PORTA.f0
#define ON   1
#define OFF  0

unsigned L_REF;
unsigned     counter;

void ISR_AC() {
      if (L_REF > 870)  {  //
         PORTC =  0x0D;
      }
      else
      if (L_REF > 700)  {  //
         PORTC = 0x05;
      }
      else
      if (L_REF > 510)  {  //
         PORTC = 0x01;
      }
      else
      if (L_REF > 480)  {  //
         PORTC = 0x00;
      }
      else
      if (L_REF > 270)  {  //
         PORTC = 0x03;
      }
      else
      if (L_REF > 110)  {  //
         PORTC = 0x07;
      }
      else                 //
         PORTC = 0x0f;
}//~

void Init() {
  PORTA = 0;
  PORTC = 0;

  TRISC = 0;
  TRISA = 0;
  

  ANSEL = 0;
  CMCON0 = 7;
  TRISA = 0x0C;               // designate gpio as output
  TRISA.ANS3 = 1;             // pin ANS3 as input
  ADCON0.VCFG = 0;            // Vdd as Vref

  // enable TMRO interrupt

  counter     =   0;          // initialize counter
}//~

void main() {
  Init();
  do {
  counter ++;
  if (counter > 1200) {
     CPU_Led = ~CPU_Led;   //CPU Led heart beat
     counter = 0;              // Reset cnt
     }
                        // beginning of a repeat loop
    L_REF = Adc_Read(ANS2); // ADC conversion
    ISR_AC();

  } while (1);              // endless loop
                           // :
}//~
I am now even more confused I was trying to reference TRISA.ANS3 = 1; it did not work; when I went from Adc_Read(ANS3); to Adc_Read(ANS2); without seting it as an input, it worked?

I found that the interupt not working (was working) wasn't able to see the CPU LED due to the ANSEL registar not set for the CPU led for D0

Andrew

Post Reply

Return to “mikroC General”