Hello everyone, I am new to the forum it is actually my first post.
I am relatively new working with the Digispark IDE and I am having some issues with something that should be really simple.
I am trying to output a 2.5 kHz square wave signal, that will oscillate +/- 500 Hz. So it goes from 2000Hz to 3000 Hz from a micro controller that uses a ATTiny85 with Digispark's IDE.
I found this tutorial online.
https://digistump.com/wiki/digispark/tricks which explains what I want to do. As they said, to modify the HW PWM frequency I changed the value from 64 to 8 of the MS_TIMER_TICK_EVERY_X_CYCLES inside wiring.c
Because of this I managed to get a higher frequency value. But I was not able to tune it, that is why I continued with the Software PWM manipulation
I have used this code.
#include <TinySoftPwm.h>
#include <DigiUSB.h>
#define HW_PWM_PIN 0 /* Used to check HW PWM with analogWrite() */
#define SW_PWM_BUILT_IN_LED_PIN 1 /* Digispark Model A (Rev2) built-in LED pin number (Change it to 2 for Model B) */
#define TIME_TEST_PIN 5 /* Used to check with oscilloscope micros(), millis() are still OK */
void setup()
{
TinySoftPwm_begin(128, 0); /* 128 x TinySoftPwm_process() calls before overlap (Frequency tuning), 0 = PWM init for all declared pins */
pinMode(TIME_TEST_PIN, OUTPUT);
DigiUSB.begin();
}
void loop()
{
static uint32_t PwmStartUs=micros();
static uint32_t PwmStartMs=millis();
static uint8_t Pwm=0;
static int8_t Dir=1;
static boolean State=LOW;
static uint32_t BlinkStartMs=millis();
/***********************************************************/
/* Call TinySoftPwm_process() with a period of 60 us */
/* The PWM frequency = 128 x 60 # 7.7 ms -> F # 130Hz */
/* 128 is the first argument passed to TinySoftPwm_begin() */
/***********************************************************/
if((micros() - PwmStartUs) >= 60)
{
/* We arrive here every 60 microseconds */
PwmStartUs=micros();
TinySoftPwm_process(); /* This function shall be called periodically (like here, based on micros(), or in a timer ISR) */
}
/*************************************************************/
/* Increment/decrement PWM on LED Pin with a period of 10 ms */
/*************************************************************/
if((millis()-PwmStartMs) >= 10)
{
/* We arrived here every 10 milliseconds */
PwmStartMs=millis();
Pwm+=Dir; /* increment or decrement PWM depending of sign of Dir */
TinySoftPwm_analogWrite(SW_PWM_BUILT_IN_LED_PIN, Pwm); /* Software PWM: Update built-in LED for Digispark */
analogWrite(HW_PWM_PIN, Pwm); /* Copy Pwm duty cycle to Hardware PWM */
if(Pwm==255) Dir=-1; /* if PWM reaches the maximum: change direction */
if(Pwm==0) Dir=+1; /* if PWM reaches the minimum: change direction */
}
/* Blink half period = 5 ms */
if(millis()-BlinkStartMs>=5)
{
BlinkStartMs=millis();
digitalWrite(TIME_TEST_PIN, State);
State=!State;
}
/* Check USB is still working */
if(DigiUSB.available()) /* Just hit "Enter" in the digiterm to check USB */
{
DigiUSB.read(); /* just to clear the Rx buffer */
DigiUSB.println(F("DigiUSB is still alive!"));
}
DigiUSB.refresh();
}
It doesnt matter what I do, I am reading an oscillating 4.065 kHz - 4.049 kHz value.
What am I doing wrong?
Is my oscilloscope broken?
Should I be doing this any other way?
Thank you very much, I am desperate, I have been working on this for days. Any suggestion or idea could help !!