In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to solve the Bug of c # code". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
BUG record 5Mel-array access out of bounds, causing the software to enter the HardFault
This is a common and easy to ignore problem. From the beginning of learning the C language array, we are told that the subscript of the array starts from 0, but in our actual case applications, the serial number usually starts from 1. In a practical application, the acquisition board collects the value of channel 1-channel 6, and the main control board also defines a data buffer of size 6. Index from 0 to 5, the acquisition board collects the data of six channels, packages the data according to the format of the beginning of the frame, the channel number, the sampling value and the end of the frame, and then sends it to the main control board, and the main control stores the sampling value to the buffer according to the channel number. Because the channel number of the acquisition board is written from 1 to 6 when coding is neglected, when the main control board receives the data frame with channel number 6, the array is stored out of bounds, and the software enters the HardFault dead loop.
Void HardFault_Handler (void) {/ * Go to infinite loop when HardFault exception occurs * / while (1) {}} BUG records the priority problem of 6 Mel-left and right shift operators and bitwise and / OR operators &, | priority issues / / Righthost_control_slave [2] = (gRtcDate&0xFF0000) > > 16 transitions hostworthy controlboxes [3] = (gRtcDate&0xFF00) > > 8 Errorhost_control_slave hostmakers controlmakers [4] = gRtcDate&0xFF; / / Errorhost_control_slave [2] = gRtcDate&0xFF0000 > 16 > Host_control_slave [3] = gRtcDate & 0xFF00 > > 8 [4] = gRtcDate & 0xFF
The left and right shift operators and the bitwise and / OR operators are combined from left to right, and the former takes precedence over the latter, so the above two blocks of code will get different results, and the later code will first move to the right, and then carry out the bitwise and operation. I am lazy, generally do not remember the priority list, usually use () to box it up, this also has some disadvantages, that is, too many parentheses seem to be very difficult, so it is recommended that some commonly used operator priority order should be noted.
BUG records the configuration problem of 7--GPIO mode void GpioConfigInit (void) {GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOC, ENABLE); / / sets GPIOC_12 to push-pull output GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz / / set GPIOC_13 to pull up and enter GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init (GPIOC, & GPIO_InitStructure);}
In the actual embedded project, the operation of GPIO is quite common, but it also hides some potential pits. After the above initialization function is configured with GPIOC_12 and GPIO_13, the push-pull output of GPIOC_12 is not configured successfully, because in this function, the structure initialization operation is carried out at the end, which is equivalent to just writing the GPIOC_13 mode into the structure parameters. Therefore, the mode of the two IO ports is pull-up input. Let's analyze the member parameters of the GPIO structure:
/ * * @ brief GPIO Init structure definition * / typedef struct {uint16_t GPIO_Pin; / *!
< Specifies the GPIO pins to be configured. This parameter can be any value of @ref GPIO_pins_define */ GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins. This parameter can be a value of @ref GPIOSpeed_TypeDef */ GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins. This parameter can be a value of @ref GPIOMode_TypeDef */}GPIO_InitTypeDef;/** * @brief Initializes the GPIOx peripheral according to the specified * parameters in the GPIO_InitStruct. * @param GPIOx: where x can be (A..G) to select the GPIO peripheral. * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that * contains the configuration information for the specified GPIO peripheral. * @retval None */void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct){ uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00; uint32_t tmpreg = 0x00, pinmask = 0x00; /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode); assert_param (IS_GPIO_PIN (GPIO_InitStruct- > GPIO_Pin)); / *-- GPIO Mode Configuration-- * / currentmode = ((uint32_t) GPIO_InitStruct- > GPIO_Mode) & ((uint32_t) 0x0F) If (uint32_t) GPIO_InitStruct- > GPIO_Mode) & ((uint32_t) 0x10))! = 0x00) {/ * Check the parameters * / assert_param (IS_GPIO_SPEED (GPIO_InitStruct- > GPIO_Speed)); / * Output mode * / currentmode | = (uint32_t) GPIO_InitStruct- > GPIO_Speed } / *-GPIO CRL Configuration-* / * Configure the eight low port pins * / if (uint32_t) GPIO_InitStruct- > GPIO_Pin & ((uint32_t) 0x00FF))! = 0x00) {tmpreg = GPIOx- > CRL; for (pinpos = 0x00; pinpos)
< 0x08; pinpos++) { pos = ((uint32_t)0x01) GPIO_Pin) & pos; if (currentpin == pos) { pos = pinpos BSRR = (((uint32_t)0x01) CRL = tmpreg; }/*---------------------------- GPIO CRH Configuration ------------------------*/ /* Configure the eight high port pins */ if (GPIO_InitStruct->GPIO_Pin > 0x00FF) {tmpreg = GPIOx- > CRH; for (pinpos = 0x00; pinpos)
< 0x08; pinpos++) { pos = (((uint32_t)0x01) GPIO_Pin) & pos); if (currentpin == pos) { pos = pinpos BSRR = (((uint32_t)0x01) CRH = tmpreg; }} 上述的函数会把GPIO的引脚、速度、模式,写入到相关的寄存器中去,在GpioConfigInit()函数中如果只写入一次引脚、速度、模式等参数,那么只会以最后一次的参数为准,因为这里定义的结构体变量为局部变量,在配置GPIOC_13时又重新被赋值,然后再被写入,所以就会出现GPIOC_12模式配置失败的问题,正确的配置函数为: void GpioConfigInit(void){ GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //设置GPIOC_12为推挽输出 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); //设置GPIOC_13为上拉输入 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); }BUG记录8--触摸屏刷新频率,并不是越快越好 提高触摸屏的刷新频率主要是为了提高用户体验感,让用户用起来感觉更加流畅,但是也不是越快越好,最好要参考触摸屏厂商提供的推荐刷新频率来设置,如下为某款触摸屏的推荐参数: MCU不要频繁向串口屏发送数据,否则串口屏的内部缓存区会满,从而导致数据丢失(缓冲区大小:标准型8K,基本型4.7K)1) 一般情况下,控制MCU向串口屏发送数据的周期大于100ms,就可以避免数据丢失的问题;2) 如果仍然有数据丢失的问题,请判断串口屏的BUSY引脚,为高时不能发送数据给串口屏。 我之前试过10ms来刷新一些参数,然后发现触摸屏的其他一些操作就非常卡顿,因为触摸屏自带的MCU一直在不断的进入数据接收中断并需要不断的处理,有时候还会出现数据乱码,其原因也就是内部的一级缓冲区产生了溢出。 BUG记录9--指针数组的错误寻址 在编程过程中,我一般将指针数组用来存放字符串信息,如下代码块: const char *ErrInf[5] = { "光子计数器故障!", "空杯测试故障!", "温控仪通讯故障!", "打印机缺纸!", "过温或欠温!"}; 那我们看下这些字符串信息如何在内存中存储:/ *! *\ brief text box update *\ param screen_id screen ID*\ param control_id control ID*/void WriteTextValue (uint16 screen_id, uint16 control_id, uint8 * pData, uint16 nDataLen) {BEGIN_CMD (); TX_8 (0xB1); TX_8 (0x10); TX_16 (screen_id); TX_16 (control_id); SendNU8 (pData, nDataLen); END_CMD ();}
If you need to update the first string information above, you should update the function as follows by calling the text box:
/ RightWriteTextValue (0x01, 0x01, (uint8*) ErrInf [0], strlen (ErrInf [0])); / / ErrorWriteTextValue (0x01, 0x01, (uint8*) & ErrInf [0], strlen (ErrInf [0]))
Because the first string stores the first address ErrInf [0] instead of & ErrInf [0], & ErrInf [0] is the address of ErrInf [0], there is a difference between the two addresses, so never add the address character &.
BUG records typedef struct {char * HybDenName; U8 SetHybDenTemp [2]; U32 SetHybDenTime [2];} HybDenPrgPara_TypeDef;typedef struct {U8 HybDenRow; U8 HybRow; U8 MulRow; U8 HybDenCol; U8 MulCol;} PrgFlowIndex_TypeDef HybDenPrgPara_TypeDef HybDenFlowPara [FLOW_NUM_MAX] = {{"DenHyb01", 0,0,0,0}, {"DenHyb02", 0,0,0,0}, {"DenHyb03", 0,0,0,0}, {"DenHyb04", 0,0,0,0}, {"DenHyb05", 0,0,0,0}, {"DenHyb07", 0,0,0,0}, {"DenHyb08", 0,0,0}. 0}, {"DenHyb09", 0,0,0}, {"DenHyb10", 0,0,0,0},} PrgFlowIndex_TypeDef PrgFlowIndex = {0x00, 0x00, 0x00, 0x00, 0x00}; / / code blockcase LoadPgUp: {if (0x00 = = PrgFlowIndex.HybDenRow) {PrgFlowIndex.HybDenRow = PRG_FLOW_NUM_MAX-0x01;} else {PrgFlowIndex.HybDenRow--;} / / write the new program name SetTextValue (HybDenLoad, LoadName, (u8*) HybDenFlowPara [PrgFlowIndex.HybDenRow] .HybDenName) in the program loading interface, running interface, name editing interface. SetTextValue (HybDenRun, LoadName, (u8*) HybDenFlowPara [PrgFlowIndex.HybDenRow] .HybDenName); SetTextValue (HybDenNameEdit, LoadName, (u8*) HybDenFlowPara [PrgFlowIndex.HybDenRow] .HybDenName); break;}
What problems will arise before placing PrgFlowIndex.HybDenRow-- on if-else? When PrgFlowIndex.HybDenRow equals 0, minus 1, the value becomes 255. the following SetTextValue () function will be written to illegal memory when writing structure array parameters, which will also cause HardFault errors.
This is the end of the content of "how to solve the problem of c # Code Bug". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.