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

📄 init_device.c

📁 设计大赛寻迹小车题目程序
💻 C
字号:
//ICC-AVR application builder : 2006-9-7 0:27:10
// Target : M32
// Crystal: 7.3728Mhz
#include "config.h"

void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0x00;
 DDRB  = 0xFF;
 PORTC = 0x00;
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x80;
}

//============timer1 PWM输出==================//
//TIMER0 initialize - prescale:1024
// desired value: 25mSec
// actual value: 25.000mSec (0.0%)
void timer0_init(void)
{
 TCCR0 = 0x00;  //stop
 TCNT0= 0x01;  //set count
 OCR0 = 0x66;  //set compare
 TCCR0 = (1<<WGM00)|(1<<WGM01)|(1<<COM01)|0x04;//0x04 0100B 代表256预分频
}

//TIMER1 initialize - prescale:8
// desired value: 50mSec
// actual value:  50mSec (0.0%)
void timer1_init(void) 
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0x4C; //setup
 TCNT1L = 0x01;
 OCR1AH = 0xB3;
 OCR1AL = 0xFF;
 OCR1BH = 0xB3;
 OCR1BL = 0xFF;
 ICR1H  = 0xB3;
 ICR1L  = 0xFF;
 TCCR1A = 0x00;
 TCCR1B = 0x02; //start Timer
}
/**************************timer2 PWM输出***********************/
// PWM频率 = 系统时钟频率/(分频系数*2*计数器上限值)) 
void timer2_init(void)
{
 TCCR2 = 0x00;  //stop
 TCNT2= 0x01;  //set count
 OCR2 = 0x66;  //set compare
 TCCR2 = (1<<WGM20)|(1<<WGM21)|(1<<COM21)|0x06; //  start timer 快速pwm模式,匹配清零,溢出置位 256分频
//高比低为:(OCR2-0X55)/(0XFF-OCR2)    OX55++++++(0X77)__________OXFF
//即OCR2越大,输出越大
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 timer2_init();
// timer1_init();
 timer0_init();
 MCUCR |= (1<<ISC01);    // INT0,INT1 下降沿产生中断请求
 //MCUCSR |= (1<<ISC2);             //INT2上升沿产生中断请求
 GICR |= (1<<INT0)|(1<<INT1);
 TIMSK = (1<<TOIE0)|(1<<TOIE2)|(1<<OCIE0)|(1<<OCIE2); //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

⌨️ 快捷键说明

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