?? pwm.c
字號:
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include "iocompat.h"
ISR (TIMER0_OVF_vect)
{
static uint16_t pwm;
static uint8_t direction=0;
if(direction==0)
{
if(pwm<=250)
pwm*=1.10;
else
{ direction=1;
}
}
if(direction==1)
{
if(pwm>=0)
pwm*=0.9;
else
direction=0;
}
OCR1B=pwm;
}
//端口初始化
void port_init(void)
{
PORTB = 0x00;
DDRB = 0xFF;
}
//定時器T0初始化
void timer0_init(void)
{
TCCR0=0x06;//系統(tǒng)時鐘64分頻 8MHZ 每中斷一次時間是 1/(8000000/64/(0xFF-0x06+1))=1/500s
// TCCR0A=_BV(COM1B1)|_BV(COM1A1)|_BV(WGM10);
// TCCR0B=_BV(CS11)|_BV(CS10);//時鐘選擇
TCNT0=0x06;
}
void init_devices(void)
{
MCUCR = 0x00; //電源管理控制,空閑模式
GICR = 0x00; //通用中斷控制寄存器
port_init();
timer0_init();
sei();//開全局中斷
}
int main (void)
{
init_devices(void) ;
/* loop forever, the interrupts are doing the rest */
while(1);
return (0);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -