I need a simple timer firing with the maximum speed of the indio.digitalWrite() function, how do I do this?
Hello, I am working on project with the DG21 based Industruino and need some help. Basically I want to create a pulse to drive/step a stepper motor with the maximum speed of 400Hz on one of the Indio.digitalWrite() outputs. There are some info about timers and/or interupts in here, but the info seems all outdated already. Can anybody give me the most simple example of some kind of timer-based function to run the sequence Indio.digitalWrite(1, HIGH), Indio.digitalWrite(1, LOW) with 400Hz? I cannot run this in loop(), because my functions need about 27millis to complete. Thank you in advance!
Hi
Because de DG21 is one core based, you cant have more than 1 loop, but 48mhz of speed is enough for multiple complex task.
First, you need to put your code to se it, because in this kind o applications you cant use delay() on the code, and you need to create some "logical timers". Check this: https://www.baldengineer.com/software-pwm-with-millis.html
Is a base to get your purpose, ill put a code for ultrasonic sensor, that maybe can help you, in the code of usage handle many things with timers and secuences, and a mega2560 is used, so if with 16mhz can do it, maybe the d21g can handle:
void ultrasonido(){// funcion ultrasonido, mide distancias
if(millis()-tdistancia>=50){//do the function at 50 millis
digitalWrite(TRIGGER_PIN,LOW);
if(micros()-tiempoBajo>=10){// The high and low pulse is to 10us
digitalWrite(TRIGGER_PIN,HIGH);
tiempoBajo=micros();
}
if(micros()-tiempoAlto>=10){//high pulse over 10us
digitalWrite(TRIGGER_PIN,LOW);
tiempoAlto=micros();
}
duration=pulseIn(ECHO_PIN,HIGH);
distance=(duration*0.034)/2;
if(distance>=0&&distance<=400){//quita las lecturas erroneas del sensor
defDistancia=distance;}
//erial.println(defDistancia);
tdistancia=millis();}
}
Regards
Thanks Carlos, I am doing something similar, but because I am using modbus functions (which block for ~25ms), I need a real timer which works reliable. There is a blog post about a timer but it refers to some old versions of some code, where I need to manual make some changes... I never got it working based on these instructions. I hope the Industruino people read this and supply us with a flexible timer library with usage examples especially made for this Industruino! Please! m(_ _)m
Thanks Carlos, I am doing something similar, but because I am using modbus functions (which block for ~25ms), I need a real timer which works reliable. There is a blog post about a timer but it refers to some old versions of some code, where I need to manual make some changes... I never got it working based on these instructions. I hope the Industruino people read this and supply us with a flexible timer library with usage examples especially made for this Industruino! Please! m(_ _)m
If you need a library maybe the Timer by stefan staub, can help you. The problem with the libraries is that almost every uses the internal hardware timers, so this can make problems with instructions that need timers for delays, because use the systick timer. But the mcu has a lot of timers to configure, but is not easy understand and configure. I think you need to make a function, that can handle changes of time, and interrupts. I put industruino to operate one filling machine with Modbus and logical timers, and at this time still working well. The Indio have many tricks to accomplish this: -The MCU D21G have one RTC internal, that you can use to count secs (datahseet pag 229) -Logical timer to count millis (i use this, and works well). -The industruino has a RTC in the design so you can play with this too, to count secs, minutes, and make functions. -You can use one of asynchronous timers in the D21G, this timers dont mess with the peripherals, so if you now use a Serial for RS485 on modbus this dont crash, or can use the TCC or TC timers. And for this "There is a blog post about a timer but it refers to some old versions of some code, where I need to manual make some changes" what code is? maybe from here can help you.
Your answer
Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!
Keep Informed
About This Forum
This community is for professionals and enthusiasts of our products and services.
Read GuidelinesQuestion tools
Stats
Asked: 12/12/23, 8:29 AM |
Seen: 608 times |
Last updated: 1/21/24, 5:25 PM |
Hi Because de DG21 is one core based, you cant have more than 1 loop, but 48mhz of speed is enough for multiple complex task. First, you need to put your code to se it, because in this kind o applications you cant use delay() on the code, and you need to create some "logical timers". Check this: https://www.baldengineer.com/software-pwm-with-millis.html Is a base to get your purpose, ill put a code for ultrasonic sensor, that maybe can help you, in the code of usage handle many things with timers and secuences, and a mega2560 is used, so if with 16mhz can do it, maybe the d21g can handle: void ultrasonido(){// funcion ultrasonido, mide distancias if(millis()-tdistancia>=50){//do the function at 50 millis digitalWrite(TRIGGER_PIN,LOW); if(micros()-tiempoBajo>=10){// The high and low pulse is to 10us digitalWrite(TRIGGER_PIN,HIGH); tiempoBajo=micros(); } if(micros()-tiempoAlto>=10){//high pulse over 10us digitalWrite(TRIGGER_PIN,LOW); tiempoAlto=micros(); } duration=pulseIn(ECHO_PIN,HIGH); distance=(duration*0.034)/2; if(distance>=0&&distance