Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Invoking the Debugger seems to hang code in 'setup' (Read 3246 times)
Mike in Michigan
Newbies
*
Offline


Posts: 5
Joined: Dec 5th, 2015
Invoking the Debugger seems to hang code in 'setup'
Jul 19th, 2017 at 2:12pm
Print Post  
Let me start of by stating that I'm not very proficient with the debugger. I've read the tutorials, etc.

Hardware: Arduino Uno
Software: AS7, 7.0.1006
Arduino IDE for Atmel Studio, 1706.25.2

Function of code: Synchronize crank signal count with a position on the camshaft signal. Once synchronized, pulse an output signal every 40 crank pulses. There are 120 crank pulses.

Parts of this code don't work exactly as intended. I want to watch certain registers while it's running. Timer 1 is running on external clock, so I can slow things down to 1Hz if I like.

Specific Problem I'm having:When I invoke the Debugger - with or without any break point - the program hangs at the CAM_SYNC call. If I rem out the CAM_SYNC, then I can debug the rest of the program. The output window says it's "Running", but it isn't. I have LEDs watching output on the board, but all output is low and nothing happens.
If I place a BP at the first line in CAM_SYNC, it's never reached.

There are many settings for the Debugger that I'm not familiar with.  The Debugger used to work fine on this project, but then I've added many functions to it - now it doesn't work. 

Any thoughts or comments are greatly appreciated. Perhaps there are noobie rules that I'm not aware of.

First part of code that hangs in Debugger mode:

Code
Select All
#define ledPin 13
volatile bool irFlag1 = false;
volatile bool compA_flag = false;
volatile bool compB_flag = false;

volatile int TCValue;
volatile int lastTCValue;

int camPulseWidth = 0;
int lastCamPulseWidth = 0;

bool shortCamPulseFound = false;
bool longCamPulseFound = false;
volatile int camEvent = 0;

unsigned long cycleCount = 0;

volatile int TC_cam_check = 0;
volatile int checkCount = 70;

volatile bool cam_ck_flag = false;


void setup()
{
	//Serial.begin(115200);
	pinMode(ledPin, OUTPUT);
	pinMode(6, OUTPUT);
	pinMode(8, OUTPUT); // No Sync indicator
	pinMode(9, OUTPUT); // Sync indicator
	pinMode(11, OUTPUT); // No Cam signal indicator %%% Debugger OK to here %%%




	// %%%%%% IF CAM_SYNC IS LEFT IN, MICRO HANGS AND WON'T PROCESS CAM_SYNC FUNCTION - EVEN WITHOUT BREAK POINTS.
	// %%%%%% IF CAM_SYNC IS REM'D OUT, TIMER1_RUN_SETUP PROCESSES, AND ALL IS WELL.
	CAM_SYNC();	// Set up TC1 in mode 14, use camInt to read TC1 value and sync crank count with cam position.




	// Set up timer for RUN mode.
	TIMER1_RUN_SETUP();



}

void CAM_SYNC()
{
// **************** Initialize Timer1 to Sync the Cam with the Crank Count. **************

noInterrupts();           // disable all interrupts
TCCR1A = 0;				// Clear both control registers.
TCCR1B = 0;
TCNT1  = 0;				// Clear TC1

TCCR1B |= (1 << CS12) | (1 << CS11) ;    // specify external clock

TCCR1B |= (1 << WGM13) | (1 << WGM12);	// Setup for mode 14 - clear at ICR1L value. This is the TOP value, and trips TOV interrupt.

ICR1 = 200;	// Define TOP value for CTC to reset at.


TIMSK1 |=  (1 << TOIE1);  // enable timer interrupt for overflow.
TIMSK1 |=  (1 << ICIE1);  // enable input capture enable interrupt  ***** this is used as the TOP overflow interrupt *****
interrupts();             // enable all interrupts

/*
	********** Description of CAM_SYNC function ***************
	Outer while loop (!irFlag1):
		1)Turn on 'no sync' led.
		2)Ensure longCamPulseFound is false so inner while loop will work.
		3)Calculate pulse width. Cam Interrupt provides new values - if no cam pulse, no new values; counter will reach TOP value and call overflow ISR.
		4)Once a pulse width value falls into the first 'if' condition, start calculating camPulseWidth for second 'if'.
			4a)This if statement needs to be true after the next cam interrupt. If it's not true, the first 'if' was true under erroneous conditions - start the whole process over.
		5)Once the first and second 'if' are true, detach the cam interrupt, clear TC1 (clearing here sets the crank count sync with the cam), and fall out of the function
		  by setting longCamPulseFound and irFlag1 to true.

*/
irFlag1 = false;

CAM_Enable_Rising();


while (!irFlag1)
{	//Serial.println(camEvent);
	PORTB |= (1 << PB0); //Turn on no Sync led. Pin 8.
	PORTB &= ~(1 << PB1); // Turn off Sync led. Pin 9.
	longCamPulseFound = false;
	camPulseWidth = TCValue-lastTCValue;
	Serial.println(camPulseWidth)	;
	if ((camPulseWidth >= 9) && (camPulseWidth <=11))	// Wait until camPulseWidth is between 9 and 11, inclusive.
	{
		camEvent = 0;	// Clear camEvent number.

		while (!longCamPulseFound)
		{

			if ((camPulseWidth >= 28) && (camPulseWidth <=30) && (camEvent < 2))	// Wait until camPulseWidth is between 28 and 30, inclusive.
			{
				irFlag1 = true;	// This will end the first while loop.
				longCamPulseFound = true; // This will end this while loop.
				PORTB |= (1 << PB1); // Turn on Sync led. Pin 9
				PORTB &= ~(1 << PB3);// Turn off 'no cam signal' led.
				PORTB &= ~(1 << PB0);// Turn off no Sync led. Pin 8


				TCNT1 = 0;	// Clear TC1. This is where the count is initiated.

				TIMSK1 &=  ~(1 << ICIE1); // Turn off mask for 'TOP' ISR.

				CAM_Disable();
				cam_ck_flag = false;
			}

			if (camEvent > 1)
			{
				longCamPulseFound = true; // Setting to true will jump out of this loop, but not the first while loop - force it to find the short pulse again.
			}

			camPulseWidth = TCValue-lastTCValue;
		}
	}



}


}

void TIMER1_RUN_SETUP()
{
	// **************** Initialize Timer1 for normal operation. **************

	noInterrupts();           // disable all interrupts
	TCCR1A = 0;
	TCCR1B = 0;
	TCNT1  = 0;

	OCR1A = 1;         // compare match register A
	OCR1B = 11;			// compare match register B



	//TCCR0A |= (1 << WGM01);   // CTC mode
	TCCR1B |= (1 << CS12) | (1 << CS11) ;    // specify external clock


	TCCR1B |= (1 << WGM13) | (1 << WGM12);	// Setup for CTC mode 12 - clear at ICR1L value.
	//	TCCR1A |= (1 << WGM11);	// Add this to the above to set Mode 12, Fast PWM. This mode does not allow update of the OCR1x registers except at the BOTTOM.
	ICR1L = 119;	// Define TOP value for CTC to reset at.


	TIMSK1 |= (1 << OCIE1A) | (1 << OCIE1B) ;  // enable timer compare interrupt for Compare A and B.
	interrupts();             // enable all interrupts

} 

« Last Edit: Jul 19th, 2017 at 2:19pm by Mike in Michigan »  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12071
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Invoking the Debugger seems to hang code in 'setup'
Reply #1 - Jul 19th, 2017 at 2:50pm
Print Post  
Hi,

The default config for the debugger is to use Serial. I think you code is changing the timer and killing the serial.

Can you try setting your project to Release mode, then un-comment your Serial.begin() and add a Serial.println() just after the line that causes the failure.

Does the serial print work?

If not then you need to explore the softwareSerial option but I am not sure if it also uses the same timer.
  
Back to top
WWW  
IP Logged
 
Mike in Michigan
Newbies
*
Offline


Posts: 5
Joined: Dec 5th, 2015
Re: Invoking the Debugger seems to hang code in 'setup'
Reply #2 - Jul 19th, 2017 at 3:28pm
Print Post  
Actually, I've been using Serial.print throughout the program, with success. I placed a Serial.print at the beginning of where it hangs in debug mode, and at the end of it as well - it works in Release.

I tried it with all the Serial statements commented out - didn't work in Debug. I didn't even think about Serial using a timer. I'm using Timer1, and have left Timer 0 and 2 alone.

The code was too large to attach it all, but it's mostly ISRs. If I understand correctly, I can't debug within an ISR - only in the 'setup' and 'loop'. And if i do have a Serial.print, I need to set the baud rate the same as what the debugger uses.

Am I on the right track so far?
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12071
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Invoking the Debugger seems to hang code in 'setup'
Reply #3 - Jul 19th, 2017 at 3:49pm
Print Post  
The baud rate can match your code and is set on the vMicro>Debugger menu

You might be able to debug in ISR but you do need to switch on "vMicro>Debugger>full speed" so that it does not uses delays.



  
Back to top
WWW  
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint