silverwolfman
Junior Member level 1
- Joined
- Feb 22, 2013
- Messages
- 16
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,408
Hi everyone,
I encountered a problem when using the I2C module on chip. dsPIC33FJ, Mplab X IDE, the I2C works fine on a same board without power and peripheral circuit(power from pickit and just the ASDA and ASCL pins connect to the slave chip), but things changed after adding in the power and other circuit: the ASDA and ASCL stay high for the whole time. Both lines are not connect to any other circuit except 2k pull up and Microchip and slave chip, and there is a message pops out when I tried to modify the configuration bits in IDE: "Changes will not written to target." any suggestion on how to debugging?
Thanks!
I encountered a problem when using the I2C module on chip. dsPIC33FJ, Mplab X IDE, the I2C works fine on a same board without power and peripheral circuit(power from pickit and just the ASDA and ASCL pins connect to the slave chip), but things changed after adding in the power and other circuit: the ASDA and ASCL stay high for the whole time. Both lines are not connect to any other circuit except 2k pull up and Microchip and slave chip, and there is a message pops out when I tried to modify the configuration bits in IDE: "Changes will not written to target." any suggestion on how to debugging?
Thanks!
Code:
// FBS
#pragma config BWRP = WRPROTECT_OFF // (Boot Segment may be written)
#pragma config BSS = NO_FLASH // Boot Segment Program Flash Code Protection (No Boot program Flash segment)
#pragma config RBS = NO_RAM // Boot Segment RAM Protection (No Boot RAM)
// FSS
#pragma config SWRP = WRPROTECT_OFF // Secure Segment Program Write Protect (Secure segment may be written)
#pragma config SSS = NO_FLASH // Secure Segment Program Flash Code Protection (No Secure Segment)
#pragma config RSS = NO_RAM // Secure Segment Data RAM Protection (No Secure RAM)
// FGS
#pragma config GWRP = OFF // General Code Segment Write Protect (User program memory is not write-protected)
#pragma config GSS = OFF // General Segment Code Protection (User program memory is not code-protected)
// FOSCSEL
#pragma config FNOSC = PRI // Oscillator Mode (Primary Oscillator (XT, HS, EC))
#pragma config IESO = OFF // Internal External Switch Over Mode (Start-up device with user-selected oscillator source)
// FOSC
#pragma config POSCMD = HS // Primary Oscillator Source (HS Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config IOL1WAY = ON // Peripheral Pin Select Configuration (Allow Only One Re-configuration)
#pragma config FCKSM = CSECME // Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are enabled)
// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler (1:32,768)
#pragma config WDTPRE = PR128 // WDT Prescaler (1:128)
#pragma config WINDIS = OFF // Watchdog Timer Window (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog timer enabled/disabled by user software)
// FPOR
#pragma config FPWRT = PWR128 // POR Timer Value (128ms)
#pragma config ALTI2C = ON // Alternate I2C pins (I2C mapped to ASDA1/ASCL1 pins)
#pragma config LPOL = OFF // Motor Control PWM Low Side Polarity bit (PWM module low side output pins have active-low output polarity)
#pragma config HPOL = OFF // Motor Control PWM High Side Polarity bit (PWM module high side output pins have active-low output polarity)
#pragma config PWMPIN = OFF // Motor Control PWM Module Pin Mode bit (PWM module pins controlled by PWM module at device Reset)
// FICD
#pragma config ICS = PGD2 // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
#pragma config JTAGEN = OFF // JTAG Port Enable (JTAG is Disabled)
int main() {
//This function will initialize the I2C(1) peripheral.
//First set the I2C(1) BRG Baud Rate.
//Consult the dSPIC Data Sheet for information on how to calculate the
//Baud Rate.
I2C1BRG = 0x0007;//set to be 400khz
//Now we will initialise the I2C peripheral for Master Mode, No Slew Rate
//Control, and leave the peripheral switched off.
I2C1CON = 0x1200;
I2C1RCV = 0x0000;
I2C1TRN = 0x0000;
//Now we can enable the peripheral
I2C1CON = 0x9200;
I2C1CONbits.SEN = 1; //Generate Start COndition
while (I2C1CONbits.SEN); //Wait for Start COndition
while(1){
while (I2C1STATbits.TRSTAT); //Wait for bus to be idle
I2C1TRN = 0x7f; //Load byte to I2C1 Transmit buffer
while (I2C1STATbits.TBF); //wait for data transmission
}
}
Last edited: