Byte to Integer

General discussion on mikroPascal.
Post Reply
Author
Message
lewjoubert
Posts: 68
Joined: 12 Apr 2006 11:55
Location: Gold Coast Australia
Contact:

Byte to Integer

#1 Post by lewjoubert » 17 Apr 2006 08:53

Hi,
I have a little problem in that I want to convert a USART received byte into an Integer.
How is this possible?
Lew.
DISCLAIMER: Content reflects the thoughts & opinions of my goldfish neighbour's mad dog; don't quote me on that; don't quote me on anything; hand wash only, tumble dry on low heat; do not bend or fold.

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

Re: Byte to Integer

#2 Post by zristic » 17 Apr 2006 09:28

Code: Select all

program bytetoint;
var myByte: byte;
    myInt: integer;
begin
  // solution 1
  myInt := myByte; // compiler will make implicit conversion
  // solution 2
  myInt := integer(myByte); // you made explicit conversion

// solution 1 makes less code.
end.

lewjoubert
Posts: 68
Joined: 12 Apr 2006 11:55
Location: Gold Coast Australia
Contact:

#3 Post by lewjoubert » 17 Apr 2006 10:21

Thanks for that info - I had tried both but came up with the same problem.

This is what I'm doing:

program ......
var
txt:byte;
counter3:integer;
begin
<I receive byte from UART into variable txt eg: 8>

counter3 := txt; // or I try counter3 := integer(txt);

<I then send Uart_Write(counter3). The value I receive from the PIC is then 56 (ASCII value) instead of 8>

If instead of counter3 := txt I hard code it like counter3 := 8 then when I receive the value back from the PIC (counter3) it is correct (8).

What am I doing wrong?

end.
DISCLAIMER: Content reflects the thoughts & opinions of my goldfish neighbour's mad dog; don't quote me on that; don't quote me on anything; hand wash only, tumble dry on low heat; do not bend or fold.

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

#4 Post by jpc » 17 Apr 2006 10:48

are you receiving a byte or a ascii character ? calling a byte txt is a little suspicious !

lewjoubert
Posts: 68
Joined: 12 Apr 2006 11:55
Location: Gold Coast Australia
Contact:

#5 Post by lewjoubert » 17 Apr 2006 11:07

.... I thought maybe its best I post a piece of the code to explain better what I'm trying to achieve, and where I'm going wrong.

I want to send a numeric value to update variable (counter3 in this case)
The problem is, the value I send (eg 8) gets placed into the variable (counter3) alright, except that it is the ASCII equivalent (56) of the numeric value sent.

program A51AC08;

// Global variables here

var counter3:integer;
sender: string[10];
menu1:byte;
control1:byte;
txt :byte; // for Read byte RS232


label label1;

procedure interrupt;
begin

counter1 := counter1 + 1;

if counter1 = 7 then // constant for 0.1 second
begin
counter2 := counter2 + 1; // counter2 incremented every 0.1 second
counter1 := 0;
end;

if counter2 = 10 then // constant for 1.0 second
begin
counter3 := counter3 + 1; // counter3 incremented every 1.0 second
counter2 := 0;
end;

// *** RTC
if counter3 = 60 then // constant for 1 minute
begin
minutes := minutes +1;
counter3 := 0
end;
if minutes = 60 then // constant for 1 hour
begin
hours := hours +1;
minutes := 0
end;
if hours = 24 then // constant for 1 day
begin
days := days + 1;
hours := 0;
end;
if days = 7 then // constant for 1 week
begin
days := 1;
end;

INTCON := 0x20;

end;


begin

counter1 := 0;
counter2 := 0;
counter3 := 0;
minutes := 0;
hours := 0;
days := 1;
//txt := 'N';
txt := '';
control1 := 0x00;
menu1 := 0x00;


OPTION_REG := 0x07; // Prescaler 256

INTCON := 0xA4; // Turn on TOIF bit else won't activate interrupt

//Initialize ports.
ADCON1 := 0x06; // Port A all digital ports
TRISA := 0xFF; // All inputs
TRISB := 0x00; // All outputs
TRISC := 0x03; // 0/1 inputs, rest output

PORTA := 0x00;
PORTB := 0x00; // ensure all outputs OFF at start
PORTC := 0x00;

// Initialize the USART comme port
Usart_Init(2400);


while true do
begin

//Monitor the RS232 RX
label1:


if Usart_Data_Ready() = 1 then txt := Usart_Read;


// Set counter3
if txt = 'T' then
begin
menu1.0 := 1;
txt := '';
end;
if txt = 'H' then
begin
menu1.1 := 1;
//txt := '';
end;
if txt = 'M' then
begin
menu1.2 := 1;
//txt := '';
end;
if txt = 'S' then
begin
menu1.3 := 1;
//txt := '';
end;
if txt = 'D' then
begin
menu1.4 := 1;
txt := '';
end;


if (menu1.0 = 1) and (menu1.4 = 1) then
begin
if (txt >= '1') and (txt <= '9') then
begin
counter3 := integer(txt);
menu1.0 := 0;
menu1.4 := 0;
txt := '';
end;
end;
// End of setting


{*** Send value in counter3 via RS232 ***}

INTCON.GIE := 0; // DISABLE INTERRUPTS
IntToStr(counter3,sender);
INTCON.GIE := 1; // EBABLE INTERRUPTS


// Arbitary routine to visualize fuctionality
if PORTA.0 = 1 then
begin
PORTB.7 := 1;
Usart_Write_Text(sender);
end
else
if PORTA.0 = 0 then
begin
PORTB.7 := 0;
end;

end; // End of MAIN While Loop

end.

... all/any help appreciated.

Lew.
DISCLAIMER: Content reflects the thoughts & opinions of my goldfish neighbour's mad dog; don't quote me on that; don't quote me on anything; hand wash only, tumble dry on low heat; do not bend or fold.

Post Reply

Return to “mikroPascal General”