二楼代码:- /******************************************************************************/
- /** \file main.c
- **
- ** A detailed description is available at
- ** [url=home.php?mod=space&uid=48136]@link[/url] Sample Group Some description @endlink
- **
- ** - 2021-03-12 1.0 xiebin First version for Device Driver Library of Module.
- **
- ******************************************************************************/
- /*******************************************************************************
- *
- * 代码许可和免责信息
- * 武汉芯源半导体有限公司授予您使用所有编程代码示例的非专属的版权许可,您可以由此
- * 生成根据您的特定需要而定制的相似功能。根据不能被排除的任何法定保证,武汉芯源半
- * 导体有限公司及其程序开发商和供应商对程序或技术支持(如果有)不提供任何明示或暗
- * 含的保证或条件,包括但不限于暗含的有关适销性、适用于某种特定用途和非侵权的保证
- * 或条件。
- * 无论何种情形,武汉芯源半导体有限公司及其程序开发商或供应商均不对下列各项负责,
- * 即使被告知其发生的可能性时,也是如此:数据的丢失或损坏;直接的、特别的、附带的
- * 或间接的损害,或任何后果性经济损害;或利润、业务、收入、商誉或预期可节省金额的
- * 损失。
- * 某些司法辖区不允许对直接的、附带的或后果性的损害有任何的排除或限制,因此某些或
- * 全部上述排除或限制可能并不适用于您。
- *
- *******************************************************************************/
- /******************************************************************************
- * Include files
- ******************************************************************************/
- #include "main.h"
- /******************************************************************************
- * Local pre-processor symbols/macros ('#define')
- ******************************************************************************/
- #define I2C_SCL_GPIO_PORT CW_GPIOB
- #define I2C_SCL_GPIO_PIN GPIO_PIN_4 //如果改动口线则GPIO初始化代码需要做同步修改
- #define I2C_SDA_GPIO_PORT CW_GPIOB
- #define I2C_SDA_GPIO_PIN GPIO_PIN_3 //如果改动口线则GPIO初始化代码需要做同步修改
- /******************************************************************************
- * Global variable definitions (declared in header file with 'extern')
- ******************************************************************************/
- /******************************************************************************
- * Local type definitions ('typedef')
- ******************************************************************************/
- /******************************************************************************
- * Local function prototypes ('static')
- ******************************************************************************/
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void NVIC_Configuration(void);
- void GenerateTestSignal(void);
- /******************************************************************************
- * Local variable definitions ('static') *
- ******************************************************************************/
- /******************************************************************************
- * Local pre-processor symbols/macros ('#define')
- ******************************************************************************/
- /*****************************************************************************
- * Function implementation - global ('extern') and local ('static')
- ******************************************************************************/
- typedef enum
- {
- POWER_OFF_CMD = 0x00, //断电:无激活状态
- POWER_ON_CMD = 0x01, //通电:等待测量指令
- RESET_REGISTER = 0x07, //重置数字寄存器(在断电状态下不起作用)
- CONT_H_MODE = 0x10, //连续H分辨率模式:在11x分辨率下开始测量,测量时间120ms
- CONT_H_MODE2 = 0x11, //连续H分辨率模式2:在0.51x分辨率下开始测量,测量时间120ms
- CONT_L_MODE = 0x13, //连续L分辨率模式:在411分辨率下开始测量,测量时间16ms
- ONCE_H_MODE = 0x20, //一次高分辨率模式:在11x分辨率下开始测量,测量时间120ms,测量后自动设置为断电模式
- ONCE_H_MODE2 = 0x21, //一次高分辨率模式2:在0.51x分辨率下开始测量,测量时间120ms,测量后自动设置为断电模式
- ONCE_L_MODE = 0x23 //一次低分辨率模式:在411x分辨率下开始测量,测量时间16ms,测量后自动设置为断电模式
- } BH1750_MODE;
- void BH1750_Init(BH1750_MODE mode)
- {
- uint8_t data;
- data = POWER_ON_CMD; // 发送启动命令
- I2C_MasterSendDataToSlave(&data,1);
- data = mode; // 设置分辨率模式
- I2C_MasterSendDataToSlave(&data,1);
- }
- uint16_t BH1750_Read(void)
- {
- uint8_t data[2];
- I2C_MasterRecDataFromSlave(data,2);
- return ((uint16_t)data[0] << 8 | data[1]);
- }
- extern void delay1ms(uint32_t u32Cnt);
- /**
- ******************************************************************************
- ** \brief Main function of project
- **
- ** \return uint32_t return value, if needed
- **
- ** This sample toggle GPIOA.00
- **
- ******************************************************************************/
- int32_t main(void)
- {
- ATIM_InitTypeDef ATIM_InitStruct;
- ATIM_OCInitTypeDef ATIM_OCInitStruct;
- /* System Clocks Configuration */
- RCC_Configuration();
- /* GPIO Configuration */
- GPIO_Configuration();
- /* NVIC Configuration */
- //NVIC_Configuration();
- LogInit();
- printf("ATIM Initial...\n");
- ATIM_InitStruct.BufferState = DISABLE;
- ATIM_InitStruct.ClockSelect = ATIM_CLOCK_PCLK;
- ATIM_InitStruct.CounterAlignedMode = ATIM_COUNT_MODE_EDGE_ALIGN;
- ATIM_InitStruct.CounterDirection = ATIM_COUNTING_UP;
- ATIM_InitStruct.CounterOPMode = ATIM_OP_MODE_REPETITIVE;
- ATIM_InitStruct.OverFlowMask = DISABLE;
- ATIM_InitStruct.Prescaler = ATIM_Prescaler_DIV1; // 计数时钟8MHz
- ATIM_InitStruct.ReloadValue = 8191; // 溢出周期1.024ms
- ATIM_InitStruct.RepetitionCounter = 0;
- ATIM_InitStruct.UnderFlowMask = DISABLE;
- ATIM_Init(&ATIM_InitStruct);
- ATIM_OCInitStruct.BufferState = ENABLE;
- ATIM_OCInitStruct.OCInterruptSelect = ATIM_OC_IT_UP_COUNTER;
- ATIM_OCInitStruct.OCInterruptState = ENABLE;
- ATIM_OCInitStruct.OCMode = ATIM_OCMODE_PWM1;
- ATIM_OCInitStruct.OCPolarity = ATIM_OCPOLARITY_NONINVERT;
- ATIM_OC2BInit(&ATIM_OCInitStruct);
- //ATIM_ITConfig(ATIM_CR_IT_OVE, ENABLE);
- //ATIM_CH2Config(ATIM_CHxB_CIE, ENABLE);
- ATIM_SetCompare2B(0);
- ATIM_Cmd(ENABLE);
- ATIM_CtrlPWMOutputs(ENABLE);
- printf("ATIM is running.\n");
- //I2C初始化
- I2C_InitTypeDef I2C_InitStruct;
- I2C_InitStruct.I2C_BaudEn = ENABLE;
- I2C_InitStruct.I2C_Baud = 0x01; //500K=(8000000/(8*(1+1))
- I2C_InitStruct.I2C_FLT = DISABLE;
- I2C_InitStruct.I2C_AA = DISABLE;
-
- I2C_DeInit();
- I2C_Master_Init(&I2C_InitStruct);//初始化模块
- I2C_Cmd(ENABLE); //模块使能
-
- PA07_SETHIGH();
- PA07_SETLOW();
-
- while(1)
- {
- delay1ms(25);
- uint16_t Illuminance = BH1750_Read()*5/6;
- ATIM_SetCompare2B(Illuminance * 12);
- BH1750_Init(ONCE_L_MODE);
-
- static uint16_t cnt = 0;
- if(++cnt >= 40){
- cnt = 0;
- //PA07_TOG();
- printf("Illuminance = \n%d", Illuminance);
- }
- }
- }
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] Configures the different system clocks.
- * @param None
- * @retval None
- */
- void RCC_Configuration(void)
- {
- RCC_HSI_Enable(RCC_HSIOSC_DIV6);
- __RCC_ATIM_CLK_ENABLE();
- __RCC_I2C_CLK_ENABLE();
- }
- /**
- * @brief Configure the GPIO Pins.
- * @param None
- * @retval None
- */
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- __RCC_GPIOA_CLK_ENABLE();
- __RCC_GPIOB_CLK_ENABLE();
-
- //PA6
- GPIO_InitStruct.IT = GPIO_IT_NONE;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pins = GPIO_PIN_6;
- PA06_AFx_ATIMCH2B();
- GPIO_Init(CW_GPIOA, &GPIO_InitStruct);
-
- //set output
- GPIO_InitStruct.Pins = GPIO_PIN_0 | GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_Init( CW_GPIOA , &GPIO_InitStruct);
-
- //I2C
- PB03_AFx_I2CSDA();
- PB04_AFx_I2CSCL();
- GPIO_InitStruct.Pins = I2C_SCL_GPIO_PIN | I2C_SDA_GPIO_PIN;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
- GPIO_Init(I2C_SCL_GPIO_PORT, &GPIO_InitStruct);
- }
- /**
- * @brief Configure the nested vectored interrupt controller.
- * @param None
- * @retval None
- */
- void NVIC_Configuration(void)
- {
- __disable_irq();
- NVIC_EnableIRQ(ATIM_IRQn);
- __enable_irq();
- }
- /******************************************************************************
- * EOF (not truncated)
- ******************************************************************************/
- #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 */
|