conversion?

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
HyperUniverse
Posts: 282
Joined: 17 Jun 2009 10:42

conversion?

#1 Post by HyperUniverse » 26 Sep 2022 11:51

how do I convert a number of 3 digits 001 to 255 to an unsigned char 0x00 to 0xFF?

thanks

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: conversion?

#2 Post by hexreader » 26 Sep 2022 12:38

Code: Select all

// PIC18F45K22 with 8MHz 2-pin crystal and 4xPLL for 32MHz clock
// be sure to tick the tick-box for Conversions library

char test_string[] = "123";                 // test value in string form
unsigned char result;

void main()
{
    ANSELB = 0;                             // all digital
    TRISB = 0;                              // all output

    result = StrToByte(test_string);
    
    LATB = result;                          // use result to prevent compiler optimising it away
    
    while (1);                              // loop forever
}
Start every day with a smile...... (get it over with) :)

HyperUniverse
Posts: 282
Joined: 17 Jun 2009 10:42

Re: conversion?

#3 Post by HyperUniverse » 26 Sep 2022 16:31

Hi hexreader

Code: Select all

StrToByte
missing

anybody got the function for this?

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: conversion?

#4 Post by hexreader » 26 Sep 2022 16:34

StrToByte is one of the functions in the conversions library.

Which PIC are you using?

Maybe this function is not available for the particular PIC you have selected, though picking a random PIC12 - conversions function is selectable
Attachments
Screenshot 2022-09-26 164226.png
Screenshot 2022-09-26 164226.png (24.16 KiB) Viewed 1007 times
Start every day with a smile...... (get it over with) :)

HyperUniverse
Posts: 282
Joined: 17 Jun 2009 10:42

Re: conversion?

#5 Post by HyperUniverse » 26 Sep 2022 16:51

dsPIC33EP512GM706

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: conversion?

#6 Post by hexreader » 26 Sep 2022 17:01

Seems you posted in the wrong sub-forum then

I agree, that there is no StrToByte function available for mikroC Pro for dsPIC compiler

I have no solution to offer
Start every day with a smile...... (get it over with) :)

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: conversion?

#7 Post by hexreader » 26 Sep 2022 18:23

On second thoughts...

Maybe this will do the job....

Code: Select all

// PIC33FJ128GP10 fitted to LV24-33 v6 dev board
// amateur code - use at own risk
// author accepts no responsibility for anything, anywhere, ever

char test_string[] = "123";                 // test value in string form
unsigned char result;

// convert 3 character null-terminated string to unsigned char
// MUST be of the form "000" to "255" - three valid decimal characters, no spaces allowed
unsigned char StrToByte(char *StByte){
  unsigned char Bresult;
    Bresult = (StByte[0] - '0') * 100;      // hundreds
    Bresult += (StByte[1] - '0') * 10;      // tens
    Bresult += StByte[2] - '0';             // units
    return Bresult;
}

void main()
{
    AD1CON1 = 0;                            // ADC1 off
    AD2CON2 = 0;                            // ADC2 off
    AD1PCFGH = 0xffff;                      // all digital
    AD1PCFGL = 0xffff;                      // all digital
    AD2PCFGL = 0xffff;                      // all digital

    TRISA = 0;                              // all output

    result = StrToByte(test_string);
    
    LATA = result;                          // use result to prevent compiler optimising it away

    while (1);                              // loop forever
}
Start every day with a smile...... (get it over with) :)

HyperUniverse
Posts: 282
Joined: 17 Jun 2009 10:42

Re: conversion?

#8 Post by HyperUniverse » 27 Sep 2022 10:58

thanks hexreader

your code works perfectly.

Post Reply

Return to “mikroC PRO for PIC General”