Hola, estoy construyendo un generador de frecuencia de onda cuadrada, en un arduino UNO, me encontré un código muy cercano a lo que yo quiero y he logrado modificarlo para mis necesidades, funciona de 0.5Hz a 100,000Hz, se puede visualizar en un LCD 16X2, así como también los microsegundos y el ciclo de trabajo, tiene tres botones, uno para subir frecuencia, otro para bajar frecuencia y uno mas para mover el ciclo de trabajo que va incrementando en pasos de 25% y al pasar de 100% vuelve a cero.
Al parecer todo funciona bien, pero me gustaría que los Hz, se visualizaran con decimales, ya que solo aparecen números enteros, pero no se por donde empezar, ya que no encuentro como hacerlo, por eso necesito de su ayuda por favor, aquí dejo el código que estoy trabajando:
#include <TimerOne.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);// RS,E,D4,D5,D6,D7
unsigned long t=5000,f,k=512;// default 5000 μs (200 Hz), meander, pulse
byte k1,kn,kn1,kn2;
int drive,drive0;
void setup()
{
lcd.begin(16, 2);// LCD 16X2
pinMode(9, OUTPUT);
pinMode(6,INPUT);// button at input 6
pinMode(7,INPUT);// button at input 7
pinMode(13,INPUT);// button at input 13
}
void loop()
{
Timer1.initialize(t); // period
Timer1.pwm(9, k); // k - fill factor 0-1023.
kn=digitalRead(6);// button input 6 (- pulse period)
kn1=digitalRead(7);// button input 7 (+ pulse period)
kn2=digitalRead(13);// button input 13 (+ circle fill factor)
if(kn==HIGH){ // decreasing the period
drive++;
if(drive<20){
t=t-10;
}
// if the button is held for a long time, the correction of the pulse
else if(drive>20 && drive<40 ){
t=t-100;
}
else if(drive>=40 && drive<100){
t=t-1000;
}
else if(drive>=100){
t=t-10000;
}
}
else{
drive=0;
}
if(kn1==HIGH){// adding a period
drive0++;
if(drive0<20){
t=t+10;
// if the button is held for a long time, the correction of the
}
else if(drive0>20 && drive0<40 ){
t=t+100;
}
else if(drive0>=40 && drive0<100){
t=t+1000;
}
else if(drive0>=100){
t=t+10000;
}
}
else{
drive0=0;
}
if(t==0 || t>1600000){ // limiting the pulse duration to the minimum, if
t=10;
}
if(t>1500000 && t<1600000){ // limiting the pulse duration to the
t=1500000;
}
f=1000000/t; // calculate the frequency
k1=k*100/1024; // calculate% fill factor
if(kn2==HIGH){// button for adjusting the fill factor (in a circle from
k=k+256;// step 16 out of 1024 (you can do 8 for smoother adjustment)
}
if(k>1024){
k=0;
}
// displaying information on the indicator
lcd.clear();
lcd.setCursor(0,1);
lcd.print("T=");
lcd.print(t);
lcd.print("us");
lcd.setCursor(12,1);
lcd.print(k1);
lcd.print("%");
lcd.setCursor(0,0);
lcd.print("F=");
lcd.print(f, 1);
lcd.print("Hz");
delay(300);
}
Gracias.