It is currently 19 Nov 2018 14:40

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Byte to Integer
PostPosted: 17 Apr 2006 08:53 
Offline

Joined: 12 Apr 2006 11:55
Posts: 68
Location: Gold Coast Australia
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.


Top
 Profile  
 
 Post subject: Re: Byte to Integer
PostPosted: 17 Apr 2006 09:28 
Offline
mikroElektronika team
User avatar

Joined: 03 Aug 2004 12:59
Posts: 6608
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.


Top
 Profile  
 
 Post subject:
PostPosted: 17 Apr 2006 10:21 
Offline

Joined: 12 Apr 2006 11:55
Posts: 68
Location: Gold Coast Australia
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.


Top
 Profile  
 
 Post subject:
PostPosted: 17 Apr 2006 10:48 
Offline

Joined: 22 Apr 2005 17:40
Posts: 1975
Location: France 87
are you receiving a byte or a ascii character ? calling a byte txt is a little suspicious !


Top
 Profile  
 
 Post subject:
PostPosted: 17 Apr 2006 11:07 
Offline

Joined: 12 Apr 2006 11:55
Posts: 68
Location: Gold Coast Australia
.... 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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: