Design name: Dynamic QR code display
Works photos:
Project Description:
QR codes can be seen everywhere in life. They are used in various payment, collection, advertising, promotion, etc., because mobile phones can scan codes, which greatly facilitates people's lives. With the help of mobile phone scanning, using the hardware resources of the gd32ee231 development board, a dynamic QR code display is designed. Users can customize and display the set information, which is convenient for everyone to scan the code at any time, and the information can be changed at any time. It is flexible and convenient, and is very suitable for advertising promotion, collection and payment and other scenarios.
Required peripherals: SPI interface LCD display
The hardware connection of the development board is as follows:
Function introduction of each part:
1. The PC is connected to the development board serial port through the serial port, and the development board is connected to the LCD display through SPI .
2. Burn the firmware to the MCU . After the PC inputs the command and QR code data through the serial shell , the MCU will generate a QR code from the data and display it on the LCD screen.
3. The MCU firmware actually converts the serial port data into the QR code data of 0 and 1 , and then sends it to the display screen. The display screen then displays 0 and 1 in white and black respectively according to the set pixels , which is the QR code we see.
4. Finally, scan the QR code with WeChat on your mobile phone to display the data entered on the PC .
Serial port source code:
void usart0_config(void)
{
/* enable USART GPIO clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* enable USART clock */
rcu_periph_clock_enable(RCU_USART0);
/* connect port to USARTx_Tx */
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9);
/* connect port to USARTx_Rx */
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10);
/* configure USART Tx as alternate function push-pull */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
/* configure USART Rx as alternate function push-pull */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
/* USART configure */
usart_deinit(USART0);
usart_baudrate_set(USART0, 115200);
usart_word_length_set(USART0, USART_WL_8BIT);
usart_stop_bit_set(USART0, USART_STB_1BIT);
usart_parity_config(USART0, USART_PM_NONE);
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
/* enable the TIMER5 interrupt */
nvic_irq_enable(USART0_IRQn, 0);
/* USART0 INT_RBNE enable */
usart_interrupt_enable(USART0, USART_INT_RBNE);
/* enable USART0 */
usart_enable(USART0);
}
LCD interface pin initialization source code:
void gpio_init(void)
{
/* enable the LED1 GPIO clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* configure LED1,2,3,4 GPIO port */
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_11|GPIO_PIN_12);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_11|GPIO_PIN_12);
/* reset LED1 GPIO pin */
//gpio_bit_reset(GPIOA,GPIO_PIN_7);
gpio_bit_set(GPIOA, GPIO_PIN_7); //led1
gpio_bit_set(GPIOA,GPIO_PIN_11); //led3
//gpio_bit_set(GPIOA,GPIO_PIN_12); //led4
//PA1,PA2,PA3,PA4,PA5,PA6
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6);
gpio_bit_set(GPIOA, GPIO_PIN_1);
gpio_bit_set(GPIOA, GPIO_PIN_2);
gpio_bit_set(GPIOA, GPIO_PIN_3);
gpio_bit_set(GPIOA, GPIO_PIN_4);
gpio_bit_set(GPIOA, GPIO_PIN_5);
gpio_bit_set(GPIOA, GPIO_PIN_6);
//spi0 gpio init
//SPI0_SCK-PB3,SPI0_MISO-PB4,SPI0_MOSI-PB5
rcu_periph_clock_enable(RCU_GPIOB);
gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_3|GPIO_PIN_5);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3|GPIO_PIN_5);
//spi1 gpio init
//SPI1_SCK-PB13,SPI1_MISO-PB14,SPI1_MOSI-PB15
gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15);
//PB0,PB1,CS-PB6,RS-PB7
gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_6|GPIO_PIN_7);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_6|GPIO_PIN_7);
gpio_bit_set(GPIOB, GPIO_PIN_0);
gpio_bit_set(GPIOB, GPIO_PIN_1);
gpio_bit_set(GPIOB, GPIO_PIN_6);
gpio_bit_set(GPIOB, GPIO_PIN_7);
//PB8,PB10,PB11,PB12
gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_8|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12);
gpio_bit_reset(GPIOB, GPIO_PIN_8);
gpio_bit_set(GPIOB, GPIO_PIN_10);
gpio_bit_set(GPIOB, GPIO_PIN_11);
gpio_bit_reset(GPIOB, GPIO_PIN_12);
//TP_CS-PA1,RST-PA2
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_1|GPIO_PIN_2);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2|GPIO_PIN_2);
gpio_bit_set(GPIOA, GPIO_PIN_1);
gpio_bit_set(GPIOA, GPIO_PIN_2);
}
SPI interface source code :
void lcd_port_init(void)
{
spi_parameter_struct spi_dev;
rcu_periph_clock_enable(RCU_SPI0);
rcu_periph_clock_enable(RCU_SPI1);
spi_i2s_deinit(SPI0);
spi_dev.device_mode = SPI_MASTER;
spi_dev.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
spi_dev.frame_size = SPI_FRAMESIZE_8BIT;
spi_dev.nss = SPI_NSS_SOFT;
spi_dev.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
spi_dev.prescale = SPI_PSC_4;
spi_dev.endian = SPI_ENDIAN_MSB;
spi_init(SPI0, &spi_dev);
spi_enable(SPI0);
}
LCD display source code:
void LCD_Fill(u16 sx, u16 sy, u16 ex, u16 ey, u16 color)
{
u16 i,j;
for(i=sy; i<=ey; i++){
for(j=sx; j<=ex; j++){
LCD_SetPoint(j, i, color);
}
}
}
Source code for generating and displaying the QR code:
void show_qrcode(u16 x, u16 y, u16 p, u8 *qrcode_data)
{
u8 i,j;
EncodeData((char *)qrcode_data);
for(i=0; i<m_nSymbleSize; i++)
{
for(j=0; j<m_nSymbleSize; j++)
{
if(m_byModuleData[j]==1){
LCD_Fill(x+p*i, y+p*j, x+p*(i+1)-1, y+p*(j+1)-1, Black);
}
}
}
}
Test the effect of generating and displaying a QR code:
|