SPI中断实现单片机之间的通信.doc_第1页
SPI中断实现单片机之间的通信.doc_第2页
SPI中断实现单片机之间的通信.doc_第3页
SPI中断实现单片机之间的通信.doc_第4页
SPI中断实现单片机之间的通信.doc_第5页
免费预览已结束,剩余2页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/*ATmega16&SPI中断实现主从的数据交换*FileName:master.cAuthor:沧海麒麟Date:2011.05.26Version :1.1Description:ATmega16单片机之间的简单的通信,利用中断的方法实现 主从机之间的各八位数据的交换。*/#include /常用头文件,如有需要可以查库文件include#include #include #include #pragma interrupt_handler spi_stc_isr:iv_SPI_STC /11#define uint unsigned int#define uchar unsigned char#define SS_1 (PORTB |= BIT(4)/PB4#define SS_0 (PORTB &= BIT(4)#define xtal 8uchar TXbuffer8 = 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f;uchar RXbuffer8 = 0,0,0,0,0,0,0,0;uchar cnt = 0;uchar M_flag = 0;void Delay_1ms(void);void Delay_nms(uint n);void SPI_MasterPort_init(void);void SPI_MasterRegisters_init(void);/*/void main(void) uchar i=0; DDRA = 0xFF; SPI_MasterPort_init(); SPI_MasterRegisters_init(); CLI(); for(i=0;i8;i+) PORTA = TXbufferi; Delay_nms(500); SEI(); SS_0; SPDR = TXbuffer0; while (1) if (M_flag = 1) for(i=0;i8;i+) PORTA = RXbufferi; Delay_nms(500); / end of while/*void Delay_1ms(void) uint i;for (i=1;i(uint)(xtal*143-2);i+);/=void Delay_nms(uint n) uint i=0; while (in) Delay_1ms(); i+; /*=从机SPI引脚方向定义=*/void SPI_MasterPort_init(void)PORTB |= 0x50; /SS(PB4自定义)MOSI(PB5自定义)DDRB |= 0xB0; /MISO(PB6输入)SCK(PB7自定义)/=void SPI_MasterRegisters_init(void)/中断方式定义SPCR = (1 SPIE)|(1 SPE)|(1 MSTR)|(1 =7) cnt = 0; M_flag = 1; cnt+; SPDR = TXbuffercnt;/byte in SPDR has been sent/received/*ATmega16&SPI中断实现主从的数据交换*FileName:slave.cAuthor:沧海麒麟Date:2011.05.26Version :1.1Description:ATmega16单片机之间的简单的通信,利用中断的方法实现 主从机之间的各八位数据的交换。*/#include /常用头文件,如有需要可以查库文件include#include #include #include #pragma interrupt_handler spi_stc_isr:iv_SPI_STC /11#define uint unsigned int#define uchar unsigned char#define xtal 8uchar RXb8 = 0,0,0,0,0,0,0,0;uchar TXb8 = 0x7f,0xbf,0xef,0xf7,0xfb,0xdf,0xfd,0xfe;uchar S_flag=0;uchar cnt=0;void Delay_1ms(void);void Delay_nms(uint n);void SPI_SlavePort_init(void);void SPI_SlaveRegisters_init(void);/*/void main(void) uchar i; DDRA=0xFF; SPI_SlavePort_init(); SPI_SlaveRegisters_init(); SEI(); SPDR = TXb0; while(1) if(S_flag=1) for(i=0;i8;i+) PORTA = RXbi; Delay_nms(500); /end of if /end of while /*void Delay_1ms(void) uint i; for(i=1;i(uint)(xtal*143-2);i+) ;/=void Delay_nms(uint n) uint i=0; while(in) Delay_1ms(); i+; /*=从机SPI引脚方向定义=*/void SPI_SlavePort_init(void)PORTB |= 0xF0; /SS(PB4输入)MOSI(PB5输入)DDRB |= 0x00; /MISO(PB6自定义

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论