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 CCS Technical Support

Reading a Digital input

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



Joined: 24 Oct 2010
Posts: 10

View user's profile Send private message

Reading a Digital input
PostPosted: Wed Nov 10, 2010 11:37 pm     Reply with quote

I have a PIC16F877A, I want to read the digital input D0-D7 coming in from a separate adc chip. I know I can set the port C of the pic to be an input by:

SET_TRIS_C(0x00);

However, how do I transform each bit (or the byte) so they can be displayed in a print statement.

Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 11, 2010 12:20 pm     Reply with quote

Quote:
I know I can set the port C of the pic to be an input by:

SET_TRIS_C(0x00);

Not true. That sets Port C to be all outputs. With CCS, you don't have
to set the TRIS anyway. The compiler will do it for you, provided that
you use the built-in CCS functions to do the i/o.

Quote:

I want to read the digital input D0-D7 coming in from a separate adc chip.

What's the manufacturer and part number of the ADC chip ?
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Thu Nov 11, 2010 2:25 pm     Reply with quote

The pattern on a digital port in your case C can be transferred to a variable
var=input_c() where var is an 8 bit unsigned integer and input_c refers to your port c.
To the PIC this information is internally notated as binary and takes on values 0..255(dec).
Now this value has an external notation representing some voltage with a voltage range with 256 stepped increments. The PIC knows nothing about this it is just 8 bits to the PIC. Next you want to change notation and want printf to reinterpret these 8 bits. Now you have a choice you could ask printf to renotate the 8 bits as ascii notated hexadecimal or maybe ascii notation in base ten or simply send it out in its currrent internal PIC notation ( binary)
Look at the manual and see the choices you have for printf.
Often you want the printf to notate your ADC value as a voltage ex 3.9v
Suppose 255 from your ADC notates for 10v and 0 for 0 volts. Then you want the pic to do a calculation so printf is supplied with the binary notated value of 100 (dec) when var is 255(dec)... 50(dec) when 127(dec). Now you need a 16bit unsigned variable var1=(var*100)/256 ( the compiler will optimize this and do a shift). Now with printf you want it to renotate the 16 bit binary as ascii decimal with an implied decimal pt the printf documentation will show you how to do it. Now this example only allows 100 values 0.0 v to 10.0v but you should be able to modify it to suit your needs
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