📄 main.c
字号:
//ICC-AVR application builder : 2008-6-16 18:47:52
// Target : M16
// Crystal: 16.000Mhz
//#define MASTER
#include <iom16v.h>
#include <macros.h>
#include "delay.h"
#ifdef MASTER
#warning "build as MASTER SPI"
#else
#warning "build as SLAVE SPI"
#endif
void port_init(void)
{
PORTA = 0xff;
DDRA = 0xff;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//SPI initialize
// clock rate: 1000000hz
void spi_init(void)
{
#ifdef MASTER
SPCR = 0x51; //setup SPI Master
#else
SPCR = 0x41|0x80; //setup SPI Slave
#endif
SPSR = 0x00; //setup SPI
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
spi_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
#pragma interrupt_handler SPI_RXC_ISR:18
void SPI_RXC_ISR(void)
{
//unsigned char data_temp;
//SPDR=0xab;
//data_temp=SPDR;
#ifdef MASTER
#else
PORTA=SPDR;
#endif
}
//
void main(void)
{
unsigned char n=0;
init_devices();
//insert your functional code here...
#ifdef MASTER
DDRB|=0x10;
PORTB|=0x10;
#else
PORTB|=0x10;
#endif
while(1)
{
n++;
#ifdef MASTER
PORTB&=0xef;
SPDR=n;
/* 启动数据传输 */
/* 等待传输结束 */
while(!(SPSR & (1<<SPIF)));
PORTB|=0x10;
#else
//PORTA=~n;
#endif
delay_nms(200);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -