我知道这很愚蠢,但我真的很困惑。
我想在 Atmega16 上制作具有 3 种模式的 PWM 脉冲: 1- 1Khz 占空比 100% 2- 4Khz 占空比 100% 3- 1Khz 占空比 50%
我离开 AVR 快 2 年了,我忘记了一切,所以我只需要简单地计算 Timer 1。我读过的任何东西都让我更加困惑。有什么可以帮助我的吗?我正在使用 Codevision AVR。
我知道这很愚蠢,但我真的很困惑。
我想在 Atmega16 上制作具有 3 种模式的 PWM 脉冲: 1- 1Khz 占空比 100% 2- 4Khz 占空比 100% 3- 1Khz 占空比 50%
我离开 AVR 快 2 年了,我忘记了一切,所以我只需要简单地计算 Timer 1。我读过的任何东西都让我更加困惑。有什么可以帮助我的吗?我正在使用 Codevision AVR。
1- 1Khz with Duty cycle 100%
2- 4Khz with Duty cycle 100%
are the same thing. There is no actual PWM at all. The output is high all the time and frequency doesn't matter.
3- 1Khz with Duty cycle 50%
is actually PWM. There are a few types to choose from, but if the duty cycle is going to be exactly 50%, then there are easy ways to achieve this with a toggle. From the manual,
A frequency (with 50% duty cycle) waveform output in fast PWM mode can be achieved by setting OC1A to toggle its logical level on each compare match (COM1A1:0 = 1). This applies only if OCR1A is used to define the TOP value (WGM13:0 = 15).
That is, set the COM1A1 and COM1A0 bits of TCCR1A to be 01
, and set all the WGM bits of TCCR1A and TCCR1B to be 1
. Choose OCR1A and the CSx prescalar bits of TCCR1B so that OCR1A is reached every 0.5 ms.
我这样做了,但是我有考试,所以发布它的时间太长了。我将 Fcpu 设置为 4MHz
这是代码:
void set1KhzDC100()
{
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 500.000 kHz
// Mode: Fast PWM top=0x01FF
// OC1A output: Non-Inv.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x82;
TCCR1B=0x0A;
TCNT1H=0;
TCNT1L=0;
ICR1H=0x00;
ICR1L=0x00;
OCR1A=511;
}
void Set4KhzDC100()
{
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 4000.000 kHz
// Mode: Fast PWM top=0x03FF
// OC1A output: Non-Inv.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x83;
TCCR1B=0x09;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1A=1023;
OCR1BH=0x00;
OCR1BL=0x00;
}
void Set1KhzDC50()
{
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 500.000 kHz
// Mode: Fast PWM top=0x01FF
// OC1A output: Non-Inv.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x82;
TCCR1B=0x0A;
TCNT1H=0;
TCNT1L=0;
ICR1H=0x00;
ICR1L=0x00;
OCR1A=255;
}