View previous topic :: View next topic |
Author |
Message |
edbfmi1
Joined: 18 Jul 2006 Posts: 103
|
Setup Multiple ADC Ports |
Posted: Thu May 26, 2011 10:57 am |
|
|
I am using PCM Version 4.089
I am converting code from a PIC16F73 chip to run on a PIC16F723A.
The old code used the following to setup the analog ports:
Code: | SETUP_ADC_PORTS(AN0_AN1_AN3); |
Pins A2 and A5 are digital outputs and Pin A4 is not used.
When it gets to this line I get the following error.
Undefined identifier AN0_AN1_AN3
I went to the PIC16F723.h file and see that it references the analog ports as sAN0 sAN1 etc. so I changed the code to the following
Code: | SETUP_ADC_PORTS(sAN0_sAN1_sAN3); |
I get the same "Undefined identifier" error message
Undefined identifier sAN0_sAN1_sAN3
The last thing I tried was the following
Code: | SETUP_ADC_PORTS(sAN0|sAN1|sAN3); |
This did not give a compile error but the analog inputs are not being read properly.
Any help on what I am doing wrong would be appreciated.
Thanks |
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Thu May 26, 2011 12:23 pm |
|
|
How do you read them, this is the correct setup method.
Ron |
|
|
edbfmi1
Joined: 18 Jul 2006 Posts: 103
|
|
Posted: Thu May 26, 2011 12:48 pm |
|
|
Hi Ron,
They are read in the following subroutine
Code: | void read_pots() // Get current pot values
{
SETUP_ADC(adc_clock_div_8);
set_adc_channel(0); // Hardness Pot Analog Channel 0
delay_ms(2);
temp_ad = read_adc();
delay_cycles(1);
delay_cycles(1);
hard_in = temp_ad;
hard = temp_ad*7; // Soft to Hard adjustment = 0 to 1275uSec
SETUP_ADC(adc_clock_div_8);
set_adc_channel(3); // Delta Pot ("Offtime") Analog Channel 3
delay_ms(2);
temp_ad = read_adc();
delay_cycles(1);
delta_in = temp_ad;
delta = 0;
hard_in = ~hard_in; //The following lines fit a variable delta
delta = hard_in; //relative to the hardness setting
delay_cycles(1);
delta = delta*delta;
delay_cycles(1);
delta >>= 8; //Divide by
delta = delta + 1; //255
delay_cycles(1);
delta = delta*3;
delay_cycles(1);
delta >>= 1; //Divide by
delta = delta + 1; //2
delay_cycles(1);
delta = delta + delta_in;
delay_cycles(1);
if (delta <= 4) delta = 4; // delta must be >0
delay_cycles(1);
onethirddelta = delta/3;
delay_cycles(1);
twothirddelta = onethirddelta;
twothirddelta <<= 1;
delay_cycles(1);
total = fixed_os + offset + hard + delta; // total hardness band
delay_cycles(1);
compoff = fixed_os + offset + hard; // comprressor off value
} |
Thanks,
Ed |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu May 26, 2011 1:01 pm |
|
|
The setup_adc_ports() function is messed up in your compiler version.
You need to setup the registers manually. |
|
|
edbfmi1
Joined: 18 Jul 2006 Posts: 103
|
|
Posted: Thu May 26, 2011 1:21 pm |
|
|
Do you know if it is not "messed up" on the latest version of the PCW compiler? I am looking at upgrading from the PCM to PCW. |
|
|
edbfmi1
Joined: 18 Jul 2006 Posts: 103
|
|
Posted: Thu May 26, 2011 1:29 pm |
|
|
Hi PCM programmer,
I guess I really need to ask if the latest version of PCM has the setup_adc_ports() function fixed. I will get the latest version of the PCM compiler if it is corrected since I use MPLAB IDE.
Thanks,
Ed |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu May 26, 2011 3:19 pm |
|
|
The generated code looks OK for compiler vs. 4.121. There is a minor
bug in the 16F723A.h file. Notice that it's missing the bitmask for sAN1.
The top byte should be 0x02 for sAN1 below:
Code: |
#define sAN0 0x01000000 // A0
#define sAN1 0x00000000 // A1 <=== Missing the bitmask
#define sAN2 0x04000000 // A2
#define sAN3 0x08000000 // A3
#define sAN4 0x20000000 // A5 |
But you can easily edit the .h file and fix that. I'll email CCS and tell
them about the problem.
There is another small problem in the compiler start-up code. They
write 0x00 to RAM addresses 0x188 and 0x189. That's for ANSELD
and ANSELE, but those registers don't exist on the 16F723A. So it's
a bug, but it doesn't matter. There's no SFR register there, so writing
0x00 to it doesn't hurt anything. I'll tell CCS about that one too.
I reviewed the .LST file and it looks OK, but it's possible that I made
some mistake. In other words, I don't work for CCS, so I don't absolutely
guarantee that your code will run on this PIC.
Here's the test file that I used:
Code: |
#include <16F723A.h>
#fuses INTRC_IO,NOWDT,NOPLLEN,VCAP_A6
#use delay(clock=4000000)
//============================
void main()
{
int8 result;
setup_adc_ports(sAN0 | sAN1 | sAN3);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
delay_us(10);
result = read_adc();
set_adc_channel(1);
delay_us(10);
result = read_adc();
set_adc_channel(3);
delay_us(10);
result = read_adc();
while(1);
} |
|
|
|
edbfmi1
Joined: 18 Jul 2006 Posts: 103
|
|
Posted: Thu May 26, 2011 4:32 pm |
|
|
Thank you PCM programmer!
I hope you get some sort of "show of appreciation" from CCS for all the effort you put into these forums.
I will take a look at what you found in the morning when I get back into the office.
Thanks again. |
|
|
edbfmi1
Joined: 18 Jul 2006 Posts: 103
|
|
Posted: Fri May 27, 2011 1:02 pm |
|
|
PCM programmer - You are a Rock Star!!!!
I downloaded the latest PCM version 4.121 and tweaked the PIC16F723a.h file like you said and it worked perfect.
Thanks again for all you help.
(CCS if you monitor this forum you need to put PCM programmer on your payroll ) |
|
|
|