Haberler:

Forum kuralları güncellendi LÜTFEN  okuyunuz:  https://bit.ly/2IjR3ME

Ana Menü

Hitech de SPI kulllanımı

Başlatan ORC, 14 Ağustos 2008, 13:21:25

ORC

SLm arkadaslar

Ben hi-tech 9.5 compilerını kullanıyorum ama iiçinde ki samples klasorunde spi ile ilgili bişey bulamadım yani özel bi komutu mu var bunun yada benim bilmediğim bi fonksiyon yardımcı olursanız sevinriim..

Şimdiden teşekkürler  ;)

ORC

tamam buldum  :oops:

Alıntı Yap//
//  Spi.c
//
// Copyright(c) 2001 Blodgett Combi
//
// Author: Scott Douglas
// Used by kind permission by PICuWEB http://www.workingtex.com/htpic
// Date: 26 Sep 00
// Build Version: .01
//
//  Compiler Hi-Tech C V7.86
//
// Program notes:
//  ----------------------------------------------------------------------
// 1. First pass using PIC development board.
//
// Revision History
//  ----------------------------------------------------------------------
// .1 - Switch from C2C to Hi-Tech compiler       9 January 2001
//
//
//  SPI Bus interaction  PIC16F877 and  Linear Tech LTC2404
//------------------------------------------------------------------------
//
//  1. Start Clock by setting chip select bit Hi on PIC  (PORTC,CCPI)
//  2. Empty the SSPBUF register with a dummy read.
//  3. Write to SSPBUF MUX selection.
//    NOTE !  The mux channel will not be used untill next pass thru!
//  4. Delay while transmission of MUX selection over buss happens
//  5. Empty the SSPBUF register of garbage with a dummy read.
//  6. Set a flag to test in main loop that SPI activty is happening.
//  7. Poll in main loop for completion of conversion process, or wait
//     133ms(US)/160ms(Euro)
//  8. Write a garbage number to SSPBUF to start send transmission of first
//     BYTE from LTC2404
//  9. Wait 1 ms to allow to clock out garbage, and clock in real number
// 10. Get real FIRST byte from SSPBUF (bit 31-24)
// 11. Garbage write to SSPBUF for next byte
// 12. Wait 1 ms to allow to clock out garbage, and clock in real number
// 13. Get real byte SECOND byte from SSPBUF (bit 23-15)
// 14. Garbage write to SSPBUF for next byte
// 15. Wait 1 ms to allow to clock out garbage, and clock in real number
// 16. Get real byte THIRD byte from SSPBUF (bit 14-7)
// 17. Garbage write to SSPBUF for next byte
// 18. Wait 1 ms to allow to clock out garbage, and clock in real number
// 19. Get real byte FOURTH byte from SSPBUF (bit 6-0)
//
// ****!!!! Note. The first real 4 bytes should be thrown out that occur
// when the LTC2404 is powered up.
//
//  Note: Data from A/D sent on PIC's falling edge clk
//  PIC should be read from when clk rises...(timing diag
//  disagrees with data sheet.)
//  Since cs is low for reading, A/D drives PIC SDI low if the
//  conversion is done, so, **bit 31 is already waiting with a low.
//
//   Linear Tech bytcode structure
//----------------------------------------------------------------------
//    **bit 30 is always low,
//    **bit 29 = 1=positive sign bit,
//    **bit 28 = is the (EXR) extended range bit
//    **bit 27 = MSB ......bit 4=LSB,
//    **bit 3..0 = sub LSB bits (4 extras for grins)
//
//
#include "spi.h"
#include "global.h"
#include <stdio.h>
#include <stdlib.h>

void SPI_IO(void)
//
// Purpose: Send or receive bytes on the SPI bus.
// Entry  :
// Exit:    The output bytes has been sent, new input bytes have been received.
//
{
   SSPSTAT = 0b01000000;// Configure register for SPI..(b6=0).
   SSPCON = 0b00100010; // Configure register  for SPI,
   RD7 = 0;             // The output enable (RD7) should be left low
                        // Except at power on (down by hardware pullup
                        // until the software says it's an output, and
                        // writes a low to drive the output bits in the latch.
   RD6 = 0;             // Parallel load, low to latch input
   RD6 = 1;             // Parallel load, GO!
   RD5 = 0;             // IO_SPI_IN_Enable

   //Byte1
   SSPBUF = Out_Byte1; // Send output byte 1
   DelayMs(1);         // Wait a wee bit
   In_Byte1 = SSPBUF;  // Recv input byte 1
   DelayMs(1);

   // Byte 2
   SSPBUF = Out_Byte2; // Send output byte 2
   DelayMs(1);         // Wait a wee bit
   In_Byte2 = SSPBUF;  // Recv input byte 2
   DelayMs(1);

   // Byte 3
   SSPBUF = Out_Byte3; // Send output byte 3
   DelayMs(1);         // Wait a wee bit
   In_Byte3 = SSPBUF;  // Recv input byte 3
   DelayMs(1);

   // Byte 4
   SSPBUF = Out_Byte4; // Send output byte 4
   DelayMs(1);         // Wait a wee bit
   In_Byte4 = SSPBUF;  // Recv input byte 4
   RD4 = 0;            // RD4 is parallel output strobe
   RD4 = 1;            // Strobe
   RD4 = 0;            // Return strobe inactive
   RD5 = 1;            // IO_SPI_IN_Enable  clear SPI bus for use by others
                       // (SPI_DO is active a lot, with many devices,)
   DelayMs(1);
   SSPSTAT = 0b11000000; // Configure register for SPI..PUT IT BACK FOR PIC ONBOARD A/D.
   SSPCON = 0b00100010; // Configure register  for SPI, PUT IT BACK FOR PIC ONBOARD A/D
}


void Read_SPI_AD(void)
//
// Purpose: Send or receive TC data on the SPI bus from th LinearTech device.
// Entry  :
// Exit:    The output bytes has been sent, new input bytes have been received.
//
{
   int i;                       // General loop counter
   int j;                       // " "
   long Word_Mask;              // Word mask
   char Temp_Byte;              // Temporary byte used for mask testing
   char Temp_Byte1, Temp_Byte2; // First two bytes from SPI A/D module
   char Temp_Byte3, Temp_Byte4; // Second two bytes from SPI A/D module
   long SPI_Value;              // 32 bit SPI value
   Word_Mask = 1;               // Initially set mask to 1
   SPI_Value = 0;
   RC2 = 1; // Starts the a/d zeroing conv=66ms, then does 66ms real conv...

   Temp_Byte = SSPBUF;          // Flush the sspbuf reg. Extra reads ok.

   // Select the chan low 4 bit pattern of SSPBUF write from LTC2404 spec
   switch (MUX)                 // Note: PIC SPI shifts out mux select bits
   {                            // automatically after this instruction.
                          // Note: a/d mux clocks (mux)data in on rising edge
      case 0:
         SSPBUF = 0b00001000;
         break;

      case 1:
         SSPBUF = 0b00001001;
         break;

      case 2:
         SSPBUF = 0b00001010;
         break;

      case 3:
         SSPBUF = 0b00001011;
         break;

      case 4:
         SSPBUF = 0b00001100;
         break;

      case 5:
         SSPBUF = 0b00001101;
         break;

      case 6:
         SSPBUF = 0b00001110;
         break;

      case 7:
         SSPBUF = 0b00001111;
         break;

   }

   DelayMs(1);       // Wait 1ms,but could have bit tested sspstat.
   Temp_Byte = SSPBUF;  // Flush the garbage that came in from previous out.
   RC2 = 0;             // A/D chip select low to read A/D
                        // Note with chip sel low, the SDO bit is driven low to
                        // indicate the conversion is complete.  could have also
                        // bit tested the sdo to tell when conv was complete, then
                        // wouldn't have needed any delay.  (Can't rock the clock if
                        // your testing the bit, else conv is lost.)
                        // Later, remove and set flag to come back and get answer when SDO is low.
   DelayMs(160);        // Wait>133ms(US)>160ms(euro) for convrsn to be completed
                        // could have made a/d chip sel low, and read in pic sdi, then
                        // if low, the conversion was completed, if high, not
                        // this is an extra delay to be removed
                        // it doesn't hurt to have extras
   SSPBUF = 0b00001111; // Garbage send to start clocking in a/d data
   DelayMs(1);          // Wait for the SPI to finish clocking first
                        // data byte. as dummy out is clocked out, the
                        // real data is clocked in.
   Temp_Byte1 = SSPBUF; // Get real number BYTE 1 (bits 31-24)
                        // &clears PIC SPI input full flag (else pic spi barfs)
   SSPBUF = 0b00001111; // Garbage send to start clocking
   DelayMs(1);          // Wait a wee bit
   Temp_Byte2 = SSPBUF; // Get real number BYTE 2 (bit23-16)
   SSPBUF = 0b00001111; // Garbage send to start clocking in
   DelayMs(1);          // Wait a wee bit
   Temp_Byte3 = SSPBUF; // Get real number BYTE 3 (bit 15-8)
   SSPBUF = 0b00001111; // Garbage send to start clocking in
   DelayMs(1);          // Wait a wee bit
   Temp_Byte4 = SSPBUF; // Get real number BYTE 4 (bit 7-0)


   // Yo mama, the conversion starts again after 32 bits are clocked in
   // set the next mux port now before we leave the routine, if we get
   // that done in the next 1 ms, then the a/d self calibration is not
   // affected
   //
   // Get each individual bit from the 4 bytes from the SPI device
   //
   //!*********** NOW, Convert bytes into a value for use *****************************
   //
   // Get FIRST byte
   j = 1;
   Byte_Mask = 1;
   for (i = 0; i < 8; i++)
   {
      Temp_Byte = Temp_Byte1; // Get fresh byte
      Temp_Byte &= Byte_Mask; // Set the mask
      if (Temp_Byte & j)
      {
         SPI_Value |= Word_Mask;
      }
      j = j * 2;
      Byte_Mask <<= 1;       // Roll the mask left 1 bit
      Word_Mask <<= 1;        // Roll the mask left 1 bit
   }

   // Get SECOND byte
   j = 1;
   Byte_Mask = 1;
   for (i = 0; i < 8; i++)
   {
      Temp_Byte = Temp_Byte2; // Get fresh byte
      Temp_Byte &= Byte_Mask; // Set the mask
      if (Temp_Byte & j)
      {
         SPI_Value |= Word_Mask;
      }
      j = j * 2;
      Byte_Mask <<= 1;        // Roll the mask left 1 bit
      Word_Mask <<= 1;        // Roll the mask left 1 bit
   }


   // Get THIRD byte
   j = 1;
   Byte_Mask = 1;
   for (i = 0; i < 8; i++)
   {
      Temp_Byte = Temp_Byte3; // Get fresh byte
      Temp_Byte &= Byte_Mask; // Set the mask
      if (Temp_Byte & j)
      {
         SPI_Value |= Word_Mask;
      }
      j = j * 2;
      Byte_Mask <<= 1;       // Roll the mask left 1 bit
      Word_Mask <<= 1;       // Roll the mask left 1 bit
   }

   // Get FOURH byte
   j = 1;
   Byte_Mask = 1;
   for (i = 0; i < 8; i++)
   {
      Temp_Byte = Temp_Byte4; // Get fresh byte
      Temp_Byte &= Byte_Mask; // Set the mask
      if (Temp_Byte & j)
      {
         SPI_Value |= Word_Mask;
      }
      j = j * 2;
      Byte_Mask <<= 1;       // Roll the mask left 1 bit
      Word_Mask <<= 1;       // Roll the mask left 1 bit
   }
   // This is the real 32 bit number, but we only need bits 28-4 to make a usuable number
   // so do some bit shifting .

   // **** Add bit shift to get the final value! *********

   // Stick the input value where it should go
   // NOTE ! DO NO CHANGE VALUE ASSIGNMENT OFFSET.
   // This is because the LTech part puts the current read into the last MUX selection
   switch (MUX)
   {
      case 0:
         SPI_Val7 = SPI_Value;
         break;

      case 1:
         SPI_Val0 = SPI_Value;
         break;

      case 2:
         SPI_Val1 = SPI_Value;
         break;

      case 3:
         SPI_Val2 = SPI_Value;
         break;

      case 4:
         SPI_Val3 = SPI_Value;
         break;

      case 5:
         SPI_Val4 = SPI_Value;
         break;

      case 6:
         SPI_Val5 = SPI_Value;
         break;

      case 7:
         SPI_Val6 = SPI_Value;
         break;
   }

} // End Read_Spi

PARSIC

selamlar hi-tech ile spi kullanmaya çalışıyorum ama sürekli hata veriyor mesela bf bitini tanımıyor neden olabilir acaba..
********************************************************************HI-TECH*******************************************************************