Digital tube Display using STM32 serial interrupt

Source: Internet
Author: User
Tags 0xc0

In order to make this experiment more obvious, I will use the serial interrupt, in the interrupt service program to read the serial number input, and then display the value on the digital tube.

First, the digital tube configuration

Before carrying out this experiment, we need to use the digital tube for the display of the experimental results, first of all, we require the truth table of the digital tube, I use ordinary common anode digital tube, so the truth table is as follows:

0

1

2

3

4

5

6

7

0xc0

0xf9

0xa4

0xb0

0x99

0x92

0x82

0xF8

8

9

A

B

C

D

E

F

0x80

0x90

0x88

0x83

0xC6

0xa1

0x86

0x8E


    After the completion of the preparation, you can formally enter the experiment. Obviously, we use the digital tube and the serial port, so we need at least the digital tube configuration function and the serial port configuration function of the two configuration functions. For the digital tube configuration function, because I use the pz6806l type of development board, through the query schematic found that the various segments of the digital tube corresponding to the LED1~LED8, and then received GPIOC 0~7 pin, so the configuration of GPIOC can be, this simple, directly give the code as follows: void Segmentdisplayinit () {gpio_inittypedef gpio_initstructure; Rcc_apb2periphclockcmd (RCC_APB2PERIPH_GPIOC, ENABLE); Gpio_initstructure.gpio_pin = Gpio_pin_all; Gpio_initstructure.gpio_speed = Gpio_speed_10mhz; Gpio_initstructure.gpio_mode = gpio_mode_out_pp; Gpio_init (GPIOC, &gpio_initstructure); Second, serial port configuration The following is the focus of this experiment, that is, RS232 serial interrupt configuration Function Usartinit (), in the STM32 Chinese reference Manual, the description of USART is as follows: Universal Synchronous Asynchronous Transceiver (USART) Provides a flexible approach to full-duplex data exchange between external devices using the industry standard NRZ asynchronous serial data format. The Usart uses a fractional baud rate generator to provide a wide range of baud rate options. It supports synchronous one-way communication and half-duplex single-line communication, and also supports Lin (local interconnect network), Smart Card Protocol, and IrDA (infrared data Organization) SIR ENDEC specification, as well as modem (cts/rts) operations. It also allows multi-processor communication. Serial communication generally has three kinds of forms, namely query mode, interrupt mode and DMA mode. Query method (also known as polling method) is not efficient, in the need to query the CPU occupied very high, the embodiment of the program is to use the loop to constantly query the flag bit state, in the query CPU does not do other things, so the efficiency is relatively low. Interrupt mode, only in the interruption of sending interrupts, receiving interrupts, transmission complete interruption, etc. will enter the serial interrupt service program, so that the CPU does not have to wait there, what to do, greatly improve the efficiency, so this experiment uses the interrupt mode. and DMA mode, because in the microcomputer principle course did not carry out the relevant knowledge of learning, so do not know much, do not dare to comment. At the beginning of the serial port interruptInitial configuration, the first should open the serial clock and PA port clock (Afio clock can not open), the reason for opening the PA port clock is that in this experiment, I used the board on the schematic, RS232 serial port Rx and TX respectively with PA10 and PA9 connected. Then, according to the TX, RX configuration PA9 and PA10 These two gpio, it should be noted here is the best mode to set the float input and multiplexing push-pull output, at least according to the following table settings: The next step is to call the Usart_init () program to configure the serial port parameters, The entry parameter of the function is a struct pointer usart_inittypedef*, the query stm32f10x_usart.h is available, the structure is defined as follows: typedef struct {  uint32_t usart_baudrate; nbsp;//baud rate, general 9600 can   uint16_t usart_wordlength;//word length eight-bit or nine-bit   uint16_t usart_stopbits;  //Stop bit selection   uint16_t usart_parity;  //parity check   uint16_t Usart_mode;  //mode, send or receive   uint16_t usart_hardwareflowcontrol;//hardware control flow, generally set to none}usart_inittypedef; Then open the serial port, using Usart_cmd (usart1,enable); Then open the serial interrupt, here I open receive interrupt, function as follows: Usart_itconfig (Usart1,usart_it_rxne, ENABLE); Finally configure the serial port interrupt priority, this configuration in the previous timer interrupt configuration has been introduced, here no longer repeat. The general configuration functions of USART are given as follows: void Usartinit () {usart_inittypedef usart_initstructure; Gpio_inittypedef gpio_initstructure; Nvic_inittypedef nvic_initstructure; Clock Configuration rcc_apb2periphclockcmd (rcc_apb2periph_gpioa| rcc_apb2periph_usart1,enable); TX and Rx correspond to Gpio's initialGpio_initstructure.gpio_pin = Gpio_pin_9; Gpio_initstructure.gpio_speed = Gpio_speed_10mhz; Gpio_initstructure.gpio_mode = gpio_mode_af_pp; Gpio_init (Gpioa, &gpio_initstructure); Gpio_initstructure.gpio_pin = gpio_pin_10; Gpio_initstructure.gpio_speed = Gpio_speed_10mhz; Gpio_initstructure.gpio_mode = gpio_mode_in_floating; Gpio_init (Gpioa, &gpio_initstructure); Initialization of the serial port basic parameters usart_initstructure.usart_baudrate = 9600; Usart_initstructure.usart_wordlength = usart_wordlength_8b; Usart_initstructure.usart_stopbits = Usart_stopbits_1; usart_initstructure.usart_parity = Usart_parity_no; usart_initstructure.usart_hardwareflowcontrol=usart_hardwareflowcontrol_none  USART_InitStructure.USART_ Mode = Usart_mode_tx | USART_MODE_RX; Usart_init (USART1, &usart_initstructure); Usart_cmd (USART1, ENABLE); Usart_itconfig (USART1, Usart_it_rxne, ENABLE); Serial-Port Interrupt priority configuration Nvic_prioritygroupconfig (nvic_prioritygroup_1); Nvic_initstructure.nvic_irqchannel = USART1_IRQN; Nvic_initstructure.nvic_irqchannelpreemptionpriority = 0; Nvic_initstructure.nvic_irqchannelsubpriority = 2; Nvic_initstructure.nvic_irqchannelcmd = ENABLE; Nvic_init (&nvic_initstructure); Third, the serial port interrupt function in this function as long as the input data is detected, and then the corresponding digital tube truth table value is written to GPIOC. The program is given directly here: void Usart1_irqhandler (void) {static U8 k; int seg[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xF8, 0x80 , 0x90, 0x88, 0x83, 0xC6, 0xa1, 0x86, 0x8E}; Usart_clearflag (USART1, USART_FLAG_TC); if (Usart_getitstatus (USART1, usart_it_rxne)! = bit_reset) {k = Usart_receivedata (USART1); Gpio_write (GPIOC, seg[k]); }} The main function is simple and is not given here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.