[Atmel] 用AtmelStudio6.2跑mega16例程(65)驱动串口屏

[复制链接]
907|0
 楼主| ddllxxrr 发表于 2014-12-5 21:23 | 显示全部楼层 |阅读模式
本例液晶屏基于HD44780近制芯片,它连接单片机串口,显示单片机串口所发送的字符信息。
程序执行时串行液晶上显示:Serial LCD DEMO 当光标在第二行闪烁时,虚拟终端中输入的字符显示
在LCD上,按下退格键进光标左移,按回车键时清屏


Proteus截图:

Atmel Studio6.2运行时截图:

程序清单:


  1. /*
  2. * GccApplication28.c
  3. *
  4. * Created: 2014-12-5 20:39:58
  5. *  Author: Administrator
  6. */
  7. #define  F_CPU 4000000UL
  8. #include <avr/io.h>
  9. #include <avr/interrupt.h>
  10. #include <util/delay.h>
  11. #include <stdint.h>

  12. void Init_USART()
  13. {
  14.         UCSRB = _BV(RXEN)| _BV(TXEN)|_BV(RXCIE);//允许接收和发送,接收中断使能
  15.         UCSRC = _BV(URSEL)| _BV(UCSZ1)| _BV(UCSZ0);
  16.         UBRRL = (F_CPU/9600/16 - 1) % 256;
  17.         UBRRH = (F_CPU/9600/16 - 1) /256;
  18. }

  19. void PutChar(char c)
  20. {
  21.         if( c == '\n') PutChar('\r');
  22.         UDR = c;
  23.         while(!(UCSRA & _BV(UDRE)));
  24. }

  25. void PutStr(char *s)
  26. {
  27.         uint8_t i = 0;
  28.         while(s[i] !='\0')
  29.         {
  30.                 PutChar(s[i++]);
  31.                 _delay_ms(5);
  32.         }
  33. }

  34. void Write_LCD_COMMAND(uint8_t comm)
  35. {
  36.         PutChar(0xFE);
  37.         PutChar(comm);
  38.         
  39. }

  40. int main(void)
  41. {
  42.    Init_USART();
  43.    sei();
  44.    _delay_ms(300);
  45.    PutStr(" Serial LCD DEMO  ");
  46.    Write_LCD_COMMAND(0xC0);
  47.    Write_LCD_COMMAND(0x0d);
  48.    while(1);
  49. }

  50. ISR(USART_RXC_vect)
  51. {
  52.         uint8_t c = UDR;
  53.         if(c == 0x0D)
  54.         {
  55.                 Write_LCD_COMMAND(0x01);
  56.                 return;
  57.         }
  58.         if(c == 0x08)
  59.         {
  60.                 Write_LCD_COMMAND(0x10);
  61.                 return;
  62.         }
  63.         PutChar(c);
  64. }

本帖子中包含更多资源

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

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

本版积分规则

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

2404

主题

7001

帖子

68

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