This is a "Book of Answers" program based on an STC microcontroller. Pressing a button refreshes the screen text and plays the corresponding audio. The code is a version without music; to play music, you need to buy a mini MP3 module, connect it, and add this code.
/******************************************************************************- Functionality: Enables the chip to play the first and second tracks upon power-up. A basic program is provided for user testing. - Date: 2013-05-06 - Operating Environment: STC crystal oscillator: 11.0592MHz, baud rate: 9600 - Note: Debugged successfully on the Puzhong Technology 51 development board --- STC89C516RD+1. This test program requires a device to be online in the module or chip solution, such as a USB flash drive, TF card, or FLASH. ******************************************************************************/
#define COMM_BAUD_RATE 9600 // Serial port baud rate #define OSC_FREQ 11059200 // Running crystal oscillator: 11.05926MHZ
static unsigned char Send_buf[10] = {0}; /*void Delay_Ms(unsigned int z){unsigned int x=0 , y=0;for(x=110 ; x>0 ;x--)for(y=z; y>0;y-- );}*//**************************************************************************************- Function description: Serial port 1 initialization - Note: Set to 9600 baud rate ******************************************************************************/void Serial_init(void){//TMOD = 0x20; // Set T1 as baud rate generator//SCON = 0x50; // 0101,0000 8 data bits, No parity //PCON = 0x00; //PCON=0; //TH1=256-(OSC_FREQ/COMM_BAUD_RATE/32/12);//Set to 9600 baud rate //TL1=256-(OSC_FREQ/COMM_BAUD_RATE/32/12);//TR1 = 1; //Timer 1 enabled //REN = 1; //Serial port 1 receive enabled //ES = 1; //Serial port 1 interrupt enabled PCON &= 0x7F; //Baud rate not multiplied SCON = 0x50; //8-bit data, variable baud rate AUXR |= 0x40; //Timer clock 1T mode AUXR &= 0xFE; //Serial port 1 selects Timer 1 as baud rate generator TMOD &= 0x0F; //Set timer mode TMOD |= 0x20; //Set timer mode TL1 = 0xD9; // Set the initial value of the timer TH1 = 0xD9; // Set the reload value of the timer ET1 = 0; // Disable timer interrupt TR1 = 1; // Start timer 1}
void Uart_PutByte(unsigned char ch){SBUF = ch;while(!TI){;}TI = 0;}/******************************************************************************- Function Description: Send commands out of the serial port [including control and query] - Parameter Description: CMD: Represents control commands, please refer to the command table, which also includes related query commands feedback: Whether a response is required [0: no response required, 1: response required] data: Parameters to be transmitted **********************************************************************************/void SendCmd(unsigned char len){unsigned char i = 0 ;Uart_PutByte(0x7E); // Start for(i=0; i{Uart_PutByte(Send_buf[i]) ;}Uart_PutByte(0xEF) ;//End}/******************************************************************************- Function Description: Sum Check - The idea of sum check is as follows: The sent command removes the start and end. The middle 6 bytes are accumulated and finally the inverse code is taken. The receiving end removes the start and end of the received frame of data. The middle data is accumulated and the received check byte is added. It is exactly 0. This means that the received data is completely correct. ******************************************************************************/void DoSum( unsigned char *Str, unsigned char len){unsigned int xorsum = 0;unsigned char i;for(i=0; i{xorsum = xorsum + Str[i];}xorsum = 0 -xorsum;*(Str+i) = (unsigned char)(xorsum >> 8);*(Str+i+1) = (unsigned char)(xorsum & 0x00ff);}void Uart_SendCMD(unsigned char CMD ,unsigned char feedback , unsigned char dat){Send_buf[0] = 0xff; // Reserved bytes Send_buf[1] = 0x06; // Length Send_buf[2] = CMD; // Control instructions Send_buf[3] = feedback;// Whether feedback is needed Send_buf[4] = (unsigned char)(dat >> 8);//datahSend_buf[5] = (unsigned char)(dat); //datalDoSum(&Send_buf[0],6); // Verify SendCmd(8); // Send this frame data}