Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Multiple AD inputs on a PIC 18F4682

Status
Not open for further replies.
You can leave them floating and I did put the wrong setting for ADCON1 set it 0 I should of seen that You don't want tad at 0 that doesn't give any time to charge and take a reading

Good thing you read the datasheet
 
Last edited:
as they say, "If all else fails, read the manual!" ;)

Yep if do that first you wouldn't be here asking for help. I been using a chip that you set the register to "1" on every pin and posted it wrong
What I was meaning is it's better to use binary for your setting so you can see what bits are set

And you can set tad to fast so if your having problems lower it.

And never think you can't do somthing wrong nine times out of ten you did and most of them is with how you hooked up your hardware.;);)
 
Last edited:
Well I have tried everything suggested, (changing TAD, osc speed etc) and also new PIC's . Still can't get the inputs to stop sinking my input voltage though.


Can you guys look at the code again, and tell me your thoughts, is it possibly that I am doing the ADCON 0 result, TRISA and ADCON 1 settings in the wrong order?

to reiterate, I have input voltages of 0-5v on AN 0 to AN8
here is "main.c" code

#include "main.h"

#pragma config OSC = IRCIO67
#pragma config WDT = OFF
//enables the PGM/PGC/PGD pins
#pragma config LVP = OFF
#pragma config PWRT = OFF

#pragma config BOREN = OFF
#pragma config PBADEN = OFF

void InterruptHandlerHigh (void);

long ticks = 0;
long debounce_tick = 0;
unsigned char enter_flag = 0;
unsigned char up_flag = 0;
unsigned char down_flag = 0;
unsigned char draw = 0;
unsigned char current_menu = 2;
unsigned char changing_menus = 0;
char DAC_byte = 0;
BYTE db_store = 10;

//0 - forward power
//1 - reflected power
//2 - MAIN PSU voltage
//3 - Aux PSU voltage
//4 - PA Heat Sink Temp
//5 - PA current
//6 - IPA current
#pragma udata udata5
float v_0;
#pragma udata udata5
float v_1;
#pragma udata udata6
float v_2;
#pragma udata udata6
float v_3;
#pragma udata udata7
float v_4;
#pragma udata udata7
float v_5;
#pragma udata udata8
float v_6;
#pragma udata udata8
float v_7;
#pragma udata udata9
float v_8;
#pragma udata udata9
float v_9;


menu top_menu;
menu power_adj_menu;
menu display_param_menu;

void output_byte_to_DAC(BYTE out)
{
DAC_b8 = (out&0x80)>>7;
DAC_b6 = (out&0x20)>>5;
DAC_b5 = (out&0x10)>>4;
DAC_b4 = (out&0x08)>>3;
DAC_b3 = (out&0x04)>>2;
DAC_b2 = (out&0x02)>>1;
DAC_b1 = (out&0x01);
DAC_b7 = (out&0x40)>>6;
}

void initMCU(void)
{
//set clock to 32 Mhz
OSCCON= 0b01110000;
OSCTUNE = 0b01000000;
INTCON = 0b10100000;
INTCON2 = 0b00000101;
TMR0H = 0; //clear timer
TMR0L = 0; //clear timer
T0CON = 0x80; //set up timer0 - prescaler 1:8

TRISA = 0xEF;
TRISE = 0x07; //direction output
TRISD = 0x00;
TRISC = 0x00;
TRISB = 0xF7;
}

void debounce(void)
{
static int up_button_cycles = 0;
static int down_button_cycles = 0;
static int enter_button_cycles = 0;
if(debounce_tick > 0)
{
//button up logic
if(button_up == 1)
{
up_button_cycles = 0;
}
else
up_button_cycles++;
if(up_button_cycles == 3)
{
up_flag = 1;
}
else if(up_button_cycles > 5)
up_button_cycles = 5;

//button down logic
if(button_down == 1)
{
down_button_cycles = 0;
}
else
down_button_cycles++;
if(down_button_cycles == 3)
{
down_flag = 1;
}
else if(down_button_cycles > 5)
down_button_cycles = 5;

//button enter logic
if(button_enter == 1)
{
enter_button_cycles = 0;
}
else
enter_button_cycles++;
if(enter_button_cycles == 3)
{
enter_flag = 1;
}
else if(enter_button_cycles > 5)
enter_button_cycles = 5;
debounce_tick = 0;
}
}


WORD ADCread(BYTE source)
{
WORD result = 0;
ADCON0 = source << 2;
ADCON0 = ADCON0 | 0x03; //enable A/D convert
while((ADCON0&0x02)!= 0x00);
result = ADRESH;
result = result<<8;
result = result | ADRESL;
ADCON0 = 0;
return result;
}

void ADCinit()
{
//enable most a/d pins and make the voltage reference based on the power pins to the PIC
ADCON1=0x06;
//right justified results
//use a sampling rate of oscilator/32
//us 2 T_ad
ADCON2=0b10101010;
}

void main (void)
{
int read_vals = 0;
int lit = 1;
WORD result;
initMCU();
initMCU_OLED();
oledinit();
ADCinit();
LED = 0;
draw = 1;

top_menu.num_items = 2;
top_menu.selected_item = 0;
top_menu.scrolled_item = 0;
display_param_menu.num_items=9;
display_param_menu.selected_item = 0;
display_param_menu.scrolled_item = 0;
power_adj_menu.num_items = 2;
power_adj_menu.selected_item = 0;
power_adj_menu.scrolled_item = 0;
for(;;)
{
if(ticks > 1)
{
//output_byte_to_DAC(0xAA);
if(read_vals > 10)
{
read_vals = 0;
result = ADCread(0);
v_0 = result/1024.0;
v_0 = (AD0UPPERVAL-AD0LOWERVAL) * (v_0*v_0*AD0QUADRATIC_A + v_0*AD0QUADRATIC_B +

AD0QUADRATIC_C)+AD0LOWERVAL;
result = ADCread(1);
v_1 = result/1024.0;
v_1 = (AD1UPPERVAL-AD1LOWERVAL) * (v_1*v_1*AD1QUADRATIC_A + v_1*AD1QUADRATIC_B +

AD1QUADRATIC_C)+AD1LOWERVAL;
result = ADCread(2);
v_2 = result/1024.0;
v_2 = (AD2UPPERVAL-AD2LOWERVAL) * (v_2*v_2*AD2QUADRATIC_A + v_2*AD2QUADRATIC_B +

AD2QUADRATIC_C)+AD2LOWERVAL;
result = ADCread(3);
v_3 = result/1024.0;
v_3 = (AD3UPPERVAL-AD3LOWERVAL) * (v_3*v_3*AD3QUADRATIC_A + v_3*AD3QUADRATIC_B +

AD3QUADRATIC_C)+AD3LOWERVAL;
result = ADCread(4);
v_4 = result/1024.0;
v_4 = (AD4UPPERVAL-AD4LOWERVAL) * (v_4*v_4*AD4QUADRATIC_A + v_4*AD4QUADRATIC_B +

AD4QUADRATIC_C)+AD4LOWERVAL;
result = ADCread(5);
v_5 = result/1024.0;
v_5 = (attenuator_db_high-attenuator_db_low)*(v_5*v_5*AD5QUADRATIC_A + v_5*AD5QUADRATIC_B +

AD5QUADRATIC_C)+attenuator_db_low;
result = ADCread(6);
v_6 = result/1024.0;
v_6 = (AD6UPPERVAL-AD6LOWERVAL) * (v_6*v_6*AD6QUADRATIC_A + v_6*AD6QUADRATIC_B +

AD6QUADRATIC_C)+AD6LOWERVAL;
result = ADCread(7);
v_7 = result/1024.0;
v_7 = (AD7UPPERVAL-AD7LOWERVAL) * (v_7*v_7*AD7QUADRATIC_A + v_7*AD7QUADRATIC_B +

AD7QUADRATIC_C)+AD7LOWERVAL;
//v_5 is going to be SWR, but it was forward voltage
v_9 = (v_6 + v_7)/(v_6 - v_7);
result = ADCread(8);
v_8 = result/1024.0;
v_8 = (AD8UPPERVAL-AD8LOWERVAL) * (v_8*v_8*AD8QUADRATIC_A + v_8*AD8QUADRATIC_B +

AD8QUADRATIC_C)+AD8LOWERVAL;
// result = ADCread(9);
// v_9 = result/1024.0;
// v_9 = (AD9UPPERVAL-AD9LOWERVAL) * (v_9*v_9*AD9QUADRATIC_A + v_9*AD9QUADRATIC_B +

AD9QUADRATIC_C)+AD9LOWERVAL;

}
controller_update();
menu_draw();
ticks = 0;
read_vals++;
}
debounce();
menu_update();
}
}


#pragma code InterruptVectorHigh = 0x08
void
InterruptVectorHigh (void)
{
_asm
goto InterruptHandlerHigh //jump to interrupt routine
_endasm
}

//----------------------------------------------------------------------------
// High priority interrupt routine

#pragma code
#pragma interrupt InterruptHandlerHigh

void
InterruptHandlerHigh ()
{
unsigned char changes, portbd;
static unsigned char last_portb;

//probably don't need this
if(INTCONbits.RBIF != 0)
{
portbd = PORTB;
INTCONbits.RBIF = 0;
}

//need this
if (INTCONbits.TMR0IF)
{
//check for TMR0 overflow
INTCONbits.TMR0IF = 0; //clear interrupt flag
ticks++;
debounce_tick++;
}
}
 
Looking at the data sheet for the PIC18F4682, on page 131, it says:

10.1 PORTA, TRISA and LATA Registers
PORTA is an 8-bit wide, bidirectional port. The
corresponding data direction register is TRISA. Setting
a TRISA bit (= 1) will make the corresponding PORTA
pin an input (i.e., put the corresponding output driver in
a high-impedance mode). Clearing a TRISA bit (= 0)
will make the corresponding PORTA pin an output (i.e.,
put the contents of the output latch on the selected pin).
Reading the PORTA register reads the status of the
pins, whereas writing to it, will write to the port latch.
The Data Latch register (LATA) is also memory
mapped. Read-modify-write operations on the LATA
register read and write the latched output value for
PORTA.


Looking at examples for simpler PICs there is a specific PORT command = 0 to set the port to an input.
Does the datasheet imply that you only have to set a TRISA to 1 to automatically set the PORTA bit to 0 and the same for the LATA register?

As I said, my inputs still seem to 'look' like outputs (i.e. sink the input voltage to zero) so something is wrong in my configuring of the AD inputs, but I can't work out what it is!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top