How to debug embedded code

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
I am trying to understand about the debugging process, how the problem can be detected by debugging the code.

For example, let me talk about the code below.

C:
#include <htc.h>
#define _XTAL_FREQ 8000000
void main()
{
  TRISB=0X00;
  PORTB=0X00;
  while(1)
  {
    PORTB=0XFF;
    __delay_ms(1000);
    PORTB=0X00;
    __delay_ms(1000);
  }
}
if the led is not blinking , on which line should I set the break point ?
 

JohnInTX

Joined Jun 26, 2012
4,787
9 and 11
Set the breakpoints and start the code. Verify it breaks at 9.
Inspect the TRIS registers and any other, perpetually missing, rest of the initialization.
Then step to 10. Inspect PORTB to see if it is now FF.
Run to 11 to get through the delay.
Step to 12. Inspect PORTB to see if it is now 00.
RUN. Verify that it loops and breaks at line 9 again.
RUN. Verify that it breaks at line 11 again.
 

402DF855

Joined Feb 9, 2013
271
Well, initially put a breakpoint anywhere from 4 to 12. Prior to main() being invoked there is startup code that, especially on embedded systems, might not work correctly and main() may never be called. In addition to the previous post, beyond inspecting PORTB, get a voltmeter or scope on the pin/LED to make sure the signal toggles between the delays. There are many possibilities, but for instance your delay may not be set up properly. If the delay isn't accurate, there could be seconds or minutes or hours between toggling, or it's toggling so fast you can't see the effect in the LED.
 

AlbertHall

Joined Jun 4, 2014
12,344
Initaially read the datasheet before trying to do it with breakpoints. If this is a PIC then there may be other things that need to be set up in order to use the port. For intsance you may need to set the port for digital rather than analog.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Press push button, LED glows for a second
C:
void main()
{
  TRISD.F0 = 1; //Configure 1st bit of PORTD as input
  TRISB.F0 = 0; //Configure 1st bit of PORTB as output
  PORTB.F0 = 0; //LED OFF
  do
  {
    if(PORTD.F0 == 0)   //If the switch is pressed
    {
       Delay_ms(100);    //Switch Debounce
       if(PORTD.F0 == 0)//If the switch is still pressed
       {
         PORTB.F0 = 1; //LED ON
         Delay_ms(1000); //1 Second Delay
         PORTB.F0 = 0; //LED OFF
       }
    }
 }while(1);
}
The first thing I should know is whether the signal is coming from the button on the microcontroller. If the microcontroller is reading the push button, then I should check the led pin

Where to set break point to check push button ?
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Which chip is this for?
I am not thinking about any specific chip right now. I work with pic16f877a

My objective is to find problem through debugging code. For example if output of a sensor is connected to a microcontroller.
When the sensor is activated, it sends a high (1) to the microcontroller and when the sensor is not active, it send low (0) to microcontroller. If the sensor is working fine but the cable that connecting to sensor and the micro is bad then low value will be found on the Microcontroller pin.

When I debug the code, the value of the pin will be low, so I will understand that there is some problem in the hardware. I will check sensors voltage and voltage on mcu

so I am trying to understand where should I set break point in code
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Because if the input is high, it will not get to line 10 and it won’t break. Pressing the button makes the input low and the code will proceed past line 8 and hit the breakpoint.
Thanks JohnInTX, Now I am looking some complex code

see this https://www.electronicwings.com/pic/pic18f4550-spi

C:
#include <pic18f4550.h>
#include "Configuration_Header_File.h"

#define CS LATA5

void SPI_Write(unsigned char);
void SPI_Init_Master();
unsigned char SPI_Read();
void MSdelay(unsigned int);

void main()
{
    int i;
    OSCCON = 0x72;        /* Use internal frequency 8 MHz */   
    INTCON2bits.RBPU=0;        /* Enable internal Pull-up of PORTB */
    SPI_Init_Master();        /* Initialize SPI communication */
    MSdelay(10);
    
    while(1)
    {
        CS = 0;
        for(i=0;i<=15;i++)    /* Start counter */
        {
            SPI_Write(i);    /* Send counter value to Slave */
            MSdelay(1000);
        }
        CS = 1;
        i=0;
    
    }
}

void SPI_Init_Master()
{
    /* PORT definition for SPI pins*/   
    TRISBbits.TRISB0 = 1;    /* RB0 as input(SDI) */
    TRISBbits.TRISB1 = 0;    /* RB1 as output(SCK) */
    TRISAbits.TRISA5 = 0;    /* RA5 as a output(SS') */
    TRISCbits.TRISC7 = 0;    /* RC7 as output(SDO) */

    /* To initialize SPI Communication configure following Register*/
    CS = 1;
    SSPSTAT=0x40;        /* Data change on rising edge of clk,BF=0*/
    SSPCON1=0x22;        /* Master mode,Serial enable,
                idle state low for clk, fosc/64 */
    PIR1bits.SSPIF=0;

    /* Disable the ADC channel which are on for multiplexed pin
    when used as an input */   
    ADCON0=0;            /* This is for de-multiplexed the SCL
                and SDI from analog pins*/
    ADCON1=0x0F;        /* This makes all pins as digital I/O */   
}

void SPI_Write(unsigned char x)
{
    unsigned char data_flush;
    SSPBUF=x;            /* Copy data in SSBUF to transmit */

    while(!PIR1bits.SSPIF);    /* Wait for complete 1 byte transmission */
    PIR1bits.SSPIF=0;        /* Clear SSPIF flag */
    data_flush=SSPBUF;        /* Flush the data */
}

unsigned char SPI_Read()
{   
    SSPBUF=0xff;        /* Copy flush data in SSBUF */
    while(!PIR1bits.SSPIF);    /* Wait for complete 1 byte transmission */
    PIR1bits.SSPIF=0;
    return(SSPBUF);        /* Return received data.*/   
}

/*************************Delay Function****************************/
void MSdelay(unsigned int val)    /* Delay of 1 ms for 8MHz Freq. */
{
     unsigned int i,j;
        for(i=0;i<val;i++)
            for(j=0;j<165;j++);
}
Now it has a many functions, either of these can be right or wrong . It can either be that the code is correct but the device is damaged.

We have to detect the problem By setting the break point. How to debug the code in which spi being used
 

JohnInTX

Joined Jun 26, 2012
4,787
First you have to understand the code completely including what it should do and what parts of the code do the various functions. Then identify what functions are not working correctly and use your understanding of the code to guide you to the suspect area. From there you debug to see if the code is performing correctly based on your knowledge of how it should work.

But it is essential that you have a solid understanding of the code and how it should flow under various conditions. There aren’t any simple cookbook answers.

You should go back to the SPI. stuff you have already worked on rather than bring up some new thing you may not understand.
 

BobaMosfet

Joined Jul 1, 2009
2,110
I am trying to understand about the debugging process, how the problem can be detected by debugging the code.

For example, let me talk about the code below.

C:
#include <htc.h>
#define _XTAL_FREQ 8000000
void main()
{
  TRISB=0X00;
  PORTB=0X00;
  while(1)
  {
    PORTB=0XFF;
    __delay_ms(1000);
    PORTB=0X00;
    __delay_ms(1000);
  }
}
if the led is not blinking , on which line should I set the break point ?
Did you initialize everything correctly? Where is your flow-chart showing your logic?

I did a flowchart based on your code (sorry, it's primitive)- Your logic looks pretty straightforward:

1582231803424.png

So, if you're not getting the desired result, ask yourself _why_? If it isn't the code in the while(), then is it the code else-where? Again, have you configured the port correctly? If the answer is yes, then you have to look at your circuit? Did you wire it correctly? If yes, are your parts defective?

It's a progression. Eventually you divide and conquer and solve. And learn.

As for breakpoints- you put a breakpoint in wherever you want to stop the program and inspect values, memory, etc. To be sure things are operating as expected- logic-wise.
 
Top