Пример #1
0
        /// <summary>
        /// Begin
        /// Initializes the Character LCD and MCP23017
        /// </summary>
        /// <param name="cols"></param>
        /// <param name="lines"></param>
        /// <param name="dotsize"></param>
        /// <returns></returns>
        public async Task BeginAsync(int cols, int lines, int dotsize = LCD_5x8DOTS)
        {
            await MCP.InitMCP23017Async();

            MCP.pinMode(RWPin, Mcp23017.Direction.OUTPUT);
            MCP.pinMode(RSPin, Mcp23017.Direction.OUTPUT);
            MCP.pinMode(EnablePin, Mcp23017.Direction.OUTPUT);

            foreach (var pin in DataPins)
            {
                MCP.pinMode(pin, Mcp23017.Direction.OUTPUT);
            }

            foreach (var pin in ButtonPins)
            {
                MCP.pinMode(pin, Mcp23017.Direction.INPUT);
                MCP.pullUp(pin, Mcp23017.Level.HIGH);
            }

            foreach (var pin in ColorPins)
            {
                MCP.pinMode(pin, Mcp23017.Direction.OUTPUT);
            }
            setBacklight(WHITE);

            if (lines > 1)
            {
                DisplayFunction |= LCD_2LINE;
            }
            NumLines    = lines;
            NumColumns  = cols;
            CurrentLine = 0;
            CurrentCol  = 0;

            // for some 1 line displays you can select a 10 pixel high font
            if ((dotsize != 0) && (lines == 1))
            {
                DisplayFunction |= LCD_5x10DOTS;
            }

            // Pull both RS and R/W low to begin commands
            MCP.digitalWrite(RSPin, Mcp23017.Level.LOW);
            MCP.digitalWrite(EnablePin, Mcp23017.Level.LOW);
            MCP.digitalWrite(RWPin, Mcp23017.Level.LOW);

            //put the LCD into 4 bit or 8 bit mode
            if (0 == (DisplayFunction & LCD_8BITMODE))
            {
                // this is according to the hitachi HD44780 datasheet
                // figure 24, pg 46

                // we start in 8bit mode, try to set 4 bit mode
                write4bits(0x03);
                usDelay(4500);

                // second try
                write4bits(0x03);
                usDelay(4500);

                // third go!
                write4bits(0x03);
                usDelay(150);

                // finally, set to 8-bit interface
                write4bits(0x02);
            }
            else
            {
                // this is according to the hitachi HD44780 datasheet
                // page 45 figure 23

                // Send function set command sequence
                command((byte)(LCD_FUNCTIONSET | DisplayFunction));
                usDelay(4500);

                // second try
                command((byte)(LCD_FUNCTIONSET | DisplayFunction));
                usDelay(150);

                // third go
                command((byte)(LCD_FUNCTIONSET | DisplayFunction));
            }

            // finally, set # lines, font size, etc.
            command((byte)(LCD_FUNCTIONSET | DisplayFunction));

            // turn the display on with no cursor or blinking default
            DisplayControl = (byte)(LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF);
            display();

            // clear it off
            clear();

            // Initialize to default text direction (for romance languages)
            DisplayMode = (byte)(LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT);
            // set the entry mode
            command((byte)(LCD_ENTRYMODESET | DisplayMode));
        }