/* * T1_T2_T3_2023_1.c * * Created: 2023/5/30 22:49:53 * Author : XY */ #include <avr/io.h> #include <avr/interrupt.h> #include <avr/sleep.h> void device_init(void) { DDRD |= 0xf0; PORTD &= 0x0f; DDRB |= (_BV(PB4)|_BV(PB6)); DDRE |= _BV(PE7); } void timer1_init(void) { TCCR1B = 0x00; //stop OCR1AH = 0x3D; //TOP of T1 OCR1AL = 0x08; OCR1BH = 0x3D; OCR1BL = 0x08; TCCR1A = 0x10; TCCR1C = 0x00; TCCR1B = 0x0B; //start Timer } void timer2_init(void) { TCCR2B = 0x00; //stop ASSR = 0x20; //set async mode OCR2A = 0x80; TCCR2A = 0x42; TCCR2B = 0x06; //start } void timer3_init(void) { TCCR3B = 0x00; //stop OCR3AH = 0x3D; OCR3AL = 0x08; TCCR3A = 0x00; TCCR3C = 0x00; TCCR3B = 0x0B; //start Timer } ISR(TIMER3_COMPA_vect) { PORTE ^= _BV(PE7); } int main(void) { device_init(); timer1_init(); timer2_init(); timer3_init(); TIMSK3 = 0x02; sei(); while (1) { SMCR |= (0x00<<SM0); sleep_mode(); } } 根据程序中 T1、T2、T3 的配置参数(包括工作模式、分频系数、初值),通过更改SMCR |= (0x00<<SM0)为SMCR |= (0x03<<SM0)和SMCR |= (0x02<<SM0)来分析三个定时器驱动 LED 灯的方式有何区别
时间: 2024-03-19 09:40:08 浏览: 66
根据程序中 T1、T2、T3 的配置参数,三个定时器的工作模式都是 CTC 模式。它们的分频系数、初值分别为:
- T1:分频系数为 64,初值为 15624。
- T2:分频系数为 64,初值为 128。
- T3:分频系数为 64,初值为 15624。
这三个定时器都会产生中断(T3 产生的中断由 ISR(TIMER3_COMPA_vect) 处理),并且在中断处理函数中,PE7 口的电平状态会翻转,从而驱动 LED 灯。
在程序中,通过更改SMCR |= (0x00<<SM0)为SMCR |= (0x03<<SM0)和SMCR |= (0x02<<SM0),可以分别将 ATmega128A 进入 Power-down 模式和 Idle 模式。这两种模式的区别在于,Power-down 模式下,CPU 和所有模块都被关闭,只有复位和外部中断能唤醒 MCU;而在 Idle 模式下,只有 CPU 被关闭,其他模块继续工作,外部中断和定时器中断都能唤醒 MCU。
因此,当将 ATmega128A 进入 Power-down 模式时,所有定时器和 LED 灯都会停止工作;而当将 ATmega128A 进入 Idle 模式时,定时器继续工作,LED 灯也会继续闪烁。
相关问题
阅读下列程序,完成填空。 // //////////////////////////////////////////////////////// // 简介: // 通过接口Runnable构造线程的例程。 // //////////////////////////////////////////////////////// /**********FILL**********/ public class J_ThreadRunnable implements ___________{ private int m_threadID; public J_ThreadRunnable(int i) { m_threadID=i; System.out.println("创建线程: " + i ); } // J_ThreadRunnable构造方法结束 /**********FILL**********/ public ____________( ) { for(int i=0; i<2; i++) { System.out.println("运行线程: " + m_threadID); try { Thread.sleep((int)(Math.random( ) * 1000)); } catch (InterruptedException e ){ System.err.println("异常InterruptedException: " + e); e.printStackTrace( ); } // try-catch结构结束 } // for循环结束 } // 方法run结束 public static void main( String args[ ] ){ Thread t1= new Thread(new J_ThreadRunnable(1)); Thread t2= new Thread(new J_ThreadRunnable(2)); /**********FILL**********/ ________ ; /**********FILL**********/ ________ ; System.out.println("方法main结束"); } // 方法main结束 } // 类J_ThreadRunnable结束
第一空应填写Runnable,第二空应填写run方法,第三空应该是t1.start(),第四空应该是t2.start()。修正后的代码如下:
```
public class J_ThreadRunnable implements Runnable {
private int m_threadID;
public J_ThreadRunnable(int i) {
m_threadID=i;
System.out.println("创建线程: " + i );
}
public void run() {
for(int i=0; i<2; i++) {
System.out.println("运行线程: " + m_threadID);
try {
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
System.err.println("异常InterruptedException: " + e);
e.printStackTrace();
}
}
}
public static void main(String args[]) {
Thread t1 = new Thread(new J_ThreadRunnable(1));
Thread t2 = new Thread(new J_ThreadRunnable(2));
t1.start();
t2.start();
System.out.println("方法main结束");
}
}
```
/* * T1_T2_T3_2023_1.c * * Created: 2023/5/30 22:49:53 * Author : XY */ #include <avr/io.h> #include <avr/interrupt.h> #include <avr/sleep.h> void device_init(void) { DDRD |= 0xf0; PORTD &= 0x0f; DDRB |= (_BV(PB4)|_BV(PB6)); DDRE |= _BV(PE7); } void timer1_init(void) { TCCR1B = 0x00; //stop OCR1AH = 0x3D; //TOP of T1 OCR1AL = 0x08; OCR1BH = 0x3D; OCR1BL = 0x08; TCCR1A = 0x10; TCCR1C = 0x00; TCCR1B = 0x0B; //start Timer } void timer2_init(void) { TCCR2B = 0x00; //stop ASSR = 0x20; //set async mode OCR2A = 0x80; TCCR2A = 0x42; TCCR2B = 0x06; //start } void timer3_init(void) { TCCR3B = 0x00; //stop OCR3AH = 0x3D; OCR3AL = 0x08; TCCR3A = 0x00; TCCR3C = 0x00; TCCR3B = 0x0B; //start Timer } ISR(TIMER3_COMPA_vect) { PORTE ^= _BV(PE7); } int main(void) { device_init(); timer1_init(); timer2_init(); timer3_init(); TIMSK3 = 0x02; sei(); while (1) { SMCR |= (0x00<<SM0); sleep_mode(); } }在SMCR |= (0x00<<SM0),SMCR |= (0x00<<SM0),SMCR |= (0x02<<SM0)三种情况下的运行结果
在 SMCR |= (0x00<<SM0)的情况下,MCU 进入 Power-save 模式,此时所有定时器正常工作,LED 灯的亮灭由程序控制。由于程序中没有设定 LED 灯的状态,因此 LED 灯可能会一直亮着或熄灭,具体状态不确定。
在 SMCR |= (0x00<<SM0)的情况下,MCU 仍然进入 Power-save 模式,定时器 T1 和 T3 仍然正常工作,LED 灯的亮灭由定时器控制。定时器 T2 停止工作,LED 灯将保持上一次的状态。由于程序中没有设定 LED 灯的初始状态,因此 LED 灯可能会一直亮着或熄灭,具体状态不确定。
在 SMCR |= (0x02<<SM0)的情况下,MCU 进入 Idle 模式,定时器 T1 和 T3 仍然正常工作,LED 灯的亮灭由定时器控制。定时器 T2 停止工作,LED 灯将保持上一次的状态。由于 MCU 进入 Idle 模式,处理器将停止工作,直到下一个中断出现。因此程序中的 while(1) 循环将不再执行,程序将停留在 sleep_mode() 语句处,直到下一个中断出现。在该模式下,MCU 的功耗比 Power-save 模式低,但是定时器 T2 停止工作,可能会影响系统的实时性能。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)