The AT32WB415 series has three built-in universal synchronous/asynchronous receivers and transmitters (USART1, USART2, and USART3).
And 1 universal asynchronous receiver and transmitter (UART5). USART3 is used to connect the chip to the wireless Bluetooth module, while UART5 only supports
TX.
In order to save pin resources, I originally planned to use the TX pin of UART5 to send data, but after testing, I found that the sending function could not be realized. In addition, PC12 used by TX5 also shares the same pin with PB6 of TX1, so it is meaningless to dwell on this.
To this end, USART1 is used directly for serial communication, with TX1 using PB6 and RX1 using PB7.
1. Serial communication
The function for configuring USART1 is:
void usart_configuration(void)
{
gpio_init_type gpio_init_struct;
crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_init_struct);
gpio_init_struct.gpio_pins = GPIO_PINS_6;
gpio_init(GPIOB, &gpio_init_struct);
gpio_init_struct.gpio_pins = GPIO_PINS_7;
gpio_init(GPIOB, &gpio_init_struct);
gpio_pin_remap_config(USART1_GMUX_0001, TRUE);
usart_init(USART1, 9600, USART_DATA_8BITS, USART_STOP_1_BIT);
usart_parity_selection_config(USART1, USART_PARITY_NONE);
usart_transmitter_enable(USART1, TRUE);
usart_receiver_enable(USART1, TRUE);
usart_enable(USART1, TRUE);
}
The procedure for sending data in byte mode is:
for(j=0;j<10;j++)
{
usart_data_transmit(USART2, cmd3[j]);
delay_ms(10);
}
The test result is shown in Figure 1, indicating that the function is normal.
Figure 1 Byte data transmission
2. MP3 module
The MP3 player module is a device that can be controlled by serial communication, and its pin distribution is shown in Figure 2.
When in use, it only occupies one serial port output pin PB6 in addition to the power pin.
In addition, since the communication baud rate of the module is 9600bps, it needs to work at this baud rate.
Figure 2 MP3 player module
Since the playback instruction has certain format requirements, it is implemented with the corresponding function, the content of which is as follows:
void playn(int index)
{
int i;
unsigned char checksum = 0;
cmd3[5] = (unsigned char)(index >> 8);
cmd3[6] = (unsigned char)(index);
for (i=2; i<8; i++)
{
checksum += cmd3[i];
}
cmd3[8] = (unsigned char)~checksum;
}
During the playback process, the song number is specified. To this end, the onboard user key is needed to select the content to be played. The onboard user key circuit is shown in Figure 3. Since only this key is available, the round-robin counting method is used to select.
Figure 3 Key circuit
The pin configuration function of the user key is:
void KEY_init(void)
{
gpio_init_type gpio_init_struct;
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_init_struct);
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
gpio_init_struct.gpio_pins = GPIO_PINS_0;
gpio_init_struct.gpio_pull = GPIO_PULL_UP;
gpio_init(GPIOA, &gpio_init_struct);
}
The statement to read the button status is:
gpio_input_data_bit_read(GPIOA, GPIO_PINS_0)
When the key is pressed, its detection value is 1; otherwise, its detection value is 0.
3. TFT screen menu display
In order to facilitate the selection of music, a TFT screen is used to display the Chinese menu. The connection relationship between the display screen and the development board is as follows:
BLK----PA5
SCL----PC0
SDA---PC1
DC-----PA1
RES---PB14
CS -----PA4
Note: The pins are allocated in this way to avoid occupying the corresponding functional pins.
Figure 4 Chinese interface
The main program to realize the playback control of the menu is:
int main(void)
{
int i,j,h;
system_clock_config();
at32_board_init();
usart_configuration();
setVolume(18);
for(j=0;j<10;j++)
{
usart_data_transmit(USART1, cmd6[j]);
delay_ms(10);
}
KEY_init();
app_tft_init();
tft_Init();
LCD_Clear(RED);
LCD_Fill(0,0,239,50,WHITE);
show_tb(60,0,0);
delay_ms(1000);
BACK_COLOR=WHITE;
POINT_COLOR=RED;
LCD_ShowChar(80,16,'M',1);
LCD_ShowChar(90,16,'P',1);
LCD_ShowChar(100,16,'3',1);
MENU();
LCD_Fill(0,287,239,319,WHITE);
BACK_COLOR=WHITE;
POINT_COLOR=RED;
LCD_ShowString(60,294,"BY: jinglixixi");
h=75;
BACK_COLOR=RED;
POINT_COLOR=YELLOW;
while(1)
{
if(gpio_input_data_bit_read(GPIOA, GPIO_PINS_0)==1)
{
if(i<6)
{
i++;
LCD_Fill(30,h,50,h+20,RED);
h=h+30;
showhanzi16(30,h,37);
}
else
{
i=0;
h=75;
}
playn(i);
for(j=0;j<10;j++)
{
usart_data_transmit(USART1, cmd3[j]);
delay_ms(10);
}
at32_led_toggle(LED3);
delay_ms(500);
}
}
}
Figure 5 Overall composition