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 pins on port b to a variable.

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







Reading pins on port b to a variable.
PostPosted: Sat Dec 08, 2001 4:56 pm     Reply with quote

Hi I have a rotary encoder connected to pins b2, b3 on
a f873 pic.

I define the pins as follows...

#define Encoder pin_b2, Pin_b3
//< global variables >
int old;
int new;
int count;

In main I set up my direction register so that pins
b2, b3 are inputs

void main(Void)
{
set_tris_b(0b00001111);

???? How do I place the value of "Encoder" into the variable old.

Also is my method of defining the pins b2, b3 valid or should
it be done some other way.

I tried using input(encoder), old)); and varients of same
but cant seem to get it to work.

Thanks....Paul.




___________________________
This message was ported from CCS's old forum
Original Post ID: 1521
ajt



Joined: 07 Sep 2003
Posts: 110

View user's profile Send private message

Re: Reading pins on port b to a variable.
PostPosted: Sat Dec 08, 2001 10:50 pm     Reply with quote

Hi Paul

I recently did a test program just like this. The following may not be the most elegant way but it works (I'm new and learning this stuff as well)

This is not a full program but just the salient points. Phase A of the encoder is on B0 and phase B is on B4. LEDs are on the C0 and C1 pins.

#INCLUDE <16F877.H>
... etc.
signed int16 count;

#INT_EXT
void isr() {
output_high(PIN_C1);
output_bit(PIN_C0, input(PIN_B4));
if (input(PIN_B4) == 1)
++count;
else --count;
printf("Count = \%04ld \r",count);
output_low(PIN_C1);
}

main() {
count = 0;
set_tris_b(0xFF);
ext_int_edge(0,L_TO_H);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
... etc.


:=Hi I have a rotary encoder connected to pins b2, b3 on
:=a f873 pic.
:=
:=I define the pins as follows...
:=
:=#define Encoder pin_b2, Pin_b3
:=//< global variables >
:=int old;
:=int new;
:=int count;
:=
:=In main I set up my direction register so that pins
:=b2, b3 are inputs
:=
:=void main(Void)
:={
:=set_tris_b(0b00001111);
:=
:=???? How do I place the value of "Encoder" into the variable old.
:=
:=Also is my method of defining the pins b2, b3 valid or should
:=it be done some other way.
:=
:=I tried using input(encoder), old)); and varients of same
:=but cant seem to get it to work.
:=
:=Thanks....Paul.
:=
:=
:=
:=
___________________________
This message was ported from CCS's old forum
Original Post ID: 1523
_________________
Al Testani
Tomi
Guest







Re: Reading pins on port b to a variable.
PostPosted: Sun Dec 09, 2001 2:41 am     Reply with quote

:=???? How do I place the value of "Encoder" into the variable old.
:=Also is my method of defining the pins b2, b3 valid or should
:=it be done some other way.
An example for correct usage of #define:
#define Encoder ((*0x06 >> 2) & 0x03) // shift b2-b3 to b0-b1 and mask it
......
char a;
a = Encoder; // possible values: 0,1,2,3
oldval = Encoder;
___________________________
This message was ported from CCS's old forum
Original Post ID: 1524
Paul H
Guest







Re: Reading pins on port b to a variable.
PostPosted: Sun Dec 09, 2001 6:05 pm     Reply with quote

Hi tomi & ajt..These little rotary encoders are a neat
device but a pain to implement "well for a newbe like myself"

I found a good link to a appnote that shows how to implement
them, check it out below. The appnote is for the pic, but the
code is in asm.

Thanks for the help.. When I get something that works I will
post the source as it might help other members of the group.
I will try and implement tmro 0 as a method of checking the
encoder.

Thanks guys...Paul.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1534
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