PDA

View Full Version : condition not working



hell_pk
- 2nd April 2012, 07:50
Following code gathers 4 bit ADC value from serial port constantly. Only 2 values are available 3265 approx (actually 3.265) and 1625approx ( actually 1.265). The program checks for first bit (rec[1] in code) in if loop and generates alarm. But despite being value of rec[1]=3 if loop is not executed. I have tried several approaches but to no avail. Please advice.
Chip used is 16F88.

INCLUDE "modedefs.bas"


DEFINE OSC 8
OSCCON=%01110000 ' INTRC = 8MHz


ANSEL=$00
CMCON = %00000111
TRISA=$00
PORTA=0


Define LCD_DREG PORTA
Define LCD_DBIT 0 '(LCD pins D4-D7 to PORTA.0 to 3)
DEFINE LCD_BITS 4
Define LCD_RSREG PORTA
Define LCD_RSBIT 4
Define LCD_EREG PORTA
Define LCD_EBIT 6
DEFINE LCD_LINES 2 ; Set number of LCD lines (1 ~ 4)
DEFINE LCD_COMMANDUS 2000 ; Set 2-millisecond command period
DEFINE LCD_DATAUS 50 ; Set data delay period


Define CCP1_PIN PORTB.0 ;connected to piezo for alarm
CCP1CON=%00001100
Trisb.0=0
portb.0=0


DEFINE HSER_RCSTA 90h


DEFINE HSER_TXSTA 20h
DEFINE HSER_BAUD 9600
' Enable serial port and continuous receive


x var byte
rec Var Byte[4]
y var byte
Lcdout $fe,1,"Serial to Parallel"
pause 200
Lcdout $fe,1
y=0
x=0


Loop:


y=y+1
Hserin [rec[y]]
x=rec[1]


if x=3 then
Lcdout $fe, $80, "alarm"
HPWM 1,125,1000 ' Send a 50% duty cycle PWM signal at 1KHz
endif


Hserout [rec[y]] 'display on hyperterminal
Lcdout $fe, $14, rec[y] 'display on LCD
Lcdout $fe, $14, Dec1 x




'when 4 bits of ADC are received,re-initialize y & LCD
if y=4 then
y=0
Lcdout $fe,2
endif



Goto Loop
End

HenrikOlsson
- 2nd April 2012, 08:37
Hi,
It seems to me like you are receiving ASCII so if you want to check for the 'digit' 3 you should compare x to 51 which is the ASCII code for '3'

/Henrik.

sayzer
- 2nd April 2012, 09:03
Try this:

Change "rec Var Byte[4]" to "rec Var Byte[5]

hell_pk
- 2nd April 2012, 10:17
right that does it.simplest of things to forget....