驱动1602应是很简单的事情,1602资料如下:
Proteus 仿真截图:
Atmel Studio6.2 编译通过截图:
程序清单:
第一个:
- /*
- * GccApplication26.c
- *
- * Created: 2014-12-2 20:34:45
- * Author: Administrator
- */
- #include <avr/io.h>
- #include <util/delay.h>
- #include <stdint.h>
- #define RS PB0
- #define RW PB1
- #define E PB2
- #define LCD_CRTL_PORT PORTB
- #define LCD_PORT PORTC
- #define LCD_PIN PINC
- #define LCD_DDR DDRC
- #define RS_1() LCD_CRTL_PORT |= _BV(RS)
- #define RS_0() LCD_CRTL_PORT &= ~_BV(RS)
- #define RW_1() LCD_CRTL_PORT |= _BV(RW)
- #define RW_0() LCD_CRTL_PORT &= ~_BV(RW)
- #define EN_1() LCD_CRTL_PORT |= _BV(E)
- #define EN_0() LCD_CRTL_PORT &= ~_BV(E)
- void LCD_BUSY_WAIT()
- {
- RS_0(); RW_1();
- LCD_DDR = 0x00;
- EN_1(); _delay_us(10);
- loop_until_bit_is_clear(LCD_PIN,7);
- EN_0();
- LCD_DDR = 0xFF;
- }
- void Write_LCD_Command(uint8_t cmd)
- {
- LCD_BUSY_WAIT();
- RS_0(); RW_0();
- LCD_PORT = cmd;
- EN_1(); EN_0();
- }
- void Write_LCD_Data(uint8_t dat)
- {
- LCD_BUSY_WAIT();
- RS_1(); RW_0();
- LCD_PORT = dat;
- EN_1(); EN_0();
- }
- void Initialize_LCD()
- {
- Write_LCD_Command(0x38);_delay_ms(15);
- Write_LCD_Command(0x01);_delay_ms(15);
- Write_LCD_Command(0x06);_delay_ms(15);
- Write_LCD_Command(0x0c);_delay_ms(15);
-
- }
- void LCD_ShowString(uint8_t x,uint8_t y,char *str)
- {
- uint8_t i = 0;
- if(y == 0)Write_LCD_Command(0x80 | x); else
- if(y == 1)Write_LCD_Command(0xC0 | x);
- for(i = 0; i<16 && str[i]!='\0';i++)
- Write_LCD_Data(str[i]);
- }
主程序:
- /*
- * GccApplication26.c
- *
- * Created: 2014-12-2 20:34:45
- * Author: Administrator
- */
- #define F_CPU 1000000UL
- #include <avr/io.h>
- #include <string.h>
- #include <stdint.h>
- const char CHAR_Table[]="0123456789+-=";
- char Disp_String[17];
- #define Key_Pressed() ((PIND & _BV(PD4))!=0x00)
- #define KEY_VALUE (PIND & 0x0f)
- extern void Initialize_LCD();
- extern void LCD_ShowString(uint8_t x,uint8_t y,char *str);
- int main(void)
- {
- char c; uint8_t sLen;
- DDRB = 0xFF;
- DDRD = 0x00; PORTD = 0xFF;
- Initialize_LCD();
- LCD_ShowString(0,0,"----LCD DRMO ----");
- while(1)
- {
- if(Key_Pressed())
- {
- sLen = strlen(Disp_String);
- if(KEY_VALUE <= 0x00)
- {
- c = CHAR_Table[KEY_VALUE];
- if(sLen <16)
- {
- Disp_String[sLen] = c;
- Disp_String[sLen + 1] = '\0';
- }
- }
-
- else
- {
- switch(KEY_VALUE)
- {
- case 0x0E:
- if(sLen > 0) Disp_String[sLen - 1] = '\0';
- break;
- case 0x0F:
- Disp_String[0]='\0';
- break;
- }
- }
-
- LCD_ShowString(0,1," ");
- LCD_ShowString(0,1,Disp_String);
- while(Key_Pressed());
- }
- }
- }
|