|
|
View previous topic :: View next topic |
Author |
Message |
zonemikel
Joined: 13 Oct 2007 Posts: 53 Location: Texas
|
read_adc() what is it returning ? pic16f877a |
Posted: Mon Oct 15, 2007 10:13 am |
|
|
I know my analogue input is outputting about .70V if i make a program that reads that value and stores it in a variable how can i convert that variable to a normal int that would be 70? Do I need a Vref or will the pic just use its input voltage ?
Code: |
void main()
{
//adc setup
set_tris_a(0b00000001); //make only pins A0 inputs. 1=input, 0=output.
setup_ADC_ports(RA0_ANALOG); //Make PIN_A0 the only analog input.
//Set the clock source for the ADC. Call with ADC_OFF if ADCs are unused.
setup_ADC(ADC_CLOCK_INTERNAL); //Use the PIC's internal RC oscillator.
set_tris_d(0b11110000);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
// no comparator (voltage) NO voltage reference
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
set_ADC_channel(0); //Which pin to read from? In this case, AN0, aka PIN_A0.
int adcval;
adcval =read_adc();
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 15, 2007 10:25 am |
|
|
Here's a program that may help:
http://www.ccsinfo.com/forum/viewtopic.php?t=32168&start=1
Quote: |
set_ADC_channel(0);
int adcval;
adcval =read_adc();
} |
This code won't compile. You can't declare variables within the program
in CCS. Try it. Compile it with vs. 4.058. You'll get an error. |
|
|
zonemikel
Joined: 13 Oct 2007 Posts: 53 Location: Texas
|
Let me post the real code and not a knock up |
Posted: Mon Oct 15, 2007 11:52 am |
|
|
Well its working now, after about a week of banging my head against the wall my lm34 is outputing .42 volts when its 79F so there is either something wrong with the lm34 or the way its setup. but the pic will output almost the exact voltage to the 7 seg led display using this code. I had to divide by 4 because my voltage is ~4v to the pic. Anyway this is my code if you have any recommendations. Thanks a bunch, this was also the reason i wanted to do serial communication just to see what adc_value was. I kept assuming the output voltage from the lm34 was .7v which is what i measured it to be a while back.
Code: | /******************************************************************************
/ Program for a pic 16F877a to take analoge input from RA0 in the range
/ of 0- ? volts and display it to a three digit 7 segment Led display. The
/ display is multiplexed and the common cathode/anode is controlled by
/ port E and the actual hex of the number is sent through port B
/
/ by, Michael
/ 10-3-07
/ www.zonemikel.com
/*****************************************************************************/
#include "C:\Documents and Settings\zonemikel\My Documents\877a.h"
#include <stdio.h>
#include <stdlib.h>
// declare functions
void findnumbers(int temp);
void sendhex(int number, int digit);
void displaynumbers(int hundreds, int tens, int ones);
int getavgtemp();
void main(void)
{
//declartations
int16 adc_value;
float volts;
int temp;
int i=0;
//adc setup
set_tris_a(0b00000001); //make only pins A0 inputs. 1=input, 0=output.
setup_ADC_ports(RA0_ANALOG); //Make PIN_A0 the only analog input.
setup_ADC(ADC_CLOCK_INTERNAL); //Use the PIC's internal RC oscillator.
set_tris_d(0b11110000);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
// no comparator (voltage) NO voltage reference
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
printf("starting while loop");
// TODO: USER CODE!!
while(1)
{
// turn on led so i can see im in main
output_high(PIN_D3);
// read value
adc_value = read_adc();
volts = (float)(adc_value * 4)/1023.0; // convert to volts
temp = (volts*100); // if volts is ~.70 multiply by 100 to get temp
// since .1V = 10 degrees F
//display value on 7 seg led 200 times to give delay between readings
for(i=0; i<200; i++)
{
findnumbers(temp);
}
// send to serial
printf("Volts: %f", volts);
output_low(PIN_D3); // turn of led to see i exited main
}
}
// function to find number
void findnumbers(int temp) // get hundreds tens ones (for seven seg display)
{
int hundreds = 0; // hundreds position
int tens = 0; // tens position
int ones = 0; // ones position
output_high(PIN_D0); // lite led to tell me im in findnumbers
if (temp < 200) // temp should never be over 200
{
hundreds = (temp/100)%(10);
tens = (temp/10)%(10);
ones = (temp%10);
}else{hundreds, tens, ones = 2;}
displaynumbers(hundreds, tens, ones);
}
// this converts a number to the desired high/low combination
// for the 7 seg led display
void sendhex(int number, int digit)
{
output_high(PIN_D1);
output_e (0x00);
// send hex to port b to make number
if (number == 0){output_b (0xC0);} // output 0 to port b
if (number == 1){output_b (0xF9);} // 1
if (number == 2){output_b (0xA4);}
if (number == 3){output_b (0xB0);}
if (number == 4){output_b (0x99);}
if (number == 5){output_b (0x92);}
if (number == 6){output_b (0x82);}
if (number == 7){output_b (0xF8);}
if (number == 8){output_b (0x80);}
if (number == 9){output_b (0x98);}
if (number == 10){output_b (0x88);} // A
if (number == 11){output_b (0xC6);} // C
if (number == 12){output_b (0x8E);} // F
if (number == 13){output_b (0x8C);} // P
if (number == 14){output_b (0xC1);} // U
//turn on the digit(send high to common cathode via transistor)
if (digit == 1){output_high(PIN_E0);}
if (digit == 2){output_high(PIN_E1);}
if (digit == 3){output_high(PIN_E2);}
output_low(PIN_D1);
}
// this just sends the right number to the right digit if its less than
// 100 it will use the extra digit to display a F eg. 45F
void displaynumbers(int hundreds, int tens, int ones)// send to led
{
int i = 0;
output_high(PIN_D2); // light led to tell me im in this function
// if its more than 100 display eg. 102 else display eg. 67F
if(hundreds > 0)
{
sendhex(hundreds, 3);
sendhex(tens, 2);
sendhex(ones, 1);
}else{
sendhex(tens, 3);
sendhex(ones, 2);
sendhex(12, 1); // 12 is F
}
output_low(PIN_D2);
}
| [/code] |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Oct 15, 2007 12:14 pm |
|
|
Beware that your LM34 may be oscillating, which causes a DC measurement to be wildly off. Read the data sheet about bypass caps and capacitive loading. It has been years since I used one of those and I remember oscillation was a problem.
If you only have a DVM you can still set it to measure AC and it should read close to zero. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
|
|
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
|