for loop debugging issue

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the for loop is being skipped.  Does not changes value of PKD_BCD.

for(i = 0; i < 10; i++)
    {
        x = ASCII_1[i] - '0';
        y = ASCII_2[i] - '0';
        
        PKD_BCD[i] = (x & 0x0f) | ((y << 4)& 0xf0);
    }

Attachment(s): 

This topic has a solution.
Last Edited: Fri. Dec 24, 2021 - 04:56 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What you are observing may depend upon:

  • where in your code execution you are looking at the contents of PKD_BCD (To see its content using the simulator, PKD_BCD must be in scope.)
  • how PKD_BCD is defined
  • whether the compiler has optimized it away.

 

 

David

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

welcome to AVRFreaks.

 

Note that you can put the image in the post - where we can see it:

 

 

See Tip #1 in my signature, at the foot of my posts, for how to do that.

 

'Unknown location' most likely means that the whole thing is being optimised away[1] - do you ever do anything with the x, y, or PKD_BCD variables ?

 

Please post a minimum but complete program which illustrates the issue - Tip #1 also shows how to post source code.

 

On optimisation: https://www.avrfreaks.net/forum/tutcoptimization-and-importance-volatile-gcc (note that, although it focuses on GCC, the principles are general)

 

EDIT

 

[1] Or, as  frog_jr said, it is out-of-scope at the point you tried to examine it. 

 

This is why it's important to see the code in context.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Dec 21, 2021 - 09:31 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#include <avr/io.h>

int main(void)
{
    unsigned char ASCII_1[] = {'9','8','7','6','5','4','3','2','1','0'};
    unsigned char ASCII_2[] = {'0','1','2','3','4','5','6','7','8','9'};
    unsigned char PKD_BCD[10];
    
    unsigned char x, y;
    int i;
    
    
    for(i = 0; i < 10; i++)
    {
        x = ASCII_1[i] - '0';
        y = ASCII_2[i] - '0';
        
        PKD_BCD[i] = (x & 0x0f) | ((y << 4)& 0xf0);
    }
    
    return 0;
}

This is my complete code. What do you mean by in-scope???

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You didn't read the tip, then?

 

So that code never does anything with x, y, or PKD_BCD - so it most likely will be all optimised away.

 

Also note that you mustn't return from main() - the microcontroller has nowhere to return to.

 

 

 What do you mean by in-scope???

C is a block-structured language - things defined within a block are not visible outside that block.

 

https://en.cppreference.com/w/c/language/scope

 

Here are some C learning & reference materials - including a free online textbook: https://blog.antronics.co.uk/201...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Dec 21, 2021 - 11:02 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please try my modified code:

 

#include <avr/io.h>

int main(void)
{
    unsigned char ASCII_1[] = {'9','8','7','6','5','4','3','2','1','0'};
    unsigned char ASCII_2[] = {'0','1','2','3','4','5','6','7','8','9'};
    volatile unsigned char PKD_BCD[10]; //volatile ensures compilation
    
    unsigned char x, y;
    int i;
    
    
    for(i = 0; i < 10; i++)
    {
        x = ASCII_1[i] - '0';
        y = ASCII_2[i] - '0';
        
        PKD_BCD[i] = (x & 0x0f) | ((y << 4)& 0xf0);
    }
    while (1) {
        asm("nop");       //tread water
    }    
    return 0;             //never get here
}

 

Place a Breakpoint on the for() loop and a Breakpoint on the NOP statement.   Run the Simulator in AS7.0.

View the PKDBCD[] array in hex e.g. as Locals

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I covered something almost identical to the issues in this thread in this one from Sunday.. 

 

https://www.avrfreaks.net/forum/breakpoints-not-hit