Hi Tech C software I2C and SPI not working

Programmer, In-Circuit Debugger, PIC Start-Up Kit, Memory Interface...

Hi Tech C software I2C and SPI not working

Postby zhenning » Sat Aug 20, 2011 9:42 pm

Hi,

I am having problem with both software I2C and SPI. Both cant seems to initialize DS1307 and 74HC595. I hope someone can help. Thanks in advance.


SPI

CODE: SELECT_ALL_CODE
#include <htc.h>
#include "sw_spi.h"
#include "delay.h"

void main(void){
   
// configure software SPI
  OpenSWSPI();
  char x = 0b10000000;

//ClearCSSWSPI(); //clear CS pin
//    WriteSWSPI(0x40);
//    WriteSWSPI(0x05);
//    WriteSWSPI(0x38);
//    SetCSSWSPI();

ClearCSSWSPI(); //clear CS pin
    WriteSWSPI(0x40);
    WriteSWSPI(0x00);
    WriteSWSPI(0x00);
    SetCSSWSPI();

ClearCSSWSPI(); //clear CS pin
    WriteSWSPI(0x40);          //write opcode
    WriteSWSPI(0x0A);          //write address
    WriteSWSPI(0xFF);          //all outputs high
    SetCSSWSPI();
}




CODE: SELECT_ALL_CODE
#include <pic18.h>
#ifndef __SW_SPI_H
#define __SW_SPI_H

/* PIC18 Software SPI library header
 *
 * To use the software spi routines, the user must define
 * the port and tris register for each of the CS, DIN, DOUT,
 * and SCK pins.  The SPI mode must also be defined (MODE0,
 * MODE1, MODE2, MODE3).
 *
 * Define the port and pin for each of the software SPI pins
 *  - Chip select pin CS must have a port and tris definition.
 *  - Data in pin DIN must have a port and tris definition.
 *  - Data out pin DOUT must have a port and tris definition.
 *  - Clock pin SCK must have a port and tris definition.
 */

#if defined (_18F13K50) || defined (_18F14K50) ||\
    defined (_18LF13K50) || defined (_18LF14K50) ||\
    defined (_18F13K20) || defined (_18F14K20) ||\
    defined (_18F13K22) || defined (_18F14K22) ||\
    defined (_18LF13K22) || defined (_18LF14K22)

#define SW_CS_PIN         PORTDbits.RD2      // Chip Select
#define TRIS_SW_CS_PIN    TRISD2
#define SW_DIN_PIN        PORTDbits.RD1     // Data in
#define TRIS_SW_DIN_PIN   TRISD1
#define SW_DOUT_PIN       PORTDbits.RD3    // Data out
#define TRIS_SW_DOUT_PIN  TRISD3
#define SW_SCK_PIN        PORTDbits.RD0     // Clock
#define TRIS_SW_SCK_PIN   TRISD0
 
#else
#define SW_CS_PIN         LATD2      // Chip Select
#define TRIS_SW_CS_PIN    TRISD2
#define SW_DIN_PIN        LATD1     // Data in
#define TRIS_SW_DIN_PIN   TRISD1
#define SW_DOUT_PIN       LATD3    // Data out
#define TRIS_SW_DOUT_PIN  TRISD3
#define SW_SCK_PIN        LATD0  // Clock
#define TRIS_SW_SCK_PIN   TRISD0
#endif
// Define the mode for software SPI
// Refer to the SPI module for PIC17C756 for definitions of CKP and CKE
// Only one mode can be uncommented, otherwise the software will not work
//#define _MODE0  // CKP=0,CKE=0
//#define _MODE1  // CKP=1,CKE=0
#define _MODE2  // CKP=0,CKE=1
//#define _MODE3  // CKP=1,CKE=1

/* OpenSWSPI  (SWOpenSPI)
 * Configure Software SPI I/O pins
 */
void OpenSWSPI(void);
#define SWOpenSPI OpenSWSPI

/* WriteSWSPI  (SWWriteSPI)
 * Write a data byte to the SPI, return byte read
 */
char WriteSWSPI(char);
#define SWWriteSPI WriteSWSPI

/* SetCSSWSPI  (SWSetCSSPI)
 * Sets the CS pin
 */
#define SetCSSWSPI( )   SW_CS_PIN=1   // Sets the CS pin
#define SWSetCSSPI SetCSSWSPI

/* ClearCSSWSPI  (SWClearCSSPI)
 * Clears the CS pin
 */
#define ClearCSSWSPI( ) SW_CS_PIN=0  // Clears the CS pin
#define SWClearCSSPI ClearCSSWSPI

/* putcSWSPI  (SWputcSPI)
 * putc is really write
 */
#define putcSWSPI WriteSWSPI
#define SWputcSPI putcSWSPI


#endif /* __SW_SPI_H */




I2C

CODE: SELECT_ALL_CODE
#include <htc.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

#include "delay.h"
#include "sw_i2c.h"


//-----------------------------------------------------------------
//Routines for DS1307
//-----------------------------------------------------------------


void write_ds1307(unsigned char address, unsigned char data)
{   di();
   short int status;
   SWStartI2C();
   SWPutcI2C(0xd0);
   SWPutcI2C(address);
   SWPutcI2C(data);
   SWStopI2C();
   SWRestartI2C();
   status=SWPutcI2C(0xd0);
   while(status==1)
   {
      SWStartI2C();
      status=SWPutcI2C(0xd0);
   }
   DelayMs(10);
   ei();
}
//==========================
// read data one byte from DS1307
//==========================
unsigned char read_ds1307(unsigned char address)
{
   di();
   unsigned char data;
   SWStartI2C();
   SWPutcI2C(0xd0);
   SWPutcI2C(address);
   SWRestartI2C();
   SWPutcI2C(0xd1);
   data = SWGetsI2C( 0, 0 );
   SWStopI2C();
   ei();
   return(data);
}




void main(void){
   
   unsigned char sec,min,hour,day,date,month,year;
      
   BRGH = 1 ;
   SYNC = 0;
   SPBRG = 129;//9600
   TXSTA = 0x20;
   RCSTA = 0x09;
   TRISC6 = 0;
   TRISC7 = 1;
   
   sec = read_ds1307(0);
     write_ds1307(0,sec & 0x7F); // enable oscillator(bit 7 =0)
   
   //Set Time
      write_ds1307(0,0x80); //Reset second to 0 sec. and stop Oscillator
      write_ds1307(1,0x10); //write min 27
      write_ds1307(2,0x01); //write hour 14
      write_ds1307(3,0x02); //write day of week 2:Monday
      write_ds1307(4,0x05); // write date 17
      write_ds1307(5,0x01); // write month 6 June
      write_ds1307(6,0x09); // write year 8 --> 2008
      write_ds1307(7,0x10); //SQWE output at 1 Hz
      write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator
      
   while(1)
   {

         
    sec=read_ds1307(0);   // read second
     min=read_ds1307(1);   // read minute
     hour=read_ds1307(2);  // read hour
     day=read_ds1307(3);   // read day
     date=read_ds1307(4);  // read date
     month=read_ds1307(5); // read month
     year=read_ds1307(6);  // read year
     putch(0x0c);
   
       printf("Time : %02d:%02d:%02d\r\n",hour,min,sec);
     printf("Day  : %02d\r\n",date);
     printf("Date : %02d/%02d/20%02d\r\n",day,month,year);
     DelayMs(500);   
   }   
}   





CODE: SELECT_ALL_CODE

#include <htc.h>
#ifndef __SWI2C16_H
#define __SWI2C16_H

/* PIC18 software I2C interface header */

/*****   COMMON FUNCTION PROTOTYPES   *****/

void SWStopI2C ( void );                // Generate bus stop condition
void SWStartI2C ( void );               // Generate bus start condition
void SWRestartI2C ( void );             // Generate bus restart condition
void SWStopI2C ( void );                // Generate bus stop condition

// USER NEEDS TO DEFINE DATA AND CLOCK  RESISTORS ARE REQUIRED ON
// DATA AND CLOCK 

#if defined(__18F1220) || defined(__18F1320) ||\
   defined(__18F1230) || defined(__18F1330)
#define  DATA_LOW   TRISB4 = 0; // define macro for data pin output
#define  DATA_HI    TRISB4 = 1; // define macro for data pin input
#define  DATA_LAT   LATB4        // define macro for data pin latch
#define  DATA_PIN   PORTBbits.RB4         // define macro for data pin

#define  CLOCK_LOW  TRISB3 = 0; // define macro for clock pin output
#define  CLOCK_HI   TRISB3 = 1; // define macro for clock pin input
#define  SCLK_LAT   LATB3        // define macro for clock pin latch
#define  SCLK_PIN   PORTBbits.RB3         // define macro for clock pin

#elif defined(__18F2455) || defined(__18F2550) || \
      defined(__18F4455) || defined(__18F4550) || \
     defined(__18F2450) || defined(__18F2458) ||\
     defined(__18F2553) || defined(__18F4450) ||\
     defined(__18F4458) || defined(__18F4553)

#define  DATA_LOW   TRISB0 = 0; // define macro for data pin output
#define  DATA_HI    TRISB0 = 1; // define macro for data pin input
#define  DATA_LAT   LATB0        // define macro for data pin latch
#define  DATA_PIN   PORTBbits.RB0         // define macro for data pin

#define  CLOCK_LOW  TRISB1 = 0; // define macro for clock pin output
#define  CLOCK_HI   TRISB1 = 1; // define macro for clock pin input
#define  SCLK_LAT   LATB1        // define macro for clock pin latch
#define  SCLK_PIN   PORTBbits.RB1         // define macro for clock pin

#elif defined(__18F24J50) || defined(__18F25J50) || \
     defined(__18LF24J50) || defined(__18LF25J50) || \
      defined(__18F26J50) || defined(__18F44J50) || \
      defined(__18LF26J50) || defined(__18LF44J50) || \
     defined(__18F45J50) || defined(__18F46J50) || \
     defined(__18LF45J50) || defined(__18LF46J50)
    
#define  DATA_LOW   TRISB5 = 0; // define macro for data pin output
#define  DATA_HI    TRISB5 = 1; // define macro for data pin input
#define  DATA_LAT   LATB5        // define macro for data pin latch
#define  DATA_PIN   PORTBbits.RB5         // define macro for data pin

#define  CLOCK_LOW  TRISB4 = 0; // define macro for clock pin output
#define  CLOCK_HI   TRISB4 = 1; // define macro for clock pin input
#define  SCLK_LAT   LATB4        // define macro for clock pin latch
#define  SCLK_PIN   PORTBbits.RB4         // define macro for clock pin    

#else

#define  DATA_LOW   TRISC4 = 0; // define macro for data pin output
#define  DATA_HI    TRISC4 = 1; // define macro for data pin input
#define  DATA_LAT   LATC4        // define macro for data pin latch
#define  DATA_PIN   PORTCbits.RC4         // define macro for data pin

#define  CLOCK_LOW  TRISC3 = 0; // define macro for clock pin output
#define  CLOCK_HI   TRISC3 = 1; // define macro for clock pin input
#define  SCLK_LAT   LATC3        // define macro for clock pin latch
#define  SCLK_PIN   PORTCbits.RC3         // define macro for clock pin
#endif

/*****   FUNCTION PROTOTYPES FOR PIC18CXXX   *****/
signed char SWAckI2C( void );             // Read bus ACK condition
signed char Clock_test( void );
unsigned int SWReadI2C( void );          // Read in single byte
signed char SWWriteI2C(  unsigned char data_out ); // Write out single byte
signed char SWGetsI2C(  unsigned char *rdptr,  unsigned char length );   // Read in string from I2C module
signed char SWPutsI2C(  unsigned char *wrptr );    // Write string to I2C module

#define  SWPutcI2C    SWWriteI2C
#define  SWGetcI2C    SWReadI2C
#define  SWNotAckI2C  SWAckI2C
#endif


zhenning
Enthusiast
 
Posts: 351
Joined: Thu Dec 30, 2010 12:32 am

Re: Hi Tech C software I2C and SPI not working

Postby shahrul » Sat Aug 20, 2011 10:16 pm

You can see this sample code and video
1. I2C (DS1307)
2. SPI (74595)
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor


Return to PIC Development Tool

Who is online

Users browsing this forum: No registered users and 25 guests