Strange Parallel Port behavior

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Strange Parallel Port behavior

352件の閲覧回数
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?

ラベル(1)
タグ(1)
0 件の賞賛
1 返信

235件の閲覧回数
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 件の賞賛