• AVR Freaks

Harmony UART driver, converting int to uint8_t

Author
Blargian
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2018/04/17 10:54:25
  • Location: 0
  • Status: offline
2018/07/21 14:27:21 (permalink)
0

Harmony UART driver, converting int to uint8_t

I am trying to output some ADC readings from a potentiometer over USART using the PIC32MX220F032B. I have the hardware all setup and working and i've set up a new project using MPLAB harmony and have the PIC picking up values from a potentiometer using the ADC module. I've set up the USART module too and I am able to output single bytes at a time using Harmony's DRV USART driver ByteWrite function however I have been struggling for hours to try convert the integer ADC readings into an ASCII representation that I can send to Putty, my terminal program. The problem is such that the harmony driver function DRV_USART0_WriteByte(argument); takes a uint8_t as argument.
I tried writing the following functions to convert each decimal to a uint8_t and transmit using the driver write function:
void intToByte(int input){
    
    int j =0;
    int size = numPlaces(input);
    
     while(j<size){
     appData.buffer = input % 10; // remainder of division with 10 gets the last digit
     input /= 10; // dividing by ten chops off the last digit.
     DRV_USART0_WriteByte(appData.buffer);
     j++;
}
    
}


int numPlaces (int n) {
    if (n == 0) return 1;
    return floor (log10l(abs(n)))+ 1;
}

unfortunately I'm just getting different non sensical ASCII characters in my serial terminal program.
I was wondering if there are any PIC32 experts who have worked with Harmony who might be able to tell me how they approach printing integers using DRV_USART0_WriteByte().
 
I have attached the main application .c and header file
post edited by Blargian - 2018/07/21 14:49:52

In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move. " - Douglas Adams
#1
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: Harmony UART driver, converting int to uint8_t 2018/07/21 14:41:01 (permalink)
0
DRV_USART0_WriteByte(appData.buffer + '0');

may work better. But still your chars would be left-right reversed...

There are a number of ways to perform the above, especially avoiding that log( down there! :)

GENOVA :D :D ! GODO
#2
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 12032
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Harmony UART driver, converting int to uint8_t 2018/07/21 14:44:03 (permalink)
4 (1)
You're not converting to ASCII anywhere that I can see.  You need to add '0' to your decimal byte to get ASCII.
#3
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 12032
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Harmony UART driver, converting int to uint8_t 2018/07/21 14:46:42 (permalink)
5 (1)
I haven't seen this forum bug before.  The "Related threads" and post menu are in a mono font because the OP didn't close the code tag correctly...
#4
Blargian
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2018/04/17 10:54:25
  • Location: 0
  • Status: offline
Re: Harmony UART driver, converting int to uint8_t 2018/07/21 14:51:52 (permalink)
0
jtemples
I haven't seen this forum bug before.  The "Related threads" and post menu are in a mono font because the OP didn't close the code tag correctly...




My apologies for that. With '0' I can now see the correct numbers. Thank you. Will think of how to fix the wrong ordering as mentioned above. 
 
Are there easier ways to do this? Any std c functions I could use?  

In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move. " - Douglas Adams
#5
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: Harmony UART driver, converting int to uint8_t 2018/07/21 14:56:43 (permalink)
5 (1)
Yep,
sprintf(buf,"%u",input);

GENOVA :D :D ! GODO
#6
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: Harmony UART driver, converting int to uint8_t 2018/07/21 14:57:19 (permalink)
0
jtemples
I haven't seen this forum bug before.  The "Related threads" and post menu are in a mono font because the OP didn't close the code tag correctly...




No John, it was there some 10 years ago or more: I remember posting a surprised reply somewhere...

GENOVA :D :D ! GODO
#7
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 12032
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Harmony UART driver, converting int to uint8_t 2018/07/21 15:51:24 (permalink)
4 (1)
DarioG
 
sprintf(buf,"%u",input);

 
+1 for using %u instead of %d.
#8
nigelwright7557
Super Member
  • Total Posts : 471
  • Reward points : 0
  • Joined: 2006/11/06 08:15:51
  • Location: 0
  • Status: offline
Re: Harmony UART driver, converting int to uint8_t 2018/07/21 17:12:35 (permalink)
3 (1)
A simple fix is to divide a2d bits by 1000 and add '0' to it and display that.
Keep the remainder and divide that by 100 then 10 then 1 for the last digit.
 
#9
muellernick
Super Member
  • Total Posts : 476
  • Reward points : 0
  • Joined: 2015/01/06 23:58:23
  • Location: Germany
  • Status: offline
Re: Harmony UART driver, converting int to uint8_t 2018/07/23 00:21:50 (permalink)
4 (1)
I suggest that distinct users flag themselves as "maker".
And that Microchips offers a functionality to filter out all postings with certain flags (like "maker").
 
 
I have been struggling for hours to try convert the integer ADC readings into an ASCII representation

Really!?
Is this your very first C program you are writing? At least that would explain why your question isn't related to Harmony at all.
 
 
Nick
#10
Jump to:
© 2020 APG vNext Commercial Version 4.5