Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] pwm noob programming

Status
Not open for further replies.

vaka85

Advanced Member level 4
Joined
May 9, 2008
Messages
100
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
2,112
Hi all,

I'm trying to use pwm with the "bit banging" method. I know it's not very efficient, but it's a start..
The code below has a problem, and I'm not able to understand what it is...
I'd like to change the pwm value after some seconds, and so I start the pwm and after 5 seconds I call the function again, but with a different duty cycle value.

When I call the function "pwm start" without
Code:
if(count<500000){
it works fine.
But when I add the condition, pwm_start runs forever, without changing value and never ending, even if the IN4 goes off.

Could you give me any help? thank you

Code:
void pwm_start(long on, long off){
	out3 = 1;
  	for (cntt=0; cntt< on;cntt++);
  	out3 = 0;
  	for (cntt=0; cntt< off;cntt++);
}

void main(void)
{
	Ton = 2500; //2.5 ms
	Toff = 2500; //2.5 ms	  
	count=0;
	
	while (1){
  	
  		if( IN4 == 1) {
  			if(count<500000){
  				pwm_start(3800,1200);
  				count++;
			} else {	
				pwm_start(4999,1);
 			}			
  		} else {
  			count=0;		
  			out3 = 0; 		
		}  		
	}
}
 

I cannot see where you have declared count and as what type. Probably the variable is overflowing if defaulting to int?
 

sorry, my fault... I thought the problem was somewhere else, but it was in the "count<500000"..
500000 is too much, about 40 minutes. sorry, problem solved !
 

Good to see you have it sorted. It is not good practice to not declare variable types as this can lead to all sorts of problems. I am surprised that your compiler did not give an error on this, I guess it must make them 16bit integers by default. If you are new to C, may I suggest looking up about variable types and how to declare them, also about different types, atomic, static etc. It is however generally good practice to initialize variables with a value on declaration as you are doing already. So if you were to use the initial 500000 you had, you would need the next size of variable which is normally 32bit, so you would declare with "long count = 0;" or whatever your compiler uses instead of long, ignore quotes. Other examples include bit,byte,int,float and should precede the variable name on declaration and thereafter just refer to the variable name when using. Hope this helps a little.
 

GrandAlf thank you for your suggestions.

I declared the variable type as long.
I didn't attached the complete code because I thought that this one was enough... But in the header I also declared the variable types..
 

That explains a lot. Could not understand why a compiler would miss such an obvious error. Header files are not usually a good place to put variables unless you need to. If you want them to be available globally putting them in startup before main will normally do the trick. Better to use local atomic variables when you can, this saves memory and make functions more portable if you wish to use them again in another project. Only use global/static when you actually need to, such as flags/semophores etc. As variables that are declared inside functions are only available to that function. C will not care if you use duplicate variable names as long as they are not in the same function. This means that many programmers can work together on the same project and just produce standalone functions that are accessed by passing parameters. A good example is the universal stdio.h you do not need to know how it works, but just what it does and how to use it. Functions could be just regarded as components to use as required. If you have come from say basic then this will seem an alien concept, but it makes perfect sense when you understand the principle.
 
  • Like
Reactions: vaka85

    vaka85

    Points: 2
    Helpful Answer Positive Rating
thank you GrandAlf for your suggestions, very useful!
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top