making small functions in AS7

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

Hello guys, I'm despaired I have project at university and deadline is coming soon. It's my first project with atmega168 and atmel. 
what am I doing mainly is that I'm driving 6 PWM but more important for me is 4 of them OCR0A,B and OCR2A,B
So what I want to do is:
I want make small program which increments PWM now what have I done is
for (a=100; a<107; a=a+0.001)
{
   OCR0A=a;
   OCR0B=a;
   b=100;
   OCR2A=b;
   OCR2B=b;

}
for (b=100; b<105; b=b+0.001)
{
   OCR0A=a;
   OCR0B=a;
   a=107;
   OCR2A=b;
   OCR2B=b;

}

 this code works as It should be but problem is that I want those small programs to be written and I call them one by one 
 for ex I'll have 6 small program like that and I have to call in main:1,4,3,5,4,3 or another possible order which will be needed.
Can someone give me an advice how to do this? plus those small programs must communicate with each other 
for ex if I run 4th program firstly and 6th secondly. 6th program must know result of 4th and start working from 4th result numbers.
 I know it's whole problem written here but I hope I can get good advice from people who knows better then me and maybe it's not as big deal as I think.

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

I'll save you some trouble with the above, since the OCR0(a,b) register only hold 8 bit integer numbers, your fractional (floating point) values are truncated to 100,101,102,103, or 104 only!

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

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

ki0bk wrote:

the OCR0(a,b) register only hold 8 bit integer numbers, your fractional (floating point) values are truncated to 100,101,102,103, or 104 only!

 

Jim

 

 

If it's like that, then why it has different speed when I write a=a+1 and a=a+0.001? duty cycle is changing slowly when I'm using 0.001 instead of 1. I'm testing results in Proteus as well as with real motors.

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

levani2828 wrote:
duty cycle is changing slowly when I'm using 0.001 instead of 1

sure it will:  0 0 0 0 0 (100 times) 1 1 1 1 1 1 (100 times) 2 2 2 2 2 2 (100 times)

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

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

Well thanks for warning me, I didn't knew it but still result is acceptable for me as long as motors are doing what I want. but still I have those problem above, I'm searching solutions everywhere I'm not trying to ask and just wait for answer. Mainly I prefer to do all work by myself with no help but now situation is different time is running out.

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

Since you are short on time, the simplest way is to use global variables to share information between the different functions.

That is, variables that are defined outside any function (including outside main). They will keep their values, so you can put your sub-programs in separate functions and call them in the order you need.

 

 

edit: btw, when is the deadline? Don't tell me it's tomorrow... you have at least the weekend, right?

Last Edited: Thu. Jan 10, 2019 - 10:48 PM