uint32_t在C语言里起什么作用,他是怎么定义使用的?

看到很多简单的c程序都有uint32_t,它是怎么定义使用的,是什么意思

c

1个回答

32位无符号整形数,这个你得看它的头文件,一般都是有宏定义的,如#define unsigned int uint32_t

weixin_43846332
从前有条河 好的谢谢!
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
C语言 Union和Struct成员赋值问题
``` #include "pch.h" #include <iostream> #include <bitset> using namespace std; typedef struct { union { uint32_t _32; uint16_t _16; uint8_t _8[2]; }gpr[8]; uint32_t eax = gpr[0]._32; uint32_t ecx = gpr[1]._32; }CPU_State; int main() { CPU_State cpu; //先测试gpr0和gpr2 //随便写的数据 cpu.gpr[0]._32 = 0xabcdf1f0; cpu.gpr[1]._32 = 0xabcdf1f1; //现在只能输出未初始化的数据 //我的电脑输出cccccccc和cccccccc cout << hex << cpu.eax << endl; cout << hex << cpu.ecx << endl; } ``` 我现在给gpr[0]._32和gpr[1]._32赋值。 现在输出的eax和ecx的值是错的。 我想把eax和ecx的值关联与gpr[0]._32和gpr[1]._32。 限定只能修改结构体的定义。 请问我该如何更改CPU_State这个结构体的定义,使得eax,ecx的值分别等于gpr[0]._32和gpr[1]._32。
STM32F105主板HAL库函数HAL_UART_Receive_IT()中第二个参数uint8_t *pData必须为无符号整型??
stm32f1xx_hal_uart.c文件中对HAL_UART_Receive_IT()函数的描述为 ``` HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) { /* Check that a Tx process is not already ongoing */ if(huart->gState == HAL_UART_STATE_READY) { if((pData == NULL) || (Size == 0U)) { return HAL_ERROR; } /* Process Locked */ __HAL_LOCK(huart); huart->pTxBuffPtr = pData; huart->TxXferSize = Size; huart->TxXferCount = Size; huart->ErrorCode = HAL_UART_ERROR_NONE; huart->gState = HAL_UART_STATE_BUSY_TX; /* Process Unlocked */ __HAL_UNLOCK(huart); /* Enable the UART Transmit data register empty Interrupt */ __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); return HAL_OK; } else { return HAL_BUSY; } } ``` 其中第二个参数定义为uint8_t *pData 本人使用串口中断接收温度数据,而温度数据是有符号整型的,与上述函数定义矛盾,请问应该怎样更改呢??
小白使用keil编程debug进入了harddefault,搞了半天没解决。求大佬帮下忙。
/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "can.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ void CAN_SetTxMsg(void) { hcan.pTxMsg->ExtId=0x1314; //使用的扩展ID hcan.pTxMsg->IDE=CAN_ID_EXT; //扩展模式 hcan.pTxMsg->RTR=CAN_RTR_DATA; //发送的是数据 hcan.pTxMsg->DLC=8; //数据长度为2字节 hcan.pTxMsg->data[0]=0x01; hcan.pTxMsg->data[1]=0x02; hcan.pTxMsg->data[2]=0x03; hcan.pTxMsg->data[3]=0x04; hcan.pTxMsg->data[4]=0x05; hcan.pTxMsg->data[5]=0x06; hcan.pTxMsg->data[6]=0x07; hcan.pTxMsg->data[7]=0x08; } /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_CAN_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ CAN_SetTxMsg(); HAL_CAN_AddTxMessage(&hcan,hcan.pTxMsg,hcan.pTxMsg->data,(uint32_t*)CAN_TX_MAILBOX0); HAL_CAN_ActivateNotification(&hcan,CAN_IT_RX_FIFO0_MSG_PENDING); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB busses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } /** Enables the Clock Security System */ HAL_RCC_EnableCSS(); } /* USER CODE BEGIN 4 */ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) { HAL_CAN_AddTxMessage(hcan,hcan->pTxMsg,hcan->pTxMsg->data,(uint32_t*)CAN_TX_MAILBOX0); HAL_CAN_ActivateNotification(hcan,CAN_IT_RX_FIFO0_MSG_PENDING); } /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 这是我的主程序,debug总是在CAN_SetMsg()那里进入了hardfault.下面是定义的一些初始化结构体。 /** ****************************************************************************** * @file stm32f1xx_hal_can.h * @author MCD Application Team * @brief Header file of CAN HAL module. ****************************************************************************** * @attention * * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2> * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef STM32F1xx_HAL_CAN_H #define STM32F1xx_HAL_CAN_H #ifdef __cplusplus extern "C" { #endif /* Includes ------------------------------------------------------------------*/ #include "stm32f1xx_hal_def.h" /** @addtogroup STM32F1xx_HAL_Driver * @{ */ #if defined (CAN1) /** @addtogroup CAN * @{ */ /* Exported types ------------------------------------------------------------*/ /** @defgroup CAN_Exported_Types CAN Exported Types * @{ */ /** * @brief HAL State structures definition */ typedef enum { HAL_CAN_STATE_RESET = 0x00U, /*!< CAN not yet initialized or disabled */ HAL_CAN_STATE_READY = 0x01U, /*!< CAN initialized and ready for use */ HAL_CAN_STATE_LISTENING = 0x02U, /*!< CAN receive process is ongoing */ HAL_CAN_STATE_SLEEP_PENDING = 0x03U, /*!< CAN sleep request is pending */ HAL_CAN_STATE_SLEEP_ACTIVE = 0x04U, /*!< CAN sleep mode is active */ HAL_CAN_STATE_ERROR = 0x05U /*!< CAN error state */ } HAL_CAN_StateTypeDef; /** * @brief CAN init structure definition */ typedef struct { uint32_t Prescaler; /*!< Specifies the length of a time quantum. This parameter must be a number between Min_Data = 1 and Max_Data = 1024. */ uint32_t Mode; /*!< Specifies the CAN operating mode. This parameter can be a value of @ref CAN_operating_mode */ uint32_t SyncJumpWidth; /*!< Specifies the maximum number of time quanta the CAN hardware is allowed to lengthen or shorten a bit to perform resynchronization. This parameter can be a value of @ref CAN_synchronisation_jump_width */ uint32_t TimeSeg1; /*!< Specifies the number of time quanta in Bit Segment 1. This parameter can be a value of @ref CAN_time_quantum_in_bit_segment_1 */ uint32_t TimeSeg2; /*!< Specifies the number of time quanta in Bit Segment 2. This parameter can be a value of @ref CAN_time_quantum_in_bit_segment_2 */ FunctionalState TimeTriggeredMode; /*!< Enable or disable the time triggered communication mode. This parameter can be set to ENABLE or DISABLE. */ FunctionalState AutoBusOff; /*!< Enable or disable the automatic bus-off management. This parameter can be set to ENABLE or DISABLE. */ FunctionalState AutoWakeUp; /*!< Enable or disable the automatic wake-up mode. This parameter can be set to ENABLE or DISABLE. */ FunctionalState AutoRetransmission; /*!< Enable or disable the non-automatic retransmission mode. This parameter can be set to ENABLE or DISABLE. */ FunctionalState ReceiveFifoLocked; /*!< Enable or disable the Receive FIFO Locked mode. This parameter can be set to ENABLE or DISABLE. */ FunctionalState TransmitFifoPriority;/*!< Enable or disable the transmit FIFO priority. This parameter can be set to ENABLE or DISABLE. */ } CAN_InitTypeDef; /** * @brief CAN filter configuration structure definition */ typedef struct { uint32_t FilterIdHigh; /*!< Specifies the filter identification number (MSBs for a 32-bit configuration, first one for a 16-bit configuration). This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0xFFFF. */ uint32_t FilterIdLow; /*!< Specifies the filter identification number (LSBs for a 32-bit configuration, second one for a 16-bit configuration). This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0xFFFF. */ uint32_t FilterMaskIdHigh; /*!< Specifies the filter mask number or identification number, according to the mode (MSBs for a 32-bit configuration, first one for a 16-bit configuration). This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0xFFFF. */ uint32_t FilterMaskIdLow; /*!< Specifies the filter mask number or identification number, according to the mode (LSBs for a 32-bit configuration, second one for a 16-bit configuration). This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0xFFFF. */ uint32_t FilterFIFOAssignment; /*!< Specifies the FIFO (0 or 1U) which will be assigned to the filter. This parameter can be a value of @ref CAN_filter_FIFO */ uint32_t FilterBank; /*!< Specifies the filter bank which will be initialized. For single CAN instance(14 dedicated filter banks), this parameter must be a number between Min_Data = 0 and Max_Data = 13. For dual CAN instances(28 filter banks shared), this parameter must be a number between Min_Data = 0 and Max_Data = 27. */ uint32_t FilterMode; /*!< Specifies the filter mode to be initialized. This parameter can be a value of @ref CAN_filter_mode */ uint32_t FilterScale; /*!< Specifies the filter scale. This parameter can be a value of @ref CAN_filter_scale */ uint32_t FilterActivation; /*!< Enable or disable the filter. This parameter can be a value of @ref CAN_filter_activation */ uint32_t SlaveStartFilterBank; /*!< Select the start filter bank for the slave CAN instance. For single CAN instances, this parameter is meaningless. For dual CAN instances, all filter banks with lower index are assigned to master CAN instance, whereas all filter banks with greater index are assigned to slave CAN instance. This parameter must be a number between Min_Data = 0 and Max_Data = 27. */ } CAN_FilterTypeDef; /** * @brief CAN Tx message header structure definition */ typedef struct { uint32_t StdId; /*!< Specifies the standard identifier. This parameter must be a number between Min_Data = 0 and Max_Data = 0x7FF. */ uint32_t ExtId; /*!< Specifies the extended identifier. This parameter must be a number between Min_Data = 0 and Max_Data = 0x1FFFFFFF. */ uint32_t IDE; /*!< Specifies the type of identifier for the message that will be transmitted. This parameter can be a value of @ref CAN_identifier_type */ uint32_t RTR; /*!< Specifies the type of frame for the message that will be transmitted. This parameter can be a value of @ref CAN_remote_transmission_request */ uint32_t DLC; /*!< Specifies the length of the frame that will be transmitted. This parameter must be a number between Min_Data = 0 and Max_Data = 8. */ uint8_t data[8]; FunctionalState TransmitGlobalTime; /*!< Specifies whether the timestamp counter value captured on start of frame transmission, is sent in DATA6 and DATA7 replacing pData[6] and pData[7]. @note: Time Triggered Communication Mode must be enabled. @note: DLC must be programmed as 8 bytes, in order these 2 bytes are sent. This parameter can be set to ENABLE or DISABLE. */ } CAN_TxHeaderTypeDef; /** * @brief CAN Rx message header structure definition */ typedef struct { uint32_t StdId; /*!< Specifies the standard identifier. This parameter must be a number between Min_Data = 0 and Max_Data = 0x7FF. */ uint32_t ExtId; /*!< Specifies the extended identifier. This parameter must be a number between Min_Data = 0 and Max_Data = 0x1FFFFFFF. */ uint32_t IDE; /*!< Specifies the type of identifier for the message that will be transmitted. This parameter can be a value of @ref CAN_identifier_type */ uint32_t RTR; /*!< Specifies the type of frame for the message that will be transmitted. This parameter can be a value of @ref CAN_remote_transmission_request */ uint32_t DLC; /*!< Specifies the length of the frame that will be transmitted. This parameter must be a number between Min_Data = 0 and Max_Data = 8. */ uint8_t Data[8]; uint32_t Timestamp; /*!< Specifies the timestamp counter value captured on start of frame reception. @note: Time Triggered Communication Mode must be enabled. This parameter must be a number between Min_Data = 0 and Max_Data = 0xFFFF. */ uint32_t FilterMatchIndex; /*!< Specifies the index of matching acceptance filter element. This parameter must be a number between Min_Data = 0 and Max_Data = 0xFF. */ } CAN_RxHeaderTypeDef; /** * @brief CAN handle Structure definition */ typedef struct __CAN_HandleTypeDef { CAN_TypeDef *Instance; /*!< Register base address */ CAN_InitTypeDef Init; /*!< CAN required parameters */ CAN_TxHeaderTypeDef* pTxMsg; CAN_RxHeaderTypeDef* pRxMsg; HAL_LockTypeDef Lock; __IO HAL_CAN_StateTypeDef State; /*!< CAN communication state */ __IO uint32_t ErrorCode; /*!< CAN Error code. This parameter can be a value of @ref CAN_Error_Code */ } CAN_HandleTypeDef; /** * @} */ /* Exported constants --------------------------------------------------------*/ /** @defgroup CAN_Exported_Constants CAN Exported Constants * @{ */ /** @defgroup CAN_Error_Code CAN Error Code * @{ */ #define HAL_CAN_ERROR_NONE (0x00000000U) /*!< No error */ #define HAL_CAN_ERROR_EWG (0x00000001U) /*!< Protocol Error Warning */ #define HAL_CAN_ERROR_EPV (0x00000002U) /*!< Error Passive */ #define HAL_CAN_ERROR_BOF (0x00000004U) /*!< Bus-off error */ #define HAL_CAN_ERROR_STF (0x00000008U) /*!< Stuff error */ #define HAL_CAN_ERROR_FOR (0x00000010U) /*!< Form error */ #define HAL_CAN_ERROR_ACK (0x00000020U) /*!< Acknowledgment error */ #define HAL_CAN_ERROR_BR (0x00000040U) /*!< Bit recessive error */ #define HAL_CAN_ERROR_BD (0x00000080U) /*!< Bit dominant error */ #define HAL_CAN_ERROR_CRC (0x00000100U) /*!< CRC error */ #define HAL_CAN_ERROR_RX_FOV0 (0x00000200U) /*!< Rx FIFO0 overrun error */ #define HAL_CAN_ERROR_RX_FOV1 (0x00000400U) /*!< Rx FIFO1 overrun error */ #define HAL_CAN_ERROR_TX_ALST0 (0x00000800U) /*!< TxMailbox 0 transmit failure due to arbitration lost */ #define HAL_CAN_ERROR_TX_TERR0 (0x00001000U) /*!< TxMailbox 1 transmit failure due to tranmit error */ #define HAL_CAN_ERROR_TX_ALST1 (0x00002000U) /*!< TxMailbox 0 transmit failure due to arbitration lost */ #define HAL_CAN_ERROR_TX_TERR1 (0x00004000U) /*!< TxMailbox 1 transmit failure due to tranmit error */ #define HAL_CAN_ERROR_TX_ALST2 (0x00008000U) /*!< TxMailbox 0 transmit failure due to arbitration lost */ #define HAL_CAN_ERROR_TX_TERR2 (0x00010000U) /*!< TxMailbox 1 transmit failure due to tranmit error */ #define HAL_CAN_ERROR_TIMEOUT (0x00020000U) /*!< Timeout error */ #define HAL_CAN_ERROR_NOT_INITIALIZED (0x00040000U) /*!< Peripheral not initialized */ #define HAL_CAN_ERROR_NOT_READY (0x00080000U) /*!< Peripheral not ready */ #define HAL_CAN_ERROR_NOT_STARTED (0x00100000U) /*!< Peripheral not started */ #define HAL_CAN_ERROR_PARAM (0x00200000U) /*!< Parameter error */ #define HAL_CAN_ERROR_INTERNAL (0x00800000U) /*!< Internal error */ /** * @} */ /** @defgroup CAN_InitStatus CAN InitStatus * @{ */ #define CAN_INITSTATUS_FAILED (0x00000000U) /*!< CAN initialization failed */ #define CAN_INITSTATUS_SUCCESS (0x00000001U) /*!< CAN initialization OK */ /** * @} */ /** @defgroup CAN_operating_mode CAN Operating Mode * @{ */ #define CAN_MODE_NORMAL (0x00000000U) /*!< Normal mode */ #define CAN_MODE_LOOPBACK ((uint32_t)CAN_BTR_LBKM) /*!< Loopback mode */ #define CAN_MODE_SILENT ((uint32_t)CAN_BTR_SILM) /*!< Silent mode */ #define CAN_MODE_SILENT_LOOPBACK ((uint32_t)(CAN_BTR_LBKM | CAN_BTR_SILM)) /*!< Loopback combined with silent mode */ /** * @} */ /** @defgroup CAN_synchronisation_jump_width CAN Synchronization Jump Width * @{ */ #define CAN_SJW_1TQ (0x00000000U) /*!< 1 time quantum */ #define CAN_SJW_2TQ ((uint32_t)CAN_BTR_SJW_0) /*!< 2 time quantum */ #define CAN_SJW_3TQ ((uint32_t)CAN_BTR_SJW_1) /*!< 3 time quantum */ #define CAN_SJW_4TQ ((uint32_t)CAN_BTR_SJW) /*!< 4 time quantum */ /** * @} */ /** @defgroup CAN_time_quantum_in_bit_segment_1 CAN Time Quantum in Bit Segment 1 * @{ */ #define CAN_BS1_1TQ (0x00000000U) /*!< 1 time quantum */ #define CAN_BS1_2TQ ((uint32_t)CAN_BTR_TS1_0) /*!< 2 time quantum */ #define CAN_BS1_3TQ ((uint32_t)CAN_BTR_TS1_1) /*!< 3 time quantum */ #define CAN_BS1_4TQ ((uint32_t)(CAN_BTR_TS1_1 | CAN_BTR_TS1_0)) /*!< 4 time quantum */ #define CAN_BS1_5TQ ((uint32_t)CAN_BTR_TS1_2) /*!< 5 time quantum */ #define CAN_BS1_6TQ ((uint32_t)(CAN_BTR_TS1_2 | CAN_BTR_TS1_0)) /*!< 6 time quantum */ #define CAN_BS1_7TQ ((uint32_t)(CAN_BTR_TS1_2 | CAN_BTR_TS1_1)) /*!< 7 time quantum */ #define CAN_BS1_8TQ ((uint32_t)(CAN_BTR_TS1_2 | CAN_BTR_TS1_1 | CAN_BTR_TS1_0)) /*!< 8 time quantum */ #define CAN_BS1_9TQ ((uint32_t)CAN_BTR_TS1_3) /*!< 9 time quantum */ #define CAN_BS1_10TQ ((uint32_t)(CAN_BTR_TS1_3 | CAN_BTR_TS1_0)) /*!< 10 time quantum */ #define CAN_BS1_11TQ ((uint32_t)(CAN_BTR_TS1_3 | CAN_BTR_TS1_1)) /*!< 11 time quantum */ #define CAN_BS1_12TQ ((uint32_t)(CAN_BTR_TS1_3 | CAN_BTR_TS1_1 | CAN_BTR_TS1_0)) /*!< 12 time quantum */ #define CAN_BS1_13TQ ((uint32_t)(CAN_BTR_TS1_3 | CAN_BTR_TS1_2)) /*!< 13 time quantum */ #define CAN_BS1_14TQ ((uint32_t)(CAN_BTR_TS1_3 | CAN_BTR_TS1_2 | CAN_BTR_TS1_0)) /*!< 14 time quantum */ #define CAN_BS1_15TQ ((uint32_t)(CAN_BTR_TS1_3 | CAN_BTR_TS1_2 | CAN_BTR_TS1_1)) /*!< 15 time quantum */ #define CAN_BS1_16TQ ((uint32_t)CAN_BTR_TS1) /*!< 16 time quantum */ /** * @} */ /** @defgroup CAN_time_quantum_in_bit_segment_2 CAN Time Quantum in Bit Segment 2 * @{ */ #define CAN_BS2_1TQ (0x00000000U) /*!< 1 time quantum */ #define CAN_BS2_2TQ ((uint32_t)CAN_BTR_TS2_0) /*!< 2 time quantum */ #define CAN_BS2_3TQ ((uint32_t)CAN_BTR_TS2_1) /*!< 3 time quantum */ #define CAN_BS2_4TQ ((uint32_t)(CAN_BTR_TS2_1 | CAN_BTR_TS2_0)) /*!< 4 time quantum */ #define CAN_BS2_5TQ ((uint32_t)CAN_BTR_TS2_2) /*!< 5 time quantum */ #define CAN_BS2_6TQ ((uint32_t)(CAN_BTR_TS2_2 | CAN_BTR_TS2_0)) /*!< 6 time quantum */ #define CAN_BS2_7TQ ((uint32_t)(CAN_BTR_TS2_2 | CAN_BTR_TS2_1)) /*!< 7 time quantum */ #define CAN_BS2_8TQ ((uint32_t)CAN_BTR_TS2) /*!< 8 time quantum */ /** * @} */ /** @defgroup CAN_filter_mode CAN Filter Mode * @{ */ #define CAN_FILTERMODE_IDMASK (0x00000000U) /*!< Identifier mask mode */ #define CAN_FILTERMODE_IDLIST (0x00000001U) /*!< Identifier list mode */ /** * @} */ /** @defgroup CAN_filter_scale CAN Filter Scale * @{ */ #define CAN_FILTERSCALE_16BIT (0x00000000U) /*!< Two 16-bit filters */ #define CAN_FILTERSCALE_32BIT (0x00000001U) /*!< One 32-bit filter */ /** * @} */ /** @defgroup CAN_filter_activation CAN Filter Activation * @{ */ #define CAN_FILTER_DISABLE (0x00000000U) /*!< Disable filter */ #define CAN_FILTER_ENABLE (0x00000001U) /*!< Enable filter */ /** * @} */ /** @defgroup CAN_filter_FIFO CAN Filter FIFO * @{ */ #define CAN_FILTER_FIFO0 (0x00000000U) /*!< Filter FIFO 0 assignment for filter x */ #define CAN_FILTER_FIFO1 (0x00000001U) /*!< Filter FIFO 1 assignment for filter x */ /** * @} */ /** @defgroup CAN_identifier_type CAN Identifier Type * @{ */ #define CAN_ID_STD ((uint32_t)0x00000000) /*!< Standard Id */ #define CAN_ID_EXT ((uint32_t)0x00000004) /*!< Extended Id */ /** * @} */ /** @defgroup CAN_remote_transmission_request CAN Remote Transmission Request * @{ */ #define CAN_RTR_DATA ((uint32_t)0x00000000) /*!< Data frame */ #define CAN_RTR_REMOTE ((uint32_t)0x00000002) /*!< Remote frame */ /** * @} */ /** @defgroup CAN_receive_FIFO_number CAN Receive FIFO Number * @{ */ #define CAN_RX_FIFO0 (0x00000000U) /*!< CAN receive FIFO 0 */ #define CAN_RX_FIFO1 (0x00000001U) /*!< CAN receive FIFO 1 */ /** * @} */ /** @defgroup CAN_Tx_Mailboxes CAN Tx Mailboxes * @{ */ #define CAN_TX_MAILBOX0 (0x00000001U) /*!< Tx Mailbox 0 */ #define CAN_TX_MAILBOX1 (0x00000002U) /*!< Tx Mailbox 1 */ #define CAN_TX_MAILBOX2 (0x00000004U) /*!< Tx Mailbox 2 */ /** * @} */ /** @defgroup CAN_flags CAN Flags * @{ */ /* Transmit Flags */ #define CAN_FLAG_RQCP0 (0x00000500U) /*!< Request complete MailBox 0 flag */ #define CAN_FLAG_TXOK0 (0x00000501U) /*!< Transmission OK MailBox 0 flag */ #define CAN_FLAG_ALST0 (0x00000502U) /*!< Arbitration Lost MailBox 0 flag */ #define CAN_FLAG_TERR0 (0x00000503U) /*!< Transmission error MailBox 0 flag */ #define CAN_FLAG_RQCP1 (0x00000508U) /*!< Request complete MailBox1 flag */ #define CAN_FLAG_TXOK1 (0x00000509U) /*!< Transmission OK MailBox 1 flag */ #define CAN_FLAG_ALST1 (0x0000050AU) /*!< Arbitration Lost MailBox 1 flag */ #define CAN_FLAG_TERR1 (0x0000050BU) /*!< Transmission error MailBox 1 flag */ #define CAN_FLAG_RQCP2 (0x00000510U) /*!< Request complete MailBox2 flag */ #define CAN_FLAG_TXOK2 (0x00000511U) /*!< Transmission OK MailBox 2 flag */ #define CAN_FLAG_ALST2 (0x00000512U) /*!< Arbitration Lost MailBox 2 flag */ #define CAN_FLAG_TERR2 (0x00000513U) /*!< Transmission error MailBox 2 flag */ #define CAN_FLAG_TME0 (0x0000051AU) /*!< Transmit mailbox 0 empty flag */ #define CAN_FLAG_TME1 (0x0000051BU) /*!< Transmit mailbox 1 empty flag */ #define CAN_FLAG_TME2 (0x0000051CU) /*!< Transmit mailbox 2 empty flag */ #define CAN_FLAG_LOW0 (0x0000051DU) /*!< Lowest priority mailbox 0 flag */ #define CAN_FLAG_LOW1 (0x0000051EU) /*!< Lowest priority mailbox 1 flag */ #define CAN_FLAG_LOW2 (0x0000051FU) /*!< Lowest priority mailbox 2 flag */ /* Receive Flags */ #define CAN_FLAG_FF0 (0x00000203U) /*!< RX FIFO 0 Full flag */ #define CAN_FLAG_FOV0 (0x00000204U) /*!< RX FIFO 0 Overrun flag */ #define CAN_FLAG_FF1 (0x00000403U) /*!< RX FIFO 1 Full flag */ #define CAN_FLAG_FOV1 (0x00000404U) /*!< RX FIFO 1 Overrun flag */ /* Operating Mode Flags */ #define CAN_FLAG_INAK (0x00000100U) /*!< Initialization acknowledge flag */ #define CAN_FLAG_SLAK (0x00000101U) /*!< Sleep acknowledge flag */ #define CAN_FLAG_ERRI (0x00000102U) /*!< Error flag */ #define CAN_FLAG_WKU (0x00000103U) /*!< Wake up interrupt flag */ #define CAN_FLAG_SLAKI (0x00000104U) /*!< Sleep acknowledge interrupt flag */ /* Error Flags */ #define CAN_FLAG_EWG (0x00000300U) /*!< Error warning flag */ #define CAN_FLAG_EPV (0x00000301U) /*!< Error passive flag */ #define CAN_FLAG_BOF (0x00000302U) /*!< Bus-Off flag */ /** * @} */ /** @defgroup CAN_Interrupts CAN Interrupts * @{ */ /* Transmit Interrupt */ #define CAN_IT_TX_MAILBOX_EMPTY ((uint32_t)CAN_IER_TMEIE) /*!< Transmit mailbox empty interrupt */ /* Receive Interrupts */ #define CAN_IT_RX_FIFO0_MSG_PENDING ((uint32_t)CAN_IER_FMPIE0) /*!< FIFO 0 message pending interrupt */ #define CAN_IT_RX_FIFO0_FULL ((uint32_t)CAN_IER_FFIE0) /*!< FIFO 0 full interrupt */ #define CAN_IT_RX_FIFO0_OVERRUN ((uint32_t)CAN_IER_FOVIE0) /*!< FIFO 0 overrun interrupt */ #define CAN_IT_RX_FIFO1_MSG_PENDING ((uint32_t)CAN_IER_FMPIE1) /*!< FIFO 1 message pending interrupt */ #define CAN_IT_RX_FIFO1_FULL ((uint32_t)CAN_IER_FFIE1) /*!< FIFO 1 full interrupt */ #define CAN_IT_RX_FIFO1_OVERRUN ((uint32_t)CAN_IER_FOVIE1) /*!< FIFO 1 overrun interrupt */ /* Operating Mode Interrupts */ #define CAN_IT_WAKEUP ((uint32_t)CAN_IER_WKUIE) /*!< Wake-up interrupt */ #define CAN_IT_SLEEP_ACK ((uint32_t)CAN_IER_SLKIE) /*!< Sleep acknowledge interrupt */ /* Error Interrupts */ #define CAN_IT_ERROR_WARNING ((uint32_t)CAN_IER_EWGIE) /*!< Error warning interrupt */ #define CAN_IT_ERROR_PASSIVE ((uint32_t)CAN_IER_EPVIE) /*!< Error passive interrupt */ #define CAN_IT_BUSOFF ((uint32_t)CAN_IER_BOFIE) /*!< Bus-off interrupt */ #define CAN_IT_LAST_ERROR_CODE ((uint32_t)CAN_IER_LECIE) /*!< Last error code interrupt */ #define CAN_IT_ERROR ((uint32_t)CAN_IER_ERRIE) /*!< Error Interrupt */ /** * @} */ /** * @} */ /* Exported macros -----------------------------------------------------------*/ /** @defgroup CAN_Exported_Macros CAN Exported Macros * @{ */ /** @brief Reset CAN handle state * @param __HANDLE__ CAN handle. * @retval None */ #define __HAL_CAN_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_CAN_STATE_RESET) /** * @brief Enable the specified CAN interrupts. * @param __HANDLE__ CAN handle. * @param __INTERRUPT__ CAN Interrupt sources to enable. * This parameter can be any combination of @arg CAN_Interrupts * @retval None */ #define __HAL_CAN_ENABLE_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->IER) |= (__INTERRUPT__)) /** * @brief Disable the specified CAN interrupts. * @param __HANDLE__ CAN handle. * @param __INTERRUPT__ CAN Interrupt sources to disable. * This parameter can be any combination of @arg CAN_Interrupts * @retval None */ #define __HAL_CAN_DISABLE_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->IER) &= ~(__INTERRUPT__)) /** @brief Check if the specified CAN interrupt source is enabled or disabled. * @param __HANDLE__ specifies the CAN Handle. * @param __INTERRUPT__ specifies the CAN interrupt source to check. * This parameter can be a value of @arg CAN_Interrupts * @retval The state of __IT__ (TRUE or FALSE). */ #define __HAL_CAN_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->IER) & (__INTERRUPT__)) /** @brief Check whether the specified CAN flag is set or not. * @param __HANDLE__ specifies the CAN Handle. * @param __FLAG__ specifies the flag to check. * This parameter can be one of @arg CAN_flags * @retval The state of __FLAG__ (TRUE or FALSE). */ #define __HAL_CAN_GET_FLAG(__HANDLE__, __FLAG__) \ ((((__FLAG__) >> 8U) == 5U)? ((((__HANDLE__)->Instance->TSR) & (1U << ((__FLAG__) & CAN_FLAG_MASK))) == (1U << ((__FLAG__) & CAN_FLAG_MASK))): \ (((__FLAG__) >> 8U) == 2U)? ((((__HANDLE__)->Instance->RF0R) & (1U << ((__FLAG__) & CAN_FLAG_MASK))) == (1U << ((__FLAG__) & CAN_FLAG_MASK))): \ (((__FLAG__) >> 8U) == 4U)? ((((__HANDLE__)->Instance->RF1R) & (1U << ((__FLAG__) & CAN_FLAG_MASK))) == (1U << ((__FLAG__) & CAN_FLAG_MASK))): \ (((__FLAG__) >> 8U) == 1U)? ((((__HANDLE__)->Instance->MSR) & (1U << ((__FLAG__) & CAN_FLAG_MASK))) == (1U << ((__FLAG__) & CAN_FLAG_MASK))): \ (((__FLAG__) >> 8U) == 3U)? ((((__HANDLE__)->Instance->ESR) & (1U << ((__FLAG__) & CAN_FLAG_MASK))) == (1U << ((__FLAG__) & CAN_FLAG_MASK))): 0U) /** @brief Clear the specified CAN pending flag. * @param __HANDLE__ specifies the CAN Handle. * @param __FLAG__ specifies the flag to check. * This parameter can be one of the following values: * @arg CAN_FLAG_RQCP0: Request complete MailBox 0 Flag * @arg CAN_FLAG_TXOK0: Transmission OK MailBox 0 Flag * @arg CAN_FLAG_ALST0: Arbitration Lost MailBox 0 Flag * @arg CAN_FLAG_TERR0: Transmission error MailBox 0 Flag * @arg CAN_FLAG_RQCP1: Request complete MailBox 1 Flag * @arg CAN_FLAG_TXOK1: Transmission OK MailBox 1 Flag * @arg CAN_FLAG_ALST1: Arbitration Lost MailBox 1 Flag * @arg CAN_FLAG_TERR1: Transmission error MailBox 1 Flag * @arg CAN_FLAG_RQCP2: Request complete MailBox 2 Flag * @arg CAN_FLAG_TXOK2: Transmission OK MailBox 2 Flag * @arg CAN_FLAG_ALST2: Arbitration Lost MailBox 2 Flag * @arg CAN_FLAG_TERR2: Transmission error MailBox 2 Flag * @arg CAN_FLAG_FF0: RX FIFO 0 Full Flag * @arg CAN_FLAG_FOV0: RX FIFO 0 Overrun Flag * @arg CAN_FLAG_FF1: RX FIFO 1 Full Flag * @arg CAN_FLAG_FOV1: RX FIFO 1 Overrun Flag * @arg CAN_FLAG_WKUI: Wake up Interrupt Flag * @arg CAN_FLAG_SLAKI: Sleep acknowledge Interrupt Flag * @retval None */ #define __HAL_CAN_CLEAR_FLAG(__HANDLE__, __FLAG__) \ ((((__FLAG__) >> 8U) == 5U)? (((__HANDLE__)->Instance->TSR) = (1U << ((__FLAG__) & CAN_FLAG_MASK))): \ (((__FLAG__) >> 8U) == 2U)? (((__HANDLE__)->Instance->RF0R) = (1U << ((__FLAG__) & CAN_FLAG_MASK))): \ (((__FLAG__) >> 8U) == 4U)? (((__HANDLE__)->Instance->RF1R) = (1U << ((__FLAG__) & CAN_FLAG_MASK))): \ (((__FLAG__) >> 8U) == 1U)? (((__HANDLE__)->Instance->MSR) = (1U << ((__FLAG__) & CAN_FLAG_MASK))): 0U) /** * @} */ /* Exported functions --------------------------------------------------------*/ /** @addtogroup CAN_Exported_Functions CAN Exported Functions * @{ */ /** @addtogroup CAN_Exported_Functions_Group1 Initialization and de-initialization functions * @brief Initialization and Configuration functions * @{ */ /* Initialization and de-initialization functions *****************************/ HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef *hcan); HAL_StatusTypeDef HAL_CAN_DeInit(CAN_HandleTypeDef *hcan); void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan); void HAL_CAN_MspDeInit(CAN_HandleTypeDef *hcan); /** * @} */ /** @addtogroup CAN_Exported_Functions_Group2 Configuration functions * @brief Configuration functions * @{ */ /* Configuration functions ****************************************************/ HAL_StatusTypeDef HAL_CAN_ConfigFilter(CAN_HandleTypeDef *hcan, CAN_FilterTypeDef *sFilterConfig); /** * @} */ /** @addtogroup CAN_Exported_Functions_Group3 Control functions * @brief Control functions * @{ */ /* Control functions **********************************************************/ HAL_StatusTypeDef HAL_CAN_Start(CAN_HandleTypeDef *hcan); HAL_StatusTypeDef HAL_CAN_Stop(CAN_HandleTypeDef *hcan); HAL_StatusTypeDef HAL_CAN_RequestSleep(CAN_HandleTypeDef *hcan); HAL_StatusTypeDef HAL_CAN_WakeUp(CAN_HandleTypeDef *hcan); uint32_t HAL_CAN_IsSleepActive(CAN_HandleTypeDef *hcan); HAL_StatusTypeDef HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan, CAN_TxHeaderTypeDef *pHeader, uint8_t aData[], uint32_t *pTxMailbox); HAL_StatusTypeDef HAL_CAN_AbortTxRequest(CAN_HandleTypeDef *hcan, uint32_t TxMailboxes); uint32_t HAL_CAN_GetTxMailboxesFreeLevel(CAN_HandleTypeDef *hcan); uint32_t HAL_CAN_IsTxMessagePending(CAN_HandleTypeDef *hcan, uint32_t TxMailboxes); uint32_t HAL_CAN_GetTxTimestamp(CAN_HandleTypeDef *hcan, uint32_t TxMailbox); HAL_StatusTypeDef HAL_CAN_GetRxMessage(CAN_HandleTypeDef *hcan, uint32_t RxFifo, CAN_RxHeaderTypeDef *pHeader, uint8_t aData[]); uint32_t HAL_CAN_GetRxFifoFillLevel(CAN_HandleTypeDef *hcan, uint32_t RxFifo); /** * @} */ /** @addtogroup CAN_Exported_Functions_Group4 Interrupts management * @brief Interrupts management * @{ */ /* Interrupts management ******************************************************/ HAL_StatusTypeDef HAL_CAN_ActivateNotification(CAN_HandleTypeDef *hcan, uint32_t ActiveITs); HAL_StatusTypeDef HAL_CAN_DeactivateNotification(CAN_HandleTypeDef *hcan, uint32_t InactiveITs); void HAL_CAN_IRQHandler(CAN_HandleTypeDef *hcan); /** * @} */ /** @addtogroup CAN_Exported_Functions_Group5 Callback functions * @brief Callback functions * @{ */ /* Callbacks functions ********************************************************/ void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan); void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan); void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan); void HAL_CAN_TxMailbox0AbortCallback(CAN_HandleTypeDef *hcan); void HAL_CAN_TxMailbox1AbortCallback(CAN_HandleTypeDef *hcan); void HAL_CAN_TxMailbox2AbortCallback(CAN_HandleTypeDef *hcan); void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan); void HAL_CAN_RxFifo0FullCallback(CAN_HandleTypeDef *hcan); void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan); void HAL_CAN_RxFifo1FullCallback(CAN_HandleTypeDef *hcan); void HAL_CAN_SleepCallback(CAN_HandleTypeDef *hcan); void HAL_CAN_WakeUpFromRxMsgCallback(CAN_HandleTypeDef *hcan); void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan); /** * @} */ /** @addtogroup CAN_Exported_Functions_Group6 Peripheral State and Error functions * @brief CAN Peripheral State functions * @{ */ /* Peripheral State and Error functions ***************************************/ HAL_CAN_StateTypeDef HAL_CAN_GetState(CAN_HandleTypeDef *hcan); uint32_t HAL_CAN_GetError(CAN_HandleTypeDef *hcan); HAL_StatusTypeDef HAL_CAN_ResetError(CAN_HandleTypeDef *hcan); /** * @} */ /** * @} */ /* Private types -------------------------------------------------------------*/ /** @defgroup CAN_Private_Types CAN Private Types * @{ */ /** * @} */ /* Private variables ---------------------------------------------------------*/ /** @defgroup CAN_Private_Variables CAN Private Variables * @{ */ /** * @} */ /* Private constants ---------------------------------------------------------*/ /** @defgroup CAN_Private_Constants CAN Private Constants * @{ */ #define CAN_FLAG_MASK (0x000000FFU) /** * @} */ /* Private Macros -----------------------------------------------------------*/ /** @defgroup CAN_Private_Macros CAN Private Macros * @{ */ #define IS_CAN_MODE(MODE) (((MODE) == CAN_MODE_NORMAL) || \ ((MODE) == CAN_MODE_LOOPBACK)|| \ ((MODE) == CAN_MODE_SILENT) || \ ((MODE) == CAN_MODE_SILENT_LOOPBACK)) #define IS_CAN_SJW(SJW) (((SJW) == CAN_SJW_1TQ) || ((SJW) == CAN_SJW_2TQ) || \ ((SJW) == CAN_SJW_3TQ) || ((SJW) == CAN_SJW_4TQ)) #define IS_CAN_BS1(BS1) (((BS1) == CAN_BS1_1TQ) || ((BS1) == CAN_BS1_2TQ) || \ ((BS1) == CAN_BS1_3TQ) || ((BS1) == CAN_BS1_4TQ) || \ ((BS1) == CAN_BS1_5TQ) || ((BS1) == CAN_BS1_6TQ) || \ ((BS1) == CAN_BS1_7TQ) || ((BS1) == CAN_BS1_8TQ) || \ ((BS1) == CAN_BS1_9TQ) || ((BS1) == CAN_BS1_10TQ)|| \ ((BS1) == CAN_BS1_11TQ)|| ((BS1) == CAN_BS1_12TQ)|| \ ((BS1) == CAN_BS1_13TQ)|| ((BS1) == CAN_BS1_14TQ)|| \ ((BS1) == CAN_BS1_15TQ)|| ((BS1) == CAN_BS1_16TQ)) #define IS_CAN_BS2(BS2) (((BS2) == CAN_BS2_1TQ) || ((BS2) == CAN_BS2_2TQ) || \ ((BS2) == CAN_BS2_3TQ) || ((BS2) == CAN_BS2_4TQ) || \ ((BS2) == CAN_BS2_5TQ) || ((BS2) == CAN_BS2_6TQ) || \ ((BS2) == CAN_BS2_7TQ) || ((BS2) == CAN_BS2_8TQ)) #define IS_CAN_PRESCALER(PRESCALER) (((PRESCALER) >= 1U) && ((PRESCALER) <= 1024U)) #define IS_CAN_FILTER_ID_HALFWORD(HALFWORD) ((HALFWORD) <= 0xFFFFU) #if defined(CAN2) #define IS_CAN_FILTER_BANK_DUAL(BANK) ((BANK) <= 27U) #endif #define IS_CAN_FILTER_BANK_SINGLE(BANK) ((BANK) <= 13U) #define IS_CAN_FILTER_MODE(MODE) (((MODE) == CAN_FILTERMODE_IDMASK) || \ ((MODE) == CAN_FILTERMODE_IDLIST)) #define IS_CAN_FILTER_SCALE(SCALE) (((SCALE) == CAN_FILTERSCALE_16BIT) || \ ((SCALE) == CAN_FILTERSCALE_32BIT)) #define IS_CAN_FILTER_ACTIVATION(ACTIVATION) (((ACTIVATION) == CAN_FILTER_DISABLE) || \ ((ACTIVATION) == CAN_FILTER_ENABLE)) #define IS_CAN_FILTER_FIFO(FIFO) (((FIFO) == CAN_FILTER_FIFO0) || \ ((FIFO) == CAN_FILTER_FIFO1)) #define IS_CAN_TX_MAILBOX(TRANSMITMAILBOX) (((TRANSMITMAILBOX) == CAN_TX_MAILBOX0 ) || \ ((TRANSMITMAILBOX) == CAN_TX_MAILBOX1 ) || \ ((TRANSMITMAILBOX) == CAN_TX_MAILBOX2 )) #define IS_CAN_TX_MAILBOX_LIST(TRANSMITMAILBOX) ((TRANSMITMAILBOX) <= (CAN_TX_MAILBOX0 | CAN_TX_MAILBOX1 | CAN_TX_MAILBOX2)) #define IS_CAN_STDID(STDID) ((STDID) <= 0x7FFU) #define IS_CAN_EXTID(EXTID) ((EXTID) <= 0x1FFFFFFFU) #define IS_CAN_DLC(DLC) ((DLC) <= 8U) #define IS_CAN_IDTYPE(IDTYPE) (((IDTYPE) == CAN_ID_STD) || \ ((IDTYPE) == CAN_ID_EXT)) #define IS_CAN_RTR(RTR) (((RTR) == CAN_RTR_DATA) || ((RTR) == CAN_RTR_REMOTE)) #define IS_CAN_RX_FIFO(FIFO) (((FIFO) == CAN_RX_FIFO0) || ((FIFO) == CAN_RX_FIFO1)) #define IS_CAN_IT(IT) ((IT) <= (CAN_IT_TX_MAILBOX_EMPTY | CAN_IT_RX_FIFO0_MSG_PENDING | \ CAN_IT_RX_FIFO0_FULL | CAN_IT_RX_FIFO0_OVERRUN | \ CAN_IT_RX_FIFO1_MSG_PENDING | CAN_IT_RX_FIFO1_FULL | \ CAN_IT_RX_FIFO1_OVERRUN | CAN_IT_WAKEUP | \ CAN_IT_SLEEP_ACK | CAN_IT_ERROR_WARNING | \ CAN_IT_ERROR_PASSIVE | CAN_IT_BUSOFF | \ CAN_IT_LAST_ERROR_CODE | CAN_IT_ERROR)) /** * @} */ /* End of private macros -----------------------------------------------------*/ /** * @} */ #endif /* CAN1 */ /** * @} */ #ifdef __cplusplus } #endif #endif /* STM32F1xx_HAL_CAN_H */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 求大佬指点哪里错了。。
c语言三维数组做函数输入变量,函数的形参怎么写?
比如uint8_t cube[5][5][5];要在函数内处理数组内的数据并能返回值,肯定是传入三维数组的地址。函数定义时形参不知道怎么写才行??void process(uint8_t ????);
stm32延时函数可行和不可行
# 1. 可行的延时 #MeLED.c ``` include "MELED.h" void LED_TIM_Config(void) { TIM_TimeBaseInitTypeDef LED_TIM_Struct; TIM_OCInitTypeDef TIM_OCInitConfig; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); TIM_DeInit(TIM4); //GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE); TIM_OCInitConfig.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitConfig.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitConfig.TIM_OCPolarity = TIM_OCPolarity_Low; LED_TIM_Struct.TIM_Prescaler = (36000 - 1); LED_TIM_Struct.TIM_Period = 2 - 1; LED_TIM_Struct.TIM_ClockDivision = TIM_CKD_DIV1; LED_TIM_Struct.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &LED_TIM_Struct); TIM_ClearFlag(TIM4, TIM_FLAG_Update); TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); TIM_OC1PreloadConfig(TIM4, ENABLE); TIM_Cmd(TIM4, ENABLE); } ``` ``` void LED_NVIC_Config(void) { NVIC_InitTypeDef LED_NVIC_Struct; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); LED_NVIC_Struct.NVIC_IRQChannel = TIM4_IRQn; LED_NVIC_Struct.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级 LED_NVIC_Struct.NVIC_IRQChannelSubPriority = 0;//响应优先级 LED_NVIC_Struct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&LED_NVIC_Struct); } ``` int i = 0;//注意该变量是定义在MeLED.c文件中的 ``` void TIM4_IRQHandler(void) { if(TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_Update); i++; } } ``` ``` void My_Systick_Delay(uint32_t nTime) { i = 0; while(nTime != i); } ``` #MeLED.h ``` #ifndef __MELED_H #define __MELED_H #include "stm32f10x.h" #include "led.h" #include "Oper_System_Delay.h" void MELED_Init(void); void MELED_PWM_Init(void); void LED_TIM_Config(void); void LED_NVIC_Config(void); void TIM4_IRQHandler(void); void My_Systick_Delay(uint32_t nTime); #endif ``` #main.c ``` #include "stm32f10x.h" #include "usart.h" #include "led.h" #include "RTC_Time.h" #include <stdio.h> #include "MeLED.h" #include "Delay.h" ``` ``` int main(void) { while(1) { My_Systick_Delay(1000); printf("Delay over\n"); } } ``` #2.不可行的延时(我想知道为什么不可行?) #Delay.c ``` #include "Delay.h" void My_Systick_Delay(uint32_t nTime) { Attain_delay = 0; while(nTime != Attain_delay);//程序会不停的在这里循环 } ``` #Delay.h ``` #ifndef __Delay_H #define __Delay_H #include "stm32f10x.h" #include "stm32f10x_tim.h" #include "stm32f10x_it.h" #include "core_cm3.h" #include "Oper_System_Delay.h" #endif ``` #Oper_System_Delay.h ``` #ifndef __Oper_System_Delay_H #define __Oper_System_Delay_H #include "stm32f10x.h" #include "stm32f10x_tim.h" #include "stm32f10x_it.h" static uint32_t Attain_delay; //void Oper_System_Delay(); #endif ``` #MeLED.c ``` void TIM4_IRQHandler(void) { if(TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_Update); Attain_delay++; } } ``` 所以第2点有人知道为什么不可行吗?最好可以提供第1和第2的差别在哪?
FFmpeg - 为什么从pcm提取音频,编码为mp3以后,时长变多了,声音也颤抖
``` #include <memory> extern "C" { #include "libavcodec/avcodec.h" #include "libavformat/avformat.h" #include "libavutil/imgutils.h" #include "libswresample/swresample.h" #include "libavutil/opt.h" #include "libavutil/avassert.h" #include "libavutil/audio_fifo.h" } // 函数退出后执行 #define DEFER(T, P, Fn) std::shared_ptr<T> P##deletor(P, [&](T *) {Fn;}) // 打印异常信息,并退出main函数 #define FATAL(M, ...) printf(M, ##__VA_ARGS__); return -1 // 自定义变量 const char *src_media = "E:/2.pcm"; const char *dst_media = "E:/3.mp3"; // PCM的原始参数 #define PCM_IN_FORMAT AV_SAMPLE_FMT_S16 #define PCM_IN_CHANNEL_LAYOUT AV_CH_LAYOUT_MONO #define PCM_IN_NB_SAMPLES 1024 #define PCM_IN_SAMPLE_RATE 44100 int main(int argc, char **argv) { // 申请一个输出的上下文 AVFormatContext *fmt_ctx = nullptr; avformat_alloc_output_context2(&fmt_ctx, nullptr, nullptr, dst_media); if (fmt_ctx == nullptr) { FATAL("alloc output format context failed."); } DEFER(AVFormatContext, fmt_ctx, avio_closep(&fmt_ctx->pb); avformat_close_input(&fmt_ctx)); // 查询编码器 AVCodec *audio_enc; if ((audio_enc = avcodec_find_encoder(fmt_ctx->oformat->audio_codec)) == nullptr) { FATAL("find audio encoder failed."); } AVStream *audio_stream = avformat_new_stream(fmt_ctx, audio_enc); audio_stream->id = fmt_ctx->nb_streams - 1; // 为编码器申请上下文 AVCodecContext *audio_enc_ctx = nullptr; if ((audio_enc_ctx = avcodec_alloc_context3(audio_enc)) == nullptr) { FATAL("allocate audio enc context failed."); } DEFER(AVCodecContext, audio_enc_ctx, avcodec_free_context(&audio_enc_ctx)); // 为编码器配置编码参数 audio_enc_ctx->sample_fmt = audio_enc->sample_fmts ? audio_enc->sample_fmts[0] : AV_SAMPLE_FMT_FLTP; audio_enc_ctx->bit_rate = 128000; audio_enc_ctx->sample_rate = 44100; audio_enc_ctx->channel_layout = AV_CH_LAYOUT_STEREO; audio_enc_ctx->channels = av_get_channel_layout_nb_channels(audio_enc_ctx->channel_layout); if (fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) { audio_enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; } audio_stream->time_base = AVRational{1, audio_enc_ctx->sample_rate}; // 打开编码器 if (avcodec_open2(audio_enc_ctx, audio_enc, nullptr) < 0) { FATAL("codec open failed."); } if (avcodec_parameters_from_context(audio_stream->codecpar, audio_enc_ctx) < 0) { FATAL("copy params failed."); } if (avio_open(&fmt_ctx->pb, dst_media, AVIO_FLAG_WRITE) < 0) { FATAL("open dst file failed."); } // 写入头信息 if (avformat_write_header(fmt_ctx, nullptr) < 0) { FATAL("write header failed."); } // 申请一个音频frame AVFrame *audio_frame = nullptr; if ((audio_frame = av_frame_alloc()) == nullptr) { FATAL("allocate frame failed."); } DEFER(AVFrame, audio_frame, av_frame_free(&audio_frame)); audio_frame->format = audio_enc_ctx->sample_fmt; audio_frame->channel_layout = audio_enc_ctx->channel_layout; audio_frame->nb_samples = audio_enc_ctx->frame_size; audio_frame->sample_rate = audio_enc_ctx->sample_rate; // 给frame的data和size分配空间 if (av_frame_get_buffer(audio_frame, 0) < 0) { FATAL("allocate frame data failed."); } // 创建swr的上下文 SwrContext *swr_cxt = swr_alloc(); DEFER(SwrContext, swr_cxt, swr_free(&swr_cxt)); // 获取输入与输出的采样格式 enum AVSampleFormat in_sample_fmt = PCM_IN_FORMAT; enum AVSampleFormat out_sample_fmt = audio_enc_ctx->sample_fmt; // 获取输入与输出的采样率 int in_sample_rate = PCM_IN_SAMPLE_RATE; int out_sample_rate = audio_enc_ctx->sample_rate; // 获取输入与输出的声道格式 uint64_t in_ch_layout = PCM_IN_CHANNEL_LAYOUT; uint64_t out_ch_layout = audio_enc_ctx->channel_layout; // 设置swr的配置项 swr_alloc_set_opts(swr_cxt, out_ch_layout, out_sample_fmt, out_sample_rate, in_ch_layout, in_sample_fmt, in_sample_rate, 0, nullptr); // 初始化swr swr_init(swr_cxt); // 申请一个packet,并初始化 AVPacket pkt; av_init_packet(&pkt); pkt.data = nullptr; pkt.size = 0; FILE *input = nullptr; if ((input = fopen(src_media, "rb")) == nullptr) { FATAL("no readable file."); } DEFER(FILE, input, fclose(input)); // 从文件中读取出来的数据,临时存储在该帧中 AVFrame *temp_frame = av_frame_alloc(); DEFER(AVFrame, temp_frame, av_frame_free(&temp_frame)); temp_frame->format = PCM_IN_FORMAT; temp_frame->nb_samples = PCM_IN_NB_SAMPLES; temp_frame->channel_layout = PCM_IN_CHANNEL_LAYOUT; temp_frame->channels = av_get_channel_layout_nb_channels(PCM_IN_CHANNEL_LAYOUT); // 给frame的data和size分配空间 if (av_frame_get_buffer(temp_frame, 0) < 0) { FATAL("allocate frame data failed."); } // 循环读取frame数据 int frame_count = 0; while (true) { if (av_frame_make_writable(audio_frame) < 0) { FATAL("frame is not writable"); } if (fread(temp_frame->data[0], 1, (size_t) temp_frame->linesize[0], input) < 0) { FATAL("read input file failed."); } else if (feof(input)) { break; } swr_convert(swr_cxt, audio_frame->data, audio_frame->nb_samples, (const uint8_t **) temp_frame->data, temp_frame->nb_samples); audio_frame->pts = av_rescale_q(frame_count, (AVRational){1, audio_enc_ctx->sample_rate}, audio_enc_ctx->time_base); frame_count += audio_frame->nb_samples; // 发送一个frame if (avcodec_send_frame(audio_enc_ctx, audio_frame) < 0) { FATAL("send frame exception."); } // 接受编码完的内容 while (true) { auto packet_ret = avcodec_receive_packet(audio_enc_ctx, &pkt); // 判断是否完全接受了packet if (packet_ret == AVERROR(EAGAIN) || packet_ret == AVERROR_EOF) { break; } // 检查是否接受异常 if (packet_ret < 0) { FATAL("receive packet exception."); } av_packet_rescale_ts(&pkt, audio_enc_ctx->time_base, audio_stream->time_base); pkt.stream_index = audio_stream->index; av_interleaved_write_frame(fmt_ctx, &pkt); av_packet_unref(&pkt); } } // 发送一个空的frame,告知编码器,已经编码完成,清空缓冲区 if (avcodec_send_frame(audio_enc_ctx, nullptr) < 0) { FATAL("send frame exception."); } // 接受编码完的内容 while (true) { auto packet_ret = avcodec_receive_packet(audio_enc_ctx, &pkt); // 判断是否完全接受了packet if (packet_ret == AVERROR(EAGAIN) || packet_ret == AVERROR_EOF) { break; } // 检查是否接受异常 if (packet_ret < 0) { FATAL("receive packet exception."); } av_packet_rescale_ts(&pkt, audio_enc_ctx->time_base, audio_stream->time_base); pkt.stream_index = audio_stream->index; av_interleaved_write_frame(fmt_ctx, &pkt); av_packet_unref(&pkt); } av_write_trailer(fmt_ctx); return 0; } ``` ### 问题已经解决,请看下面的代码 ```cpp #include <memory> extern "C" { #include "libavcodec/avcodec.h" #include "libavformat/avformat.h" #include "libswresample/swresample.h" } // 申请智能指针变量 #define NEW_PTR(T, P, V, Fn) T *P = V; std::shared_ptr<T> P##P(P, [&P](T *){if(P != nullptr){Fn;}}) // 打印异常信息,并退出main函数 #define FATAL(M, ...) printf(M, ##__VA_ARGS__); return -1 // 自定义变量 const char *src_media = "D:/2.pcm"; const char *dst_media = "D:/2.mp3"; // PCM的原始参数 #define PCM_IN_FORMAT AV_SAMPLE_FMT_S16 #define PCM_IN_CHANNELS 1 #define PCM_IN_SAMPLE_RATE 44100 int main(int argc, char **argv) { // 申请一个输出的上下文 NEW_PTR(AVFormatContext, fmt_ctx, nullptr, avio_closep(&fmt_ctx->pb); avformat_close_input(&fmt_ctx)); avformat_alloc_output_context2(&fmt_ctx, nullptr, nullptr, dst_media); if (fmt_ctx == nullptr) { FATAL("alloc output format context failed."); } // 查询编码器 AVCodec *audio_enc; if ((audio_enc = avcodec_find_encoder(fmt_ctx->oformat->audio_codec)) == nullptr) { FATAL("find audio encoder failed."); } // 为编码器申请上下文 NEW_PTR(AVCodecContext, audio_enc_ctx, nullptr, avcodec_free_context(&audio_enc_ctx)); if ((audio_enc_ctx = avcodec_alloc_context3(audio_enc)) == nullptr) { FATAL("allocate audio enc context failed."); } // 为编码器配置编码参数 audio_enc_ctx->sample_fmt = audio_enc->sample_fmts ? audio_enc->sample_fmts[0] : AV_SAMPLE_FMT_FLTP; audio_enc_ctx->bit_rate = 64000; audio_enc_ctx->sample_rate = 44100; audio_enc_ctx->channel_layout = AV_CH_LAYOUT_STEREO; audio_enc_ctx->channels = av_get_channel_layout_nb_channels(audio_enc_ctx->channel_layout); if (fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) { audio_enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; } // 打开编码器 if (avcodec_open2(audio_enc_ctx, audio_enc, nullptr) < 0) { FATAL("codec open failed."); } AVStream *audio_stream = avformat_new_stream(fmt_ctx, audio_enc); audio_stream->id = fmt_ctx->nb_streams - 1; audio_stream->time_base = AVRational{1, audio_enc_ctx->sample_rate}; if (avcodec_parameters_from_context(audio_stream->codecpar, audio_enc_ctx) < 0) { FATAL("copy params failed."); } if (avio_open(&fmt_ctx->pb, dst_media, AVIO_FLAG_WRITE) < 0) { FATAL("open dst file failed."); } // 写入头信息 if (avformat_write_header(fmt_ctx, nullptr) < 0) { FATAL("write header failed."); } // 申请一个音频frame NEW_PTR(AVFrame, audio_frame, nullptr, av_frame_free(&audio_frame)); if ((audio_frame = av_frame_alloc()) == nullptr) { FATAL("allocate frame failed."); } audio_frame->format = audio_enc_ctx->sample_fmt; audio_frame->channel_layout = audio_enc_ctx->channel_layout; audio_frame->nb_samples = audio_enc_ctx->frame_size; audio_frame->sample_rate = audio_enc_ctx->sample_rate; if (av_frame_get_buffer(audio_frame, 0) < 0) { FATAL("audio frame get buffer failed."); } // 创建一个frame,用来存储从pcm读取的数据 NEW_PTR(AVFrame, buf_frame, nullptr, av_frame_free(&buf_frame)); if ((buf_frame = av_frame_alloc()) == nullptr) { FATAL("allocate buf frame failed."); } buf_frame->format = PCM_IN_FORMAT; buf_frame->nb_samples = audio_frame->nb_samples; buf_frame->channel_layout = (uint64_t) av_get_default_channel_layout(PCM_IN_CHANNELS); buf_frame->sample_rate = PCM_IN_SAMPLE_RATE; if (av_frame_get_buffer(buf_frame, 0) < 0) { FATAL("create buf frame buffer failed."); } // 从pcm文件中读取适应音频帧的尺寸数据 auto readSize = av_samples_get_buffer_size(nullptr, buf_frame->channels, buf_frame->nb_samples, (AVSampleFormat) buf_frame->format, 1); NEW_PTR(uint8_t , buf, (uint8_t*)av_malloc((size_t)readSize), av_freep(&buf)); // 创建swr的上下文 NEW_PTR(SwrContext, swr_ctx, swr_alloc(), swr_free(&swr_ctx)); swr_alloc_set_opts(swr_ctx, audio_frame->channel_layout, (AVSampleFormat)audio_frame->format, audio_frame->sample_rate, av_get_default_channel_layout(PCM_IN_CHANNELS), PCM_IN_FORMAT, PCM_IN_SAMPLE_RATE, 0, nullptr); swr_init(swr_ctx); // 申请一个packet,并初始化 AVPacket pkt; av_init_packet(&pkt); pkt.data = nullptr; pkt.size = 0; NEW_PTR(FILE, input, nullptr, fclose(input)); if ((input = fopen(src_media, "rb")) == nullptr) { FATAL("no readable file."); } // 循环读取frame数据 int audio_pts = 0; while (true) { // 用来编码的帧 AVFrame * encode_frame = nullptr; if (fread(buf, 1, (size_t) readSize, input) < 0) { FATAL("read input file failed."); } else if (!feof(input)) { // 文件没有到结尾,则获取编码帧 av_samples_fill_arrays(buf_frame->data, buf_frame->linesize, (const uint8_t*)buf, buf_frame->channels, buf_frame->nb_samples, (AVSampleFormat)buf_frame->format, 1); swr_convert(swr_ctx, audio_frame->data, audio_frame->nb_samples, (const uint8_t**)buf_frame->data, buf_frame->nb_samples); audio_frame->pts = audio_pts; audio_pts += av_rescale_q(audio_frame->nb_samples, AVRational{1, audio_enc_ctx->frame_size}, audio_enc_ctx->time_base); encode_frame = audio_frame; } else { // 文件结束了,则发送一个空指针的frame,用来清空缓冲区 encode_frame = nullptr; } // 发送一个frame if (avcodec_send_frame(audio_enc_ctx, encode_frame) < 0) { FATAL("send frame exception."); } // 接受编码完的内容 while (true) { auto packet_ret = avcodec_receive_packet(audio_enc_ctx, &pkt); // 判断是否完全接受了packet if (packet_ret == AVERROR(EAGAIN) || packet_ret == AVERROR_EOF) { break; } // 检查是否接受异常 if (packet_ret < 0) { FATAL("receive packet exception."); } av_packet_rescale_ts(&pkt, audio_enc_ctx->time_base, audio_stream->time_base); pkt.stream_index = audio_stream->index; av_interleaved_write_frame(fmt_ctx, &pkt); av_packet_unref(&pkt); } // 编码帧为空,则表示已经处理完所有的编码,退出该循环 if (encode_frame == nullptr) break; } av_write_trailer(fmt_ctx); return 0; } ```
俄罗斯方块C语言 12列改为10列后出错,新人求助!
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <conio.h> #include <windows.h> #ifdef _MSC_VER // M$的编译器要给予特殊照顾 #if _MSC_VER <= 1200 // VC6及以下版本 #error 你是不是还在用VC6?! #else // VC6以上版本 #if _MSC_VER >= 1600 // 据说VC10及以上版本有stdint.h了 #include <stdint.h> #else // VC10以下版本,自己定义int8_t和uint16_t typedef signed char int8_t; typedef unsigned short uint16_t; #endif #ifndef __cplusplus // 据说VC都没有stdbool.h,不用C++编译,自己定义bool typedef int bool; #define true 1 #define false 0 #endif #endif #else // 其他的编译器都好说 #include <stdint.h> #ifndef __cplusplus // 不用C++编译,需要stdbool.h里的bool #include <stdbool.h> #endif #endif // ============================================================================= // 7种方块的4旋转状态(4位为一行) static const uint16_t gs_uTetrisTable[7][4] = { { 0x00F0U, 0x2222U, 0x00F0U, 0x2222U }, // I型 { 0x0072U, 0x0262U, 0x0270U, 0x0232U }, // T型 { 0x0223U, 0x0074U, 0x0622U, 0x0170U }, // L型 { 0x0226U, 0x0470U, 0x0322U, 0x0071U }, // J型 { 0x0063U, 0x0264U, 0x0063U, 0x0264U }, // Z型 { 0x006CU, 0x0462U, 0x006CU, 0x0462U }, // S型 { 0x0660U, 0x0660U, 0x0660U, 0x0660U } // O型 }; // ============================================================================= // 初始状态的游戏池 // 每个元素表示游戏池的一行,下标大的是游戏池底部 // 两端各置2个1,底部2全置为1,便于进行碰撞检测 // 这样一来游戏池的宽度为12列 // 如果想要传统的10列,只需多填两个1即可(0xE007),当然显示相关部分也要随之改动 // 当某个元素为0xFFFFU时,说明该行已被填满 // 顶部4行用于给方块,不显示出来 // 再除去底部2行,显示出来的游戏池高度为22行 static const uint16_t gs_uInitialTetrisPool[28] = { 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xFFFFU, 0xFFFFU }; #define COL_BEGIN 2 #define COL_END 14 #define ROW_BEGIN 4 #define ROW_END 26
用串行口接收W25Q16的ID值为什么不行了 接收的永远是我的初始值
#include"reg511.h" typedef unsigned int uint16; //对数据类型进行声明定义 typedef unsigned char uint8; sbit cs=P1^3; sbit msio=P0^0; sbit clk=P1^4; sbit mosi=P0^1; sbit led=P1^5; uint8 we_d; uint8 Temp1=0X12; void delay_ms(uint16 x) { uint16 j,i; for(j=0;j<x;j++) { for(i=0;i<1100;i++); } } void SPI_WriteByte(uint8 date) { uint8 temp,i; temp = date; for (i = 0; i < 8; i++) { clk=0; if((temp&0x80)==0x80) { mosi=1; } else { mosi=0; } clk=1 ; temp <<= 1; } mosi=0; } uint8 SPI_ReadByte(void) { uint8 temp=0; uint8 i; for(i = 0; i < 8; i++) { temp <<= 1; clk=0 ; if(msio) {temp++; } clk=1 ; } return(temp); } uint16 W25QXX_ReadID(void) { uint16 Temp = 0; uint8 Temp1 = 0; uint8 Temp2 = 0; cs=0; SPI_WriteByte(0x90); //发送读取ID命令 SPI_WriteByte(0x00); SPI_WriteByte(0x00); SPI_WriteByte(0x00); Temp1|=SPI_ReadByte(); Temp2|=SPI_ReadByte(); Temp = Temp1*256+Temp2; cs=1; return Temp; } void UartInit(void) //9600bps@33.1776MHz { PCON &= 0x7F; //波特率不倍速 SCON = 0x50; //8位数据,可变波特率 AUXR |= 0x40; //定时器1时钟为Fosc,即1T AUXR &= 0xFE; //串口1选择定时器1为波特率发生器 TMOD &= 0x0F; //清除定时器1模式位 TMOD |= 0x20; //设定定时器1为8位自动重装方式 TL1 = 0x94; //设定定时初值 TH1 = 0x94; //设定定时器重装值 ET1 = 0; //禁止定时器1中断 TR1 = 1; //启动定时器1 ES=1; //打开接收中断 EA=1; //打开总中断 } void main(void) { uint16 Temp; uint8 Temp2; P1M1=0X00;P1M0=0X00; P0M1 &= 0xE1; P0M0 &= 0xE1; cs=1; //设置P1为推挽输出 UartInit(); // 串口初始化 delay_ms(600); Temp=W25QXX_ReadID(); Temp1=(uint8)(Temp/256); Temp2=(uint8)Temp; } void Usart() interrupt 4 { uint8 receiveData; receiveData=SBUF;//出去接收到的数据 RI = 0;//清除接收中断标志位 SBUF=Temp1;//将接收到的数据放入到发送寄存器 while(!TI); //等待发送数据完成 TI=0; //清除发送完成标志位 }
请问STM32L0K6芯片的SPI的收发数据的问题
问题描述:配置好SPI后,然后通过SPI给AS3956芯片收发数据,第一次正常收发成功。 但后续的收发,则接收数据均为0X00(第一次调试,图见后面)。 复位后,继续调试,接收的数据均为0xFF(第二次调试,图见后面)。 谁知道这个问题的解决方案吗? 下面是我的代码: ``` /* 宏定义 */ #define CS_LOW() { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);} #define CS_HIGH() { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);} /* 收发数据 */ CS_LOW(); if(HAL_OK==HAL_SPI_TransmitReceive(&hspi1,&pTxData1,&pRxData2,1, 10)) { if(HAL_OK!=HAL_SPI_TransmitReceive(&hspi1,(uint8_t *)0xff,&pRxData1,1, 10)) { return -1; } } CS_HIGH(); CS_LOW(); if(HAL_OK!=HAL_SPI_TransmitReceive(&hspi1,&pTxData2,&pRxData2,1, 10)) { return -1; } CS_HIGH(); CS_LOW(); if(HAL_OK!=HAL_SPI_TransmitReceive(&hspi1,&pTxData3,&pRxData2,1, 10)) { return -1; } CS_HIGH(); /* SPI1 parameter configuration*/ hspi1.Instance = SPI1; hspi1.Init.Mode = SPI_MODE_MASTER; hspi1.Init.Direction = SPI_DIRECTION_2LINES; hspi1.Init.DataSize = SPI_DATASIZE_8BIT; hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; hspi1.Init.NSS = SPI_NSS_SOFT; hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi1.Init.TIMode = SPI_TIMODE_DISABLE; hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; hspi1.Init.CRCPolynomial = 7; if (HAL_SPI_Init(&hspi1) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } ``` ![第一次调试](https://img-ask.csdn.net/upload/201905/14/1557804076_56872.png) 第一次调试图 ![第二次调试](https://img-ask.csdn.net/upload/201905/14/1557804107_804381.png) 第二次调试图(复位后)
zigbee 用serialApp串口透传例子作为模板加温湿度测量,能帮忙解决问题的可有偿,先谢谢大家!
温湿度显示不出来,完整的工程代码如下, # 部分关键代码如下,完整工程文件在下面的链接 自定义的函数! static void SampleApp_Send_P2P_Message(void) { char temp[3], humidity[3], strTemp[7]; DHT11(); //获取温湿度 //将数据整合后方便发给协调器显示 osal_memcpy(strTemp, temp, 2); osal_memcpy(&strTemp[2], " ", 1); osal_memcpy(&strTemp[3], humidity, 3); //获得的温湿度通过串口输出到电脑显示 HalUARTWrite(0, "T&H:", 4); HalUARTWrite(0, (uint8 *)strTemp, 5); HalUARTWrite(0, "\n",1); afAddrType_t SerialApp_TxAddr; SerialApp_TxAddr.addrMode = (afAddrMode_t)Addr16Bit; SerialApp_TxAddr.endPoint = SERIALAPP_ENDPOINT; SerialApp_TxAddr.addr.shortAddr = 0x0000; if (afStatus_SUCCESS != AF_DataRequest(&SerialApp_TxAddr, (endPointDesc_t *)&SerialApp_epDesc, SAMPLEAPP_P2P_CLUSTERID, 5, (uint8 *)strTemp, &SerialApp_MsgID, 0, AF_DEFAULT_RADIUS)) { osal_set_event(SerialApp_TaskID, SampleApp_Send_P2P_EVT); } } ```在接收函数中自己添加了一个簇ID处理这个消息 void SerialApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt ) { case SAMPLEAPP_P2P_CLUSTERID: HalUARTWrite(SERIAL_APP_PORT, "T&H:", 4); //提示接收到数据 HalUARTWrite(SERIAL_APP_PORT, pkt->cmd.Data, pkt->cmd.DataLength); //输出接收到的数据 HalUARTWrite(SERIAL_APP_PORT, "\n", 1); // 回车换行 } ## 在事件处理函数中 自己添加了一个事件 在事件处理函数中 自己添加了一个事件 if ( events & SampleApp_Send_P2P_EVT ) { SampleApp_Send_P2P_Message(); //osal_start_timerEx( SerialApp_TaskID, SampleApp_Send_P2P_EVT, 1000 ); return ( events ^ SampleApp_Send_P2P_EVT ); } 在这个例子模板中都是点播发送! 哪位前辈能帮忙指导一下,先谢过! ```
实现登录界面,自定义用户名和密码
实现一个登录界面,自定义用户名和密码,然后登录时输入用户名和密码,成功就登录进去,显示主窗口,失败则清空用户输入的用户名和密码,退出就直接退出。 但现在不论输入什么都会清空用户输入 INT_PTR CALLBACK Dialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: CreateWindow(_T("button"), _T("确认"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 0, 70, 100, 30, hDlg, (HMENU)5001, hInst, NULL); CreateWindow(_T("button"), _T("退出"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 100, 70, 100, 30, hDlg, (HMENU)5002, hInst, NULL); CreateWindow(_T("edit"), _T("请输入用户:"), WS_CHILD | WS_VISIBLE, 0, 0, 1024, 20, hDlg, (HMENU)6001, hInst, NULL); CreateWindow(_T("edit"), _T("请输入密码:"), WS_CHILD | WS_VISIBLE, 0, 30, 1024, 20, hDlg, (HMENU)6002, hInst, NULL); return (INT_PTR)TRUE; case WM_COMMAND://命令消息 switch (LOWORD(wParam)) { case 5002: EndDialog(hDlg, 0); break; case 5001: { char str1[20] = {}; char str2[20] = {}; GetDlgItemTextA(hDlg, 6001, str1, 1024); GetDlgItemTextA(hDlg, 6002, str2, 1024); if (strcmp("1234",str1)==0 && strcmp("4321",str2)==0) { EndDialog(hDlg,0); } else { SetDlgItemText(hDlg, 6001, _T("请输入用户:")); SetDlgItemText(hDlg, 6002, _T("请输入密码:")); } } break; } if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; }
在单片机中 设置pwm时会用到定时器,定时器的作用是什么了 详细一些
#define MAIN_Fosc 11059200L //定义主时钟 #include "15W4KxxS4.H" #define uint16 unsigned int #define uint8 unsigned char /************************************** 功能描述:延时函数 入口参数:uint16 x ,该值为1时,延时1ms 返回值:无 ***************************************/ void delay_ms(uint16 x) { uint16 j,i; for(j=0;j<x;j++) { for(i=0;i<1100;i++); } } /************************************************************************************** 功能描述:对PWM3和PWM3进行初始化 入口参数:无 返回值:无 **************************************************************************************/ void PWM2PWM3_Configuration(void) { PWMCFG &= 0xBF; //将CBTADC位置0,即PWM计数器归零时不触发ADC转换 PWMIF &= 0xBF; //将CBIF位置0,PWM计数器归零中断标志位,需软件清零 P_SW2 |= 0x80; //将EAXSFR位置1,以访问PWM在扩展RAM区的特殊功能寄存器 //对PWM2的初始化部分 PWM2CR |= 0x08; //将PWM2_PS位置1,选择PWM2的输出引脚是P2.7 PWMCR |= 0x01; //将ENC2O位置1,PWM2的端口为PWM输出口,受PWM波形发生器控制 PWMCFG &= 0xFE; //将C2INI位置0,设置PWM2输出端口的初始电平为低电平 PWMIF &= 0xFE; //将C2IF位置0,PWM2中断标志位,需软件清零 PWM2CR |= 0x04; //将EPWM2I位置1,使能PWM2中断 PWM2CR &= 0xFD; //将EC2T2SI位置0,关闭T2翻转时中断 PWM2CR &= 0xFE; //将EC2T1SI位置0,关闭T1翻转时中断 //对PWM3的初始化部分 PWM3CR |= 0x08; //将PWM3_PS位置1,选择PWM3的输出引脚是P4.5 PWMCR |= 0x02; //将ENC3O位置1,PWM3的端口为PWM输出口,受PWM波形发生器控制 PWMCFG &= 0xFD; //将C3INI位置0,设置PWM3输出端口的初始电平为低电平 PWMIF &= 0xFD; //将C3IF位置0,PWM3中断标志位,需软件清零 PWM3CR |= 0x04; //将EPWM3I位置1,使能PWM3中断 PWM3CR &= 0xFD; //将EC3T2SI位置0,关闭T2翻转时中断 PWM3CR &= 0xFE; //将EC3T1SI位置0,关闭T1翻转时中断 //对PWM2和PWM3翻转计数器赋初值 PWM2T1 =1; //赋值PWM2第一次翻转计数器值 PWM2T2 = 0x00FA; //赋值PWM2第二次翻转计数器值 PWM3T1 =1; //赋值PWM3第一次翻转计数器值 PWM3T2 = 0x00FA; //赋值PWM3第二次翻转计数器值 //对PWM波形发生器时钟源进行初始化 PWMCKS |= 0x10; //将SELT2位置1,PWM时钟源为定时器2溢出脉冲 PWMC = 0x00FA; //PWM计数器赋值(同时对PWMCH和PWMCL进行了赋值) AUXR |= 0x04; //定时器2时钟为Fosc,即1T T2L = 0xE0; //设定定时初值 T2H = 0xFE; //设定定时初值 AUXR |= 0x10; //启动定时器2 P_SW2 &= 0x7F; //将EAXSFR位置0,恢复访问XRAM //PWM外部异常控制寄存器的操作 PWMFDCR &= 0xDF; //将ENFD位置0,关闭PWM外部异常检测功能 PWMFDCR &= 0xF7; //将ENDI位置0,关闭PWM异常检测中断 PWMFDCR &= 0xFB; //将FDCMP位置0,比较器与PWM无关 PWMFDCR &= 0xFD; //将FDIO位置0,P2.4的状态与PWM无关 PWMFDCR &= 0xFE; //将FDIF位置0,PWM异常检测中断标志位,需软件清零 IP2 |= 0x40; //将PPWM位置1,使能PWM中断为最高优先级中断 //使能PWM波形发生器 PWMCR |= 0x80; //将ENPWM位置1,使能PWM波形发生器,PWM计数器开始计数 PWMCR &= 0xBF; //将ECBI位置0,禁止PWM计数器归零中断 } /*************************************************************************** * 描 述 : PWM中断服务函数 * 入 参 : 无 * 返回值 : 无 **************************************************************************/ void PWM(void) interrupt PWM_VECTOR using 1 { PWMIF &= 0xBF; //将CBIF位置0,PWM计数器归零中断标志位,需软件清零 PWMIF &= 0xFE; //将C2IF位置0,PWM2中断标志位,需软件清零 PWMIF &= 0xFD; //将C3IF位置0,PWM3中断标志位,需软件清零 } /******************************************************** 功能描述:主函数 入口参数:无 返回值:无 *********************************************************/ int main() { uint8 flag=1; uint16 ledpwmval=0; ///////////////////////////////////////////////// //注意: STC15W4K32S4系列的芯片,上电后所有与PWM相关的IO口均为 // 高阻态,需将这些口设置为准双向口或强推挽模式方可正常使用 //相关IO: P0.6/P0.7/P1.6/P1.7/P2.1/P2.2 // P2.3/P2.7/P3.7/P4.2/P4.4/P4.5 ///////////////////////////////////////////////// P2M1 &= 0x7F; P2M0 &= 0x7F; //设置P2.7为准双向口 P4M1 &= 0xDF; P4M0 &= 0xDF; //设置P4.5为准双向口 PWM2PWM3_Configuration(); //初始化PWM2和PWM3口 EA = 1; //允许总中断 while(1) { delay_ms(100); //延迟每次指示灯亮度的时间,更方便观察实验现象 if(flag) //如果标识符为1则递增变量ledpwmval ledpwmval++; else //如果标识符为0则递减变量ledpwmval ledpwmval--; if(ledpwmval>248) //如果变量ledpwmval递增到一定值则控制标识符为0,以实现ledpwmval递减 flag=0; if(ledpwmval==1) //如果变量ledpwmval递减到一定值则控制标识符为1,以实现ledpwmval递增 flag=1; P_SW2 |= 0x80; //将EAXSFR位置1,以访问PWM在扩展RAM区的特殊功能寄存器 PWM2T1 =(uint16)ledpwmval; //赋值PWM2第一次翻转计数器值(不断变化值) PWM2T2 = 0x00FA; //赋值PWM2第二次翻转计数器值(定值) PWM3T1 =(uint16)ledpwmval; //赋值PWM3第一次翻转计数器值(不断变化值) PWM3T2 = 0x00FA; //赋值PWM3第二次翻转计数器值(定值) P_SW2 &= 0x7F; //将EAXSFR位置0,恢复访问XRAM } }
问下大神定时器T0中断导致串口通信无法正常进行的问题。
这是我的代码,将之烧入51单片机时,串口通信本来是让led显示,但是可能由于红外遥控的中断函数干扰了串口通信,导致红外遥控工作正常,串口通信无法进行,led无法显示。 #include<reg51.h> #include<intrins.h> #include"lcd.h" #include"temp.h" #define uint unsigned int #define uchar unsigned char sbit moto=P1^0; uchar CNCHAR[6] = "摄氏度"; void LcdDisplay(int); void UsartConfiguration(); /******************************************************************************* * 函数名 : main * 函数功能 : 主函数 * 输入 : 无 * 输出 : 无 *******************************************************************************/ void UsartConfiguration() { SCON=0X50; //设置为工作方式1 TMOD=0X20; //设置计数器工作方式2 PCON=0X80; //波特率加倍 TH1=0XF3; //计数器初始值设置,注意波特率是4800的 TL1=0XF3; //ES=1; //打开接收中断 //EA=1; //打开总中断 TR1=1; //打开计数器 } uchar time0; bit IRok;//33数据位处理完成标志位 bit handle_ok; uchar IRcode[4];//4个字节 uchar IRdata[33];//33位数据 void InitUART() //定时器初始化 { TMOD=0x02;//定时器重装初值 TH0=0; TL0=0; EA=1; EX0=1; TCON=0X01; ET0=1; TR0=1;//启动定时器 } void t0() interrupt 1 { time0++;//一次中断为277.76us } void int0() interrupt 0 { static uchar i; static bit flag; if(flag) { ES=0; if((time0<54)&&(time0>32)) i=0; IRdata[i]=time0; time0=0; i++; if(i==33) { i=0; IRok=1; } } else { time0=0; flag=1; } } void handle_data() { uchar i;//处理四个字节 uchar j;//处理八位 uchar k;//处理三十三个数据 k=1; for(i=0;i<4;i++) { for(j=0;j<8;j++) { if(IRdata[k]>5) IRcode[i]|=0x80; k++; if(j<7) IRcode[i]>>=1; } } handle_ok=1; } void work() { uchar j; j=0x01; switch (IRcode[2]) { case 0x0c:moto=0;break;//1 数字一按键停止(修改这里更改控制按键) case 0x18:moto=1;break;//2数字二按键启动 default:break; } } but() { while(1) { if(IRok) { handle_data(); IRok=0; } if(handle_ok)//如果处理完成处理遥控器相关程序 { work(); handle_ok=0; } } } void main() { InitUART(); UsartConfiguration(); LcdInit(); //初始化LCD1602 LcdWriteCom(0x88); //写地址 80表示初始地址 LcdWriteData('C'); while(1) { but(); LcdDisplay(Ds18b20ReadTemp()); } } /******************************************************************************* * 函数名 : LcdDisplay() * 函数功能 : LCD显示读取到的温度 * 输入 : v * 输出 : 无 *******************************************************************************/ void LcdDisplay(int temp) //lcd显示 { unsigned char i, datas[] = {0, 0, 0, 0, 0}; //定义数组 float tp; if(temp< 0) //当温度值为负数 { LcdWriteCom(0x80); //写地址 80表示初始地址 SBUF='-';//将接收到的数据放入到发送寄存器 while(!TI); //等待发送数据完成 TI=0; //清除发送完成标志位 LcdWriteData('-'); //显示负 //因为读取的温度是实际温度的补码,所以减1,再取反求出原码 temp=temp-1; temp=~temp; tp=temp; temp=tp*0.0625*100+0.5; //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点 //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就 //算由?.5,还是在小数点后面。 } else { LcdWriteCom(0x80); //写地址 80表示初始地址 LcdWriteData('+'); //显示正 SBUF='+';//将接收到的数据放入到发送寄存器 while(!TI); //等待发送数据完成 TI=0; //清除发送完成标志位 tp=temp;//因为数据处理有小数点所以将温度赋给一个浮点型变量 //如果温度是正的那么,那么正数的原码就是补码它本身 temp=tp*0.0625*100+0.5; //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点 //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就 //算加上0.5,还是在小数点后面。 } if(temp>=2900) { moto=1; //开启电机 } else { moto=0; //关闭电机 } datas[0] = temp / 10000; datas[1] = temp % 10000 / 1000; datas[2] = temp % 1000 / 100; datas[3] = temp % 100 / 10; datas[4] = temp % 10; LcdWriteCom(0x82); //写地址 80表示初始地址 LcdWriteData('0'+datas[0]); //百位 SBUF = '0'+datas[0];//将接收到的数据放入到发送寄存器 while (!TI) ; //等待发送数据完成 TI = 0; LcdWriteCom(0x83); //写地址 80表示初始地址 LcdWriteData('0'+datas[1]); //十位 SBUF = '0'+datas[1];//将接收到的数据放入到发送寄存器 while (!TI); //等待发送数据完成 TI = 0; LcdWriteCom(0x84); //写地址 80表示初始地址 LcdWriteData('0'+datas[2]); //个位 SBUF = '0'+datas[2];//将接收到的数据放入到发送寄存器 while (!TI); //等待发送数据完成 TI = 0; LcdWriteCom(0x85); //写地址 80表示初始地址 LcdWriteData('.'); //显示 ‘.’ SBUF = '.';//将接收到的数据放入到发送寄存器 while (!TI); //等待发送数据完成 TI = 0; LcdWriteCom(0x86); //写地址 80表示初始地址 LcdWriteData('0'+datas[3]); //显示小数点 SBUF = '0'+datas[3];//将接收到的数据放入到发送寄存器 while (!TI); //等待发送数据完成 TI = 0; LcdWriteCom(0x87); //写地址 80表示初始地址 LcdWriteData('0'+datas[4]); //显示小数点 SBUF = '0'+datas[4];//将接收到的数据放入到发送寄存器 while (!TI); //等待发送数据完成 TI = 0; for(i=0; i<6; i++) { SBUF = CNCHAR[i];//将接收到的数据放入到发送寄存器 while (!TI); //等待发送数据完成 TI = 0; } }
用ATmega8驱动OLED时候显示屏不亮。
用ATmega8驱动OLED代码如下,用ICCAVR编程,显示屏不亮,不知道代码是否有问题,还是需要检查平台的配置? ``` #include<iom8v.h> #include<macros.h> //#include "asiic.h" #define uchar unsigned char #define uint unsigned int const xtal = 8; const unsigned char f6x8[][6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// sp 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00,// ! 0x00, 0x00, 0x07, 0x00, 0x07, 0x00,// " 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14,// # 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12,// $ 0x00, 0x62, 0x64, 0x08, 0x13, 0x23,// % 0x00, 0x36, 0x49, 0x55, 0x22, 0x50,// & 0x00, 0x00, 0x05, 0x03, 0x00, 0x00,// ' 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00,// ( 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00,// ) 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14,// * 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08,// + 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00,// , 0x00, 0x08, 0x08, 0x08, 0x08, 0x08,// - 0x00, 0x00, 0x60, 0x60, 0x00, 0x00,// . 0x00, 0x20, 0x10, 0x08, 0x04, 0x02,// / 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E,// 0 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00,// 1 0x00, 0x42, 0x61, 0x51, 0x49, 0x46,// 2 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31,// 3 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10,// 4 0x00, 0x27, 0x45, 0x45, 0x45, 0x39,// 5 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30,// 6 0x00, 0x01, 0x71, 0x09, 0x05, 0x03,// 7 0x00, 0x36, 0x49, 0x49, 0x49, 0x36,// 8 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E,// 9 0x00, 0x00, 0x36, 0x36, 0x00, 0x00,// : 0x00, 0x00, 0x56, 0x36, 0x00, 0x00,// ; 0x00, 0x08, 0x14, 0x22, 0x41, 0x00,// < 0x00, 0x14, 0x14, 0x14, 0x14, 0x14,// = 0x00, 0x00, 0x41, 0x22, 0x14, 0x08,// > 0x00, 0x02, 0x01, 0x51, 0x09, 0x06,// ? 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E,// @ 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C,// A 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36,// B 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22,// C 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C,// D 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41,// E 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01,// F 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A,// G 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F,// H 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00,// I 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01,// J 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41,// K 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40,// L 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F,// M 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F,// N 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E,// O 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06,// P 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E,// Q 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46,// R 0x00, 0x46, 0x49, 0x49, 0x49, 0x31,// S 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01,// T 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F,// U 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F,// V 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F,// W 0x00, 0x63, 0x14, 0x08, 0x14, 0x63,// X 0x00, 0x07, 0x08, 0x70, 0x08, 0x07,// Y 0x00, 0x61, 0x51, 0x49, 0x45, 0x43,// Z 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00,// [ 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55,// 55 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00,// ] 0x00, 0x04, 0x02, 0x01, 0x02, 0x04,// ^ 0x00, 0x40, 0x40, 0x40, 0x40, 0x40,// _ 0x00, 0x00, 0x01, 0x02, 0x04, 0x00,// ' 0x00, 0x20, 0x54, 0x54, 0x54, 0x78,// a 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38,// b 0x00, 0x38, 0x44, 0x44, 0x44, 0x20,// c 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F,// d 0x00, 0x38, 0x54, 0x54, 0x54, 0x18,// e 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02,// f 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C,// g 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78,// h 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00,// i 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00,// j 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00,// k 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00,// l 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78,// m 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78,// n 0x00, 0x38, 0x44, 0x44, 0x44, 0x38,// o 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18,// p 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC,// q 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08,// r 0x00, 0x48, 0x54, 0x54, 0x54, 0x20,// s 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20,// t 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C,// u 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C,// v 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C,// w 0x00, 0x44, 0x28, 0x10, 0x28, 0x44,// x 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C,// y 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44,// z 0x14, 0x14, 0x14, 0x14, 0x14, 0x14,// horiz lines }; //延时函数 void delay_ms(uchar n) { uint i; for(i=n;i<(uint)(xtal*143-2);i++);//xtal为晶振大小,单位为MHz } void delay_us(uint a) { uint i; for(i = 0;i < a;i++) { asm("nop"); } } /*------------------------------------------------------- 注意: 该模块不和5.0V接口兼容,所以请大家在使用的时候一定 要小心,别直接接到5V的系统上去,接5V 系统时,应串接 3.3K电阻,电源依然是3.3V。 所用模块为4线SPI:每个数据长度均为8位,在SCLK的上升沿,数 据从SDIN移入到SSD1306,并且是高位在前的 D0 -> PB5 串行时钟线SCLK D1 -> PB3 串行数据线SDIN RES -> PC6 硬复位OLED D/C -> PB1 命令/数据标志(0,读写命令;1,读写数据) CS -> PB2 OLED片选信号(高电平有效) -------------------------------------------------------*/ //---------------宏定义--------------------------------- //SCLK #define SCLK_high (PORTB |= (1<<PB5)) #define SCLK_low (PORTB &=~(1<<PB5)) // SDIN #define DSDIN_high (PORTB |= (1<<PB3)) #define DSDIN_low (PORTB &=~(1<<PB3)) #define RES_high (PORTC |= (1<<PC6)) #define RES_low (PORTC &=~(1<<PC6)) //写数据 #define DC_high (PORTB |= (1<<PB1)) //写命令 #define DC_low (PORTB &=~(1<<PB1)) //------------------------------------------------------ //端口初始化 void port_init(void) { DDRC = 0xff; PORTC = 0x00; PORTC |= 0x40; DDRB = 0xff; PORTB = 0x2f; //PORTB |=(1<<PB1); /* SCLK_high; DSDIN_high; DC_low; RES_high; */ } //向Oled写入数据 void Oled_WR_Data(uchar data) { uchar i; uchar temp = 0; DC_high; //写数据 SCLK_low; for(i=0;i<8;i++) { temp =data & 0x80; // SCLK_high;//高位在前 if(temp==0) { DSDIN_low; } else { DSDIN_high; } SCLK_high; delay_us(10); SCLK_low; data <<= 1; } // DSDIN_high; } //向Oled写入命令 void Oled_WR_Cmd(uchar cmd) { uchar i; uchar temp = cmd & 0x80; DC_low; //写命令 SCLK_low; for(i=0;i<8;i++) { // SCLK_high; //高位在前 if(temp==0) { DSDIN_low; } else { DSDIN_high; } SCLK_high; delay_us(10); SCLK_low; cmd <<= 1; } // DSDIN_high; } //满屏 void Oled_Fill(uchar dat) { uchar x,y; for(y=0;y<8;y++) { Oled_WR_Cmd(0xb0 + y); Oled_WR_Cmd(0x01); Oled_WR_Cmd(0x10); for(x=0;x<128;x++) Oled_WR_Data(dat); } } //清屏 void Oled_Cls(void) { uchar x,y; for(y=0;y<8;y++) { Oled_WR_Cmd(0xb0 + y); Oled_WR_Cmd(0x01); Oled_WR_Cmd(0x10); for(x=0;x<128;x++) Oled_WR_Data(0); } } //初始光标位置 void Oled_Set_Pos(uchar x,uchar y) { Oled_WR_Cmd(0xb0 + y); Oled_WR_Cmd(((x & 0xf0)>>4)|0x10); Oled_WR_Cmd((x & 0x0f)|0x01); } // OLED的初始化 void Oled_init(void) { SCLK_high; RES_low; delay_ms(100); RES_high; //从上电到下面开始初始化要有足够的时间,即等待RC复位完毕 Oled_WR_Cmd(0xae);//关闭显示 Oled_WR_Cmd(0x00);//---set low column address Oled_WR_Cmd(0x10);//---set high column address Oled_WR_Cmd(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F) Oled_WR_Cmd(0x81);//--对比度设置 Oled_WR_Cmd(0xcf); // Set SEG Output Current Brightness Oled_WR_Cmd(0xa1);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常 Oled_WR_Cmd(0xc8);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常 Oled_WR_Cmd(0xa6);//--set normal display Oled_WR_Cmd(0xa8);//设置驱动路数(1 to 64) Oled_WR_Cmd(0x3f);//--1/64 默认0X3F Oled_WR_Cmd(0xd3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F) Oled_WR_Cmd(0x00);//-not offset Oled_WR_Cmd(0xd5);//--set display clock divide ratio/oscillator frequency Oled_WR_Cmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec Oled_WR_Cmd(0xd9);//设置预充电周期 Oled_WR_Cmd(0xf1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock Oled_WR_Cmd(0xda);//--set com pins hardware configuration Oled_WR_Cmd(0x12); Oled_WR_Cmd(0xdb);//--set vcomh Oled_WR_Cmd(0x40);//Set VCOM Deselect Level Oled_WR_Cmd(0x20);//设置内存地址模式 (0x00/0x01/0x02) Oled_WR_Cmd(0x02);// Oled_WR_Cmd(0x8d);//电荷泵设置 Oled_WR_Cmd(0x14);//--set(0x10) disable Oled_WR_Cmd(0xa4);// Disable Entire Display On (0xa4/0xa5) Oled_WR_Cmd(0xa6);// Disable Inverse Display On (0xa6/a7) Oled_WR_Cmd(0xaf);//--开启显示 Oled_Fill(0xff); //Oled_Cls(); //初始清屏 Oled_Set_Pos(0,0);//初始位置 } void Oled_6x8_Str(uchar x,uchar y,uchar ch) { uchar c=0,i=0,j=0; while (ch) { ch -= 32; if(x > 126) { x = 0; y++; } Oled_Set_Pos(x,y); for(i=0;i<6;i++) Oled_WR_Data(f6x8[ch][i]); x += 6; j ++; } } void main(void) { port_init(); Oled_init(); Oled_Fill(0xff); // while(1) // { // if((PINB&(1<<PB0))==0) // { // while(!(PINB&(1<<PB0))); // if(count>=10) // count = 0; // else // count += 1; // } // delay_ms(5000); // Oled_Cls(); // Oled_6x8_Str(2,2,count); // delay_ms(5000); // Oled_Fill(0x00); // } } ```
如何修改程序使单片机工作周期缩短至一秒内
#include "reg51.h" #include<stdio.h> #include<intrins.h> //打开头文件 #define uint8 unsigned char #define uint16 unsigned int #define uchar unsigned char #define uint unsigned int //宏定义 uint16 cnt2ms; //10ms计数器 uint8 cnt1s; //1秒计数器 //电阻-------------------------------- #define Z1 13700053.00 #define Z2 3685279.00 float RZ=0; unsigned long RX=0; unsigned long RX2=0; unsigned long RX3=0; unsigned long RX4=0; unsigned long RX5=0; unsigned long RX6=0; unsigned long RX7=0; unsigned long RX8=0; unsigned long RX9=0; //电感-------------------------------- float LZ=0; #define C1 0.0461 //电容-------------------------------- float CZ=0; float CX=0; #define R1 24874 uint8 KL=0; // unsigned int Pulsecnt=0; //脉冲个数 unsigned int Pulsenum; //脉冲个数 uint8 start=0; // uint16 PulseTime; //10ms计数器 uint16 Pulse; //10ms计数器 bit flag=0; uint8 CH=0; // unsigned long x; void Init_Extint(void); void TimerInitProc(); void Init_System(void); void Display(uint8 chose_dat, uint8 dat); void ShowDisp(uint8 tPulsenum, uint8 tPulsecnt, uint8 tcnt1s); void DelayMs(uint8 Ms); uint8 Pulse_FLAG=0; uint8 Pulse_5=0; /********************************LCD液晶**************************************/ #define LCD_Data P0 //LCD的数据口 sbit LCD_BF=LCD_Data^7; //LCD忙信号位 sbit LCD_RS=P2^5; sbit LCD_RW=P2^6; sbit LCD_EN=P2^7; #define LCD_GO_HOME 0x02 //AC=0,光标、画面回HOME位 //输入方式设置 #define LCD_AC_AUTO_INCREMENT 0x06 //数据读、写操作后,AC自动增一 #define LCD_AC_AUTO_DECREASE 0x04 //数据读、写操作后,AC自动减一 #define LCD_MOVE_ENABLE 0x05 //数据读、写操作,画面平移 #define LCD_MOVE_DISENABLE 0x04 //数据读、写操作,画面不动 //设置显示、光标及闪烁开、关 #define LCD_DISPLAY_ON 0x0C //显示开 #define LCD_DISPLAY_OFF 0x08 //显示关 #define LCD_CURSOR_ON 0x0A //光标显示 #define LCD_CURSOR_OFF 0x08 //光标不显示 #define LCD_CURSOR_BLINK_ON 0x09 //光标闪烁 #define LCD_CURSOR_BLINK_OFF 0x08 //光标不闪烁 //光标、画面移动,不影响DDRAM #define LCD_LEFT_MOVE 0x18 //LCD显示左移一位 #define LCD_RIGHT_MOVE 0x1C //LCD显示右移一位 #define LCD_CURSOR_LEFT_MOVE 0x10 //光标左移一位 #define LCD_CURSOR_RIGHT_MOVE 0x14 //光标右移一位 //工作方式设置 #define LCD_DISPLAY_DOUBLE_LINE 0x38 //两行显示 #define LCD_DISPLAY_SINGLE_LINE 0x30 //单行显示 /*定义子程序*/ void LCD_ClrAll(void); //清屏 void Judge_LCD_busy(void); //检测是否忙碌 void LCD_Write(uchar WriteData); //写控制字 void LCD_write_data(uchar LCD_data); //写数据显示 void LCD_cursor(uchar x); //光标起始地址 void LCD_printc(unsigned char lcd_data) ; //输出一个字符 void LCD_prints(unsigned char *lcd_string);//输出字符串 /*LCD1602忙碌判断子程序*/ void Judge_LCD_busy(void) //判断LCD1602是否忙状态 { while(1) { LCD_EN=0; LCD_RS=0; LCD_RW=1; LCD_Data=0xff; LCD_EN=1; //EN 是 1—0 使能 if(!LCD_BF)break; //LCD_BF=1表示忙碌,需要等待。 } LCD_EN=0; } /******LCD清屏***************/ void LCD_ClrAll(void) { Judge_LCD_busy(); //判断是否忙碌 LCD_RS=0; LCD_RW=0; LCD_Data=0x01; LCD_EN=1; LCD_EN=0; } /*******LCD写数据定义各种模式*********/ void LCD_Write(uchar WriteData) //写指令到LCD { Judge_LCD_busy(); LCD_RS=0; LCD_RW=0; P0=WriteData; //把WriteData的数据送到数据口 LCD_EN=1; LCD_EN=0; } /********LCD显示数据***********/ void LCD_write_data(uchar LCD_data) //输出一个字节数据到LCD { Judge_LCD_busy(); LCD_RS=1; LCD_RW=0; P0=LCD_data; LCD_EN=1; LCD_EN=0; } /****光标位置的确定***/ void LCD_cursor(uchar x) //LCD光标定位到x处 { LCD_Write(0x80+x); //第一行地址是0x80 } /*输出一个字符*/ void LCD_printc(unsigned char lcd_data) { LCD_write_data(lcd_data); } /*输出字符串*/ void LCD_prints(unsigned char *lcd_string) { unsigned char i=0; while(lcd_string[i]!=0x00) { LCD_write_data(lcd_string[i]); i++; } } /*初始化程序*/ void LCD_initial(void) //初始化LCD { LCD_Write(LCD_AC_AUTO_INCREMENT|LCD_MOVE_DISENABLE); LCD_Write(LCD_DISPLAY_ON|LCD_CURSOR_OFF); LCD_Write(LCD_DISPLAY_DOUBLE_LINE); LCD_ClrAll(); } /****延时******/ void delay_1ms(uint x) { uint j; uchar i; for(j=0;j<x;j++) { for(i=0;i<120;i++); } } void Value_to_ASCII(unsigned long value,uchar add) { unsigned char temp[] = "0000.000K "; temp[0] = value/1000000 + 0x30;//数值改成字符,液晶显示需要ASCII码 value = value%1000000; temp[1] = value/100000 + 0x30; value = value%100000; temp[2] = value/10000 + 0x30; value = value%10000; temp[3] = value/1000 + 0x30; value = value%1000; temp[5] = value/100 + 0x30; value = value%100; temp[6] = value/10 + 0x30; value = value%10; temp[7] = value + 0x30; LCD_cursor(add); //光标起始地址 ,第一行地址是0x00~0x0F,第二行地址是0x40~0x4f LCD_prints(temp); } //--------------------主程序-------------------------------- void main() { TMOD=0x51; //设置定时器0,方式1:16位定时器 TH1=0; TL1=0; TH0=(65536-2000)/256; //设定定时周期 TL0=(65536-2000)%256; TR0=1; //打开定时器0 TR1=1; //打开定时器1 ET1=1; ET0=1; EA=1; //打开总中断 LCD_initial(); delay_1ms(50); LCD_initial(); delay_1ms(50); LCD_cursor(0x00); LCD_prints(" MEASURE Rx "); LCD_cursor(0x45); while(1) { if(flag==1) { flag=0; x=Pulsenum*65536+TH1*256+TL1;//计算1s内的脉冲个数,频率=脉冲个数 RZ=(Z1/x);//测量100到1000 RX=RZ-1000; RX=RX>>1; if(RX>=1005){ RX=RX-1005;}else{RX=0;} if(x==0){RX=0;RX2=0;RX3=0;RX4=0;RX5=0;RX6=0;RX7=0;RX8=0;RX9=0;} if(RX>1000000){RX=1000000; } RX2=(RX+RX3+RX4+RX5+RX6+RX7+RX8+RX9)>>3;//求平均提高精度 RX9=RX8;RX8=RX7;RX7=RX6;RX6=RX5;RX5=RX4;RX4=RX3;RX3=RX; //保存上一次的阻值 Value_to_ASCII(RX2,0x44);//更新阻值显示 cnt2ms=0; Pulsenum=0; TH1=0; TL1=0; TR1=1; } } } /******************************************************************************* * 函 数 名: Exti0_interrupt * 函数功能: /T0引脚下降沿进入中断 * 入口参数: 无 * 返 回: 无 *******************************************************************************/ void T1_interrupt(void) interrupt 3 using 0 { Pulsenum++; //保存测得脉冲值 } /******************************************************************** 函数功能:定时器/计数器1中断处理 入口参数:null 返 回:null 备 注:null ********************************************************************/ void Timer0IntProc() interrupt 1 using 0 //2ms { TH0=(65536-2000)/256; TL0=(65536-2000)%256; cnt2ms++; if(cnt2ms==497) //1s计时到 { TR1=0; cnt2ms = 0; flag=1; } }
关于单片机旋转POV显示C语言程序的错误
#include <reg52.h> #define uint unsigned int #define uchar unsigned char uchar code buffer[]= //*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*// { 0x10,0x60,0x02,0x8C,0x20,0x18,0x08,0x08,0x88,0x7F,0x88,0x08,0x28,0x18,0x08,0x00, //沈 0x04,0x04,0x7E,0x01,0x80,0x40,0x30,0x0C,0x03,0x00,0x3F,0x40,0x40,0x40,0x70,0x00, 0x00,0xFE,0x02,0x22,0xDA,0x06,0x00,0xFC,0x04,0x04,0x04,0x04,0x04,0xFC,0x00,0x00, //阳 0x00,0xFF,0x08,0x10,0x08,0x07,0x00,0xFF,0x41,0x41,0x41,0x41,0x41,0xFF,0x00,0x00, 0x80,0xFC,0x96,0xE5,0x84,0xFC,0x00,0x08,0xC8,0x49,0x4A,0xC8,0x08,0x08,0x00,0x00, //航 0x80,0x7F,0x02,0x4C,0x80,0x7F,0x80,0x60,0x1F,0x00,0x00,0x3F,0x40,0x40,0x78,0x00, 0x10,0x0C,0x44,0x24,0x14,0x04,0x05,0x06,0x04,0x04,0x14,0x24,0x44,0x14,0x0C,0x00, //空 0x00,0x40,0x40,0x41,0x41,0x41,0x41,0x7F,0x41,0x41,0x41,0x41,0x40,0x40,0x00,0x00, 0x80,0xFC,0x96,0xE5,0x84,0xFC,0x00,0x08,0xC8,0x49,0x4A,0xC8,0x08,0x08,0x00,0x00, //航 0x80,0x7F,0x02,0x4C,0x80,0x7F,0x80,0x60,0x1F,0x00,0x00,0x3F,0x40,0x40,0x78,0x00, 0x40,0x40,0x42,0x42,0x42,0x42,0x42,0xFE,0x42,0x42,0x42,0x42,0x42,0x40,0x40,0x00, //天 0x80,0x80,0x40,0x20,0x10,0x0C,0x03,0x00,0x03,0x0C,0x10,0x20,0x40,0x80,0x80,0x00, 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xFF,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00, //大 0x80,0x80,0x40,0x20,0x10,0x0C,0x03,0x00,0x03,0x0C,0x10,0x20,0x40,0x80,0x80,0x00, 0x40,0x30,0x11,0x96,0x90,0x90,0x91,0x96,0x90,0x90,0x98,0x14,0x13,0x50,0x30,0x00, //学 0x04,0x04,0x04,0x04,0x04,0x44,0x84,0x7E,0x06,0x05,0x04,0x04,0x04,0x04,0x04,0x00, }; uint i,sw=0,sd=0; void delay(void) { EA=0; TH0=0x78;TL0=0x78; EA=1; while(0==sw); sw=0; } void display(uint kk) //显示函数,其中kk为定义第几个字 { P1=0xff;P2=0xff; for(i=0;i<16;++i) { P1=buffer[i*2+kk*32];P2=buffer[1+i*2+kk*32]; P1=0xff;P2=0xff; delay(); } P1=0xff;P2=0xff; } void display_pov(int j) { if(j>=7) j=0; display(j); j++; } void timer0(void) interrupt 1 using 1 { TH1=0xf6;TL1=0x3a; sd=1; } void intersvr0(void) interrupt 1 //定时器0中断 { sw=1; } void timer0_init(void) //定时器初始化 { TMOD=0x12; TH0=0x78;TL0=0x78; //方式2定时(T0),定时每个帧的时间为278us TH1=0xf6;TL1=0x3a; //方式1定时(T1),定时显示每个字的时间为5004us,其中包括两列的空操作 IT0=1;ET0=1; IT1=1;ET1=1; TR0=1;TR1=1; } void main() { uint k=0; timer0_init(); EA=1; while(1) { if(sd==1) { display_pov(k); sd=0; } } }
基于单片机的焓度计设计
一个是温湿度传感器的程序 一个是LCD显示的程序 一个是温湿度转成焓度的程序 一个是时钟电路的程序 ![图片说明](https://img-ask.csdn.net/upload/201905/05/1557053348_781973.jpg) 求完整源码!!! 示例: #include <reg52.h> #include <intrins.h> #define MAIN_Fosc 11059200UL //宏定义主时钟HZ /*==================================== 自定义类型名 ====================================*/ typedef unsigned char INT8U; typedef unsigned char uchar; typedef unsigned int INT16U; typedef unsigned int uint; /*==================================== 硬件接口位声明 ====================================*/ sbit DS = P2^2; //DS18B20单总线 sbit DU = P2^6; //数码管段选 sbit WE = P2^7; //数码管位选 /*==================================== 共阴极数码管段选码 ====================================*/ uchar code table[]={ //0 1 2 3 4 5 6 7 8 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, //9 A B C D E F - . 关显示 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71, 0x40, 0x80, 0x00 }; /*==================================== 数码管位选码 ====================================*/ //第1位 2位 3位 4位 5位 6位 7位 8位 uchar code T_COM[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f};//数码管位码 /*==================================== 函数:void Delay_Ms(INT16U ms) 参数:ms,毫秒延时形参 描述:12T 51单片机自适应主时钟毫秒级延时函数 ====================================*/ void Delay_Ms(INT16U ms) { INT16U i; do{ i = MAIN_Fosc / 96000; while(--i); //96T per loop }while(--ms); } /*us延时函数,执行一次US--所需6.5us进入一次函数需要11.95us*/ void Delay_us(uchar us) { while(us--); } /*==================================== 函数:void Display(INT16U Value) 参数:Value,显示值 取值0-65535 描述:共阴极数码管显示函数可显示一个字节的数 ====================================*/ void Display(INT16U Value) //注意由于需要显示的数大于一个字节所有形参需为int型 { //------------------------------ DU = 0; //关闭段选 P0 = table[Value/100]; //数码管显示百位 DU = 1; //打开段选 DU = 0; //关闭段选 WE = 0; //关闭位选 P0 = T_COM[0]; //第一位数码管 WE = 1; //打开位选 WE = 0; //关闭位选 Delay_Ms(3); //------------------------------- DU = 0; P0 = table[Value%100/10]|0x80; //显示十位 DU = 1; DU = 0; WE = 0; P0 = T_COM[1]; //第二位数码管 WE = 1; WE = 0; Delay_Ms(3); //------------------------------- DU = 0; P0 = table[Value%10]; //显示个位 DU = 1; DU = 0; WE = 0; P0 = T_COM[2]; //第三位数码管 WE = 1; WE = 0; Delay_Ms(3); } /*单总线初始化时序*/ bit ds_init() { bit i; DS = 1; _nop_(); DS = 0; Delay_us(75); //拉低总线499.45us 挂接在总线上的18B20将会全部被复位 DS = 1; //释放总线 Delay_us(4); //延时37.95us 等待18B20发回存在信号 i = DS; Delay_us(20); //141.95us DS = 1; _nop_(); return (i); } /*写一个字节*/ void write_byte(uchar dat) { uchar i; for(i=0;i<8;i++) { DS = 0; _nop_();//产生些时序 DS = dat & 0x01; Delay_us(10);//76.95us DS = 1; //释放总线准备下一次数据写入 _nop_(); dat >>= 1; } } uchar read_byte() { uchar i, j, dat; for(i=0;i<8;i++) { DS = 0; _nop_();//产生读时序 DS = 1; _nop_();//释放总线 j = DS; Delay_us(10);//76.95us DS = 1; _nop_(); dat = (j<<7)|(dat>>1); } return (dat); } void main() { uint i; uchar L, M; /* ds_init();//初始化DS18B20 write_byte(0xcc);//发送跳跃ROM指令 write_byte(0x4e);//写暂存器指令 write_byte(0x7f); write_byte(0xf7); write_byte(0x1f);//配置工作在9位模式下 ds_init();//初始化DS18B20 write_byte(0xcc);//发送跳跃ROM指令 write_byte(0x48);*/ while(1) { ds_init();//初始化DS18B20 write_byte(0xcc);//发送跳跃ROM指令 write_byte(0x44);//发送温度转换指令 ds_init();//初始化DS18B20 write_byte(0xcc);//发送跳跃ROM指令 write_byte(0xbe);//读取DS18B20暂存器值 L = read_byte(); M = read_byte(); i = M; i <<= 8; i |= L; i = i * 0.0625 * 10 + 0.5; Display(i); } }
相见恨晚的超实用网站
搞学习 知乎:www.zhihu.com 简答题:http://www.jiandati.com/ 网易公开课:https://open.163.com/ted/ 网易云课堂:https://study.163.com/ 中国大学MOOC:www.icourse163.org 网易云课堂:study.163.com 哔哩哔哩弹幕网:www.bilibili.com 我要自学网:www.51zxw
花了20分钟,给女朋友们写了一个web版群聊程序
参考博客 [1]https://www.byteslounge.com/tutorials/java-ee-html5-websocket-example
爬虫福利二 之 妹子图网MM批量下载
爬虫福利一:27报网MM批量下载    点击 看了本文,相信大家对爬虫一定会产生强烈的兴趣,激励自己去学习爬虫,在这里提前祝:大家学有所成! 目标网站:妹子图网 环境:Python3.x 相关第三方模块:requests、beautifulsoup4 Re:各位在测试时只需要将代码里的变量 path 指定为你当前系统要保存的路径,使用 python xxx.py 或IDE运行即可。
字节跳动视频编解码面经
引言 本文主要是记录一下面试字节跳动的经历。 三四月份投了字节跳动的实习(图形图像岗位),然后hr打电话过来问了一下会不会opengl,c++,shador,当时只会一点c++,其他两个都不会,也就直接被拒了。 七月初内推了字节跳动的提前批,因为内推没有具体的岗位,hr又打电话问要不要考虑一下图形图像岗,我说实习投过这个岗位不合适,不会opengl和shador,然后hr就说秋招更看重基础。我当时
Java学习的正确打开方式
在博主认为,对于入门级学习java的最佳学习方法莫过于视频+博客+书籍+总结,前三者博主将淋漓尽致地挥毫于这篇博客文章中,至于总结在于个人,实际上越到后面你会发现学习的最好方式就是阅读参考官方文档其次就是国内的书籍,博客次之,这又是一个层次了,这里暂时不提后面再谈。博主将为各位入门java保驾护航,各位只管冲鸭!!!上天是公平的,只要不辜负时间,时间自然不会辜负你。 何谓学习?博主所理解的学习,它
程序员必须掌握的核心算法有哪些?
由于我之前一直强调数据结构以及算法学习的重要性,所以就有一些读者经常问我,数据结构与算法应该要学习到哪个程度呢?,说实话,这个问题我不知道要怎么回答你,主要取决于你想学习到哪些程度,不过针对这个问题,我稍微总结一下我学过的算法知识点,以及我觉得值得学习的算法。这些算法与数据结构的学习大多数是零散的,并没有一本把他们全部覆盖的书籍。下面是我觉得值得学习的一些算法以及数据结构,当然,我也会整理一些看过
大学四年自学走来,这些私藏的实用工具/学习网站我贡献出来了
大学四年,看课本是不可能一直看课本的了,对于学习,特别是自学,善于搜索网上的一些资源来辅助,还是非常有必要的,下面我就把这几年私藏的各种资源,网站贡献出来给你们。主要有:电子书搜索、实用工具、在线视频学习网站、非视频学习网站、软件下载、面试/求职必备网站。 注意:文中提到的所有资源,文末我都给你整理好了,你们只管拿去,如果觉得不错,转发、分享就是最大的支持了。 一、电子书搜索 对于大部分程序员...
linux系列之常用运维命令整理笔录
本博客记录工作中需要的linux运维命令,大学时候开始接触linux,会一些基本操作,可是都没有整理起来,加上是做开发,不做运维,有些命令忘记了,所以现在整理成博客,当然vi,文件操作等就不介绍了,慢慢积累一些其它拓展的命令,博客不定时更新 顺便拉下票,我在参加csdn博客之星竞选,欢迎投票支持,每个QQ或者微信每天都可以投5票,扫二维码即可,http://m234140.nofollow.ax.
比特币原理详解
一、什么是比特币 比特币是一种电子货币,是一种基于密码学的货币,在2008年11月1日由中本聪发表比特币白皮书,文中提出了一种去中心化的电子记账系统,我们平时的电子现金是银行来记账,因为银行的背后是国家信用。去中心化电子记账系统是参与者共同记账。比特币可以防止主权危机、信用风险。其好处不多做赘述,这一层面介绍的文章很多,本文主要从更深层的技术原理角度进行介绍。 二、问题引入 假设现有4个人...
Python 基础(一):入门必备知识
目录1 标识符2 关键字3 引号4 编码5 输入输出6 缩进7 多行8 注释9 数据类型10 运算符10.1 常用运算符10.2 运算符优先级 1 标识符 标识符是编程时使用的名字,用于给变量、函数、语句块等命名,Python 中标识符由字母、数字、下划线组成,不能以数字开头,区分大小写。 以下划线开头的标识符有特殊含义,单下划线开头的标识符,如:_xxx ,表示不能直接访问的类属性,需通过类提供
这30个CSS选择器,你必须熟记(上)
关注前端达人,与你共同进步CSS的魅力就是让我们前端工程师像设计师一样进行网页的设计,我们能轻而易举的改变颜色、布局、制作出漂亮的影音效果等等,我们只需要改几行代码,不需...
国产开源API网关项目进入Apache孵化器:APISIX
点击蓝色“程序猿DD”关注我回复“资源”获取独家整理的学习资料!近日,又有一个开源项目加入了这个Java开源界大名鼎鼎的Apache基金会,开始进行孵化器。项目名称:AP...
程序员接私活怎样防止做完了不给钱?
首先跟大家说明一点,我们做 IT 类的外包开发,是非标品开发,所以很有可能在开发过程中会有这样那样的需求修改,而这种需求修改很容易造成扯皮,进而影响到费用支付,甚至出现做完了项目收不到钱的情况。 那么,怎么保证自己的薪酬安全呢? 我们在开工前,一定要做好一些证据方面的准备(也就是“讨薪”的理论依据),这其中最重要的就是需求文档和验收标准。一定要让需求方提供这两个文档资料作为开发的基础。之后开发
网页实现一个简单的音乐播放器(大佬别看。(⊙﹏⊙))
今天闲着无事,就想写点东西。然后听了下歌,就打算写个播放器。 于是乎用h5 audio的加上js简单的播放器完工了。 欢迎 改进 留言。 演示地点跳到演示地点 html代码如下`&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;music&lt;/title&gt; &lt;meta charset="utf-8"&gt
Python十大装B语法
Python 是一种代表简单思想的语言,其语法相对简单,很容易上手。不过,如果就此小视 Python 语法的精妙和深邃,那就大错特错了。本文精心筛选了最能展现 Python 语法之精妙的十个知识点,并附上详细的实例代码。如能在实战中融会贯通、灵活使用,必将使代码更为精炼、高效,同时也会极大提升代码B格,使之看上去更老练,读起来更优雅。 1. for - else 什么?不是 if 和 else 才
数据库优化 - SQL优化
前面一篇文章从实例的角度进行数据库优化,通过配置一些参数让数据库性能达到最优。但是一些“不好”的SQL也会导致数据库查询变慢,影响业务流程。本文从SQL角度进行数据库优化,提升SQL运行效率。 判断问题SQL 判断SQL是否有问题时可以通过两个表象进行判断: 系统级别表象 CPU消耗严重 IO等待严重 页面响应时间过长
2019年11月中国大陆编程语言排行榜
2019年11月2日,我统计了某招聘网站,获得有效程序员招聘数据9万条。针对招聘信息,提取编程语言关键字,并统计如下: 编程语言比例 rank pl_ percentage 1 java 33.62% 2 c/c++ 16.42% 3 c_sharp 12.82% 4 javascript 12.31% 5 python 7.93% 6 go 7.25% 7
通俗易懂地给女朋友讲:线程池的内部原理
餐厅的约会 餐盘在灯光的照耀下格外晶莹洁白,女朋友拿起红酒杯轻轻地抿了一小口,对我说:“经常听你说线程池,到底线程池到底是个什么原理?”我楞了一下,心里想女朋友今天是怎么了,怎么突然问出这么专业的问题,但做为一个专业人士在女朋友面前也不能露怯啊,想了一下便说:“我先给你讲讲我前同事老王的故事吧!” 大龄程序员老王 老王是一个已经北漂十多年的程序员,岁数大了,加班加不动了,升迁也无望,于是拿着手里
经典算法(5)杨辉三角
杨辉三角 是经典算法,这篇博客对它的算法思想进行了讲解,并有完整的代码实现。
编写Spring MVC控制器的14个技巧
本期目录 1.使用@Controller构造型 2.实现控制器接口 3.扩展AbstractController类 4.为处理程序方法指定URL映射 5.为处理程序方法指定HTTP请求方法 6.将请求参数映射到处理程序方法 7.返回模型和视图 8.将对象放入模型 9.处理程序方法中的重定向 10.处理表格提交和表格验证 11.处理文件上传 12.在控制器中自动装配业务类 ...
腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹?
昨天,有网友私信我,说去阿里面试,彻底的被打击到了。问了为什么网上大量使用ThreadLocal的源码都会加上private static?他被难住了,因为他从来都没有考虑过这个问题。无独有偶,今天笔者又发现有网友吐槽了一道腾讯的面试题,我们一起来看看。 腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹? 在互联网职场论坛,一名程序员发帖求助到。二面腾讯,其中一个算法题:64匹
面试官:你连RESTful都不知道我怎么敢要你?
面试官:了解RESTful吗? 我:听说过。 面试官:那什么是RESTful? 我:就是用起来很规范,挺好的 面试官:是RESTful挺好的,还是自我感觉挺好的 我:都挺好的。 面试官:… 把门关上。 我:… 要干嘛?先关上再说。 面试官:我说出去把门关上。 我:what ?,夺门而去 文章目录01 前言02 RESTful的来源03 RESTful6大原则1. C-S架构2. 无状态3.统一的接
求小姐姐抠图竟遭白眼?痛定思痛,我决定用 Python 自力更生!
点击蓝色“Python空间”关注我丫加个“星标”,每天一起快乐的学习大家好,我是 Rocky0429,一个刚恰完午饭,正在用刷网页浪费生命的蒟蒻...一堆堆无聊八卦信息的网页内容慢慢使我的双眼模糊,一个哈欠打出了三斤老泪,就在此时我看到了一张图片:是谁!是谁把我女朋友的照片放出来的!awsl!太好看了叭...等等,那个背景上的一堆鬼画符是什么鬼?!真是看不下去!叔叔婶婶能忍,隔壁老王的三姨妈的四表...
为啥国人偏爱Mybatis,而老外喜欢Hibernate/JPA呢?
关于SQL和ORM的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行了一番讨论,感触还是有一些,于是就有了今天这篇文。 声明:本文不会下关于Mybatis和JPA两个持久层框架哪个更好这样的结论。只是摆事实,讲道理,所以,请各位看官勿喷。 一、事件起因 关于Mybatis和JPA孰优孰劣的问题,争论已经很多年了。一直也没有结论,毕竟每个人的喜好和习惯是大不相同的。我也看
SQL-小白最佳入门sql查询一
不要偷偷的查询我的个人资料,即使你再喜欢我,也不要这样,真的不好;
项目中的if else太多了,该怎么重构?
介绍 最近跟着公司的大佬开发了一款IM系统,类似QQ和微信哈,就是聊天软件。我们有一部分业务逻辑是这样的 if (msgType = "文本") { // dosomething } else if(msgType = "图片") { // doshomething } else if(msgType = "视频") { // doshomething } else { // doshom...
致 Python 初学者
欢迎来到“Python进阶”专栏!来到这里的每一位同学,应该大致上学习了很多 Python 的基础知识,正在努力成长的过程中。在此期间,一定遇到了很多的困惑,对未来的学习方向感到迷茫。我非常理解你们所面临的处境。我从2007年开始接触 python 这门编程语言,从2009年开始单一使用 python 应对所有的开发工作,直至今天。回顾自己的学习过程,也曾经遇到过无数的困难,也曾经迷茫过、困惑过。开办这个专栏,正是为了帮助像我当年一样困惑的 Python 初学者走出困境、快速成长。希望我的经验能真正帮到你
“狗屁不通文章生成器”登顶GitHub热榜,分分钟写出万字形式主义大作
一、垃圾文字生成器介绍 最近在浏览GitHub的时候,发现了这样一个骨骼清奇的雷人项目,而且热度还特别高。 项目中文名:狗屁不通文章生成器 项目英文名:BullshitGenerator 根据作者的介绍,他是偶尔需要一些中文文字用于GUI开发时测试文本渲染,因此开发了这个废话生成器。但由于生成的废话实在是太过富于哲理,所以最近已经被小伙伴们给玩坏了。 他的文风可能是这样的: 你发现,...
程序员:我终于知道post和get的区别
是一个老生常谈的话题,然而随着不断的学习,对于以前的认识有很多误区,所以还是需要不断地总结的,学而时习之,不亦说乎
《程序人生》系列-这个程序员只用了20行代码就拿了冠军
你知道的越多,你不知道的越多 点赞再看,养成习惯GitHub上已经开源https://github.com/JavaFamily,有一线大厂面试点脑图,欢迎Star和完善 前言 这一期不算《吊打面试官》系列的,所有没前言我直接开始。 絮叨 本来应该是没有这期的,看过我上期的小伙伴应该是知道的嘛,双十一比较忙嘛,要值班又要去帮忙拍摄年会的视频素材,还得搞个程序员一天的Vlog,还要写BU...
加快推动区块链技术和产业创新发展,2019可信区块链峰会在京召开
11月8日,由中国信息通信研究院、中国通信标准化协会、中国互联网协会、可信区块链推进计划联合主办,科技行者协办的2019可信区块链峰会将在北京悠唐皇冠假日酒店开幕。   区块链技术被认为是继蒸汽机、电力、互联网之后,下一代颠覆性的核心技术。如果说蒸汽机释放了人类的生产力,电力解决了人类基本的生活需求,互联网彻底改变了信息传递的方式,区块链作为构造信任的技术有重要的价值。   1...
程序员把地府后台管理系统做出来了,还有3.0版本!12月7号最新消息:已在开发中有github地址
第一幕:缘起 听说阎王爷要做个生死簿后台管理系统,我们派去了一个程序员…… 996程序员做的梦: 第一场:团队招募 为了应对地府管理危机,阎王打算找“人”开发一套地府后台管理系统,于是就在地府总经办群中发了项目需求。 话说还是中国电信的信号好,地府都是满格,哈哈!!! 经常会有外行朋友问:看某网站做的不错,功能也简单,你帮忙做一下? 而这次,面对这样的需求,这个程序员...
相关热词 c# 二进制截断字符串 c#实现窗体设计器 c#检测是否为微信 c# plc s1200 c#里氏转换原则 c# 主界面 c# do loop c#存为组套 模板 c# 停掉协程 c# rgb 读取图片
立即提问