Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <main.h>
- #include <flex_lcd.c>
- // Ad hoc programming, only for testing purpose (demo).
- // Needs to be revised before using.
- #define trigger PIN_A1
- #define echo PIN_A0
- #define button PIN_B0
- int32 overflow;
- #INT_TIMER1
- void tm1_int(){
- overflow++;
- }
- long tr_strobe(){
- set_timer1(0);
- while( input(echo) );
- output_high(trigger);
- delay_ms(1);
- output_low(trigger);
- while( ! input(echo) );
- setup_timer_1( T1_INTERNAL | T1_DIV_BY_8 );
- while( input(echo) );
- setup_timer_1( T1_DISABLED | T1_DIV_BY_8 );
- return(get_timer1());
- }
- void main()
- {
- lcd_init();
- port_b_pullups(true);
- printf(lcd_putc, "Hello World!\nPress Button.");
- while( input(button) );
- lcd_init();
- int32 ctime;
- int distance_m;
- int32 distance_cm;
- int32 time_ms;
- int time_s;
- enable_interrupts(GLOBAL);
- enable_interrupts(INT_TIMER1);
- printf(lcd_putc, "Time:\nDist.:");
- while(true){
- set_timer1(0);
- overflow=0;
- ctime=tr_strobe();
- ctime = (ctime | (overflow<<16));
- ctime = (ctime << 3);
- ctime = ctime / 10;
- time_s = ctime / 100000;
- time_ms = ctime - time_s * 100000;
- lcd_gotoxy(6,1);
- printf(lcd_putc, "%3.1u.%05lu s", time_s, time_ms);
- lcd_gotoxy(7,0);
- if(time_s==0 && time_ms < 40000){
- distance_m = 17 * ( ( ctime + 5 ) / 10 ) / 1000;
- distance_cm = ( ( 17 * ctime + 5 ) / 10 - 1000 * distance_m + 5 ) / 10;
- printf(lcd_putc, "%5.1u.%02lu m", distance_m, distance_cm);
- }
- else {
- lcd_putc(" --.-- m");
- }
- while(input(PIN_B0));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement