Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 27, 2024, 01:56:39 01:56


Login with username, password and session length


Pages: [1]
Print
Author Topic: PIC I2C(slave) source code request  (Read 13883 times)
0 Members and 1 Guest are viewing this topic.
edi14_10
Junior Member
**
Offline Offline

Posts: 45

Thank You
-Given: 2
-Receive: 7


« on: November 24, 2007, 02:59:39 02:59 »

Does anyone have a source code that use PIC I2C as slave???
Logged
ppa88
Active Member
***
 Muted
Offline Offline

Posts: 200

Thank You
-Given: 49
-Receive: 131


« Reply #1 on: November 24, 2007, 05:18:50 05:18 »

 I think you will get some code in Microchip Application note 734. You can google for that or directly get it from Microchip site.
Logged
fagari
Junior Member
**
Offline Offline

Posts: 35

Thank You
-Given: 4
-Receive: 41


« Reply #2 on: November 24, 2007, 03:16:54 15:16 »

Here is sample Code. I not try this code.

You may found other example at CCS Forum. (http://www.ccsinfo.com/forum/)

SLAVE I2C CODE:
Code:
#include <16F876A.H> 

// 10-bit A/D conversion
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP

#use Delay(Clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,brgh1ok)

unsigned char read_i2c(void);
void i2c_interrupt_handler(void);
void i2c_initialize(void);
void i2c_error(void);
void write_i2c(unsigned char transmit_byte);

#INT_SSP
void ssp_interupt ()
{
    i2c_interrupt_handler();
}


/* 16f87X bytes */
/* Change it per chip */
#byte PIC_SSPBUF=0x13
#byte PIC_SSPADD=0x93
#byte PIC_SSPSTAT=0x94
#byte PIC_SSPCON1=0x14
#byte PIC_SSPCON2=0x91

/* Bit defines */
#define PIC_SSPSTAT_BIT_SMP     0x80
#define PIC_SSPSTAT_BIT_CKE     0x40
#define PIC_SSPSTAT_BIT_DA      0x20
#define PIC_SSPSTAT_BIT_P       0x10
#define PIC_SSPSTAT_BIT_S       0x08
#define PIC_SSPSTAT_BIT_RW      0x04
#define PIC_SSPSTAT_BIT_UA      0x02
#define PIC_SSPSTAT_BIT_BF      0x01

#define PIC_SSPCON1_BIT_WCOL    0x80
#define PIC_SSPCON1_BIT_SSPOV   0x40
#define PIC_SSPCON1_BIT_SSPEN   0x20
#define PIC_SSPCON1_BIT_CKP     0x10
#define PIC_SSPCON1_BIT_SSPM3   0x08
#define PIC_SSPCON1_BIT_SSPM2   0x04
#define PIC_SSPCON1_BIT_SSPM1   0x02
#define PIC_SSPCON1_BIT_SSPM0   0x01

#define PIC_SSPCON2_BIT_GCEN    0x80
#define PIC_SSPCON2_BIT_ACKSTAT 0x40
#define PIC_SSPCON2_BIT_ACKDT   0x20
#define PIC_SSPCON2_BIT_ACKEN   0x10
#define PIC_SSPCON2_BIT_RCEN    0x08
#define PIC_SSPCON2_BIT_PEN     0x04
#define PIC_SSPCON2_BIT_RSEN    0x02
#define PIC_SSPCON2_BIT_SEN     0x01


#define RX_BUF_LEN  32
#define NODE_ADDR   0x02    /* I2C address of the slave node */

unsigned char slave_buffer[RX_BUF_LEN];
int buffer_index;
int comms_error;
int debug_state;


void i2c_initialize(void)
{
   /* Set up SSP module for 7-bit */
   PIC_SSPCON1 = 0x36;   /* 0011 0101 */
   PIC_SSPADD = NODE_ADDR;  /* Set the slave's address */
   PIC_SSPSTAT = 0x00;     /* Clear the SSPSTAT register. */
   enable_interrupts(INT_SSP);  /* Enable MSSP interrupts. */
}   

void i2c_interrupt_handler(void)
{

    unsigned char i2c_mask = 0x2D;  /* 0010 1101 */
    unsigned char temp_sspstat;
    unsigned char this_byte;
    unsigned char tx_byte;
    int x;
   
    /* Mask out the unnecessary bits */
    temp_sspstat = PIC_SSPSTAT & i2c_mask;
   
    switch (temp_sspstat)
    {
        /* Write operation, last byte was an address, buffer is full */
        case 0x09:   /* 0000 1001 */
            /* Clear the receive buffer */
            for (x=0; x<RX_BUF_LEN; x++)
            {
               slave_buffer[x] = 0x00;
            }
            buffer_index = 0;  /* Clear the buffer index */
            this_byte = read_i2c();   /* Do a dummy read of PIC_SSPBUF */
           
            debug_state = 1;
            break;
       
        /* Write operation, last byte was data, buffer is full */
        case 0x29:   /* 0010 1001 */
            /* Point to the buffer */
            this_byte = read_i2c();  /* Get the byte from the SSP */
            slave_buffer[buffer_index] = this_byte; /* Put it into the buffer */
            buffer_index++; /* Increment the buffer pointer */
            /* Get the current buffer index */
            /* Subtract the buffer length */
            /* Has the index exceeded the buffer length? */
            if (buffer_index >= RX_BUF_LEN)
            {
               buffer_index = 0; /* Yes, clear the buffer index. */
            }
            debug_state = 2;
            break;
       
        /* Read operation; last byte was an address, buffer is empty */
        case 0x0C:   /* 0000 1100 */
            buffer_index = 0; /* Clear the buffer index */
            /* Point to the buffer */
            tx_byte = slave_buffer[buffer_index]; /* Get byte from the buffer */
            write_i2c(tx_byte); /* Write the byte to PIC_SSPBUF */
            buffer_index++; /* increment the buffer index */
            debug_state = 3;
            break;
       
        /* Read operation; last byte was data, buffer is empty */
        case 0x2C:   /* 0010 1100 */
            /* Get the current buffer index */
            /* Subtract the buffer length */
            /* Has the index exceeded the buffer length? */
            if (buffer_index >= RX_BUF_LEN)
            {
                buffer_index = 0; /* Yes, clear the buffer index */
            }   
            /* Point to the buffer */
            /* Get the byte */
            tx_byte = slave_buffer[buffer_index];
            write_i2c(tx_byte);  /* Write to PIC_SSPBUF */
            buffer_index++; /* increment the buffer index */
            debug_state = 4;
            break;
           
        /* A NACK was received when transmitting data back from the master. */
        /* Slave logic is reset in this case. R_W=0, D_A=1, and BF=0. */
        /* If we don't stop in this state, then something is wrong!! */
        case 0x28:   /* 0010 1000 */
            debug_state = 5;
            break;
       
        /* Something went wrong!! */
        default:
            i2c_error();
            break;
    }
}

void i2c_error(void)
{
    comms_error = 1;
    printf ("I2C ERROR!\r\n");
}

void write_i2c(unsigned char transmit_byte)

    unsigned char write_collision = 1;
   
    while (PIC_SSPSTAT & PIC_SSPSTAT_BIT_BF) /* Is BF bit set in PIC_SSPSTAT? */
    {
        /* If yes, then keep waiting */
    }
   
    while (write_collision)
    {
        /* If not, then do the i2c_write. */   
        PIC_SSPCON1 &= ~PIC_SSPCON1_BIT_WCOL;  /* Clear the WCOL flag */
        PIC_SSPBUF = transmit_byte;
   
        /* Was there a write collision? */
        if (PIC_SSPCON1 & PIC_SSPCON1_BIT_WCOL)
        {
            /* Yes there was a write collision. */
            write_collision = 1;
        }
        else
        {
            /* NO, there was no write collision. */
            /* The transmission was successful */
            write_collision = 0;
        }
    }
    PIC_SSPCON1 |= PIC_SSPCON1_BIT_CKP;  /* Release the clock. */
}

/* This function returns the byte in SSPBUF */
unsigned char read_i2c(void)
{
    return PIC_SSPBUF;
}

void main(void)
{
    debug_state = 0;
    i2c_initialize();
    enable_interrupts(GLOBAL);
   printf("i2c slave 09 Jan 2005\n\r\n\r");
   
    while (1)
    {
        if (debug_state)
        {
            // printf ("debug state = %d\r\n", debug_state);
            debug_state = 0;
        }

       
    }
}

Logged
edi14_10
Junior Member
**
Offline Offline

Posts: 45

Thank You
-Given: 2
-Receive: 7


« Reply #3 on: November 26, 2007, 10:11:03 10:11 »

Thanks fagari for the source code, but can u also give me the schematic of the circuit to implement the source code?
Logged
fagari
Junior Member
**
Offline Offline

Posts: 35

Thank You
-Given: 4
-Receive: 41


« Reply #4 on: November 26, 2007, 09:13:30 21:13 »

Hello edi14_10 ;

This is only source code, no any schematic.

You connect only two pin PIC SDA(Data) and SCL(Clock).

in slave mode PIC run like I2C Eprom.

You must change source code for your need.

if give me more information yours hardware. I can help you.
Logged
edi14_10
Junior Member
**
Offline Offline

Posts: 45

Thank You
-Given: 2
-Receive: 7


« Reply #5 on: November 28, 2007, 05:14:58 05:14 »

Hello fagari ;

Actually I try to make a servo controller like SD21 or SSC32.

I really appreciate for your help Cheesy
Logged
vovchik02
Junior Member
**
Offline Offline

Posts: 62

Thank You
-Given: 43
-Receive: 13



« Reply #6 on: November 28, 2007, 10:12:37 10:12 »

In Microchip MaestroAplcation present good code for all PIC slave/master, dsPIClib present in peripheral_Lib. Simple schem's only 2-wire in datasheet for your PIC.
GOOD Luck!!
Logged
fagari
Junior Member
**
Offline Offline

Posts: 35

Thank You
-Given: 4
-Receive: 41


« Reply #7 on: November 30, 2007, 10:30:28 22:30 »

Hi edi14_10,

SSC32 servo controller is open sorce project.

http://www.lynxmotion.com/images/html/proj078.htm
Logged
edi14_10
Junior Member
**
Offline Offline

Posts: 45

Thank You
-Given: 2
-Receive: 7


« Reply #8 on: December 04, 2007, 09:36:08 09:36 »

Hi fagari.
I've already download the souce code of SSC32 (32v106XE). but i have some problem when compile it.
It appear an error message"L396:a value of type 'struct x[]' can't be assigned to an entity of type 'unsigned int'". that message appear in the pulsedge.c file. i compile it using cavr 1.25.5. can u help me to find out what is wrong? thanks.




Edi
Logged
fagari
Junior Member
**
Offline Offline

Posts: 35

Thank You
-Given: 4
-Receive: 41


« Reply #9 on: December 05, 2007, 09:41:32 21:41 »

Hi edit14_10,

SSC32 source code write with CodeVisionAVR compiler. I don't have this compiler. for that reson I can't help you.

But look at this massage on project site. may be help you.

This is the complete SSC-32 source code written in C. (Requires CodeVisionAVR compiler.) Includes readme.txt with some caveats and extra build instruction. If you have difficulty building, you can contact Mike here. " [email protected] "
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC