📄 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;//系统时钟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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -