News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

I2COut does it really need 'Control?

Started by Ecoli-557, Mar 14, 2025, 07:54 PM

Previous topic - Next topic

Ecoli-557

Here is what I have:
Proc ReadWM8940 (CODECReg As Byte), Word
    Dim ReadValue As Word
    Low CODEC_CS
    SHOut SDA_Pin, SCL_Pin, 5,[CODECReg]
    SHIn SDA_Pin, SCL_Pin, 7,[ReadValue\16]
    High CODEC_CS
    Result = ReadValue
EndProc
The Call:
Dim wdato as word
wdato= ReadWM8940(CODEC_RST)
HSerOut ["chip id =", dec wdato, " \n\r"]
The Result:
[14:40:19 --- Rx] chip id =65535

Pepe

#61

;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 18F67K40

Config_Start
'-------------------------------------------------------------------------------
'**** Added by Fuse Configurator ****
'Use the Fuse Configurator plug-in to change these settings
FEXTOSC = OFF    ;Oscillator not enabled
RSTOSC = HFINTOSC_64MHZ    ;HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1
CLKOUTEN = OFF    ;CLKOUT function is disabled
CSWEN = On    ;Writing to NOSC and NDIV is allowed
FCMEN = OFF    ;Fail-Safe Clock Monitor disabled
MCLRE = EXTMCLR    ;If LVP = 0, MCLR pin is MCLR; If LVP = 1, RG5 pin function is MCLR
PWRTE = On    ;Power up timer enabled
LPBOREN = OFF    ;ULPBOR disabled
BOREN = SBORDIS    ;Brown-out Reset enabled , SBOREN bit is ignored
BORV = VBOR_285    ;Brown-out Reset Voltage (VBOR) set to 2.85V
ZCD = OFF    ;ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
PPS1WAY = OFF    ;PPSLOCK bit can be set and cleared repeatedly (subject to the unlock sequence)
STVREN = On    ;Stack full/underflow will cause Reset
Debug = OFF    ;Background debugger disabled
XINST = OFF    ;Extended Instruction Set and Indexed Addressing Mode disabled
WDTCPS = WDTCPS_31    ;Divider ratio 1:65536; software control of WDTPS
WDTE = OFF    ;WDT Disabled
WDTCWS = WDTCWS_7    ;window always open (100%); software control; keyed access not required
WDTCCS = LFINTOSC    ;WDT reference clock is the 31.0 kHz LFINTOSC
WRT0 = OFF    ;Block 0 (000800-003FFFh) not write-protected
WRT1 = OFF    ;Block 1 (004000-007FFFh) not write-protected
WRT2 = OFF    ;Block 2 (008000-00BFFFh) not write-protected
WRT3 = OFF    ;Block 3 (00C000-00FFFFh) not write-protected
WRT4 = OFF    ;Block 4 (010000-013FFFh) not write-protected
WRT5 = OFF    ;Block 5 (014000-017FFFh) not write-protected
WRT6 = OFF    ;Block 6 (018000-01BFFFh) not write-protected
WRT7 = OFF    ;Block 7 (01C000-01FFFFh) not write-protected
WRTC = OFF    ;Configuration registers (300000-30000Bh) not write-protected
WRTB = OFF    ;Boot Block (000000-0007FFh) not write-protected
WRTD = OFF    ;Data EEPROM not write-protected
SCANE = On    ;Scanner module is available for use, SCANMD bit can control the module
LVP = OFF    ;HV on MCLR/VPP must be used for programming
Cp = OFF    ;UserNVM code protection disabled
CPD = OFF    ;DataNVM code protection disabled
EBTR0 = OFF    ;Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
EBTR1 = OFF    ;Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
EBTR2 = OFF    ;Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
EBTR3 = OFF    ;Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
EBTR4 = OFF    ;Block 4 (010000-013FFFh) not protected from table reads executed in other blocks
EBTR5 = OFF    ;Block 5 (014000-017FFFh) not protected from table reads executed in other blocks
EBTR6 = OFF    ;Block 6 (018000-01BFFFh) not protected from table reads executed in other blocks
EBTR7 = OFF    ;Block 7 (01C000-01FFFFh) not protected from table reads executed in other blocks
EBTRB = OFF    ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
Config_End
'**** End of Fuse Configurator Settings ****

Declare Xtal = 64
Declare Optimiser_Level = 0 'DISABLES the Optimiser
Declare Create_Coff On

Declare Watchdog Off

Symbol SDA_Pin = PORTE.4                'Pin To use For the I2C data
Symbol SCL_Pin = PORTE.7                'Pin To use For the I2C clock
Symbol CODEC_CS = PORTD.2                'Pin To use For the I2C CS
         

' WM8940 I²C address (changed to $1A)
Symbol WM8940R = $34      '  WM8940 read address
Symbol WM8940W = $35      '  WM8940 write address


' WM8940 register definitions
Symbol WM8940_REG_RESET      = $00  ' Reset register
Symbol WM8940_REG_OSC_CTRL    = $19  ' Oscillator control
Symbol WM8940_REG_CLOCK_CTRL  = $1A  ' Clock control
Symbol WM8940_REG_POWER_CTRL  = $1E  ' Power control
Symbol WM8940_REG_DAC_CTRL    = $0A  ' DAC control
Symbol WM8940_REG_AUDIO_CTRL  = $31  ' Audio control

 
 Dim lec As Word
 
Low CODEC_CS

WM8940_init()
' Main loop

    ' Add necessary operations inside the loop
     lec= WM8940_Read(WM8940_REG_RESET)
HSerOut ["chip id =", Dec lec, " \n\r"]
   
Do
Loop

' WM8940 initialization with I²C address $1A
Proc WM8940_init()
WM8940_Write(WM8940_REG_RESET, $0000)        ' Reset
WM8940_Write(WM8940_REG_OSC_CTRL, $0002)    ' Enable internal oscillator
WM8940_Write(WM8940_REG_CLOCK_CTRL, $0001)  ' Set clock divider
WM8940_Write(WM8940_REG_POWER_CTRL, $0000)  ' Set system clock

WM8940_Write(WM8940_REG_DAC_CTRL, $00FF)    ' Configure output gain
WM8940_Write(WM8940_REG_AUDIO_CTRL, $00C0)  ' Enable DAC and audio output
EndProc

' Procedure to write to the WM8940 using I²C address $35

Proc WM8940_Write(Register As Byte, wdato As Word)
 
   ' HBusOut  WM8940W, [Register, Dato]

    I2COut SDA_Pin, SCL_Pin, WM8940W, [Register, wdato]

EndProc

' Procedure to read to the WM8940 using I²C address $34

Proc WM8940_Read(Register As Byte) ,Word

Dim wdato As Word
 
   ' HBusIn  WM8940R, register, [wdato]

    I2CIn SDA_Pin, SCL_Pin, WM8940R, WM8940_REG_RESET , [wdato]

    Result = wdato
EndProc