nayakajit87
Member level 5
Here is simple code written to communicate with I2c device.
I could not able to get any Start bit.weather below code is correct. I have used MCC generated code library ,
I could not able to get any Start bit.weather below code is correct. I have used MCC generated code library ,
Code C# - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 #include "mcc_generated_files/mcc.h" #define PFS RB0 #define RESET1 RB1 #define SELECT RB2 #define ADVANCE RB3 #define RLY_DRV1 RB4 #define DISPCLK RB5 #define DISPDATA RB6 #define STROBE RB7 #define LED_MS RA0 #define LED_HM RA1 #define LED_SS RA2 #define START_SIGNAL RA3 #define PRG_EN RA4 #define HLD_RST RA5 #define RLY_DRV2 RC0 unsigned int sec; unsigned int min; unsigned int hour; unsigned int date; unsigned int month; unsigned int year; unsigned int day; void I2C1_SlaveRestart(void) { SSP1CON2bits.RSEN = 1; } void WaitMSSP() { while(PIR3bits.SSP1IF == 0); // SSP1IF is set when operation complete { PIR3bits.SSP1IF = 0; // clear interrupt flag } } void MCP7940_Write(unsigned char addr ,unsigned char data) { SSP1CON2bits.SEN=1; while(1==SSP1CON2bits.SEN); SSP1BUF = 0b11011110; WaitMSSP(); while(SSP1CON2bits.ACKSTAT == 1); SSP1BUF = addr; WaitMSSP(); while(SSP1CON2bits.ACKSTAT == 1); SSP1BUF = data; WaitMSSP(); SSP1CON2bits.PEN = 1; while(1==SSP1CON2bits.PEN); PIR3bits.SSP1IF=0; } unsigned int MCP7940_Read(char addr) { char result; SSP1CON2bits.RSEN = 1;// send restart while(1==SSP1CON2bits.RSEN); // wait for transmission to complete PIR3bits.SSP1IF = 0; // clear interrupt flag SSP1BUF = 0b11011110; // send control byte to MCP7940 - write WaitMSSP(); while(SSP1CON2bits.ACKSTAT== 1); // wait for RTC to be ready SSP1BUF = addr; // send register address to MCP7940 WaitMSSP(); while(SSP1CON2bits.ACKSTAT == 1); // wait for RTC to be ready SSP1CON2bits.RSEN = 1; // send restart WaitMSSP(); SSP1BUF= 0b11011111; // send control byte to MCP7940 - read WaitMSSP(); while(SSP1CON2bits.ACKSTAT== 1); // wait for RTC to be ready SSP1CON2bits.RCEN = 1; // put master in receive mode while(1==SSP1CON2bits.RCEN); // wait for transmission to complete PIR3bits.SSP1IF = 0; // clear interrupt flag SSP1CON2bits.ACKDT = 1; SSP1CON2bits.ACKEN = 1; while(1==SSP1CON2bits.ACKEN); // wait for NACK send to complete SSP1CON2bits.PEN = 1; // send STOP while(PEN); PIR3bits.SSP1IF = 0; // clear interrupt flag result = SSP1BUF; // get byte that was sent by RTC return result; // and return it to caller } unsigned int bcdtodecimal(unsigned int bcd) { unsigned int decimal; decimal = (((bcd & 0xF0) >> 4) * 10) + (bcd & 0x0F); return decimal; } void main(void) { // Initialize the device SYSTEM_Initialize(); // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts // Use the following macros to: // Enable the Global Interrupts INTERRUPT_GlobalInterruptEnable(); // Disable the Global Interrupts //INTERRUPT_GlobalInterruptDisable(); // Enable the Peripheral Interrupts INTERRUPT_PeripheralInterruptEnable(); // Disable the Peripheral Interrupts //INTERRUPT_PeripheralInterruptDisable(); I2C1_Initialize(); MCP7940_Write(0,0x10); MCP7940_Write(1,0x10); MCP7940_Write(2,0x10); MCP7940_Write(3,0x10); MCP7940_Write(4,0x10); MCP7940_Write(5,0x10); MCP7940_Write(6,0x10); while (1) { sec = MCP7940_Read(0); sec=bcdtodecimal(sec); min = MCP7940_Read(1); min = bcdtodecimal(min); hour = MCP7940_Read(2); hour=bcdtodecimal(hour); day= MCP7940_Read(3); //Process_RUN_MODE(); /*LED_MS=1; if (Main_Flag == 0) { Main_Flag = 1; PRG_RUN_RESET(); } Process_RUN_MODE(); */ } }
Last edited by a moderator: