PDA

View Full Version : HSERIN issue with a 18F452 and IF THEN statements or Case statements



BrianS
- 2nd June 2021, 13:42
I am using PB3 and programming a 18F452. The code I am using waits for a "%" from HSERIN then grabs the next 4 characters and processes them. A "!" is sent and then a numeric value. Then a "?" is sent next followed by another numeric value. So the whole incoming string would look like this %!2?3 for example. Everything seems to work OK as far as receiving the serial data. I just can't process it afterwards using IF THEN statements or CASE statements either. I am only interested in the numeric values of 2 and 3 in the %!2?3 received string. Now as you can see in my posted code the value after I receive the "!" is a 2 and it should be passed to the variable PS_State. Using an LCD I can confirm I am getting 2 passed to PS_State. The problem is the IF THEN statement "IF PS_State = 2 Then" will not process it. It will not update my PORTB and PORTC lines. I tried using a CASE statement to handle this before and it wouldn't work either. IF someone can offer some help or suggestions of what is wrong with this code it would be greatly appreciated. I have been trying to figure this out for several days with no success. I am using Labview to communicate with the 18F452 PIC that seems to be working correctly too. Labview communicates at 9600 baud, 8N1.


Include "modedefs.bas"

#CONFIG
CONFIG OSC = HS ; XT oscillator
CONFIG OSCS = OFF ; Oscillator system clock switch option is disabled (main oscillator is source)
CONFIG PWRT = OFF ; PWRT disabled
CONFIG BOR = OFF ; Brown-out Reset enabled
CONFIG BORV = 20 ; VBOR set to 2.0V
CONFIG WDT = OFF ; WDT enabled
CONFIG WDTPS = 128 ; 1:128
CONFIG CCP2MUX = OFF ; CCP2 input/output is multiplexed with RC1
CONFIG STVR = ON ; Stack Full/Underflow will cause RESET
CONFIG LVP = OFF ; Low Voltage ICSP disabled
CONFIG DEBUG = OFF ; Background Debugger disabled. RB6 and RB7 configured as general purpose I/O pins.
CONFIG CP0 = OFF ; Block 0 (000200-001FFFh) not code protected
CONFIG CP1 = OFF ; Block 1 (002000-003FFFh) not code protected
CONFIG CP2 = OFF ; Block 2 (004000-005FFFh) not code protected
CONFIG CP3 = OFF ; Block 3 (006000-007FFFh) not code protected
CONFIG CPB = OFF ; Boot Block (000000-0001FFh) not code protected
CONFIG CPD = OFF ; Data EEPROM not code protected
CONFIG WRT0 = OFF ; Block 0 (000200-001FFFh) not write protected
CONFIG WRT1 = OFF ; Block 1 (002000-003FFFh) not write protected
CONFIG WRT2 = OFF ; Block 2 (004000-005FFFh) not write protected
CONFIG WRT3 = OFF ; Block 3 (006000-007FFFh) not write protected
CONFIG WRTC = OFF ; Configuration registers (300000-3000FFh) not write protected
CONFIG WRTB = OFF ; Boot Block (000000-0001FFh) not write protected
CONFIG WRTD = OFF ; Data EEPROM not write protected
CONFIG EBTR0 = OFF ; Block 0 (000200-001FFFh) not protected from Table Reads executed in other blocks
CONFIG EBTR1 = OFF ; Block 1 (002000-003FFFh) not protected from Table Reads executed in other blocks
CONFIG EBTR2 = OFF ; Block 2 (004000-005FFFh) not protected from Table Reads executed in other blocks
CONFIG EBTR3 = OFF ; Block 3 (006000-007FFFh) not protected from Table Reads executed in other blocks
CONFIG EBTRB = OFF ; Boot Block (000000-0001FFh) not protected from Table Reads executed in other blocks
#ENDCONFIG

DEFINE OSC 20
DEFINE USE_LFSR 1
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTA
DEFINE LCD_RSBIT 3
DEFINE LCD_EREG PORTA
DEFINE LCD_EBIT 1
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_RWREG PORTA
DEFINE LCD_RWBIT 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50

DEFINE HSER_CLROERR 1
DEFINE HSER_BAUD 9600
'DEFINE HSER_SPBRG 129 '9600 Baud @ 20MHz, 0.16%

LCDOUT $FE, $01
Pause 100

TRISA = %11111111
TRISB = %11100000
TRISC = %10110000
TRISE = %111
ADCON0=$07
ADCON1=$07
CCP1CON = $0
CCP2CON = $0
RCSTA = $90
TXSTA = $20
INTCON = %00000000
PORTC.2 = 0
PORTB = %00010000


RX_Buff VAR Byte [5]
PS_State VAR Byte


MainLoop:
GOSUB RCVfromPC 'Go wait for data from the PC
GOSUB SortData
GOSUB Power_Supply
GOTO MainLoop

RCVfromPC:
HSERIN 5000,TimeOut, [WAIT("%"), STR RX_Buff\4]
RETURN

TimeOut:
pause(50)
GOTO RCVfromPC

SortData:
IF RX_Buff(0) = 33 THEN PS_State = RX_Buff(1)
RETURN


Power_Supply:
IF PS_State = 1 Then
PORTB.0 = 1
PORTB.1 = 0
PORTB.2 = 0
PORTB.3 = 0
PORTC.2 = 1
EndIf
IF PS_State = 2 Then
PORTB.0 = 1
PORTB.1 = 1
PORTB.2 = 0
PORTB.3 = 0
PORTC.2 = 1
EndIf
IF PS_State = 3 Then
PORTB.0 = 1
PORTB.1 = 1
PORTB.2 = 1
PORTB.3 = 0
PORTC.2 = 1
EndIf
IF PS_State = 4 Then
PORTB.0 = 1
PORTB.1 = 1
PORTB.2 = 1
PORTB.3 = 1
PORTC.2 = 1
EndIf

LCDOUT $FE, 1, PS_State
Pause(2000)
LCDOUT $FE, $01

RETURN

richard
- 2nd June 2021, 14:00
The problem is the IF THEN statement "IF PS_State = 2 Then" will not process it.

did you send binary 2 or ascii 2? if its ascii

IF PS_State = "2" Then" .... or IF PS_State = 50 Then" .... or IF PS_State = $32 Then" ....

BrianS
- 2nd June 2021, 15:11
It was an ASCII 2.
I tried $32 and that worked. Thank you.