原创 TIMx 的PWM波输出,ST的TIMx example4

2008-4-13 17:36 2664 6 8 分类: MCU/ 嵌入式

ST的example4 产生PWM波,无中断程序
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
u16 CCR1_Val = 0x1F4;
u16 CCR2_Val = 0x177;
u16 CCR3_Val = 0xFA;
u16 CCR4_Val = 0x7D;
ErrorStatus HSEStartUpStatus;
  
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
   
/* Private functions ---------------------------------------------------------*/



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


  /* System Clocks Configuration */
  RCC_Configuration();


  /* NVIC Configuration */
  NVIC_Configuration();
 
  /* GPIO Configuration */
  GPIO_Configuration();


/* -----------------------------------------------------------------------
  TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
  TIM3CLK = 36 MHz, Prescaler = 0x0, TIM3 counter clock = 36 MHz
  TIM3 ARR Register = 0x3E7 => TIM3 Frequency = TIM3 counter clock/(ARR + 1)
  TIM3 Frequency = 36 KHz.
  TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
  TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
  TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
  TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
----------------------------------------------------------------------- */
 
  /* Time base configuration */
 TIM_TimeBaseStructure.TIM_Period = 0x3E7;             //0x3E7=999, 从0到999相当于计数1000次,到了999再从0开始计数
  TIM_TimeBaseStructure.TIM_Prescaler = 0x00;      
  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;   
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  // PWM mode1, upcounting mode下, CNT

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);


  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;         
  TIM_OCInitStructure.TIM_Channel = TIM_Channel_1;         
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;             //0x1F4=500, Duty cycle="500/1000"=0.5
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
   
  TIM_OCInit(TIM3, &TIM_OCInitStructure);


  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);


  /* PWM1 Mode configuration: Channel2 */
  TIM_OCInitStructure.TIM_Channel = TIM_Channel_2;         
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;             //0x177=375, D="375/1000"=0.375
 
  TIM_OCInit(TIM3, &TIM_OCInitStructure);


  TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);


  /* PWM1 Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_Channel = TIM_Channel_3;         
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;             //0xFA=250, D="375/1000"=0.25



  TIM_OCInit(TIM3, &TIM_OCInitStructure);


  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);


  /* PWM1 Mode configuration: Channel4 */
  TIM_OCInitStructure.TIM_Channel = TIM_Channel_4;         
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;            //0x177=125, D="375/1000"=0.125



  TIM_OCInit(TIM3, &TIM_OCInitStructure);


  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
 
  TIM_ARRPreloadConfig(TIM3, ENABLE);


  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);


  while(1)
  {
  } 
}

PARTNER CONTENT

文章评论2条评论)

登录后参与讨论

用户1359586 2008-4-23 19:48

u16 是 typedef unsigned short u16; 把无符号short数据结构(16bit)用u16来代替。 你可以选中u16后点右键,点go to definition of u16

用户137881 2008-4-22 14:41

大哥可不可以简单的解释下这个程序 u16 CCR1_Val = 0x1F4这个U16是什么东西啊 我是各菜鸟 不要笑话啊
相关推荐阅读
用户1359586 2011-07-07 11:49
一个简单字符驱动
网上常见的一个linux字符驱动,见 http://www.dzsc.com/data/html/2009-5-31/76528.html insmod test.ko lsmod      就可...
用户1359586 2010-05-14 22:38
FPGA自己产生reset
遇到一个FPGA没有外部的reset,只能自己产生了,这么简单一个问题居然想了很久才实现。在modelsim仿真是对的,还没有实际操作,也希望和大家讨论一下module reset_generatio...
用户1359586 2010-05-10 22:27
SRAM的时序约束
http://blog.ednchina.com/ilove314/198969/message.aspx#85821  读SRAM时序约束分析分析了SRAM的IO计算,但是没有讲如何具体的计算和Ti...
用户1359586 2010-04-07 21:09
CCS6000安装问题
http://bbs.21ic.com/icview-39374-1-1.html在安装CCS6000的时候,运行ccs6000.exe的时候,碰到好几机子都装不上。主要问题是在安装到“compone...
用户1359586 2010-01-13 21:15
matlab 函数:sprintf
for i="1:20"     j="sprintf"('%03d',i)endj =001j =002j =003j =004j =005j =006j =007j =008j =009j =01...
用户1359586 2009-09-29 09:54
有着十三亿人众的孔孟之乡没有一个教育家
   耶鲁大学校长 小贝诺.施密德特  曾任耶鲁大学校长的小贝诺?施密德特,日前在耶鲁大学学报上公开撰文批判中国大学,引起了美国教育界人士对中国大学的激烈争论。 对中国大学近年来久盛不衰的“做大做强”...
EE直播间
更多
我要评论
2
6
关闭 站长推荐上一条 /3 下一条