Dear All, I've tried to read the pin of Port1 , but i failed . I should write one to it right ? , how do i read a one again ? If i tried to write the following code,it works without checking the status of the pin Port1_3=1; if(Port1_3==1) { // execute code here .. // code here executes in the simulator //without checking if the pin is //high or not } thanks in advance
Look at the description of the port operation in chapter 3 of the so-called "bible" for the 8051: Chapter 3 - 80C51 Family Hardware Description: http://www.semiconductors.philips.com/acrobat/various/80C51_FAM_HARDWARE_1.pdf Page 3 describes what happens when you write a '1' to a port bit; now think about what happens when an external circuit tries to force that to '0' or '1'... The so-called "bible" is absolutely fundamental to your understanding of the 8051 and its derivatives; the other 2 chapters are: Chapter 1 - 80C51 Family Architecture: http://www.semiconductors.philips.com/acrobat/various/80C51_FAM_ARCH_1.pdf Chapter 2 - 80C51 Family Programmer's Guide and Instruction Set: http://www.semiconductors.philips.com/acrobat/various/80C51_FAM_PROG_GUIDE_1.pdf You should download these, familiarise yourself with their content, and keep them constantly to hand for reference!
thanks , Neil Kurzman yes , i mean P1^3. Andy , thanks , i read them but i still didnt figure out how can i read an input 1 on the pin :(.
You just read it. If the externally-applied signal is 1, you read 1; it it's zero, you read zero. Think about this: The standard 8051 IO ports & pins don't have direction registers (unlike some other processors). The output "latch" has to be set to something, and that can only be 0 or 1 So, which are you going to choose - will it be 0, or will it be 1? What would happen if you chose 0?
Thanks Andy , If the externally-applied signal is 1, you read 1 yea ,that's what i need , but when writting if ( test pin ==1) in Keil C51 the next expression is executed whatever happens .. there's no checking occurs e.g if ( P1^3==1) { P2=0x00; // P2 will be zeros in //the simulator , cuz P1^3 is initially //set to 1 , so how do i check then for // a 1 again ?? }
You are using the '^' incorrectly!
if ( P1^3==1)
sorry am newbie :- its supposed to be sbit P1_3=P1^3; if ( P1_3==1) { // execute ... }
its supposed to be sbit P1_3=P1^3; NO, do not use Px.y in code, only in definitions. use something like sbit SBG_P1_USB_SSI = 0x96; // USB SSI a surefire way (TCON as an example): sbit SB_TCON_TF1 = 0x8F; sbit SB_TCON_TR1 = 0x8E; sbit SB_TCON_TF0 = 0x8D; sbit SB_TCON_TR0 = 0x8C; sbit SB_TCON_IE1 = 0x8B; sbit SB_TCON_IT1 = 0x8A; sbit SB_TCON_IE0 = 0x89; sbit SB_TCON_IT0 = 0x88; This avoid ALL confusion. I like to remove confusion, that keeps me more productive. I find using SB_TCON_ in the definitions very helpful (a global search on TCON gives all uses whether bit set ot SFR load) but, if you find that distasteful, the method works with any naming convention. Erik
" ... its supposed to be ... " 1st law of programming: the compiler will do what you tell it to do; it will not attempt to second-guess what you actually wanted it to do!! So, if you actually write
sbit P1_3=P1^3; if ( P1_3==1) { // execute ... }
sbit door_open = P1^3; if ( door_open ) { // execute ... }
thanks erik. I tried to use this code and it works in the simulator, could you tell me what's ur opinion about it ? : void main (void) { pin_4=1; while(1) { while (pin_4 !=1) { P2=0xFF; DELAY_LOOP_Wait(90); if ( pin_4==1) { p++; if ( p==5) { P2=0x00; } } } }