By default, in I2CHW Slave user module, the address is configured as a user module parameter and hence cannot be changed dynamically in the project. By making some custom modifications to the I2CHW source file, dynamic addressing may be implemented.
1. Open the I2CHW_1Int.asm file from the "lib\Library Source Files" folder in the Workspace Explorer.
2. Add the following code inside the custom code area marker beginning with @PSoC_UserCode_INIT@ (Do not change this line.)
_SlaveAddress: BLK 1
This will create a variable "SlaveAddress" to hold the dynamic slave address. The "_" before the variable is to access the variable from C.
3. Find the Label "NewADDRNotSet:"
4. Below this label there is a custom code area marker "@PSoC_UserCode_BODY4@"
5. Inside this area, comment out the following code, which does an address compare with the value set in the I2CHW user module parameter.
xor A, I2CHW_1_SLAVE_ADDR
6. Add the following code, which now compares the address with the value stored in variable SlaveAddress
xor A, [_SlaveAddress]
7. In your C source file, add a reference to the SlaveAddress variable
extern BYTE SlaveAddress;
8. Update the variable SlaveAddress with the desired Slave Address. For example
SlaveAddress = 0x45; // Set Slave Address
To set the address in an assembly file:
mov [_SlaveAddress], 0x45