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 microcontroller port pins changing

Status
Not open for further replies.

svhanu4010

Newbie
Joined
Apr 6, 2015
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
26
i want to interface both spi and i2c devices in same pic microcontroller
suppose if we have a possibility to change pins of i2c how can i
plz help me
 

Depends on which PIC you have.

Some PICs have alternative pin functions or pin mapping - others do not.

The datasheet for you PIC will have the answer, but without knowing exactly which PIC you use, it is impossible for anyone here to answer
 

You can implement either software I2C or software SPI using arbitrary GPIO pins. Or use a modern PIC with two MSSP interfaces.
 

i want to interface both spi and i2c devices in same pic microcontroller
suppose if we have a possibility to change pins of i2c
At first sight, you could share the Data pin for both, but Clock pin should be dedicated for each one, summing therefore 3 pins for both devices.
 

No reason to use h/w I2C perepherials. I suggest you to use s/w. It is available in MikroC. So you will be able to assign any free pins for I2C purpose.
But SPI is a high speed interface and I prefer to use it by h/w. Much faster.
 

can you give some example code thereby i can understand clearly

- - - Updated - - -

can you give example code for defining the SDA and SCL pins to other pins
 

I can provide an example of s/w I2c library that I currently using, but it is for STM8/32. Not for PIC. And it is really complicated.
 

hi svhanu,

example code for I2C Master....PIC16f877A


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
#include<pic.h>
#define _XTAL_FREQ 10000000
void lcd_cmd(unsigned char cmd)
{
    PORTB=cmd;
    PORTDbits.RD0=0;
    PORTDbits.RD1=0;
    PORTDbits.RD2=1;
    __delay_ms(1);
        PORTDbits.RD2=0;
}
 
 
void lcd_data(unsigned char dat)
{
    PORTB=dat;
    PORTDbits.RD0=1;
    PORTDbits.RD1=0;
    PORTDbits.RD2=1;
    __delay_ms(1);
    PORTDbits.RD2=0;
}
 
 
void lcd_ini()
{
    lcd_cmd(0x38);
    lcd_cmd(0x01);
    lcd_cmd(0x80);
    lcd_cmd(0x0c);
}
 
 
void lcd_str(unsigned char *p)
{
    while(*p != '\0')
    {
        lcd_data(*p);
        p++;
    }
}
void str(unsigned char x)
{
SSPCON2|=x;   //star condition
while((PIR1&0x08)==0x00);  //polling
PIR1&=~0x08;             //clear flag
}
 
void add(unsigned char x)
{
SSPBUF=x;             //slave addrs
while((PIR1&0x08)==0x00);
PIR1&=~0x08;
}
void main(){
unsigned char s[]={"sumit"};
int i;
TRISC=0xff;
SSPCON=0X28;        //enable +mode
PORTC=0xff;
lcd_ini();
SSPADD=24;        //baud rate
SSPSTAT=0X80;    //slew rate
 
str(0x01);
for(i=0;i<5;i++)
{
add(s[i]);
}
str(0x04);
while(1);
}

 
Last edited by a moderator:

Since PIC16F877A has only one hardware module that can do SPI or I2C, but not both at the same time, then you either need to change PIC, or use software for one or other function.

I know nothing about software I2C or software SPI.

If it were me, I would change to a better PIC
 

Attached s/w I2C library.
View attachment SW_I2C.zip
Example of use:
Code:
#include "SW_I2C.h"
Code:
I2C_SW_InitStructTypeDef I2C_Struct;
Code:
	// Init I2C
I2C_Struct.delay_func = delay;
I2C_Struct.SCL_GPIO = GPIOB;
I2C_Struct.SDA_GPIO = GPIOB;
I2C_Struct.SCL_PIN = GPIO_Pin_6;
I2C_Struct.SDA_PIN = GPIO_Pin_7;
I2C_Struct.DelayValue = 50; //130kHz clock

delay is a function. Also, you need to change or declare your own functions like a PIN_OFF, PIN_ON, PIN_IN which I used in this driver.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top