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.

[SOLVED] Port Expander SPI MCP23S17 Help

Status
Not open for further replies.
Advanced Member level 6
Joined
Dec 4, 2012
Messages
4,280
Helped
822
Reputation
1,654
Reaction score
791
Trophy points
1,393
Location
Bangalore, India
Visit site
Activity points
0
What is wrong with my Code. Why it is not working?


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.F5
#define CS PORTC.F6
 
#define HRDWADD 0 // device hard address  //6
unsigned char gAddrPins = HRDWADD <<1;
unsigned char SerialMode = SPIMODE; //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 MCP_Init(void);
 
void ResetMCP23X17(){
 IORESET = 0;
 delay_us(100);
 //IORESET = 1;;
}
 
void SPIWriteByte(unsigned char reg, unsigned char data_)
{
   PORTC.F6 = 0;
   SPI1_Write(WrtCmd | gAddrPins);
   SPI1_Write(reg);
   SPI1_Write(data_);
   PORTC.F6 = 1;
 
}
 
//****************************************
//   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_)
{
                                   //Else 23S17 is selected
     SPIWriteByte(reg, data_);  //
 
}
 
void MCP23X17_Init()
{
 
 
  //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;
 
      SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
      //SPI1_Init();
 
      MCP23X17_Init();
 
      while(1){
 
 
          Write23X17(GPIOA, 0xAA);
          Delay_ms(1000);
          Write23X17(GPIOA, 0x55);
          Delay_ms(1000);
 
      }
}

 

Attachments

  • Port Expander I2C v2.rar
    53.7 KB · Views: 79

I Converted this microchip sample code to mikroC Code but it is not working. What is the problem?

**broken link removed**


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
//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.F7
#define CS PORTC.F6
 
#define HRDWADD 0 // device hard address  //6
 
void main(){
 
      TRISA = TRISB = TRISD = PORTA = PORTB = PORTD = 0x00;
      TRISC = 0x00;
      PORTC = 0x00;
 
      CS = 1;
      IORESET = 0;
      Delay_us(100);
      IORESET = 1;
      
      SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV16, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_HIGH_2_LOW);
 
      CS = 0;
      SPI1_Write(0x40);
      SPI1_Write(IOCONA);
      SPI1_Write(0x28);
      CS = 1;
 
      CS = 0;
      SPI1_Write(0x42);
      SPI1_Write(IODIRA);
      SPI1_Write(0x00);
      CS = 1;
            
      while(1){
 
            CS = 0;
            SPI1_Write(0x42);
            SPI1_Write(GPIOA);
            SPI1_Write(0xA6);
            CS = 1;
 
            Delay_ms(500);
 
      }
}



Edit: I got it working. Here is the working 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
//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.F7
#define CS PORTC.F6
 
#define HRDWADD 0 // device hard address  //6
 
void main(){
 
      TRISA = TRISB = TRISD = PORTA = PORTB = PORTD = 0x00;
      TRISC = 0x00;
      PORTC = 0x00;
 
      CS = 1;
      IORESET = 0;
      Delay_us(100);
      IORESET = 1;
      
      SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV16, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_HIGH_2_LOW);
 
      CS = 0;
      SPI1_Write(0x40);
      SPI1_Write(IOCONA);
      SPI1_Write(0x28);
      CS = 1;
 
      CS = 0;
      SPI1_Write(0x40);
      SPI1_Write(IODIRA);
      SPI1_Write(0x00);
      CS = 1;
            
      while(1){
 
            CS = 0;
            SPI1_Write(0x40);
            SPI1_Write(GPIOA);
            SPI1_Write(0xAA);
            CS = 1;
 
            Delay_ms(500);
            
            CS = 0;
            SPI1_Write(0x40);
            SPI1_Write(GPIOA);
            SPI1_Write(0x55);
            CS = 1;
 
            Delay_ms(500);
 
      }
}



 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top