2014-02-04 06:04 PM
Guys
How can create a tone with timer on STM32 ? This code is the way I made it on AVR, how can I mimic it on STM32 ?timer1_ctc_init()
{
TCCR1A =
// Mode 4 CTC 1:64 Prescaler
_BV(COM1A0);
// set OC1A on compare match, clear them at top
//| _BV(COM1A1); // set OC1A on compare match, clear them at top
//TCCR1B |= _BV(CS11) | _BV(CS10) | _BV(WGM12);
TCCR1B |= _BV(CS11) | _BV(WGM12);
//1:8 pre scaler
}
void
tone(
int
freq)
{
timer1_ctc_init();
OCR1A=freq;
}
thanks
2014-02-04 06:43 PM
Factor (TIMCLK / freq) into the Period and Prescaler settings.
2014-02-04 06:47 PM
2014-02-04 07:30 PM
// STM32F4-Discovery TIM1 16-bit PWM (PA.08 TIM1_CH1) - sourcer32@gmail.com
// TIM1CLK = 168000000 APB2=AHB/2 * 2
#include ''stm32f4_discovery.h''
/**************************************************************************/
void RCC_Config(void)
{
/* GPIOA clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
}
/**************************************************************************/
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM1 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**************************************************************************/
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOA Configuration: TIM1 CH1 (PA8) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect TIM1 pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
}
/**************************************************************************/
void TIM1_Config(uint32_t Freq)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint32_t Prescaler, Period;
// Roughly factor into 16-bit
Period = (SystemCoreClock / 1) / Freq;
Prescaler = (Period / 65536) + 1;
Period = (Period / Prescaler);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = Prescaler - 1;
TIM_TimeBaseStructure.TIM_Period = Period - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = Period / 2; // 50/50 duty
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* TIM Interrupts enable */
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
/* TIM2 enable counter */
TIM_Cmd(TIM1, ENABLE);
/* Main Output Enable, and Input */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
/**************************************************************************/
void TIM1_UP_TIM10_IRQHandler(void)
{
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
/* LED3 toggling with frequency halved */
STM_EVAL_LEDToggle(LED3);
}
}
/**************************************************************************/
int main(void)
{
RCC_Config();
NVIC_Config();
GPIO_Config();
/* Initialize Leds mounted on STM32F4-Discovery board */
STM_EVAL_LEDInit(LED4);
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED5);
STM_EVAL_LEDInit(LED6);
/* Turn on LED4, LED3, LED5 and LED6 */
STM_EVAL_LEDOn(LED4);
STM_EVAL_LEDOn(LED3);
STM_EVAL_LEDOn(LED5);
STM_EVAL_LEDOn(LED6);
/* TIM1 Configuration */
TIM1_Config(50); // 50 Hz
while(1); // Do not exit
}
/**************************************************************************/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d
'', file, line) */
while (1)
{}
}
#endif
2014-02-04 08:28 PM
So I will connect PA8 into my Audio amplifier to magnify the signal or this one is good enough for driving a speaker....
Is the code adaptable to STM32F107 ? thanks2014-02-04 08:30 PM
So I will connect PA8 into my Audio amplifier to magnify the signal or this one is good enough for driving a speaker....
Is the code adaptable to STM32F107 ? thanks2014-02-05 01:48 AM
Hi
'' or this one is good enough for driving a speaker....'' I doubt it. You will need an amplifier.2014-02-06 10:14 PM
I don't have :
: function ''GPIO_PinAFConfig on STM32F103 ? What's the similarity for it ? It happened when I tried to compile it.. ..\USER\main.c(43): error: #20: identifier ''TIM1_UP_TIM10_IRQn'' is undefined ..\USER\main.c(63): warning: #223-D: function ''GPIO_PinAFConfig'' declared implicitly ..\USER\main.c(63): error: #20: identifier ''GPIO_AF_TIM1'' is undefined2014-02-06 10:23 PM
what I can see from
stm32f10x_gpio.h:void
GPIO_DeInit(GPIO_TypeDef* GPIOx);
void
GPIO_AFIODeInit(
void
);
void
GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
void
GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
void
GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void
GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void
GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
void
GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
void
GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void
GPIO_EventOutputConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);
void
GPIO_EventOutputCmd(FunctionalState NewState);
void
GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState);
void
GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);
void
GPIO_ETH_MediaInterfaceConfig(uint32_t GPIO_ETH_MediaInterface);
2014-02-07 12:28 PM
// STM32VL-Discovery TIM1 16-bit PWM (PA.08 TIM1_CH1) - sourcer32@gmail.com
// TIM1CLK = 24000000 APB2=AHB/2 * 2
#include ''stm32F10x.h''
#include ''STM32vldiscovery.h''
//****************************************************************************
void RCC_Configuration(void)
{
// clock for GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// clock for TIM1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
}
//****************************************************************************
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; // PA.08 TIM1_CH1
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
//****************************************************************************
void TIM1_Configuration(uint32_t Freq)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint32_t Prescaler, Period;
// Roughly factor into 16-bit
Period = (SystemCoreClock / 1) / Freq;
Prescaler = (Period / 65536) + 1;
Period = (Period / Prescaler);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = Prescaler - 1;
TIM_TimeBaseStructure.TIM_Period = Period - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Enable TIM1 Preload register on ARR */
TIM_ARRPreloadConfig(TIM1, ENABLE);
/* TIM PWM1 Mode configuration */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = Period / 2; // 50/50 duty
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* Output Compare PWM1 Mode configuration: Channel1 PA.08 */
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* TIM Interrupts enable */
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
/* TIM1 enable counter */
TIM_Cmd(TIM1, ENABLE);
}
//****************************************************************************
void TIM1_UP_TIM16_IRQHandler(void)
{
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
/* LED3 toggling with frequency halved */
STM32vldiscovery_LEDToggle(LED3);
}
}
//****************************************************************************
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM1 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM16_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//****************************************************************************
int main(void)
{
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
/* Initialize Leds mounted on STM32 VL-Discovery board */
STM32vldiscovery_LEDInit(LED3);
STM32vldiscovery_LEDInit(LED4);
/* Turn on LED3 and LED4 */
STM32vldiscovery_LEDOn(LED3);
STM32vldiscovery_LEDOn(LED4);
TIM1_Configuration(50); // 50 Hz, LED3 toggles at 25 Hz
while(1);
}
//****************************************************************************
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d
'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif