void UART_Driver()//UART通信监控函数,在主循环中调用。如果接收到字符串,会自动调用另行编写的UART_Action(uint8 *dat,uint8 len) { uint8 pdata dat[64]; uint8 len; if(UART_ResiveStringEndFlag) { UART_ResiveStringEndFlag=0; len=UART_Read(dat,64); UART_Action(dat,len); } }
void interrupt Interrupt(){ if(RCIF) { char letterReceived; letterReceived = UART_Read(); switch(letterReceived) { case 'A': RA0 = 1; __delay_ms(500); RA0 = 0; break; case 'B': RA1 = 1; __delay_ms(500); RA1 = 0; break; case 'C': RA2 = 1; __delay_ms(500); RA2 = 0; break; case 'D': RA3 = 1; __delay_ms(500); RA3 = 0; break; case 'E': RA4 = 1; __delay_ms(500); RA4 = 0; break; case 'F': RA5 = 1; __delay_ms(500); RA5 = 0; break; default: break; } } }
unsigned char * UART_Read_Text() { unsigned const char *a="Keyed in \r\n"; unsigned static char string[20]; unsigned char x, i = 0; //receive the characters until ENTER is pressed (ASCII for ENTER = 13) while((x = UART_Read()) != 13) { //and store the received characters into the array string[] one-by-one string[i++] = x; } //insert NULL to terminate the string string[i] = '\0'; UART_Write_Text(a); //return the received string return(string); }
void main(void) { const unsigned char *mas="\r\n---------------MASTER DEVICE-----------------\r\n"; const unsigned char * arr1 = "\r\nTaking in the text \r\n"; const unsigned char *arr2="\r\nEnter your choice \r\n 1.Slave 1(Address:0xAA\r\n2.Slave 2(Address:0xBB)\r\n3.Slave 3(Address:0xCC\r\n"; const unsigned char *arr3= "You have entered:\r\n"; const unsigned char *arr4= "\r\nUART Initialised\r\n"; const unsigned char *arr5= "\r\nI2C initialised:\r\n"; const unsigned char *msg1="\r\nSending to Slave 1 (Address 0xAA)\r\n"; const unsigned char *msg2="\r\nSending to Slave 2 (Address 0xBB)\r\n"; const unsigned char *msg3="\r\nSending to Slave 3 (Address 0xCC)\r\n"; const unsigned char *msg4="\r\nAddress sent\r\n"; const unsigned char *msg5="\r\nData sent\r\n"; const unsigned char *err="\r\nNo message will be sent since no slave no entered\r\n"; const unsigned char *fin="\r\nClosing Communication!\r\n"; const unsigned char *msgm="\r\nYou have entered choice number:\r\n"; unsigned char choice; OSCCONbits.IRCF = 0x07; // Configure Internal OSC for 8MHz Clock while(!OSCCONbits.HTS); // Wait Until Internal Osc is Stable INTCON=0; // purpose of disabling the interrupts. UART_Init(baud_rate); UART_Write_Text(mas); UART_Write_Text(arr4); delay_ms(500); I2C_init(); UART_Write_Text(arr5); //Initialisation done while(1) { UART_Write_Text(arr1); i2c_idle(); //receive the characters until ENTER is pressed (ASCII for ENTER = 13) is=UART_Read_Text(); UART_Write_Text(arr3); UART_Write_Text(is); UART_Write_Text(arr2); choice=UART_Read(); UART_Write_Text(msgm); UART_Write(choice); switch(choice) { case 0x31: { UART_Write_Text(msg1); I2C_Start(); if(I2C_address_send())//device address { delay_us(20);//clock settle and then send I2C_Write_Text(is); } else break; I2C_Stop(); break; } case 0x32: { UART_Write_Text(msg2); I2C_Start(); if(I2C_address_send1())//device address { delay_us(20);//clock settle and then send I2C_Write_Text(is); } else break; I2C_Stop(); break; } case 0x33: { UART_Write_Text(msg3); I2C_Start(); if(I2C_address_send2())//device address { delay_us(20);//clock settle and then send I2C_Write_Text(is); } else break; I2C_Stop(); break; } default: UART_Write_Text(err); break; } //Choice entered data sent respectively to slaves now stop //i2c_SendAcknowledge(I2C_LAST); PIR1bits.SSPIF = 0; UART_Write_Text(fin); } }
void UART_Read_Text(char *Output, unsigned int length) { unsigned int i; for(int i=0;i<length;i++) Output[i] = UART_Read(); }
bool OfflineDownloadwithUART(void) { u32 i,j; //u8 tmp[2]; if(!mcu_scfg.flash_done) // no user FW for MCU return FALSE; //connect if(!UART_Connect()) return FALSE; //check pid /*if(!UART_GetPID(tmp)) return FALSE; if(memcmp(tmp,mcu_scfg.MCUPID,2)!=0) return FALSE;*/ //check boot loader version //still not real boot loader version //remove password if(!UART_RemovePWD()) return FALSE; //write password or not if(mcu_scfg.pwdflag[0]==FISH_MAN) { if(!UART_WritePWD(mcu_scfg.max_auth_num,TRUE)) return FALSE; } //download user FW for(i=0;i<MCU_FLASH_PAGES;i++) { if(mcu_scfg.flash_map[i]) { for(j=0;j<PRO_PAGE_SIZE/MAX_DATA_SIZE;j++) { if(!UART_Write(MCU_FLASH_BASE+i*PRO_PAGE_SIZE+j*MAX_DATA_SIZE-mcu_scfg.flash_offset, MAX_DATA_SIZE, (u8*)(MCU_FLASH_BASE+i*PRO_PAGE_SIZE+j*MAX_DATA_SIZE), TRUE)) return FALSE; DelayMs(10); } } } //verify for(i=0;i<MCU_FLASH_PAGES;i++) { if(mcu_scfg.flash_map[i]) { for(j=0;j<PRO_PAGE_SIZE/MAX_DATA_SIZE;j++) { if(!UART_Read(MCU_FLASH_BASE+i*PRO_PAGE_SIZE+j*MAX_DATA_SIZE-mcu_scfg.flash_offset, MAX_DATA_SIZE, RD_Buffer, TRUE)) return FALSE; if(memcmp(RD_Buffer,(u8*)(MCU_FLASH_BASE+i*PRO_PAGE_SIZE+j*MAX_DATA_SIZE),MAX_DATA_SIZE)!=0) return FALSE; DelayMs(5); } } } //GO usr app if(!UART_GoUserApp(0)) return FALSE; return TRUE; }
void fixed_parking() { time_elapsed = 0; distance_traveled = 0; int spot = 0, exit = 0; uint16_t distance_temp; while (check_command(NOMESSAGE)); spot = UART_Read(); clear_last_received(); while (check_command(NOMESSAGE)); exit = UART_Read(); clear_last_received(); switch (spot) { case 1: { while (!Object(front2,13)) Motors_Forward(PWM,PWM); Rotate_Right(23); while (!Object(right,30)) Motors_Forward(PWM, PWM); while (!Object(front2,15)) Wall_Follow(right,12); Rotate_Left(23); distance_temp = distance_traveled; while (distance_traveled - distance_temp < 35) Wall_Follow(right,14); Rotate_Left(23); while (!Object(left,30)) Motors_Forward(PWM,PWM); while (!Object(front2,14)) Wall_Follow(left,3); Motors_Stop(); LEDs_Blink(12000); while (!Object(back,10)) Motors_Back(PWM,PWM); switch (exit) { case 1: Rotate_Right(23); while (Object(right,30)) Wall_Follow(right,14); Go_Forward(10); Rotate_Right(23); Go_Forward(50); break; case 2: Rotate_Right(23); while (Object(right,30)) Wall_Follow(right,14); while(!Object(right,30)) Motors_Forward(PWM, PWM); while(!Object(front2,15)) Wall_Follow(right,14); Rotate_Left(23); while (!Object(left,30)) Motors_Forward(PWM,PWM); while (Object(right,30)) Wall_Follow(left,10); Go_Forward(10); Rotate_Right(23); Go_Forward(50); break; case 3: Rotate_Left(23); while (!Object(front2,15)) Wall_Follow(left,14); Rotate_Right(23); while (!Object(right,30)) Motors_Forward(PWM,PWM); while (Object(right,30)) Wall_Follow(right,10); while (!Object(front2,15)) Motors_Forward(PWM, PWM); Rotate_Right(23); while (Object(left,30)) Wall_Follow(left,14); Go_Forward(12); Rotate_Left(23); Go_Forward(50); break; } break; } case 2: { while (!Object(front2,13)) Motors_Forward(PWM,PWM); Rotate_Right(23); while (!Object(right,30)) Motors_Forward(PWM, PWM); while (!Object(front2,15)) Wall_Follow(right,12); Rotate_Left(23); while (Object(right,30)) Wall_Follow(right,14); distance_temp = distance_traveled; while (distance_traveled - distance_temp < 10) Motors_Forward(PWM,PWM); Rotate_Left(23); while (!Object(left,30)) Motors_Forward(PWM,PWM); while (!Object(front2,14)) Wall_Follow(left,3); Motors_Stop(); LEDs_Blink(12000); Go_Back(40); switch (exit) { case 1: Rotate_Left(46); Go_Forward(50); break; case 2: Rotate_Right(23); while(!Object(right,30)) Motors_Forward(PWM, PWM); while(!Object(front2,15)) Wall_Follow(right,14); Rotate_Left(23); while (!Object(left,30)) Motors_Forward(PWM,PWM); while (Object(right,30)) Wall_Follow(left,10); Go_Forward(10); Rotate_Right(23); Go_Forward(50); break; case 3: Rotate_Left(23); while (!Object(left,30)) Motors_Forward(PWM,PWM); while (!Object(front2,15)) Wall_Follow(left,14); Rotate_Right(23); while (!Object(right,30)) Motors_Forward(PWM,PWM); while (Object(right,20)) Wall_Follow(right,10); while (!Object(front2,15)) Motors_Forward(PWM, PWM); Rotate_Right(23); while (Object(left,30)) Wall_Follow(left,14); Go_Forward(12); Rotate_Left(23); Go_Forward(50); break; } break; } case 3: { while (!Object(front2,13)) Motors_Forward(PWM,PWM); Rotate_Right(23); while (!Object(right,30)) Motors_Forward(PWM, PWM); while (!Object(front2,15)) Wall_Follow(right,12); Rotate_Left(23); while (Object(right,30)) Wall_Follow(right,14); while (!Object(right,30)) Motors_Forward(PWM,PWM); distance_temp = distance_traveled; while (distance_traveled - distance_temp < 10) Wall_Follow(right,14); Rotate_Left(23); while (!Object(left,30)) Motors_Forward(PWM,PWM); while (!Object(front2,14)) Wall_Follow(left,3); Motors_Stop(); LEDs_Blink(12000); while (!Object(back,10)) Motors_Back(PWM,PWM); switch (exit) { case 1: Rotate_Left(23); while (Object(left,30)) Wall_Follow(left,14); Go_Forward(10); Rotate_Left(23); Go_Forward(50); break; case 2: Rotate_Right(23); while(!Object(front2,15)) Wall_Follow(right,14); Rotate_Left(23); while (!Object(left,30)) Motors_Forward(PWM,PWM); while (Object(right,30)) Wall_Follow(left,10); Go_Forward(10); Rotate_Right(23); Go_Forward(50); break; case 3: Rotate_Left(23); while (Object(left,30)) Wall_Follow(left,14); while (!Object(left,30)) Motors_Forward(PWM,PWM); while (!Object(front2,15)) Wall_Follow(left,14); Rotate_Right(23); while (!Object(right,30)) Motors_Forward(PWM,PWM); while (Object(right,30)) Wall_Follow(right,10); while (!Object(front2,15)) Motors_Forward(PWM, PWM); Rotate_Right(23); while (Object(left,30)) Wall_Follow(left,14); Go_Forward(12); Rotate_Left(23); Go_Forward(50); break; } break; } case 4: { while (!Object(front2,13)) Motors_Forward(PWM,PWM); Rotate_Left(23); while (!Object(left,30)) Motors_Forward(PWM, PWM); while (!Object(front2,15)) Wall_Follow(left,12); Rotate_Right(23); while (Object(left,30)) Wall_Follow(left,14); while (!Object(left,30)) Motors_Forward(PWM,PWM); distance_temp = distance_traveled; while (distance_traveled - distance_temp < 13) Wall_Follow(left,14); Rotate_Right(23); while (!Object(right,30)) Motors_Forward(PWM,PWM); while (!Object(front2,14)) Wall_Follow(right,3); Motors_Stop(); LEDs_Blink(12000); while (!Object(back,10)) Motors_Back(PWM,PWM); switch (exit) { case 1: Rotate_Right(23); while (Object(right,30)) Wall_Follow(right,14); while (!Object(right,30)) Motors_Forward(PWM,PWM); while (!Object(front2,15)) Wall_Follow(right,14); Rotate_Left(23); while (!Object(left,30)) Motors_Forward(PWM,PWM); while (Object(left,30)) Wall_Follow(left,10); while (!Object(front2,15)) Motors_Forward(PWM, PWM); Rotate_Left(23); while (Object(right,30)) Wall_Follow(right,14); Go_Forward(12); Rotate_Right(23); Go_Forward(50); break; case 2: Rotate_Left(23); while(!Object(front2,15)) Wall_Follow(left,14); Rotate_Right(23); while (!Object(right,30)) Motors_Forward(PWM,PWM); while (Object(left,30)) Wall_Follow(right,10); Go_Forward(10); Rotate_Left(23); Go_Forward(50); break; case 3: Rotate_Right(23); while (Object(right,30)) Wall_Follow(right,14); Go_Forward(10); Rotate_Right(23); Go_Forward(50); break; } break; } case 5: { while (!Object(front2,13)) Motors_Forward(PWM,PWM); Rotate_Left(23); while (!Object(left,30)) Motors_Forward(PWM, PWM); while (!Object(front2,15)) Wall_Follow(left,12); Rotate_Right(23); while (Object(left,30)) Wall_Follow(left,14); distance_temp = distance_traveled; while (distance_traveled - distance_temp < 10) Motors_Forward(PWM,PWM); Rotate_Right(23); while (!Object(right,30)) Motors_Forward(PWM,PWM); while (!Object(front2,14)) Wall_Follow(right,3); Motors_Stop(); LEDs_Blink(12000); Go_Back(40); switch (exit) { case 1: Rotate_Right(23); while (!Object(right,30)) Motors_Forward(PWM,PWM); while (!Object(front2,30)) Wall_Follow(right,14); Rotate_Left(23); while (!Object(left,30)) Motors_Forward(PWM,PWM); while (Object(left,30)) Wall_Follow(left,10); while (!Object(front2,15)) Motors_Forward(PWM, PWM); Rotate_Left(23); while (Object(right,30)) Wall_Follow(right,14); Go_Forward(12); Rotate_Right(23); Go_Forward(50); break; case 2: Rotate_Left(23); while(!Object(left,30)) Motors_Forward(PWM, PWM); while(!Object(front2,15)) Wall_Follow(left,14); Rotate_Right(23); while (!Object(right,30)) Motors_Forward(PWM,PWM); while (Object(left,30)) Wall_Follow(right,10); Go_Forward(10); Rotate_Left(23); Go_Forward(50); break; case 3: Rotate_Right(46); Go_Forward(50); break; } break; } case 6: { while (!Object(front2,13)) Motors_Forward(PWM,PWM); Rotate_Left(23); while (!Object(left,30)) Motors_Forward(PWM, PWM); while (!Object(front2,15)) Wall_Follow(left,12); Rotate_Right(23); distance_temp = distance_traveled; while (distance_traveled - distance_temp < 33) Wall_Follow(left,14); Rotate_Right(23); while (!Object(right,30)) Motors_Forward(PWM,PWM); while (!Object(front2,14)) Wall_Follow(right,3); Motors_Stop(); LEDs_Blink(12000); while (!Object(back,10)) Motors_Back(PWM,PWM); switch (exit) { case 1: Rotate_Right(23); while (!Object(front2,15)) Wall_Follow(right,14); Rotate_Left(23); while (!Object(left,30)) Motors_Forward(PWM,PWM); while (Object(left,30)) Wall_Follow(left,10); while (!Object(front2,15)) Motors_Forward(PWM, PWM); Rotate_Left(23); while (Object(right,30)) Wall_Follow(right,14); Go_Forward(12); Rotate_Right(23); Go_Forward(50); break; case 2: Rotate_Left(23); while (Object(left,30)) Wall_Follow(left,14); while(!Object(left,30)) Motors_Forward(PWM, PWM); while(!Object(front2,15)) Wall_Follow(left,14); Rotate_Right(23); while (!Object(right,30)) Motors_Forward(PWM,PWM); while (Object(left,30)) Wall_Follow(right,10); Go_Forward(10); Rotate_Left(23); Go_Forward(50); break; case 3: Rotate_Left(23); while (Object(left,30)) Wall_Follow(left,14); Go_Forward(10); Rotate_Left(23); Go_Forward(50); break; } break; } } Motors_Stop(); }