技能梳理38@stm32+CC2530+超声波+光敏+oled
1、项目简介
2、实现逻辑
#主从机使用zigbee通信
#主机oled显示当前停车场位置图
#从机通过超声波和光敏一起检测是否有车
#当某位置车辆进出,在主机上oled进行提示
3、应用场景
#车辆无线检测
#货架商品有无检测
4、核心代码梳理
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 */
char buf[256];
uint16_t send_flag = 1;
/* 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_USART1_UART_Init();
MX_I2C1_Init();
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
#ifdef MASTER
__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);//open uart1 RXNE
OLED_Init();
OLED_Clear();
//oled_print(0, 0, "hello"); //first 衿 64是半个屏 second 刿 2是一个错位间跿
OLED_ShowString(0,0,"---------------",16);
OLED_ShowString(0,2,"|____|____|____|",16);
OLED_ShowString(0,4,"| | | |",16);
OLED_ShowString(0,6,"---------------",16);
//oled_print(20, 4, "*");
//oled_print(100, 4, "*");
#endif
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_Delay(500);
#ifdef SLAVE_CHECK1
if ((HAL_GPIO_ReadPin(light_GPIO_Port, light_Pin) == GPIO_PIN_RESET) || (HC_SR04_Measure() > 20)) //无车 //
{
if(buf[1] == '1')
{
send_flag = 1;
HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_SET);
}
sprintf(buf, "10");
}
else
{
if(buf[1] == '0')
{
send_flag = 1;
HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_RESET);
}
sprintf(buf, "11");
}
if(send_flag == 1)
{
send_flag = 0;
HAL_UART_Transmit(&huart1, (uint8_t*)buf, strlen(buf), HAL_MAX_DELAY);
}
#endif
#ifdef SLAVE_CHECK2
if ((HAL_GPIO_ReadPin(light_GPIO_Port, light_Pin) == GPIO_PIN_RESET) || (HC_SR04_Measure() > 20)) //无车 //
{
if(buf[1] == '1')
{
send_flag = 1;
HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_SET);
}
sprintf(buf, "20");
}
else
{
if(buf[1] == '0')
{
send_flag = 1;
HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_RESET);
}
sprintf(buf, "21");
}
if(send_flag == 1)
{
send_flag = 0;
HAL_UART_Transmit(&huart1, (uint8_t*)buf, strlen(buf), HAL_MAX_DELAY);
}
#endif
#ifdef MASTER
if(rx_ok)
{
rx_ok = 0;
if(rx_order1 == 1) //YES
{
oled_print(20, 4, "*");
}
else
{
oled_print(20, 4, " ");
}
if(rx_order2 == 1) //YES
{
oled_print(100, 4, "*");
}
else
{
oled_print(100, 4, " ");
}
}
#endif
}
/* USER CODE END 3 */
}
void RCC_delay_us(uint32_t udelay)
{
uint32_t i=0;
for(i=0;i<udelay;i++){
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
}
}
static void HC_SR04_Start()
{
/* output high level */
HAL_GPIO_WritePin(TRIG_GPIO_Port, TRIG_Pin, GPIO_PIN_SET);
/* maintain high level at least 10us */
RCC_delay_us(10);
/* resume low level */
HAL_GPIO_WritePin(TRIG_GPIO_Port, TRIG_Pin, GPIO_PIN_RESET);
}
//return cm
double HC_SR04_Measure()
{
uint32_t tick_us;
HC_SR04_Start();
__HAL_TIM_SetCounter(&htim2, 0);
/* waitting for start of the high level through echo pin */
while (HAL_GPIO_ReadPin(ECHO_GPIO_Port, ECHO_Pin) == GPIO_PIN_RESET);
/* start the tim and enable the interrupt */
HAL_TIM_Base_Start(&htim2);
/* waitting for end of the high level through echo pin */
while (HAL_GPIO_ReadPin(ECHO_GPIO_Port, ECHO_Pin) == GPIO_PIN_SET);
/* stop the tim */
HAL_TIM_Base_Stop(&htim2);
/* get the time of high level */
tick_us = __HAL_TIM_GetCounter(&htim2);
/* calc distance in unit cm */
double dis = (double)(tick_us/1000000.0) * 340.0 / 2.0 *100.0;
return dis;
}
void Delay(__IO uint32_t nCount) {
while(nCount--) {
}
}
//oled display
void oled_print(uint8_t x, uint8_t y, char *str)
{
OLED_ShowString(x,y,str,16);
}
5、部分参考资料
6、注意事项
#zigbee模块买的一家现成的模块,比较便宜十几块钱
#zigbee需要一个协调器和两个路由器
#原理图上画出了zigbee即cc2530的详细设计图
#oled上有车时,对应位置使用“*”指代
#主从机程序写到一个工程文件中了,使用宏定义进行区分
视频讲解B站地址
完整可运行项目地址(¥5.9)
或 点击下方卡片,关注回复"停车场"免费下载