/////////////////////////////////////////////// //数据统一转成 拥有4位有效数字【科学计数法】 然后,4位有效数字以【int16】格式发送 如【1.234】按照【1234】发送 //科学计数位数以【int8】格式发送 如【10^12】以【12】发送 void PC_WriteNumNew(double data) { union i16tou8 { int16 value; uint8 datas[2]; }coef; int8 cnt=floor(log10(fabs(data))); coef.value=(int16)(data/pow(10,cnt-3)); UART0_SendByte(coef.datas[0]); UART0_SendByte(coef.datas[1]); UART0_SendByte((uint8)(cnt-3)); }
/********************************************************************** * @name UART0_SendString * * @param None * * @return int * * @description Write character to Serial Port * ***********************************************************************/ void UART0_SendString (unsigned char *s) { while (*s != 0) { UART0_SendByte(*s++); } }
//This function sends a string of characters on the serial port void UART0_SendStr(const unsigned char *str) { while(1) { if( *str == '\0' ) break; UART0_SendByte(*str++); } }
/*---------------------------------------------------------------------------* * Routine: UART0_SendDataBlock *---------------------------------------------------------------------------* * Description: * Send a group of data to the SCI2 transmit buffer. Block until * all data is sent. * Inputs: * uint8_t *aData -- Pointer to bytes to send out * uint32_t aLen -- Number of bytes to send * Outputs: * void *---------------------------------------------------------------------------*/ void UART0_SendDataBlock(const uint8_t *aData, uint32_t aLen) { /* Send each byte one at a time unless out of space */ uint32_t i; for (i = 0; i < aLen; ++i) { while (!UART0_SendByte(aData[i])); } }
/******************************************************************************************* * 函数名:UART0_SendString() * 参数 :uint8_t *s 指向字符串的指针 * 返回值:void * 描述 :UART0发送字符串函数 *********************************************************************************************/ void UART0_SendString(uint8_t *s) { while(*s) //判断是否到字符串末尾 { UART0_SendByte(*s); //发送指针当前所指的字节 s++; //地址加1 } }
/******************************************************************************************* * 函数名:UART0_Handler() * 参数 :void * 返回值:void * 描述 :UART0中断服务函数 *********************************************************************************************/ void UART0_Handler(void) { uint8_t temp; if((UART0->UART_SR& UART_SR_RXRDY)==1) { //接收数据中断 temp= UART0->UART_RHR&0xff; //接收一个字节 UART0_SendByte(temp); //将接收的数据发回 } }
/********************************************************************** * @name UART0_SendChar * * @param None * * @return int * * @description Write character to Serial Port * ***********************************************************************/ void UART0_SendChar(uint16_t disp) { uint16_t dispbuf[4]; uint8_t i; dispbuf[3] = disp%10 + '0'; dispbuf[2] = disp/10%10 + '0'; dispbuf[1] = disp/10/10%10 + '0'; dispbuf[0] = disp/10/10/10%10 + '0'; for(i=0;i<4;i++) UART0_SendByte(dispbuf[i]); }
/*---------------------------------------------------------------------------* * Routine: AtModem_Write *---------------------------------------------------------------------------* * Description: * ATCmdLib callback to write a string of characters to the module. * Inputs: * const uint8_t *txData -- string of bytes * uint32_t dataLength -- Number of bytes to transfer * Outputs: * void *---------------------------------------------------------------------------*/ void AtModem_Write(const void *txData, uint16_t dataLength) { const uint8_t *tx = (uint8_t *)txData; while (dataLength--) { /* Keep trying to send this data until it goes */ while (!UART0_SendByte(*tx)) { } tx++; } }
/*---------------------------------------------------------------------------* * Routine: UART0_SendData *---------------------------------------------------------------------------* * Description: * Attempts to send a group of data to the SCI2 transmit buffer. * Returns the number of bytes actually sent. * Inputs: * uint8_t *aData -- Pointer to bytes to send out * uint32_t aLen -- Number of bytes to send * Outputs: * void *---------------------------------------------------------------------------*/ uint32_t UART0_SendData(const uint8_t *aData, uint32_t aLen) { uint32_t i; /* Send each byte one at a time unless out of space */ for (i = 0; i < aLen; ++i) { if (!UART0_SendByte(aData[i])) break; } /* Return the number of bytes that did get into the transmit FIFO */ return i; }
int main(void) { char usFdiv; SystemInit(); UART0_Init(); //初始化串口 Rest(); //复位 UART0_GetChar(17); //接收复位信息 UART0_SendByte(4,SendBuf_0); //发送提速命令 UART0_GetChar(4); LPC_UART0->LCR = 0x83; /* 允许设置波特率 */ usFdiv = (11059200*2 / 16) / 230400; /* 设置波特率 */ LPC_UART0->DLM = usFdiv / 256; LPC_UART0->DLL = usFdiv % 256; LPC_UART0->LCR = 0x1F; /* 锁定波特率 */ LPC_UART0->FCR = 0x87; //清空接收发送寄存器 UART0_SendByte(5,SendBuf_1); //发送获取随机数命令 UART0_GetChar(11); //获取随机数 while(1); }
//This function is UART0 Receive ISR. This functions is called whenever UART0 receives any data void __irq IRQ_UART0(void) { Temp = U0RBR; if(Temp == 0x38) //ASCII value of 8 { Forward(); //forward } if(Temp == 0x32) //ASCII value of 2 { Back(); //back } if(Temp == 0x34) //ASCII value of 4 { Left(); //left } if(Temp == 0x36) //ASCII value of 6 { Right(); //right } if(Temp == 0x35) //ASCII value of 5 { Stop(); //stop } if(Temp == 0x37) //ASCII value of 7 { BUZZER_ON(); } if(Temp == 0x39) //ASCII value of 9 { BUZZER_OFF(); } VICVectAddr = 0x00; UART0_SendByte(Temp); //Echo Back received character }
/******************************************************************************************************************************** *函数名:fputc() * 参数:int ch,FILE *f * 返回值:int * 功能:重新定义stdio.h中的fputc()函数,使printf()输出到USART1 *********************************************************************************************************************************/ int fputc(int ch,FILE *f) { UART0_SendByte((uint8_t)ch); //发送1个字节 return ch; //返回 ch }