pwm.c

来自「16个单片机c语言实验程序」· C语言 代码 · 共 55 行

C
55
字号
//PWM输出程序
//在端口PD4,PD5(led5,led6)输出PWM脉冲,由于占空比不同,显示亮度不同
#include <iom16v.h>
#include <macros.h>

void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0xff;
 DDRD  = 0xff;
}

//TIMER1 initialize - prescale:64
// WGM: 10) PWM phz correct, TOP= ICRn
// desired value: 1000Hz
// actual value: 1000.000Hz (0.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xFF; //setup
 TCNT1L = 0x83;
 OCR1AH = 0x00;
 OCR1AL = 0x65; //占空比
 OCR1BH = 0x00;
 OCR1BL = 0x10; //占空比
 ICR1H  = 0x00;
 ICR1L  = 0x7D;
 TCCR1A = 0xA2;
 TCCR1B = 0x13; //start Timer
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 timer1_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

main() 
{
init_devices();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?