\ Program Name: freq-gen.txt \ Hardware: STM32F0 Discovery board which runs Mecrisp-Stellaris for the stm32f051 at 8 Mhz using the RC clock \ Author: t.porter November 2016 \ All register names are CMSIS-SVD compliant so use Svd2forth-v2 to generate your memory map words or use freq-gen-memap.txt which is included in this project. \ Overall Design \ Registers used: GPIOB, TIM14 \ Timer = tim14 \ Tone output pin: PB-1 \ init words : tim14-cc1e-as-output %1 0 lshift TIM14_CCER bis! ; \ TIM14_CC1E : tim14-clock-enable %1 8 lshift RCC_APB1ENR bis! ; \ TIM14 clock enable : tim14-pwm_mode %110 4 lshift TIM14_CCMR1_Output bis! ; \ TIM14_OC1M 110: PWM mode 1 : tim14-start %1 0 lshift TIM14_CR1 bis! ; \ TIM14_CCR1 counter enable (start timer) : pb1-af0 %10 2 lshift GPIOB_MODER bis! ; \ GPIOB_MODER1 \ program words : pulse-length TIM14_ARR ! ; \ set frequency : duty-cycle TIM14_CCR1 ! ; \ set duty cycle : arr \ auto reload register value (frequency at 50% duty cycle (800 = 10.06khz)) dup dup pulse-length ." TIM14_ARR is $" hex. cr shr \ pulse-length divided by 2 for 50% duty cycle duty-cycle ; : hz DUP ." for a output of " . ." Hz (CLOCK = 8MHz, Prescaler = 0) " 8000000 swap / arr ; \ 123 Hz (minimum) = $FE10, 15Khz = $215, 20Khz = $190, 350KHz (max) = $16 \ init tim14-clock-enable tim14-cc1e-as-output tim14-pwm_mode tim14-start pb1-af0 \ output a 5 KHz square wave on PB-1 5000 hz \ 5025 Hz at 50.25% duty cycle \ Screenshot:- \ Forth> 20000 hz \ -> 20000 hz \ <- for a output of 20000 Hz (CLOCK = 8MHz, Prescaler = 0) TIM14_ARR is $00000190 \ <- ok.