16F877A stack problems


Closed Thread
Results 1 to 3 of 3
  1. #1
    rocketman's Avatar
    rocketman Guest

    Default 16F877A stack problems

    Problem: 16F877a begins sending a infinite series of "1" as output to the RS232 conveter.

    From the best I can tell the error originates in my "If Command=Q" statement where I read the TEMP2 on a DS18S20 sensor. I assume I am screwing up the stack somewhere, but I can not tell where.

    Here's the code, can someone please tell me where I went wrong:

    include "modedefs.bas"

    DEFINE OSC 20
    DEFINE ADC_BITS 10 ' Set number of bits in result
    DEFINE ADC_CLOCK 2 ' Set clock source (3=rc)
    DEFINE ADC_SAMPLEUS 100 ' Set sampling time in uS

    DEFINE HSER_TXSTA 24H
    DEFINE HSER_RCSTA 90H
    DEFINE HSER_BAUD 9600
    DEFINE HSER_CLROERR

    'LCD SETTINGS
    DEFINE LCD_DREG PORTD
    DEFINE LCD_DBIT 4
    DEFINE LCD_RSREG PORTD
    DEFINE LCD_RSBIT 2
    DEFINE LCD_EREG PORTD
    DEFINE LCD_EBIT 3
    DEFINE LCD_BITS 4
    DEFINE LCD_LINES 4

    COMMAND VAR BYTE[2]

    COUNTR VAR BYTE
    COUNTC VAR BYTE
    DS18S20F VAR WORD
    DS18S20 VAR WORD

    TEMP1 VAR PORTB.7
    TEMP2 VAR PORTB.6

    'LIGHTS
    PUMP1 VAR PORTC.1
    LIGHT VAR PORTE.2

    'I/O CONFIGURATION
    TRISA=%00011111
    TRISB=%00000000
    TRISC=%11001011
    TRISD=%11110001
    TRISE=%00000000

    'USART SETTINGS
    PIE1=%01110000
    RCSTA=%10010000 'ENABLE SERIAL PORT AND RECEPTION
    TXSTA=%00100100

    'Turn everything off at startup
    LOW LIGHT
    LOW PUMP1

    LCDOUT $FE,1, "RESET"
    HSEROUT ["MARINETIX ZEOSC 1.0"]
    PAUSE 1000
    HSEROUT ["KX"] 'power up responce

    MAIN:

    If PIR1.5=1 then 'if there is something in the UART buffer read it in
    HSERIN ,2000, TIMEOUT, [STR COMMAND\2] 'IF THERE IS SOMETHING IN THE RECIEVE REGISTER STORE IT IN COMMAND
    GOSUB EXECUTE
    TIMEOUT: 'if it times out, just start over at main
    ENDIF

    GoTo MAIN

    '***** EXECUTE THE INCOMING COMMAND BASED ON THE CHARACTERS RECIEVED ****

    EXECUTE:
    IF COMMAND(0) = "K" THEN
    HSEROUT ["KX"]
    ENDIF

    IF COMMAND(0)="T" THEN

    LOW TEMP1 'initialize DS18S20
    PAUSEUS 500 'wait for return pulse
    TRISB.7=1 'set pin as input to get temp reading
    PAUSEUS 100

    IF TEMP1=1 THEN
    DS18S20F=999 'VB error code signaling no sensor connected
    HSEROUT ["T", DEC (DS18S20F),"X"]
    RETURN
    ENDIF

    PAUSEUS 400

    'START TEMP READING
    OWOUT TEMP1, 1, [$CC, $44] ' $CC-SKIP SEARCHING FOR MULTIPLE DS18S20, $44-START

    WAITFORCONVERSION:
    OWIN TEMP1, 4, [COUNTR] 'CHECK FOR STILL BUSY CONVERTING SIGNAL
    IF COUNTR=0 THEN WAITFORCONVERSION

    OWOUT TEMP1, 1, [$CC, $BE] 'READ THE TEMPERATURE
    OWIN TEMP1, 0, [DS18S20.LOWBYTE, DS18S20.HIGHBYTE, SKIP 4, COUNTR, COUNTC]

    DS18S20=((DS18S20>>1*100)-25)+(((COUNTC-COUNTR)*100)/COUNTC)
    DS18S20F=(DS18S20 */ 461)+3200 'CALCULATE TEMP IN DEGREES F, NOT VALID FOR NEGATIVE TEMP
    HSEROUT ["T", DEC (DS18S20F/100), ".", DEC1 (DS18S20F),"X"]
    RETURN
    ENDIF

    IF COMMAND(0)="Q" THEN

    LOW TEMP2 'initialize DS18S20
    PAUSEUS 500 'wait for return pulse
    TRISB.6=1 'set pin as input to get temp reading
    PAUSEUS 100

    IF TEMP2=1 THEN
    DS18S20F=999 'VB error code signaling no sensor connected
    HSEROUT ["Q", DEC (DS18S20F),"X"]
    RETURN
    ENDIF

    PAUSEUS 400

    'START TEMP READING
    OWOUT TEMP2, 1, [$CC, $44] ' $CC-SKIP SEARCHING FOR MULTIPLE DS18S20, $44-START

    WAITFORTEMP2:
    OWIN TEMP2, 4, [COUNTR] 'CHECK FOR STILL BUSY CONVERTING SIGNAL
    IF COUNTR=0 THEN WAITFORTEMP2

    OWOUT TEMP2, 1, [$CC, $BE] 'READ THE TEMPERATURE
    OWIN TEMP2, 0, [DS18S20.LOWBYTE, DS18S20.HIGHBYTE, SKIP 4, COUNTR, COUNTC]

    DS18S20=((DS18S20>>1*100)-25)+(((COUNTC-COUNTR)*100)/COUNTC)
    DS18S20F=(DS18S20 */ 461)+3200 'CALCULATE TEMP IN DEGREES F, NOT VALID FOR NEGATIVE TEMP
    HSEROUT ["Q", DEC (DS18S20F/100), ".", DEC1 (DS18S20F),"X"]

    ENDIF

    RETURN

    'END OF CODE

  2. #2
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    RETURN
    ENDIF

    Hello Friend...

    A couple of things I saw that could give you problems...


    Your ifs and END ifs... Some of your Ifs do NOT have a end if. I can't tell if you are only wanting to a *if* on just one line of code, or 5 lines...., but just below these missing end ifs, you *do* have a endif.


    Then more importantly , 1/2 down your routine, you have the following:

    RETURN
    ENDIF


    This means the return will NOT be excuted (to return to where your gosub was), *UNLESS* the if statement is true.

    ENDIF should ALLLWAYs be before the return!




    Here is a routine....I am not sure what you want to do with:

    IF COMMAND(0)="T" THEN

    LOW TEMP1 'initialize DS18S20
    PAUSEUS 500 'wait for return pulse
    TRISB.7=1 'set pin as input to get temp reading
    PAUSEUS 100

    IF TEMP1=1 THEN
    DS18S20F=999 'VB error code signaling no sensor connected
    HSEROUT ["T", DEC (DS18S20F),"X"]
    RETURN
    ENDIF


    1. See that ENDIF after the return??? that is a killer!
    2. When does your first IF statement end??? should it really be the following???? (

    IF COMMAND(0)="T" THEN

    LOW TEMP1 'initialize DS18S20
    PAUSEUS 500 'wait for return pulse
    TRISB.7=1 'set pin as input to get temp reading
    PAUSEUS 100
    ENDIF

    IF TEMP1=1 THEN
    DS18S20F=999 'VB error code signaling no sensor connected
    HSEROUT ["T", DEC (DS18S20F),"X"]
    ENDIF
    RETURN


    I see this in all your routines...


    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  3. #3
    rocketman's Avatar
    rocketman Guest


    Did you find this post helpful? Yes | No

    Default

    thanks, looks like I have multiple problems here. I'll re-work the code and clean up all the If-Endif and return statements and try it again.

Similar Threads

  1. Serial VB 2005 pic 16f877a problems
    By Snap in forum mel PIC BASIC Pro
    Replies: 28
    Last Post: - 8th July 2013, 00:52
  2. Software Stack
    By yasser hassani in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 18th December 2007, 10:04
  3. EEROM problems in 16F877A
    By BrianT in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 1st May 2007, 02:52
  4. 48 level Software Stack
    By Darrel Taylor in forum Code Examples
    Replies: 0
    Last Post: - 30th November 2003, 01:07
  5. Problems with 16F877A code
    By NightHawk2 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 20th August 2003, 01:36

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts