找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4674|回复: 0
打印 上一主题 下一主题
收起左侧

ATMEGA16单片机usart通信(主从多机通信)proteus仿真与源码

[复制链接]
跳转到指定楼层
楼主
ATMEGA16 usart通信仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


一共3个单片机,一个主机 2个从机实现多机通信效果



全部资料51hei下载地址:
usart通信.zip (188.5 KB, 下载次数: 80)


部分单片机源程序如下:
  1. /*****************************************************
  2. This program was produced by the
  3. CodeWizardAVR V2.05.1b Evaluation
  4. Automatic Program Generator
  5. ?Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.

  6. Project :
  7. Version :
  8. Date    : 2017/12/21
  9. Author  : Freeware, for evaluation and non-commercial use only
  10. Company :
  11. Comments:


  12. Chip type               : ATmega16
  13. Program type            : Application
  14. AVR Core Clock frequency: 4.000000 MHz
  15. Memory model            : Small
  16. External RAM size       : 0
  17. Data Stack size         : 256
  18. *****************************************************/

  19. #include <mega16.h>

  20. flash unsigned char led_7[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
  21. unsigned char point=0;


  22. // External Interrupt 0 service routine
  23. interrupt [USART_RXC] void usart_rx_isr(void)          //接收中断服务
  24. {
  25.   unsigned char status,resh,resl;
  26.   status=UCSRA;
  27.   resh=UCSRB;
  28.   resl=UDR;
  29.   if((status&(1<<4|(1<<3)|1<<2)) ==0)
  30.   {
  31.     resh=(resh>>1)&0x01;
  32.    
  33.     if (resh==0)
  34.     PORTA=led_7[resl];
  35.     else
  36.     PORTA=0x00;
  37.         
  38.   }
  39. #asm("sei")
  40. }
  41. interrupt [EXT_INT0] void ext_int0_isr(void)
  42. {
  43. // Place your code here
  44. while(!(UCSRA&(1<<5)));
  45. UDR=point;
  46. #asm("sei")



  47. }

  48. // Standard Input/Output functions
  49. #include <stdio.h>

  50. // Declare your global variables here

  51. void main(void)
  52. {
  53. // Declare your local variables here

  54. // Input/Output Ports initialization
  55. // Port A initialization
  56. // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
  57. // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
  58. PORTA=0x00;
  59. DDRA=0xFF;

  60. // Port B initialization
  61. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  62. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  63. PORTB=0x00;
  64. DDRB=0x00;

  65. // Port C initialization
  66. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  67. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  68. PORTC=0x00;
  69. DDRC=0x00;

  70. // Port D initialization
  71. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  72. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  73. PORTD=0x03;
  74. DDRD=0x02;

  75. // Timer/Counter 0 initialization
  76. // Clock source: System Clock
  77. // Clock value: Timer 0 Stopped
  78. // Mode: Normal top=0xFF
  79. // OC0 output: Disconnected
  80. TCCR0=0x00;
  81. TCNT0=0x00;
  82. OCR0=0x00;

  83. // Timer/Counter 1 initialization
  84. // Clock source: System Clock
  85. // Clock value: Timer1 Stopped
  86. // Mode: Normal top=0xFFFF
  87. // OC1A output: Discon.
  88. // OC1B output: Discon.
  89. // Noise Canceler: Off
  90. // Input Capture on Falling Edge
  91. // Timer1 Overflow Interrupt: Off
  92. // Input Capture Interrupt: Off
  93. // Compare A Match Interrupt: Off
  94. // Compare B Match Interrupt: Off
  95. TCCR1A=0x00;
  96. TCCR1B=0x00;
  97. TCNT1H=0x00;
  98. TCNT1L=0x00;
  99. ICR1H=0x00;
  100. ICR1L=0x00;
  101. OCR1AH=0x00;
  102. OCR1AL=0x00;
  103. OCR1BH=0x00;
  104. OCR1BL=0x00;

  105. // Timer/Counter 2 initialization
  106. // Clock source: System Clock
  107. // Clock value: Timer2 Stopped
  108. // Mode: Normal top=0xFF
  109. // OC2 output: Disconnected
  110. ASSR=0x00;
  111. TCCR2=0x00;
  112. TCNT2=0x00;
  113. OCR2=0x00;

  114. // External Interrupt(s) initialization
  115. // INT0: On
  116. // INT0 Mode: Falling Edge
  117. // INT1: Off
  118. // INT2: Off
  119. GICR|=0x40;
  120. MCUCR=0x02;
  121. MCUCSR=0x00;
  122. GIFR=0x40;

  123. // Timer(s)/Counter(s) Interrupt(s) initialization
  124. TIMSK=0x00;

  125. // USART initialization
  126. // Communication Parameters: 8 Data, 1 Stop, No Parity
  127. // USART Receiver: On
  128. // USART Transmitter: On
  129. // USART Mode: Asynchronous
  130. // USART Baud Rate: 9600
  131. UCSRA=0x00;
  132. UCSRB=0x9d;
  133. UCSRC=0x86;
  134. UBRRH=0x00;
  135. UBRRL=0x19;

  136. // Analog Comparator initialization
  137. // Analog Comparator: Off
  138. // Analog Comparator Input Capture by Timer/Counter 1: Off
  139. ACSR=0x80;
  140. SFIOR=0x00;

  141. ……………………

  142. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:






评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶1 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表