|
|
View previous topic :: View next topic |
Author |
Message |
ECACE
Joined: 24 Jul 2006 Posts: 94
|
ADC not working |
Posted: Wed Dec 13, 2006 2:13 pm |
|
|
I have stripped down the code to a simple AD reading and clock it out on two pins to see if it is working...it's not. Everything else on my board is working except the AD. In this case, RA4/AN3 is connected to a photo transistor and I do have a variable voltage at RA4/AN3 if I cover the phot transistor or not. I do have both outputs working (RC0, RC1), but no matter what the voltage is at RA4/AN3, it always clocks out 7F.
Take a look at the code below and smack me in the back of the head, because I am not seeing what I am doing wrong. It's got to be something stupid simple as I have gone over this too many times and still can't see it. Version = 4.018
Thanks in advance for any help you guys can give.
Eric
#include <16F690.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC oscillator enabled
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOMCLR //MCLR will act as IO
#FUSES NOCPD //No EE protection
#FUSES NOPROTECT //Code is not protected from reads
#FUSES NOIESO //No switchover
#FUSES NOFCMEN //No Fail Safe Clock Monitor
#use delay(clock=8000000)
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#define PORTA = 5
#define PORTB = 6
#define PORTC = 7
#define LED PIN_C4 //OUTPUT_HIGH will turn the LED ON
#define SENSOR PIN_A4 //Flag sensor input
#define test_data_clock PIN_A0
#define test_data_data PIN_A1
#define PA3 PIN_A3
#ZERO_RAM
////////////////////////////////////////////////////////////////////////////////
// Variables and Constants //
////////////////////////////////////////////////////////////////////////////////
int flag_sensor = 0; //Value read back from the flag sensor
////////////////////////////////////////////////////////////////////////////////
// Functions //
////////////////////////////////////////////////////////////////////////////////
void setup_periferals()
{
set_tris_a(0b11110100); //7,6,5,4,2 = Inputs 3,1,0 = Outputs
set_tris_b(0b01010000); //6,4 = Inputs, 7,5,3,2,1,0 = Outputs
set_tris_c(0b01000000); //6 = Input << 7,5,4,3,2,1,0 = Outputs
setup_adc(ADC_CLOCK_DIV_32);
setup_adc_ports(sAN3);
set_adc_channel(3); //Entire program will only use ananlog(3)
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1,199,1);
setup_comparator(NC_NC_NC_NC);
setup_ccp1(CCP_PWM);
setup_oscillator(OSC_8MHZ);
}
void ClockOutData()
{
int m=0;
for(m=0; m<=7; ++m) //This section will clock out the A/D value
{ //of the photoq reading.
output_high(test_data_clock); //Start with a Clock High. Valid data will
//be on the falling edge of the clock.
if(flag_sensor,m == 0) //Clock out the A/D value of the photoq
{
output_low(test_data_data);
delay_us(10); //10uS clock time
output_low(test_data_clock); //Clock Low (This is valid data)
delay_us(10); //10uS clock time
}
else
{
output_high(test_data_data);
delay_us(10); //10uS clock time
output_low(test_data_clock); //Clock Low (This is valid data)
delay_us(10); //10uS clock time
}
}
output_low(test_data_clock);
output_low(test_data_data);
}
////////////////////////////////////////////////////////////////////////////////
// Program Start //
////////////////////////////////////////////////////////////////////////////////
void main(void)
{
setup_periferals();
while(TRUE)
{
delay_ms(1);
output_low(LED); //Turn off LED
delay_ms(1);
flag_sensor = read_adc(); //Read the value of the flag sensor
ClockOutData(); //Clock out the received data
output_high(LED); //Repeat with the LED on
delay_ms(1);
flag_sensor = read_adc();
ClockOutData(); //Clock out the received data
}
} _________________ A HW Engineer 'trying' to do SW !!! Run!!! |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Wed Dec 13, 2006 2:42 pm |
|
|
First, use the code button.
Next, your output function looks like the problem.
Try this (untested, just typed it in)
Code: |
// put these at the beginning
#byte PORTA 0x05
#bit CLOCK PORTA.0
#bit DATA PORTA.1
void OutputData(int8 adcdata)
{
int8 m;
CLOCK = 1; // I think you wanted to start clock high
for(m=0;m<8;m++)
{
DATA = bit_test(adcdata,m); // starting with LSB
CLOCK = 0;
delay_us(10);
CLOCK = 1;
delay_us(10);
}
DATA = 1;
CLOCK = 1; // looks like you wanted to exit your function with both high
} |
Like I said, untested but maybe you get the idea.
In your code, I'm not seeing how you step throught he bits of test_data_data, let alone how you are expecting to get the ADC data in there. It is living in flag_sensor due to your line "flag_sensor = read_adc()". _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
ECACE
Joined: 24 Jul 2006 Posts: 94
|
|
Posted: Thu Dec 14, 2006 8:44 am |
|
|
Thank you much. That was it. This is the trouble when a HW only engineer starts trying to do C. School of hard knocks...or smacks in the back of the head. _________________ A HW Engineer 'trying' to do SW !!! Run!!! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|