Hello,
I'm using a MCP23S08 8-Bit I/O Expander with Serial Interface.
I want flash the LED with an SPI Microchip "MCP23S08".
My C Code:
#include <msp430.h> #define MCP_IODIR 0x00 #define MCP_IPOL 0x01 #define MCP_GPINTEN 0x02 #define MCP_DEFVAL 0x03 #define MCP_INTCON 0x04 #define MCP_IOCON 0x05 #define MCP_GPPU 0x06 #define MCP_INTF 0x07 #define MCP_INTCAP 0x08 #define MCP_GPIO 0x09 #define MCP_OLAT 0x0a #define OPCODE 0x40 //0100A2A1 (01000000) void SPI_init(void); // initialize the SPI communication void SPI_send(UChar data); // send a SPI command void Timing_init(void); int main(void) { WDTCTL = WDTPW + WDTHOLD; // stop Watchdog timer Timing_init(); // set Timing SPI_init(); // Initialize SPI-Module UCA1 while(1) { P3OUT &= ~BIT2; // CS set low __delay_cycles(200); while ((UCA1IFG & UCTXIFG) == 0); SPI_send(MCP_IODIR); // init IODIR P3OUT |= BIT2; // CS set high __delay_cycles(200); P3OUT &= ~BIT2; // CS set low __delay_cycles(200); while ((UCA1IFG & UCTXIFG) == 0); SPI_send(OPCODE); // init OPCODE P3OUT |= BIT2; // CS set high __delay_cycles(200); P3OUT &= ~BIT2; // CS set low __delay_cycles(200); while ((UCA1IFG & UCTXIFG) == 0); SPI_send(MCP_IODIR); // init MCP_IODIR P3OUT |= BIT2; // CS set high __delay_cycles(200); P3OUT &= ~BIT2; // CS set low __delay_cycles(200); while ((UCA1IFG & UCTXIFG) == 0); SPI_send(0x00); // init 0x00 LED0 P3OUT |= BIT2; // CS set high __delay_cycles(200); } } void SPI_init(void) { P2SEL1 |= BIT4; // Select secondary function of P2.4 (UCA1CLK) P2SEL1 |= BIT5; // Select secondary function of P2.5 (UCA1SIMO) P2SEL1 |= BIT6; // Select secondary function of P2.6 (UCA1SOMI) P3DIR |= BIT2; // Declare P3.2 as Select Slave port P3OUT |= BIT2; // Start with high on Select Slave P3.2 UCA1CTLW0 |= UCCKPH; // select clock phase to rising clock phase UCA1CTLW0 |= UCMSB; // direction of shift register UCA1CTLW0 |= UCMST; // SPI master UCA1CTLW0 |= UCSYNC; // Synchronous mode enable UCA1CTLW0 |= UCSSEL_2; // SMCLK clock selected UCA1CTLW0 |= UCSWRST; // Software reset enable. Have to be selected here UCA1CTL1 ^= UCSWRST; // Software reset disable. Set this way UCSWRST = 0 } void SPI_send(UChar data) { // P3OUT &= ~BIT2; // Select Slave with setting P3.2 as low UCA1TXBUF = data; // send data // P3OUT |= BIT2; // End Select Slave with setting P3.2 as high } void Timing_init(void) { // set up Clock System CSCTL0 = CSKEY; // enable clock system CSCTL1 = DCOFSEL_3; // frequency = 8,0 MHz CSCTL2 = SELA__XT1CLK | SELS__DCOCLK | SELM__DCOCLK; // select clock sources CSCTL3 = DIVA__8 | DIVS__32 | DIVM__8; // set frequency divider CSCTL4 = XT2OFF | XTS | XT1DRIVE_0; // XT2 disabled, XT1: HF mode, low power, no bypass CSCTL0_H = 0; // disable clock system }
Could someone show me the spot I did wrong, or tell me what I might have forgotten.
I tried to study datasheet but it not confortable to get that chip working.