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

📄 init._c

📁 一个用mega8开发的速度发生器,速度比较准确,值得下载
💻 _C
字号:
//ICC-AVR application builder : 2006-10-11 上午 11:14:38
// Target : M8
// Crystal: 6.0000Mhz

#include <iom8v.h>
#include <macros.h>

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

//Watchdog initialize
// prescale: 16K 
void watchdog_init(void)
{
 WDR(); //this prevents a timout on enabling
 WDTCR = 0x08; //WATCHDOG ENABLED - dont forget to issue WDRs
}

//TIMER1 initialize - prescale:1
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 320Hz
// actual value: 320.000Hz (0.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 
 TCNT1H = 0xB6; //setup
 TCNT1L = 0xC2;
 
 OCR1AH = 0x49;
 OCR1AL = 0x3E;
 OCR1BH = 0x49;
 OCR1BL = 0x3E;
 ICR1H  = 0x49;
 ICR1L  = 0x3E;
 TCCR1A = 0x00;
 
 TCCR1B = 0x01; //start Timer
}

#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
 //TIMER1 has overflowed
 TCNT1H = 0xB6; //reload counter high value
 TCNT1L = 0xC2; //reload counter low value
}

//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
 UCSRB = 0x00; //disable while setting baud rate
 UCSRA = 0x00;
 UCSRC = BIT(URSEL) | 0x06;
 UBRRL = 0x26; //set baud rate lo
 UBRRH = 0x00; //set baud rate hi
 UCSRB = 0xD8;
}

#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
 //uart has received a character in UDR
}

#pragma interrupt_handler uart0_tx_isr:14
void uart0_tx_isr(void)
{
 //character has been transmitted
}

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

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

void main(void)
{
 	 init_devices();
	 //while(1)
	 //{
	 // WDR(); 
	 //}
}

⌨️ 快捷键说明

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