[Atmel] 用AtmelStudio6.2跑mega16例程(63)1602

[复制链接]
 楼主| ddllxxrr 发表于 2014-12-2 22:33 | 显示全部楼层 |阅读模式
驱动1602应是很简单的事情,1602资料如下:



Proteus 仿真截图:


Atmel Studio6.2 编译通过截图:



程序清单:
第一个:
  1. /*
  2. * GccApplication26.c
  3. *
  4. * Created: 2014-12-2 20:34:45
  5. *  Author: Administrator
  6. */


  7. #include <avr/io.h>
  8. #include <util/delay.h>
  9. #include <stdint.h>

  10. #define RS PB0
  11. #define RW PB1
  12. #define E PB2

  13. #define LCD_CRTL_PORT PORTB
  14. #define LCD_PORT PORTC
  15. #define LCD_PIN PINC
  16. #define LCD_DDR DDRC

  17. #define RS_1() LCD_CRTL_PORT |= _BV(RS)
  18. #define RS_0() LCD_CRTL_PORT &= ~_BV(RS)
  19. #define RW_1() LCD_CRTL_PORT |= _BV(RW)
  20. #define RW_0() LCD_CRTL_PORT &= ~_BV(RW)
  21. #define EN_1() LCD_CRTL_PORT |= _BV(E)
  22. #define EN_0() LCD_CRTL_PORT &= ~_BV(E)

  23. void LCD_BUSY_WAIT()
  24. {
  25.         RS_0(); RW_1();
  26.         LCD_DDR = 0x00;
  27.         EN_1(); _delay_us(10);
  28.         loop_until_bit_is_clear(LCD_PIN,7);
  29.         EN_0();
  30.         LCD_DDR = 0xFF;
  31. }

  32. void Write_LCD_Command(uint8_t cmd)
  33. {
  34.         LCD_BUSY_WAIT();
  35.         RS_0(); RW_0();
  36.         LCD_PORT = cmd;
  37.         EN_1(); EN_0();
  38. }

  39. void Write_LCD_Data(uint8_t dat)
  40. {
  41.         LCD_BUSY_WAIT();
  42.         RS_1(); RW_0();
  43.         LCD_PORT = dat;
  44.         EN_1(); EN_0();
  45. }

  46. void Initialize_LCD()
  47. {
  48.         Write_LCD_Command(0x38);_delay_ms(15);
  49.         Write_LCD_Command(0x01);_delay_ms(15);
  50.         Write_LCD_Command(0x06);_delay_ms(15);
  51.         Write_LCD_Command(0x0c);_delay_ms(15);
  52.         
  53. }

  54. void LCD_ShowString(uint8_t x,uint8_t y,char *str)
  55. {
  56.         uint8_t i = 0;
  57.         if(y == 0)Write_LCD_Command(0x80 | x); else
  58.         if(y == 1)Write_LCD_Command(0xC0 | x);
  59.         for(i = 0; i<16 && str[i]!='\0';i++)
  60.         Write_LCD_Data(str[i]);
  61. }



主程序:

  1. /*
  2. * GccApplication26.c
  3. *
  4. * Created: 2014-12-2 20:34:45
  5. *  Author: Administrator
  6. */


  7. #define F_CPU 1000000UL
  8. #include <avr/io.h>
  9. #include <string.h>
  10. #include <stdint.h>

  11. const char CHAR_Table[]="0123456789+-=";
  12. char Disp_String[17];

  13. #define  Key_Pressed() ((PIND & _BV(PD4))!=0x00)
  14. #define KEY_VALUE (PIND & 0x0f)
  15. extern void Initialize_LCD();
  16. extern void LCD_ShowString(uint8_t x,uint8_t y,char *str);
  17. int main(void)
  18. {
  19.     char c; uint8_t sLen;
  20.         DDRB = 0xFF;
  21.         DDRD = 0x00; PORTD = 0xFF;
  22.         Initialize_LCD();
  23.         LCD_ShowString(0,0,"----LCD DRMO ----");
  24.         while(1)
  25.     {
  26.        if(Key_Pressed())
  27.            {
  28.                 sLen = strlen(Disp_String);
  29.                         if(KEY_VALUE <= 0x00)
  30.                         {
  31.                              c = CHAR_Table[KEY_VALUE];
  32.                                  if(sLen <16)
  33.                                  {
  34.                                          Disp_String[sLen] = c;
  35.                                          Disp_String[sLen + 1] = '\0';
  36.                                  }
  37.                         }
  38.          
  39.            else
  40.            {
  41.                  switch(KEY_VALUE)
  42.                          {
  43.                               case 0x0E:
  44.                                        if(sLen > 0) Disp_String[sLen - 1] = '\0';
  45.                                            break;
  46.                               case 0x0F:
  47.                                        Disp_String[0]='\0';
  48.                                            break;
  49.                          }
  50.            }
  51.            
  52.            LCD_ShowString(0,1,"       ");
  53.            LCD_ShowString(0,1,Disp_String);
  54.            while(Key_Pressed());
  55.     }
  56.   }
  57. }


本帖子中包含更多资源

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

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

本版积分规则

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

2403

主题

6994

帖子

68

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