CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Send ADC result to PORTD (LED pattern)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cxiong



Joined: 09 Sep 2003
Posts: 52

View user's profile Send private message MSN Messenger

Send ADC result to PORTD (LED pattern)
PostPosted: Fri Jul 18, 2003 9:29 pm     Reply with quote

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

View user's profile Send private message

Re: Send ADC result to PORTD (LED pattern)
PostPosted: Sat Jul 19, 2003 2:13 am     Reply with quote

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

View user's profile Send private message MSN Messenger

Re: Send ADC result to PORTD (LED pattern)
PostPosted: Mon Jul 21, 2003 12:20 pm     Reply with quote

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

View user's profile Send private message MSN Messenger

Re: Send ADC result to PORTD (LED pattern)
PostPosted: Mon Jul 21, 2003 12:26 pm     Reply with quote

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)
PostPosted: Mon Jul 21, 2003 3:25 pm     Reply with quote

:=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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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