View previous topic :: View next topic |
Author |
Message |
cxiong
Joined: 09 Sep 2003 Posts: 52
|
Send ADC result to PORTD (LED pattern) |
Posted: Fri Jul 18, 2003 9:29 pm |
|
|
How do you put the ADC result to PORTD? I have PortD connected to a LED Bar (12 led).
The example only teach you how to send it to the TX for R232 line only.
Please help
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516168 |
|
|
Felix Althaus
Joined: 09 Sep 2003 Posts: 67 Location: Winterthur, Switzerland
|
Re: Send ADC result to PORTD (LED pattern) |
Posted: Sat Jul 19, 2003 2:13 am |
|
|
Hi
If your adc result and portD are 8Bit, simply copy the ad result value (in the ADRES register) to the PortD register. Something like this:
#byte PortD = [hex adress of PortD]
int adc_result;
adc_result = read_adc();
PortD = adc_result;
If you have more than 8Bits you have to use a long for adc_result and you must copy two bytes to the output ports.
mfg
Felix
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516174 |
|
|
cxiong
Joined: 09 Sep 2003 Posts: 52
|
Re: Send ADC result to PORTD (LED pattern) |
Posted: Mon Jul 21, 2003 12:20 pm |
|
|
I have tried it and still not work. Do you have an entire program that is running?
What other include files do I need?
:=Hi
:=
:=If your adc result and portD are 8Bit, simply copy the ad result value (in the ADRES register) to the PortD register. Something like this:
:=
:=#byte PortD = [hex adress of PortD]
:=int adc_result;
:=
:=
:=adc_result = read_adc();
:=PortD = adc_result;
:=
:=If you have more than 8Bits you have to use a long for adc_result and you must copy two bytes to the output ports.
:=
:=
:=mfg
:=Felix
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516226 |
|
|
cxiong
Joined: 09 Sep 2003 Posts: 52
|
Re: Send ADC result to PORTD (LED pattern) |
Posted: Mon Jul 21, 2003 12:26 pm |
|
|
Here is my code:
What do I miss?
Please help
Thanks...
--------------------------------------------------
/////////////////////////////////////////////////////////////////////////
#include <16F877.H>
#use delay(clock=4000000)
#byte PortD = 0x08
int adc_result;
main()
{
setup_adc_ports( RA0_ANALOG );
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );
adc_result = read_adc();
PortD = adc_result;
while(1)
}
------------------------------------------
:=Hi
:=
:=If your adc result and portD are 8Bit, simply copy the ad result value (in the ADRES register) to the PortD register. Something like this:
:=
:=#byte PortD = [hex adress of PortD]
:=int adc_result;
:=
:=
:=adc_result = read_adc();
:=PortD = adc_result;
:=
:=If you have more than 8Bits you have to use a long for adc_result and you must copy two bytes to the output ports.
:=
:=
:=mfg
:=Felix
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516227 |
|
|
R.J.Hamlett Guest
|
Re: Send ADC result to PORTD (LED pattern) |
Posted: Mon Jul 21, 2003 3:25 pm |
|
|
:=Here is my code:
:=
:=What do I miss?
:=
:=Please help
:=
:=Thanks...
:=--------------------------------------------------
First thing. You are 'bypassing' the compiler, and doing direct IO to port C, without having set the TRIS register. Either code the output as:
output_d(adc_result);
which will force the TRIS register to be updated, or code as:
#use fast_io(d)
Then in the main code, use:
set_tris_d(0); //set port d as output
PortD=adc_result;
Second comment, you _must_ have a delay between selecting the AD channel, and reading it. Add a 'delay_us(20);' instruction between these two operations (the actual time needed depends on the impedance of the source you have driving the ADC pin, but the minimum is normally about 10uSec.
Third comment. You are not specifying the accuracy of the data to be returned by the ADC. This is done using:
#device adc=8 near the start of the code. I am not sure what word length the CCS compiler will return without this definition (remember the chip returns a 10bit word). If a 10bit word is being returned, the 'pattern' you see on the LED's, will go from all off, to all on, four times as the voltage changes from 0 to 5v, which could confuse...
Best Wishes
:=/////////////////////////////////////////////////////////////////////////
:=
:=#include <16F877.H>
:=#use delay(clock=4000000)
:=
:=#byte PortD = 0x08
:=int adc_result;
:=
:=main()
:= {
:= setup_adc_ports( RA0_ANALOG );
:= setup_adc( ADC_CLOCK_INTERNAL );
:= set_adc_channel( 0 );
:= adc_result = read_adc();
:= PortD = adc_result;
:= while(1)
:= }
:=
:=------------------------------------------
:=
:=
:=:=Hi
:=:=
:=:=If your adc result and portD are 8Bit, simply copy the ad result value (in the ADRES register) to the PortD register. Something like this:
:=:=
:=:=#byte PortD = [hex adress of PortD]
:=:=int adc_result;
:=:=
:=:=
:=:=adc_result = read_adc();
:=:=PortD = adc_result;
:=:=
:=:=If you have more than 8Bits you have to use a long for adc_result and you must copy two bytes to the output ports.
:=:=
:=:=
:=:=mfg
:=:=Felix
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516230 |
|
|
|