Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Ultra_Sensor.c
- *
- * Created on: Mar 3, 2017
- * Author: tyborch
- */
- #include <stdbool.h>
- #include <stdio.h>
- #include "lcd.h"
- #include "Timer.h"
- #include "driverlib/interrupt.h"
- volatile unsigned rise, fall, intCount;
- int overflow;
- void Timer_Interrupt(void);
- void TimerInit() { //Using input edge timing
- SYSCTL_RCGCTIMER_R |= SYSCTL_RCGCTIMER_R3;
- TIMER3_CTL_R &= ~(TIMER_CTL_TAEN | TIMER_CTL_TBEN); //Disables timer B
- TIMER3_CTL_R |= 0x00000C00; //Trigger on both edges
- TIMER3_CFG_R |= TIMER_CFG_16_BIT;
- TIMER3_TBMR_R |= 0x00000017; //
- TIMER3_TBILR_R = 0xFFFF; //set upper bound here
- TIMER3_ICR_R = (TIMER_ICR_CBECINT | TIMER_ICR_CAECINT); //Clears the timer B capture event interrupt
- TIMER3_IMR_R |= (TIMER_IMR_CBEIM | TIMER_IMR_CAEIM); //Enables the timer capture event interrupts
- NVIC_EN1_R = 0x00000018; //bits 35 and 36
- IntMasterEnable();
- TIMER3_CTL_R |= (TIMER_CTL_TAEN | TIMER_CTL_TBEN); //Enables timer
- }
- void Timer_Interrupt(void) {
- TIMER3_ICR_R |= TIMER_RIS_CBERIS;
- if (intCount % 2 == 0){
- rise = TIMER3_TBR_R;
- }
- else {
- fall = TIMER3_TBR_R;
- }
- intCount++;
- }
- void sensorInit() {
- SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R1;
- GPIO_PORTB_DEN_R |= 0x08;
- GPIO_PORTB_DIR_R |= 0x08;
- }
- void sendPulse() { //Sending the pulse
- GPIO_PORTB_AFSEL_R &= 0xF7; //Disables alternate function for p3
- GPIO_PORTB_DIR_R |= 0x08; //Sets portB to output
- GPIO_PORTB_DATA_R |= 0x08; //Sets PB3 to high
- timer_waitMicros(7);
- GPIO_PORTB_DATA_R &= 0xF7; //Sets PB3 to low
- GPIO_PORTB_DIR_R &= 0xF7; //Sets portB to input
- GPIO_PORTB_AFSEL_R |= 0x08; //Enables alternate function for p3
- GPIO_PORTB_PCTL_R |= 0x7000; //CHOOSES what function to use.
- timer_waitMillis(50);
- }
- double redistance(int time) {
- double t = 0.00225*(time/2);
- return t;
- }
- unsigned int get_time() {
- sendPulse();
- int time = fall - rise;
- if(time < 0) {
- overflow++;
- }
- timer_waitMillis(200);
- double dist = redistance(time);
- lcd_printf("%d ms \n%.2f cm\nOverflow: %d", time/1000, dist, overflow);
- return time;
- }
- int main() {
- lcd_init();
- TimerInit();
- sensorInit();
- overflow = 0;
- IntRegister(INT_TIMER3B, Timer_Interrupt);
- while (1) {
- get_time();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement