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

📄 init.c

📁 avr单片机与flash存储器通信程序,调试通过,希望对大家有所帮助.
💻 C
字号:
//ICC-AVR application builder : 2006-4-28 17:54:09
// Target : M8515
// Crystal: 11.059Mhz

#include <iom8515v.h>
#include <macros.h>

void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0xFF;
 PORTB = 0x00;
 DDRB  = 0xBF;
 PORTC = 0xE0;
 DDRC  = 0xFC;
 PORTD = 0x20;
 DDRD  = 0x22; 
 PORTE = 0x00;
 DDRE  = 0x00; 
}

//TIMER1 initialize - prescale:1
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1mSec
// actual value:  1.000mSec (0.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xD4; //setup
 TCNT1L = 0xCE;
 OCR1AH = 0x2B;
 OCR1AL = 0x32;
 OCR1BH = 0x2B;
 OCR1BL = 0x32;
 TCCR1A = 0x00;
 TCCR1B = 0x01; //start Timer
}

unsigned int count =0;
extern unsigned char ledflag;
extern unsigned char shuflag;
extern unsigned char lcdflag;
#pragma interrupt_handler timer1_ovf_isr:7
void timer1_ovf_isr(void)
{
 TCNT1H = 0xD4; //reload counter high value
 TCNT1L = 0xCE; //reload counter low value
 if((++count&0xff)==0)ledflag=1;
 if((count&0x3ff)==0)shuflag=1;
 if((count&0x1ff)==0)lcdflag=1;
 
}

//SPI initialize
// clock rate: 5529498hz
void spi_init(void)
{
 SPCR = 0xFC; //setup SPI
}

#pragma interrupt_handler spi_stc_isr:9
void spi_stc_isr(void)
{
 //byte in SPDR has been sent/received
 PORTB^=0x10;
 PORTB^=0x10;
}

//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
void uart0_init(void)
{
 UCSRB = 0x00; //disable while setting baud rate
 UCSRA = 0x00;
 UCSRC = BIT(URSEL) | 0x06;
 UBRRL = 0x47; //set baud rate lo
 UBRRH = 0x00; //set baud rate hi
 UCSRB = 0x18;
}

//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();
 spi_init();
 uart0_init();

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

⌨️ 快捷键说明

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