Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

routine for converting hex values to decimal equivalent in C

Status
Not open for further replies.

mgbglasgow

Junior Member level 1
Joined
Jun 15, 2011
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,508
I am accepting settings for my pic via rs232 from pc, i wish to enter values from 0-999 for settings and store the 3 values as units,tens,hundreds and use these in my timer loops.

My question is how do i capture that data , my compiler doesnt allow me to use sprintf (XC8 v1.33) and im not sure how to approach this problem.

if user enters "60" then i'm in danger of entering "6" in the hundreds area of eeprom, so some intelligence in what is input is also required.

Does anyone know how to address this problem best,

Steven
 

you will have to send a string of 3 ascii char
ie "999" to your MCU , probably with CR (carriage return or Enter) to valide and send it
so your MCU will receive this string into a buffer

your receiving routine must put zero into the buffer if detect CR car.

ie :

Code:
char buff[16];
//if you send 999 ,buff will contain 
buff[0]='9';    // or 48+9=57 decimal 
buff[1]='9';
buff[2]='9';
buff[3]=0 ;  // because code of CR detected

when you receive CR code , you must put a Zero value to replace CR value and terminate the string

Code:
//if you send 6 value, you will receive
buff[0]='6'   // or 48+6=54
buff[1]=0
CR received so buff[1]=0 // string terlmminator

check if you have a libray like "stdlib.h" (exist in C18)
to use a function to tranform ascii string into int value.

declare an integer value:


Code:
int TimerValue;

//transform buff witch contain the string (ascii value) to an integer
Timervalue=atoi(buff);
TMR0H= Timervalue <<8;
TMR0L= Timervalue & 0x00FF;
 

Excellent reply, many thanks,

i will write the code and see how i get on,,, its a fresh change to get an actual answer than someone posting a question about content of the post, grammar, or just plain "they havent read it very closely",

Thanks

Steven
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top