|
#include "IOSTM8S103F3.h"
unsigned int FRE=200; //frequency 频率 4M / 100 = 40K
void PWM_GPIO_Init( void )
{
PC_DDR |= ( 1 << 7 ); //PC7 推挽输出
PC_CR1 |= ( 1 << 7 );
}
void TIM1_Init( void )
{
PWM_GPIO_Init();
TIM1_CR1 = ( 1 << 7 ); //自动预装载允许
TIM1_CCMR2 = ( 6 << 4 ) | ( 1 << 3 ) | ( 1 << 2 ); //TIM1 CH2 PWM1 输出比较1预装载使能 输出比较1 快速使能
TIM1_CCER1 |= 0x10; //CC2为输出 高电平有效
TIM1_PSCRH = 0x00; //预分频 0
TIM1_PSCRL = 0x03; //16M/(1+3)=4 M
TIM1_ARRH = FRE >> 8; //设定自动重装载值高8位
TIM1_ARRL = FRE; //设置自动重装载值低8位
TIM1_BKR = 0x80; //刹车寄存器 使能OC1输出 定时器不工作时输出无效电平
TIM1_EGR = ( 1 << 0 ); //产生更新事件
TIM1_CR1 |= 0x01; //使能计数器
}
//TIM1 CH2 PC7
void TIM1_CH2_FRE_DC_Change( unsigned int freq, unsigned int dc )
{
TIM1_ARRH = freq >> 8; //设置频率
TIM1_ARRL = freq;
TIM1_CCR2H = dc >> 8;
TIM1_CCR2L = dc;
}
void SysClkInit( void )
{
CLK_SWR = 0xe1; //HSI为主时钟源 16MHz CPU时钟频率
CLK_CKDIVR = 0x00; //CPU时钟0分频,系统时钟0分频
}
void main( void )
{
unsigned int i = 100;
SysClkInit();
__asm( "sim" );
TIM1_Init();
__asm( "rim" );
while( 1 )
{
for( i = 100; i < 2000; i++ )
{
TIM1_CH2_FRE_DC_Change( i, i / 2 );
}
}
}
|
评分
-
查看全部评分
|