完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛|
我需要一些帮助使用XC8编译器在PIC18F26K80上进行UART通信。我有TX和RX连接到蓝牙,以将数据发送到Android手机。当我使用我的函数UARTHARY WORD时,我只能写一个数字(0—9)或一个字符,而当它被两个撇号EX“8”所包围时,电话就在另一端被捡起。当我使用UARTHI WrreEXT文本时,当它被两个引号括起来时,能够发送一个字符串和/或数字。“Hello World”。因为必须使用“我不能发送一个值,我写一个变量。有没有办法来解决这个问题,还是有更好的方法来编写UART代码。我已经把代码粘贴到这个线程中,并附上我当前正在运行的代码,我对所有的建议都很开放,并且非常感激任何输入。 以上来自于百度翻译 以下为原文 I need some help with a UART communication on a PIC18F26K80 using XC8 complier. I have the TX and RX connected to Bluetooth to send the data to an android phone. When I use my function UART_Write I am only able to write a one digit number (0-9) or one character and the phone picks it up on the other end when it is enclosed by two apostrophes ex. '8'. When I use UART_Write_Text and am able to send a string of characters and/ or numbers when it is enclosed by two quotation marks ex. "Hello World". Because of having to use the " " I am not able to send a value that I write to a variable. Is there any way around this or is there a better way to write Uart code. I have pasted the code into this thread along with attaching the code that I am currently running, I am open to all suggestions and would really appreciate any input. /*PIC18F26k80 http://ww1.microchip.com/downloads/en/DeviceDoc/39977f.pdf */ #define _XTAL_FREQ 8000000 #include #include "UART.h" void main() { TRISC4 = 0; // LED output TRISC3 = 0; // LED output TRISC2 = 0; // LED output TRISC5 = 1; // input for data LATC4 = 1; // initiate LED to on LATC3 = 0; // initiate LED to off LATC2 = 0; // initiate LED to off UART_Init(9600); // baud rate while(1) { //UART_Write('8'); // only will send a 1 digit number or letter inclosed by '' UART_Write_Text("5"); // must include "" to send a string of numbers or letters LATC4 = ~LATC4; // toggle LED to show program is running through the loop __delay_ms(1000); // how often information is sent } } /*PIC18F26k80 http://ww1.microchip.com/downloads/en/DeviceDoc/39977f.pdf */ char UART_Init(const long int baudrate) { unsigned int x; TRISC6 = 0; TRISC7 = 1; x = (_XTAL_FREQ - baudrate*64)/(baudrate*64); //x = (_XTAL_FREQ / baudrate / 64) - 1; // same as above, different formula if(x<256) // low speed (using for this example) { TXSTA1bits.BRGH = 0; // 8-bit/Asynchronous SPBRG1 = x; // speed setting TXSTA1bits.SYNC = 0; // 0 = Asynchronous mode of EUSART RCSTA1bits.SPEN = 1; // 1 = Serial port is enabled (configures RXx/DTx and TXx/CKx pins as serial port pins) RCSTA1bits.CREN = 1; // 1 = Enables continuous receiver PIE1bits.TX1IE = 0; TXSTA1bits.TXEN = 1; // 1 = Transmit is enabled return 1; } return 0; } void UART_Write(char data) { while(!TXSTA1bits.TRMT); // 1 = TSR is empty | 0 = TSR is full // Transmit Shift Register Status bit waits until TSR is full TXREG1 = data; // Tx is filled with data to transmit LATC3 = ~LATC3; // LED Lights when data is transfered } void UART_Write_Text(char *text) { int i; for(i=0;text!=' |
