[英]Setting high speed PWM on ATtiny85
我在ATtiny85上設置高速PWM時遇到問題。 我需要以400 kHz的速度使用PCK。 我相信我已經正確地遵循了數據手冊,但是由於某些原因,定時器中斷標志不起作用。
如果我對器件進行編程,則相應引腳的輸出為恆定5V。
如果我注釋掉PCK設置並改用系統時鍾,則標志已正確設置並且PWM可以正常工作。 代碼已發布。 為什么沒有設置標志並且PWM不起作用?
#include <avr/io.h>
#include <avr/interrupt.h>
int main(void)
{
PORTB = 0; //Reset values on port B
// After setting up the timer counter,
// set the direction of the ports to output
DDRB |= (1<<PB1) | (1<<PB0); // Set the direction of PB1 to an output
// PLLCSR - PLL control and status register:
// PLL is a clock multiplier - multiplies system 8 MHz by 8 to 64 MHz
// PLL is enabled when:PLLE bit is enabled,
// CKSEL fuse is programmed to 0001. This clock is
// switched off in sleep modes!
PLLCSR |= (1<<PLLE); // PLL enable
// Wait until the PLOCK bit is enabled
// before allowing the PCK to be enabled
//WaitForPLOCK();
//unsigned int i = 0;
while ((PLLCSR & (1<<PLOCK)) == 0x00)
{
// Do nothing until plock bit is set
}
PLLCSR |= (1<<PCKE); // Enable asynchronous mode, sets PWM clock source
TCCR1 =
(1<<CTC1) | // Enable PWM
(1<<PWM1A) | // Set source to pck
(1<<(CS10)) | // Clear the pin when match with ocr1x
(1<<COM1A1);
GTCCR = (1<<PWM1B) | (1<<COM1B1);
// Set PWM TOP value - counter will count and reset
// after reaching this value
// OCR1C
// 400 kHz 159
// 450 kHz 141
// 500 kHz 127
OCR1C = 159;
// Enable Timer1 OVF interrupt
TIMSK = (1<<OCIE1A) | (1<<TOIE1);
sei();
// This should set the duty cycle to about 75%
OCR1A = 120;
解決方案涉及CKDIV8保險絲。 為了正確編程此保險絲,需要HVSP“高壓串行編程”。 拆下此保險絲以使器件工作在8 MHz之后,PWM提供了400 kHz的輸出。 希望其他人覺得這有用!
勘誤表(數據表的第27節)指出“ PLL在6 MHz以下不會鎖定”。 列出的唯一解決方法是將時鍾設置為6 MHz或更高。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.