Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "lcd.h"
- #include "lab7.h"
- #include "timer.h"
- #include <stdbool.h>
- #include "driverlib/interrupt.h"
- enum {LOW, HIGH, DONE} state;
- unsigned int overflow = 0;
- unsigned int rising_time = 0; // start time of the return pulse
- unsigned int falling_time = 0 ; // end time of the return pulse
- unsigned int ping_read(void){
- //delta = rising_time - falling_time;
- send_pulse();
- if(falling_time < rising_time){
- overflow++;
- state = LOW;
- return rising_time - falling_time;
- }
- state = LOW;
- return falling_time - rising_time;
- }
- void send_pulse() {
- GPIO_PORTB_DIR_R |= 0x8; // set PB3 as output
- GPIO_PORTB_AFSEL_R &= 0xFFF7; // turn off alternative function
- GPIO_PORTB_DATA_R |= 0x8; // set PB3 to high
- // wait at least 5 microseconds based on data sheet
- timer_waitMicros(6);
- GPIO_PORTB_DATA_R &= 0x7; // set PB3 to low
- GPIO_PORTB_AFSEL_R |= 0x0008; //turn off alternative function
- GPIO_PORTB_DIR_R &= 0x7; // set PB3 as input
- //timer_waitMicros(6);
- }
- void init(){
- SYSCTL_RCGCGPIO_R |= 0b000010;// Enable Port B clock
- GPIO_PORTB_DIR_R &= 0xF7;//Setting PB3 to output
- GPIO_PORTB_PUR_R |= 0x03;//Set pins' pull-up resists
- GPIO_PORTB_DEN_R |= 0x08; //Enable PB3
- }
- void TIMER3B_Handler(void) {
- if(TIMER3_RIS_R &= 0x400){
- if(state == LOW){
- rising_time = TIMER3_TBR_R;
- state = HIGH;
- }
- else if(state == HIGH){
- falling_time = TIMER3_TBR_R;
- state = DONE;
- }
- }
- TIMER3_ICR_R |= 0x400; //clear interrupts BIT 10
- }
- void clock_timer_init(void) {
- SYSCTL_RCGCGPIO_R |= 0x2; //enable Clock on PortB
- GPIO_PORTB_DEN_R |= 0x8; // set pin 3
- GPIO_PORTB_AFSEL_R |= 0x8; //turning on alternative function
- GPIO_PORTB_PCTL_R |= 0x7000; //enable TX and RX on pin 3
- SYSCTL_RCGCTIMER_R |= 0x8; //enable timer 3 clock
- TIMER3_CTL_R &= 0xEFF; //sets TBEN to 0, have to disable before you can change settings
- TIMER3_CTL_R |= 0xC00; //sets TBEvent to 11
- TIMER3_CFG_R |= 0x4; //set to 16 bit timer
- TIMER3_TBMR_R |= (0x3 | 0x4 | 0x10); //capture mode
- TIMER3_TBILR_R |= 0xFFFFFFFF; //Set upper bound.
- TIMER3_ICR_R |= 0xF00; // clear interrupts
- TIMER3_IMR_R |= 0x400; //enable capture interrupt.
- NVIC_EN1_R |= 0x10; //enable bit 5
- IntRegister(INT_TIMER3B, TIMER3B_Handler); //register TIMER3B interrupt handler
- IntMasterEnable(); //intialize global interrupts
- TIMER3_CTL_R |= 0x100; //sets TBEN to 1, enables timer3B
- }
- unsigned int get_Overflow(){
- return overflow;
- }
- int main(void) {
- lcd_init();
- init();
- clock_timer_init();
- while(1) {
- timer_waitMillis(100);
- unsigned int pingHolder= ping_read() ;
- //lcd_setCursorPos(0, 0);
- lcd_printf("Distance: %d \nPulse: %d \nOverflow: %d ", pingHolder/990, pingHolder, get_Overflow());
- // lcd_setCursorPos(0, 1);
- // lcd_printf("Pulse Width: ", pingHolder);
- //lcd_setCursorPos(0, 2);
- // lcd_printf("Overflow: %d \n",get_Overflow());
- // lcd_printf("Testing ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement