完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
亲爱的,我想把4个数字(每个14位)从DSPIC发送到UART上的PC。所以我试着把每个数字分成2个部分(每8个比特),然后用UART发送如下。得到一些随机输出。请帮忙?我的发送逻辑只包含在代码中。我应该接收“4242 1024 1234 3015”(十进制)或“1092 0400 04D2 0BC7”(HEX)。对的。。。。。。。。。。。?
以上来自于百度翻译 以下为原文 Dear all, I want to send 4 numbers(each 14 bit) from dsPIC to PC over UART. So i tried to divide each number to 2 parts(each 8 bits) and send by UART as follows. Getting some random outputs. Please help? My sending logic only included in code. void Send_EnCoderInfo(unsigned int count1,unsigned int count2,unsigned int count3,unsigned int count4) { unsigned char cnt; unsigned char send_buffer[12] = {0x00}; send_buffer[0] = count1>>8; send_buffer[1] = count1 & 0xff; send_buffer[2] = ' '; send_buffer[3] = count2>>8; send_buffer[4] = count2 & 0xff; send_buffer[5] = ' '; send_buffer[6] = count3>>8; send_buffer[7] = count3 & 0xff; send_buffer[8] = ' '; send_buffer[9] = count4>>8; send_buffer[10] = count4 & 0xff; send_buffer[11] = ' '; for(cnt = 0; cnt < 12; cnt++) { U2TXREG = (send_buffer[cnt]); } // U2TXREG =' '; //send space after each count value U2TXREG = 0x0A; //keyboard enter after each count value // U2TXREG = 0x0D; //keyboard enter after each count value } int main() { unsigned int encoder_count1=4242; unsigned int encoder_count2=1024; unsigned int encoder_count3=1234; unsigned int encoder_count4=3025; while(1) { Send_EnCoderInfo(encoder_count1,encoder_count2,encoder_count3,encoder_count4); } } I should receive "4242 1024 1234 3015"(decimal) or"1092 0400 04D2 0BC7"(Hex). correct...........? |
|
相关推荐
15个回答
|
|
谢谢您。在我的终端窗口中,我有选择来检查ASCII或二进制或十六进制的十六进制。但是所有的数字都被混淆了,并且有一些错误。你能建议一些无差错的机制来发送这4个数字吗?
以上来自于百度翻译 以下为原文 Thank you. In my terminal window i have option to check in ASCII or BINARY or HEX of DECIMAL. But all numbers are getting mixed up and there are some errors. Can you suggest some error free mechanism to send these 4 numbers? |
|
|
|
使用Strutff()将数字格式化成十六进制格式的字符串,然后发送该字符串。
以上来自于百度翻译 以下为原文 Use sprintf() to format the numbers into a string in hex format, then send that string. |
|
|
|
你还没有说你希望他们怎样到达。ASCII十进制ASCII十六进制。现在你用原始二进制文件发送它们。那么二进制十六进制?
以上来自于百度翻译 以下为原文 You still have not said how you would like them to arrive. Ascii decimal Ascii hex. Right now you are sending them in raw binary. So Binary hex? |
|
|
|
我想以十六进制的形式到达数据。请让我知道如何用代码写。
以上来自于百度翻译 以下为原文 I would like to arrive the data in HEX. Please let me know how to write in code. |
|
|
|
我支持StastFF(),使用%02x作为说明符。在上面的代码中,在任何情况下,您都应该等待TX缓冲器在任何字符之后被“完成”。
以上来自于百度翻译 以下为原文 I support the sprintf() idea, using %02X as specifier. In the code above, in any case, you should wait for TX buffer to be "done", after any char. |
|
|
|
我想我已经做过了。你知道如何使用Simulf()吗?正如Cinzia刚才提到的,在第一篇文章中的另一个大问题是,在编写下面的字符之前,不要等待TXREG准备就绪。
以上来自于百度翻译 以下为原文 I thought I already did. Do you know how to use sprintf() ? As Cinzia just mentioned, another huge problem in your first post is you weren't waiting for the TXREG to be ready before writing the following character. |
|
|
|
你应该收到0x92 0x10 0x00 0x40等等……编辑&对不起。我还没有完全清醒。您将收到:0x10 0x92 0x20 0x40 0x00 0x20等…
以上来自于百度翻译 以下为原文 |
|
|
|
我对UART/SCANTFF了解不多。第一次工作。所以请大家在DSPIC33(微芯片)上对UART有很好的理解。
以上来自于百度翻译 以下为原文 I do not know much about UART/ sprintf. Working for the first time. So request all to suggest some reference for good understanding about UART in dsPIC33(microchip only).pink: |
|
|
|
Simulff()是标准C,所以你可以在任何一本书上读到它。然后,从上面的代码开始,你可以做类似这样的事情。
以上来自于百度翻译 以下为原文 sprintf() is standard C so you can read about it on any book. Then, starting from your code above, you can do something like this char send_buffer[32]; sprintf(send_buffer, "%02X %02X %02Xn",count3, count4, etc); for(cnt = 0; cnt < 10; cnt++) // use strlen or alike { U2TXREG = (send_buffer[cnt]); while(U2TXIF); // pseudo code, check real flag as we said above. }[/code] |
|
|
|
有两种不同的方式来发送数据:二进制,这样你就可以用程序文本读取它,这样你就可以用你的眼睛看到终端软件了,你需要选择你想要的。
以上来自于百度翻译 以下为原文 There are two different ways to send data: - binary, so that you can read it with your program - text, so that you can see it with your eyes using terminal software First, you need to choose which of them you want. |
|
|
|
是的,在HTTP://www. McCHIP.COM/FUMMS/FUNDSPOT/94179之上被问及并再次确认。
以上来自于百度翻译 以下为原文 Yeah, it was asked and stated and reasked and again above http://www.microchip.com/forums/FindPost/994179 |
|
|
|
很难说“HEX”是什么意思。它可能意味着二进制(因为人们使用十六进制编辑器编辑二进制数据),或者它可能意味着文本(因为十六进制是二进制数据的文本表示),所以,十六进制并不重要。是终端数据还是PC程序?
以上来自于百度翻译 以下为原文 It's hard to tell what people mean by "HEX". It may mean binary (because people use HEX editor to edit binary data) or It may mean text (because HEX is a textual presentation of binary data) So, HEX doesn't mean much. Is the data for the terminal or for your program on PC? |
|
|
|
正如CCICIA和QHB已经写到的,你的主要问题是你根本不等待传输完成。因此,您的数据会被混淆,因为在实际发送之前,您正在重写一些要发送的数据。为了使它更复杂,UART有一个内部缓冲区,例如4字节(不知道你的PIC),所以很难判断错误看起来是什么样的。因为你的终端程序可以同时显示ASCII和HEX,你应该把它转换成十六进制,然后推迟如何格式化你的串行。数据,直到您修复了bug。我首先推荐使用您的原始代码,但是正如Cincia所说,添加所需的状态检查。即在每次写入U2TxRG之前,必须检查状态位……(U2Stest.UTXBF);U2TXRG= =(SeNdHuffer-[CNT])……或从腹膜中起函数。你可能会在你的终端程序中看到它不聚集16位数字(如你所想象的),而是简单地显示字节序列。然后,您可以使用SaveTFF将数据格式化为更好的人类可读格式(无论对您来说哪个更好)。如果你需要更多的帮助,你应该用PC上接收到的数据来描述你真正想要做的事情。
以上来自于百度翻译 以下为原文 As Cincia and qhb already wrote your primary problem is that you simply do not wait for transmission to be finished. Thus your data gets mixed up, because you are overwriting some of the data to be sent, before it actually is sent. To make it more complicated the UART has an internal buffer of e.g. 4 bytes (don't know for your PIC) so its difficult to tell how the error really looks alike. Since your Terminal program can display both ASCII and HEX, you should switch it to HEX and postpone the question how to format your serial data until you have fixed the bug. I would recommend first simply using your original code, but as Cincia said, add the required status checking. I.e. before each write to U2TXREG you must check the status bits. ... while(U2STAbits.UTXBF); U2TXREG = (send_buffer[cnt]); ... or make a function out of it void uart2Putc(char ch) { while(U2STAbits.UTXBF == 1); // wait while buffer is full (until at least one data word can be written) U2TXREG = ch; } Once this works you will likely see in your terminal program that it does not aggregate 16 bit numbers (as you imagine) but simply displays a byte sequence. You can then use sprintf to format your data into better human readable format (whatever is better for you). If you need more help you should describe what you really want to do with the received data on the PC. |
|
|
|
在您的代码中首先检查UART缓冲区溢出!
以上来自于百度翻译 以下为原文 In your code first check UART buffer for overflow!while(!U2STAbits.TRMT); U2TXREG = (send_buffer[cnt]); |
|
|
|
在回答之前阅读整个主题是一个好主意。检查UTXBF比TrMT更有效,这是罗迪姆斯在两周前发表的第15页中提出的建议。
以上来自于百度翻译 以下为原文 It's a good idea to read the entire topic before replying. It is more efficient to check UTXBF than TRMT, which is what rodims suggested in post#15 two weeks ago. |
|
|
|
只有小组成员才能发言,加入小组>>
5234 浏览 9 评论
2026 浏览 8 评论
1950 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3201 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2253 浏览 5 评论
771浏览 1评论
659浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
588浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
670浏览 0评论
571浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-20 13:50 , Processed in 1.556917 second(s), Total 106, Slave 90 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号