[Atmel] 用AtmelStudio6.2跑mega16例程(52)串口通信

[复制链接]
1544|8
 楼主| ddllxxrr 发表于 2014-11-12 21:34 | 显示全部楼层 |阅读模式
本例得安装VSPD,虚拟串口。当按下k1时会向PC串口发送数据。
以下是Studio6.2编译通过的截图:



程序清单:

  1. /*
  2. * GccApplication15.c
  3. *
  4. * Created: 2014-11-12 20:55:37
  5. *  Author: Administrator
  6. */

  7. #define F_CPU 4000000UL
  8. #include <avr/io.h>
  9. #include<avr/delay.h>
  10. #include <avr/interrupt.h>
  11. #include <stdint.h>

  12. struct  
  13. {
  14.         uint8_t Buf_Aray[100];
  15.         uint8_t Buf_Len;
  16. }Receive_Buffer;

  17. uint8_t Clear_Buffer_Flag = 0;

  18. const uint8_t SEG_CODE[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00};
  19.        
  20. char *s = "this is AVR computer!\n",*P;

  21. void Init_USART()
  22. {
  23.    UCSRB = _BV(RXEN)|_BV(TXEN)|_BV(RXCIE);
  24.    UCSRC = _BV(URSEL)|_BV(UCSZ1)|_BV(UCSZ0);
  25.    UBRRL = (F_CPU/9600/16-1)% 256;
  26.    UBRRH = (F_CPU/9600/16-1)/256;
  27. }

  28. void PutChar(char c)
  29. {
  30.        
  31.         if(c=='\n')PutChar('\r');
  32.         UDR = c;
  33.         while(!(UCSRA & _BV(UDRE)));
  34. }

  35. void Show_Received_Digits()
  36. {
  37.     uint8_t i;
  38.         for(i=0;i<Receive_Buffer.Buf_Len;i++)
  39.         {
  40.                 PORTC = SEG_CODE[Receive_Buffer.Buf_Aray[i]];
  41.                 _delay_ms(400);
  42.         }       
  43. }


  44. int main(void)
  45. {
  46.     Receive_Buffer.Buf_Len=0;
  47.         DDRB = 0x00;PORTB = 0xFF;
  48.         DDRC = 0xFF;PORTC = 0x00;
  49.         DDRD = 0x02;PORTD = 0xFF;
  50.         MCUCR = 0x08;
  51.         GICR = _BV(INT1);
  52.         Init_USART();
  53.         sei();
  54.         while(1) Show_Received_Digits();
  55. }

  56. ISR(USART_RXC_vect)
  57. {
  58.         uint8_t c= UDR;
  59.         if(c == '\r'||c=='\n')Clear_Buffer_Flag = 1;
  60.         if(c >= '0'&& c<='9')
  61.         {
  62.                
  63.                 if(Clear_Buffer_Flag==1)
  64.                 {
  65.                        
  66.                         Receive_Buffer.Buf_Len = 0;
  67.                         Clear_Buffer_Flag = 0;
  68.                 }
  69.                 Receive_Buffer.Buf_Aray[Receive_Buffer.Buf_Len]= c-'0';
  70.                 if(Receive_Buffer.Buf_Len<100)Receive_Buffer.Buf_Len++;
  71.         }
  72. }

  73. ISR(INT1_vect)
  74. {
  75.         uint8_t i=0;
  76.         while(s[i]!='\0')PutChar(s[i++]);
  77. }




本帖子中包含更多资源

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

×
moyansen 发表于 2014-12-9 23:56 | 显示全部楼层
您好,我在学习ATMega16;麻烦你帮忙看一下我的这个串口通信程序出错在哪里,我只能在串口调试助手接收到80 98 9E E0 9E FE 9E 98 FE 9E 9E FE,我本想发送hello world的。编译器是Atmel Studio 6.2
下面是代码
  1. #define F_CPU 4000000UL
  2. #include <avr/io.h>
  3. #include <avr/interrupt.h>
  4. #include <util/delay.h>
  5. #define uchar unsigned char
  6. #define uint unsigned int

  7. //初始化IO口
  8. void IO_init()
  9. {
  10.         DDRD=0x02;                //TxD(PD1)为输出
  11.         PORTD=0xFF;
  12. }
  13. //初始化串口
  14. void USART_init()
  15. {
  16.         UCSRA=0x00;
  17.         UCSRC|=_BV(URSEL)|_BV(UCSZ1)|_BV(UCSZ0);        //8位数据位
  18.         UCSRB|=_BV(TXEN);                                                        //使能发送
  19.         UBRRL=(F_CPU/9600/16-1)%256;
  20.         UBRRH=(F_CPU/9600/16-1)/256;
  21. }
  22. //发送一个字符
  23. void TxD_Byte(uchar c)
  24. {
  25.         while(!(UCSRA&_BV(UDRE)));
  26.         UDR=c;
  27. }
  28. //发送一串字符
  29. void TxD_String(uchar *p,uchar len)
  30. {
  31.         uchar i;
  32.         for(i=0;i<len;i++)  //控制字符个数
  33.         {
  34.                 TxD_Byte(*p);
  35.                 p++;
  36.         }
  37. }

  38. int main()
  39. {
  40.         IO_init();
  41.         USART_init();
  42.         while(1)
  43.         {
  44.                 TxD_String("Hello World",11);
  45.                 _delay_ms(500);
  46.         }
  47. }
 楼主| ddllxxrr 发表于 2014-12-11 21:28 | 显示全部楼层
moyansen 发表于 2014-12-9 23:56
您好,我在学习ATMega16;麻烦你帮忙看一下我的这个串口通信程序出错在哪里,我只能在串口调试助手接收到80 ...

是不是波特率错了,或外边的晶振错了,反正我看你这个就是波特率不对
moyansen 发表于 2014-12-13 00:22 | 显示全部楼层
ddllxxrr 发表于 2014-12-11 21:28
是不是波特率错了,或外边的晶振错了,反正我看你这个就是波特率不对

电路图我是使用 单片机C语言程序设计实训100例-基于AVR+protues仿真  这本书的,程序也是在他上面修改的,但是不知道为什么就是不能正常发送数据
 楼主| ddllxxrr 发表于 2014-12-13 11:15 | 显示全部楼层
moyansen 发表于 2014-12-13 00:22
电路图我是使用 单片机C语言程序设计实训100例-基于AVR+protues仿真  这本书的,程序也是在他上面修改的 ...

楼主是用Proteus环境仿真还是实际电路跑呢????
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

2404

主题

7001

帖子

68

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