Strange Parallel Port behavior

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Strange Parallel Port behavior

345 Views
davila
Contributor I

Hi, guys,

 

I'm stuck with a strange Port Behavior. The code is so simple, I can't figure out what could be wrong. Please enlighten me! I'm using a MC9S08AC32 controller, programming in C - Codewarrion 10.5.

 

Take a look at this smple code:

 

PTCD_PTCD0 = 0;     // Initializing 2 port pins:

PTCD_PTCD1 = 0;     // Pin PTC0 to Output, 0

PTCDD_PTCDD0 = 1;   // Pin PTC1 to Input, 0 latched (but not output)

PTCDD_PTCDD1 = 0;


PTCDD_PTCDD1 = 1;   // Setting PTC1 to Output, latched value (zero) goes to pin (AS EXPECTED)

PTCDD_PTCDD1 = 0;   // Setting PTC1 to Input, pin goes High (I have pullup) (AS EXPECTED)

PTCDD_PTCDD1 = 1;   // Same Action, Same Result (AS EXPECTED)

PTCDD_PTCDD1 = 0;   // Same Action, Same Result (AS EXPECTED)

 

Now the surprising part. Look at this:

 

PTCD_PTCD0 = 0;     // Initializing 2 port pins:

PTCD_PTCD1 = 0;     // Pin PTC0 to Output, 0

PTCDD_PTCDD0 = 1;   // Pin PTC1 to Input, 0 latched (but not output)

PTCDD_PTCDD1 = 0;


PTCDD_PTCDD1 = 1;   // Setting PTC1 to Output, latched value (zero) goes to pin (AS EXPECTED)

PTCDD_PTCDD1 = 0;   // Setting PTC1 to Input, pin goes High (I have pullup) (AS EXPECTED)

PTCD_PTCD0 = 1;     // Setting the PTC0 Pin High (nothing to do with the PTC1, right?

PTCDD_PTCDD1 = 1;   // Ohooo... !!! Pin PTC1 does NOT go Low, as expected! It seems a "1" Value crept into the latch, the pin goes HIGH!

PTCDD_PTCDD1 = 0;

 

This happens either testing the code on chip, or in Full Chip Simulation.

I must confess I can't figure out what is happening here. I'm astonished to be stuck in so simple a piece of code.

Any ideas?

Labels (1)
Tags (1)
0 Kudos
1 Reply

228 Views
kef2
Senior Contributor IV

Ugh. Pins are not addresseable! Even though you write PTCDD_PTCDD1 = 1, whole PTCDD byte gets written. CPU is told to 1) read PTCDD, 2) modify required bit (PTCDD1) in PTCDD bit pattern and 3) write modified bit pattern back to PTCDD. You can't modify one bit without rewriting whole byte. Even CPU instructions like BSET or BCLR involve all 3 steps listed above: read byte, modify it, write byte back.

Fixing your comments:

// assuming PTCDD=0 here

PTCD_PTCD0 = 0;     // latch PTCD0=0

PTCD_PTCD1 = 0;     // 1) latch PTCD1=1. 2) ^^ previous line effect is abandoned,

                    // PTCD0 is now latched with value supplied externally to PTCD0 input!

To make it working as expected, either 1) init whole PTCD, or 2) set pin direction to output after each single pin latch.

0 Kudos