View previous topic :: View next topic |
Author |
Message |
mfciril
Joined: 18 Oct 2011 Posts: 5
|
how to use icspclk and icspdata as adc analog input |
Posted: Tue Oct 18, 2011 1:30 pm |
|
|
Hi,
I'm trying to use the AN0 and AN1 channel of the PIC16F690 for an a/d conversion, but they don't work. I'm using eight adc channel and only six work well. The PIC doesn't convert the signal on icspclk and icspdata pins. How I can configure it?
This is the code that I use:
Code: |
#include <16F690.H>
#device ADC=10
#define CLOCK_SP 8000000
#fuses HS, NOWDT, NOPROTECT
#define TxD PIN_B7
#define RxD PIN_B5
#use delay (internal=CLOCK_SP)
#use rs232(stream=pc,baud=57600,parity=N,xmit=TxD,rcv=RxD,stop=1,BITS=8)
#byte ADCON0 = 0x01F
char a;
char dati[4];
char value;
void main (void)
{
setup_adc(ADC_CLOCK_DIV_16);
setup_adc_ports(sAN0|sAN1|sAN2|sAN3|sAN4|sAN5|sAN6|sAN7);
disable_interrupts(GLOBAL);
while(1){
a = 0;
while(a != 'a')
{
if(kbhit(pc))
{
a = getc(pc);
}
}
//Acquisisco dal primo canale A0/AN0
set_adc_channel(0);
delay_us(10);
value=read_adc();
delay_us(30);
dati[0] = value;
dati[1] = value >> 8;
putc(dati[0],pc);
putc(dati[1],pc);
//Acquisisco dal secondo canale A1/AN1
set_adc_channel(1);
delay_us(10);
value=read_adc();
delay_us(30);
dati[2] = value;
dati[3] = value >> 8;
putc(dati[2],pc);
putc(dati[3],pc);
}
}
|
I tryed also to set the adc channel with the ADCON0 register, but nothing...
Thank you so much...... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Oct 18, 2011 2:11 pm |
|
|
Do you have your ICD or Pickit programmer connected to the 16F690 ?
Are you compiling the code in "Debug" mode ?
If so, compile and program the code in "Programmer" mode. Then
disconnect your ICD or Pickit from the board and then test it.
Also, there is a problem in your code below. First, you are telling the
compiler to use the HS fuse, which is for a high speed external crystal.
But then, in the #use delay you tell it to use the INTERNAL built-in
oscillator. Those are conflicting settings.
Quote: |
#fuses HS, NOWDT, NOPROTECT
#use delay (internal=CLOCK_SP) |
|
|
|
mfciril
Joined: 18 Oct 2011 Posts: 5
|
|
Posted: Tue Oct 18, 2011 2:40 pm |
|
|
i use the pickit2 only to program the pic. after I use it only for the VDD and GND and I disconnect the other connections. I use the internal oscillator: must I remove HS fuse? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Oct 18, 2011 2:44 pm |
|
|
What's your board ? Did you buy it ? If so, post the manufacturer and
part number.
Just change the HS to INTRC_IO. That's for the internal oscillator.
Also, what's your compiler version ? It's given at the top of the .LST file.
Versions:
http://www.ccsinfo.com/devices.php?page=versioninfo |
|
|
mfciril
Joined: 18 Oct 2011 Posts: 5
|
|
Posted: Wed Oct 19, 2011 3:19 am |
|
|
I'm not using a board. I connect the pickit2 to the pic with simple wires only. The version of the compiler is 4.104. |
|
|
mfciril
Joined: 18 Oct 2011 Posts: 5
|
|
Posted: Wed Oct 19, 2011 7:28 am |
|
|
I have a new: the icspdata and icspclk channel work well as adc channels. The PIC doesn't convert first and second channels specified in the code. All channels that I put in first and second position don't work. Why? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 19, 2011 12:21 pm |
|
|
I don't understand what you mean by "first and second position". I made
a simple test program for channels 0 and 1 below. Does this program
fail for you ? Post a simple program. Show me the problem.
Code: |
#include <16F690.h>
#device adc=10
#fuses INTRC_IO,NOWDT,BROWNOUT,PUT
#use delay(clock=8M)
#use rs232(baud=9600, xmit=PIN_B7, rcv=PIN_B5)
//============================
void main()
{
int16 result;
setup_adc_ports(sAN0 | sAN1);
setup_adc(ADC_CLOCK_DIV_16);
while(1)
{
set_adc_channel(0);
delay_us(10);
result = read_adc();
printf("sAN0: %lu \n\r", result);
set_adc_channel(1);
delay_us(10);
result = read_adc();
printf("sAN1: %lu \n\r", result);
}
} |
|
|
|
mfciril
Joined: 18 Oct 2011 Posts: 5
|
|
Posted: Thu Oct 20, 2011 2:55 am |
|
|
The AN0 and AN1 work well with your code. Now my problem is another. I have to read by eight adc channel. If I use seven channel only all works well; if I put the code for the 8th channel the first and the second ones don't work. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 20, 2011 4:49 pm |
|
|
I made a test program and a test fixture to test ADC channels 0 to 7.
I installed vs. 4.104. I have a 16F690 installed on a Microchip "Low Pin
Count" board. I jumpered AN2 to AN7 to ground. AN0 is connected to
a trimpot on the board. I jumpered AN1 to another trimpot on a
breadboard. I programmed the PIC with the code below. I then
unplugged the PicKit 2 from the board, and then powered the board from
a +5v bench power supply. That way, the Pickit 2 won't interfere with
the A/D inputs on AN0 and AN1 (because those pins are used for ICSP
programming by the Pickit 2).
I then set the trimpot on AN1 to be in the middle of its range. I then
started running the program and slowly turned the trimpot on AN0 from
one end to the other end. I got these results:
Code: |
sAN0-7: 0 497 0 1 0 0 0 0
sAN0-7: 0 498 1 1 1 1 0 0
sAN0-7: 0 498 0 0 0 0 0 1
sAN0-7: 0 497 0 0 0 0 0 0
sAN0-7: 1 497 0 1 0 0 0 0
sAN0-7: 84 498 1 1 0 0 0 1
sAN0-7: 247 498 1 1 0 1 0 1
sAN0-7: 326 498 0 0 0 0 1 1
sAN0-7: 384 498 0 0 0 0 1 0
sAN0-7: 514 499 1 0 0 0 0 0
sAN0-7: 678 497 0 1 0 0 0 0
sAN0-7: 914 501 0 1 0 1 0 1
sAN0-7: 1023 497 1 0 0 0 1 1
sAN0-7: 1023 498 0 0 0 0 1 1
sAN0-7: 1023 497 1 0 0 0 0 1
sAN0-7: 1023 497 2 0 0 1 0 0
|
You can see that AN0 goes from 0 up to the max value of 1023 as I
turn the trimpot from 0v to 5v. That's working. Then AN2 remains at
about 498 or in that area, because I set that trimpot to the middle of
the range. The other ADC inputs, AN2 to AN7 are grounded and they
stay at either 0 or 1 mostly. The ground jumpers are 12 inches (30.5cm)
long and go to a breadboard, so that probably accounts for the bobbling
between 0 and 1. So it looks like it's working. I don't see a problem.
Code: |
#include <16F690.h>
#device adc=10
#fuses INTRC_IO,NOWDT,BROWNOUT,PUT
#use delay(clock=8M)
#use rs232(baud=9600, xmit=PIN_B7, rcv=PIN_B5)
//============================
void main()
{
int16 result;
setup_adc_ports(sAN0|sAN1|sAN2|sAN3|sAN4|sAN5|sAN6|sAN7);
setup_adc(ADC_CLOCK_DIV_16);
while(1)
{
set_adc_channel(0);
delay_us(10);
result = read_adc();
printf("sAN0-7: %4lu ", result);
set_adc_channel(1);
delay_us(10);
result = read_adc();
printf("%4lu ", result);
set_adc_channel(2);
delay_us(10);
result = read_adc();
printf("%4lu ", result);
set_adc_channel(3);
delay_us(10);
result = read_adc();
printf("%4lu ", result);
set_adc_channel(4);
delay_us(10);
result = read_adc();
printf("%4lu ", result);
set_adc_channel(5);
delay_us(10);
result = read_adc();
printf("%4lu ", result);
set_adc_channel(6);
delay_us(10);
result = read_adc();
printf("%4lu ", result);
set_adc_channel(7);
delay_us(10);
result = read_adc();
printf("%4lu ", result);
printf("\n\r");
delay_ms(1000);
}
} |
|
|
|
|