Forum: Mikrocontroller und Digitale Elektronik PIC32 -> SPI-> MAX7301 Beispiel Code


von skyloop (Gast)


Lesenswert?

Hi,

Hi,
ich bin Besitzer eines Pic32 Starter Kits. Ich soll mit diesem eine 
Kommunikation über SPI mit einem MAX7301 (I/O Expander)aufbauen. Leider 
haut das nicht ganz hin:-(

Bitte gibt mir Ratschläge oder Tipps.. Ich freue mich über jede Anregung

Falls Jemand helfen will, poste ich meinen Code. Danke schon mal im 
Voraus.

!!!!!HEADER FILE!!!!!!
CSPI.H

#ifndef _CSPI_H
#define _CSPI_H

//---------- TYPDEF ----

typedef unsigned char        byte;
typedef unsigned short int   word;
typedef unsigned long        dword;
typedef signed char          int8;
typedef signed int           int16;
typedef long                 int32;
typedef unsigned char        uint8;
typedef unsigned  int        uint16;
typedef unsigned long        uint32;


extern int rData;
extern void Init_MAX7301();

extern uint16 MAX7301_Read (unsigned char ucCommand);

extern uint16 MAX7301_Write (unsigned char ucCommand, unsigned int 
uiValue);

#endif
/* EOF */


!!!!!C-FILE!!!!!!

#include "plib.h"
#include "proc/p32mx795f512l.h"
#include "CSPI.H"

//With these settings PIC32 will run at 80MHz and peripherial bus at Fpb 
= 10MHz

#pragma config FNOSC    = PRIPLL
#pragma config FPLLIDIV = DIV_2
#pragma config FPLLMUL  = MUL_20
#pragma config FPLLODIV = DIV_1
#pragma config FPBDIV   = DIV_8
#pragma config FWDTEN   = OFF
#pragma config WDTPS    = PS1
#pragma config FCKSM    = CSDCMD
#pragma config OSCIOFNC = OFF
#pragma config POSCMOD  = XT
#pragma config IESO     = OFF
#pragma config FSOSCEN  = OFF
#pragma config CP       = OFF
#pragma config BWP      = OFF
#pragma config PWP      = OFF
#pragma config ICESEL   = ICS_PGx2
#pragma config DEBUG    = OFF

//   * Hardware   *

//   * I/O definitions  
#define SPI_OUT     PORTDbits.RD0     /*
#define SPI_IN      PORTCbits.RC4     /*
#define SPI_CLK     PORTDbits.RD10    /*
#define SPI_CS      PORTDbits.RD9     /*

void SPI(int t, int p)
{
PORTDbits.RD9=p;
TRISDbits.TRISD9 = t;
//LATDbits.LATD9=l;
}


// SPI peripheral configurations
#define SPI_CONF      0x8420
//SPI on, 16-bit master,CKE=0,CKP=0  //is this OK??????????


#define SPI_BAUD    63 // clock divider Fpb/(2 * (63+1)) , so 10MHz/ (2*
                          (63+1))=78.125KHz

void Init_MAX7301()
{
/* Config Register */
MAX7301_Write (0x04,0x01);

// Mask the Transition Register
// MAX7301_Write (0x06,0xFF);

/*set P12-P21 output*/
MAX7301_Write(0x0B, 0x55);  // P12..P15 Outp
//MAX7301_Write(0x0C, 0x55);// P16..P19 Outp
//MAX7301_Write(0x0D, 0xA5);// P20..P21 Outp

/*set P12-P21 logic high
MAX7301_Write(0x4C, 0xFF);   Do I need to DO?
MAX7301_Write(0x54, 0x03);
*/

/*Set the shutdown/run bit,normal operation*/
MAX7301_Write(0x04, 0x01);

/* Config Register*/
MAX7301_Write (0x04,0x81);
}

uint16 MAX7301_Write (unsigned char ucCommand, unsigned int uiValue)
{
SPI(1,0 );                          // Set CS low
//SPI_CS = 0;
 SPI1BUF = ((unsigned int) (ucCommand << 8)) + uiValue & ~0x8000;
// write to buffer for TX; First D15,D0 last; D15 low-> write command

//while( !SPI1STATbits.SPITBE);  IS this OK??// wait for transfer 
comüplete
// SPI_CS = 1;   // Set CS High
   SPI(0,1);
   rData = SPI1BUF;
   return rData;           // read the received value

}//writeSPI2

uint16 MAX7301_Read (unsigned char ucCommand)
{

   SPI_CS = 0;                      // Set CS low controlled by hardware
   SPI1BUF  =  ((unsigned  int)(ucCommand << 8)) | 0x8000;
// D15 high->   read command; MSB must be high for Read

   while( !SPI1STATbits.SPIRBF);    // wait until single-word transfer 
bit
                                       is set
   SPI_CS = 1;                      // Set CS  High
rData = MAX7301_Write(0x00,0x00);   // Dummy No - OP Write Access to 
shift Data
return rData;
}



int main()
{


Init_MAX7301();


} // main




Freue mich über jedes Feedback

von M. Н. (Gast)


Lesenswert?

Sorry das ich den Thread wieder rauskrame aber was für einen externen 
Oszillator benutzt du? Quarz oder quarzoszillator?

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.