This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Collect 8bit data from IO ports

I'm having a problem with collecting data from Port1. the variable ValueX has a value of FF, regardless of my input through the IO pins.With here i enclose partial of my code, any help or advice is highly appreciated, thank you!

sbit P1^0 BIT0; //these 8 ports will be input
.
.
.
.
sbit P1^7 BIT7;

signed char ValueX = 0x00;
.
.
void ReadValue(void);
void main()
{
P1 = 0xFF; //port set high to activate as input
.
.
}

void ReadValue(void) interrupt 0 using 0
{
 .
 .
 .
 ValueX = ValueX | BIT0;
 ValueX = (ValueX <<1) | BIT1;
 ValueX = (ValueX <<1) | BIT2;
 ValueX = (ValueX <<1) | BIT3;
 ValueX = (ValueX <<1) | BIT4;
 ValueX = (ValueX <<1) | BIT5;
 ValueX = (ValueX <<1) | BIT6;
 ValueX = (ValueX <<1) | BIT7;
}
}
i'm sure tat my setting for this ISR works because i had other function inside this ISR that works. But the ValueX doesn't seems to change. It always maintain the value FF. Any help, please.. Thanks

Parents
  • Dear all thanks for the advice. But even after i change ValueX = 0x00; in the ISr, and then change the OR logic to ValueX = P1;
    the value of ValueX is still FF. This is most of my code which i include here(there might be some extra variables). All it does is that it read teh 8 bit data and send it through the TX pin. Any advice please... thanks

    #include <reg52.h>
    
    
    sbit LED = P3^3;
    sbit EXINT = P3^7;
    
    sbit BIT7 = P1^0;
    sbit BIT6 = P1^1;
    sbit BIT5 = P1^2;
    sbit BIT4 = P1^3;
    sbit BIT3 = P1^4;
    sbit BIT2 = P1^5;
    sbit BIT1 = P1^6;
    sbit BIT0 = P1^7;
    
    //char ValueX, ValueY;
    signed int ValueX=0, ValueY;
    
    char mouse_in[3];
    
    unsigned char i;
    char t25ms, seconds;
    unsigned char byte_num=0;
    unsigned char flag = 0;
    unsigned char byte = 0;
    
    
    void TimeDelay (unsigned int milisec);
    
    
    void main (void)
    {
    
    	P1 = 0xFF;
    	P3 = 0xFF;
    	LED = 0;
    
     	TMOD = 0x21; 				// set timer 0 mode  1 (16-bit mode)  timer 1 8 bit auto reload mode
    	TH1 = 0xFD; 				// 9600 baud rate
    	TR1 = 1;
    	SCON = 0X50; 				//mode 1, 8 bit data
    
        TH0 = 0x4C; // 46080 * 6 / 11.0592MHz = 25ms
    	TL0 = 0;
    	t25ms = 0;
    	seconds = 0;
    
    	ET0 = 1; // Enable Timer 0 interrupt
    	ES = 1;
    
    	IT0 = 0;   // External interrupt 0 uses low level triggered
    	EX0 = 1;	//external interrupt 0 enabled
    
    	EA = 1; // Enable overall interrupt
    
    	while(1)
    	{
    	 while(flag == 0);
    
    	  TI = 1;
    
    	}
    }
    
    void SendPacket() interrupt 4 using 2
    {
      TI = 0;
      SBUF = ValueX;
      flag = 0;
    }
    void ReadBits (void) interrupt 0 using 0
    {
    	ValueX = 0x00;
    
    	LED = 1;
    	TimeDelay(50);
    	LED = 0;
    
    	ValueX = P1;
    
    /*
     ValueX = (ValueX | BIT7)<<1;
     ValueX = (ValueX | BIT6)<<1;
     ValueX = (ValueX | BIT5)<<1;
     ValueX = (ValueX | BIT4)<<1;
     ValueX = (ValueX | BIT3)<<1;
     ValueX = (ValueX | BIT2)<<1;
     ValueX = (ValueX | BIT1)<<1;
     ValueX = (ValueX | BIT0);
    */
    
    
      flag = 1;
    
    }
    
    void TimeDelay (unsigned int milisec)
    {
       unsigned int i;
    
       for (i = 1; i <= milisec; i++)
       {
          TH0 = 0xF8;
          TL0 = 0xCD;
          TR0 = 1;
    
          while (!TF0);
          TF0 = 0;
          TR0 = 0;
       }
    }
    
    

Reply
  • Dear all thanks for the advice. But even after i change ValueX = 0x00; in the ISr, and then change the OR logic to ValueX = P1;
    the value of ValueX is still FF. This is most of my code which i include here(there might be some extra variables). All it does is that it read teh 8 bit data and send it through the TX pin. Any advice please... thanks

    #include <reg52.h>
    
    
    sbit LED = P3^3;
    sbit EXINT = P3^7;
    
    sbit BIT7 = P1^0;
    sbit BIT6 = P1^1;
    sbit BIT5 = P1^2;
    sbit BIT4 = P1^3;
    sbit BIT3 = P1^4;
    sbit BIT2 = P1^5;
    sbit BIT1 = P1^6;
    sbit BIT0 = P1^7;
    
    //char ValueX, ValueY;
    signed int ValueX=0, ValueY;
    
    char mouse_in[3];
    
    unsigned char i;
    char t25ms, seconds;
    unsigned char byte_num=0;
    unsigned char flag = 0;
    unsigned char byte = 0;
    
    
    void TimeDelay (unsigned int milisec);
    
    
    void main (void)
    {
    
    	P1 = 0xFF;
    	P3 = 0xFF;
    	LED = 0;
    
     	TMOD = 0x21; 				// set timer 0 mode  1 (16-bit mode)  timer 1 8 bit auto reload mode
    	TH1 = 0xFD; 				// 9600 baud rate
    	TR1 = 1;
    	SCON = 0X50; 				//mode 1, 8 bit data
    
        TH0 = 0x4C; // 46080 * 6 / 11.0592MHz = 25ms
    	TL0 = 0;
    	t25ms = 0;
    	seconds = 0;
    
    	ET0 = 1; // Enable Timer 0 interrupt
    	ES = 1;
    
    	IT0 = 0;   // External interrupt 0 uses low level triggered
    	EX0 = 1;	//external interrupt 0 enabled
    
    	EA = 1; // Enable overall interrupt
    
    	while(1)
    	{
    	 while(flag == 0);
    
    	  TI = 1;
    
    	}
    }
    
    void SendPacket() interrupt 4 using 2
    {
      TI = 0;
      SBUF = ValueX;
      flag = 0;
    }
    void ReadBits (void) interrupt 0 using 0
    {
    	ValueX = 0x00;
    
    	LED = 1;
    	TimeDelay(50);
    	LED = 0;
    
    	ValueX = P1;
    
    /*
     ValueX = (ValueX | BIT7)<<1;
     ValueX = (ValueX | BIT6)<<1;
     ValueX = (ValueX | BIT5)<<1;
     ValueX = (ValueX | BIT4)<<1;
     ValueX = (ValueX | BIT3)<<1;
     ValueX = (ValueX | BIT2)<<1;
     ValueX = (ValueX | BIT1)<<1;
     ValueX = (ValueX | BIT0);
    */
    
    
      flag = 1;
    
    }
    
    void TimeDelay (unsigned int milisec)
    {
       unsigned int i;
    
       for (i = 1; i <= milisec; i++)
       {
          TH0 = 0xF8;
          TL0 = 0xCD;
          TR0 = 1;
    
          while (!TF0);
          TF0 = 0;
          TR0 = 0;
       }
    }
    
    

Children
  • Possibly this is your problem:

    void ReadBits (void) interrupt 0 using 0
    
    I have not taken a look to see what effect this will have, but here are some things you might like to check.

    Generally, the main() thread is executed using register bank 0 and a different register bank is used for each priority level of ISR.

    However, you have this interrupt using register bank 0 too. It is possible that the compiler will assume that it can safely switch to register bank 0 without saving any of the registers of main(). Chaos results.

    Try:
    void ReadBits (void) interrupt 0 using 1
    

  • thanks for the tips. But the condition doesn't change when i change the using 0 to using 1. I still get FF. I think i found the problem. But i can't seems to fix it. Because when i try to send something to SBUF when i activate the external interrupt ISR, the output would be FF no matter what. But when i disable the external interrupt. i could send what ever data i want. for example, when the external interrupt is activated. when i write SBUF = 0x12; it will just show FF. But when i disable the external interrupt. it'll show the correct value(12). why is this so? Any advice is appreciated? Thanks i advance