Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1.  
  2. /*
  3.  * Ultra_Sensor.c
  4.  *
  5.  *  Created on: Mar 3, 2017
  6.  *      Author: tyborch
  7.  */
  8. #include <stdbool.h>
  9. #include <stdio.h>
  10. #include "lcd.h"
  11. #include "Timer.h"
  12. #include "driverlib/interrupt.h"
  13. volatile unsigned rise, fall, intCount;
  14. int overflow;
  15. void Timer_Interrupt(void);
  16. void TimerInit() { //Using input edge timing
  17.     SYSCTL_RCGCTIMER_R |= SYSCTL_RCGCTIMER_R3;
  18.     TIMER3_CTL_R &= ~(TIMER_CTL_TAEN | TIMER_CTL_TBEN); //Disables timer B
  19.     TIMER3_CTL_R |= 0x00000C00; //Trigger on both edges
  20.     TIMER3_CFG_R |= TIMER_CFG_16_BIT;
  21.     TIMER3_TBMR_R |= 0x00000017; //
  22.     TIMER3_TBILR_R = 0xFFFF; //set upper bound here
  23.     TIMER3_ICR_R = (TIMER_ICR_CBECINT | TIMER_ICR_CAECINT); //Clears the timer B capture event interrupt
  24.     TIMER3_IMR_R |= (TIMER_IMR_CBEIM | TIMER_IMR_CAEIM); //Enables the timer capture event interrupts
  25.     NVIC_EN1_R = 0x00000018; //bits 35 and 36
  26.     IntMasterEnable();
  27.     TIMER3_CTL_R |= (TIMER_CTL_TAEN | TIMER_CTL_TBEN); //Enables timer
  28.  
  29. }
  30. void Timer_Interrupt(void) {
  31.     TIMER3_ICR_R |= TIMER_RIS_CBERIS;
  32.     if (intCount % 2 == 0){
  33.         rise = TIMER3_TBR_R;
  34.     }
  35.     else {
  36.         fall = TIMER3_TBR_R;
  37.     }
  38.     intCount++;
  39. }
  40. void sensorInit() {
  41.     SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R1;
  42.     GPIO_PORTB_DEN_R |= 0x08;
  43.     GPIO_PORTB_DIR_R |= 0x08;
  44. }
  45. void sendPulse() { //Sending the pulse
  46.     GPIO_PORTB_AFSEL_R &= 0xF7; //Disables alternate function for p3
  47.     GPIO_PORTB_DIR_R |= 0x08; //Sets portB to output
  48.     GPIO_PORTB_DATA_R |= 0x08; //Sets PB3 to high
  49.     timer_waitMicros(7);
  50.     GPIO_PORTB_DATA_R &= 0xF7; //Sets PB3 to low
  51.     GPIO_PORTB_DIR_R &= 0xF7; //Sets portB to input
  52.     GPIO_PORTB_AFSEL_R |= 0x08; //Enables alternate function for p3
  53.     GPIO_PORTB_PCTL_R |= 0x7000; //CHOOSES what function to use.
  54.     timer_waitMillis(50);
  55. }
  56. double redistance(int time) {
  57.     double t = 0.00225*(time/2);
  58.     return t;
  59. }
  60. unsigned int get_time() {
  61.     sendPulse();
  62.     int time = fall - rise;
  63.     if(time < 0) {
  64.         overflow++;
  65.     }
  66.     timer_waitMillis(200);
  67.     double dist = redistance(time);
  68.     lcd_printf("%d ms \n%.2f cm\nOverflow: %d", time/1000, dist, overflow);
  69.     return time;
  70. }
  71. int main() {
  72.     lcd_init();
  73.     TimerInit();
  74.     sensorInit();
  75.     overflow = 0;
  76.     IntRegister(INT_TIMER3B, Timer_Interrupt);
  77.     while (1) {
  78.         get_time();
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement