help: for my lm35 code converting basic layout to procedurel layout

Started by okmn, Jul 23, 2021, 02:40 PM

Previous topic - Next topic

okmn

dear Les,
could you support me about my lm35 code that ;
i wrote it by basic program layout want convert to procedurel program layout.

'****************************************************************
'*  Name    : LM35_SICAKLIK.BAS                                 *
'*  Author  : [select VIEW...EDITOR OPTIONS]                    *
'*  Notice  : Copyright (c) 2021 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 17.07.2021                                        *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
Device 16F1508
Declare Xtal = 16

Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_OFF, MCLRE_ON, CP_OFF, BOREN_OFF, CLKOUTEN_OFF
Config2 WRT_OFF, STVREN_OFF, BORV_LO, LPBOR_OFF, LVP_OFF
OSCCON = %01111010

Declare Adin_Res = 10 ' 10-bit result required
Declare Adin_Tad = cFRC ' RC OSC chosen for the ADC
Declare ADin_Delay = 50 ' Allow 50us sample time
ADCON1 = %11110000
OPTION_REG.7 = 0
TRISA = 1
TRISB = %00000000
TRISC = 0
ANSELA = 1 ' RA0 analog diğerleri digital
ANSELB = 0
ANSELC = 0
SSP1CON1.5 = 0
SSP1CON1.4 = 0
PORTA = 0
PORTB = 0
PORTC = 0

Symbol ENABLE_BIRLER = PORTB.4
Symbol ENABLE_ONLAR = PORTB.5
Symbol ENABLE_YUZLER = PORTB.6
Dim DEGER As Word
Dim sayac As Byte
Dim TEMP As Float
Dim TEMP_TOPLAM As Word
Dim BIRLER As Byte
Dim ONLAR As Byte
Dim YUZLER As Byte
Dim DISPLAY As Byte
Dim SICAKLIK As Word
ENABLE_BIRLER = 0
ENABLE_ONLAR = 0
BAUDCON.1 = 0
GoTo Main

Main:
GoSub LM35_OKU
GoSub SEVEN_SEG
GoTo Main

LM35_OKU:
For sayac = 0 To 9
    SICAKLIK = ADIn 0
    TEMP = (SICAKLIK /1023)*500 'AN0 dan okunan değer
    TEMP_TOPLAM = TEMP + TEMP_TOPLAM    'Sıcaklık değerine skala edilir
Next sayac
DEGER = TEMP_TOPLAM/10      'OKUNAN 10 DEĞERİN ORTALAMASI ALINIR
TEMP_TOPLAM = 0              'BİR SONRAKİ OKUMA İÇİN GEÇİCİ DEĞİŞKEN SIFIRLANIR

Return

SEVEN_SEG:

BIRLER = Dig DEGER,0
ONLAR  = Dig DEGER,1
YUZLER = Dig DEGER,2

DISPLAY = LookUpL BIRLER,[111,5,91,87,53,118,126,7,127,119]
PORTC = DISPLAY
ENABLE_BIRLER = 1
DelayMS 2
ENABLE_BIRLER = 0
DelayMS 1

DISPLAY = LookUpL ONLAR,[111,5,91,87,53,118,126,7,127,119]
If YUZLER = 0 And ONLAR = 0 Then DISPLAY = 0
PORTC = DISPLAY

ENABLE_ONLAR = 1
DelayMS 2
ENABLE_ONLAR = 0
DISPLAY = LookUpL YUZLER,[111,5,91,87,53,118,126,7,127,119]
If YUZLER = 0 Then DISPLAY = 0
PORTC = DISPLAY
ENABLE_YUZLER = 1
DelayMS 2
ENABLE_YUZLER = 0

Return

mytemplm35.zip

JonW

I find procedures are best used for code re-use, breaking down the processes and to make the code easier to read.

for instance create a procedure to measure a specific ADC port and the number of times you want to oversample.  Then all you need to do is pass the ADC pin you want to measure, the number of times you want to oversample and then get the result back.  This way you can then re-use the procedure in future code

There are loads of examples in the manual and also on Les site within the include files. 

Example:

I lately created a command to read UBX messages from Flash memory and send them to a software UART connected to GPS module.

'-------------------------------------------------------------------------------------
'                          PROCEDURES
'-------------------------------------------------------------------------------------
' THIS PROCEDURE IS PASSED THE FLASH LABEL AND BYTES TO SENT VIA RSOUT.
' TO CALL WE SENDUBX(FLASH_LABEL,NO_OF_BYTES)

        Proc SENDUBX(ByRef UBXADDR As Word, ByVal BYTENUM As Byte) ' PASS FLASH LOCATION BY REF AND BYTES TO GO BYVAL
            ' VARIABLE DECLARATION
            Dim BYTELOC As Byte                     ' LOCAL VARIABLE
            Dim BYTEOUT As Byte                     ' LOCAL VARIABLE

            For BYTELOC = 0 To (BYTENUM - 1)
                BYTEOUT = LRead8 UBXADDR[BYTELOC]
                RsOut BYTEOUT
            Next
        EndProc

;****** Call procedure and send the flash reference and the number of bytes to send

CMD_R:
         SENDUBX(COLD_START,12)      , SENDS 12 BYTES IN COLD START UBX MESSAGE
         HRSOut "COLD STARTING",13,10
         GoTo RRESET

Where COLD_START is a flash label (need to pass by reference). 


SBAS_OFF:  ' 16 BYTES
LData As Byte $B5,$62,$06,$16,$08,$00,$00,$03
LData As Byte $03,$00,$89,$A3,$07,$00,$5D,$CC

POLL_PVT:  ' 8 BYTES
LData As Byte $B5,$62,$01,$07,$00,$00,$08,$19

COLD_START: ' 12 BYTES
LData As Byte $B5,$62,$06,$04,$04,$00,$FF,$B9
LData As Byte $02,$00,$C8,$8F



This way I can send any message of any length stored in flash. 



okmn

what i actually need,
I need an example of the code written as "procedural" that will do the same function as the code I wrote above.
I don't know why, but I had a hard time understanding the usage of this "procedure" command.
I still do not fully understand its use.
I'm too busy doing major transformations in the company to get the mdr certificate..I guess that's why I can't pay attention (focus) to the coding.
I hope that I will understand better (even very well)and speedly  if dear Les can share with me the "procedure" version of my own code.

JonW

I think this will work as a simple procedure to measure a ADC Pin Oversample times and return the result.  You can then add any other code in to the procedure. 

I personally think its unfair to ask Les to write your code.  Just try the procedure below as a template to help understand its operation. 

        DIM LM35 AS WORD


'       PROCEDURE ADC SAMPLE
          PROC ADC_SAMPLE(ADC_PIN AS BYTE, OVERSAMPLE AS BYTE ), WORD
              DIM ADC_SUM  AS WORD      ' local vars
              DIM ADC_LOOP AS BYTE
               
                ADC_SUM = 0
                FOR ADC_LOOP = 1 to OVERSAMPLE
                    ADC_SUM = ADC_SUM + ADIN ADC_PIN
                Next
                RESULT = ADC_SUM/OVERSAMPLE
          EndProc

' THIS IS HOW YOU CALL THE PROCEDURE

          LM35 = ADC_SAMPLE(0,10)  ' read ADC pin 0, oversample 10 times and return result

Jon

okmn

How could we use the procedure command for 7 segment displays with two or 3 digits and dots (for the most efficient written code)?

JonW


JonW

Also ask yourself why you need to create a procedure?  on many occasions I find rather than creating the mother of all procedures you need to ask yourself.. are you just doing it for using the procedure function, or for the right reasons, as in many cases a subroutine is just as good.  Break the procedures into a generic library functions, so they can be reused easily and then you can build your own library of custom "generic" functions that can be reused easily and also be contained within .inc files. 

The real power is of them is the variable passing/handling/returns.  Read the manual on Proc..End Proc carefully as its VERY well written.  Look at the examples on Les' site too as these show how he intended them to be used as there are real " GEMS" hidden within his code that emphasise  the power of the procedures.  Rather than having more and more custom library functions the user can create new variants of the same "basic functions" from the compiler and adapt them to suit.

I really do understand your frustration struggling to get to grip with them but if you don't understand the principle of the procedures then its pointless someone else writing them for you as you still wont understand them







okmn

thanks a lot johnw,
I got the second biontec vaccine for covid-19 and it finished me officially..what would happen to me if covid-19 got infected!! god bless.
For this reason, I have been sick for days, I could not follow the topic I opened.

I also ask myself why the procedure command appeared, of course,
What could not be done in the normal compiler, Les created this "procedure" command.