C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 25th, 2012, 02:59 PM
tightmopedman9 tightmopedman9 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 3 tightmopedman9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 18 sec
Reputation Power: 0
Simple while loop question.

Using AVR-GCC, trying to have my program 'hold' and not process anymore code when a condition is set as true. I'm pretty sure this is how I do it, correct?

while(CONDITION_IS_TRUE);


The program sets an output to low in an interrupt service routine and I want to make sure that the values are not updated to the output until the condition is set back to true.


The ISR:
Code:
ISR(TIMER3_COMPA_vect)
{
	if (n==0)
	{ 
		PWM_off = 1;
		OCR1A_old = OCR1A;
		OCR1B_old = OCR1B;
		OCR1C_old = OCR1C;
		OCR1A = 0;
		OCR1B = 0;
		OCR1C = 0;
	}
	else
	{
		PWM_off = 0;
		OCR1A = OCR1A_old;
		OCR1B = OCR1B_old;
		OCR1C = OCR1C_old;
	}
	if (n==0)
	{
		n++;
	}
	else
	{
		n--;
	}
	
}


The attempt a program 'pause":
Code:
while(PWM_off); //pause here until PWM_off is set to 0
		OCR1A = R * brightness;
		OCR1B = G * brightness;
		OCR1C = B * brightness;

Reply With Quote
  #2  
Old August 26th, 2012, 12:30 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 21 m 53 sec
Reputation Power: 1774
Have you tried
volatile int PWM_off;

If the compiler "knows" the value is 1, and can see no way for the variable to change, it can easily generate code for while(1) instead.

The volatile qualifier tells the compiler to not optimise out apparently redundant accesses to the variable.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old August 26th, 2012, 01:10 AM
tightmopedman9 tightmopedman9 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 3 tightmopedman9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 18 sec
Reputation Power: 0
The variable'PWM_off' is called in an ISR and in main so it is already set as volatile. My question is just: will it hang at the while lopp as long as the condition passed to it is true?

Reply With Quote
  #4  
Old August 26th, 2012, 01:51 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 21 m 53 sec
Reputation Power: 1774
> My question is just: will it hang at the while lopp as long as the condition passed to it is true?
Tell me, have you tried to run this code in the 12 hours since you posted it?

Try it, and post some actual information based on observed results. There is only so much guessing we can do based on incomplete code.

Yes it will "stop" at the while loop until the flag is set to zero, and no it won't stop the ISR from running. Assuming of course you haven't disabled all interrupts somewhere else in the code you haven't posted.

Reply With Quote
  #5  
Old August 26th, 2012, 02:25 AM
tightmopedman9 tightmopedman9 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 3 tightmopedman9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 18 sec
Reputation Power: 0
Quote:
Originally Posted by salem
> My question is just: will it hang at the while lopp as long as the condition passed to it is true?
Tell me, have you tried to run this code in the 12 hours since you posted it?

Try it, and post some actual information based on observed results. There is only so much guessing we can do based on incomplete code.

Yes it will "stop" at the while loop until the flag is set to zero, and no it won't stop the ISR from running. Assuming of course you haven't disabled all interrupts somewhere else in the code you haven't posted.


I have tested it, but in all honesty there isn't much way I can check to see if it is working as intended. I can check for simple glitches in the PWM signal, but they aren't very apparent at ~1000Hz. I could also enable a HID USB listen program to communicate debug messages, but I'm not sure how I would check a no argument while loop.

Reply With Quote
  #6  
Old August 26th, 2012, 06:05 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,808 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 42 m 37 sec
Reputation Power: 1800
Quote:
Originally Posted by tightmopedman9
The variable'PWM_off' is called in an ISR and in main so it is already set as volatile. My question is just: will it hang at the while lopp as long as the condition passed to it is true?


That was not obvious in your question - you should have included the declaration. Normally people post here what they have a problem, so the answers you received addressed the most likley problem from the information given. It seems in fact that you don't have a problem other than you do not know how to effectively test and debug your code.

The best solution would be to use a hardware debugger such as the AVR Dragon for example. Any serious embedded development will be severely hampered without some sort of in-circuit debugger. The problem with using USB for debug I/O is that the necessary device stack is not an insignificant amount of software with a relatively high CPU load requirement, that software itself must be debugged and the load may affect the operation of teh software being monitored. A better ad-hoc approach would be to use UART serial I/O and a cheap USB<->RS232 adapter if your PC lacks a legacy serial port.

A simple way to debug this specific code would be to toggle a GPIO output pin after the wile loop. If the loop never terminates you will see no toggling, if it never waits you will see a very high frequency, and if it all works it will toggle at the PWM frequency (with perhaps some software overhead/jitter). You will of course need an oscilloscope to see that, but again if you are serious about embedded development, that would be standard equipment. If you are on a budget and the frequencies are sufficiently low, a simple PC based USB scope may be sufficient. Prices start very low for devices that work at frequencies of a few KHz, you pay more for more channels and high frequencies.

Finally another method is software simulation. Using an AVR simulator will allow you to test the code on a PC independent of hardware. The simulator will need the ability to emulate the peripheral devices with interrupt generation rather than just a basic instruction set simulator. Ideally it would work with your existing IDE integrated debugger if you are using one. You may already have such a simulator in your existing tool chain.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Simple while loop question.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap