-
Notifications
You must be signed in to change notification settings - Fork 4
Adding steven's ussensor code #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jvasquez864
wants to merge
1
commit into
master
Choose a base branch
from
USSensorCleaned
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #ifndef U_SENSOR_H | ||
| #define U_SENSOR_H 1 | ||
|
|
||
| #include "stm32f3xx_hal.h" | ||
| #include "hal_common_includes.h" | ||
|
|
||
| //Initialize ultrasound sensor gpio as needed for the board we are using. | ||
| void usensor_gpio_init(void); | ||
|
|
||
| //Simple software delay microseconds. | ||
| void delay_us(uint32_t n); | ||
|
|
||
| //Measures a pulse in microseconds. | ||
| long pulse_in(GPIO_TypeDef* echo_port); | ||
|
|
||
| //Returns the distance from object. | ||
| int get_sensor_dist(GPIO_TypeDef* trig_port, GPIO_TypeDef* echo_port, uint16_t trigger); | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| /* | ||
| This is the main user software application | ||
| In this case, we have below the code necessary to set up and control usensor. | ||
|
|
||
| */ | ||
| #include "stm32f3xx_hal.h" | ||
| #include "u_sensor" | ||
| long duration; | ||
| int distance; | ||
| int inches; | ||
|
|
||
| //GPIO initializations | ||
| void usensor_gpio_init(void) | ||
| { | ||
|
|
||
| GPIO_InitTypeDef GPIO_InitStruct; | ||
|
|
||
| /* GPIO Ports Clock Enable */ | ||
| __HAL_RCC_GPIOF_CLK_ENABLE(); | ||
| __HAL_RCC_GPIOB_CLK_ENABLE(); | ||
| __HAL_RCC_GPIOE_CLK_ENABLE(); | ||
| __HAL_RCC_GPIOD_CLK_ENABLE(); | ||
|
|
||
| /*Configure GPIO pin Output Level */ | ||
| HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); | ||
|
|
||
| /*Configure GPIO pin Output Level */ | ||
| HAL_GPIO_WritePin(GPIOE, GPIO_PIN_10, GPIO_PIN_RESET); | ||
|
|
||
| /*Configure GPIO pin Output Level */ | ||
| HAL_GPIO_WritePin(GPIOD, GPIO_PIN_0, GPIO_PIN_RESET); | ||
|
|
||
| /*Configure GPIO pins : PF9 PF6 */ | ||
| GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_6; | ||
| GPIO_InitStruct.Mode = GPIO_MODE_INPUT; | ||
| GPIO_InitStruct.Pull = GPIO_NOPULL; | ||
| HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); | ||
|
|
||
| /*Configure GPIO pin : PB0 */ | ||
| GPIO_InitStruct.Pin = GPIO_PIN_0; | ||
| GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; | ||
| GPIO_InitStruct.Pull = GPIO_NOPULL; | ||
| GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; | ||
| HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); | ||
|
|
||
| /*Configure GPIO pin : PE10 */ | ||
| GPIO_InitStruct.Pin = GPIO_PIN_10; | ||
| GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; | ||
| GPIO_InitStruct.Pull = GPIO_NOPULL; | ||
| GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; | ||
| HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); | ||
|
|
||
| /*Configure GPIO pin : PE11 */ | ||
| GPIO_InitStruct.Pin = GPIO_PIN_11; | ||
| GPIO_InitStruct.Mode = GPIO_MODE_INPUT; | ||
| GPIO_InitStruct.Pull = GPIO_NOPULL; | ||
| HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); | ||
|
|
||
| /*Configure GPIO pin : PD0 */ | ||
| GPIO_InitStruct.Pin = GPIO_PIN_0; | ||
| GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; | ||
| GPIO_InitStruct.Pull = GPIO_NOPULL; | ||
| GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; | ||
| HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); | ||
|
|
||
| } | ||
|
|
||
| //Delay 1us x n | ||
| void delay_us(uint32_t n) // Or n*72 clock cycles | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. n*12 |
||
| { | ||
|
|
||
| uint32_t i = 0U; | ||
| uint32_t j = 0U; | ||
|
|
||
| for(i = n; i > 0; --i) | ||
| { | ||
| for(j = 12; j > 0; --j) | ||
| { | ||
| asm("nop"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| void cpu_sw_delay(uint32_t n) | ||
| { | ||
| uint32_t i = 0U; | ||
| uint32_t j = 0U; | ||
|
|
||
| for(i = 0U; i < n; ++i) | ||
| { | ||
| for(j = 0U; j < 72; ++j) //72 CPU ticks per microsecond | ||
| { | ||
| asm("nop"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| */ | ||
|
|
||
| //Measures a HIGH pulse | ||
| long pulse_in(GPIO_TypeDef* echo_port) | ||
| { | ||
| unsigned long counter = 0; | ||
| //Sensor#0 echo port | ||
| if(echo_port == GPIOE) { | ||
| //Stall and wait for echo PE11 to go high. | ||
| while((echo_port->IDR & 0x0800) == 0) {} | ||
| //Time the pulse. Start counting clock cycles until the echoPin goes low. | ||
| while((echo_port->IDR & 0x0800) == 1) { | ||
| ++counter; | ||
| asm("nop"); | ||
| } | ||
| } | ||
| //Sensor#1 echo port | ||
| else if(echo_port == GPIOF) { | ||
| //Stall and wait for echo PF6 to go high. | ||
| while((echo_port->IDR & 0x0040) == 0) {} | ||
| while((echo_port->IDR & 0x0800) == 1) { | ||
| ++counter; | ||
| asm("nop"); | ||
| } | ||
| } | ||
| //Sensor#2 echo port | ||
| else if(echo_port == GPIOA) { | ||
| //Stall and wait for echo PA13 to go high. | ||
| while((echo_port->IDR & 0x2000) == 0) {} | ||
| while((echo_port->IDR & 0x0800) == 1) { | ||
| ++counter; | ||
| asm("nop"); | ||
| } | ||
| } | ||
|
|
||
| //Every 72 clock cycles yields 1 microseconds | ||
| return counter/72; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be 12 too? |
||
| } | ||
|
|
||
|
|
||
| int get_sensor_dist(GPIO_TypeDef* trig_port, GPIO_TypeDef* echo_port, uint16_t trigger) | ||
| { | ||
| //Preparing the trigger pin. | ||
| HAL_GPIO_WritePin(trig_port, trigger, GPIO_PIN_RESET); | ||
| delay_us(2); | ||
| //10us sensor required initiation pulse on the trigger pin. | ||
| HAL_GPIO_WritePin(trig_port, trigger, GPIO_PIN_SET); | ||
| delay_us(10); | ||
| HAL_GPIO_WritePin(trig_port, trigger, GPIO_PIN_RESET); | ||
| //pulse_in returns the sound wave travel time in microseconds | ||
| duration = pulse_in(echo_port); | ||
| distance = duration*.0133858/2; | ||
| inches = distance%12; | ||
| //Delay 50ms as per manufacture recomendation | ||
| delay_us(50000); | ||
| return inches; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| // Arduino example | ||
| /* | ||
| const int trigPin = 9; | ||
| const int echoPin = 10; | ||
|
|
||
| // defines variables | ||
| long duration; | ||
| int distance; | ||
| int feet; | ||
| int inches; | ||
|
|
||
| void setup() { | ||
| pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output | ||
| pinMode(echoPin, INPUT); // Sets the echoPin as an Input | ||
| Serial.begin(9600); // Starts the serial communication | ||
| } | ||
|
|
||
| void loop() { | ||
| // Clears the trigPin | ||
| digitalWrite(trigPin, LOW); | ||
| delayMicroseconds(2); | ||
| // Sets the trigPin on HIGH state for 10 micro seconds | ||
| digitalWrite(trigPin, HIGH); | ||
| delayMicroseconds(10); | ||
| digitalWrite(trigPin, LOW); | ||
| // Reads the echoPin, returns the sound wave travel time in microseconds | ||
| duration = pulseIn(echoPin, HIGH); | ||
| // Calculating the distance | ||
| distance= duration*.0133858/2; | ||
| // Prints the distance on the Serial Monitor | ||
| feet = distance/12; | ||
| inches = distance%12; | ||
| Serial.print("Distance: "); | ||
| Serial.print(feet); | ||
| Serial.print(" ft "); | ||
| Serial.print(inches); | ||
| Serial.print(" in\n"); | ||
| delay(500); | ||
| }*/ | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be u_sensor.h