View previous topic :: View next topic |
Author |
Message |
Rez
Joined: 10 Feb 2019 Posts: 26
|
Help With 12f683 Two Analog Inputs |
Posted: Tue Feb 19, 2019 1:04 pm |
|
|
I have two timers and I am controlling the time with A0 and A1 as inputs (2 10k pots). I am not sure if I am setting up the ports correctly but I can't get them both to work. If I have input A0 by itself it works.
This is my setup for the ports.
Code: |
#use delay(clock = 4000000)
#fuses INTRC_IO,NOWDT,NOPROTECT,NOMCLR
setup_adc(ADC_CLOCK_INTERNAL); //internal clock
setup_adc_ports(sAN0 | sAN1); //setup port A0
set_adc_channel(0); //output to port A0
set_adc_channel(1); //output to port A1
setup_comparator(NC_NC_NC_NC); //turn off comp
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Tue Feb 19, 2019 1:23 pm |
|
|
quick comment....
from the datasheet, ADC section, table 9-1....
notes:
4: When the device frequency is greater than 1 MHz, the FRC clock source is only recommended if the
conversion will be performed during Sleep.
You've decised on a 4MHz clock ,so...you only have 2 choices according to the table. |
|
|
Rez
Joined: 10 Feb 2019 Posts: 26
|
|
Posted: Tue Feb 19, 2019 1:47 pm |
|
|
temtronic wrote: | quick comment....
from the datasheet, ADC section, table 9-1....
notes:
4: When the device frequency is greater than 1 MHz, the FRC clock source is only recommended if the
conversion will be performed during Sleep.
You've decised on a 4MHz clock ,so...you only have 2 choices according to the table. |
I am new to ADC so I don't understand enough of what the data sheet is saying, could you help out and explain some of it to me so I can learn some more about it, thx. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Tue Feb 19, 2019 1:51 pm |
|
|
Then set_adc_channel(0), is not a 'setup'.
It physically connectes AN0 to the ADC.
Once you gave done this, you have to wait for Tacq, and then read the ADC
This will then return the digital value corresponding to the voltage on AN0.
You can then set_adc_channel(1), again wait for Tacq, and then read the
port again to read the value from AN1. |
|
|
Rez
Joined: 10 Feb 2019 Posts: 26
|
|
Posted: Tue Feb 19, 2019 2:29 pm |
|
|
Ttelmah wrote: | Then set_adc_channel(0), is not a 'setup'.
It physically connectes AN0 to the ADC.
Once you gave done this, you have to wait for Tacq, and then read the ADC
This will then return the digital value corresponding to the voltage on AN0.
You can then set_adc_channel(1), again wait for Tacq, and then read the
port again to read the value from AN1. |
I got this to work, is this a correct way of doing it, is there a better way?
Code: |
setup_adc(ADC_CLOCK_INTERNAL); //internal clock
setup_adc_ports(sAN0 | sAN1); //read port A0 and A1
set_adc_channel(0); //output to port A0
result = read_adc();
delay_us(10);
set_adc_channel(1); //output to port A1
resultACT = read_adc();
delay_us(10);
|
|
|
|
Rez
Joined: 10 Feb 2019 Posts: 26
|
|
Posted: Tue Feb 19, 2019 2:55 pm |
|
|
Rez wrote: | Ttelmah wrote: | Then set_adc_channel(0), is not a 'setup'.
It physically connectes AN0 to the ADC.
Once you gave done this, you have to wait for Tacq, and then read the ADC
This will then return the digital value corresponding to the voltage on AN0.
You can then set_adc_channel(1), again wait for Tacq, and then read the
port again to read the value from AN1. |
I got this to work, is this a correct way of doing it, is there a better way?
Code: |
setup_adc(ADC_CLOCK_INTERNAL); //internal clock
setup_adc_ports(sAN0 | sAN1); //read port A0 and A1
set_adc_channel(0); //output to port A0
result = read_adc();
delay_us(10);
set_adc_channel(1); //output to port A1
resultACT = read_adc();
delay_us(10);
|
|
I thought this was working ,but its not quit right the AN1's timer is working but AN0 is not. |
|
|
Rez
Joined: 10 Feb 2019 Posts: 26
|
|
Posted: Tue Feb 19, 2019 3:08 pm |
|
|
Rez wrote: | Rez wrote: | Ttelmah wrote: | Then set_adc_channel(0), is not a 'setup'.
It physically connectes AN0 to the ADC.
Once you gave done this, you have to wait for Tacq, and then read the ADC
This will then return the digital value corresponding to the voltage on AN0.
You can then set_adc_channel(1), again wait for Tacq, and then read the
port again to read the value from AN1. |
I got this to work, is this a correct way of doing it, is there a better way?
Code: |
setup_adc(ADC_CLOCK_INTERNAL); //internal clock
setup_adc_ports(sAN0 | sAN1); //read port A0 and A1
set_adc_channel(0); //output to port A0
result = read_adc();
delay_us(10);
set_adc_channel(1); //output to port A1
resultACT = read_adc();
delay_us(10);
|
|
I thought this was working ,but its not quit right the AN1's timer is working but AN0 is not. |
It is working I didn't have one of my jumpers in the correct spot. |
|
|
Rez
Joined: 10 Feb 2019 Posts: 26
|
|
Posted: Tue Feb 19, 2019 3:24 pm |
|
|
Rez wrote: | Rez wrote: | Rez wrote: | Ttelmah wrote: | Then set_adc_channel(0), is not a 'setup'.
It physically connectes AN0 to the ADC.
Once you gave done this, you have to wait for Tacq, and then read the ADC
This will then return the digital value corresponding to the voltage on AN0.
You can then set_adc_channel(1), again wait for Tacq, and then read the
port again to read the value from AN1. |
I got this to work, is this a correct way of doing it, is there a better way?
Code: |
setup_adc(ADC_CLOCK_INTERNAL); //internal clock
setup_adc_ports(sAN0 | sAN1); //read port A0 and A1
set_adc_channel(0); //output to port A0
result = read_adc();
delay_us(10);
set_adc_channel(1); //output to port A1
resultACT = read_adc();
delay_us(10);
|
|
I thought this was working ,but its not quit right the AN1's timer is working but AN0 is not. |
It is working I didn't have one of my jumpers in the correct spot. |
I am driving myself crazy, I thouhgt yet again it was working,but its not, I am so lost. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Tue Feb 19, 2019 4:26 pm |
|
|
this is still wrong
setup_adc(ADC_CLOCK_INTERNAL); //internal clock
...
HOW do you see the 'result'?
you need to post your complete program... |
|
|
arachid
Joined: 15 Oct 2016 Posts: 11
|
|
Posted: Tue Feb 19, 2019 5:07 pm |
|
|
you Don't use the delay_us(10) at right moment, you need to wait after change channel not after read, and you frotgot to indicate the reference.
like this:
setup_adc(ADC_CLOCK_INTERNAL); // adc internal clock
setup_adc_ports(sAN0 | sAN1|VSS_VDD); //read port A0 and A1 analog input
set_adc_channel(0); //input of the adc is on port A0 pin 7
delay_us(10); //wait after change input of adc channel
result = read_adc(); // start and read the adc
set_adc_channel(1); //input of the adc is on port A1 pin 6
delay_us(10); //wait after change input of adc channel
resultACT = read_adc(); // start and read the adc |
|
|
arachid
Joined: 15 Oct 2016 Posts: 11
|
|
Posted: Tue Feb 19, 2019 5:15 pm |
|
|
sorry not need raference if you not use vref
setup_adc_ports(sAN0 | sAN1); //read port A0 and A1 analog input
is ok because
VSS_VDD is 0x0000
them it is by default. |
|
|
Rez
Joined: 10 Feb 2019 Posts: 26
|
|
Posted: Tue Feb 19, 2019 5:18 pm |
|
|
temtronic wrote: | this is still wrong
setup_adc(ADC_CLOCK_INTERNAL); //internal clock
...
HOW do you see the 'result'?
you need to post your complete program... |
I got this code to work.
Please explain to me about this being wrong, thx. setup_adc(ADC_CLOCK_INTERNAL);
Code: |
#include <12F683.h>
#define ACT PIN_A5
#define IN PIN_A3 //Main Input
#use delay(clock = 4000000)
#fuses INTRC_IO,NOWDT,NOPROTECT,NOMCLR
//time 1
int result;
void Time(void)
{
set_adc_channel(0); //output to port A0
result = read_adc();
delay_us(10);
for(int i = 0; i < result; i++)
{
delay_ms(120);
}
}
//time 2
int resultACT;
void TimeACT(void)
{
set_adc_channel(1); //output to port A1
resultACT = read_adc();
delay_us(10);
for(int i = 0; i < resultACT; i++)
{
delay_ms(15);
}
}
void main()
{
//setup_oscillator(0);
setup_adc(ADC_CLOCK_INTERNAL); //internal clock
setup_adc_ports(sAN0 | sAN1); //read port A0 and A1
set_adc_channel(0); //use port A0
set_adc_channel(1); //use port A1
setup_comparator(NC_NC_NC_NC); //turn off comp
delay_ms(1000);
while(TRUE)
{
if(input(IN)) //push button to start time
{
output_high(ACT);
TimeACT();
output_low(ACT);
Time();
output_high(ACT);
TimeACT();
output_low(ACT);
delay_ms(1000);
}
while(input(IN));
delay_ms(50);
while(!input(IN));
delay_ms(100);
}
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Tue Feb 19, 2019 6:11 pm |
|
|
this
setup_adc(ADC_CLOCK_INTERNAL); //internal clock
is still wrong |
|
|
arachid
Joined: 15 Oct 2016 Posts: 11
|
|
Posted: Tue Feb 19, 2019 6:30 pm |
|
|
try:
//time 1
int result;
void Time(void)
{
set_adc_channel(0); //output to port A0
delay_us(10);
result = read_adc();
for(int i = 0; i < result; i++)
{
delay_ms(120);
}
} |
|
|
arachid
Joined: 15 Oct 2016 Posts: 11
|
|
Posted: Tue Feb 19, 2019 6:32 pm |
|
|
try
Code: |
#include <12F683.h>
#define ACT PIN_A5
#define IN PIN_A3 //Main Input
#use delay(clock = 4000000)
#fuses INTRC_IO,NOWDT,NOPROTECT,NOMCLR
//time 1
int result;
void Time(void)
{
set_adc_channel(0); //output to port A0
delay_us(10);
result = read_adc();
for(int i = 0; i < result; i++)
{
delay_ms(120);
}
}
//time 2
int resultACT;
void TimeACT(void)
{
set_adc_channel(1); //output to port A1
delay_us(10);
resultACT = read_adc();
for(int i = 0; i < resultACT; i++)
{
delay_ms(15);
}
}
void main()
{
//setup_oscillator(0);
setup_adc_ports(sAN0 | sAN1); //read port A0 and A1
setup_adc(ADC_CLOCK_INTERNAL); //internal clock
setup_comparator(NC_NC_NC_NC); //turn off comp
delay_ms(1000);
while(TRUE)
{
if(input(IN)) //push button to start time
{
output_high(ACT);
TimeACT();
output_low(ACT);
Time();
output_high(ACT);
TimeACT();
output_low(ACT);
delay_ms(1000);
}
while(input(IN));
delay_ms(50);
while(!input(IN));
delay_ms(100);
}
} |
|
|
|
|