Serial SPI interface. Using resistors when pinning and pulling resistors on the MCU input or output pins Which mcu pin will have such a signal
![Serial SPI interface. Using resistors when pinning and pulling resistors on the MCU input or output pins Which mcu pin will have such a signal](https://i2.wp.com/robotclass.ru/wp-content/uploads/2017/05/nodemcu-matrix-8x8-max7219-6_%D1%81%D1%85%D0%B5%D0%BC%D0%B0-1024x225.png)
Another interesting and even useful project on NodeMCU is a creeping line with WiFi control... Surely, all of us every day see such devices on the streets of the city. For example, the creeping line is widely used in transport to display the route number, the next stop, and intrusive advertising of all sorts of things. A typical creeping line is an LED matrix with a sweep control circuit and a microcontroller on board. The text crawling over this matrix can be stored locally, or updated dynamically via WiFi or GSM. Of course, having such a powerful platform as NodeMCU (or any other ESP8266), you can make a running line at home. It is rational to install it somewhere in a public place, for example, at a school. Through such an information board, it will be convenient to report internal school news, the temperature outside the window, or even the names of excellent students! In our laboratory, we installed a ticker in a window on the first floor. With the help of this IoT device, we congratulated all passers-by a Happy New Year and Merry Christmas 🙂
1. Connecting LED Matrix to NodeMCU
We will work with ready-made matrix modules controlled by the MAX7219 microcircuit. We have already written in detail about the work of such modules in one of the lessons for the Arduino platform -. In short, each such module has 10 pins. Five on one side and the same on the other. This is done so that the modules can be connected one after the other in a chain. At the entrance we have:- two pins for power supply: ground GND and + 5V;
- three contacts for the SPI bus: CS, DIN, CLK;
8x8 LED Array with MAX7219 | VCC | GND | DIN | CS | CLK |
NodeMCU | + 5V | GND | D7 | D8 | D5 |
![](https://i2.wp.com/robotclass.ru/wp-content/uploads/2017/05/nodemcu-matrix-8x8-max7219-6_%D1%81%D1%85%D0%B5%D0%BC%D0%B0-1024x225.png)
![](https://i0.wp.com/robotclass.ru/wp-content/uploads/2017/05/nodemcu-matrix-8x8-max7219-6_bb-1024x226.png)
![](https://i2.wp.com/robotclass.ru/wp-content/uploads/2017/05/IND-LED-8X8-4X_crop.jpg)
2. Program for controlling MAX7219 on NodeMCU
Let's try to run a creeping line on matrices, while we cannot remotely connect to NodeMCU. That is, the creeping line will rotate some kind of static text. Basically, this is code from. The only thing that has changed is the size of the chain. Here we are using not six matrices, but only four. #include3. Web server on NodeMCU to control the LED array
Now let's add a web server to the program, which will display one single HTML page with a field for entering the text of the scrolling line and with a button. #include" ""; // the function is called when the client presses the button void handleSubmit () (tape \u003d server.arg (" text "); server.send (200," text / html ", page);) void handleRoot () (if ( server.hasArg ("text")) (handleSubmit ();) else (server.send (200, "text / html", page);)) void setup (void) (delay (1000); WiFi.softAP (ssid ); server.on ("/", handleRoot); server.begin (); matrix.setIntensity (7);) void handleTicker () (for (int i \u003d 0; i< width * tape.length() + matrix.width() - 1 - spacer; i++) { matrix.fillScreen(LOW); int letter = i / width; int x = (matrix.width() - 1) - i % width; int y = (matrix.height() - 8) / 2; // центровка по вертикали while (x + width - spacer >\u003d 0 && letter\u003e \u003d 0) (if (letter< tape.length()) { matrix.drawChar(x, y, tape, HIGH, LOW, 1); server.handleClient(); } letter--; x -= width; } matrix.write(); delay(50); } } void loop(void){ server.handleClient(); handleTicker(); } Загружаем программу на Node MCU и подаем питание. По-умолчанию, бегущая строка будет крутить текст «RobotClass». Чтобы его изменить, необходимо подключиться к WiFi точке «ESP» и зайти через браузер по адресу: http://127.0.0.1/ В ответ появится страница с полем для ввода текста бегущей строки и кнопкой «Set text». Вводим в поле новый текст, жмем кнопку и смотрим на бегущую строку! Your browser does not support the video tag.
4. Creeping line in Russian
In its current form, our device does not support Russian. If you try to enter text in Russian, instead of letters, utf8 codes will appear on the matrix. To fix this, we need an additional utf2rus function. In addition, we will add the password for the WiFi hotspot to the program. #include" ""; String utf8rus (String source) (int i, k; String target; unsigned char n; char m \u003d (" 0 "," \\ 0 "); k \u003d source.length (); i \u003d 0; while (i< k) { n = source[i]; i++; if (n >\u003d 0xC0) (switch (n) (case 0xD0: (n \u003d source [i]; i ++; if (n \u003d\u003d 0x81) (n \u003d 0xA8; break;) if (n\u003e \u003d 0x90 && n<= 0xBF) n = n + 0x2F; break; } case 0xD1: { n = source[i]; i++; if (n == 0x91) { n = 0xB7; break; } if (n >\u003d 0x80 && n<= 0x8F) n = n + 0x6F; break; } } } // switch m = n; target = target + String(m); } return target; } void handleSubmit(){ tape = utf8rus(server.arg("text")); server.send(200, "text/html", page); } void handleRoot() { if (server.hasArg("text")) { handleSubmit(); } else { server..softAP(ssid, pwd); server.on("/", handleRoot); server.begin(); matrix.setIntensity(7); } void handleTicker(){ for (int i = 0 ; i < width * tape.length() + matrix.width() - 1 - spacer; i++) { matrix.fillScreen(LOW); int letter = i / width; int x = (matrix.width() - 1) - i % width; int y = (matrix.height() - 8) / 2; while (x + width - spacer >\u003d 0 && letter\u003e \u003d 0) (if (letter< tape.length()) { matrix.drawChar(x, y, tape, HIGH, LOW, 1); server.handleClient(); } letter--; x -= width; } matrix.write(); delay(50); } } void loop(void){ server.handleClient(); handleTicker(); } Готово! Теперь устройство готово к непрерывной эксплуатации. Осталось сделать крепление для матрицы и поставить бегущую строку на видное место. Чертежи подходящего крепления можно найти тут:
The Power-On Reset (POR) circuitry ensures that the microcontroller starts up only when Vcc reaches a safe level. As shown in Fig. 24, a built-in timer clocked by a built-in watchdog generator keeps the MCU from starting up for some time after reaching the power-on limit Vpot, independent of the slew rate Vcc (see Fig. 26).
Table 6 shows the settings of the SUT1 and SUT0 bits used to set the length of the start-up delay period. The user is given the option to select the start time delay. Setting SUT 1/0 \u003d 00, which starts the MCU after 5 clock cycles, is used when using an external clock applied to the XTAL1 pin. This setting provides fast startup from power down or power save modes, provided a clock signal is present in these modes. See the Programming section for details.
If the built-in start-up delay is sufficient, then RESET can be connected to Vcc directly or through an external pull-up resistor. By keeping the pin low while energizing, the power-on reset period can be extended. An example of such clocking is shown on. Figure: 27.
Figure: 25. Initial launch of the MCU. RESET pin connected to Vcc, Vcc ramp up
Figure: 26. Initial launch of MCU. RESET pin connected to Vcc, Vcc ramp up slowly
According to previous notes, and after reading the specification, some probably had a question - what is this second mysterious MCU processor operating at 100 MHz? Why is it needed? How to use it?
Meanwhile, the role of the MCU in some cases is extremely important. Those who have tried using Edison to work with various sensors may have already noticed that Intel Edison does not provide real-time response to their readings when working from Linux. And that's where the MCU comes in. It's time to tell a little about this embedded microcontroller, its architecture, applications and consider a practical example.
Intel Edison software version 2.1 adds the ability to use an embedded microcontroller.
Consider a system on a chip used in the Intel Edison Compute Module:
The system-on-a-chip used in the Intel Edison Compute Module includes two processors:
- Dual-core Intel Atom processor running at 500 MHz. Designated as Host CPU.
- Microcontroller with Minute IA architecture, operating at 100 MHz. Designated as MCU.
The microcontroller application runs on top of the Viper core and controls the peripherals connected to the MCU independently of the Intel Atom processor. For example, it can control GPIO ports, communicate with sensors via I2C or UART protocol, and communicate with an Intel Atom processor.
Why do you need a microcontroller in Intel Edison?
I would highlight two areas where an embedded microcontroller can be applied:- Work with I / O ports and interfaces with real-time response.
- Energy efficiency.
It is possible to improve energy efficiency using a microcontroller in those applications where the main processor can be in a sleep state, and the microcontroller can wait for a certain event (for example, exceeding threshold values \u200b\u200bfrom a sensor).
The microcontroller wakes up the main processor if necessary. An example implementation is provided in Using the MCU SDK and API: Code examples.
As an example of working with the Intel Edison microcontroller, consider connecting the HC-SR04 ultrasonic distance sensor. The measured distance will be displayed on the Grove LCD RGB Backlight character screen.
![](https://i1.wp.com/habrastorage.org/files/f4c/05c/681/f4c05c68170140cab2b81385b35b0f20.jpg)
The sensor has 4 outputs:
- Vcc - 5V.
- Trig - Trigger signal to the sensor. The microcontroller applies a 10 microsecond pulse to the sensor. The sensor initiates the measuring process.
- Echo - Echo signal from sensor to microcontroller. The pulse duration is proportional to the measured distance.
- Gnd - Earth.
![](https://i2.wp.com/habrastorage.org/files/308/3c8/08d/3083c808d8f44f3a957ab84b46fef7a6.png)
- 1 channel - Trig
- 2 channel - Echo
The pulse width is proportional to the measured distance.
The measured distance is calculated using the formula (taken from the sensor specification):
distance (cm) \u003d Echo pulse duration (microseconds) / 58
According to the specification, the sensor can measure distances from 2 to 400 cm.
It will be problematic to measure the pulse duration with a predicted error without real-time.
The measurement process can, for example, be supplanted by the scheduler and the measurement result will be incorrect.
We connect HC-SR04 to Intel Edison microcontroller
![](https://i2.wp.com/habrastorage.org/files/a3a/75f/5cf/a3a75f5cf2f14895a183d8fbb7670e1f.jpg)
Components used:
- Edison Compute Module
- Edison Arduino Board
- Grove basic shield
- Grove LCD RGB Backlight Character Screen
- Ultrasonic distance sensor HC-SR04
- Bread board
The HC-SR04 Ultrasonic Distance Sensor connects to the Grove Basic Shield as follows:
- Vcc to + 5V.
- Trig to pin # 3.
- Echo to pin # 4.
- Gnd to Gnd.
Intel Edison firmware update
Microcontroller support is available in the Intel Edison® Board Firmware Software Release since version 2.1. If your firmware is older, you need to update it.You can find out the current firmware version with the command:
# configure_edison --version
This example was created on firmware version 146.
The firmware update process is detailed in the Flashing Intel Edison article. Personally, I usually use the method described in the section Alternate flashing method.
Read the instructions carefully before flashing.
Connecting Intel Edison via Ethernet-over-USB
To work with Edison from the MCU SDK, you need to create a network connection.To do this, for example, you need to connect a USB cable to the middle micro-USB port (the switch must be set towards the micro-USB ports)
On Linux, the network is configured with the command:
# ifconfig usb0 192.168.2.2
Intel Edison IP: 192.168.2.15
The connection process is described in more detail in the article Connecting to your Intel® Edison board using Ethernet over USB.
MCU SDK
A cross-platform development environment MCU SDK based on Eclipse has been released to create applications that will run on an embedded microcontroller. The installation process is detailed in the Installing the MCU SDK article.The MCU SDK allows you to create, compile, download and debug microcontroller applications.
Interfacing with MCU
Several interfaces are available to interact with the microcontroller from Linux:/ dev / ttymcu0 - Channel for data exchange. You can work from Linux using standard file operations. From the program on the microcontroller, the exchange is performed using the host_send and host_receive functions.
/ dev / ttymcu1 - Channel through which the microcontroller sends debug messages using the debug_print function.
/ sys / devices / platform / intel_mcu / log_level - Allows you to set the level of debug messages (fatal, error, warning, info, debug).
Linux program
A small Python script that will receive data from an embedded microcontroller and display it on a character display. To work with the character display, we will use the Jhd1313m1 module from the UPM library.Show_distance.py script:
import time import pyupm_i2clcd RET_ERROR \u003d -1 if __name__ \u003d\u003d "__main__": lcd \u003d pyupm_i2clcd.Jhd1313m1 (6, 0x3E, 0x62) with open ("/ dev / ttymcu0", "w + t") as f: while True: f.write ("get_distance \\ n") # Send command to MCU f.flush () line \u003d f.readline () # Read response from MCU, -1 \u003d ERROR value \u003d int (line.strip ("\\ n \\ r \\ t ")) lcd.clear () if value \u003d\u003d RET_ERROR: lcd.setColor (255, 0, 0) # RED lcd.write (" ERROR ") else: lcd.setColor (0, 255, 0) # GREEN lcd.write ("% d cm"% (value,)) time.sleep (1)
Microcontroller program
The program on the microcontroller should, upon receiving the get_distance command from the host, measure the distance and send the result to the host (distance in centimeters, or -1 in case of an error).Configuring ports on the Edison Arduino Board:
./init_DIG.sh -o 3 -d output ./init_DIG.sh -o 4 -d input
Let me remind you that the microcontroller works with GPIO ports on the Edison Compute Module, which differ from the numbering on the Edison Arduino Board. The correspondence table is given, for example, at the end of the article Blinking an LED using the MCU.
Microcontroller program in MCU SDK:
#include "mcu_api.h" #include "mcu_errno.h" // Arduino Extension PIN \u003d 3 #define TRIG 12 // Arduino Extension PIN \u003d 4 #define ECHO 129 // From HC-SR04 datasheet #define MIN_DISTANCE 2 #define MAX_DISTANCE 400 #define MAX_WAIT 10000 #define RET_ERROR -1 int get_distance () (// Send Trig signal to HC-SR04 gpio_write (TRIG, 1); mcu_delay (10); gpio_write (TRIG, 0); // Read Echo signal from HC -SR04 int i; i \u003d 0; while ((gpio_read (ECHO) \u003d\u003d 0) && (i< MAX_WAIT)) { mcu_delay(1); i++; } unsigned long t0 = time_us(); if (gpio_read(ECHO) == 0 || i == MAX_WAIT) { return RET_ERROR; } i = 0; while ((gpio_read(ECHO) == 1) && (i < MAX_WAIT)) { mcu_delay(1); i++; } unsigned long t1 = time_us(); if (gpio_read(ECHO) == 1 || i == MAX_WAIT) { return RET_ERROR; } unsigned long distance = (t1 - t0) / 58; if (MIN_DISTANCE < distance && distance < MAX_DISTANCE) { return distance; } else { return RET_ERROR; } } #define MAX_BUF 255 unsigned char buf; void mcu_main() { // Setup Trig as OUTPUT gpio_setup(TRIG, 1); // Initially set Trig to LOW gpio_write(TRIG, 0); // Setup Echo as INPUT gpio_setup(ECHO, 0); while (1) { unsigned int len; len = host_receive(buf, MAX_BUF); if ((len >\u003d 12) && (strncmp (buf, "get_distance", 12) \u003d\u003d 0)) (unsigned int distance; distance \u003d get_distance (); len \u003d mcu_snprintf (buf, MAX_BUF, "% d \\ n", distance); host_send (buf, len);)))