【发布时间】:2016-05-08 23:13:04
【问题描述】:
如果在初始 Pragma 配置后给定 187.5hz,则大约为 1190,以便有 1 秒延迟。我被困在如何计算这个数字上。请帮忙!
这是初始编译指示配置
#pragma config JTAGEN = OFF // JTAG Enable OFF (only use for '250)
#pragma config FNOSC = FRCPLL // Fast RC w PLL 8mHz internal rc Osc
#pragma config FPLLIDIV = DIV_10 // PLL in 8mHz/10 = 800 kHz
#pragma config FPLLMUL = MUL_15 // PLL mul 800 khz * 15 = 12 Mhz
#pragma config FPLLODIV = DIV_64 // PLL Out: 12MHz / 64 187.5 kHz
#pragma config FPBDIV = DIV_1 // Peripheral Bus Divisor
#pragma config FCKSM = CSECME // Clock Switch Enable, FSCM Enabled
#pragma config POSCMOD = OFF // Primary osc disabled
#pragma config IESO = OFF // Internal/external switch over
#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin
#pragma config FWDTEN = OFF // Watchdog Timer Enable:
1190 是这样来的
// Delay routine
// With an 8MHz internal RC clock and settings to give a system clock
// of 187.5 kHz, the delay() function must be given about 1190 to get 1 sec
// delay:
// stopwatch: 100 complete ON/OFF pulses in 119 seconds.
// 500/((100/119)/2) = 1190
void delay(int d)
{
int i; // Declare variable to be used in the loop
while(d) // While d > 0
{
i = 100; // set i to 100 for inner loop
while(i) // while i > 0
{
i--; // decrement i (e.g. i will equal 99, 98, 97,...)
}
d--; // decrement d
}
}
即使看了评论,我仍然不知道数字是如何计算的......
【问题讨论】:
-
编写
delay函数,检查汇编代码,然后查看CPU的文档,知道每条指令需要多少个周期,以及循环(或循环)的总周期数.然后,如果您知道一段代码需要多少个周期,那么当您拥有时钟频率(它决定每秒的周期数)时,很容易计算出作为参数所需的值。 -
你在为什么芯片编程?通常你会使用某种中断来实现延迟。
-
没有可以查询的硬件时钟(亚秒级精度)吗?简单的旧忙等待延迟循环可能很难调整且耗时。你是编译到实际硬件还是模拟器?
-
@FUZxxl 我正在为 PIC32MX250F128B 编程
-
@JoachimPileborg 我的芯片是 32 位的,如何在数据表中查找周期?每个乘法和除法是否不同?