Hi all , I tried before to run DAC on ATtiny85 after I found code that uses timer/interrupter to generate PWM signal similar to PCM to have 8bit/8khz sound output from M-C .
and since STM32 architecture are totally different I found my self lost here , I found few documents explain STM32F1 implementation of arduino code e.g this maple doc explain the board have different PWM duty cycle than arduino nano
frequency is configurable, defaulting to about 1100 Hz, or 1.1 KHz. This is because the PWM frequency is the frequency of the timer which controls PWM output
on the particular arduino didn't accept their solution to change timer frequency timer.setPeriod(2041);
anyway I'll try to get back to PWM timer later . Now what I did is using simple sketch to test this board .
I use both **analogWrite() ** and delayMicroseconds( ) to output PCM signal using PWM , I hookup small speaker (with resistor ) to A8 .
but I barley hear the voice output most of the sound were continues noise .. Here is the project code and PCM unsigned header file .
You can't use a PWM DAC to produce frequencies that are almost equal to the PWM frequency. Of course, you will get noise. It's intermodulation. That will happen on any PWM, any processor.
You could try it on an STMF3xx device or similar, that has real DAC hardware, or interface a separate DAC IC/module.
aarg:
You can't use a PWM DAC to produce frequencies that are almost equal to the PWM frequency. Of course, you will get noise. It's intermodulation. That will happen on any PWM, any processor.
I done this before on smaller M-C "ATtiny85" and this for sure will work on STM32F103 , The wrong thing I did is using analogWrite() to do PWM/DAC Implementation , which will not work of course .
I use it temporary until I find away to use STM32F103 timer and interrupter to output PCM signal similar to the input audio codes ( unsigned 8bit/8khz PCM) sound .
Here is my ATtiny85 code that's works very-well .
#include "hex.h"
unsigned int quack_wav_len = 5102; // 2038 3k , 2732 4k
unsigned int p = 0;
void setup() {
// put your setup code here, to run once:
// Enable 64 MHz PLL and use as source for Timer1
PLLCSR = 1<<PCKE | 1<<PLLE;
// Set up Timer/Counter1 for PWM output
TIMSK = 0; // Timer interrupts OFF
TCCR1 = 1<<PWM1A | 2<<COM1A0 | 1<<CS10; // PWM A, clear on match, 1:1 prescale
GTCCR = 1<<PWM1B | 2<<COM1B0; // PWM B, clear on match
OCR1A = 128 ; OCR1B = 128; // 50% duty at start
// Set up Timer/Counter0 for 8kHz interrupt to output samples.
TCCR0A = 3<<WGM00; // Fast PWM
TCCR0B = 1<<WGM02 | 2<<CS00; // 1/8 prescale
TIMSK = 1<<OCIE0A; // Enable compare match
OCR0A = 250; // Divide by 1000 "1000 ÷ 8 " >>
set_sleep_mode( SLEEP_MODE_PWR_DOWN );
pinMode( 4 , OUTPUT ) ; pinMode( 1 , OUTPUT ) ;
}
void loop() {
}
// repeat loop
ISR( TIMER0_COMPA_vect ) {
char sample = pgm_read_byte(&quack_wav[p++]);
OCR1A = sample;
OCR1B = sample ^ 255;
if ( p == quack_wav_len ) p=0;
}
What you're doing there, is running the PWM at a much higher frequency. Then of course, you can reproduce audio since in that case, the audio is at a relatively lower frequency. The STM32F103 has a really great timer subsystem with PWM, I've done a lot with it but using CUBE IDE not Arduino.
aarg:
I've done a lot with it but using CUBE IDE not Arduino.
I use arduino because it's code easier to understand from me and shorter , I tried CUBE IDE until now I couldn't have suitable Linux C++ IDE/Compiler for it ,
I watch couple tutorials and honesty I need big reference to start modifying the code it generate .
if you have time could your read the last code I post (that for ATtiny85) and tell me how translate that code (the timer and the interrupter ) it to match STM32F103 timer/registers , my experience with low level programming are very little .
Alright I get the code working thanks to this video , and this Library HardwareTimer thanks huaweiwx .
I get to know how to change PWM frequency and decrees duty cycle time (the value I use now is bit crazy but got me smooth audio output without need for external filters )