News:

;) This forum is the property of Proton software developers

Main Menu

Trying to understand Procedures and how to effectively use them

Started by Ecoli-557, Jan 21, 2025, 07:50 PM

Previous topic - Next topic

Ecoli-557

OK Guys (and Ladies), I am trying to work with a CODEC (WM8940) and I wish to use a Procedure as they seem to be quite useful - problem is, I can't seem to get my head wrapped around how to actually use one?!?
Some code:
'3-Wire SPI for the WM8940 CODEC ----------------------------------
    Dim SPIDataPin As Pin                       'Holds the pin to use for the SPI data line
    Dim SPIClockPin As Pin                      'Holds the pin to use for the SPI clock line
    Dim CODEC_CS As Pin                           'Holds the pin to use for the SPI CS line
    SPIDataPin = PORTE.4
    SPIClockPin = PORTE.7
    CODEC_CS = PORTD.2   
    Symbol SPI_Out As Word                      'Out data
    Symbol SPI_In As Word                       'In Data
    Symbol CODECReg As Byte                     'WM8940 register
'WM8940 CODEC -----------------------------------------------------
     ' WM8940 register space
    Symbol CODEC_RST        0x00        'Soft reset
    Symbol CODEC_Pwr1 0x01        'Power Mgmt-1
    Symbol CODEC_Pwr2 0x02        'Power Mgmt-2
    Symbol CODEC_Pwr3    0x03        'Power Mgmt-3
    Symbol CODEC_AudInt 0x04        'Audio Interface
    Symbol CODEC_CompCtl 0x05        'Companding Control
    Symbol CODEC_Clk 0x06        'Clock Gen COntrol
    Symbol CODEC_AddlCntl 0x07        'Additional COntrol
    Symbol CODEC_GPIO 0x08        'GPIO stuff
    Symbol CODEC_CtlInt     0x09        'Control Interface
    Symbol CODEC_DAC 0x0A        'DAC Control
    Symbol CODEC_DACvol 0x0B        'DAC Digital volume

    Symbol CODEC_ADC 0x0E        'ADC Control
    Symbol CODEC_ADCvol 0x0F        'ADC Digital volume
    Symbol CODEC_Notch1 0x10        'Notch Filter-1
    Symbol CODEC_Notch2 0x11        'Notch Filter-2
    Symbol CODEC_Notch3 0x12        'Notch Filter-3
    Symbol CODEC_Notch4 0x13        'Notch Filter-4
    Symbol CODEC_Notch5 0x14        'Notch Filter-5
    Symbol CODEC_Notch6 0x15        'Notch Filter-6
    Symbol CODEC_Notch7 0x16        'Notch Filter-7
    Symbol CODEC_Notch8 0x17        'Notch Filter-8

and my attempt at a Procedure:
Proc WriteWM8940 ()
    CODEC_CS = 0
    SHOut SPIDataPin, SPIClockPin, LsbFirst_H, [SPI_Out, CODECReg]
    CODEC_CS = 1
EndProc

And when I try to use it, such as:
InitWM8940:
    ' Reset CODEC
    WriteWM8940(0x00, 0x0000)

Compiler says 'Too many parameters!'
I am giving it the register data 1st, then the register address explicitly before I use the Symbol equivalent.
I feel it is so stupid simple - but can't see the forest.....
Thanks in advance for any pointers...
Regards!

Frizie

I (we) would like to help you but first read the very good HELP files (search for PROC) that Les supplied with the compiler.
Usually that gives enough information.
Ohm sweet Ohm | www.picbasic.nl

Wimax

Hello Ecoli-557,

Yes, the help file by Les is very clear.
Just a comment for the code you posted: when you define the procedure you must also indicate which variables it should receive if you plan to pass them on (if the variables that the procedure has to process are not global) i.e.:

Proc WriteWM8940 (SPI_Out as Byte, CODECReg as Byte)
    CODEC_CS = 0
    SHOut SPIDataPin, SPIClockPin, LsbFirst_H, [SPI_Out, CODECReg]
    CODEC_CS = 1
EndProc

So that the procedure "WriteWM8940" can process the parameters correctly as with the call "WriteWM8940(0x00, 0x0000)"
 

JonW

If you are passing parameters to the procedure then the procedure needs to know what the parameters are and the size of them. 

Proc WriteWM8940 (DAT as byte, CODECReg as word)
    CODEC_CS = 0
    SHOut SPIDataPin, SPIClockPin, LsbFirst_H, [DAT, CODECReg]
    CODEC_CS = 1
EndProc

EDIT: Just beat me to it Wimax: codecreg is word :-)

Ecoli-557

Ahhh, this helps - and I did read the bits in the manual but it just was not clear.
Will try stating the size within the proc.
Thanks!

Ecoli-557

I thought because I has set the vars earlier that I did not need to state them in the proc itself.
Stating the size in the proc header seemed to do the trick.
Thanks for clearing that finer detail.
I am liking this over PBP!

Wimax

Quote from: JonW on Jan 21, 2025, 08:12 PMEDIT: Just beat me to it Wimax: codecreg is word :-)

Yes ! You're right ! It's a Word !  ;D

JonW

The procedure creates new independent variables (parameters). You must match the size of what you pass to the procedure parameters and in the order they are written within the brackets.

If you want to return  a parameter then you place the size of the return variable after the brackets. Then use the result keyword to return the value back to the main program.

The manual is well written  but it can be confusing at first.

Try a simple example passing two constants adding them and returning the result.  You'll soon get the hang of it.

Proc Sum ( A as byte, B as byte), word ' pass 2 bytes and return a word
Result = ( A + B) ' assign A + B to the returned word
EndProc

To call, declare a variable to match the result at the top of the program. Example

Dim w_Sum as word
w_Sum = Sum( 2,4) ' w_Sum will be 6




Ecoli-557