打印
[AVR单片机]

AVR mega16 SPI双机通讯例子 (为新手设计,简单易懂)

[复制链接]
3613|13
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
suoma|  楼主 | 2009-11-16 22:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本程序实现的功能:主机发送1~255,丛机接收并在LED上显示出来。

连接方式: 两个mega16最小系统板 PB4到PB7全部对连。
主机程序:
CODE:

//ICC-AVR application builder : 2007-7-18 13:01:11
// Target : M16
// Crystal: 7.3728Mhz
// 作者:古欣
// AVR与虚拟仪器 http://www.avrvi.com
// 功能:SPI主机模式,循环发送从1~255

#include <iom16v.h>
#include <macros.h>

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<<PB4) | (1<<PB5) | (1<<PB6) | (1<<PB7);
DDRB |= (1<<DDB5) | (1<<DDB7) | (1<<DDB4);     //Set MOSI, SCK AND SS as outputs
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<<PB4); //强制接收方进入从模式
SPCR |= (1<<MSTR); // MSTR有时会被清零,这里强制进入主机模式
/* 启动数据传输 */
SPDR = cData;
/* 等待传输结束 */
while(!(SPSR & (1<<SPIF)))
;
PORTB |= (1<<PB4);
}

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();
}
}
}

相关帖子

沙发
suoma|  楼主 | 2009-11-16 22:27 | 只看该作者
从机程序


CODE:

//ICC-AVR application builder : 2007-7-18 12:56:10
// Target : M16
// Crystal: 7.3728Mhz
// 作者:古欣
// AVR与虚拟仪器 http://www.avrvi.com
// 功能:从机模式,中断方式接收,并在LED上显示

#include <iom16v.h>
#include <macros.h>

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<<PB6); //MOSI 设置为输出
while(1)
;//等待中断
}

使用特权

评论回复
评分
参与人数 1威望 +1 收起 理由
lion1899 + 1 很给力!
板凳
Karlshen| | 2009-11-16 22:33 | 只看该作者
看帖子也给分啊:lol

使用特权

评论回复
地板
suoma|  楼主 | 2009-11-17 14:08 | 只看该作者
恩,所以要勤快点哦

使用特权

评论回复
5
Jimdo260122332| | 2009-11-24 09:23 | 只看该作者
:)

使用特权

评论回复
6
错过-21IC| | 2009-11-24 13:04 | 只看该作者
我在虚拟论坛看过,不错的

使用特权

评论回复
7
328500920| | 2009-11-25 23:15 | 只看该作者
支持

使用特权

评论回复
8
suoma|  楼主 | 2009-12-2 22:33 | 只看该作者
:)

使用特权

评论回复
9
zhao3462910| | 2013-5-14 09:27 | 只看该作者
支持··

使用特权

评论回复
10
lion1899| | 2013-6-15 18:41 | 只看该作者
支持+1!!

使用特权

评论回复
11
Frank2013| | 2013-6-21 12:58 | 只看该作者
学习一下

使用特权

评论回复
12
wentao0100| | 2013-7-3 22:41 | 只看该作者
来学习的。

使用特权

评论回复
13
回到从前| | 2013-7-4 16:10 | 只看该作者
来学习一下!

使用特权

评论回复
14
cclgxuanshao| | 2016-3-9 15:18 | 只看该作者
MARK一下

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

82

主题

714

帖子

5

粉丝