It would help us to help you if you could describe what you are having difficulty with and what kind of help you require.
The pseudo?code papabravo provided is very simple. All you need to do is determine the t value for the next point. It should be a simple matter of adding either the on_time or the off_time to the current "next_edge" everytime you reach one, according to the frequency and duty cycle.
I've never programmed a microcontroller before so I can't provide any specific help for your uC but as it's a matter of software, the solution is pretty simple.
Just creating loop delays between on/off points is an extremely crude way of doing it, but it works. Properly it should be done with an interrupt and a timer. I have no experience with AVR's, but I'm sure there is plenty of sample code for timers out there, and I'm sure AVR FREAKS can help.
Just creating loop delays between on/off points is an extremely crude way of doing it, but it works. Properly it should be done with an interrupt and a timer. I have no experience with AVR's, but I'm sure there is plenty of sample code for timers out there, and I'm sure AVR FREAKS can help.
while I agree with you on the basics, when one first makes it work I think they should start in an organized way that can eventually lead to something fast and elegant. But this may be small enough that a complete re-write isnt a big deal.
I would say:
first understand each prinicpal and skill involved
make a plan
contruct your project
To be honest my normal approach to software is "make it perfect first time and make all code easily and efficiently reusable for the future" , but as I said I've never used a microcontroller. Also my language of choice is java, which is much better for reusability than c.
Just creating loop delays between on/off points is an extremely crude way of doing it, but it works. Properly it should be done with an interrupt and a timer. I have no experience with AVR's, but I'm sure there is plenty of sample code for timers out there, and I'm sure AVR FREAKS can help.
#include <util/delay.h> /* for _delay_ms() */
PORTD |= (1<<PD5); // output 1 on the pin 5 of PORTD
_delay_ms(980); //wait for 980 mS.
PORTD &= ~(1<<PD5); // output 1 on the pin 5 of PORTD
_delay_ms(980); //wait for 980 mS.
and put that in for loop or while loops any how you want to build your wave form. don't forget to set the port's DDR as output!
#include <util/delay.h> /* for _delay_ms() */
PORTD |= (1<<PD5); // output 1 on the pin 5 of PORTD
_delay_ms(980); //wait for 980 mS.
PORTD &= ~(1<<PD5); // output 1 on the pin 5 of PORTD
_delay_ms(980); //wait for 980 mS.
and put that in for loop or while loops any how you want to build your wave form. don't forget to set the port's DDR as output!
First of all i would like to know about the ckt configuration is it a stand alone unit that to generate this pulse alone or the processor has to do other work too. Here i would like to suggest the following me
1. Timer Control
Steps:
(*) Use the maximum frequency that the MCU can support 8Mhz or above
(*) Use one timer that should produce an interrupt for every 10usec
(*) Have four variables Stage(S), Time(T), DutyCycle (DC), Base Time Count (BTC)
(*) Initialize S=0; T=49250(for 985ms/20us),: DC=15(for 80%)
(*) Initialize BTC to zero and run the timer (for 10uSec) and set logic level to "1"
(*) Increment BTC till 19(for total 20usec pulse), change the logic level to "0" when it reaches 15(for 80% CD)
(*) Reinitialize BTC to zero when it counts 20
(*) At the end of each pulse cycle decrement T
(*) At the end of each time cycle change stage (0,1,2,3)
(*) Initialize T=49250; DC=15 for S=0
T=250 ; DC=9 for S=1
T=250 ; DC=0 for S=2 (a minor glitch will appear can be avoided using a LPF)
T=250 ; DC=15 for S=3
(*) Repeat the loop for ever to get the same sequence continuously else stop it when S=4
To be honest my normal approach to software is "make it perfect first time and make all code easily and efficiently reusable for the future" , but as I said I've never used a microcontroller. Also my language of choice is java, which is much better for reusability than c.
I hope your means you are kidding, this is very bad advice, particurlarly in a OO enviroment. Google "how to write bad code" for more info. on this topic. It is often a beginners approach or a sign of hubris, or both. Do not mean to be harsh, just say you were kidding, please.