Writing to MCP4725 EEPROM with PIC16F876, using assembly. - Page 1
Forum section: electronics / general

author: Avelino Sampaio
author_rank: Frequent Contributor
timestamp: March 04, 2023, 05:10:18 pm
content: Mission: Write to the EEPROM of the MCP4725 DAC, the value corresponding to 0.7vThis will be the first time I'm going to work with I2C and to complicate things a little more, it will have to be programmed in assembly.For this task I will use the PIC16F876I asked ChatGPT if they had any default code to offer me. His answer was that he couldn't offer me a code but he gave me good directions. It also offered me a very interesting code structure, which I intend to follow.QuoteMy first question is about the calculated value of the SSPADD register. I configured the SSPSTAT register (SMP=1) to work with the standard speed. I configured the SSPCON register to work with the formula (clock=Fosc/(4*(SSPADD +1)). My question is whether the Clock value to be used in the formula is 100khz, since I configured it for Standard speed. The Fosc is 4Mhz. So is it correct for me to say that the calculated value of SSPADD is 9?Code:; Initiate I2C communicationstart_i2c:; Send START signalbank1;Set bank 1bsfTRISC,3;SCL input(1)bsfTRISC,4;SDA input(1)clrfSSPCON2;clear SSPCON2 registermovlwB'10000000'movwfSSPSTAT;set to standard speedmovlwB'????????';movwfSSPADD;bank0;set bank 0movlwB'00101000';SSPEN =1movwfSSPCON;I2C module Enabled and I2C Masterbank1;set bank 1bsfSSPCON2,SEN;initiate Start conditionbtfscSSPCON2,SEN;wait for Start condition to completegoto$ - 1bank0;set bank 0bsfPORTB,3;led 1 ONreturn

author: DavidAlfa
author_rank: Super Contributor
timestamp: March 04, 2023, 08:31:36 pm
content: Yes, that value seems correct.Do you have oscilloscope or logic analyzer?Would make your development much easier!Little tip: use BANKSEL directive to automatically select the correct bank for the specified register:Code:BANKSEL TRISCBANKSEL SSPSTATThe assembler will determine the bank and replace it with bank0/1/etc.

author: Avelino Sampaio
author_rank: Frequent Contributor
timestamp: March 05, 2023, 06:48:05 am
content: David, thank you for your participation. Yes, I have the Hantek oscilloscope and I checked that it has the Decode button. I have never used this option and it will be an opportunity to learn. The issue is that I still haven't been able to complete the code to test.I have watched several videos on youtube with different techniques, however I intend to follow the typical transmission sequence, offered in the PIC16F876 datasheet.QuoteI took it a step further by building the SEND_DEVICE_ADDR subroutine:Code:; Send MCP4725 device address for writesend_device_addr:; Send device address (0xC0)movlwB'11000000'movwfSSPBUF;send the Slave address and R/W bitbtfssPIR1,SSPIF;wait for Acknowledge. SSPIF is set every 9th clock cyclegoto$ - 1bcfPIR1,SSPIF;clear SSP interrupt Flagbank1btfscSSPCON2,ACKSTAT;check ACKgotono_ack;no ackbank0bsfPORTB,2;led 2 ONreturnMy other question here is whether I would have to enable the interrupts (GIE and PEIE) to use the SSPIF flag of the PIR1 register.

author: Avelino Sampaio
author_rank: Frequent Contributor
timestamp: March 05, 2023, 12:43:25 pm
content: HiI built a test circuit. I still hope to finish the programming today, so that I can carry out the tests in practice.

author: Avelino Sampaio
author_rank: Frequent Contributor
timestamp: March 05, 2023, 02:58:55 pm
content: I completedsend_data_addrandsend_data. Missing only thestop_i2c.Code:; Send address of internal data output registersend_data_addr:; Send address of register (0x40) - Write DAC Register: (C2 = 0, C1 = 1, C0 = 0); Send address of register (0x60) - Write DAC Register and EEPROM: (C2 = 0, C1 = 1, C0 = 1)movlwB'01100000'movwfSSPBUF;send DAC Register and EEPROMbtfssPIR1,SSPIF;wait for Acknowledge. SSPIF is set every 9th clock cyclegoto$ - 1bcfPIR1,SSPIF;clear SSP interrupt Flagbank1btfscSSPCON2,ACKSTAT;check ACKgotono_ack;no ackbank0bsfPORTB,1;led 3 ONreturn; Send data to be written to the internal registersend_data:; Load 12-bit value into a register; Send the 8 most significant bits; Send the 4 least significant bits; write 0.7v in EEPROM (573)(1000111101);hightmovlwB'00100011'movwfSSPBUF;send data HightbtfssPIR1,SSPIF;wait for Acknowledge. SSPIF is set every 9th clock cyclegoto$ - 1bcfPIR1,SSPIF;clear SSP interrupt Flagbank1btfscSSPCON2,ACKSTAT;check ACKgotono_ack;no ack;lowbank0movlwB'11010000'movwfSSPBUF;send data lowbtfssPIR1,SSPIF;wait for Acknowledge. SSPIF is set every 9th clock cyclegoto$ - 1bcfPIR1,SSPIF;clear SSP interrupt Flagbank1btfscSSPCON2,ACKSTAT;check ACKgotono_ack;no ackbank0bsfPORTB,0;led4 ONreturn

author: DavidAlfa
author_rank: Super Contributor
timestamp: March 05, 2023, 04:29:14 pm
content: You can try simulating the program in Proteus, supports that Pic and several i2c devices, mostly eeproms.

author: Avelino Sampaio
author_rank: Frequent Contributor
timestamp: March 05, 2023, 11:47:01 pm
content: David, mission complete. The MCP4725 boots at 0.7v.Below is the complete ASM file, applied in this mission.