中科之旅-------PIC16F18855 i2c初始化

2019-04-15 12:05发布

管脚初始化

void PIN_MANAGER_Initialize(void)
{
LATB = 0x03; //RB4 init value 0  0000 0010  MOSI MISO SS = 0 ,SLCK = 1;
    LATA = 0x0;
    //LATC = 0x1;
    LATC = 0x61;
   WPUA = 0xF0;
  //  WPUB = 0xE9;//1001
   WPUB = 0xE4;//0100 RB2 pull up
    WPUC = 0xE0;

    ANSELA = 0xFF;
    ANSELB = 0x00; //RB4 digital out put
   // ANSELC = 0xE5;
   ANSELC = 0x25;
    TRISB = 0xE4;  //RB4 output reset pin  RB0 spi_cs = input RB2 MISO = input 1110 0101   
   // TRISC = 0xFE;
       TRISC = 0xBE;
    TRISA = 0xFF;
 /*
    LATB = 0x0;
    WPUE = 0x8;
    LATA = 0x0;
    LATC = 0x7F; //rc6,rc7


    WPUA = 0xFF;
    WPUB = 0xFF;
    WPUC = 0xFF;
   
    ANSELA = 0xFF;
    ANSELB = 0xFF;
    ANSELC = 0x00;



    TRISB = 0xFF;
    TRISC = 0x80;//rc6,rc7 output
    TRISA = 0xF0;
*/
    
    bool state = GIE;
    GIE = 0;
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS


    //RC0PPS = 0x10;   //RC0->EUSART:TX;
    // RXPPSbits.RXPPS = 0x11;   //RC1->EUSART:RX;
    RC3PPS = 0x16;   //RC3->MSSP2:SDA2;
    RC4PPS = 0x17;   //RC4->MSSP2:SCL2;
    SSP2DATPPSbits.SSP2DATPPS = 0x14;   //RC3->MSSP2:SDA2;
    SSP2CLKPPSbits.SSP2CLKPPS = 0x13;   //RC4->MSSP2:SCL2;


//SSP1DATPPSbits.SSP1DATPPS
    RC6PPS = 0x10;   //RC6->EUSART:TX;
    RXPPSbits.RXPPS = 0x17;   //RC7->EUSART:RX;




//spi config


   RB0PPS = 0x00;
   RB1PPS = 0x14; //spi_sck
   SSP1CLKPPSbits.SSP1CLKPPS = 0x09; //rb1
   
   SSP1DATPPSbits.SSP1DATPPS = 0x0A;  //miso
   RB3PPS = 0x15;   //mosi
   
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS


    GIE = state;


}时钟初始化void OSCILLATOR_Initialize(void)
{
    // NOSC HFINTOSC; NDIV 4; 
    OSCCON1 = 0x62;
    // CSWHOLD may proceed; SOSCPWR Low power; 
    OSCCON3 = 0x00;
    // MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled; 
    OSCEN = 0x00;
    // HFFRQ 4_MHz; 
    OSCFRQ = 0x02;
    // HFTUN 0; 
    OSCTUNE = 0x00;
    // Set the secondary oscillator
    
}mssp初始化void I2C2_Initialize(void)
{
    i2c2_object.pTrHead = i2c2_tr_queue;
    i2c2_object.pTrTail = i2c2_tr_queue;
    i2c2_object.trStatus.s.empty = true;
    i2c2_object.trStatus.s.full = false;


    i2c2_object.i2cErrors = 0;


    // SMP Standard Speed; CKE disabled; 
    SSP2STAT = 0xC0;
    // SSPEN enabled; CKP Idle:Low, Active:High; SSPM FOSC/4_SSPxADD_I2C; 
    SSP2CON1 = 0x28;
    // SBCDE disabled; BOEN disabled; SCIE disabled; PCIE disabled; DHEN disabled; SDAHT 100ns; AHEN disabled; 
    SSP2CON3 = 0x00;
    // Baud Rate Generator Value: SSPADD 4;   
    SSP2ADD = 0x09;


   
    // clear the master interrupt flag
    PIR3bits.SSP2IF = 0;
    // enable the master interrupt
    PIE3bits.SSP2IE = 1;
    
}