I had to use an helper function to convert from int to byte array, since Serial.write does not support directly an int. Serial.print(datastring) // Sends 0x31 0x30 0x32 Serial.print(convertToByteArrayBigEndian(supportArray,data32),4) // Sends 0x00 0x00 0x00 0圆4 Serial.print(convertToByteArrayLittleEndian(supportArray,data32),4) // Sends 0圆4 0x00 0x00 0x00 Uint8_t *convertToByteArrayBigEndian(uint8_t *buffer, int32_t val) What I suggest you is to send them in binary mode, so uint8_t *convertToByteArrayLittleEndian(uint8_t *buffer, int32_t val) This, as you noticed, is highly inefficient. So: uint8_t data8 = 102 Įach of these are sent exactly in the same way, i.e. Serial.print sends the ASCII representation of the data. Take a look at it and you can see how you can craft a packet to send lots of data as one stream of data. One example of doing that is my ICSC library. Instead it's better to buffer up a batch of values and send them as a block with just a few extra bytes to define the packet. Just how you do that is entirely up to you - do you just wrap each pair of bytes in a special set of other bytes? Nah - that becomes as wasteful as Serial.print. So you need some way of "packetizing" the data so that you know "This is a correct set of values". how do you know which byte coming in is which? It's very very easy to get out of sync and not get the right values when you read the data in. You can't send one byte for the full value, so you would have to send it as two bytes: Serial.write((myVal > 8) & 0xFF) Serial.write(myVal) įor anything greater than 255, i.e., you want to send an int or uint16_t then you will have to come up with some better way. If you are sending values between 0 and 255 ( uint8_t) you can just send the raw value as a single ASCII character, since there are 256 of those. The number of characters depends on the size of the number. Serial.print will format the number as a string of ASCII characters. That is simply because the maximum is 11520 bytes per second. At 115200 baud you can't transmit 2 x 6000 bytes per second. In fact, fiddling with the prescaler isn't really the issue here because the serial transmission time is the limiting factor.įorget about the ADC and how it works for the moment. You can sample asynchronously (in the background) so you transmit one sample while taking the next one, but you are still limited by the transmission speed of serial, so it would still take 174 µs per sample. This is too slow if you want to achieve it inside 167 µs. If you sample then transmit you will therefore take, per sample: 104 + 174 = 278 µs At 115200 baud you can transmit one byte every 1/11520 seconds (86.8 µs) so therefore two bytes (16 bits) would take 173.6 µs. You also need to take into account the transmission time. The number of conversions you can do a second (taking the inverse of the above) is: Prescaler Conversions/sec If you go for maximum resolution (10 bits) you need a prescaler of 128, which takes 104 µs. Amongst other things is a table of conversion times for the different ADC prescalers, assuming a 16 MHz clock: PrescalerĢ * 13 * 1/16E6 = 0.000001625 ( 1.625 µs) I have a lengthy discussion about the ADC hardware. Lcd.OK, so that means you need to sample and send every 1/6000 seconds (167 µs). Pins for LCD: RS, RW, E, D4, D5, D6 ,D7 I want the formatted output as this is the way to get the number correctly alligned.ĭoes some body know how? Or do I have to program my own formatting routine? Is this a bugg? Is this a restriction of the compiler? Do I need a special format string in stead of %d? The sprintf works ok for number below 32767 (0X7FFF). It seems that sprintf %d only works on the least significant bytes of the 4 byte long value. When I use formated printing with sprintf the result is wrong. When I just print the value direct it is correctly displayed. I assign it a number that is larger then would fit in a 2 byte integer. The code snipped below is to illustrate my problem. I have been fighting with this for a long time.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |