Пт июн 12, 2020 13:55:17
// PIC16F18326 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FEXTOSC = OFF // FEXTOSC External Oscillator mode Selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with 2x PLL (32MHz))
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; I/O or oscillator function on OSC2)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config MCLRE = ON // Master Clear Enable bit (MCLR/VPP pin function is MCLR; Weak pull-up enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config WDTE = OFF // Watchdog Timer Enable bits (WDT disabled; SWDTEN is ignored)
#pragma config LPBOREN = OFF // Low-power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bits (Brown-out Reset enabled, SBOREN bit ignored)
#pragma config BORV = LOW // Brown-out Reset Voltage selection bit (Brown-out voltage (Vbor) set to 2.45V)
#pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (The PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a Reset)
#pragma config DEBUG = OFF // Debugger enable bit (Background debugger disabled)
// CONFIG3
#pragma config WRT = OFF // User NVM self-write protection bits (Write protection off)
#pragma config LVP = ON // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored.)
// CONFIG4
#pragma config CP = OFF // User NVM Program Memory Code Protection bit (User NVM code protection disabled)
#pragma config CPD = OFF // Data NVM Memory Code Protection bit (Data NVM code protection disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ 32000000
#define STOP_BIT (SSP1STATbits.P)
#define START_BIT (SSP1STATbits.S)
#define I2C_TX_ST (SSP1STATbits.R_nW) //1 = Transmit is in progress
#define I2C_RX_ST (SSP1STATbits.BF) // Receive complete, SSPxBUF is full
#define WR_COL_BIT (SSP1CON1bits.WCOL) //Write Collision Detect bit- must be Cleared in software
#define RX_OV_BIT (SSP1CON1bits.SSPOV) // Receive Overflow Indicator bit- must be Cleared in software
#define ACK_ST (!SSP1CON2bits.ACKSTAT) //Acknowledge Status bit
#define ACK (SSP1CON2bits.ACKDT=0) //Acknowledge DATA bit, 0 = Acknowledge
#define NACK (SSP1CON2bits.ACKDT=1)
#define ACK_EN_BIT (SSP1CON2bits.ACKEN) //Acknowledge Sequence Enable bit
#define RC_EN_BIT (SSP1CON2bits.RCEN) //Receive Enable bit
#define STOP_EN_BIT (SSP1CON2bits.PEN) //Stop Condition Enable bit
#define RS_EN_BIT (SSP1CON2bits.RSEN) //Repeated Start Condition Enable bit
#define START_EN_BIT (SSP1CON2bits.SEN)//Start Condition Enable/
#define FOSC (32000000)
#define I2C_CLK (100000)
#define SSP_ADD ((int)((FOSC/4.0/I2C_CLK)-1))
#define SSP_EN (SSP1CON1bits.SSPEN)
#define DEV_ADD (0x3C)
#define DEV_ADDWR (DEV_ADD &(0xFE))
#define DEV_ADDRD (DEV_ADD |(0x01))
void I2C_Init(void)
{
SSP_EN=0;
INTCONbits.GIE=0;
PPSLOCK = 0x55;
PPSLOCK = 0xAA;
PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS
SSP1CLKPPS = 0x10; //SSP1 CLK -> RC0;
RC0PPS = 0x18; //RC0->SCL1;
SSP1DATPPS = 0x11; //SSP1 DAT -> RC1;
RC1PPS=0x19 ;//RC1->SDA1
PPSLOCK = 0x55;
PPSLOCK = 0xAA;
PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS
SSP1STATbits.SMP=1;//Slew rate control disabled for Standard Speed mode
SSP1STATbits.CKE=0; //Disable SMBus specific inputs
SSP1CON1bits.SSPM=8; // I2C Master mode, clock = FOSC / (4 * (SSPxADD+1))
SSP1CON2bits.GCEN=0;// General call address disabled
SSP1CON3bits.PCIE=0;// NO INTERRUPT FOR STOP
SSP1CON3bits.SCIE=0; // NO INTERRUPT ON START BIT
SSP1CON3bits.SDAHT=1; //Minimum of 300 ns hold time on SDA after the falling edge of SCL
// SSP1MSKbits.SSPMSK=0;//The received address bit n is not used to detect I2C address match
SSP1ADD=SSP_ADD;
SSP_EN=1;
TRISCbits.TRISC0=1;// RC0- SCL
TRISCbits.TRISC1=1;// RC1- SDA
ANSELCbits.ANSC0=0;
ANSELCbits.ANSC1=0;
ADCON0bits.ADON=0;
}
void I2C_TX(unsigned char txdata)
{
//while(I2C_RX_ST);
WR_COL_BIT=0;
RX_OV_BIT=0;
SSP1BUF=txdata;
while(I2C_TX_ST);
// PIR3bits.SSP1IF=0;
}
void main(void) {
TRISCbits.TRISC0 = 0; //сначала делаю аутпут лоу из-за бага, в функции INIT делаю инпутом. нихрена не меняет на самом деле.
TRISCbits.TRISC1 = 0;
LATCbits.LATC0 = 0;
LATCbits.LATC1 = 0;
TRISCbits.TRISC2 = 0; //DEBUGGING LED PIN OUTPUT
LATCbits.LATC2 = 0; //DEBUGGING LED OFF
I2C_Init();
//__delay_ms(10);
//PIR3bits.SSP1IF=0;
START_EN_BIT=1;
while(START_EN_BIT);
//PIR3bits.SSP1IF=0;
//LATCbits.LATC2 = 1; //DEBUGGING LED ON ЭТО РАБОТАЕТ
I2C_TX(0x3C);
LATCbits.LATC2 = 1; //DEBUGGING LED ON А ЭТО УЖЕ НЕТ
if(ACK_ST)
{
I2C_TX(0xAA);
// send data to be written
if(ACK_ST)
{
I2C_TX(0x22);
if(ACK_ST)
{
STOP_EN_BIT=1;
while(STOP_EN_BIT);
//PIR3bits.SSP1IF=0;
}
else
{
//return;// No acknowledge
}
}
else
{
//return;// No acknowledge
}
}
else
{
//return 0;// No acknowledge
}
//return 1;
STOP_EN_BIT=1;
while(STOP_EN_BIT);
while(1);
return;
}
Пт июн 12, 2020 19:58:38
START_EN_BIT=1;
while(START_EN_BIT);
Пт июн 12, 2020 22:17:48
#pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (The PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
Сб июн 13, 2020 11:32:14
Сб июн 13, 2020 13:08:31
Сб июн 13, 2020 13:40:28
void main(void)
{
TRISCbits.TRISC2 = 0;
LATCbits.LATC2 = 0; //DEBUGGING LED OFF
I2C_Init();
unsigned char txdata = 0;
// Make a loop that sends stuff continuously
while (1) { // Main loop
// Set start condition and wait for it
SSP1CON2bits.SEN = 1;
while(SSP1CON2bits.SEN)
;
// Send byte and wait for all nine bit clocks!
PIR1bits.SSP1IF = 0;
SSP1BUF = txdata++;
while (!PIR1bits.SSP1IF)
;
// In your "real" application, here's where you check to see
// whether or not the target ACKed it
// Set stop condition and wait for it
SSP1CON2bits.PEN = 1;
while(SSP1CON2bits.PEN)
;
LATCbits.LATC2 ^= 1; // Toggle the LED to see that it's running
// Give lots of time between bytes so that you know for dang
// sure when one ends and the next one begins
__delay_ms(1000);
} // End of Main loop
} // End of main()
Сб июн 13, 2020 14:26:51
Пн июн 15, 2020 12:10:17
Пн июн 15, 2020 17:58:19