Hello,
Do i have to send the address of the LM4970 every time i send it one byte of data, for example, i want to program all of its register, meaning i have to have separate transaction everytime i want to write to it.
Second question, is there a specific order that i have to send the data in, meaning, does it load each register in order, i dont know if my question makes sense.
I have attached my code, can you guys take a look and comment?
#include "p24hxxxx.h" #include "p24HJ64GP502.h" #include <stdio.h> #include <stdlib.h> #include "i2c.h" #ifdef __PIC24HJ64GP502__ _FOSCSEL(FNOSC_FRC); // set oscillator mode for FRC ~ 8 Mhz _FOSC(FCKSM_CSDCMD & OSCIOFNC_ON & POSCMD_NONE); // use OSCIO pin for RA3 _FWDT(FWDTEN_OFF); // turn off watchdog #elif defined(__dsPIC33FJ64MC802__) _FOSCSEL(FNOSC_FRC); _FOSC(FCKSM_CSDCMD & OSCIOFNC_ON & POSCMD_NONE); _FWDT(FWDTEN_OFF); #endif #define LM4970_ADDR_0 0x7A // first 7 bits of address 0 (ADR = LOW) #define LM4970_ADDR_1 0x7B // first 7 bits of address 1 (ADR = HIGH) #define LM4970_MODE_NORM 0x00 // power up, normal I2C, no random #define LM4970_MODE_RAND 0x04 // power up, normal I2C, random #define LM4970_FREQ 0x50 // 6.3kHz High Pass / 15kHz PWM #define LM4970_PATT_AUDIO 0x60 // pattern gen by audio input #define LM4970_PATT_I2C 0x61 // pattern gen by I2C - all off #define LM4970_CURR 0xAA // 1x current (21mA)on LED 1-3 #define LM4970_GAIN 0xEA // gain = 10dB / midband = medium #define I2C_ON 0xFFFF #define I2C_START_EN 0xFFFF void i2c_init(); void i2c_send(int x, int y); int main () { OSCCON = 0x0000; OSCCONbits.COSC=1; // FRC osc with PLL OSCCONbits.NOSC=1; OSCCONbits.IOLOCK=1; //peripheral pins lock CLKDIVbits.PLLPOST=1; //N2=4 CLKDIVbits.PLLPRE=6; //N1=8 CLKDIVbits.DOZE =1; // doze 2 CLKDIVbits.DOZEN=1; PLLFBDbits.PLLDIV=171; // M=173 OSCTUN = 0x0000; RCONbits.SWDTEN = 0; //Disable Watch Dog Timer AD1PCFGL=0xFFFF; PORTBbits.RB8=0; //clock on oin B8 PORTBbits.RB9=0; //data on pin B9 i2c_init(); // start I2C module i2c_send(LM4970_ADDR_0,LM4970_MODE_NORM); // power up, normal I2C, no random i2c_send(LM4970_ADDR_0,LM4970_FREQ); // 6.3kHz High Pass / 15kHz PWM i2c_send(LM4970_ADDR_0,LM4970_PATT_AUDIO); // // pattern gen by audio input i2c_send(LM4970_ADDR_0,LM4970_CURR); // 1x current (21mA)on LED 1-3 i2c_send(LM4970_ADDR_0,LM4970_GAIN); // gain = 10dB / midband = medium I2C1CONbits.I2CEN = 0; // stop I2C module } void i2c_init() { I2C1BRG = 197; I2C1CONbits.I2CEN = 0; // Disable I2C Mode I2C1CONbits.DISSLW = 1; // Disable slew rate control IFS1bits.MI2C1IF = 0; // Clear Interrupt I2C1CONbits.I2CEN = 1; // Enable I2C Mode I2C1CONbits.RCEN = 0; I2C1STATbits.IWCOL = 0; // no collision I2C1STATbits.BCL = 0; // no collision on master bus } void i2c_send(int x, int y) { I2C1CONbits.SEN = 1; // start condition enable while(I2C1CONbits.SEN); I2C1TRN = x; // place address into send register while(I2C1STATbits.TBF); while(I2C1STATbits.TRSTAT); while(I2C1STATbits.ACKSTAT); I2C1TRN = y; //place data into send register while(I2C1STATbits.TBF); while(I2C1STATbits.TRSTAT); while(I2C1STATbits.ACKSTAT); I2C1CONbits.PEN = 1; // stop condition enable }
can i program the LM4970 in that manner that i have in the code?