Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Pic16lf1823_nxp Write Program

Status
Not open for further replies.

Rupesh2015

Newbie level 2
Joined
Apr 8, 2015
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
64
Hello All,

I am trying to performe write operation by PIC16LF1823 on NXP chip(SL3S4021). For this, I am using
IDE with ICD3 circuit for programming.

But it is not working.

Please let me know if it is a good way to program.

****************************************************

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
#include <stdlib.h>
 
#include <xc.h> // include standard header file
 
 
// Set Config bits - we are using internal oscillator without the PLL
#pragma config FOSC=INTOSC, PLLEN=OFF, MCLRE=ON, WDTE=OFF
#pragma config LVP=OFF, CLKOUTEN=OFF,
 
 
// Definitions
#define _XTAL_FREQ  16000000        // this is used by the __delay_ms(xx) and __delay_us(xx) functions
#define device_control_code_NXP  0b1010 // All I2C devices have a control code assigned to them.
                                    // and the control code for a serial eeprom is b'1010'
 
 
//**************************************************************************************
// Send one byte to SEE
//**************************************************************************************
void Send_I2C_Data(unsigned int databyte)
{
    PIR1bits.SSP1IF=0;          // clear SSP interrupt bit
    SSPBUF = databyte;              // send databyte
    while(!PIR1bits.SSP1IF);    // Wait for interrupt flag to go high indicating transmission is complete
}
 
//**************************************************************************************
// Read one byte from SEE
//**************************************************************************************
unsigned int Read_I2C_Data(void)
{
    PIR1bits.SSP1IF=0;          // clear SSP interrupt bit
    SSPCON2bits.RCEN=1;         // set the receive enable bit to initiate a read of 8 bits from the serial eeprom
    while(!PIR1bits.SSP1IF);    // Wait for interrupt flag to go high indicating transmission is complete
    return (SSPBUF);            // Data from eeprom is now in the SSPBUF so return that value
}
 
void Send_I2C_ControlByte_NXP(unsigned int BlockAddress,unsigned int RW_bit)
{
    PIR1bits.SSP1IF=0;          // clear SSP interrupt bit
SSPBUF = ((device_control_code_NXP << 4) | (BlockAddress <<1)) + RW_bit;  // send the control byte
    while(!PIR1bits.SSP1IF);    // Wait for interrupt flag to go high indicating transmission is complete
}
 
//**************************************************************************************
// Send start bit to SEE
//**************************************************************************************
void Send_I2C_StartBit(void)
{
    PIR1bits.SSP1IF=0;          // clear SSP interrupt bit
    SSPCON2bits.SEN=1;          // send start bit
    while(!PIR1bits.SSP1IF);    // Wait for the SSPIF bit to go back high before we load the data buffer
}
 
//**************************************************************************************
// Send stop bit to SEE
//**************************************************************************************
void Send_I2C_StopBit(void)
{
    PIR1bits.SSP1IF=0;          // clear SSP interrupt bit
    SSPCON2bits.PEN=1;          // send stop bit
    while(!PIR1bits.SSP1IF);    // Wait for interrupt flag to go high indicating transmission is complete
}
 
 
//**************************************************************************************
// Send ACK bit to SEE
//**************************************************************************************
void Send_I2C_ACK(void)
{
   PIR1bits.SSP1IF=0;          // clear SSP interrupt bit
   SSPCON2bits.ACKDT=0;        // clear the Acknowledge Data Bit - this means we are sending an Acknowledge or 'ACK'
   SSPCON2bits.ACKEN=1;        // set the ACK enable bit to initiate transmission of the ACK bit to the serial eeprom
   while(!PIR1bits.SSP1IF);    // Wait for interrupt flag to go high indicating transmission is complete
}
 
//**************************************************************************************
// Send NAK bit to SEE
//**************************************************************************************
void Send_I2C_NAK(void)
{
    PIR1bits.SSP1IF=0;           // clear SSP interrupt bit
    SSPCON2bits.ACKDT=1;        // set the Acknowledge Data Bit- this means we are sending a No-Ack or 'NAK'
    SSPCON2bits.ACKEN=1;        // set the ACK enable bit to initiate transmission of the ACK bit to the serial eeprom
    while(!PIR1bits.SSP1IF);    // Wait for interrupt flag to go high indicating transmission is complete
}
 
void main() {
 
    unsigned int ACK_bit;
    volatile unsigned int eeprom_data;//,incoming_data;
    volatile unsigned int block_address, word_address1, word_address2;
 
 
    word_address1 = 0x20;    // Set the eeprom word address that we will write the data to
    word_address2 = 0x04;    // Set the eeprom word address that we will write the data to
    eeprom_data = 0x11;     // This is the data  we are going to write
 
 
// set up oscillator control register
    OSCCONbits.IRCF = 0x0F; //set OSCCON IRCF bits to select OSC frequency=16Mhz
    OSCCONbits.SCS = 0x02; //set the SCS bits to select internal oscillator block
 
    //  PORT C Assignments
    TRISCbits.TRISC0 = 1; // RC0 = SCL signal to SEE (must be set as input)
    TRISCbits.TRISC1 = 1; // RC1 = SDA signal to SEE (must be set as input)
    TRISCbits.TRISC2 = 0; // RC2 = nc
    TRISCbits.TRISC3 = 0; // RC3 = nc
    TRISCbits.TRISC4 = 0; // RC4 = nc
    TRISCbits.TRISC5 = 0; // RC5 = nc
 
 
//**********************************************************************************
 
    SSPCONbits.SSPM=0x08;       // I2C Master mode, clock = Fosc/(4 * (SSPADD+1))
    SSPCONbits.SSPEN=1;         // enable MSSP port
 
    // **************************************************************************************
 
    SSPADD = 39;                // set Baud rate clock divider
    // **************************************************************************************
 
    __delay_ms(10); // let everything settle.
 
 
        // ******************************************************************************
        // ***********  write a single byte of data to address of 5NXP)  **************
        // *******************************************************************************
        Send_I2C_StartBit();                    // send start bit
        Send_I2C_ControlByte_NXP(block_address,0);  // send control byte with R/W bit set low
 
        // The Most significant byte is sent first, followed by the least significant byte
        Send_I2C_Data(word_address1);            // send word address
        Send_I2C_ACK();
        Send_I2C_Data(word_address2);            // send word address
        ACK_bit = 1;            // init the Ack bit high
       
                Send_I2C_StartBit();
                Send_I2C_ControlByte_NXP(block_address,0);
                ACK_bit = SSPCON2bits.ACKSTAT;  // Ack bit will come back low when the write is complete
            
        Send_I2C_Data(eeprom_data);             // send data byte
        Send_I2C_StopBit();                     // send stop bit
 
 
}

 
Last edited by a moderator:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top