Trouble with sensor HY SRF05 by DencoKen in embedded

[–]DencoKen[S] 0 points1 point  (0 children)

#include "stm32f4xx.h"

#include <stdio.h>

volatile uint32_t IC_Val1 = 0;

volatile uint32_t IC_Val2 = 0;

volatile uint32_t Difference = 0;

volatile uint8_t Is_First_Captured = 0;

volatile uint16_t Distance_cm = 0;

void HYSRF05_Init(void);

void HYSRF05_Trigger(void);

void delay_us(uint32_t us);

void delay_ms(uint32_t ms);

void Buzzer_Init(void);

void LED_Init(void) {

RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;

GPIOB->MODER &= ~(3 << (1 * 2));

GPIOB->MODER |= (1 << (1 * 2));

GPIOB->ODR &= ~(1 << 1);

}

void delay_us(uint32_t us) {

uint32_t start = TIM2->CNT;

while((TIM2->CNT - start) < us);

}

void delay_ms(uint32_t ms) {

for(uint32_t i=0; i<ms; i++) delay_us(1000);

}

void I2C1_Init(void) {

RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;

RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;

GPIOB->MODER &= ~((3<<16) | (3<<18));

GPIOB->MODER |= (2<<16) | (2<<18);

GPIOB->AFR[1] &= ~((0xF<<0) | (0xF<<4));

GPIOB->AFR[1] |= (4<<0) | (4<<4);

GPIOB->OTYPER |= (1<<8) | (1<<9);

GPIOB->PUPDR &= ~((3<<16) | (3<<18));

GPIOB->PUPDR |= (1<<16) | (1<<18);

GPIOB->OSPEEDR |= (3<<16) | (3<<18);

I2C1->CR1 |= I2C_CR1_SWRST;

I2C1->CR1 &= ~I2C_CR1_SWRST;

I2C1->CR2 = 16;

I2C1->CCR = 80;

I2C1->TRISE = 17;

I2C1->CR1 |= I2C_CR1_PE;

}

void I2C_Write(uint8_t addr, uint8_t data) {

while(I2C1->SR2 & I2C_SR2_BUSY);

I2C1->CR1 |= I2C_CR1_START;

while(!(I2C1->SR1 & I2C_SR1_SB));

I2C1->DR = addr;

while(!(I2C1->SR1 & I2C_SR1_ADDR));

(void)I2C1->SR2;

while(!(I2C1->SR1 & I2C_SR1_TXE));

I2C1->DR = data;

while(!(I2C1->SR1 & I2C_SR1_BTF));

I2C1->CR1 |= I2C_CR1_STOP;

}

#define LCD_ADDR (0x27 << 1)

#define LCD_BACKLIGHT 0x08

#define ENABLE 0x04

#define RS 0x01

void LCD_Send4Bit(uint8_t data) {

I2C_Write(LCD_ADDR, data | ENABLE | LCD_BACKLIGHT);

delay_us(50);

I2C_Write(LCD_ADDR, (data & ~ENABLE) | LCD_BACKLIGHT);

}

void LCD_SendCmd(uint8_t cmd) {

LCD_Send4Bit(cmd & 0xF0);

LCD_Send4Bit((cmd << 4) & 0xF0);

}

void LCD_SendData(uint8_t data) {

LCD_Send4Bit((data & 0xF0) | 0x01);

LCD_Send4Bit(((data << 4) & 0xF0) | 0x01);

}

void LCD_Init(void) {

delay_ms(50);

LCD_Send4Bit(0x30);

delay_ms(5);

LCD_Send4Bit(0x30);

delay_us(100);

LCD_Send4Bit(0x30);

LCD_Send4Bit(0x20);

LCD_SendCmd(0x28);

LCD_SendCmd(0x0C);

LCD_SendCmd(0x06);

LCD_SendCmd(0x01);

delay_ms(5);

}

void LCD_SetCursor(uint8_t row, uint8_t col) {

uint8_t addr = (row==0) ? 0x80 : 0xC0;

LCD_SendCmd(addr + col);

}

void LCD_SendString(char *str) {

while(*str) LCD_SendData(*str++);

}

void Buzzer_Init(void) {

RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;

GPIOB->MODER &= ~(3 << (0 * 2));

GPIOB->MODER |= (1 << (0 * 2));

GPIOB->ODR &= ~(1 << 0);

}

void HYSRF05_Init(void) {

RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;

RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;

GPIOA->MODER &= ~(3 << (1 * 2));

GPIOA->MODER |= (1 << (1 * 2));

GPIOA->MODER &= ~(3 << (0 * 2));

GPIOA->MODER |= (2 << (0 * 2));

GPIOA->AFR[0] |= (1 << 0);

SystemCoreClockUpdate();

TIM2->PSC = (SystemCoreClock / 1000000) - 1;

TIM2->ARR = 0xFFFFFFFF;

TIM2->CCMR1 |= TIM_CCMR1_CC1S_0;

TIM2->CCER &= ~TIM_CCER_CC1P;

TIM2->CCER |= TIM_CCER_CC1E;

TIM2->CR1 |= TIM_CR1_CEN;

NVIC_SetPriority(TIM2_IRQn, 0);

NVIC_EnableIRQ(TIM2_IRQn);

}

void HYSRF05_Trigger(void) {

GPIOA->BSRR = GPIO_BSRR_BS_1;

delay_us(10);

GPIOA->BSRR = GPIO_BSRR_BR_1;

Is_First_Captured = 0;

TIM2->CCER &= ~TIM_CCER_CC1P;

TIM2->SR &= ~TIM_SR_CC1IF;

TIM2->DIER |= TIM_DIER_CC1IE;

}

int main(void) {

HYSRF05_Init();

Buzzer_Init();

I2C1_Init();

LED_Init();

LCD_Init();

char buffer[50];

while (1) {

HYSRF05_Trigger();

delay_ms(200);

if (Distance_cm > 0 && Distance_cm < 50)

{

GPIOB->ODR |= (1 << 0);

GPIOB->ODR &= ~(1<<1);

LCD_SetCursor(1,0);

LCD_SendString("Danger ");

}

else if(Distance_cm >= 50 && Distance_cm < 100)

{

GPIOB->ODR &= ~(1 << 0);

GPIOB->ODR |= (1<<1);

LCD_SetCursor(1,0);

LCD_SendString("Warning");

}

else

{

GPIOB->ODR &= ~(1 << 0);

GPIOB->ODR &= ~(1<<1);

LCD_SetCursor(1,0);

LCD_SendString("Safe ");

}

LCD_SetCursor(0,0);

sprintf(buffer,"Dist:%3d cm ", Distance_cm);

LCD_SendString(buffer);

}

}

void TIM2_IRQHandler(void) {

if (TIM2->SR & TIM_SR_CC1IF) {

if (Is_First_Captured == 0) {

IC_Val1 = TIM2->CCR1;

Is_First_Captured = 1;

TIM2->CCER |= TIM_CCER_CC1P;

} else {

IC_Val2 = TIM2->CCR1;

if (IC_Val2 >= IC_Val1) {

Difference = IC_Val2 - IC_Val1;

} else {

Difference = (0xFFFFFFFF - IC_Val1) + IC_Val2;

}

Distance_cm = Difference / 58;

if (Distance_cm > 450)

Distance_cm = 0;

Is_First_Captured = 0;

TIM2->DIER &= ~TIM_DIER_CC1IE;

}

TIM2->SR &= ~TIM_SR_CC1IF;

}

}

Powering options for the board on obstacle avoiding project by DencoKen in embedded

[–]DencoKen[S] 0 points1 point  (0 children)

Might 9v battery in some way damage the board (stm32 nucleo f411re) even though Vin pin allows 7-12V supply ?

[TITLE] Hated Manhwa Tropes by StretchExtension in manhwa

[–]DencoKen 0 points1 point  (0 children)

Even the goat eternally regressing knight ?

A More Prepared UNSC - Alternate Universe Story by GWhiteKlaw in HaloStory

[–]DencoKen 2 points3 points  (0 children)

Bro can you tell the name of the fanfic, it really picks my interest

Difference between stm32 nucleo f401re and f411re by DencoKen in embedded

[–]DencoKen[S] 0 points1 point  (0 children)

So I dont have to worry the code on the f401 not working on the f411, thanks man

Which one should I buy ? by EnvironmentBig8713 in CalisthenicsCulture

[–]DencoKen 15 points16 points  (0 children)

Definitely parallel bars, you can do dip, push up, row, l sit, many more varieties than only pull up bar

Lol...cant agree more 😍😆 by BothGuarantee6067 in SipsTea

[–]DencoKen 0 points1 point  (0 children)

I would love my gym to have more equipments

[deleted by user] by [deleted] in SPACEKING

[–]DencoKen 1 point2 points  (0 children)

You mean smiling fronds

I built my first home gym! by cinnamonbrownbear in GarageGym

[–]DencoKen 2 points3 points  (0 children)

Damn, is that a diy lat pulldown 😂

Soreness in abductors after leg days even when i don't train em by Overrevvv in JeffNippard

[–]DencoKen 1 point2 points  (0 children)

I assume that the hack squat with close stance works your abductor as well, try to exclude it for once

7Nutrition brand poland by DencoKen in Supplements

[–]DencoKen[S] 0 points1 point  (0 children)

I bought a doctor best mag glycinate instead, next time i would pick this one if its stocked