打印

老Xuan贡献:STM8L TIM2 PWM 生成demo程序

[复制链接]
7913|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
solarmax1969|  楼主 | 2012-8-25 17:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 solarmax1969 于 2012-8-25 17:48 编辑

老Xuan是初学者,边学习边贡献,哈哈,贡献也是一种幸福啊!
研究了二天,PWM波形基本搞出来了,具体实用程序请看下面。


/* TIM2 ,通道1,用于生成 38Khz 红外载波信号 */
void TIM2_Init(void)
{
CLK_PCKENR1 |= (1<<CLK_TIM2);
  
TIM2_CR1   = 0x00; //计数禁用
TIM2_PSCR  = 0x00; //预分频系数=1

TIM2_ARRH  = 0x00;
TIM2_ARRL  =   53; //设置输出频率 38khz

TIM2_CCR1H = 0x00;
// TIM2_CCR1L =  10; //设置Duty
TIM2_CCR1L =  30; //设置Duty 大约48%

TIM2_CCER1 |= ((1<<CC1P) | (1<<CC1E)); //低电平有效,同时启用输出
TIM2_CCMR1 = 0x70; //PWM输出模式
TIM2_IER |= (1<<CC1IE); //启用中断
TIM2_CR1 |= (1<<CEN); //计数器使能,开始工作

TIM2_BKR  |=(1<<MOE); //主输出使能
}

//下面的中断也可以不用它
@far @interrupt void TIM2_OC1(void) //TIM2,通道1,比较匹配中断
{
CLI();
TIM2_SR1  &= (~(1<<CC1IF)); //清楚标志位
// 可在这里加入对Duty修改的语句
SEI();
}

---------------------------------- 下面是自己搞的寄存器头文件
/***********************************************************
**文 件 名:SM_STM8L151_REGS.h
**创建时间:2012/08/10
**作    者:JIM XUAN
************************************************************
**功    能:处理单片机核心资源的定义,Bit标识符
**备    注:                                                
**修    改:
***********************************************************/
#ifndef __SM_STM8L151_REGS_H
#define __SM_STM8L151_REGS_H

#define SEI() _asm("rim") // 允许CPU 全局中断
#define CLI() _asm("sim") // 禁止CPU 全局中断
#define NOP() _asm("NOP")

/*  Peripheral clock gating register 1 (CLK_PCKENR1) */
#define CLK_DAC    7
#define CLK_BEEP   6
#define CLK_USART1 5
#define CLK_SPI1   4
#define CLK_I2C1   3
#define CLK_TIM4   2
#define CLK_TIM3   1
#define CLK_TIM2   0
/*  Peripheral clock gating register 2 (CLK_PCKENR2) */
#define CLK_BOOTROOM 7 //
#define CLK_COMP12 5
#define CLK_DMA1   4
#define CLK_LCD    3
#define CLK_RTC    2
#define CLK_TIM1   1
#define CLK_ADC1   0
/*  ADC1  ADC1_CR1 */
#define OVERIE 7
#define RES1   6
#define RES0   5
#define AWDIE  4
#define EOCIE  3
#define CONT   2
#define START  1
#define ADON   0
/* ADC1_CR2 */
#define PRESC 7
#define TRIG_EDGE1 6
#define TRIG_EDGE0 5
#define EXTSEL1    4
#define EXTSEL0    3
#define SMTP12     2
#define SMTP11     1
#define SMTP10     0
/* ADC1_CR3 */
#define SMTP22     7
#define SMTP21     6
#define SMTP20     5
#define CHSEL4     4
#define CHSEL3     3
#define CHSEL2     2
#define CHSEL1     1
#define CHSEL0     0
#define ADC1_VREFINT 0b00011100  
#define ADC1_TS      0b00011101
/* ADC1_SR */
#define OVER 2 //overrun flag
#define AWD  1 //Analog watchdog flag,电压过高or过低。可定义高低阀值。
#define EOC  0 //End of conversion

/* TIM23_CR1 */
#define ARPE 7
#define CMS1 6
#define CMS0 5
#define DIR  4
#define OPM  3
#define URS  2
#define UDIS 1
#define CEN  0
/* TIM23_CR2 */
#define TI1S 7
#define MMS2 6
#define MMS1 5
#define MMS0 4
#define CCDS 3
/* TIM23_SMCR */
#define MSM  7
#define TS2  6
#define TS1  5
#define TS0  4
#define SMS2 2
#define SMS1 1
#define SMS0 0
/* TIM23_ETR*/
#define ETP   7
#define ECE   6
#define ETPS1 5
#define ETPS0 4
#define ETF3  3
#define ETF2  2
#define ETF1  1
#define ETF0  0
/* DMA request enable register (TIMx_DER) */
#define CC2DE 2
#define CC1DE 1
#define UDE   0
/* Interrupt enable register (TIMx_IER) */
#define BIE   7
#define TIE   6
#define CC2IE 2
#define CC1IE 1
#define UIE   0
/* Status register 1 (TIMx_SR1) */
#define BIF   7
#define TIF   6
#define CC2IF 2
#define CC1IF 1
#define UIF   0
/* Status register 2 (TIMx_SR2) */
#define CC2OF 2
#define CC1OF 1
/* Event generation register (TIMx_EGR) */
#define BG   7
#define TG   6
#define CC2G 2
#define CC1G 1
#define UG   0
/* Capture/compare mode register 1 (TIMx_CCMR1) */
#define OC1M2 6
#define OC1M1 5
#define OC1M0 4
#define OC1PE 3
#define OC1FE 2
#define CC1S1 1
#define CC1S0 0
#define IC1F3 7
#define IC1F2 6
#define IC1F1 5
#define IC1F0 4
#define IC1PSC1 3
#define IC1PSC0 2
#define CC1S1  1
#define CC1S0  0
/* Capture/compare mode register 2 (TIMx_CCMR2) */
#define OC2M2 6
#define OC2M1 5
#define OC2M0 4
#define OC2PE 3
#define OC2FE 2
#define CC2S1 1
#define CC2S0 0
#define IC2F3 7
#define IC2F2 6
#define IC2F1 5
#define IC2F0 4
#define IC2PSC1 3
#define IC2PSC0 2
#define CC2S1  1
#define CC2S0  0
/* Capture/compare enable register 1 (TIMx_CCER1) */
#define CC2P 5
#define CC2E 4
#define CC1P 1
#define CC1E 0
/* Counter high (TIMx_CNTRH) */
/* Counter low (TIMx_CNTRL)  */
/* Prescaler register (TIMx_PSCR) */
#define PSC2 2
#define PSC1 1
#define PSC0 0
/* Auto-reload register high (TIMx_ARRH) */
/* Auto-reload register high (TIMx_ARRL) */
/* Capture/compare register 1 high (TIMx_CCR1H) */
/* Capture/compare register 1 high (TIMx_CCR1L) */
/* Capture/compare register 1 high (TIMx_CCR2H) */
/* Capture/compare register 1 high (TIMx_CCR2L) */
/* Break register (TIMx_BKR) */
#define MOE 7
#define AOE 6
#define BKP 5
#define BKE 4
#define OSSI 2
#define LOCK1 1
#define LOCK0 0
/* Output idle state register (TIMx_OISR) */
#define OIS2 2
#define OIS1 0  



#endif

***************8
上面程序已经调试成功能用的。
现在还有几个关键的问题:
1:Fpwm 频率到底如果计算的?
2:PWM位数是几位?
沙发
suzhiqiang168| | 2012-9-22 13:53 | 只看该作者
不错,顶了

使用特权

评论回复
板凳
liudongyu| | 2012-9-22 17:02 | 只看该作者
看看。。。学习学习

使用特权

评论回复
地板
baidudz| | 2012-9-22 22:24 | 只看该作者
同意lZ的观点,贡献也是一种幸福

使用特权

评论回复
5
火箭球迷| | 2012-9-22 22:34 | 只看该作者
鼓励共享

使用特权

评论回复
6
nienianhong| | 2014-4-2 16:28 | 只看该作者
你用的是STM8L那个型号啊?

使用特权

评论回复
7
BitFu| | 2019-4-12 23:28 | 只看该作者
我也是搞了一晚上都搞不出来,后来参考此程序 补上 “”TIM2_BKR  |=(1<<MOE); //主输出使能“ 之后OK了,谢谢楼主!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

26

帖子

2

粉丝