Having issues with Read and Write Fcn's


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2013
    Posts
    3

    Default Having issues with Read and Write Fcn's

    I need to use the internal eeprom to store three variables that are set up on a serial menu.
    The serial menu is available at boot up. I check the content of the eeprom with the read command in my menu.
    That is right all the time. When I shut down the board, I usually lose the data and it gives me back crap. Any Ideas?

    Thanks,

    Rick

    Code:
    ' SOURCE CODE Version A4 – PIC18F26k80
    ' 4 Mhz
    
    
    DEFINE OSC 4        'tells compiler clock speed
    OSCCON = 011010  'tells pic clock speed
    
    
    DEFINE ADC_BITS 12  'tells software / hardware to setup the ADC
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50
    ADCON2.7 = 1
    ten con 10
    DEFINE HSER2_RCSTA 90h
    define HSER2_TXSTA 20h
    Define HSER2_BAUD 2400
    define HSER2_SPBRG 25
    define HSER2_SPBRGH 0
    define HSER2_CLROERR 1
    
    
    TRISC.1 = 0 'led pin 9 - yellow
    TRISC.7 = 1
    TRISB.4 = 0 'pin 22 heater 1
    TRISA = 255 'set pins as analog inputs
    
    
    a1 VAR Word: a2 VAR Word: a3 Var word: a4 var word: a5 var word: a6 var word: a7 var word: a8 var word: a9 var word: a10 var word
    b1 VAR Word: b2 VAR Word: b3 Var word: b4 var word: b5 var word: b6 var word: b7 var word: b8 var word: b9 var word: b10 var word
    af var word: bf var word
    mVT var word: mVS var word
    ET var word: ES var word
    rx var word: i var word: j var long: k var word: d0 var long: d1 var long: d2 var word: d3 var word: d4 var word: d5 var word: d6 var word
    sernum var long: calcon var long: senres var long
    dat var word[10]
    
    
    High PORTB.4    ' Pin 22 - heater 
    High PORTC.1    ' Pin 9 yellow LED
    pause 1000
    'HSerOUT ["Ready ", 13, 10]
    
    
    HSERIN 1000, mainloop, [rx]
    if rx = 120 then
      HSerOUT [" Ca Mode Activated", 13, 10, 10,10,10,10]
      RCSTA.4 = 0
      RCSTA.4 = 1
      RCSTA.4 = 0
      RCSTA.4 = 1
      goto menue
    endif
    pause 5000
    senres = 0
    calcon = 0
    sernum = 0
    read 40, long sernum 
    read 20, long calcon
    read 0, long senres
    hserout[" Sen Res = ",dec senres,10,13]   
    hserout[" CalCon = ",dec calcon,10,13]
    hserout[" Ser # = ",dec sernum,10,13]
    
    
    mainloop:
    toggle PORTC.1 'Pin 9 yellow LED
    pause 499
    ADCIN 3,b10                              'Read sensor on pin 3 ADC3
    b1=b2: b2=b3: b3=b4: b4=b5: b5=b6: b6=b7: b7=b8: b8=b9: b9=b10
    bf=(b10+b9+b8+b7+b6+b5+b4+b3+b2+b1)/10   'avarage
    mVS = (bf/4095)*5                        'convert to PPM
    ES = mVS
    Pause 500
    'sample #, raw temperture, temp degrees C, on time, offtime, sensor, boxcar sensor, PPM 
    HSerOUT [dec sernum, " ", dec ET, " ", dec ES, 13, 10]
    pause 1000
    GoTo mainloop
    
    
    menue:
      pause 1000
      HSerOUT [" Send C for calabrate or R to read back, D when done.", 13, 10]
      HSERIN [rx]
      i = 0
      if rx = 82 then
        HSerOUT [" Read", 13,10]
        pause 1000
        senres = 0
        calcon = 0
        sernum = 0
        read 40, long sernum 
        read 20, long calcon
        read 0, long senres
        HSerOUT [" Serial # =", dec sernum, " Calibration Number =", dec calcon, " Sensor resistance =", dec senres,   13,10]
        hserout [" :)", 13,10]
      elseif rx = 68 then
        HSerOUT [" Done", 13,10]
        pause 1000
        goto mainloop
      elseif rx = 67 then
        HSerOUT [" Calibralte ", 13,10,10,10]
        HSerOUT [" Send  Resistance: (Like 999999, Max Vaue)",13,10]
        d5 = 0: d4 = 0: d3 = 0: d2 = 0: d1 = 0: d0 = 0
        for i = 0 to 10 step 1
          HSERIN [rx]
          HSerOUT [rx]
          dat[i] = rx
          if rx = 13 then
             j = 0
            if i = 6 then
                 'hserout[" 6 ",10]
                 d0 = dat[0]: d1 = dat[1]: d2 = dat[2]: d3 = dat[3]: d4 = dat[4]: d5 = dat[5]
                 d0 = (d0-48)*100000
                 d1 = (d1-48)*10000
                 d2 = (d2-48)*1000
                 d3 = (d3-48)*100
                 d4 = (d4-48)*10
                 d5 = (d5-48)
                 j = d0+d1+d2+d3+d4+d5
            elseif i = 5 then
                 'hserout[" 5 ",10]
                 d0 = dat[0]: d1 = dat[1]: d2 = dat[2]: d3 = dat[3]: d4 = dat[4]
                 d0 = (d0-48)*10000
                 d1 = (d1-48)*1000
                 d2 = (d2-48)*100
                 d3 = (d3-48)*10
                 d4 = (d4-48)
                 j = d0+d1+d2+d3+d4
            elseif i = 4 then
                 'hserout[" 4 ",10]
                 d0 = dat[0]: d1 = dat[1]: d2 = dat[2]: d3 = dat[3]
                 d0 = (d0-48)*1000
                 d1 = (d1-48)*100
                 d2 = (d2-48)*10
                 d3 = (d3-48)
                 j = d0+d1+d2+d3
            elseif i = 3 then
                 'hserout[" 3 ",10]
                 d0 = dat[0]: d1 = dat[1]: d2 = dat[2]
                 d0 = (d0-48)*100
                 d1 = (d1-48)*10
                 d2 = (d2-48)
                 j = d0+d1+d2
            elseif i = 2 then
                 'hserout[" 2 ",10]
                 d0 = dat[0]: d1 = dat[1]
                 d0 = (d0-48)*10
                 d1 =  d1-48
                 j = d0+d1
            elseif i = 1 then
                 'hserout[" 1 ",10]
                 d0 = dat[0]
                 j = d0-48 
            endif
            i = 11
            hserout[" j = ",dec j,10,13]
            senres = j
            write 0, long senres
            write 0, long senres
          endif
        next i
        pause 1500
        HSerOUT [" Send Ca con: (Ex 9999.99, Max Value)"]
        d5 = 0: d4 = 0: d3 = 0: d2 = 0: d1 = 0: d0 = 0
        for i = 0 to 10 step 1
          HSERIN [rx]
          HSerOUT [rx]
          dat[i] = rx
          if rx = 13 then
            j = 0
            if i = 6 then
                 'hserout[" 6 ",10]
                 d0 = dat[0]: d1 = dat[1]: d2 = dat[2]: d3 = dat[3]: d4 = dat[4]: d5 = dat[5]
                 d0 = (d0-48)*100000
                 d1 = (d1-48)*10000
                 d2 = (d2-48)*1000
                 d3 = (d3-48)*100
                 d4 = (d4-48)*10
                 d5 = (d5-48)
                 j = d0'+d1+d2+d3+d4+d5
            elseif i = 5 then
                ' hserout[" 5 ",10]
                 d0 = dat[0]: d1 = dat[1]: d2 = dat[2]: d3 = dat[3]: d4 = dat[4]
                 d0 = (d0-48)*10000
                 d1 = (d1-48)*1000
                 d2 = (d2-48)*100
                 d3 = (d3-48)*10
                 d4 = (d4-48)
                 j = d0+d1+d2+d3+d4
            elseif i = 4 then
                 'hserout[" 4 ",10]
                 d0 = dat[0]: d1 = dat[1]: d2 = dat[2]: d3 = dat[3]
                 d0 = (d0-48)*1000
                 d1 = (d1-48)*100
                 d2 = (d2-48)*10
                 d3 = (d3-48)
                 j = d0+d1+d2+d3
            elseif i = 3 then
                 'hserout[" 3 ",10]
                 d0 = dat[0]: d1 = dat[1]: d2 = dat[2]
                 d0 = (d0-48)*100
                 d1 = (d1-48)*10
                 d2 = (d2-48)
                 j = d0+d1+d2
            elseif i = 2 then
                 'hserout[" 2 ",10]
                 d0 = dat[0]: d1 = dat[1]
                 d0 = (d0-48)*10
                 d1 =  d1-48
                 j = d0+d1
            elseif i = 1 then
                 'hserout[" 1 ",10]
                 d0 = dat[0]
                 j = d0-48 
            endif
            i = 11
            hserout[" j = ",dec j,10,13]
            senres = j
            j = 0
            write 20, long senres
            write 20, long senres
          endif
        next i
        pause 1500
        HSerOUT [" Send serial number: (Ex 123456)"]
        d5 = 0: d4 = 0: d3 = 0: d2 = 0: d1 = 0: d0 = 0
        for i = 0 to 10 step 1
          HSERIN [rx]
          HSerOUT [rx]
          dat[i] = rx
          if rx = 13 then
            j = 0
            if i = 6 then
                 'hserout[" 6 ",10]
                 d0 = dat[0]: d1 = dat[1]: d2 = dat[2]: d3 = dat[3]: d4 = dat[4]: d5 = dat[5]
                 d0 = (d0-48)*100000
                 d1 = (d1-48)*10000
                 d2 = (d2-48)*1000
                 d3 = (d3-48)*100
                 d4 = (d4-48)*10
                 d5 = (d5-48)
                 j = d0+d1+d2+d3+d4+d5
            elseif i = 5 then
                 'hserout[" 5 ",10]
                 d0 = dat[0]: d1 = dat[1]: d2 = dat[2]: d3 = dat[3]: d4 = dat[4]
                 d0 = (d0-48)*10000
                 d1 = (d1-48)*1000
                 d2 = (d2-48)*100
                 d3 = (d3-48)*10
                 d4 = (d4-48)
                 j = d0+d1+d2+d3+d4
            elseif i = 4 then
                ' hserout[" 4 ",10]
                 d0 = dat[0]: d1 = dat[1]: d2 = dat[2]: d3 = dat[3]
                 d0 = (d0-48)*1000
                 d1 = (d1-48)*100
                 d2 = (d2-48)*10
                 d3 = (d3-48)
                 j = d0+d1+d2+d3
            elseif i = 3 then
                 'hserout[" 3 ",10]
                 d0 = dat[0]: d1 = dat[1]: d2 = dat[2]
                 d0 = (d0-48)*100
                 d1 = (d1-48)*10
                 d2 = (d2-48)
                 j = d0+d1+d2
            elseif i = 2 then
                 'hserout[" 2 ",10]
                 d0 = dat[0]: d1 = dat[1]
                 d0 = (d0-48)*10
                 d1 =  d1-48
                 j = d0+d1
            elseif i = 1 then
                 'hserout[" 1 ",10]
                 d0 = dat[0]
                 j = d0-48 
            endif
            i = 11
            hserout[" j = ",dec j,10,13]
            sernum = j
            j = 0
            write 40, long sernum
            write 40, long sernum
            read 40, long j
            hserout[" j = ",dec j,10,13]
          endif
        next i
        pause 3000
        HSerOUT [" Done!", 13,10,10,10]
      else
        HSerOUT [" Not Valid", 13,10]
        pause 1000
      endif
    goto menue

  2. #2
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Having issues with Read and Write Fcn's

    Lots of problems.
    The max value a word variable can store is 65536,
    so what's this supposed to do:
    Code:
    d0 = (d0-48)*100000
    Any value in other than zero in d0 will result in at least 1000000
    which severely overflow that variable, and then you do it again and again.
    and yes, I see you are subtracting from an ASCII value to acquire a real value.

    It looks like you're also only reading and writing one byte to EEPROM
    which might be fine as long as the value is always less than 256.
    The long you are trying to write is likely four bytes.

    Is there really a long variable type in PBP now?
    Something like this might work if there is:
    Code:
    long j = 0;
    
    long = long + (d0-48)*100000
    long = long + (d1-48)*10000
    long = long + (d2-48)*1000
    long = long + (d3-48)*100
    long = long + (d4-48)*10
    long = long + (d5-48)
    Depending on which var the left side of the sum is calculated in,
    otherwise you'll need to be more creative.
    Last edited by Art; - 7th June 2013 at 03:02.

  3. #3
    Join Date
    Jun 2013
    Posts
    3


    Did you find this post helpful? Yes | No

    Default Re: Having issues with Read and Write Fcn's

    With my compiler I have if I include "-n" in the command line it allows longs.

    Please look at the section of code:

    Code:
                 d0 = dat[0]: d1 = dat[1]: d2 = dat[2]: d3 = dat[3]: d4 = dat[4]: d5 = dat[5]
                 d0 = (d0-48)*100000
                 d1 = (d1-48)*10000
                 d2 = (d2-48)*1000
                 d3 = (d3-48)*100
                 d4 = (d4-48)*10
                 d5 = (d5-48)
                 j = d0+d1+d2+d3+d4+d5
    This converts a string of char #'s into a integer I can store/use.
    So If I sent the pic 745647 into a serial terminal, the pic would be able to use the integer number.
    It doesn't over flow, I have tested it to 999999 w/o issue.
    Per PBP Compiler manual, I can send the write command a long w/o issue.
    Code:
    write 0, long senres
    When I read it back in the serial menu at this point:
    Code:
     if rx = 82 then 'found R
        HSerOUT [" Read", 13,10]
        pause 1000
        senres = 0  ' clear all values
        calcon = 0
        sernum = 0
        read 40, long sernum  ' read them from EEPROM
        read 20, long calcon
        read 0, long senres
        HSerOUT [" Serial # =", dec sernum, " Calibration Number =", dec calcon, " Sensor resistance =", dec senres,   13,10] ' Display
        hserout [" :)", 13,10]
    It reports the values expected. When The Pic reboots, the values are gone at boot:

    Code:
    pause 5000
    senres = 0
    calcon = 0
    sernum = 0
    read 40, long sernum 
    read 20, long calcon
    read 0, long senres
    hserout[" Sen Res = ",dec senres,10,13]   
    hserout[" CalCon = ",dec calcon,10,13]
    hserout[" Ser # = ",dec sernum,10,13]
    Any help would be great.

  4. #4
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Having issues with Read and Write Fcn's

    Sorry, I didn't see that the two highest vars are actually longs, and the rest are words.
    My error.

    With regard to longs in PBP, they are new to me.

  5. #5
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Having issues with Read and Write Fcn's

    First thing I noticed, since it recently bit me, is that some of your defines are lower case and some upper case. They must all me upper for PBP to read them.

    I think your OSCCON number needs the percent sign %.

    I was trying to read data sheet to look at rest and ran out of time. Try those fixes.

  6. #6
    Join Date
    Jun 2013
    Posts
    3


    Did you find this post helpful? Yes | No

    Default Re: Having issues with Read and Write Fcn's

    This is what fixed it:

    Code:
    
    HSERIN 1000, mainloop, [rx]if rx = 120 then  HSerOUT [" Ca Mode Activated", 13, 10, 10,10,10,10]  RCSTA.4 = 0  RCSTA.4 = 1  RCSTA.4 = 0  RCSTA.4 = 1  goto menueendifpause 5000senres = 0calcon = 0sernum = 0mainloop:read 40, long sernum 
    read 20, long calcon
    read 0, long senres
    inloop:toggle PORTC.1 'Pin 9 yellow LEDpause 499ADCIN 3,b10                              'Read sensor on pin 3 ADC3b1=b2: b2=b3: b3=b4: b4=b5: b5=b6: b6=b7: b7=b8: b8=b9: b9=b10bf=(b10+b9+b8+b7+b6+b5+b4+b3+b2+b1)/10   'avaragemVS = (bf/4095)*5                        'convert to PPMES = mVSPause 500'sample #, raw temperture, temp degrees C, on time, offtime, sensor, boxcar sensor, PPM HSerOUT [dec sernum, " ", dec ET, " ", dec ES, 13, 10]pause 1000GoTo inloop
    who the hell knows why....

  7. #7
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Having issues with Read and Write Fcn's

    There's no returns in that.. it's hell to read.

Similar Threads

  1. PLEASE HELP...read write issues
    By nomad77 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 17th March 2011, 14:39
  2. Read Write Problem
    By kduck63 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 15th June 2009, 19:03
  3. Write. Read. (How hard can it be?)
    By Meriachee in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 12th January 2009, 21:57
  4. I2C Read and Write
    By DerekMacom in forum mel PIC BASIC
    Replies: 14
    Last Post: - 21st April 2008, 15:44
  5. Write and Read from eeprom
    By savnik in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 9th June 2007, 14:56

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