nikoniko
Published © GPL3+

Ultrasonic module test with STONE lcd Module

Ultrasonic module test with STONE lcd Module

Full instructions provided397
Ultrasonic module test with STONE lcd Module

Things used in this project

Hardware components

STONE TOOLBox
×1
STONE HMI touch screen
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Story

Read more

Code

Code snippet #1

Plain text
void EXTI9_5_IRQHandler(void){
	static u8 flag_Sta = 0;
	if(EXTI_GetITStatus(EXTI_Line5) != RESET)		
	{
		EXTI_ClearITPendingBit(EXTI_Line5);  
		if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==1)
		{
			 TIM_SetCounter(TIM6,0);	
			 flag_Sta=1;
			 TIM_Cmd(TIM6, ENABLE);     
		}
		else
		{
			TIM_Cmd(TIM6, DISABLE); 					
			if(flag_Sta)
			{		
				Distance = TIM_GetCounter(TIM6);
				Distance = Distance /29;
				if(Distance > 300)
					Distance = 300;
				Done = 1;
			}
			flag_Sta=0;
		}
	}
}

Code snippet #2

Plain text
#include "usart1.h"#include "Sonic.h"
#include "SysTick.h"
 
#define DISPLYER_ADDR           0x05
#define CHANGE_TIME_ADDR        0x10
u8 data_send[8]=  {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x00, 0x00,0x00};
extern u16  Distance;
extern u8  USART_RX_BUF[10];
extern u8 USART_RX_END;
extern u16 USART_RX_STA;
void UART1_Send_Array(u8 send_array[],unsigned char num)
{
        u8 i=0;  
        while(i=time_set)
		{
			time=0;
			data_send[5]=DISPLYER_ADDR;
			data_send[6] = Distance >> 8;//hight
			data_send[7] = Distance & 0x00ff;//low
			UART1_Send_Array(data_send,8);
		//	printf("The Distance is: %d centimetre\r\n",Distance);
		}
		if(USART_RX_END)
		{		
		 // UART1_Send_Array(USART_RX_BUF,8);
			switch (USART_RX_BUF[5])	
			{
				case CHANGE_TIME_ADDR:
					time_set=USART_RX_BUF[7]*10;
					break;
				default:
					USART_RX_END=0;
					USART_RX_STA=0;
					break;
			}
					USART_RX_END=0;
					USART_RX_STA=0;
		}
	}
}

Code snippet #3

Plain text
#include "Sonic.h"/*******************************************************************************
*	Sonic  Init
*******************************************************************************/
u32  Distance = 0;
u8   Done;
u32 __IO time_1ms = 0;
 
void TIM6_Init(void)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  //NVIC_InitTypeDef 		   NVIC_InitStructure;
  /* TIM6 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 0xFFFF; 
	TIM_TimeBaseStructure.TIM_Prescaler = 142;          //144£¬500K
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 
  TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
  TIM_ITConfig(TIM6, TIM_IT_Update, DISABLE);	
	TIM_Cmd(TIM6, DISABLE);
}
 
void Sonic_Init(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	EXTI_InitTypeDef EXTI_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC| RCC_APB2Periph_AFIO,ENABLE);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;       
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;						//PC4 Trig
	GPIO_Init(GPIOC,&GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IPD;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;           		//PC5 Echo
	GPIO_Init(GPIOC,&GPIO_InitStructure);   
	
	GPIO_WriteBit(GPIOC,GPIO_Pin_4,(BitAction)0); 			//trig
	
	//EXTI_DeInit();
	EXTI_ClearITPendingBit(EXTI_Line5);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource5);
	EXTI_InitStructure.EXTI_Line= EXTI_Line5; 
	EXTI_InitStructure.EXTI_Mode= EXTI_Mode_Interrupt; 
	EXTI_InitStructure.EXTI_Trigger= EXTI_Trigger_Rising_Falling;
	EXTI_InitStructure.EXTI_LineCmd=ENABLE;
  EXTI_Init(&EXTI_InitStructure);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority= 0;        
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;   
	NVIC_Init(&NVIC_InitStructure);
	
	Distance = 0;
	Done = 1;
}
 
void Sonic_Trig(void)
{
	u16 i = 0;
	if((Done == 1)&&(time_1ms > 100))
	{
		time_1ms = 0;
		GPIO_WriteBit(GPIOC,GPIO_Pin_4,(BitAction)1);
		for(i=0;i<0xf0;i++);
		GPIO_WriteBit(GPIOC,GPIO_Pin_4,(BitAction)0); 
		Done = 0;
	}
}
 
void EXTI9_5_IRQHandler(void)
{
	static u8 flag_Sta = 0;
	if(EXTI_GetITStatus(EXTI_Line5) != RESET)		
	{
		EXTI_ClearITPendingBit(EXTI_Line5);  
		if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==1)
		{
			 TIM_SetCounter(TIM6,0);	
			 flag_Sta=1;
			 TIM_Cmd(TIM6, ENABLE);     
		}
		else
		{
			TIM_Cmd(TIM6, DISABLE); 					
			if(flag_Sta)
			{		
				Distance = TIM_GetCounter(TIM6);
				Distance = Distance /29;
				if(Distance > 300)
					Distance = 300;
				Done = 1;
			}
			flag_Sta=0;
		}
	}
}
 
/*******************      (C) COPYRIGHT 2017       *END OF FILE************/

Code snippet #4

Plain text
#include "usart1.h" 
 
void USART1_Config(uint32_t uBaud)
{
	USART1_Configuration(uBaud);
	USART1_NVIC_Configuration();
}
 
 
 
void USART1_Configuration(uint32_t uBaud)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	
	/* config USART1 clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
	
	/* USART1 GPIO config */
	/* Configure USART1 Tx (PA.09) as alternate function push-pull */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);    
	/* Configure USART1 Rx (PA.10) as input floating */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	  
	/* USART1 mode config */
	USART_InitStructure.USART_BaudRate = uBaud;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No ;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART1, &USART_InitStructure); 
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
	USART_Cmd(USART1, ENABLE);
}
void USART1_NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure; 
	/* Configure the NVIC Preemption Priority Bits */  
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
	
	/* Enable the USARTy Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;	 
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}
 
int fputc(int ch, FILE *f)
{
	USART_SendData(USART1, (unsigned char) ch);
	while (!(USART1->SR & USART_FLAG_TXE));
	//while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);	
	return (ch);
}
u16 USART_RX_STA=0;
u8 USART_RX_END=0;
u8 USART_RX_BUF[10]; 
void USART1_IRQHandler(void)
{  
	u8 Res;	
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
	{ 
		Res =USART_ReceiveData(USART1);
		  //printf("%x",USART_ReceiveData(USART1));
			// USART_SendData(USART1,Res);  
			if(USART_RX_END==0)
			{
				USART_RX_BUF[USART_RX_STA]=Res ;
				USART_RX_STA++;
				if(USART_RX_STA>=8)
				{
					USART_RX_END=1;
				}
			}
	}
}
Sys_tick
void SysTick_Handler(void)
{
	time_1ms++;
	time_120ms++;
	if(time_120ms>=80)
	{
		Sonic_Trig();				//50ms Trig Sonic
		time_120ms=0;
	}
	TimingDelay_Decrement();
}

Credits

nikoniko

nikoniko

5 projects • 2 followers

Comments