不得不说,STM32C031C6T6这个芯片很赞,在同价位芯片来说性能还是非常好的,而且编程方便,使用stm32CubeMx即可完成配置,完成了各个基本功能的测试。
在配置串口时,我使用了内部晶振,打开串口中断,实现PC端与开发板的通信功能。
#define LED_ON() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); #define LED_OFF() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); uint8_t Tx_star1[] = "hello world!\r\n"; uint8_t Tx_star2[] ="led open\r\n"; uint8_t Tx_star3[] ="led off\r\n"; uint8_t Rx_dat = 0; void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if(huart->Instance == USART2) { if(Rx_dat == 0xa1) { LED_ON(); HAL_UART_Transmit(&huart2,Tx_star2,sizeof(Tx_star2),10000); HAL_UART_Receive_IT(&huart2,&Rx_dat,1); } else if(Rx_dat == 0xa2) { LED_OFF(); HAL_UART_Transmit(&huart2,Tx_star3,sizeof(Tx_star3),10000); HAL_UART_Receive_IT(&huart2,&Rx_dat,1); } } } /* 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_USART2_UART_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ LED_ON(); HAL_UART_Transmit(&huart2,Tx_star1,sizeof(Tx_star1),10000); LED_OFF(); HAL_UART_Receive_IT(&huart2,&Rx_dat,1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }
本来想上云来着,但是本人技术问题,搞了很久还没连上,只能在后续补上;做了一个关于室内人数统计的小功能,可以实现屋里进出人数以及室内人数统计;首先须要OLED的移植,SPI通信的OLED非常好移植,只需要配置相应的管脚就好:
随后将中景园的代码移植更改,同样只需要更改引脚定义的函数就好:
void OLED_Init() { GPIO_InitTypeDef GPIO_InitStruct; __HAL_RCC_GPIOB_CLK_ENABLE(); //CS GPIO_InitStruct.Pin = OLED_CS_GPIO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(OLED_CS_GPIO, &GPIO_InitStruct); //DC GPIO_InitStruct.Pin = OLED_DC_GPIO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(OLED_DC_GPIO, &GPIO_InitStruct); //SCLK GPIO_InitStruct.Pin = OLED_SCLK_GPIO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(OLED_SCLK_GPIO, &GPIO_InitStruct); //SDIN GPIO_InitStruct.Pin = OLED_SDIN_GPIO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(OLED_SDIN_GPIO, &GPIO_InitStruct); HAL_GPIO_WritePin(OLED_CS_GPIO, OLED_CS_GPIO_PIN, GPIO_PIN_SET); HAL_GPIO_WritePin(OLED_DC_GPIO, OLED_DC_GPIO_PIN, GPIO_PIN_SET); HAL_GPIO_WritePin(OLED_SCLK_GPIO, OLED_SCLK_GPIO_PIN, GPIO_PIN_SET); HAL_GPIO_WritePin(OLED_SDIN_GPIO, OLED_SDIN_GPIO_PIN, GPIO_PIN_SET); //RESET THE OLED #if 0 OLED_RST_Set(); delay_ms(100); OLED_RST_Clr(); delay_ms(100); OLED_RST_Set(); #endif OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel OLED_WR_Byte(0x02,OLED_CMD);//---set low column address OLED_WR_Byte(0x10,OLED_CMD);//---set high column address OLED_WR_Byte(0x40,OLED_CMD);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F) OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常 OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常 OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64) OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F) OLED_WR_Byte(0x00,OLED_CMD);//-not offset OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration OLED_WR_Byte(0x12,OLED_CMD); OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02) OLED_WR_Byte(0x02,OLED_CMD);// OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5) OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7) OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/ OLED_Clear(); OLED_Set_Pos(0,0); }
配合两个光电开关就可以实现进入和离开室内的功能判断了,然后就是代码中最核心的逻辑代码:
void READ_PRO() { if(in_sign==1) { enter_flag = 1; } if((enter_flag==1)&&(out_sign==1))//进入 { while(out_sign==1); enter_flag=0; also_have++; enter_namber++; } if(out_sign == 1) { out_flag = 1; } if((out_flag==1)&&(in_sign==1))//离开 { while(in_sign==1); out_flag = 0; also_have--; out_number++; } }
将所有的数据显示在OLED上:
void oled_display() { OLED_ShowCHinese(0,0,0);//进 OLED_ShowCHinese(18,0,1);//入 OLED_ShowCHinese(0,3,2);//离 OLED_ShowCHinese(18,3,3);//开 OLED_ShowCHinese(0,6,4);//室 OLED_ShowCHinese(18,6,5);//内 OLED_ShowNum(36,0,enter_namber,3,16); OLED_ShowNum(36,3,out_number,3,16); OLED_ShowNum(36,6,also_have,3,16); }
这个功能可以统计室内人数,以及进出入的总次数,至于连接云端,后期会继续更新的。