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

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

c

1个回答

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

weixin_43846332
从前有条河 好的谢谢!
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
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 本人使用串口中断接收温度数据,而温度数据是有符号整型的,与上述函数定义矛盾,请问应该怎样更改呢??

求解 intmax_t 与 uintmax_t 含义和用法

求解 intmax_t 与 uintmax_t 含义和用法

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。

小白使用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语言或者C++中的宏定义的代码解释,可能和指针有点关系。

有没有人能帮忙解释一下最后一行的代码的含义呢,宏定义我能明白,GPIO_TypeDef 是定义的一个结构体,不胜感激 ``` ``` ``` #define PERIPH_BASE ((uint32_t)0x40000000) #define AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000) #define GPIOA_BASE (AHB1PERIPH_BASE + 0x0000) #define GPIOA ((GPIO_TypeDef *) GPIOA_BASE) ``` ``` ``` ```

C语言中数组赋值的问题

问题: - 小白尝试使用c语言实现转化阿拉伯数字到大写汉字 - 我想分两步走,第一步算出几位数字没问题, 第二部通过取余数分别取出个十百千位数字 - 取余数失败, position数组中所有赋值都是零 - 我看了一晚上也没研究明白, 求大神指点, 感激不尽 ```c #include <stdio.h> int main() { int num; int i = 0; int j; int position[4] = {-1, -1, -1, -1}; printf("输入数字:"); scanf("%d", &num); //判断有多少位数字 while(num >= 1) { num /= 10; i ++; } //i是位数 printf("%d位数字\n", i); //判断每位数字是什么 for(j = 0; j < i; j++) { position[j] = num % (10 ^ (i + 1)); //printf("%d\t", position[j]); num = num - (num % 10); printf("%d\t", num); } } ```

定义全局指针malloc遇到Hard Fault

# 定义全局指针malloc遇到Hard Fault 定义了一个全局指针:uint8_t *ReturnSignlocalhash = NULL; _然后在在一个函数里面使用malloc algorithm_log(" ======= 1 ======== "); if( ReturnSignlocal == NULL ){ algorithm_log(" ======= 2 ======== "); ReturnSignlocal = malloc(150); } memset(ReturnSignlocal,0,150); 然后发现执行到if语句的时候就直接Hard Fault 了。把uint8_t *ReturnSignlocalhash = NULL;改为局部的就ok了。

用结构体变量给另一个变量赋值

我想问一下将一个结构体成员赋给另一个变量,赋给的是什么值呢? 例如: typedef struct { uint8_t char[]; uint8_t start; uint8_t end; }BUF_T uint8_t temp; temp=BUF_T.end; 我想知道temp里面得到的值是什么? 谢谢大家了!

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的差别在哪?

c语言三维数组做函数输入变量,函数的形参怎么写?

比如uint8_t cube[5][5][5];要在函数内处理数组内的数据并能返回值,肯定是传入三维数组的地址。函数定义时形参不知道怎么写才行??void process(uint8_t ????);

stm32使用HT1621B显示变量中的值

stm32使用HT1621B的液晶显示时如何可以将变量的值显示到LCD中,小弟目前只能用数组来循环显示我定义好的数字 例:一个变量Temp里面有一个值,如何使用下面的程序将Temp的值显示出来。 ``` ``` ``` void display_Num(void) { uint8_t i=0; for(i=0;i<=9;i++){ //动态显示 GPIO_ResetBits(GPIOA, HT1621_CS); // CS = 0; write_mode(DAT); //进入写入模式 write_address(0x0F);//温度起始地址 for(uint8_t j=0;j<3;j++){ //结束地址 write_data_8bit(FontLibrary[i]);} //数组里为计算好的值0~9 Uldelay_ms(170); //延迟 GPIO_SetBits(GPIOA, HT1621_CS); //cs = 1; } } ```

51单片机外部中断自动重复了一次

51单片机外部中断自动重复了一次 ``` #include <reg52.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int void delay(uint); void breath(); uchar tr_1; void main() { EX0=1; IT0=1; EA=1; P0=0xfe; delay(4000); while(1) { tr_1=P0; P0=_crol_(P0,1); tr_1=P0; delay(4000); } } void breath() { uint count_1; count_1=0; while(count_1<100) { P0=0xff; delay(100-count_1); P0=0x00; delay(count_1); count_1++; } while(count_1>0) { P0=0x00; delay(count_1); P0=0xff; delay(100-count_1); count_1--; } P0=tr_1; } void delay(uint t) { uint count_1; while(t>0) { for(count_1=0;count_1<5;count_1++); t--; } } void led() interrupt 0 { uint count_1; count_1=0; while(count_1<1) { breath(); count_1++; } } ``` 点击了外部中断按钮之后,应该只出现一次呼吸灯的亮和灭,可是实际上有时候是一次,有时候是两次。???求助

实现登录界面,自定义用户名和密码

实现一个登录界面,自定义用户名和密码,然后登录时输入用户名和密码,成功就登录进去,显示主窗口,失败则清空用户输入的用户名和密码,退出就直接退出。 但现在不论输入什么都会清空用户输入 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; }

俄罗斯方块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

请问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) 第二次调试图(复位后)

运行SGX- graphene 报错显示“Creating enclave failed: 25”

### 问题描述 1. 环境:Ubuntu16版本,2.6版本graphene和对应的SGX-driver 2. 问题具体: 安装好SGX需要的driver,psw,sdk之后编译graphene成功,同样进入到测试目录````make SGX=1``` 成功,显示为 ![图片说明](https://img-ask.csdn.net/upload/202004/28/1588042610_941926.png) 但是当运行```SGX=1 ./pal_loader curl``` 之后却出现 ![图片说明](https://img-ask.csdn.net/upload/202004/28/1588042804_415152.png) 并不是预期结果![图片说明](https://img-ask.csdn.net/upload/202004/28/1588043286_716559.png) 3.自己用grep -r 找"Creating enclave failed"输出,发现在$GRAPHENE/Pal/src/host/Linux-SGX目录下的sgx_main.c当中的 initialize_enclave函数当中的![图片说明](https://img-ask.csdn.net/upload/202004/28/1588044500_356143.png)这一段是输出这句话的地方,可能是ret值小于0导致输出这句话,再次寻找create_enclave 这个函数, 在sgx_framesork.c当中找到定义 ,具体的代码如下: ``` int create_enclave(sgx_arch_secs_t * secs, sgx_arch_token_t * token) { assert(secs->size && IS_POWER_OF_2(secs->size)); assert(IS_ALIGNED(secs->base, secs->size)); int flags = MAP_SHARED; if (!zero_page) { zero_page = (void *) INLINE_SYSCALL(mmap, 6, NULL, g_page_size, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); if (IS_ERR_P(zero_page)) return -ENOMEM; } secs->ssa_frame_size = get_ssaframesize(token->body.attributes.xfrm) / g_page_size; secs->misc_select = token->masked_misc_select_le; memcpy(&secs->attributes, &token->body.attributes, sizeof(sgx_attributes_t)); /* Do not initialize secs->mr_signer and secs->mr_enclave here as they are * not used by ECREATE to populate the internal SECS. SECS's mr_enclave is * computed dynamically and SECS's mr_signer is populated based on the * SIGSTRUCT during EINIT (see pp21 for ECREATE and pp34 for * EINIT in https://software.intel.com/sites/default/files/managed/48/88/329298-002.pdf). */ uint64_t addr = INLINE_SYSCALL(mmap, 6, secs->base, secs->size, PROT_READ|PROT_WRITE|PROT_EXEC, flags|MAP_FIXED, isgx_device, 0); if (IS_ERR_P(addr)) { if (ERRNO_P(addr) == 1 && (flags | MAP_FIXED)) pal_printf("Permission denied on mapping enclave. " "You may need to set sysctl vm.mmap_min_addr to zero\n"); SGX_DBG(DBG_I, "enclave ECREATE failed in allocating EPC memory " "(errno = %ld)\n", ERRNO_P(addr)); return -ENOMEM; } assert(secs->base == addr); struct sgx_enclave_create param = { .src = (uint64_t) secs, }; int ret = INLINE_SYSCALL(ioctl, 3, isgx_device, SGX_IOC_ENCLAVE_CREATE, &param); if (IS_ERR(ret)) { SGX_DBG(DBG_I, "enclave ECREATE failed in enclave creation ioctl - %d\n", ERRNO(ret)); return -ERRNO(ret); } if (ret) { SGX_DBG(DBG_I, "enclave ECREATE failed - %d\n", ret); return -EPERM; } secs->attributes.flags |= SGX_FLAGS_INITIALIZED; SGX_DBG(DBG_I, "enclave created:\n"); SGX_DBG(DBG_I, " base: 0x%016lx\n", secs->base); SGX_DBG(DBG_I, " size: 0x%016lx\n", secs->size); SGX_DBG(DBG_I, " misc_select: 0x%08x\n", secs->misc_select); SGX_DBG(DBG_I, " attr.flags: 0x%016lx\n", secs->attributes.flags); SGX_DBG(DBG_I, " attr.xfrm: 0x%016lx\n", secs->attributes.xfrm); SGX_DBG(DBG_I, " ssa_frame_size: %d\n", secs->ssa_frame_size); SGX_DBG(DBG_I, " isv_prod_id: 0x%08x\n", secs->isv_prod_id); SGX_DBG(DBG_I, " isv_svn: 0x%08x\n", secs->isv_svn); return 0; } ``` 现在想要正常运行graphene,应该怎么改呢?

基于单片机的焓度计设计

一个是温湿度传感器的程序 一个是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); } }

DS18b20对单片机无应答,无法在12864上显示温度

#include<reg52.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int sbit a=P1^0; sbit CS1=P3^4;//片选1,低电平有效,CS1=0开左屏幕,CS1=1关左屏幕 sbit CS2=P3^3;//片选2,低电平有效,CS2=0开右屏幕,CS2=1关右屏幕![图片说明](https://img-ask.csdn.net/upload/202003/27/1585277082_423215.png)![图片说明](https://img-ask.csdn.net/upload/202003/27/1585277095_99069.png) sbit RS=P3^7;//数据,指令选择信号,RS=1为数据操作,RS=0为写指令或读状态(一般为读忙标志) sbit RW=P3^6;//读写选择信号,RW=1为读选通,RW=0为写选通 sbit EN=P3^5;//读写使能信号 sbit DQ = P1^1; // DS18B20传感器的引脚定义 void show(uchar screen,uchar page,uchar column,uchar *p); unsigned char code dang[]= { 0x00,0x40,0x42,0x44,0x58,0x40,0x40,0x7F,0x40,0x40,0x50,0x48,0xC6,0x00,0x00,0x00, 0x00,0x40,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0xFF,0x00,0x00,0x00 }; unsigned char code qian[]= { 0x08,0x08,0xE8,0x29,0x2E,0x28,0xE8,0x08,0x08,0xC8,0x0C,0x0B,0xE8,0x08,0x08,0x00, 0x00,0x00,0xFF,0x09,0x49,0x89,0x7F,0x00,0x00,0x0F,0x40,0x80,0x7F,0x00,0x00,0x00 }; unsigned char code wen[]= { 0x10,0x60,0x02,0x8C,0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00, 0x04,0x04,0x7E,0x01,0x40,0x7E,0x42,0x42,0x7E,0x42,0x7E,0x42,0x42,0x7E,0x40,0x00 }; unsigned char code t[]= { 0x06,0x09,0x09,0xE6,0xF8,0x0C,0x04,0x02,0x02,0x02,0x02,0x02,0x04,0x1E,0x00,0x00, 0x00,0x00,0x00,0x07,0x1F,0x30,0x20,0x40,0x40,0x40,0x40,0x40,0x20,0x10,0x00,0x00 }; unsigned char code du[]= { 0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25,0x26,0x24,0xFC,0x24,0x24,0x24,0x04,0x00, 0x40,0x30,0x8F,0x80,0x84,0x4C,0x55,0x25,0x25,0x25,0x55,0x4C,0x80,0x80,0x80,0x00 }; unsigned char code t1[][32]={ {0x00,0x00,0x00,0x00,0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00,0x00,0x70,0x08,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00,0x00,0x30,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x18,0x20,0x21,0x21,0x22,0x1C,0x00,0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00,0x00,0xC0,0x20,0x18,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x03,0x02,0x12,0x12,0x1F,0x12,0x12,0x00,0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00,0x00,0xFC,0x44,0x44,0x44,0x84,0x04,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x0C,0x10,0x10,0x10,0x08,0x07,0x00,0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00,0x00,0xE0,0x10,0x88,0x88,0x90,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x20,0x1F,0x00,0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00,0x00,0x18,0x08,0x08,0x88,0x68,0x18,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00,0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00,0x00,0xF0,0x08,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x01,0x12,0x22,0x22,0x11,0x0F,0x00,0x00,0x00,0x00,0x00}, }; void Delay15us(void) { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } void uartinit() { SCON=0x50; //设定串口工作方式 PCON=0x00; //波特率不倍增 TMOD|=0x20; //定时器1工作于8位自动重载模式, 用于产生波特率 EA=1; ES = 1; //允许串口中断 TL1=0xfd; TH1=0xfd; //波特率9600 TR1=1; } //温度------------------------------------------------------------ /*********************************************************/ // 复位DS18B20(初始化) /*********************************************************/ void DS18B20_ReSet(void) { uchar i; a=0; DQ=0; i=240; while(--i); DQ=1; i=30; while(--i); while(~DQ); i=4; while(--i); a=1; } /*********************************************************/ // 向DS18B20写入一个字节 /*********************************************************/ void DS18B20_WriteByte(uchar dat) { uchar j; uchar btmp; for(j=0;j<8;j++) { btmp=0x01; btmp=btmp<<j; btmp=btmp&dat; if(btmp>0) // 写1 { DQ=0; Delay15us(); DQ=1; Delay15us(); Delay15us(); Delay15us(); Delay15us(); } else // 写0 { DQ=0; Delay15us(); Delay15us(); Delay15us(); Delay15us(); DQ=1; Delay15us(); } } } void delay (uchar z) { uchar i,j; for(i=z;i>0;i--) for(j=114;j>0;j--); } /*********************************************************/ // 读取温度值 /*********************************************************/ int DS18B20_ReadTemp(void) { uchar j; int b,temp=0; DS18B20_ReSet(); // 产生复位脉 DS18B20_WriteByte(0xcc); // 忽略ROM指令 DS18B20_WriteByte(0x44); // 启动温度转换指令 DS18B20_ReSet(); // 产生复位脉 DS18B20_WriteByte(0xcc); // 忽略ROM指令 DS18B20_WriteByte(0xbe); // 读取温度指令 for(j=0;j<16;j++) // 读取温度数量 { DQ=0; _nop_(); _nop_(); DQ=1; Delay15us(); b=DQ; Delay15us(); Delay15us(); Delay15us(); b=b<<j; temp=temp|b; } temp=temp*0.0625*10; // 合成温度值并放大10倍 return (temp); // 返回检测到的温度值 } /*********************************************************/ // 温度值的显示 /*********************************************************/ void LcdPrintTemp(int temp) { if(temp<0) { temp=0-temp; // 负数转为正数 } else { show(3,1,0*16,t1[temp/1000+0x30]); // 显示百位show(2,2,2*16,t1[flag/100+0x30]); } show(2,2,0*16,t1[temp%1000/100+0x30]); // 显示十位 show(2,2,1*16,t1[temp%100/10+0x30]); // 显示个位 //show('.'); // 显示小数点 show(2,2,2*16,t1[temp%10+0x30]); // 显示小数后一位小数 } void busy() {P0=0X00; RS=0; RW=1; EN=1; while(P0&0X80);//P0&0X80结果为真则一直执行空操作,也就是说P0&0X80为0则结束循环 EN=0; } void wcmd(uchar cmd) {busy(); //每次写入指令或数据之前都进行忙检测 RS=0; RW=0; P0=cmd; EN=1;_nop_();_nop_();//在EN下降沿写入数据和指令 EN=0; } void wdata(uchar dat) //写命令和写数据只在RS=0或RS=1上不同,其余都相同 {busy(); RS=1; RW=0; P0=dat; EN=1;_nop_();_nop_();//在EN下降沿写入数据和指令 EN=0; } void set_page(uchar page)//设置页,12864LCD共有8页,每页有8行点阵点。 {page=0xb8|page; //首页地址为0XB8 wcmd(page); //page取值范围为0~7,表示第1到8页 } void set_line(uchar line)//设置显示的起始行,共有0——63行,一般从0行开始显示 {line=0xc0|line; //起始行地址0XC0 wcmd(line); //line取值范围为0~63,表示第1到64行 } void set_column(uchar column)//设置显示的列 {//column=column&0x3f; //列的最大值为0X3F,即64 column=0x40|column; //列的首地址为0X40, wcmd(column); //column的取值范围为0~63,共64列 } void set_onoff(uchar onoff)//设置显示开关,onoff取值为0或1 {onoff|=0x3e;//0X3E是关显示,0X3F是开显示 wcmd(onoff);//所以若onoff为0,则表示关显示,onoff为1,则表示开显示 } void select_screen(uchar screen)//选屏,screen取值范围为0,1,2 {switch(screen) {case 0:CS1=0;CS2=0;break; //全屏 case 1:CS1=0;CS2=1;break; //左半屏 case 2:CS1=1;CS2=0;break; //右半屏 default:break; } } void clear_screen(uchar screen)//清屏 {uchar i,j; select_screen(screen);//先选屏 for(i=0;i<8;i++)//控制页数0——7,共8页 {set_page(i);//设置页 set_column(0); //设置列,每页都从第1列开始,共64列 for(j=0;j<64;j++)//控制列数0——63,共64列 wdata(0x00);//写入0,列地址指针会自动加1 } } void init() //LCD初始化 {busy(); //忙检测 select_screen(0);//选屏,全屏 set_onoff(0); //关显示 select_screen(0); //选屏,全屏 set_onoff(1); //开显示 select_screen(0); //选屏,全屏 clear_screen(0); //清屏 set_line(0); //起始行:0 } void show(uchar screen,uchar page,uchar column,uchar *p)//显示一个汉字,一个汉字有32字节数据 {uchar i; //screen表示选择屏幕,page表示页,column表示列,*p表示汉字的数组 select_screen(screen); //选屏 set_page(page); //设置页,写上半页 set_column(column);//设置列 for(i=0;i<16;i++) //控制16列的数据输出 wdata(p[i]); //汉字的上半部分 set_page(page+1); //写下半页 set_column(column); //控制列 for(i=0;i<16;i++) //控制16列的数据输出 wdata(p[i+16]); //汉字的下半部分 } void uart_init() { TMOD = 0x20; TH1 = 0XFd; TL1 = 0XFd; SCON = 0x50; PCON = 0X00; TR1 = 1; EA = 1; ES = 1; } void DelayMs(uint time) { uint i,j; for(i=0;i<time;i++) for(j=0;j<112;j++); } //------------------------------------------------------------------ void main() { int temp; init(); clear_screen(0); uart_init() ; set_line(0); while(1) { show(1,0,2*16,dang); show(1,0,3*16,qian); show(2,0,0*16,wen); show(2,0,1*16,du ); show(2,3,3*16,t); temp=DS18B20_ReadTemp(); LcdPrintTemp(temp); // 显示测量结果 } }![图片说明](https://img-ask.csdn.net/upload/202003/27/1585277112_164513.png)

51单片机多功能时钟程序 LCD显示温度,实物显示不了。

实物LCD1602显示温度为0,其他信息正常显示,仿真一切正常,若将显示温度的函数单独提取成文件,则正常显示温度,不知道怎么改 PS只需看主函数,LCD1602.h和temp.h就行 ``` #include"key.h" #include"lcd1602.h" #include"ds1302.h" #include"naozhong.h" #include"temp.h" #include"nongli.h" #include "intrins.h" #define uchar unsigned char #define uint unsigned int uchar flag_nl,temperature; /*在LCD1602的坐标(x, y)位置显示星期*/ void LcdShowWeek(uchar x, uchar y, uchar dat) { uchar addr; if (y == 0) { addr = 0x00 + x; //第一行的x位置显示 } else { addr = 0x40 + x; //第二行x的位置显示 } LCD1602WriteCmd(addr + 0x80); switch(dat) { case 1:LCD1602WriteData('M');//星期数为1时,显示 LCD1602WriteData('O'); LCD1602WriteData('N'); break; case 2:LCD1602WriteData('T');//星期数据为2时显示 LCD1602WriteData('U'); LCD1602WriteData('E'); break; case 3:LCD1602WriteData('W');//星期数据为3时显示 LCD1602WriteData('E'); LCD1602WriteData('D'); break; case 4:LCD1602WriteData('T');//星期数据为4是显示 LCD1602WriteData('H'); LCD1602WriteData('U'); break; case 5:LCD1602WriteData('F');//星期数据为5时显示 LCD1602WriteData('R'); LCD1602WriteData('I'); break; case 6:LCD1602WriteData('S');//星期数据为6时显示 LCD1602WriteData('T'); LCD1602WriteData('A'); break; case 7:LCD1602WriteData('S');//星期数据为7时显示 LCD1602WriteData('U'); LCD1602WriteData('N'); break; } } /*显示温度*/ void LcdTimeDisplay(int temp) //lcd显示 { unsigned char datas[]={0,0}; //定义数组,室温比较小,所以只显示两位温度 float tp; if(temp< 0) //当温度值为负数 { LCD1602WriteCmd(0x80+0x0c); //写地址 LCD1602WriteData('-'); temp=temp-1; temp=~temp; tp=temp; temp=tp*0.0625*100+0.5; } else { LCD1602WriteCmd(0x80+0x0c); //写地址 LCD1602WriteData('+'); tp=temp; temp=tp*0.0625*100+0.5; } datas[0] = temp / 10000; datas[1] = temp % 10000 / 1000; datas[2] = temp % 1000 / 100; datas[3] = temp % 100 / 10; datas[4] = temp % 10; LCD1602WriteCmd(0x8d); //写地址 LCD1602WriteData('0'+datas[1]); //十位 Delay_nms(1); LCD1602WriteCmd(0x8e); //写地址 LCD1602WriteData('0'+datas[2]); //个位 Delay_nms(1); } /*初始化计时器*/ void Timer0Init() { EA = 1; //开总中断 TMOD = 0X01; //定时器0、工作方式1 ET0 = 1; //开定时器0中断 TR0 = 1; //允许定时器0定时 } /*读取DS1302中的时间*/ void Ds1302ReadTime() { miao = Ds1302Read(READ_RTC_ADDR[0]); //读秒 fen = Ds1302Read(READ_RTC_ADDR[1]); //读分 shi = Ds1302Read(READ_RTC_ADDR[2]); //读时 ri = Ds1302Read(READ_RTC_ADDR[3]); //读日 yue = Ds1302Read(READ_RTC_ADDR[4]); //读月 week = Ds1302Read(READ_RTC_ADDR[5]); //读星期 nian = Ds1302Read(READ_RTC_ADDR[6]); //读年 Conversion(0,nian,yue,ri); //农历转换 n_nian = year_moon ; n_yue = month_moon ; n_ri = day_moon ; } void DS1302ShowPart() //LCD显示时间、温度 { LcdShowWeek(12,1,week); LcdShowNum(2,1,shi); LcdShowNum(5,1,fen); LcdShowNum(8,1,miao); if(flag_nl == 0) //显示阳历 { LcdShowNum(2,0,nian); LcdShowNum(5,0,yue); LcdShowNum(8,0,ri); } else { LcdShowNum(2,0,n_nian); LcdShowNum(5,0,n_yue); LcdShowNum(8,0,n_ri); } } /*时钟+*/ void Inc_Time() { if(bit_flag == 1) //设置年 { nian+=0x01; if((nian & 0x0f) >= 0x0a) nian = (nian & 0xf0) + 0x10; if(nian >= 0x9a) nian = 1; } if(bit_flag == 2) //设置月 { yue+=0x01; if((yue & 0x0f) >= 0x0a) yue = (yue & 0xf0) + 0x10; if(yue >= 0x13) yue = 1; } if(bit_flag == 3) //设置日 { ri+=0x01; if((ri & 0x0f) >= 0x0a) ri = (ri & 0xf0) + 0x10; if(ri >= 0x32) ri = 0; } if(bit_flag == 4) //设置星期 { week+=0x01; if((week & 0x0f) >= 0x0a) week = (week & 0xf0) + 0x10; if(week >= 0x08) week = 1; } if(bit_flag == 5) //设置时 { shi+=0x01; if((shi & 0x0f) >= 0x0a) shi = (shi & 0xf0) + 0x10; ///***shi & 0xf0低四位清零,(shi & 0xf0) + 0x10向高位进1(高四位加1)***/// if(shi >= 0x24) shi = 0; } if(bit_flag == 6) //设置分 { fen+=0x01; if((fen & 0x0f) >= 0x0a) fen = (fen & 0xf0) + 0x10; if(fen >= 0x60) fen = 0; } if(bit_flag == 7) //设置秒 { miao+=0x01; if((miao & 0x0f) >= 0x0a) miao = (miao & 0xf0) + 0x10; if(miao >= 0x60) miao = 0; } } /*时钟-*/ void Dec_Time() { if(bit_flag == 1) { if(nian == 0x01) nian = 0x9a; if((nian & 0x0f) == 0x00) nian = (nian | 0x0a) - 0x10; nian -- ; } if(bit_flag == 2) { if(yue == 0x01) yue = 0x13; if((yue & 0x0f) == 0x00) yue = (yue | 0x0a) - 0x10; yue -- ; } if(bit_flag == 3) { if(ri == 0x01) ri = 0x32; if((ri & 0x0f) == 0x00) ri = (ri | 0x0a) - 0x10; ri -- ; } if(bit_flag == 4) //设置星期 { if(week == 0x01) week = 0x08; if((week & 0x0f) == 0x00) week = (week | 0x0a) - 0x10; week -- ; } if(bit_flag == 5) //设置时 { if(shi == 0x00) shi = 0x24; if((shi & 0x0f) == 0x00) shi = (shi | 0x0a) - 0x10; ///***如果个位为0,高四位减1***/// shi -- ; } if(bit_flag == 6) //设置分 { if(fen == 0x00) fen = 0x5a; if((fen & 0x0f) == 0x00) fen = (fen | 0x0a) - 0x10; fen -- ; } if(bit_flag == 7) { if(miao == 0x00) miao = 0x5a; if((miao & 0x0f) == 0x00) miao = (miao | 0x0a) - 0x10; miao -- ; } } /*具体按键功能*/ void KeyAction() { if (KeyValue == 1) { mode_flag++; if(mode_flag == 1) // 设置时钟 { bit_flag=1; //设置位为年的十位 LcdShowStr(0,0,"20 - - C"); LcdShowStr(0,1," : : "); } if(mode_flag == 2) //设置闹钟 { bit_flag = 1; //设置位为时的十位 LcdShowStr(0,0," set alarm "); LcdShowStr(0,1,"ON "); } if(mode_flag > 2) //回到正常显示 { mode_flag = 0; bit_flag = 0; write_guangbiao(2,0,0); //关闭光标 Int_1602_Dis(); //初始化液晶显示 } } if(KeyValue == 2) //选择键 { flag_200ms = 1; if(mode_flag == 1) //设置时间 { bit_flag ++; //设置位右移 if(bit_flag > 7) bit_flag= 1; } if(mode_flag == 2) //设置闹钟 { bit_flag ++; if(bit_flag > 5) bit_flag = 1; } } if(mode_flag == 1) { if(KeyValue == 3) //加 { Inc_Time(); } if(KeyValue == 4) //减 { Dec_Time(); } LcdShowNum(2,0,nian); //显示时间 LcdShowNum(5,0,yue); LcdShowNum(8,0,ri); LcdShowWeek(12,1,week); LcdShowNum(2,1,shi); LcdShowNum(5,1,fen); LcdShowNum(8,1,miao); LcdTimeDisplay(temperature); switch(bit_flag) // 光标显示 { case 1: write_guangbiao(2,0,1); break; case 2: write_guangbiao(5,0,1); break; case 3: write_guangbiao(8,0,1); break; case 4: write_guangbiao(12,1,1); break; case 5: write_guangbiao(2,1,1); break; case 6: write_guangbiao(5,1,1); break; case 7: write_guangbiao(8,1,1); break; } SetTimetoDS1302(); } /***************设置闹钟*********************/ if(mode_flag == 2) { if(bit_flag == 1) //设置闹钟开关 { if(KeyValue == 3) { alarm_open = 1; //闹钟功能开 } if(KeyValue == 4) { alarm_open = 0; //闹钟功能关 } } if(alarm_open == 1) { LcdShowStr(0,1,"ON "); } else { LcdShowStr(0,1,"OFF"); } if(bit_flag == 2) //设置第一个闹钟时 { if(KeyValue == 3) //加 { shi1+=0x01; if((shi1 & 0x0f) >= 0x0a) shi1 = (shi1 & 0xf0) + 0x10; if(shi1 >= 0x24) shi1 = 0; } if(KeyValue == 4) //减 { if(shi1 == 0x00) shi1 = 0x5a; if((shi1 & 0x0f) == 0x00) shi1 = (shi1 | 0x0a) - 0x10; shi1 -- ; } } if(bit_flag == 3) //设置第一个闹钟分 { if(KeyValue == 3) //加 { fen1+=0x01; if((fen1 & 0x0f) >= 0x0a) fen1 = (fen1 & 0xf0) + 0x10; if(fen1 >= 0x60) fen1 = 0; } if(KeyValue == 4) //减 { if(fen1 == 0x00) fen1 = 0x5a; if((fen1 & 0x0f) == 0x00) fen1 = (fen1 | 0x0a) - 0x10; fen1 -- ; } } if(bit_flag == 4) //设置第二个闹钟时 { if(KeyValue == 3) //加 { shi2+=0x01; if((shi2 & 0x0f) >= 0x0a) shi2 = (shi2 & 0xf0) + 0x10; if(shi2 >= 0x24) shi2 = 0; } if(KeyValue == 4) //减 { if(shi2 == 0x00) shi2 = 0x5a; if((shi2 & 0x0f) == 0x00) shi2 = (shi2 | 0x0a) - 0x10; shi2 -- ; } } if(bit_flag == 5) //设置第二个闹钟分 { if(KeyValue == 3) //加 { fen2+=0x01; if((fen2 & 0x0f) >= 0x0a) fen2 = (fen2 & 0xf0) + 0x10; if(fen2 >= 0x60) fen2 = 0; } if(KeyValue == 4) //减 { if(fen2 == 0x00) fen2 = 0x5a; if((fen2 & 0x0f) == 0x00) fen2 = (fen2 | 0x0a) - 0x10; fen2 -- ; } } LcdShowNum(4,1,shi1); LcdShowNum(7,1,fen1); LcdShowNum(10,1,shi2); LcdShowNum(13,1,fen2); switch(bit_flag) // 光标显示 { case 1: write_guangbiao(0,1,1); break; case 2: write_guangbiao(4,1,1); break; case 3: write_guangbiao(7,1,1); break; case 4: write_guangbiao(10,1,1); break; case 5: write_guangbiao(13,1,1); break; } } } void main() { InitLCD1602(); InitDS1302(); Timer0Init(); //初始化定时器 Int_1602_Dis(); Delay_nms(500); while(1) { KeyDown(); if(KeyValue<10) { if(beep_flag ==0) //闹钟不响的状态下可以设置 { KeyAction(); } else { beep_flag= 0; //按下任意键可关闭闹钟 beep = 1; Ds1302ReadTime(); } if(mode_flag == 0) { if(KeyValue == 4) { flag_nl = ~flag_nl; //切换农历和阳历的显示标志位 if(flag_nl == 1) LcdShowStr(0,0,"N "); } } } if(mode_flag == 0) { Ds1302ReadTime(); //读时间 DS1302ShowPart(); LcdTimeDisplay(Ds18b20ReadTemp()); } naozhong_alarm(); //闹钟报警函数 Delay_nms(1); } } /**************定时器0中断程序*****************/ void Timer0() interrupt 1 { static uchar value; TH0 = 0X3C; TL0 = 0XB0; //50ms value ++; if((value % 2) == 0) //100ms flag_100ms = 1; } /****************************************************************/ #ifndef _temp_H_ #define _temp_H_ #include <reg51.h> //---重定义关键词---// #ifndef uchar #define uchar unsigned char #endif #ifndef uint #define uint unsigned int #endif uchar temp; //--定义使用的IO口--// sbit DSPORT=P3^7; /******************************************************************************* * 函 数 名 : Delay1ms * 函数功能 : 延时函数 * 输 入 : 无 * 输 出 : 无 *******************************************************************************/ void Delay1ms(uint y) { uint x; for( ; y>0; y--) { for(x=110; x>0; x--); } } /******************************************************************************* * 函数名 : Ds18b20Init * 函数功能 : 初始化 * 输入 : 无 * 输出 : 初始化成功返回1,失败返回0 *******************************************************************************/ unsigned char Ds18b20Init() { unsigned int i; DSPORT=0; //将总线拉低480us~960us i=70; while(i--);//延时642us DSPORT=1; //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低 i=0; while(DSPORT) //等待DS18B20拉低总线 { i++; if(i>5000)//等待>5MS return 0;//初始化失败 } return 1;//初始化成功 } /******************************************************************************* * 函数名 : Ds18b20WriteByte * 函数功能 : 向18B20写入一个字节 * 输入 : com * 输出 : 无 *******************************************************************************/ void Ds18b20WriteByte(unsigned char dat) { unsigned int i,j; for(j=0;j<8;j++) { DSPORT=0; //每写入一位数据之前先把总线拉低1us i++; DSPORT=dat&0x01; //然后写入一个数据,从最低位开始 i=6; while(i--); //延时68us,持续时间最少60us DSPORT=1; //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值 dat>>=1; } } /******************************************************************************* * 函数名 : Ds18b20ReadByte * 函数功能 : 读取一个字节 * 输入 : com * 输出 : 无 *******************************************************************************/ unsigned char Ds18b20ReadByte() { unsigned char byte,bi; unsigned int i,j; for(j=8;j>0;j--) { DSPORT=0;//先将总线拉低1us i++; DSPORT=1;//然后释放总线 i++; i++;//延时6us等待数据稳定 bi=DSPORT; //读取数据,从最低位开始读取 /*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/ byte=(byte>>1)|(bi<<7); i=4; //读取完之后等待48us再接着读取下一个数 while(i--); } return byte; } /******************************************************************************* * 函数名 : Ds18b20ChangTemp * 函数功能 : 让18b20开始转换温度 * 输入 : com * 输出 : 无 *******************************************************************************/ void Ds18b20ChangTemp() { Ds18b20Init(); Delay1ms(1); Ds18b20WriteByte(0xcc); //跳过ROM操作命令 Ds18b20WriteByte(0x44); //温度转换命令 // Delay1ms(100); //等待转换成功,而如果你是一直刷着的话,就不用这个延时了 } /******************************************************************************* * 函数名 : Ds18b20ReadTempCom * 函数功能 : 发送读取温度命令 * 输入 : com * 输出 : 无 *******************************************************************************/ void Ds18b20ReadTempCom() { Ds18b20Init(); Delay1ms(1); Ds18b20WriteByte(0xcc); //跳过ROM操作命令 Ds18b20WriteByte(0xbe); //发送读取温度命令 } /******************************************************************************* * 函数名 : Ds18b20ReadTemp * 函数功能 : 读取温度 * 输入 : com * 输出 : 无 *******************************************************************************/ int Ds18b20ReadTemp() { int temp=0; unsigned char tmh,tml; Ds18b20ChangTemp(); //先写入转换命令 Ds18b20ReadTempCom(); //然后等待转换完后发送读取温度命令 tml=Ds18b20ReadByte(); //读取温度值共16位,先读低字节 tmh=Ds18b20ReadByte(); //再读高字节 temp=tmh; temp<<=8; temp|=tml; return temp; } #endif /************************************************************************/ #ifndef _lcd1602_H_ #define _lcd1602_H_ #include<reg51.h> #define LCD1602_DB P0 #define uchar unsigned char #define uint unsigned int #define LCD1602_4PINS //引脚定义 sbit LCD1602_RS = P2^6; sbit LCD1602_RW = P2^5; sbit LCD1602_EN = P2^7; uchar code table_num[]="0123456789abcdefg"; /*延时函数,延时1ms*/ void Lcd1602_Delay1ms(uint c) //误差 0us { uchar a,b; for (; c>0; c--) { for (b=199;b>0;b--) { for(a=1;a>0;a--); } } } /*LCD1602写命令*/ void LCD1602WriteCmd(uchar cmd) { LCD1602_RS = 0; LCD1602_RW = 0; LCD1602_EN = 0; LCD1602_DB = cmd; Lcd1602_Delay1ms(1); //等待数据稳定 LCD1602_EN = 1;//高脉冲 Lcd1602_Delay1ms(5); //等待数据稳定 LCD1602_EN = 0;//关闭液晶输出 LCD1602_DB = cmd<<4; Lcd1602_Delay1ms(1); LCD1602_EN = 1;//高脉冲 Lcd1602_Delay1ms(5); //等待数据稳定 LCD1602_EN = 0;//关闭液晶输出 } void LCD1602WriteData(uchar dat) { LCD1602_RS = 1; LCD1602_RW = 0; LCD1602_EN = 0; LCD1602_DB = dat;//送入数据 Lcd1602_Delay1ms(1); LCD1602_EN = 1;//高脉冲 Lcd1602_Delay1ms(5); LCD1602_EN = 0;//关闭液晶输出 LCD1602_DB = dat<<4; Lcd1602_Delay1ms(1); LCD1602_EN = 1;//高脉冲 Lcd1602_Delay1ms(5); LCD1602_EN = 0;//关闭液晶输出 } /*液晶初始化*/ void InitLCD1602() { LCD1602WriteCmd(0x32); LCD1602WriteCmd(0x28); LCD1602WriteCmd(0x0C); //开显示不显示光标 LCD1602WriteCmd(0x06); //写入字符时字符指针++且光标++ LCD1602WriteCmd(0x01); //显示清屏 } /*在LCD1602的坐标(x, y)位置显示str*/ void LcdShowStr(uchar x, uchar y, uchar * str) { uchar addr; if (y == 0) { addr = 0x00 + x; //第一行的x位置显示 } else { addr = 0x40 + x; //第二行x的位置显示 } LCD1602WriteCmd(addr + 0x80); while (*str != '\0') { LCD1602WriteData(*str++); } } /*在LCD1602的坐标(x, y)位置显示十进制数*/ void LcdShowNum(uchar x, uchar y, uchar dat) { uchar addr; if (y == 0) { addr = 0x00 + x; //第一行的x位置显示 } else { addr = 0x40 + x; //第二行x的位置显示 } LCD1602WriteCmd(addr + 0x80); LCD1602WriteData(table_num[dat/16]); LCD1602WriteData(table_num[dat%16]); } /*光标控制函数*/ void write_guangbiao(uchar x,uchar y,uchar dat) { if(y==0) LCD1602WriteCmd(0x80+x); else LCD1602WriteCmd(0x80+0x40+x); if(dat == 1) LCD1602WriteCmd(0x0f); //显示光标并且闪烁 else LCD1602WriteCmd(0x0c); //关闭光标 } /*屏幕初始化函数*/ void Int_1602_Dis() { LcdShowStr(0,0,"20 - - C"); LcdShowStr(0,1," : : "); } #endif /**********************************************************/ #ifndef _naozhong_H_ #define _naozhong_H_ sbit beep=P1^5; unsigned char alarm_open; //闹钟功能标志 unsigned char fen1=0x00,shi1=0x00,fen2=0x00,shi2=0x00; //两个闹钟变量的定义 unsigned char beep_flag;//闹钟开关标志 bit flag_100ms; unsigned char alarm_open=0; /*************闹钟报警函数***************/ void naozhong_alarm() { static unsigned char t,r; if(flag_100ms == 1) //100ms执行一次 { flag_100ms = 0; if(alarm_open == 1) //如果闹钟打开 { if((miao == 0) && (fen == fen1) && (shi == shi1)) { beep_flag = 1; //有报警 打开蜂鸣器响的标志位 } if(((miao == 0) && (fen == fen2) && (shi == shi2))) { beep_flag = 1; //有报警 打开蜂鸣器响的标志位 } if(beep_flag == 1) //闹钟以被打开 { beep = ~beep; Delay_nms(10); r ++; if(r >= 10) { r = 0; t++; if(t >= 60) { t = 0; beep_flag = 0; beep = 1; } } } } } } #endif /******************************************************/ #ifndef _key_H_ #define _key_H_ #include <reg51.h> #define uchar unsigned char #define uint unsigned int uchar KeyValue; //用来存放读取到的键值 uchar mode_flag; //模式标志 uchar bit_flag; //位标志 bit flag_200ms; void Delay_nms(unsigned int c) //延时1ms { unsigned char a,b; for (; c>0; c--) { for (b=199;b>0;b--) { for(a=1;a>0;a--); } } } //判断按键是否按下 并得到键值 void KeyDown() { static uchar key_new; KeyValue = 20; //按键值还原 P3 |= 0xff; //对应的按键IO口输出为1 if((P3 & 0xff) != 0xff) //按键按下 { Delay_nms(1); //按键消抖动 if(((P3 & 0xff) != 0xff) && (key_new == 1)) { //确认是按键按下 key_new = 0; switch(P3 & 0xff) { case 0xfe: KeyValue = 4; break; //得到按键值 case 0xfd: KeyValue = 3; break; //得到按键值 case 0xfb: KeyValue = 2; break; //得到按键值 case 0xf7: KeyValue = 1; break; //得到按键值 } } } else key_new = 1; } #endif /**********************************************************************/ #ifndef _nongli_H_ #define _nongli_H_ #include <intrins.h> //公历年对应的农历数据,每年三字节, //格式第一字节BIT7-4 位表示闰月月份,值为0 为无闰月,BIT3-0 对应农历第1-4 月的大小 //第二字节BIT7-0 对应农历第5-12 月大小,第三字节BIT7 表示农历第13 个月大小 //月份对应的位为1 表示本农历月大(30 天),为0 表示小(29 天). //第三字节BIT6-5 表示春节的公历月份,BIT4-0 表示春节的公历日期 //********阳历转换阴历表************************************ code unsigned char year_code[597]={ 0x04,0xAe,0x53, //1901 0 0x0A,0x57,0x48, //1902 3 0x55,0x26,0xBd, //1903 6 0x0d,0x26,0x50, //1904 9 0x0d,0x95,0x44, //1905 12 0x46,0xAA,0xB9, //1906 15 0x05,0x6A,0x4d, //1907 18 0x09,0xAd,0x42, //1908 21 0x24,0xAe,0xB6, //1909 0x04,0xAe,0x4A, //1910 0x6A,0x4d,0xBe, //1911 0x0A,0x4d,0x52, //1912 0x0d,0x25,0x46, //1913 0x5d,0x52,0xBA, //1914 0x0B,0x54,0x4e, //1915 0x0d,0x6A,0x43, //1916 0x29,0x6d,0x37, //1917 0x09,0x5B,0x4B, //1918 0x74,0x9B,0xC1, //1919 0x04,0x97,0x54, //1920 0x0A,0x4B,0x48, //1921 0x5B,0x25,0xBC, //1922 0x06,0xA5,0x50, //1923 0x06,0xd4,0x45, //1924 0x4A,0xdA,0xB8, //1925 0x02,0xB6,0x4d, //1926 0x09,0x57,0x42, //1927 0x24,0x97,0xB7, //1928 0x04,0x97,0x4A, //1929 0x66,0x4B,0x3e, //1930 0x0d,0x4A,0x51, //1931 0x0e,0xA5,0x46, //1932 0x56,0xd4,0xBA, //1933 0x05,0xAd,0x4e, //1934 0x02,0xB6,0x44, //1935 0x39,0x37,0x38, //1936 0x09,0x2e,0x4B, //1937 0x7C,0x96,0xBf, //1938 0x0C,0x95,0x53, //1939 0x0d,0x4A,0x48, //1940 0x6d,0xA5,0x3B, //1941 0x0B,0x55,0x4f, //1942 0x05,0x6A,0x45, //1943 0x4A,0xAd,0xB9, //1944 0x02,0x5d,0x4d, //1945 0x09,0x2d,0x42, //1946 0x2C,0x95,0xB6, //1947 0x0A,0x95,0x4A, //1948 0x7B,0x4A,0xBd, //1949 0x06,0xCA,0x51, //1950 0x0B,0x55,0x46, //1951 0x55,0x5A,0xBB, //1952 0x04,0xdA,0x4e, //1953 0x0A,0x5B,0x43, //1954 0x35,0x2B,0xB8, //1955 0x05,0x2B,0x4C, //1956 0x8A,0x95,0x3f, //1957 0x0e,0x95,0x52, //1958 0x06,0xAA,0x48, //1959 0x7A,0xd5,0x3C, //1960 0x0A,0xB5,0x4f, //1961 0x04,0xB6,0x45, //1962 0x4A,0x57,0x39, //1963 0x0A,0x57,0x4d, //1964 0x05,0x26,0x42, //1965 0x3e,0x93,0x35, //1966 0x0d,0x95,0x49, //1967 0x75,0xAA,0xBe, //1968 0x05,0x6A,0x51, //1969 0x09,0x6d,0x46, //1970 0x54,0xAe,0xBB, //1971 0x04,0xAd,0x4f, //1972 0x0A,0x4d,0x43, //1973 0x4d,0x26,0xB7, //1974 0x0d,0x25,0x4B, //1975 0x8d,0x52,0xBf, //1976 0x0B,0x54,0x52, //1977 0x0B,0x6A,0x47, //1978 0x69,0x6d,0x3C, //1979 0x09,0x5B,0x50, //1980 0x04,0x9B,0x45, //1981 0x4A,0x4B,0xB9, //1982 0x0A,0x4B,0x4d, //1983 0xAB,0x25,0xC2, //1984 0x06,0xA5,0x54, //1985 0x06,0xd4,0x49, //1986 0x6A,0xdA,0x3d, //1987 0x0A,0xB6,0x51, //1988 0x09,0x37,0x46, //1989 0x54,0x97,0xBB, //1990 0x04,0x97,0x4f, //1991 0x06,0x4B,0x44, //1992 0x36,0xA5,0x37, //1993 0x0e,0xA5,0x4A, //1994 0x86,0xB2,0xBf, //1995 0x05,0xAC,0x53, //1996 0x0A,0xB6,0x47, //1997 0x59,0x36,0xBC, //1998 0x09,0x2e,0x50, //1999 294 0x0C,0x96,0x45, //2000 297 0x4d,0x4A,0xB8, //2001 0x0d,0x4A,0x4C, //2002 0x0d,0xA5,0x41, //2003 0x25,0xAA,0xB6, //2004 0x05,0x6A,0x49, //2005 0x7A,0xAd,0xBd, //2006 0x02,0x5d,0x52, //2007 0x09,0x2d,0x47, //2008 0x5C,0x95,0xBA, //2009 0x0A,0x95,0x4e, //2010 0x0B,0x4A,0x43, //2011 0x4B,0x55,0x37, //2012 0x0A,0xd5,0x4A, //2013 0x95,0x5A,0xBf, //2014 0x04,0xBA,0x53, //2015 0x0A,0x5B,0x48, //2016 0x65,0x2B,0xBC, //2017 0x05,0x2B,0x50, //2018 0x0A,0x93,0x45, //2019 0x47,0x4A,0xB9, //2020 0x06,0xAA,0x4C, //2021 0x0A,0xd5,0x41, //2022 0x24,0xdA,0xB6, //2023 0x04,0xB6,0x4A, //2024 0x69,0x57,0x3d, //2025 0x0A,0x4e,0x51, //2026 0x0d,0x26,0x46, //2027 0x5e,0x93,0x3A, //2028 0x0d,0x53,0x4d, //2029 0x05,0xAA,0x43, //2030 0x36,0xB5,0x37, //2031 0x09,0x6d,0x4B, //2032 0xB4,0xAe,0xBf, //2033 0x04,0xAd,0x53, //2034 0x0A,0x4d,0x48, //2035 0x6d,0x25,0xBC, //2036 0x0d,0x25,0x4f, //2037 0x0d,0x52,0x44, //2038 0x5d,0xAA,0x38, //2039 0x0B,0x5A,0x4C, //2040 0x05,0x6d,0x41, //2041 0x24,0xAd,0xB6, //2042 0x04,0x9B,0x4A, //2043 0x7A,0x4B,0xBe, //2044 0x0A,0x4B,0x51, //2045 0x0A,0xA5,0x46, //2046 0x5B,0x52,0xBA, //2047 0x06,0xd2,0x4e, //2048 0x0A,0xdA,0x42, //2049 0x35,0x5B,0x37, //2050 0x09,0x37,0x4B, //2051 0x84,0x97,0xC1, //2052 0x04,0x97,0x53, //2053 0x06,0x4B,0x48, //2054 0x66,0xA5,0x3C, //2055 0x0e,0xA5,0x4f, //2056 0x06,0xB2,0x44, //2057 0x4A,0xB6,0x38, //2058 0x0A,0xAe,0x4C, //2059 0x09,0x2e,0x42, //2060 0x3C,0x97,0x35, //2061 0x0C,0x96,0x49, //2062 0x7d,0x4A,0xBd, //2063 0x0d,0x4A,0x51, //2064 0x0d,0xA5,0x45, //2065 0x55,0xAA,0xBA, //2066 0x05,0x6A,0x4e, //2067 0x0A,0x6d,0x43, //2068 0x45,0x2e,0xB7, //2069 0x05,0x2d,0x4B, //2070 0x8A,0x95,0xBf, //2071 0x0A,0x95,0x53, //2072 0x0B,0x4A,0x47, //2073 0x6B,0x55,0x3B, //2074 0x0A,0xd5,0x4f, //2075 0x05,0x5A,0x45, //2076 0x4A,0x5d,0x38, //2077 0x0A,0x5B,0x4C, //2078 0x05,0x2B,0x42, //2079 0x3A,0x93,0xB6, //2080 0x06,0x93,0x49, //2081 0x77,0x29,0xBd, //2082 0x06,0xAA,0x51, //2083 0x0A,0xd5,0x46, //2084 0x54,0xdA,0xBA, //2085 0x04,0xB6,0x4e, //2086 0x0A,0x57,0x43, //2087 0x45,0x27,0x38, //2088 0x0d,0x26,0x4A, //2089 0x8e,0x93,0x3e, //2090 0x0d,0x52,0x52, //2091 0x0d,0xAA,0x47, //2092 0x66,0xB5,0x3B, //2093 0x05,0x6d,0x4f, //2094 0x04,0xAe,0x45, //2095 0x4A,0x4e,0xB9, //2096 0x0A,0x4d,0x4C, //2097 0x0d,0x15,0x41, //2098 0x2d,0x92,0xB5, //2099 }; ///月份数据表 code unsigned int day_code[12]={0x00,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3,0x111,0x130,0x14e}; bit c_moon; //世纪 unsigned char year_moon,month_moon,day_moon; //判断农历月的大月或小月,如果该月为大返回1,为小返回0 bit get_moon_day(unsigned char month_p,unsigned int table_addr) { unsigned char temp; switch (month_p){ case 1:{temp=year_code[table_addr]&0x08; if (temp==0)return(0);else return(1);} case 2:{temp=year_code[table_addr]&0x04; if (temp==0)return(0);else return(1);} case 3:{temp=year_code[table_addr]&0x02; if (temp==0)return(0);else return(1);} case 4:{temp=year_code[table_addr]&0x01; if (temp==0)return(0);else return(1);} case 5:{temp=year_code[table_addr+1]&0x80; if (temp==0) return(0);else return(1);} case 6:{temp=year_code[table_addr+1]&0x40; if (temp==0)return(0);else return(1);} case 7:{temp=year_code[table_addr+1]&0x20; if (temp==0)return(0);else return(1);} case 8:{temp=year_code[table_addr+1]&0x10; if (temp==0)return(0);else return(1);} case 9:{temp=year_code[table_addr+1]&0x08; if (temp==0)return(0);else return(1);} case 10:{temp=year_code[table_addr+1]&0x04; if (temp==0)return(0);else return(1);} case 11:{temp=year_code[table_addr+1]&0x02; if (temp==0)return(0);else return(1);} case 12:{temp=year_code[table_addr+1]&0x01; if (temp==0)return(0);else return(1);} case 13:{temp=year_code[table_addr+2]&0x80; if (temp==0)return(0);else return(1);} } return(0); } /* 函数功能:输入BCD阳历数据,输出BCD阴历数据(只允许1901-2099年) 调用函数示例:Conversion(c_sun,year_sun,month_sun,day_sun) 如:计算2004年10月16日Conversion(0,0x4,0x10,0x16); c_sun,year_sun,month_sun,day_sun均为BCD数据,c_sun为世纪标志位,c_sun=0为21世 纪,c_sun=1为19世纪 调用函数后,原有数据不变,读c_moon,year_moon,month_moon,day_moon得出阴历BCD数据 */ void Conversion(bit c,unsigned char year,unsigned char month,unsigned char day)//公历日期 { //c=0 为21世纪,c=1 为19世纪 输入输出数据均为BCD数据 unsigned char temp1,temp2,temp3,month_p; unsigned int temp4,table_addr; bit flag2,flag_y; temp1=year/16; //BCD->hex 先把数据转换为十六进制 temp2=year%16; year=temp1*10+temp2; temp1=month/16; temp2=month%16; month=temp1*10+temp2; temp1=day/16; temp2=day%16; day=temp1*10+temp2; //定位数据表地址 if(c==0){ table_addr=(year+0x64-1)*0x03; //19..年 } else { table_addr=(year-1)*0x03; } //取当年春节所在的公历月份 temp1=year_code[table_addr+2]&0x60; temp1=_cror_(temp1,5); //取当年春节所在的公历日 temp2=year_code[table_addr+2]&0x1f; // 计算当年春年离当年元旦的天数,春节只会在公历1月或2月 if(temp1==0x01){ temp3=temp2-1; } else{ temp3=temp2+0x1f-1; } // 计算当年春年离当年元旦的天数完成 //计算公历日离当年元旦的天数 temp4=day_code[month-1]+day-1; if ((year%0x04==0)||((year%0x64)&&(year%0x190))) //闰年 { if((month>0x02)) //月份超过2 temp4+=1; } //判断公历日在春节前还是春节后 if (temp4>=temp3) { //公历日在春节后或就是春节当日使用下面代码进行运算 temp4-=temp3; month=0x01; month_p=0x01; //month_p为农历月份指向,公历日在春节前或就是春节当日month_p指向首月 flag2=get_moon_day(month_p,table_addr); //检查该农历月为大小还是小月,大月返回1,小月返回0 flag_y=0; if(flag2==0) temp1=0x1d; //小月29天 else temp1=0x1e; //大月30天 temp2=year_code[table_addr]&0xf0; temp2=_cror_(temp2,4); //从数据表中取该年的闰月月份,如为0则该年无闰月 while(temp4>=temp1){ temp4-=temp1; month_p+=1; if(month==temp2) { flag_y=~flag_y; //闰了该月,月份数值不加 if(flag_y==0) month+=1; } else month+=1; flag2=get_moon_day(month_p,table_addr); if(flag2==0) temp1=0x1d; else temp1=0x1e; } day=temp4+1; } else { //公历日在春节前使用下面代码进行运算 temp3-=temp4; if (year==0x00) //是否在21世纪 {year=0x63;c=1;} else year-=1; table_addr-=0x03; month=0x0c; temp2=year_code[table_addr]&0xf0; //闰月月份 temp2=_cror_(temp2,4); if (temp2==0) //无闰月 month_p=0x0c; //指向12月 else month_p=0x0d; // 指向13月 //month_p为月份指向,如果当年有闰月,一年有十三个月,月指向13,无闰月指向12 flag_y=0; flag2=get_moon_day(month_p,table_addr); if(flag2==0) temp1=0x1d; else temp1=0x1e; while(temp3>temp1) { temp3-=temp1; month_p-=1; if(flag_y==0) month-=1; if(month==temp2) flag_y=~flag_y; flag2=get_moon_day(month_p,table_addr); if(flag2==0) temp1=0x1d; else temp1=0x1e; } day=temp1-temp3+1; } c_moon=c; //HEX->BCD ,运算结束后,把数据转换为BCD数据 temp1=year/10; temp1=_crol_(temp1,4); temp2=year%10; year_moon=temp1|temp2; temp1=month/10; temp1=_crol_(temp1,4); temp2=month%10; month_moon=temp1|temp2; temp1=day/10; temp1=_crol_(temp1,4); temp2=day%10; day_moon=temp1|temp2; } #endif ```

大学四年自学走来,这些私藏的实用工具/学习网站我贡献出来了

大学四年,看课本是不可能一直看课本的了,对于学习,特别是自学,善于搜索网上的一些资源来辅助,还是非常有必要的,下面我就把这几年私藏的各种资源,网站贡献出来给你们。主要有:电子书搜索、实用工具、在线视频学习网站、非视频学习网站、软件下载、面试/求职必备网站。 注意:文中提到的所有资源,文末我都给你整理好了,你们只管拿去,如果觉得不错,转发、分享就是最大的支持了。 一、电子书搜索 对于大部分程序员...

在中国程序员是青春饭吗?

今年,我也32了 ,为了不给大家误导,咨询了猎头、圈内好友,以及年过35岁的几位老程序员……舍了老脸去揭人家伤疤……希望能给大家以帮助,记得帮我点赞哦。 目录: 你以为的人生 一次又一次的伤害 猎头界的真相 如何应对互联网行业的「中年危机」 一、你以为的人生 刚入行时,拿着傲人的工资,想着好好干,以为我们的人生是这样的: 等真到了那一天,你会发现,你的人生很可能是这样的: ...

程序员请照顾好自己,周末病魔差点一套带走我。

程序员在一个周末的时间,得了重病,差点当场去世,还好及时挽救回来了。

ArrayList源码分析(入门篇)

ArrayList源码分析 前言: 写这篇博客的主要原因是,在我上一次参加千牵科技Java实习生面试时,有被面试官问到ArrayList为什么查找的速度较快,插入和删除的速度较慢?当时我回答得不好,很大的一部分原因是因为我没有阅读过ArrayList源码,虽然最后收到Offer了,但我拒绝了,打算寒假学得再深入些再广泛些,下学期开学后再去投递其他更好的公司。为了更加深入理解ArrayList,也为

我以为我学懂了数据结构,直到看了这个导图才发现,我错了

数据结构与算法思维导图

String s = new String(" a ") 到底产生几个对象?

老生常谈的一个梗,到2020了还在争论,你们一天天的,哎哎哎,我不是针对你一个,我是说在座的各位都是人才! 上图红色的这3个箭头,对于通过new产生一个字符串(”宜春”)时,会先去常量池中查找是否已经有了”宜春”对象,如果没有则在常量池中创建一个此字符串对象,然后堆中再创建一个常量池中此”宜春”对象的拷贝对象。 也就是说准确答案是产生了一个或两个对象,如果常量池中原来没有 ”宜春” ,就是两个。...

技术大佬:我去,你写的 switch 语句也太老土了吧

昨天早上通过远程的方式 review 了两名新来同事的代码,大部分代码都写得很漂亮,严谨的同时注释也很到位,这令我非常满意。但当我看到他们当中有一个人写的 switch 语句时,还是忍不住破口大骂:“我擦,小王,你丫写的 switch 语句也太老土了吧!” 来看看小王写的代码吧,看完不要骂我装逼啊。 private static String createPlayer(PlayerTypes p...

和黑客斗争的 6 天!

互联网公司工作,很难避免不和黑客们打交道,我呆过的两家互联网公司,几乎每月每天每分钟都有黑客在公司网站上扫描。有的是寻找 Sql 注入的缺口,有的是寻找线上服务器可能存在的漏洞,大部分都...

讲一个程序员如何副业月赚三万的真实故事

loonggg读完需要3分钟速读仅需 1 分钟大家好,我是你们的校长。我之前讲过,这年头,只要肯动脑,肯行动,程序员凭借自己的技术,赚钱的方式还是有很多种的。仅仅靠在公司出卖自己的劳动时...

上班一个月,后悔当初着急入职的选择了

最近有个老铁,告诉我说,上班一个月,后悔当初着急入职现在公司了。他之前在美图做手机研发,今年美图那边今年也有一波组织优化调整,他是其中一个,在协商离职后,当时捉急找工作上班,因为有房贷供着,不能没有收入来源。所以匆忙选了一家公司,实际上是一个大型外包公司,主要派遣给其他手机厂商做外包项目。**当时承诺待遇还不错,所以就立马入职去上班了。但是后面入职后,发现薪酬待遇这块并不是HR所说那样,那个HR自...

女程序员,为什么比男程序员少???

昨天看到一档综艺节目,讨论了两个话题:(1)中国学生的数学成绩,平均下来看,会比国外好?为什么?(2)男生的数学成绩,平均下来看,会比女生好?为什么?同时,我又联想到了一个技术圈经常讨...

副业收入是我做程序媛的3倍,工作外的B面人生是怎样的?

提到“程序员”,多数人脑海里首先想到的大约是:为人木讷、薪水超高、工作枯燥…… 然而,当离开工作岗位,撕去层层标签,脱下“程序员”这身外套,有的人生动又有趣,马上展现出了完全不同的A/B面人生! 不论是简单的爱好,还是正经的副业,他们都干得同样出色。偶尔,还能和程序员的特质结合,产生奇妙的“化学反应”。 @Charlotte:平日素颜示人,周末美妆博主 大家都以为程序媛也个个不修边幅,但我们也许...

MySQL数据库面试题(2020最新版)

文章目录数据库基础知识为什么要使用数据库什么是SQL?什么是MySQL?数据库三大范式是什么mysql有关权限的表都有哪几个MySQL的binlog有有几种录入格式?分别有什么区别?数据类型mysql有哪些数据类型引擎MySQL存储引擎MyISAM与InnoDB区别MyISAM索引与InnoDB索引的区别?InnoDB引擎的4大特性存储引擎选择索引什么是索引?索引有哪些优缺点?索引使用场景(重点)...

如果你是老板,你会不会踢了这样的员工?

有个好朋友ZS,是技术总监,昨天问我:“有一个老下属,跟了我很多年,做事勤勤恳恳,主动性也很好。但随着公司的发展,他的进步速度,跟不上团队的步伐了,有点...

我入职阿里后,才知道原来简历这么写

私下里,有不少读者问我:“二哥,如何才能写出一份专业的技术简历呢?我总感觉自己写的简历太烂了,所以投了无数份,都石沉大海了。”说实话,我自己好多年没有写过简历了,但我认识的一个同行,他在阿里,给我说了一些他当年写简历的方法论,我感觉太牛逼了,实在是忍不住,就分享了出来,希望能够帮助到你。 01、简历的本质 作为简历的撰写者,你必须要搞清楚一点,简历的本质是什么,它就是为了来销售你的价值主张的。往深...

玩转springboot启动banner定义所得

最近接手了一个springboot项目,不是不熟悉这个框架,启动时打印的信息吸引了我。 这不是我熟悉的常用springboot的打印信息啊,我打开自己的项目: 还真是的,不用默认的感觉也挺高大上的。一时兴起,就去研究了一下源代码,还正是有些收获,稍后我会总结一下。正常情况下做为一个老程序员,是不会对这种小儿科感兴趣的,不就是一个控制台打印嘛。哈哈! 于是出于最初的好奇,研究了项目的源代码。看到

带了6个月的徒弟当了面试官,而身为高级工程师的我天天修Bug......

即将毕业的应届毕业生一枚,现在只拿到了两家offer,但最近听到一些消息,其中一个offer,我这个组据说客户很少,很有可能整组被裁掉。 想问大家: 如果我刚入职这个组就被裁了怎么办呢? 大家都是什么时候知道自己要被裁了的? 面试软技能指导: BQ/Project/Resume 试听内容: 除了刷题,还有哪些技能是拿到offer不可或缺的要素 如何提升面试软实力:简历, 行为面试,沟通能...

离职半年了,老东家又发 offer,回不回?

有小伙伴问松哥这个问题,他在上海某公司,在离职了几个月后,前公司的领导联系到他,希望他能够返聘回去,他很纠结要不要回去? 俗话说好马不吃回头草,但是这个小伙伴既然感到纠结了,我觉得至少说明了两个问题:1.曾经的公司还不错;2.现在的日子也不是很如意。否则应该就不会纠结了。 老实说,松哥之前也有过类似的经历,今天就来和小伙伴们聊聊回头草到底吃不吃。 首先一个基本观点,就是离职了也没必要和老东家弄的苦...

男生更看重女生的身材脸蛋,还是思想?

往往,我们看不进去大段大段的逻辑。深刻的哲理,往往短而精悍,一阵见血。问:产品经理挺漂亮的,有点心动,但不知道合不合得来。男生更看重女生的身材脸蛋,还是...

为什么程序员做外包会被瞧不起?

二哥,有个事想询问下您的意见,您觉得应届生值得去外包吗?公司虽然挺大的,中xx,但待遇感觉挺低,马上要报到,挺纠结的。

当HR压你价,说你只值7K,你该怎么回答?

当HR压你价,说你只值7K时,你可以流畅地回答,记住,是流畅,不能犹豫。 礼貌地说:“7K是吗?了解了。嗯~其实我对贵司的面试官印象很好。只不过,现在我的手头上已经有一份11K的offer。来面试,主要也是自己对贵司挺有兴趣的,所以过来看看……”(未完) 这段话主要是陪HR互诈的同时,从公司兴趣,公司职员印象上,都给予对方正面的肯定,既能提升HR的好感度,又能让谈判气氛融洽,为后面的发挥留足空间。...

面试:第十六章:Java中级开发(16k)

HashMap底层实现原理,红黑树,B+树,B树的结构原理 Spring的AOP和IOC是什么?它们常见的使用场景有哪些?Spring事务,事务的属性,传播行为,数据库隔离级别 Spring和SpringMVC,MyBatis以及SpringBoot的注解分别有哪些?SpringMVC的工作原理,SpringBoot框架的优点,MyBatis框架的优点 SpringCould组件有哪些,他们...

面试阿里p7,被按在地上摩擦,鬼知道我经历了什么?

面试阿里p7被问到的问题(当时我只知道第一个):@Conditional是做什么的?@Conditional多个条件是什么逻辑关系?条件判断在什么时候执...

终于懂了TCP和UDP协议区别

终于懂了TCP和UDP协议区别

你打算用Java 8一辈子都不打算升级到Java 14,真香

我们程序员应该抱着尝鲜、猎奇的心态,否则就容易固步自封,技术停滞不前。

无代码时代来临,程序员如何保住饭碗?

编程语言层出不穷,从最初的机器语言到如今2500种以上的高级语言,程序员们大呼“学到头秃”。程序员一边面临编程语言不断推陈出新,一边面临由于许多代码已存在,程序员编写新应用程序时存在重复“搬砖”的现象。 无代码/低代码编程应运而生。无代码/低代码是一种创建应用的方法,它可以让开发者使用最少的编码知识来快速开发应用程序。开发者通过图形界面中,可视化建模来组装和配置应用程序。这样一来,开发者直...

面试了一个 31 岁程序员,让我有所触动,30岁以上的程序员该何去何从?

最近面试了一个31岁8年经验的程序猿,让我有点感慨,大龄程序猿该何去何从。

大三实习生,字节跳动面经分享,已拿Offer

说实话,自己的算法,我一个不会,太难了吧

程序员垃圾简历长什么样?

已经连续五年参加大厂校招、社招的技术面试工作,简历看的不下于万份 这篇文章会用实例告诉你,什么是差的程序员简历! 疫情快要结束了,各个公司也都开始春招了,作为即将红遍大江南北的新晋UP主,那当然要为小伙伴们做点事(手动狗头)。 就在公众号里公开征简历,义务帮大家看,并一一点评。《启舰:春招在即,义务帮大家看看简历吧》 一石激起千层浪,三天收到两百多封简历。 花光了两个星期的所有空闲时...

《经典算法案例》01-08:如何使用质数设计扫雷(Minesweeper)游戏

我们都玩过Windows操作系统中的经典游戏扫雷(Minesweeper),如果把质数当作一颗雷,那么,表格中红色的数字哪些是雷(质数)?您能找出多少个呢?文中用列表的方式罗列了10000以内的自然数、质数(素数),6的倍数等,方便大家观察质数的分布规律及特性,以便对算法求解有指导意义。另外,判断质数是初学算法,理解算法重要性的一个非常好的案例。

立即提问
相关内容推荐