[National Technology N32G457 Review] ML component control
[Copy link]
This post was last edited by jinglixixi on 2022-2-7 20:43
N32G457 provides multiple serial ports for use. Providing these serial ports can greatly expand the functions of the development board, because there are currently a variety of serial port-based functional modules to choose from and use, such as serial port-based MP3 voice playback module, serial port-based touch display, serial port-based ultrasonic sensor, etc. It is very important to master the use of serial ports.
Here we introduce an example of using a serial port to drive an ML component. It can encapsulate the detected data into data that drives the ML component and send it to the host computer via the serial port to intuitively indicate data changes with dynamic changes.
There are many types of ML components, such as scales, instrument panels, batteries, progress balls, and digital tubes. Figure 1 is a display interface made with ML components, and Figure 2 shows the display effect driven by data.
Figure 1 ML display interface
Figure 2 Data driven effect
The data encapsulation of ML components is relatively complex. To facilitate porting and use, we wrote the corresponding data encapsulation function according to its structure, and stored the encapsulation results in an array for serial port transmission.
The corresponding array initial values are:
char zl[20]={0x58, 0x5A, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
char data[4]={0x00, 0x00, 0x00, 0x00};
The function to implement floating point conversion is:
void updateCanvas(char * data, unsigned short datalen)
{
zl[17]= data[2];
zl[18]= data[3];
}
The data encapsulation function of the ML component is:
void change(uint8_t i, uint8_t n)
{
uint8_t CV;
CV=0xC9;
if(i==0)
{
zl[9]=0xEF;
}
if(i==1)
{
zl[9]=0xE9;
}
if(i==2)
{
zl[9]=0xEA;
}
if(i==3)
{
zl[9]=0xF2;
}
if(i==4)
{
zl[9]=0xEB;
}
...
if(n==0)
{
zl[11]=0x00;
}
if(n==1)
{
zl[11]=0x01;
}
if(n==2)
{
zl[11]=0x02;
}
...
CV=CV+zl[9]+zl[11]+zl[17]+zl[18];
zl[19]=CV;
}
The serial port sending function used is:
void Uart_Send(void)
{
uint8_t i;
for(i=0;i<20;i++)
{
USART_SendData(USARTx,zl);
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET);
}
}
The main program to achieve the driving effect shown in Figure 2 is:
int main(void)
{
float x = 57;
RCC_Configuration();
GPIO_Configuration();
USART_InitStructure.BaudRate = 9600;
USART_InitStructure.WordLength = USART_WL_8B;
USART_InitStructure.StopBits = USART_STPB_1;
USART_InitStructure.Parity = USART_PE_NO;
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
USART_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX;
USART_Init(USARTx, &USART_InitStructure);
USART_Enable(USARTx, ENABLE);
updateCanvas( (char *)&x, sizeof(float));
change(1, 0);
Uart_Send();
SysTick_Delay_Ms(100);
x = 60;
updateCanvas( (char *)&x, sizeof(float));
change(2, 0);
Uart_Send();
SysTick_Delay_Ms(100);
x = 26;
updateCanvas( (char *)&x, sizeof(float));
change(3, 0);
Uart_Send();
SysTick_Delay_Ms(100);
x = 75;
updateCanvas( (char *)&x, sizeof(float));
change(3, 1);
Uart_Send();
SysTick_Delay_Ms(100);
x = 1024;
updateCanvas( (char *)&x, sizeof(float));
change(4, 1);
Uart_Send();
SysTick_Delay_Ms(100);
x = 720;
updateCanvas( (char *)&x, sizeof(float));
change(4, 0);
Uart_Send();
SysTick_Delay_Ms(100);
while (1);
}
The test result using the serial port debugging tool is shown in Figure 3. Based on this program, dynamic data indication can be achieved by connecting corresponding sensors.
Figure 3 Serial port test results
|