Home > 2008-04 / PSoC > This Entry [com : 3][Tb : 0]

Example
An application uses P0[0] as a sourcing LED output, and P0[1] as a pulled-up input for a normally open switch connected to ground. After setting the drive mode registers for port 0, the firmware initializes the LED to off and enables the input with the following C statement.
PRT0DR = 0x02;
Without a shadow, register, the firmware toggles the LED by the following C statement.
PRT0DR ^= 0x01;
On the initial LED toggle operation, the value read from PRT0DR is 0x00 if the switch is closed, and 0x02 if the switch is open. Performing the XOR operation to toggle the LED results in 0x01 (0x00 XOR 0x01) if the switch is closed but 0x03 (0x02 XOR 0x01) if the switch is open when the initial toggle occurs. Toggling the LED when the switch is closed causes P0[1] to be driven to 0V internally. When the switch is opened, the value read from PRT0DR is still 0x01 because the voltage on P0[1] is still 0V. The switch input has been inadvertently disabled.
The solution is to always manipulate the shadow register first, then copy the shadow register value into the port data register. The following C statements initialize the LED output and the switch input.
Port0_Data_Shade = 0x02;
PRT0DR = Port0_Data_Shade;
The following code toggles the LED without affecting the switch input
Port0_Data_Shade ^= 0x01;
PRT0DR = Port0_Data_Shade;
http://edycube.blog2.fc2.com/tb.php/377-7d856b9c
Comment
情報ありがとうございます。
個人的には、日本語に対応っていうのが結構嬉しかったり。
前からPSoC Express で作ったプロジェクトファイルをDesignerで開くと、SHADOWREGSがぞろぞろ出てきたりしていました。Expressでビルドするとき、レジスタを直接いじるための隠しモジュールだったんでしょうね。
いつから仕込まれていたのやら?
これ嵌りました。って自分の場合は、他のビットでActive-H(Pull−Down抵抗)のSW入力使った設計してしまって、SWとは無関係のところ、PRT0DR ^= 0x01 なんてことしたらSWがONになってしましました。