This post was last edited by jinglixixi on 2024-1-23 20:37
The KT148A voice chip is a functional device controlled by an MCU and works in a single-line communication mode, commonly known as a one-line serial port, but it is not a communication like a UART. Its communication protocol is non-standardized and needs to be generated according to its standard.
The functions of each pin of the chip are shown in Table 1:
The functional module made of this chip is shown in Figure 1, and its circuit schematic is shown in Figure 2. When in use, an 8-ohm speaker needs to be connected via the SPK interface.
Figure 1 Functional modules
Figure 2 Schematic diagram
Before playing the voice, you need to use the USB to TTL communication module to download the voice clip, and then you can use the TRIG key to play the test. Each time you press it, it will play a segment and automatically point to the next segment.
When downloading voice clips, you need to connect the TX pin of the USB to TTL communication module to the PB1 of the voice module, and press the DOWN download button to enter the voice download mode.
The software for downloading voice clips is shown in Figure 3. It should be noted that the file format of the voice clips is *.f1a, and the ordinary format MP3 files need to be converted through the software.
Figure 3 Software interface
With the previous preparations, you can program the CH32X035 for single-line communication. Through the control of the program, you can arbitrarily specify the voice segment to be played. The hardware structure of the entire circuit is shown in Figure 4. The test is to trigger the playback of the voice segment through the buttons on the development board. In actual applications, the playback of the corresponding voice segment is automatically triggered according to the progress of the work to achieve the effect of operation prompts.
Figure 4 Hardware composition
When PB8 is connected to the voice module PB1, the corresponding software design is as follows.
The function to configure PB8 as the data transmission pin and PB7 as the key detection pin is:
void BSP_TX_Init(void)
{
GPIO_InitTypeDefGPIO_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
The statements to achieve high and low level output are defined as:
#define IO1_HIGH() GPIO_WriteBit(GPIOB, GPIO_Pin_8, Bit_SET);
#define IO1_LOW() GPIO_WriteBit(GPIOB, GPIO_Pin_8, Bit_RESET);
The function to play the specified voice segment is:
void oneline_send_one_data(u8 dat)
{
u8 i = 0;
IO1_LOW();
HAL_DelayUs(5000);
for(i=0;i<8;i++)
{
if(dat& 0x01)
{
IO1_HIGH();
HAL_DelayUs(500);
IO1_LOW();
HAL_DelayUs(170);
}
else
{
IO1_HIGH();
HAL_DelayUs(170);
IO1_LOW();
HAL_DelayUs(500);
}
dat = dat>>1;
}
IO1_HIGH();
}
The main test program for voice playback is:
int main(void)
{
u8 i=0;
int n=1,M=6;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
SystemCoreClockUpdate();
Delay_Init();
BSP_LED_Init();
BSP_LED_On();
GPIO_Toggle_INIT();
BSP_TX_Init();
while(1)
{
if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7)==1)
{
GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET);
oneline_send_one_data(n);
if(n<M) n=n+1;
else
n=1;
}
else
{
GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_RESET);
}
}
}
After compiling and downloading the program, it is proved that the program is correct and valid.
Demo video:
语音播报