Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1.  
  2. #include "lcd.h"
  3. #include "lab7.h"
  4. #include "timer.h"
  5. #include <stdbool.h>
  6. #include "driverlib/interrupt.h"
  7.  
  8.  
  9. enum {LOW, HIGH, DONE} state;
  10. unsigned int overflow = 0;
  11. unsigned int rising_time = 0; // start time of the return pulse
  12. unsigned int falling_time = 0 ; // end time of the return pulse
  13.  
  14.  
  15.  
  16. unsigned int ping_read(void){
  17.  
  18. //delta = rising_time - falling_time;
  19. send_pulse();
  20. if(falling_time < rising_time){
  21. overflow++;
  22. state = LOW;
  23. return rising_time - falling_time;
  24. }
  25. state = LOW;
  26. return falling_time - rising_time;
  27. }
  28.  
  29.  
  30. void send_pulse() {
  31.  
  32. GPIO_PORTB_DIR_R |= 0x8; // set PB3 as output
  33. GPIO_PORTB_AFSEL_R &= 0xFFF7; // turn off alternative function
  34. GPIO_PORTB_DATA_R |= 0x8; // set PB3 to high
  35.  
  36. // wait at least 5 microseconds based on data sheet
  37. timer_waitMicros(6);
  38. GPIO_PORTB_DATA_R &= 0x7; // set PB3 to low
  39. GPIO_PORTB_AFSEL_R |= 0x0008; //turn off alternative function
  40. GPIO_PORTB_DIR_R &= 0x7; // set PB3 as input
  41. //timer_waitMicros(6);
  42. }
  43.  
  44. void init(){
  45.  
  46. SYSCTL_RCGCGPIO_R |= 0b000010;// Enable Port B clock
  47. GPIO_PORTB_DIR_R &= 0xF7;//Setting PB3 to output
  48. GPIO_PORTB_PUR_R |= 0x03;//Set pins' pull-up resists
  49. GPIO_PORTB_DEN_R |= 0x08; //Enable PB3
  50.  
  51. }
  52.  
  53.  
  54. void TIMER3B_Handler(void) {
  55.  
  56. if(TIMER3_RIS_R &= 0x400){
  57. if(state == LOW){
  58. rising_time = TIMER3_TBR_R;
  59. state = HIGH;
  60. }
  61. else if(state == HIGH){
  62. falling_time = TIMER3_TBR_R;
  63. state = DONE;
  64. }
  65. }
  66. TIMER3_ICR_R |= 0x400; //clear interrupts BIT 10
  67. }
  68.  
  69.  
  70. void clock_timer_init(void) {
  71.  
  72. SYSCTL_RCGCGPIO_R |= 0x2; //enable Clock on PortB
  73. GPIO_PORTB_DEN_R |= 0x8; // set pin 3
  74. GPIO_PORTB_AFSEL_R |= 0x8; //turning on alternative function
  75. GPIO_PORTB_PCTL_R |= 0x7000; //enable TX and RX on pin 3
  76. SYSCTL_RCGCTIMER_R |= 0x8; //enable timer 3 clock
  77. TIMER3_CTL_R &= 0xEFF; //sets TBEN to 0, have to disable before you can change settings
  78. TIMER3_CTL_R |= 0xC00; //sets TBEvent to 11
  79. TIMER3_CFG_R |= 0x4; //set to 16 bit timer
  80. TIMER3_TBMR_R |= (0x3 | 0x4 | 0x10); //capture mode
  81. TIMER3_TBILR_R |= 0xFFFFFFFF; //Set upper bound.
  82. TIMER3_ICR_R |= 0xF00; // clear interrupts
  83. TIMER3_IMR_R |= 0x400; //enable capture interrupt.
  84. NVIC_EN1_R |= 0x10; //enable bit 5
  85. IntRegister(INT_TIMER3B, TIMER3B_Handler); //register TIMER3B interrupt handler
  86. IntMasterEnable(); //intialize global interrupts
  87. TIMER3_CTL_R |= 0x100; //sets TBEN to 1, enables timer3B
  88.  
  89.  
  90. }
  91.  
  92. unsigned int get_Overflow(){
  93.  
  94. return overflow;
  95. }
  96.  
  97. int main(void) {
  98. lcd_init();
  99. init();
  100.  
  101. clock_timer_init();
  102. while(1) {
  103. timer_waitMillis(100);
  104. unsigned int pingHolder= ping_read() ;
  105. //lcd_setCursorPos(0, 0);
  106. lcd_printf("Distance: %d \nPulse: %d \nOverflow: %d ", pingHolder/990, pingHolder, get_Overflow());
  107. // lcd_setCursorPos(0, 1);
  108. // lcd_printf("Pulse Width: ", pingHolder);
  109. //lcd_setCursorPos(0, 2);
  110. // lcd_printf("Overflow: %d \n",get_Overflow());
  111. // lcd_printf("Testing ");
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement