Search code examples
carraysfunctionloops8051

C - 8051 Array output


I am getting a very trivial but yet seemingly hard bug in my code to fix. Long story short, I have a an array that gets filled with information as I am reading values of data from a chip. This array called data_buffer then sends every character into the UART in a sort of hex file format for debugging purposes.

When I type the command and the characters are output into the terminal the array is spitting out null characters for spaces in which I specified there be data.

The code is as follows and I will try my best to put it in sequential order:

void read_256_address(void)
{
    char SFRPAGE_SAVE=SFRPAGE;
    SFRPAGE=CONFIG_PAGE;

    RFLAG=0;

    address_sum=((unsigned long)address_byte3<<16)
                +((unsigned long)address_byte2<<8)
                +((unsigned long)address_byte1);

    if(chip_byte>=0 && chip_byte<=76)
    {
        Port4=chip_byte;
        read_address_loop();
    }

    while(RFLAG==0){if(RFLAG==1){break;}}

    SFRPAGE=SFRPAGE_SAVE;
}

void read_address_loop(void)
{
    unsigned long int i=14;

    char SFRPAGE_SAVE=SFRPAGE;
    SFRPAGE=CONFIG_PAGE;

    RFLAG=0;

    if(command_byte==0x82)
    {
        write_P4();
  for(next_address=address_sum;next_address<(address_sum+256);next_address++)
        {
            output_address();
            read_op();
            hex_data=data_read;
            HEXtoASC();
            data_buffer[i]=msb;i++;
            data_buffer[i]=lsb;i++;
            data_buffer[i]=' ';i++;
            if(next_address+=15){format_data_dump();}
        }
   }

void output_data(void)
{
    unsigned char i;

    for(i=0;i<65;i++)
    {
        send_byte(data_buffer[i]);
    }
}

I apologize for the indention on the for loop but that was due to formatting. It should be aligned with the write_P4(); code above it. When the code above runs it outputs the format wanted but not the values, such as:

M82 1B 104560 FF ****null char for remaining indices**** 0x0D 0x0A
M82 1B 104560 FF FF ****null char for remaining indices**** 0x0D 0x0A
M82 1B 104560 FF FF FF ****null char for remaining indices**** 0x0D 0x0A

What I am expecting to see is below:

M82 1B 1040560 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 0x0D 0x0A
M82 1B 1040570 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 0x0D 0x0A
.
.
.
M82 1B 1040650 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 0x0D 0x0A

Could someone please give me some insight as to why this may be happening? It seems like the buffer is increasing in length after each iteration of the entire buffer not the same length every time.


Solution

  • I may not find your issue, but let me put some comments on what I see in your code for you to ponder. Comments will appears above code in question:

    void read_256_address(void)
    {
        char SFRPAGE_SAVE=SFRPAGE;
        SFRPAGE=CONFIG_PAGE;
    
        RFLAG=0;
    

    Assuming address sum is at least 32-bits in size?

        address_sum=((unsigned long)address_byte3<<16)
                    +((unsigned long)address_byte2<<8)
                    +((unsigned long)address_byte1);
    

    If chip_bytes is unsigned, the 0 check is not needed

        if(chip_byte>=0 && chip_byte<=76)
        {
            Port4=chip_byte;
            read_address_loop();
        }
    

    I pray that RFLAG is volatile here otherwise chaos. On a threaded system, a yield or the like would be good here instead of spinning.

        while(RFLAG==0){if(RFLAG==1){break;}}
    
        SFRPAGE=SFRPAGE_SAVE;
    }
    
    void read_address_loop(void)
    {
    

    Very magical 14 and i as a non-index is also not a good idea (oh, I see - used later for indexing.)

        unsigned long int i=14;
    
        char SFRPAGE_SAVE=SFRPAGE;
        SFRPAGE=CONFIG_PAGE;
    
        RFLAG=0;
    
        if(command_byte==0x82)
        {
            write_P4();
    

    If address_sum is close to the size of its type, this could loop forever.

            for(next_address=address_sum;
                next_address<(address_sum+256);
                next_address++)
            {
                output_address();
                read_op();
                hex_data=data_read;
                HEXtoASC();
    

    Consider placing the ++ on the i in the indexing

                data_buffer[i]=msb;i++;
                data_buffer[i]=lsb;i++;
    

    A space for the 3rd byte? That is interesting - guessing this is the part where you are getting the NUL values instead. Perhaps double check your converter to see if it is helping you by converting spaces to 0.

                data_buffer[i]=' ';i++;
    

    Consider using next_address & 0x0f if you are checking for 0 in low nibble, the plus 15 is very strange. Do you perhaps mean plus 16?

                if(next_address+=15){format_data_dump();}
            }
       }
    
    void output_data(void)
    {
        unsigned char i;
    

    Magical 65 size again - consider sizeof() the array or checking for end via another method.

        for(i=0;i<65;i++)
        {
            send_byte(data_buffer[i]);
        }
    }
    

    Of all the stuff I call out above, the if check with the +=15 is the area that seems the most troubling. Again, I don't know if anything of what I have catches your issue, but it is worth looking at them deeper to be sure.