Reading data from SPI Interface

This is a discussion on Reading data from SPI Interface within the C Programming forums, part of the General Programming Boards category; Hi all, I'm programming a PIC micro-controller using C and there's a little problem with the code: Code: #include <pic.h> ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    2

    Reading data from SPI Interface

    Hi all,

    I'm programming a PIC micro-controller using C and there's a little problem with the code:

    Code:
    #include <pic.h>
    
    
    __CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
      & UNPROTECT & BORDIS & IESODIS & FCMDIS);
    
    
    // Variables
    long int whatIwant;
    
    #define FOSC 8000000L			// Internal Oscillator to 8Mhz
    #define CS RA4					// AD Converter 
    #define SDI RB4					// AD Converter
    #define SCK RB6					// AD Converter
    
    
    
    
    
    // Read AD Converter Function
    long int Read3551(void)
    { 
      short b;
      long int result=0;
      char byte[3], TrashByte;			// Temp Bytes for reading
    
      CS=0;   			                // AD Chip Select low to read
      while (SDI==1);		            // Wait for SDO/RDY to become low (AD internal conversion complete)
      for (b=0; b<3; b++)			    // Loop for reading Byte 1, 2 and 3
      {
        SSPBUF=0b00001111;			    // Garbage send to start clocking in AD Data Byte 3
        while (BF==0);			    	// Wait for Buffer Full Status Bit to be set 
        byte[b]=SSPBUF;    		    	// get byte
      }
    
      if (byte[2] < 32)			    	// Last 3 Bits must be zero (no overflow and no negative sign)
       result = ((byte[0] <<16) + (byte[1] <<8 + (byte[2]));    //Add the Bytes to get the whole Value
    
      CS=1;					        	// AD Chip select high to stop AD sending
      return result;		        	// return the Value
    }	
    
    
    
    
    
    main()								// ------- MAIN -------
    {
        
    // declaration
    
    	OSCCON = 0x75;				// Oscillator to 8 Mhz
        ANSEL = 0;                  // Turn off ADC
        TRISB6= 0;					// SCK (AD) is Output (Master Mode)
        TRISA4= 0;					// RA4 (CS from AD) is Output
    	CS=1;						// CS high (AD stops sending)
    	SSPCON= 0b00110000;			// 00, Enable SPI (SSPEN) , Idle State for SDI clock is high level (CKP), Fosc:4 
    	SMP= 0;						// SPI Input Data sampled at middle of data output time
    	CKE= 0;						// SDI Data transmitted on rising Edge of SCK
    	
    
    
    // Infinite loop with main program
    
    for (;;) 	  								
     whatIwant=Read3551();
    					
    }  										//  End of MAIN

    This part of the program is supposed to read the data from the SPI interface and somehow it doesn't work.

    Can someone please help me out here? Thanks!

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    What pic are you using?

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    2
    the pic is PIC16F690, and FYI, the AD converter used is MCP3551, both devices are manufactured by microchip.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading formatted data files
    By gL_nEwB in forum C++ Programming
    Replies: 5
    Last Post: 04-22-2006, 10:09 PM
  2. Trouble Reading Data from Files
    By CConfusion in forum C Programming
    Replies: 11
    Last Post: 04-06-2006, 07:12 PM
  3. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-29-2005, 11:24 PM
  4. help with reading a data file
    By zipfur in forum C Programming
    Replies: 4
    Last Post: 11-02-2002, 05:50 PM
  5. Help reading data file...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-25-2002, 11:49 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21