• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

how can write C code for make this waveform?

Status
Not open for further replies.

Papabravo

Well-Known Member
One statement at a time where each statement is one of the following:
OUT = 0 ;
while(t < next_edge) ; // do nothing
OUT = 1 ;

unless you know something we don't and have a magic Ace up your sleeve.
 
Last edited:

giftiger_wunsch

New Member
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.
 
Last edited:

DirtyLude

Well-Known Member
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.
 

Papabravo

Well-Known Member
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.
We agree on the basics.
  1. First make it work
  2. Then make it fast
  3. Then make it elegant
It is usually a good idea to learn to crawl before attempting a marathon.
 
Last edited:

Triode

Active Member
First make it work
Then make it fast
Then make it elegant
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
 

giftiger_wunsch

New Member
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" :D, 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.
 
Last edited:

be80be

Well-Known Member
One statement at a time where each statement is one of the following:
OUT = 0 ;
while(t < next_edge) ; // do nothing
OUT = 1 ;

unless you know something we don't and have a magic Ace up your sleeve.
This would be the best way to start. It will help you with flow.
You learn flow and you'll write good code.
 
Last edited:

swallow_159

New Member
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.
ok but becoze they r very fast pulse with high frequency I think maby when AVR want exert the prgram line the 4 u sec past
 

ikalogic

Member
Hello,

you can use the _delay_ms() function like that :
Code:
#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!
 
Hello,

you can use the _delay_ms() function like that :
Code:
#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!
I'm not sure but I'm pretty sure you'd need
Code:
#include <avr/io.h>
Not sure.
 

ikalogic

Member
yes, you need IO.H, along with the rest of the code!! i just included the file related with the delay function
 

bnprakash4u

New Member
Timer control

Hai swallow_159,

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

This can also be done through PWM technique

Advance wishes for success
-BNP
 

Russ Hensel

New Member
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" :D, 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 :D 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.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top