Microchip | The Embedded Control Solutions Company Register Login
Browse All
atruntime.com
 10 Code Snippets

SSI to UART converter

atruntime.com

The SSI (Simple Sensor Interface) protocol is a simple communications protocol designed for data transfer between computers or user terminals and smart sensors. The SSI protocol is an Application layer protocol as in the OSI model. This software example reads SSI datagram and translates it to UART then you can visualize the information on a serial console.

About this Code Example

#include <plib.h>
#include <stdlib.h>
#include <stdio.h>

// Configuration Bit settings
// SYSCLK = 72 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 36 MHz
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
// WDT OFF
// Other options are don't care
//
#pragma config FPLLMUL = MUL_18, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_2

#define SYSCLK (72000000)

#define DESIRED_BAUDRATE     (9600)      // The desired BaudRate

#define DATA PORTDbits.RD3 //INPUT
#define CLK LATDbits.LATD2 //OUTPUT

typedef union 
{
  unsigned long FULL;

 struct 
 {
   unsigned BIT0:1;
   unsigned BIT1:1;
   unsigned BIT2:1;
   unsigned BIT3:1;
   unsigned BIT4:1;
   unsigned BIT5:1;
   unsigned BIT6:1;
   unsigned BIT7:1;
   unsigned BIT8:1;
   unsigned BIT9:1;
   unsigned BIT10:1;
   unsigned BIT11:1;
   unsigned BIT12:1;
   unsigned BIT13:1;
   unsigned BIT14:1;
   unsigned BIT15:1;
   unsigned BIT16:1;
   unsigned BIT17:1;
   unsigned BIT18:1;
   unsigned BIT19:1;
   unsigned BIT20:1;
   unsigned BIT21:1;
   unsigned BIT22:1;
   unsigned BIT23:1;
   unsigned BIT24:1;
      
 };
  
}PACKAGE;

PACKAGE SSI;

void delay(unsigned int time)
{
  while(time--);
}

unsigned long ReadSSI()
{
      
  SSI.FULL=0;
  delay(600);

  CLK=0;     //START OF PACKAGE
 delay(33);

  CLK=1;
 delay(33);   
  SSI.BIT24= DATA;
  CLK=0;
 delay(33);

  CLK=1;
 delay(33);   
  SSI.BIT23= DATA;
  CLK=0;
 delay(33); 

  CLK=1;
 delay(33);   
  SSI.BIT22= DATA;
  CLK=0;
 delay(33);

  CLK=1;
 delay(33);   
  SSI.BIT21= DATA;
  CLK=0;
 delay(33);

  CLK=1;
 delay(33);   
  SSI.BIT20= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT19= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT18= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT17= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT16= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT15= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT14= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT13= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT12= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT11= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT10= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT9= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT8= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT7= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT6= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT5= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT4= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT3= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT2= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT1= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
 delay(33);   
  SSI.BIT0= DATA;
  CLK=0;
 delay(33);   

  CLK=1;
  delay(600);  //END OF PACKAGE
  
  return SSI.FULL;    
}

int main(void)
{
int pbClk;
    unsigned char buffer[50];
pbClk=SYSTEMConfigPerformance(SYSCLK);

    OpenUART2(UART_EN, UART_RX_ENABLE | UART_TX_ENABLE, pbClk/16/DESIRED_BAUDRATE-1);
   

    TRISDbits.TRISD2=0;//CLK is output
    TRISDbits.TRISD3=1;//DATA is input

    CLK=1; 
    delay(1000);

         
    while(1)
    {
                  
      sprintf(buffer, "<%06d>\r\n", (ReadSSI()/20));
     
      putsUART2(buffer);
      delay(360000);
      
    };

   return 0;
}

Categories

Release History

  • Jan 30, 2012

    Version: 1.0.0

    first release

Download Contact the Developer

*Required Fields


  • Current Version:1.0.0
  • Created:Jan 30, 2012
  • Updated:Feb 26, 2015
  • Downloads:6619
  • MPLAB Version:
  • C Compiler:C32
  • Development Tools:ICD3, OLIMEX
  • Supported Devices:PIC32

Corporate News

Resources

Stay Connected

Stay up to date with the latest news from Microchip Technology.