您的位置 首页 发布

调试经过的AVR mega16 SPI双机通讯比如

//ICC-AVRapplicationbuilder:2007-7-1813:01:11//Target:M16//Crystal:7.3728Mhz//作者:古欣//AVR与虚拟仪器[…

//ICC-AVR application builder : 2007-7-18 13:01:11

// Target : M16
// Crystal: 7.3728Mhz
// 作者:古欣
// AVR与虚拟仪器 [url]http://www.avrvi.com[/url]
// 功用:SPI主机形式,循环发送从1~255

#include
#include

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

//SPI initialize
// clock rate: 57599hz
void spi_init(void)
{
PORTB |= (1<DDRB |= (1<SPCR = 0x73; //setup SPI
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
}

void SPI_MasterTransmit(char cData)
{
PORTB &=~ (1<SPCR |= (1</* 发动数据传输 */
SPDR = cData;
/* 等候传输完毕 */
while(!(SPSR & (1<;
PORTB |= (1<}

void Delay(void) //延时,没有具体核算
{
unsigned int i,j;
for(i=1000;i>0;i–)
{
for(j=200;j>0;j–)
;
}
}

void main(void)
{
unsigned char i=0;
init_devices();
while(1)
{
for(i=255;i>0;i–)
{
SPI_MasterTransmit(i);
Delay();
}
}
}

//ICC-AVR application builder : 2007-7-18 12:56:10
// Target : M16
// Crystal: 7.3728Mhz
// 作者:古欣
// AVR与虚拟仪器 [url]http://www.avrvi.com[/url]
// 功用:从机形式,中止方法接纳,并在LED上显现

#include
#include

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

//SPI initialize
// clock rate: 57599hz
void spi_init(void)
{
SPCR = 0xE3; //setup SPI
SPSR = 0x00; //setup SPI
}

#pragma interrupt_handler spi_stc_isr:11
void spi_stc_isr(void)
{
//byte in SPDR has been sent/received
PORTA = SPDR;
}

//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
}

void main(void)
{
init_devices();
DDRB|=(1<while(1)
;//等候中止
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/changshang/fabu/273201.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部