ขอคำแนะนำ
เอก |
ผมใช้บอร์ด ET-BASE ARM7 LPC2103 เซ็นเซอร์อัลตร้าโซนิค http://www.seeedstudio.com/wiki/index.php?title=Ultra_Sonic_range_measurement_module ตัวนี้ครับ คือว่าผมจะเขียนโค้ดให้เซ็นเซอร์อัลตร้าโซนิคติดต่อเข้ากับ P0.3 แล้วไปแสดงผลที่จอ16x2 ครับ ไม่ทราบว่าพอจะมีโค้ดตัวอย่าง หรือแนวทางไหนบ้างไหมครับ ขอบคุณมากครับ |
เว็บมาสเตอร์ |
เอา code ไปลองเล่นดูนะครับ
LPC2103 with ultrasonic // Ultrasonic Sensor Serial Implementation // We are going to Pin 4 as the trigger and Pin 5 as the echo #include <LPC2103.H> // LPC2103 MPU Register #include <stdio.h> // For Used Function printf #define ECHO_PIN_5 0x20 // setup for PIN 5 ECHO #define TRIG_PIN_4 0x10 // setup for PIN 4 TRIGGER // int regist_1 = 0x20 ; /* pototype section */ long ranging(int sys); // Calculating distances in centimeters and inches void initialize(void); void init_serial0 (void); // Initil UART-0 int putchar (int ch); // Put Char to UART-0 int getchar (void); // Get Char From Uart-0 void wait(unsigned long int counter); // Wait Function int runCounter = 0; int firstLoop = 0; long duration = -1; long distance_cm, distance_inc; int main(void) { char data; int static READPIN; initialize(); init_serial0(); // Initilial UART0 = 9600,N,8,1 printf("ET-ARM7 BASE LPC2103...TESTING ULTRASONIC SENSOR\n"); // Call printf Function printf("Press 'r' key to Test...ULTRASONIC SENSOR\n"); printf("******************************************************\n"); printf("******************************************************\n"); while(1) // Loop Continue { wait(1000000); // 100msec delay runCounter = 0; firstLoop = 0; READPIN = (IOPIN&ECHO_PIN_5); duration = READPIN; // testing Echo information outside loop printf("READPIN information outside of loop: \n", READPIN); printf("Duration information outside of loop: \n", duration); printf("******************************************************\n"); printf("******************************************************\n"); data = getchar(); if(data == 'r') { putchar(data); IOCLR = TRIG_PIN_4; // HIGH wait(100); // need to wait 100ns = 10uS IOSET = TRIG_PIN_4; // LOW if(duration != READPIN) { if(READPIN != 0) { duration = READPIN; printf("READPIN information inside loop: \n", READPIN); // Testing information inside loop printf("Duration information inside loop: \n", duration); // Print Echo information printf("******************************************************\n"); printf("******************************************************\n"); // ranging(); // printf("Distance in centimeters: %d \n", distance_cm); } else { duration = READPIN; printf("\nNo Echo information sent!!\n"); } } else { printf("\nIncorrect key pressed!!!\n"); } } } } long ranging(int sys) { distance_cm = duration/29 /2; distance_inc = duration/74 /2; if(sys) return distance_cm; else return distance_inc; } void initialize(void) { int counter1 = 0; int runCounter = 0; int firstLoop = 0; // SETUP I/O PIN0.4 IODIR = TRIG_PIN_4; // Pin 4 is a trigger output, everything else is input after reset wait(1000000); // 100msec delay } /******************************/ /* Initial UART0 = 9600,N,8,1 */ /* VPB(pclk) = 29.4912 MHz */ /******************************/ void init_serial0 (void) { PINSEL0 &= 0xFFFFFFF0; // Reset P0.0,P0.1 Pin Config PINSEL0 |= 0x00000001; // Select P0.0 = TxD(UART0) PINSEL0 |= 0x00000004; // Select P0.1 = RxD(UART0) U0LCR &= 0xFC; // Reset Word Select(1:0) U0LCR |= 0x03; // Data Bit = 8 Bit U0LCR &= 0xFB; // Stop Bit = 1 Bit U0LCR &= 0xF7; // Parity = Disable U0LCR &= 0xBF; // Disable Break Control U0LCR |= 0x80; // Enable Programming of Divisor Latches // U0DLM:U0DLL = 29.4912MHz / [16 x Baud] // = 29.4912MHz / [16 x 9600] // = 192 = 0x00C0 // changed to 38400 Baud = 48 = 0x30 U0DLM = 0x00; // Program Divisor Latch(192) for 9600 Baud U0DLL = 0x30; U0LCR &= 0x7F; // Disable Programming of Divisor Latches U0FCR |= 0x01; // FIF0 Enable U0FCR |= 0x02; // RX FIFO Reset U0FCR |= 0x04; // TX FIFO Reset U0FCR &= 0x3F; } /****************************/ /* Write Character To UART0 */ /****************************/ int putchar (int ch) { if (ch == '\n') { while (!(U0LSR & 0x20)); // Wait TXD Buffer Empty U0THR = 0x0D; // Write CR } while (!(U0LSR & 0x20)); // Wait TXD Buffer Empty return (U0THR = ch); // Write Character } /*****************************/ /* Read Character From UART0 */ /*****************************/ int getchar (void) { while (!(U0LSR & 0x01)); // Wait RXD Receive Data Ready return (U0RBR); // Get Receice Data & Return } void wait (unsigned long int counter) { while(counter > 0) { counter--; } // wait function } |
Page : 1
แสดงความคิดเห็น