AVR microcontroller tutorial --LCD1602

Display

Development Kit, there are two screens, is the large LCD (liquid crystal display), a small OLED (Organic Light Emitting Diode). Working with contrast, dapper expensive as you think, and less of this lecture topic --LCD1602-- function, it is also much simpler to use.

This screen display is character units. Each character is 8 pixels high, 5 pixels wide. 1602 The name, derived from the number of characters in the show, a total of 2 lines, 16 characters per line. 1602 sale of businesses provide a document: extraction code 8c1u .

hardware

A typical 1602 display has 16 pins (some module is a serial bus driven):

name Features connection
VSS Power Ground GND
VDD Positive supply VCC(5V)
VO Contrast adjustment The left side of the potentiometer, the GND its left end, a right end connected to VCC
RS Data / command selection PB0
R/W Read / write selection PB1
E Enable PB2
D0~D7 Bidirectional parallel data lines 74HC595 output through exout_writeoutput
A The backlight LED anode The emitter of NPN transistor having a collector connected to the VCC, a base connected to BAK
K The backlight LED cathode GND

Very complicated, right? Fortunately, the board has been developed to deal with this, we only need to focus on two sets of lines: RS, R/W, Ethese three control lines, through DDRBand PORTBto manipulate, and D0- D7that with the data line 8, through exout_writeto the output in bytes (in this the first phase of the last one, I finally succeeded in "minimizing wiring" principle carry out in the end in the first period). There are other contrast CONinterfaces, may not external, there are a backlight BAKinterfaces, 5V or 3.3V can be connected to turn on the backlight. Of course, if you are not mainstream, you can CONreceive the DAC, to BAKreceive the PWM.

Further, also we need to extend the highest bit of the output (marked Ext Out 7) on to a single chip pin. About why this strange connection, which is the design of mistakes (it may be a last resort, after all, the microcontroller 32 has filled the IO), see: Reflections on a low level triggered .

protocol

Between 1602 and the microcontroller via a parallel bus communication. AVR microcontroller does not support parallel bus on hardware, software needs to be implemented by the timing simulation.

The timing of the write operation as follows:

Perform a write operation, it is necessary to let the RSset level, depending on the type of write R/Woutput low, D0- D7the data output to be transmitted, then Ethe rising edge of the other data is read, and maintain R/Wthe D0~ D7level unchanged until Ethe after falling. Two Eat least 400us the time interval between the rising edges.

A total of 1602 eight instructions are a byte length. From high to low, each instruction consists of a number 0, a 1and composition effective instruction, such that no two instructions have the same binary representation. This encoding become prefix code, UTF-8 is also used a similar coding.

The number of leading 0 Features
0 Set the DDRAM (display memory) address
1 Set CGRAM (character generator memory) address
2 Setting the bus width, the number of lines of characters, character size
3 The scrolling object (cursor or screen), a scroll direction
4 The setting screen, the cursor blinking switch
5 Setting AC (address counter) whether a change in the direction of change
6 The AC cleared
7 Clear screen

After 1602 development board plug in only the first line with a black background. We put it to 2 lines output, which is the simplest visual effect of instruction.

#include <avr/io.h>
#include <ee1/lcd.h>

inline void short_delay()
{
    asm("nop");
    asm("nop");
    asm("nop");
    asm("nop");
}

int main()
{
    DDRB |= 0b111; // PB2:0 for LCD control: E, R/W, RS
    exout_init();  // Ext Out 7:0 for LCD instructions/data
#define LCD_CONTROL(x) (PORTB = (PORTB & ~0b111) | (x))
    LCD_CONTROL(0b000);
    short_delay();
    LCD_CONTROL(0b100);
    short_delay();
    exout_write(0b00111000); // 8-bit, 2 lines, 5x7 font
    short_delay();
    LCD_CONTROL(0b000);
}

In this code, asm("nop")is a compilation of statements nop, no operation is performed on behalf of the CPU cycle can be delayed for a short time and accurately. Add a short delay between the control signal and the data signal, so that timing 1602 to meet the requirements.

其他指令见数据手册,这份数据手册本身就是很好的教程。需要提醒的是,显示屏内的控制器执行指令需要一定时间,程序在发送下一指令之前,应通过RS为低电平的读取操作来获取其状态,并等待直到状态为空闲。

软件

开发板的库不仅包装了这些硬件操作,还添加了对字符串的支持,包括对\b\t\n\r等不可打印字符的处理。

下面我们用1602显示屏来完成一个小项目,在之前用按键控制蜂鸣器的基础上,加入LCD显示与背光动态效果。

#include <string.h>
#include <ee1/lcd.h>
#include <ee1/pwm.h>
#include <ee1/button.h>
#include <ee1/tone.h>
#include <ee1/delay.h>

int main()
{
    lcd_init(PIN_3);
    lcd_set_status(true, false, false);
    button_init(PIN_6, PIN_7);
    wave_mode(WAVE_0, WAVE_MODE_PWM);
    wave_mode(WAVE_1, WAVE_MODE_TONE);
    char display[6] = {[0] = '\r', [5] = '\0'};        // 1. 为什么要开6字节字符数组?
    char* notes = display + 1;                         // 2. display数组是如何使用的?
    uint8_t brightness;                                // 3. 这一行有什么问题?
    uint16_t frequency[] = {262, 330, 392, 523};
    uint16_t buzzer = 0, tone;
    while (1)
    {
        memset(notes, ' ', 4);                         // 4. memset是什么函数?
        tone = 0;
        for (uint8_t i = 0; i != 4; ++i)
            if (button_down(i))
            {
                notes[i] = 0xFF;                       // 5. 0xFF是什么字符?
                brightness = 255;
                tone = frequency[i];                   // 6. 如果有多个按键按下,蜂鸣器会响什么音?
            }
        lcd_print_string(display);
        pwm_set(WAVE_0, brightness * brightness >> 8); // 7. 为什么第二个参数不直接写brightness?
        if (tone != buzzer)                            // 8. 为什么必须引入buzzer变量并在这里作判断?
        {
            buzzer = tone;
            tone_set(WAVE_1, buzzer);
        }
        if (brightness)
            brightness -= 5;
        delay(10);
    }
}

在这AVR单片机教程第一期最后一讲中,我在代码中留了8个问题。如果你能全部答上来,说明你对C语言已经有一些了解,熟悉了单片机开发中的部分细节,并具备初步的工程素养了。

作业

思考题:

  1. 明明可以选择4位总线来节省IO,为什么开发板上还是8位连接?

  2. 实现读取1602空闲状态。

  3. 在1602上画一颗心。

  4. 修改例程,使1602以滚动的横线指示按下的按键与发出的音符(类似于你玩过或看过的音游)。

扩展阅读:

  1. How LCDs Work

  2. OLED和LCD什么区别?

Guess you like

Origin www.cnblogs.com/jerry-fuyi/p/12174393.html