c++ - 这个常量不正确吗?

标签 c++ timer arduino atmel

我目前正在阅读来自 atmel 的应用说明,我目前正在尝试了解 code .该代码生成一个梯形斜坡,用于控制步进电机。

我不明白代码的一件事是常量设置为

// Timer/Counter 1 running on 3,686MHz / 8 = 460,75kHz (2,17uS). (T1-FREQ 460750)
#define T1_FREQ 1382400

我不确定我是否理解为什么该值是 1382400 而不是 460750,因为按 8 预分频的时钟频率是 460750... 那么为什么是 1382400?我错过了什么吗?

.h文件

/*This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
 *
 * \brief Header file for speed_cntr.c.
 *
 * - File:               speed_cntr.h
 * - Compiler:           IAR EWAAVR 4.11A
 * - Supported devices:  All devices with a 16 bit timer can be used.
 *                       The example is written for ATmega48
 * - AppNote:            AVR446 - Linear speed control of stepper motor
 *
 * \author               Atmel Corporation: http://www.atmel.com \n
 *                       Support email: avr@atmel.com
 *
 * $Name: RELEASE_1_0 $
 * $Revision: 1.2 $
 * $RCSfile: speed_cntr.h,v $
 * $Date: 2006/05/08 12:25:58 $
 *****************************************************************************/

#ifndef SPEED_CNTR_H
#define SPEED_CNTR_H

/*! \brief Holding data used by timer interrupt for speed ramp calculation.
 *
 *  Contains data used by timer interrupt to calculate speed profile.
 *  Data is written to it by move(), when stepper motor is moving (timer
 *  interrupt running) data is read/updated when calculating a new step_delay
 */
typedef struct {
  //! What part of the speed ramp we are in.
  unsigned char run_state : 3;
  //! Direction stepper motor should move.
  unsigned char dir : 1;
  //! Peroid of next timer delay. At start this value set the accelration rate.
  unsigned int step_delay;
  //! What step_pos to start decelaration
  unsigned int decel_start;
  //! Sets deceleration rate.
  signed int decel_val;
  //! Minimum time delay (max speed)
  signed int min_delay;
  //! Counter used when accelerateing/decelerateing to calculate step_delay.
  signed int accel_count;
} speedRampData;

/*! \Brief Frequency of timer1 in [Hz].
 *
 * Modify this according to frequency used. Because of the prescaler setting,
 * the timer1 frequency is the clock frequency divided by 8.
 */
// Timer/Counter 1 running on 3,686MHz / 8 = 460,75kHz (2,17uS). (T1-FREQ 460750)
#define T1_FREQ 1382400

//! Number of (full)steps per round on stepper motor in use.
#define FSPR 200

#ifdef HALFSTEPS
  #define SPR (FSPR*2)
  #pragma message("[speed_cntr.c] *** Using Halfsteps ***")
#endif
#ifdef FULLSTEPS
  #define SPR FSPR
  #pragma message("[speed_cntr.c] *** Using Fullsteps ***")
#endif
#ifndef HALFSTEPS
  #ifndef FULLSTEPS
    #error FULLSTEPS/HALFSTEPS not defined!
  #endif
#endif

// Maths constants. To simplify maths when calculating in speed_cntr_Move().
#define ALPHA (2*3.14159/SPR)                    // 2*pi/spr
#define A_T_x100 ((long)(ALPHA*T1_FREQ*100))     // (ALPHA / T1_FREQ)*100
#define T1_FREQ_148 ((int)((T1_FREQ*0.676)/100)) // divided by 100 and scaled by 0.676
#define A_SQ (long)(ALPHA*2*10000000000)         // ALPHA*2*10000000000
#define A_x20000 (int)(ALPHA*20000)              // ALPHA*20000

// Speed ramp states
#define STOP  0
#define ACCEL 1
#define DECEL 2
#define RUN   3

void speed_cntr_Move(signed int step, unsigned int accel, unsigned int decel, unsigned int speed);
void speed_cntr_Init_Timer1(void);
static unsigned long sqrt(unsigned long v);
unsigned int min(unsigned int x, unsigned int y);

//! Global status flags
extern struct GLOBAL_FLAGS status;

#endif

最佳答案

/*! \Brief Frequency of timer1 in [Hz].
 *

注释告诉您根据需要修改 T1_FREQ。

 * Modify this according to frequency used. Because of the prescaler setting,
 * the timer1 frequency is the clock frequency divided by 8.
 */

这一行给你一个计算的例子。这只是一个例子。如果愿意,他们本可以使用完全不同的数字。他们不会在每次使用不同的预分频器或频率时都更改通用示例。

// Timer/Counter 1 running on 3,686MHz / 8 = 460,75kHz (2,17uS). (T1-FREQ 460750)

这一行是您应该根据需要更改的行。 1382400 是他们在代码中使用的。如果您的情况需要 460750,请替换数字...

#define T1_FREQ 1382400

关于c++ - 这个常量不正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41102925/

相关文章:

c++ - 异步接口(interface)托管 -> 非托管代码

c++ - 我如何编码类似std::variant的开关?

c# - 带有 TimerCallback 和 TimeSpan 参数的计时器未循环调用回调

jquery - 同时使用 setTimeout 和 setInterval 可以吗?

android - 为什么我的毫秒数倒计时不一致?

c++ - 为什么arduino中有两个if语句?

arrays - Arduino 类数组成员

c++ - 如何使用 visual c++ 从 wireshark 读取 pcap 文件

C++ 类循环引用?

arduino - 如何在arduino flex传感器中以45度开始点亮?