#include "stm32f4xx_conf.h"
#include "math.h"
#include "stdio.h"
#include "stdlib.h"
#define crystal 168
#define ECHO GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15)
#define TRIGER_SET GPIO_WriteBit(GPIOA, GPIO_Pin_14, 1)
#define TRIGER_RESET GPIO_WriteBit(GPIOA, GPIO_Pin_14, 0)
unsigned int count=0;
unsigned char jrk;
unsigned char jrk_dpn;
unsigned char kata1[16];
unsigned char kata2[16];
void delay_ms(unsigned int waktu)
{
unsigned int a, b, c;
c=(float)crystal*45.4876273654;
for(a=0;a<waktu;a++)
{
for(b=0;b<c;b++);
}
}
void delay_us(unsigned int waktu)
{
unsigned int a, b;
b=(float)crystal*0.03472222222222222;
waktu=(float)waktu*b;
for(a=0;a<waktu;a++)
{
for(b=0;b<crystal;b++);
}
}
void inisialisasi_GPIO()
{
GPIO_InitTypeDef definisi_GPIO;
definisi_GPIO.GPIO_Pin = GPIO_Pin_14;
definisi_GPIO.GPIO_Mode = GPIO_Mode_OUT;
definisi_GPIO.GPIO_OType = GPIO_OType_PP;
definisi_GPIO.GPIO_PuPd = GPIO_PuPd_NOPULL;
definisi_GPIO.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &definisi_GPIO);
definisi_GPIO.GPIO_Pin = GPIO_Pin_15;
definisi_GPIO.GPIO_Mode = GPIO_Mode_IN;
definisi_GPIO.GPIO_PuPd = GPIO_PuPd_UP;
definisi_GPIO.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &definisi_GPIO);
}
unsigned char baca_srf04()
{
count=0;
//pemberian pemicu berupa pulsa positif minimal 10us utk aktivasi sensor
TRIGER_SET;
delay_ms(10);
TRIGER_RESET;
//menunggu hingga ECHO = 1
while(ECHO==0){};
//mencacah untuk mengukur waktu lamanya pulsa 1 dari ECHO
while(ECHO==1)
{
count++;
};
//mengkonversikan hasil cacahan menjadi jarak dalam cm
jrk=count*10/232;
return jrk;
}
int srf=0;
int main(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
inisialisasi_GPIO();
while (1)
{
srf=baca_srf04();
}
}
Home
»
»Unlabelled
» Read SRF04 with STM32F4 discovery
Subscribe to:
Post Comments (Atom)
0 komentar:
Post a Comment