BASIC4MCU | 질문게시판 | stm32f767 초음파센서 질문
페이지 정보
작성자 이기심의긍정 작성일2019-06-10 01:10 조회9,884회 댓글1건본문
안녕하세요 stm32f767 키트 쓰는데 초음파센서로 거리측정하려고 하는데
초음파 센서는 알맞게 동작하는데 초음파의 에코값을 에이디컨버터에 넣어서 변환시킨다음에 adc 변환값의 상승,하강엣지 구간을 타이머로 재서 거리 측정을 하려고 하는데 동작을안합니다 ㅜㅜ 어디가 틀린 걸까요
#include "stm32f767xx.h"
#include "OK-STM767.h"
#include "OK-STM767_large.h"
void Delay_us(U32 time_us); // time delay for us in 216MHz
void Delay_ms(U32 time_ms);
int main(void)
{
float r;
unsigned echotime;
unsigned result;
Initialize_MCU(); // initialize MCU and kit
Delay_ms(50); // wait for system stabilization
Initialize_LCD(); // initialize text LCD module
Initialize_TFT_LCD();
RCC->AHB1ENR |= 0x00000001;
GPIOA->MODER &= 0xFFFF0CFF;
GPIOA->MODER |= 0x0001200; // use 포트4 10 포트6 출력모드 포트 7 입력모드 ///
RCC->APB1ENR |= 0x00000020; // 타이머 7 클락
RCC->APB2ENR |= 0x00000100; // enable ADC1 clock
ADC->CCR = 0x00000000;
ADC1->SMPR2 = 0x00001000; // 채널 4의 샘플링 주기 15 cycle
ADC1->CR1 = 0x00000000; // 12-bit 분해능
ADC1->CR2 = 0x00000001; // 오른쪽정렬, 단일변환!
ADC1->SQR1 = 0x00000000; // total regular channel number = 1/
TIM7->PSC = 1079; // 108MHz/(1079+1) = 100khz
TIM7->CNT = 0; // clear counter
TIM7->DIER = 0x0000; // enable update interrupt
TIM7->CR1 = 0x0005;
while(1)
{
void trigger_input(); // 12us트리거 펄스 입력
ADC1->SQR3 = 0x00000004; // channel 4 (+5.0V)
ADC1->CR2 |= 0x40000000; // start conversion by software
while(!(ADC1->SR & 0x00000002));// wait for end of conversion
result = ADC1->DR;
TFT_xy(24,7); // display voltage
TFT_color(Cyan,Black);
TFT_signed_float((float)result*2.5/4095., 1, 2);
if(result >1.6) //상승 엣지 되면 타이머 초기화 후 시작
{ TIM7->CNT = 0;
TIM7->CR1 = 0x0005;
}
if(result <1.0) //하강엣지 되면 타이머 종료하고 현재시간 값 저장후 초기화
{ TIM7->CR1 = 0x0000;
echotime = TIM7->CNT;
r=echotime*10/58;
TIM7->CNT = 0;
}
TFT_xy(24,9);
TFT_color(Cyan,Black);
TFT_signed_float((float)echotime, 4, 4);
TFT_xy(24,11);
TFT_color(Cyan,Black);
TFT_signed_float((float) r, 4, 4);
Delay_ms(250);
}
}
void trigger_input(void)
{
GPIOA->ODR = 0x00000040; //
Delay_us(12);
GPIOA->ODR = 0x00000000;
Delay_us(12);
}
댓글 1
조회수 9,884master님의 댓글
master 작성일
초음파센서의 에코 신호는 디지털 신호입니다.
디지털 신호를 아나로그 입력으로 받는 이유가 무엇일까요?
//
if(result>1.6){} //상승 엣지 되면
if(result<1.0){} //하강엣지 되면
이런 코드로는 상승엣지나 하강엣지를 검출하지 못합니다.
엣지 검출을 위해서는 변수가 추가되어야하며, 이전값과 현재값이 달라질 때를 체크해야합니다.