main.c

来自「使用定时器产生PWM波」· C语言 代码 · 共 71 行

C
71
字号
/******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
* File Name          : main.c
* Author             : MCD Application Team
* Date First Issued  : 28/07/2003
* Description        : This file gives an example on the PWM generation.
*                      The device is configured to generate a PWM wave with a
*                      pulse equal to 0x30 timer clock ticks and a full period
*                      equal to 0xC0.
********************************************************************************
* History:
*  03/07/03 : First Version
*******************************************************************************/

#include "71x_lib.h"
#include "Main.h"

u32 Note = 0x800, Period;

void PlayNextTone(void)
{
	if(Period)
		Period--;
	else
	{
		TIM_PWMOModeConfig(TIM1, Note >> 1, TIM_HIGH, Note, TIM_LOW);
		Note ^= 0x1000;
		Period = 0x200;
	}
}


int main(void)
{
#ifdef DEBUG
	debug();
#endif

	RCCU_Div2Config(ENABLE);
	RCCU_MCLKConfig(RCCU_DEFAULT);
	RCCU_FCLKConfig(RCCU_RCLK_2);
	RCCU_PCLKConfig(RCCU_RCLK_2);
	RCCU_RCLKSourceConfig(RCCU_CLOCK2);
	// at this step the PCLK2 signal should be equal to 4 MHz

	//-------------------------------------------------------------------------
	// Initialization
	//-------------------------------------------------------------------------
	// Configure the GPIO1 Port
	// Timer 1 Output Compare function pin (P1.7) configured as Alternate function pushpull
	GPIO_Config(GPIO1, 0x0080, GPIO_AF_PP);

	// Initialize the Timer
	TIM_Init(TIM1);
	// Configure the TIM Prescaler
	TIM_PrescalerConfig(TIM1, 1);		// 2 MHz
	// Enable the OverFlow Interrupt
	TIM_ITConfig(TIM1, TIM_TO_IT, ENABLE);
	TIM_CounterConfig(TIM1, TIM_START);

	//-------------------------------------------------------------------------
	// Configure the EIC Timer1 IRQ channel
	//-------------------------------------------------------------------------
	// Enable the IRQ0 for timer 1
	EIC_Init();
	EIC_IRQChannelConfig(T1TIMI_IRQChannel, ENABLE);
	EIC_IRQChannelPriorityConfig(T1TIMI_IRQChannel, 1);
	EIC_IRQConfig(ENABLE);

	while(1);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?