#include "ioSTM8S207S8.h"
#include "Includes.h"
#include "Constants.h"
#include "Functions.h"
#include "Globals.h"
/* Author: EighthArmy @ July.2010 */
/* Excel2000 expression as: v = sin( DEGREE * PI()/180 ) */
/* 0 degree = 0x024A / 2 = 0x0125 = 293 */
/* 10 degree = 0x0125 + sin10 = 344 */
#define SIN_WAVE_LEN 36
const uint SIN_TABLE[SIN_WAVE_LEN] = {
9376, 9312, 9088, 8752, 8272, 7696,
7040, 6288, 5504, 4688, 3872, 3088,
2352, 1680, 1104, 624, 288, 64,
0, 64, 288, 624, 1104, 1680,
2352, 3088, 3872, 4688, 5504, 6288,
7040, 7696, 8272, 8752, 9088, 9312
};
volatile uchar ucSinIndex = 0;
#pragma vector = TM3CC_VECT
__interrupt void TIM3_UPDATE_ISR( void ){
if( TIM3_SR1 & ( 1 << CC1IF ))
{
TIM3_SR1 &= ~( 1 << CC1IF );
LoadValueOfPWM();
/* 固定脉宽试验 */
// TIM3_CCR1H = ( uchar )( PERCENT_080 >> 8 );
// TIM3_CCR1L = ( uchar )( PERCENT_080 );
/* F = 30Hz, Duty = 80% High-level */
}
}
/*****************************************************
@Fn: ConfigT3_PWM2()
@Br: Timer3的初始化为PWM模式1
@Pa: 无
@Rt: 无
@Sp:
Timer2,Timer3,Timer5 是通用16位定时器.
*****************************************************/
void ConfigT3_PWM2( void ){
TIM3_PSCR = ( 1 << PSC02 );
/* Configure TIM3 prescaler = 4 */
/* Fck_cnt = Fck_psc / 2 ^[3:0] */
TIM3_CCMR1 = ( 1 << OC1M02 ) + ( 1 << OC1M01 ) + ( 1 << OC1M00 )
+ ( 1 << OC1PE );
/* 输出比较1预装载使能, PWM模式2 */
TIM3_CCER1 = ( 1 << CC1P ) + ( 1 << CC1E );
/* OC1低电平有效, 使能TIM3_CH1引脚功能 */
TIM3_ARRH = 0x2F; /* 确定频率 */
TIM3_ARRL = 0xFF;
TIM3_CNTRH = 0x24;
TIM3_CNTRL = 0xA0;
/* ARR一定要大于CNT,否则输出波形不正确 */
TIM3_CCR1H = 0; /* 初始化TIM3_CH1引脚缓冲器为零 */
TIM3_CCR1L = 0;
TIM3_SR1 &= ~( 1 << CC1IF ); /* 清空中断标志 */
TIM3_CR1 = ( 1 << ARPE ) + ( 1 << CEN ); /* 自动重载及计数器使能 */
TIM3_IER = ( 1 << CC1IE ); /* 使能中断 */
}
/*****************************************************
@Fn: LoadValueOfPWM()
@Br: 载入波表值
@Pa: 无
@Rt: 无
@Sp:
Timer2,Timer3,Timer5 是通用16位定时器.
*****************************************************/
void LoadValueOfPWM( void ){
TIM3_CCR1H = ( uchar )( SIN_TABLE[ ucSinIndex ] >> 8 ); /* 2.5赫正弦波 */
TIM3_CCR1L = ( uchar )( SIN_TABLE[ ucSinIndex ] );
ucSinIndex++;
if( ucSinIndex >= SIN_WAVE_LEN )
{
ucSinIndex = 0;
}
}