Convert from C to Basic generate errors

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
igeorge
Posts: 593
Joined: 28 Dec 2005 09:15

Convert from C to Basic generate errors

#1 Post by igeorge » 11 Mar 2020 16:04

I have a small program on MikroC Pro.
It works.
I do not know C and i want to use it on Mikrobasc so i can develop more.
Here is the MikroC program

Code: Select all

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

void main()
{
  char i,rfid[13] = "123456781212";
  Lcd_Init();                         // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);           // Cursor off
  Lcd_Out(1,1,"RFID Tag Reader");     // Write text in first row
  
  UART1_Init(9600);
  
  rfid[12] = '\0';

  while(1)
  {
     if(UART1_Data_Ready())
     {
       for(i=0;i<12;)
       {
         if(UART1_Data_Ready())
         {
            rfid[i] = UART1_Read();
            i++;
         }
       }

       if((rfid[0] ^ rfid[2] ^ rfid[4] ^ rfid[6] ^ rfid[8] == rfid[10]) && (rfid[1] ^ rfid[3] ^ rfid[5] ^ rfid[7] ^ rfid[9] == rfid[11]))
       {
          Lcd_Out(2,1,rfid);
       }
       else
         Lcd_Out(2,1,"Error           ");
     }
  }
}
and here is the Mikrobasic version i got. They might be errors because as i said i do not know C. I just read some help files.

Code: Select all

dim LCD_RS as sbit at RB4_bit
dim LCD_EN as sbit at RB5_bit
dim LCD_D4 as sbit at RB0_bit
dim LCD_D5 as sbit at RB1_bit
dim LCD_D6 as sbit at RB2_bit
dim LCD_D7 as sbit at RB3_bit
dim LCD_RS_Direction as sbit at TRISB4_bit
dim LCD_EN_Direction as sbit at TRISB5_bit
dim LCD_D4_Direction as sbit at TRISB0_bit
dim LCD_D5_Direction as sbit at TRISB1_bit
dim LCD_D6_Direction as sbit at TRISB2_bit
dim LCD_D7_Direction as sbit at TRISB3_bit


main:
  dim i as byte
  dim rfid as string[13]
  Lcd_Init()                         ' Initialize LCD
  Lcd_Cmd(_LCD_CLEAR)                'Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF)           ' Cursor off
  Lcd_Out(1,1,"RFID Tag Reader")     ' Write text in first row

  UART1_Init(9600)

  rfid[12]=0
  
  while true
     if(UART1_Data_Ready()) then
       for i=0 to 11
         if(UART1_Data_Ready()) then
            rfid[i] = UART1_Read()
            i = i + 1
         end if
       next i
     end if
     if ((rfid[0] xor rfid[2] xor rfid[4] xor rfid[6] = rfid[10]) and (rfid[1] xor rfid[3] xor rfid[5] xor rfid[7] xor rfid[9] = rfid[11])) then
         Lcd_Out(2,1,rfid)
       else
         Lcd_Out(2,1,"Error           ")
       end if
  wend
end.
Please help . I really need it.
Thank you very much in advance.
Experience is something you don't get until just after you need it

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: Convert from C to Basic generate errors

#2 Post by stefan.filipovic » 12 Mar 2020 12:47

Hi George,

I've found some "bugs" in your mikroBasic code.

Instead of the for loop, you should be using a while loop to get the same logic as in C code.
You ended the first if(UART1_Data_Ready()) statement too early, and xor rfid[8] is missing in the last if statement.

Please try with this code:

Code: Select all

while true
     if(UART1_Data_Ready()) then
       i = 0
       while i < 12
         if(UART1_Data_Ready()) then
            rfid[i] = UART1_Read()
            i = i + 1
         end if
       wend
       if ((rfid[0] xor rfid[2] xor rfid[4] xor rfid[6] xor rfid[8] = rfid[10]) and (rfid[1] xor rfid[3] xor rfid[5] xor rfid[7] xor rfid[9] = rfid[11])) then
         Lcd_Out(2,1,rfid)
       else
         Lcd_Out(2,1,"Error           ")
       end if

     end if
  wend
Kind regards,
Stefan Filipović

igeorge
Posts: 593
Joined: 28 Dec 2005 09:15

Re: Convert from C to Basic generate errors

#3 Post by igeorge » 12 Mar 2020 13:17

Thank you very much Stefan.
The code compile correctly , but the result is ERROR.
I am surprised why.
If i use the MikroC version with the same RFID card and the same RFID reader = EM18,
It works fine and display the correct ID of the card.
Experience is something you don't get until just after you need it

igeorge
Posts: 593
Joined: 28 Dec 2005 09:15

Re: Convert from C to Basic generate errors

#4 Post by igeorge » 12 Mar 2020 13:24

This is what i get using the MikroC
Attachments
20200312_082020.jpg
20200312_082020.jpg (4.3 MiB) Viewed 2397 times
Experience is something you don't get until just after you need it

igeorge
Posts: 593
Joined: 28 Dec 2005 09:15

Re: Convert from C to Basic generate errors

#5 Post by igeorge » 12 Mar 2020 13:29

And this is what i get using MB.
Sorry.
Attachments
20200312_082643.jpg
20200312_082643.jpg (4.22 MiB) Viewed 2396 times
Experience is something you don't get until just after you need it

igeorge
Posts: 593
Joined: 28 Dec 2005 09:15

Re: Convert from C to Basic generate errors

#6 Post by igeorge » 12 Mar 2020 13:32

What do you suggest to do next on MikroBasic ?
I need to develop more around the program using the eeprom write and read , and i cannot do it if i am in MikroC. Sorry.
Experience is something you don't get until just after you need it

igeorge
Posts: 593
Joined: 28 Dec 2005 09:15

Re: Convert from C to Basic generate errors

#7 Post by igeorge » 12 Mar 2020 13:37

Guess what.
If i do not use the CRC checking it display correct the tag.

Code: Select all

         Lcd_Out(2,1,rfid)

'       if ((rfid[0] xor rfid[2] xor rfid[4] xor rfid[6] xor rfid[8] = rfid[10]) and (rfid[1] xor rfid[3] xor rfid[5] xor rfid[7] xor rfid[9] = rfid[11])) then
'         Lcd_Out(2,1,rfid)
'       else
'         Lcd_Out(2,1,"Error           ")
'       end if
Unfortunately, i need to check the calculated CRC against the received CRC :(
Experience is something you don't get until just after you need it

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: Convert from C to Basic generate errors

#8 Post by stefan.filipovic » 12 Mar 2020 14:34

Hi George,

I believe the issue is caused by the operator precedence in C, please refer to the following table:
https://en.cppreference.com/w/c/languag ... precedence

So, I'm not sure whether the result of this statement will be what you expect it to be.

Code: Select all

if((rfid[0] ^ rfid[2] ^ rfid[4] ^ rfid[6] ^ rfid[8] == rfid[10]) && (rfid[1] ^ rfid[3] ^ rfid[5] ^ rfid[7] ^ rfid[9] == rfid[11]))
The following statement would be equal to the one in mikroBasic:

Code: Select all

if(((rfid[0] ^ rfid[2] ^ rfid[4] ^ rfid[6] ^ rfid[8]) == rfid[10]) && ((rfid[1] ^ rfid[3] ^ rfid[5] ^ rfid[7] ^ rfid[9]) == rfid[11]))
Kind regards,
Stefan Filipović

igeorge
Posts: 593
Joined: 28 Dec 2005 09:15

Re: Convert from C to Basic generate errors

#9 Post by igeorge » 12 Mar 2020 14:40

Thank you Stefan.
I will try to keep you posted.
Experience is something you don't get until just after you need it

igeorge
Posts: 593
Joined: 28 Dec 2005 09:15

Re: Convert from C to Basic generate errors

#10 Post by igeorge » 12 Mar 2020 15:01

Sorry Stefan, but does not work.
What is curious, is the fact that if i use a programer calculator(windows has one) i get the proper numbers.
I got lost here
Experience is something you don't get until just after you need it

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: Convert from C to Basic generate errors

#11 Post by stefan.filipovic » 12 Mar 2020 15:28

Hi George,

I'm afraid I wasn't clear enough.

This code in C, which you say that works:

Code: Select all

if((rfid[0] ^ rfid[2] ^ rfid[4] ^ rfid[6] ^ rfid[8] == rfid[10]) && (rfid[1] ^ rfid[3] ^ rfid[5] ^ rfid[7] ^ rfid[9] == rfid[11]))
is the same as this code in mikroBasic:

Code: Select all

if ((rfid[0] xor rfid[2] xor rfid[4] xor rfid[6] xor (rfid[8] = rfid[10])) and (rfid[1] xor rfid[3] xor rfid[5] xor rfid[7] xor (rfid[9] = rfid[11])))
Kind regards,
Stefan Filipović

igeorge
Posts: 593
Joined: 28 Dec 2005 09:15

Re: Convert from C to Basic generate errors

#12 Post by igeorge » 12 Mar 2020 16:25

BUT ..... nobody can understand the God way or the woman mind.
I believe it is the identical code, but for i do not understand what reason, it does not works on Basic but works on C.
I will get later today new chps from Digikey and try again.
It is ridiculous, but i will try to see.
Thank you for all your hard work and help Stefan.
You will be the first to find out if any change.
Experience is something you don't get until just after you need it

igeorge
Posts: 593
Joined: 28 Dec 2005 09:15

Re: Convert from C to Basic generate errors

#13 Post by igeorge » 12 Mar 2020 16:33

IT WORKS !!!!!!!!!!!!!!!!!!!!!!!!!!!!
I was so confused with this program that i forgot to delete a line from an old check,
and being the last one , it always printed the word ERROR

THANK YOU STEFAN
Experience is something you don't get until just after you need it

Post Reply

Return to “mikroBasic PRO for PIC General”