how do I convert a number of 3 digits 001 to 255 to an unsigned char 0x00 to 0xFF?
thanks
conversion?
Re: conversion?
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)
-
- Posts: 282
- Joined: 17 Jun 2009 10:42
Re: conversion?
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
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 (24.16 KiB) Viewed 1007 times
Start every day with a smile...... (get it over with)
Re: conversion?
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
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)
Re: conversion?
On second thoughts...
Maybe this will do the job....
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)
-
- Posts: 282
- Joined: 17 Jun 2009 10:42
Re: conversion?
thanks hexreader
your code works perfectly.
your code works perfectly.