[Atmel] 用AtmelStudio6.2跑mega16例程(9):串口通讯

[复制链接]
3855|2
 楼主| ddllxxrr 发表于 2014-9-8 11:07 | 显示全部楼层 |阅读模式
本帖最后由 ddllxxrr 于 2014-9-8 11:15 编辑

两个单片机通过串口进行通讯。第一个单片机把拨码开关的状态发到第二个,第二个负责显示到LED小灯上。本仿真的截图:



Atmel Studio6的发程序:
  1. /*
  2. * GccApplication15.c
  3. *
  4. * Created: 2014-9-8 10:50:02
  5. *  Author: Administrator
  6. */


  7. #include <avr/io.h>

  8. #define uchar unsigned char
  9. #define uint unsigned int
  10. #define RXC  7
  11. #define UDRE 5
  12. uchar key;
  13. void uart0_send(uchar i)
  14. {
  15.         while(!(UCSRA&(1<<UDRE)));
  16.         UDR=i;
  17. }
  18. int main(void)
  19. {
  20.         DDRB=0x00;
  21.         PORTB=0xFF;
  22.         UCSRA=0x00;
  23.         UBRRH=0;
  24.         UBRRL=25;            //系统时钟8MHz,波特率为9600bps
  25.         UCSRB=0x18;
  26.         UCSRC=0x86;
  27.         while(1)
  28.         {
  29.                 key=PINB;
  30.                 uart0_send(key);
  31.         }
  32. }

Atmel Studio6的收程序:
  1. /*
  2. * GccApplication16.c
  3. *
  4. * Created: 2014-9-8 10:51:22
  5. *  Author: Administrator
  6. */


  7. #include <avr/io.h>
  8. #include <avr/interrupt.h>
  9. #define  uchar unsigned char
  10. #define  uint unsigned  int

  11. #define  UDRE         5
  12. #define  FE           4
  13. #define  PE           2
  14. #define  DOR          3
  15. uchar LEDdisp;
  16. //interrupt [USART_RXC] void usart_rx_isr(void)//USART串行接收中断
  17. ISR(USART_RXC_vect)
  18. {
  19.         uchar status,data;
  20.         status=UCSRA;
  21.         data=UDR;
  22.         if((status&((1<<FE)|(1<<PE)|(1<<DOR)))==0)
  23.         {
  24.                 LEDdisp=data;
  25.         }
  26. }

  27. int main(void)
  28. {
  29.         DDRB=0xFF;
  30.         PORTB=0xFF;
  31.         UCSRA=0x00;
  32.         UBRRH=0;
  33.         UBRRL=25;            //系统时钟8MHz,波特率为9600bps
  34.         UCSRB=0x90;
  35.         UCSRC=0x86;
  36.         //#asm("sei")
  37.         sei();
  38.         while(1)
  39.         {
  40.                 PORTB=LEDdisp;
  41.         }
  42. }


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
daliaoloveu 发表于 2016-12-5 18:05 | 显示全部楼层
程序不行 收不到数据
 楼主| ddllxxrr 发表于 2016-12-6 08:01 | 显示全部楼层
daliaoloveu 发表于 2016-12-5 18:05
程序不行 收不到数据

你用的什么开发板,我用的PROTEUS都已经跑通以截图为证
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2404

主题

7002

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部