View Single Post
  #3   Spotlight this post!  
Unread 31-01-2008, 12:14
Mark McLeod's Avatar
Mark McLeod Mark McLeod is online now
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,854
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: IR_Sensor Programming

Quote:
Originally Posted by Red Mage View Post
...a mysterious port j. We found where it is in the code but cannot understand how to use it.
Here's the background on the mysterious PORTJ.

The robot system variables we use (e.g., rc_dig_in18) are just aliases for connections or "windows" into the real PIC hardware. In the hardware, 8-bit registers are wired to hold the constantly sampled state from the pins (or the output state we want) and our aliases are just simpler ways to look at individual bits within these registers, one bit per digital I/O pin.

If you look in the file ifi_aliases.h you'll see these aliases defined for our 18 digital inputs:
Code:
#define rc_dig_in01 PORTBbits.RB2 
#define rc_dig_in02 PORTBbits.RB3 
#define rc_dig_in03 PORTBbits.RB4 
#define rc_dig_in04 PORTBbits.RB5 
#define rc_dig_in05 PORTBbits.RB6 
#define rc_dig_in06 PORTBbits.RB7 
#define rc_dig_in07 PORTHbits.RH0
#define rc_dig_in08 PORTHbits.RH1
#define rc_dig_in09 PORTHbits.RH2
#define rc_dig_in10 PORTHbits.RH3
#define rc_dig_in11 PORTJbits.RJ1
#define rc_dig_in12 PORTJbits.RJ2
#define rc_dig_in13 PORTJbits.RJ3
#define rc_dig_in14 PORTCbits.RC0
#define rc_dig_in15 PORTJbits.RJ4
#define rc_dig_in16 PORTJbits.RJ5
#define rc_dig_in17 PORTJbits.RJ6
#define rc_dig_in18 PORTJbits.RJ7
You'll notice all the values we use come through some register, such as:
-- PORTB for the interrupts on digital inputs 1-6
-- PORTH for inputs 7-10
-- PORTJ for inputs 11-13 and 15-18
-- PORTC for input 14

We can use any of these registers we wish or just use the aliases everyone's accustomed to. PORTH also has four pins grouped together that we could easily use by masking out (using the "&" operator) the other upper four bits in that register.

The demo code wants only the last four bits of PORTJ (15-18), so a simple way to eliminate the other unwanted values is to just truncate them by shifting the four bits we want to the right thereby pushing the four bits we don't want out of the variable altogether, e.g., 10001111 >> 4 becomes 00001000

The trick demonstrated with PORTJ allows the code to capture all the pin values at once in an instant of time. It just illustrates the potential window of error that could occur if the IR sensor changed the signals while we were testing them one at a time.

P.S. The excrutiating detail on PORTJ is described in the PIC18F8722_data_sheet.pdf that came on the CBOT compact disk in the KOP.
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle

Last edited by Mark McLeod : 01-02-2008 at 10:23. Reason: tense