⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pwm_ctc.c

📁 avr单片机pwm功能的使用
💻 C
字号:
//ICC-AVR application builder : 2008-8-4 下午 05:07:24
// Target : M48
// Crystal: 1.0000Mhz

#include <iom48v.h>
#include <macros.h>

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

//TIMER0 initialize - prescale:8
// WGM: CTC
// desired value: 1000Hz
// actual value: 992.063Hz (-0.8%)
void timer0_init(void)
{
 TCCR0B = 0x00; //stop
 TCNT0 = 0x01; //set count
 TCCR0A = 0x72; 
 TCCR0B = 0x0b; //start timer
 OCR0A= 0x80;
}

#pragma interrupt_handler timer0_compa_isr:15
void timer0_compa_isr(void)
{
 TCNT0=0x01;
 OCR0A= 0x80;
 //compare TCNT0=OCR0A
}

#pragma interrupt_handler timer0_compb_isr:16
void timer0_compb_isr(void)
{
 //compare TCNT0=OCR0B
}

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

 MCUCR = 0x00;
 EICRA = 0x00; //extended ext ints
 EIMSK = 0x00;
 
 TIMSK0 = 0x06; //timer 0 interrupt sources
 TIMSK1 = 0x00; //timer 1 interrupt sources
 TIMSK2 = 0x00; //timer 2 interrupt sources
 
 PCMSK0 = 0x00; //pin change mask 0 
 PCMSK1 = 0x00; //pin change mask 1 
 PCMSK2 = 0x00; //pin change mask 2
 PCICR = 0x00; //pin change enable 
 PRR = 0x00; //power controller
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}
void main()
{
init_devices();

while(1)

{

}

}

⌨️ 快捷键说明

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