Problem compare two variable and send down UART if equal

Please check the frequently asked questions before posting to the forum.
Post Reply
Author
Message
sawani
Posts: 19
Joined: 02 Sep 2011 13:06

Problem compare two variable and send down UART if equal

#1 Post by sawani » 24 Aug 2013 04:19

I am trying to compare the number sent on UART with the number set by deep switch DSW1 on the circuit and send back a message throuth the UART if the numbes are equal. The problem is it doesn't seem to work. I do not know where I do mistake. Please anyone who can help.
This is what I am doing. I send the number through UART and I store it in the variable Ticket. Then on the loop on the main project I call Deep_Switch subprocedure to detamine the number represented by the deep switch. then I check if they are equal with the number sent on UART and send down the text "got you brother" on the UART.
I use virtual terminal to test this but it doest work. Kindly your help

[img]C:\Users\user\Desktop\humu[/img]

Code: Select all

C:\Users\user\Desktop\humu

sawani
Posts: 19
Joined: 02 Sep 2011 13:06

Re: Problem compare two variable and send down UART if equal

#2 Post by sawani » 24 Aug 2013 04:22

sorry, I get trouble attaching files here. I do not understand the way. I copy the code down here. I do not know how to post the picture of my circuit. Now following is my code I paste down:

program CHARGER_MODULE

' Declarations section
const VoltageDropLimit = 983

dim VoltageLevel as word
dim DPSW
dim Ticket
dim flag0 as bit
dim flag1 as bit
dim Update_Flag as bit
dim i as byte

'SUB PTOCEDURES:
sub procedure Deep_Switch
flag0 = TestBit(PORTC,4)
flag1 = TestBit(PORTC,5)

if (Flag0 = 0) and (Flag1 =0) then
DPSW = 0
else
if (Flag0 = 1) and (Flag1 = 0) then
DPSW = 1
else
if (Flag0 = 0) and (Flag1 = 1) then
DPSW = 2
else
if (Flag0 = 1) and (Flag1 = 1) then
DPSW = 3
end if
end if
end if
end if
end sub

sub procedure interrupt()
INTCON.GIE = 0
if (PIR1.RCIF = 1) then
PIR1.RCIF = 0
Ticket = UART1_Read()
Update_Flag = 1

end if

INTCON.GIE = 1

end sub



'===============================================================================
main: 'Main program

'INITIALISATION:

'Ports Initialisation...
ADCON1 = %00001110 'Only RA0 is analog input
LATA = %00000000
TRISA = %11111101 'Port A direction
LATB = %00000000
TRISB = %00000000 'Port B direction
LATC = %00000000
TRISC = %10110000


' Setup interrupts RCIE_bit = 1 ' Enable Rx1 intterupts
RCIE_Bit = 1
PEIE_bit = 1
GIE_bit = 1


'Initialise UART --------------------------------------------------------------
UART1_Init(9600) ' initialize UART module
Delay_ms(100)

update:
Deep_Switch
if Ticket = DPSW then
UART1_Write_Text("got you brother")
end if
goto update
end

sawani
Posts: 19
Joined: 02 Sep 2011 13:06

Re: Problem compare two variable and send down UART if equal

#3 Post by sawani » 25 Aug 2013 05:33

Hi buddies, I have also discovered that the number I send to the (through virtual terminal) is not the number I read back. I wonder if this is what brings problem. If press 2 on the keyboard to send it to the mcu, and later Use UART1_Write(Ticket) to send the number back to the virtual terminal, the virtual terminal print 32. If i send 8 I get 38, if I send 0 I get 30. It seem that there is a monster who add 30 to any number I send to the mcu. Can any one help me the way to get rid of this?

Thank you in advance

User avatar
milos.vicentijevic
Posts: 965
Joined: 23 Apr 2013 08:09

Re: Problem compare two variable and send down UART if equal

#4 Post by milos.vicentijevic » 26 Aug 2013 12:38

Hello Sawani,

it is not monster it is ASCII code. Every letter has it's own code including digits. So 0 is represented by 48 decimal (30h), 1 is represented by 49 decimal (31h) and so on.. So, for single digit it is relatively easy to convert them to real numbers.

Only thing you need to do before comparison is to subtract 48 decimal from char you have got over UART.

Best Regards,
Milos Vicentijevic

sawani
Posts: 19
Joined: 02 Sep 2011 13:06

Re: Problem compare two variable and send down UART if equal

#5 Post by sawani » 26 Aug 2013 13:06

Thank you very much, milos :). Let me work on that.

Post Reply

Return to “mikroPascal FAQ”