Use PC’s RS232 port to control LED lights
Source: InternetPublisher:三岁就很酷 Updated: 2019/02/25
Through this experiment, you can learn the basic hardware interface circuit and software programming method for mutual communication between the PC serial port and the AVR microcontroller serial port, and achieve a simple application of using a PC to control LEDs.
Everyone is familiar with the PC serial port, right? We can use a serial port cable to directly connect the serial ports of two PCs to achieve serial communication between the two PCs. However, the communication between the PC and the microcontroller cannot be directly connected with a cable. The reason is that the level standard of the PC's RS232 serial port is inconsistent with the TTL level of the microcontroller. Therefore, the serial communication between the microcontroller and the PC must have an RS232/ TTL level conversion circuit. Usually this circuit is designed with a dedicated RS232 interface level conversion integrated circuit, such as MAX232, HIN232, etc. This experiment uses the NIH232CP chip to complete the serial interface circuit. NIH232 and MAX232 can be directly interchanged. Of course, we can also use discrete components to build an RS232 level conversion circuit for our experimental use. The figure below shows a common circuit. As long as the device is intact and the circuit is welded, it can work normally. After actual use, the effect is good.
Writing the AVR microcontroller serial port program The serial port of the Atmega8 microcontroller supports a variety of data frame structures and has three completely independent interrupts (reception completed, transmission completed, sending data register empty) and other features. It also supports multi-machine communication mode. This experiment uses Atmega8 as an example to introduce the programming of the AVR microcontroller serial port. The microcontroller serial port must be initialized before use. The initialization content is as follows: 1. Enable settings of the serial port receiver and transmitter: First, we need to enable the M8 serial port receiver or transmitter as needed, so that the I/O ports (PD1:TXD, PD2:RXD) can USART works. 2. Setting the serial port interrupt working mode: If the serial port needs to work in the interrupt mode, the USART interrupt setting must be set as needed. If the serial port works in the query mode, then the interrupt setting is not necessary. In this experiment, the serial port is set to respond to the reception completion interrupt, that is, after the receiver receives a complete data frame, a reception completion interrupt will be triggered. 3. Baud rate setting: that is, setting the data transmission rate of the serial port. For normal communication between the two serial ports, the baud rate must be the same. The baud rate set in this experiment is 9600. The system uses the built-in 8MHZ clock source. You can directly look up the table (page 129 of "Atmega8 Principle and Application Manual") to get UBRR=51. 4. Data frame structure setting: This experiment is set to 8 data bits + 1 stop bit. Therefore, the serial port initialization program for this experiment is as follows: void UART_init(void) { UCSRB = BIT(RXCIE)|BIT(RXEN) |BIT(TXEN); //Allow the serial port to send and receive, and respond to the reception completion interrupt UBRR = 51; //Set the serial port baud rate to 9600 UCSRC = BIT(URSEL)|BIT(UCSZ1)|BIT(UCSZ0); //The frame structure is 8-bit data + 1-bit stop bit} If you are not familiar with the settings of the AVR microcontroller serial port If you are familiar with it, you can also use the application wizard in the ICC compiler to directly generate serial port related code. The operation method is as follows: 1. Create a new file in ICC; 2. Click the Application Builder submenu in the Tools menu, the following interface will pop up, and then select the CPU as M8 and the crystal oscillator frequency as 8MHZ as needed.
3. Click the UART menu in the above interface, and the following interface will appear. Then press the options below to select enable reception and transmission, baud rate 9600, data bits 8 bits, and reception completion interrupt.
4. After clicking OK, the complete code is generated in the newly created file. The serial port initialization code is as follows: //UART0 initialize // desired baud rate: 9600 // actual: baud rate:9615 (0.2%) // char size: 8 bit // parity: Disabled void uart0_init(void) { UCSRB = 0x00; //disable while setting baud rate UCSRA = 0x00; UCSRC = BIT(URSEL) | 0x06; UBRRL = 0x33; //set baud rate lo UBRRH = 0x00; //set baud rate hi UCSRB = 0x98; } Let's see if UCSRC, UBRR, and UCSRB are the same in the code automatically generated using ICC and the code we wrote ourselves? Communication protocols and commands (2004-10-29) In order to ensure normal and reliable communication between the microcontroller and the PC and avoid malfunction of the system, we need
Latest Microcontroller Circuits
- Simple USB interface data acquisition system
- Interface circuit between 51 single chip microcomputer and MAX7219
- How to design an AirPlay speaker using Raspberry Pi Zero
- How to Design a Fan Cooling System Using a Microcontroller
- How to make a smart mirror using Raspberry Pi
- How to use ADXL335 to realize the design of gesture control robot
- Experiment on using 51 microcontroller to drive relay
- ID card attendance machine circuit
- Circuit diagram: 8515 extended RAM
- Communication conversion circuit diagram of RS232 and RS485