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.

Port Expander Problem

Status
Not open for further replies.
Joined
Dec 4, 2012
Messages
4,280
Helped
822
Reputation
1,654
Reaction score
791
Trophy points
1,393
Location
Bangalore, India
Activity points
0
I have written a code for MCP27X17 I2C Port Expander but it is not working. Can anyone help me fix the code. I have attached the mikroC Project files and Proteus File.

Address of MCP27017 is 0x000 (A0-A2 grounded)

mikroC PRO PIC v6.0.1 C Code


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
//Project: Port Expander
//MCU:     PIC16F877A
//Clock:   8 MHz
//Author:  Jayanth Devarayanadurga
 
#define I2CMODE 1
#define SPIMODE 0
#define WrtCmd  0
#define RdCmd   1
 
//#defines are with IOCON.BANK = 0
 
#define IODIRA      0x00
#define IODIRB      0x01
#define IPOLA       0x02
#define IPOLB       0x03
#define GPINTENA    0x04
#define GPINTENB    0x05
#define DEFVALA     0x06
#define DEFVALB     0x07
#define INTCONA     0x08
#define INTCONB     0x09
#define IOCONA      0x0A
#define IOCONB      0x0B
#define GPPUA       0x0C
#define GPPUB       0x0D
#define INTFA       0x0E
#define INTFB       0x0F
#define INTCAPA     0x010
#define INTCAPB     0x011
#define GPIOA       0x012
#define GPIOB       0x013
#define OLATA       0x014
#define OLATB       0x015
 
#define IORESET PORTC.F4
#define HRDADD 0x00;
 
#define HRDWADD 6 // device hard address
unsigned char gAddrPins = HRDWADD <<1;
unsigned char SerialMode = I2CMODE;
unsigned char gControlByte = 0x40;
unsigned char mode = 0; //1 for SPI, 0 for I2C
 
void I2CWriteByte(unsigned char reg, unsigned char data_);
void Write23X17(unsigned char reg, unsigned char data_);
void ResetMCP23X17();
void InitMCP(void);
 
void ResetMCP23X17(){
 IORESET = 0;
 delay_us(100);
 //IORESET = 1;;
}
 
void SPIWriteByte(unsigned char reg, unsigned char data_)
{
   SPI1_Write(gControlByte | WrtCmd | gAddrPins);
   SPI1_Write(reg);
   SPI1_Write(data_);
 
}
 
//****************************************
//   I2CWriteByte(unsigned char addr, unsigned char byte)
//   Writes a byte to the 23017
//****************************************
/*************************************************************
   Function Name:  Write23X17
   Return Value:   void
   Parameters:     Register address, Data
   Description:    Writes a 23X17 register. I2C or SPI is in
                   global byte
**************************************************************/
void Write23X17(unsigned char reg, unsigned char data_)
{
   if(SerialMode == I2CMODE)    //If 23017 selected
      I2CWriteByte(reg, data_); //
   else                                 //Else 23S17 is selected
      SPIWriteByte(reg, data_);  //
 
}
 
void InitMCP()
{
 
 
  //Configure 23017
  //Write23X17(GPPUA,  0x00);   // Pullups
  Write23X17(IOCONA, 0x40);     //
  Write23X17(IOCONB, 0x40);
  Write23X17(IODIRA, 0x00);     //All outs
  Write23X17(IODIRB, 0x00);     //All outs
 
}
 
void I2CWriteByte(unsigned char reg, unsigned char data_)
{
   I2C1_Start();
   I2C1_Wr(gControlByte | WrtCmd | gAddrPins);
   I2C1_Wr(reg);
   I2C1_Wr(data_);
   I2C1_Stop();
}
 
void main(){
 
      TRISA = TRISB = TRISD = PORTA = PORTB = PORTD = 0x00;
      TRISC = 0x00;
      PORTC = 0x00;
      
      if(mode == 0)
      I2C1_Init(100000);
      else if(mode == 1)
      SPI1_Init();
      
      InitMCP();
 
      while(1){
      
      
          Write23X17(GPIOA, 0xAA);
      
      
      }
}





Edit: The problem is solved. It is working now.
 

Attachments

  • Port Expander I2C.rar
    55.1 KB · Views: 35
Last edited:

You're not taking the part out of reset. Look in function ResetMCP23X17. IORESET=1 is commented out.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top