LPC2000中利用定时器/计数器 0 进行系统定时,完成下面各部分内容 (Fpclk=11.0592MHZ)。 (1)写出定时器时钟为 2 分频,实现定时 1 秒时长的定时器初始化程序。 (2)写出定时器时钟不分频的情况下,每两秒时长利用外部匹配 0 实现匹配输出为方 波的初始化程序。 3)写出定时器时钟为不分频,测量下图脉冲宽度的程序段。
时间: 2024-02-16 13:00:00 浏览: 116
好的,我会根据你的要求进行回答。针对你的问题,以下是我的回答:
(1)定时器时钟为 2 分频,实现定时 1 秒时长的定时器初始化程序:
```c
#include <LPC21xx.h>
void timer_init() {
T0TCR = 0x02; // Reset Timer0
T0PR = 0x00; // Set Prescale to 0
T0MCR = 0x03; // Set Timer0 to Interrupt & Reset on match
T0MR0 = 5508000; // Set Match Register 0 to 1 second
T0TCR = 0x01; // Enable Timer0
}
```
(2)定时器时钟不分频的情况下,每两秒时长利用外部匹配 0 实现匹配输出为方波的初始化程序:
```c
#include <LPC21xx.h>
void timer_init() {
T0TCR = 0x02; // Reset Timer0
T0CTCR = 0x00; // Timer Mode: Every rising PCLK edge
T0MCR = 0x04; // Set T0MR0 to Reset on match
T0MR0 = 11059200; // Set Match Register 0 to 2 seconds
T0EMR = 0x30; // Set External Match 0 to Toggle on match
T0TCR = 0x01; // Enable Timer0
}
```
(3)定时器时钟为不分频,测量脉冲宽度的程序段:
```c
#include <LPC21xx.h>
void timer_init() {
T0TCR = 0x02; // Reset Timer0
T0CTCR = 0x00; // Timer Mode: Every rising PCLK edge
T0MCR = 0x04; // Set T0MR0 to Reset on match
T0EMR = 0x01; // Set External Match 0 to Enable Match
T0TCR = 0x01; // Enable Timer0
}
int measure_pulse_width() {
int pulse_width;
T0TCR = 0x02; // Reset Timer0
IO0DIR &= ~(1 << 2); // Set P0.2 to input
IO0SET = (1 << 2); // Set P0.2 to high
while ((IO0PIN & (1 << 2)) == 0); // Wait for rising edge
T0TCR = 0x01; // Enable Timer0
while ((IO0PIN & (1 << 2)) != 0); // Wait for falling edge
pulse_width = T0TC;
T0TCR = 0x02; // Disable Timer0
return pulse_width;
}
```
希望这些代码能够对你有所帮助!
阅读全文