//************************************************************************************ //** //** File name: C:\Documents and Settings\khaled\Desktop\MotionSensor22.c //** Generated by: Flowcode v3.4.7.48 //** Date: Friday, October 31, 2008 23:15:31 //** Licence: //** Registered to: khaled //** //** //** NOT FOR COMMERCIAL USE //** //** http://www.matrixmultimedia.com //************************************************************************************ #define MX_PIC //Defines for microcontroller #define P16F877 #define MX_EE #define MX_EE_SIZE 256 #define MX_SPI #define MX_SPI_C #define MX_SPI_SDI 4 #define MX_SPI_SDO 5 #define MX_SPI_SCK 3 #define MX_UART #define MX_UART_C #define MX_UART_TX 6 #define MX_UART_RX 7 #define MX_I2C #define MX_MI2C #define MX_I2C_C #define MX_I2C_SDA 4 #define MX_I2C_SCL 3 #define MX_PWM #define MX_PWM_CNT 2 #define MX_PWM_TRIS1 trisc #define MX_PWM_1 2 #define MX_PWM_TRIS2 trisc #define MX_PWM_2 1 //Functions #include #pragma CLOCK_FREQ 8000000 //Configuration data #pragma DATA 0x2007, 0x3f3a //Internal functions #include "C:\Program Files\Matrix Multimedia\Flowcode V3\FCD\internals.h" //Macro function declarations void FCM_TMR0isr(); //Variable declarations char FCV_CAPTUREL; char FCV_ECHOLOOP; short FCV_DISTANCE; char FCV_ECHO; char FCV_IRQCOUNT; char FCV_CAPTUREH; //LCDDisplay0: //Macro function declarations void FCD_LCDDisplay0_GetDefines(); void FCD_LCDDisplay0_Start(); void FCD_LCDDisplay0_Clear(); void FCD_LCDDisplay0_PrintASCII(char Character); void FCD_LCDDisplay0_Command(char in); void FCD_LCDDisplay0_RawSend(char in, char mask); void FCD_LCDDisplay0_Cursor(char x, char y); void FCD_LCDDisplay0_PrintNumber(short Number); void FCD_LCDDisplay0_PrintString(char* String, char MSZ_String); //Supplementary defines //LCDDisplay0: //Macro implementations void FCD_LCDDisplay0_GetDefines() { } //Dummy end of function to allow defines to be added correctly //component connections #define LCD_10027840_PORT portb #define LCD_10027840_TRIS trisb #define LCD_10027840_BIT0 0 #define LCD_10027840_BIT1 1 #define LCD_10027840_BIT2 2 #define LCD_10027840_BIT3 3 #define LCD_10027840_RS 4 #define LCD_10027840_E 5 #ifdef _BOOSTC #define LCD_10027840_DELAY delay_10us(10) #endif #ifdef _C2C_ #define LCD_10027840_DELAY delay_us(100) #endif #ifndef LCD_10027840_DELAY #define LCD_10027840_DELAY delay_us(100) #endif //internal function prototypes void LCD_10027840_RawSend(char nIn, char nMask); //internal function implementations void LCD_10027840_RawSend(char nIn, char nMask) { unsigned char pt; unsigned char outVal; outVal = LCD_10027840_PORT; clear_bit(outVal, LCD_10027840_BIT0); clear_bit(outVal, LCD_10027840_BIT1); clear_bit(outVal, LCD_10027840_BIT2); clear_bit(outVal, LCD_10027840_BIT3); clear_bit(outVal, LCD_10027840_RS); clear_bit(outVal, LCD_10027840_E); pt = ((nIn >> 4) & 0x0f); if (pt & 0x01) set_bit(outVal, LCD_10027840_BIT0); if (pt & 0x02) set_bit(outVal, LCD_10027840_BIT1); if (pt & 0x04) set_bit(outVal, LCD_10027840_BIT2); if (pt & 0x08) set_bit(outVal, LCD_10027840_BIT3); if (nMask) set_bit(outVal, LCD_10027840_RS); LCD_10027840_PORT = outVal; LCD_10027840_DELAY; set_bit (LCD_10027840_PORT, LCD_10027840_E); LCD_10027840_DELAY; clear_bit (LCD_10027840_PORT, LCD_10027840_E); pt = (nIn & 0x0f); LCD_10027840_DELAY; outVal = LCD_10027840_PORT; clear_bit(outVal, LCD_10027840_BIT0); clear_bit(outVal, LCD_10027840_BIT1); clear_bit(outVal, LCD_10027840_BIT2); clear_bit(outVal, LCD_10027840_BIT3); clear_bit(outVal, LCD_10027840_RS); clear_bit(outVal, LCD_10027840_E); if (pt & 0x01) set_bit(outVal, LCD_10027840_BIT0); if (pt & 0x02) set_bit(outVal, LCD_10027840_BIT1); if (pt & 0x04) set_bit(outVal, LCD_10027840_BIT2); if (pt & 0x08) set_bit(outVal, LCD_10027840_BIT3); if (nMask) set_bit(outVal, LCD_10027840_RS); LCD_10027840_PORT = outVal; LCD_10027840_DELAY; set_bit (LCD_10027840_PORT, LCD_10027840_E); LCD_10027840_DELAY; clear_bit (LCD_10027840_PORT, LCD_10027840_E); LCD_10027840_DELAY; } // Dummy function to close the defines section off void LCD_10027840_Dummy_Function(); void LCD_10027840_Dummy_Function() { } void FCD_LCDDisplay0_Start() { clear_bit(LCD_10027840_TRIS, LCD_10027840_BIT0); clear_bit(LCD_10027840_TRIS, LCD_10027840_BIT1); clear_bit(LCD_10027840_TRIS, LCD_10027840_BIT2); clear_bit(LCD_10027840_TRIS, LCD_10027840_BIT3); clear_bit(LCD_10027840_TRIS, LCD_10027840_RS); clear_bit(LCD_10027840_TRIS, LCD_10027840_E); delay_ms(12); LCD_10027840_RawSend(0x33, 0); delay_ms(2); LCD_10027840_RawSend(0x32, 0); delay_ms(2); LCD_10027840_RawSend(0x2c, 0); delay_ms(2); LCD_10027840_RawSend(0x06, 0); delay_ms(2); LCD_10027840_RawSend(0x0c, 0); delay_ms(2); //clear the display LCD_10027840_RawSend(0x01, 0); delay_ms(2); LCD_10027840_RawSend(0x02, 0); delay_ms(2); } void FCD_LCDDisplay0_Clear() { LCD_10027840_RawSend(0x01, 0); delay_ms(2); LCD_10027840_RawSend(0x02, 0); delay_ms(2); } void FCD_LCDDisplay0_PrintASCII(char Character) { LCD_10027840_RawSend(Character, 0x10); } void FCD_LCDDisplay0_Command(char in) { LCD_10027840_RawSend(in, 0); delay_ms(2); } void FCD_LCDDisplay0_RawSend(char in, char mask) { //Error Reading Code For LCD Display::Macro_RawSend } void FCD_LCDDisplay0_Cursor(char x, char y) { #if (2 == 1) y=0x80; #endif #if (2 == 2) if (y==0) y=0x80; else y=0xc0; #endif #if (2 == 4) if (y==0) y=0x80; else if (y==1) y=0xc0; else if (y==2) y=0x94; else y=0xd4; #endif LCD_10027840_RawSend(y+x, 0); delay_ms(2); } void FCD_LCDDisplay0_PrintNumber(short Number) { short tmp_int; char tmp_byte; if (Number < 0) { LCD_10027840_RawSend('-', 0x10); Number = 0 - Number; } tmp_int = Number; if (Number >= 10000) { tmp_byte = tmp_int / 10000; LCD_10027840_RawSend('0' + tmp_byte, 0x10); while (tmp_byte > 0) { tmp_int = tmp_int - 10000; tmp_byte--; } } if (Number >= 1000) { tmp_byte = tmp_int / 1000; LCD_10027840_RawSend('0' + tmp_byte, 0x10); while (tmp_byte > 0) { tmp_int = tmp_int - 1000; tmp_byte--; } } if (Number >= 100) { tmp_byte = tmp_int / 100; LCD_10027840_RawSend('0' + tmp_byte, 0x10); while (tmp_byte > 0) { tmp_int = tmp_int - 100; tmp_byte--; } } if (Number >= 10) { tmp_byte = tmp_int / 10; LCD_10027840_RawSend('0' + tmp_byte, 0x10); while (tmp_byte > 0) { tmp_int = tmp_int - 10; tmp_byte--; } } LCD_10027840_RawSend('0' + tmp_int, 0x10); } void FCD_LCDDisplay0_PrintString(char* String, char MSZ_String) { char idx; for (idx=0; idx C2 trisc = trisc & 0xfb; if (0xff) portc = (portc & 0xfb) | 0x04; else portc = portc & 0xfb; //C Code //C Code: tmr0 = 0; //Interrupt //Interrupt: Enable TMR0 Overflow option_reg.T0CS=0; option_reg.T0SE=1; option_reg = (option_reg & 0xF0) | 0x04; intcon.GIE=1; intcon.T0IE=1; //Loop //Loop: While EchoLoop while (1) { //Input //Input: C4 -> Echo trisc = trisc | 0x10; FCV_ECHO = ((portc & 0x10) == 0x10); //Decision //Decision: Echo = 0? if (FCV_ECHO == 0) { //Calculation //Calculation: // CaptureH = IrqCount FCV_CAPTUREH = FCV_IRQCOUNT; //C Code //C Code: FCV_CAPTUREL = tmr0; //Decision //Decision: CaptureH = IrqCount? if (FCV_CAPTUREH == FCV_IRQCOUNT) { } else { //Calculation //Calculation: // CaptureH = IrqCount // CaptureL = 0 FCV_CAPTUREH = FCV_IRQCOUNT; FCV_CAPTUREL = 0; } //Calculation //Calculation: // EchoLoop = 0 FCV_ECHOLOOP = 0; } else { //Decision //Decision: IrqCount > 2? if (FCV_IRQCOUNT > 2) { //Calculation //Calculation: // EchoLoop = 0 FCV_ECHOLOOP = 0; } } if ((FCV_ECHOLOOP) == 0) break; } //Calculation //Calculation: // Distance = ( CaptureH << 8 ) + CaptureL FCV_DISTANCE = ( FCV_CAPTUREH << 8 ) + FCV_CAPTUREL; //Output //Output: 0 -> C2 trisc = trisc & 0xfb; if (0) portc = (portc & 0xfb) | 0x04; else portc = portc & 0xfb; //Call Component Macro //Call Component Macro: LCDDisplay(0)::Clear FCD_LCDDisplay0_Clear(); //Call Component Macro //Call Component Macro: LCDDisplay(0)::PrintNumber(Distance) FCD_LCDDisplay0_PrintNumber(FCV_DISTANCE); //Call Component Macro //Call Component Macro: LCDDisplay(0)::PrintASCII("mm") FCD_LCDDisplay0_PrintASCII('m'); FCD_LCDDisplay0_PrintASCII('m'); //Delay //Delay: 250 ms delay_ms(250); } mainendloop: goto mainendloop; } void interrupt(void) { if (intcon & (1 << T0IF)) { FCM_TMR0isr(); clear_bit(intcon, T0IF); } }