PIC18F45K80与QT1481的SPI通信

时间:2018-10-19 08:03:35

标签: pic spi

我已经安装了MPLAB X,并且正在尝试编写代码,以使用C语言从PIC18F45K80(未连接RX。无法将命令发送到MOSI,然后获得对MISO的响应)发送命令0x0F到QT1481。内部时钟应为1MHz。输出应发送0x0F,而QT1481应将0xF0发送回PIC18F45K80。我正在使用Docklight(串行终端软件)来查看是否可以收到响应。

简介 QT1481使用SPI或UART通信模式。它不能同时使用两者。 QT1481 在收到命令的接口上做出响应。 QT1481还包括一个Debug输出接口,该接口 可用于监视产品开发过程中的许多操作变量。 主机设备始终启动通信序列。 QT1481无法将数据data回至 主办。这是出于FMEA和IEC / EN60730的目的,以便主机始终完全控制 与QT1481通信。

在SPI模式下,QT1481是从机,因此即使命令后返回数据也由主机控制。

在UART模式下,QT1481仍然仅在命令后响应主机,但响应不受主机的控制。

来自主机的命令总是以QT1481的某种响应结尾。某些传输类型来自 主机或QT1481使用CRC校验字节来提供可靠的通信。 提供了一条用于握手传输的DRDY线。 QT1481的主机需要这样做,以确保 QT1481繁忙或尚未处理先前的命令时,不发送传输。在UART模式下 线路是双向的,如果主机繁忙,则QT1481可以使用它来暂停向主机的传输。 如果主机未遵循正确的DRDY时序,则可能会导致随机通信错误。 启动或重置通信: 复位后,或者由于噪声或接收顺序错误导致通信丢失时,主机应 重复等待不少于QT1481通信超时(110 ms±5 ms)的时间,并发送 0x0F(返回最后一个命令)命令,直到收到0x0F的补码(即0xF0)为止。然后, 主机可以从干净的开始恢复正常的运行模式通信。

  /*
     * File:   main.c
     * Author: Mateusz
     *
     * Created on 18 October 2018, 11:59
    */


    #include <xc.h>
    #include <PIC18F45K80.h>
    #include "mcc_generated_files/mcc.h"
    #include <stdio.h>
    #include <stdlib.h>

    #define SPI_CLK TRISCbits.TRISC3
    #define SPI_SDI TRISCbits.TRISC4
    #define SPI_SDO TRISCbits.TRISC5
    #define SPI_SS  TRISDbits.TRISD4

    unsigned char SPI_Recv[256];

    //Declaration Methods for EUSART!

    void WriteChar (unsigned char _data);
    void WriteText (unsigned char *text); 
    void Delay ();
    void Configuration_EUSART();
    void GetChar (); 

    //****************** Declaration SPI MASTER COnfiguration 
    *******************************

    void SPI_Init();
    void SPI_Write(unsigned char data);
    void SPI_Read();

    void main(void)
    {
        Configuration_EUSART();
        SPI_Init();
        while (1)
        {   
            WriteText("0x0F");
            SPI_Read();
            for (int x=0; x<=500;x++)
            {
                __delay_ms(100);
            }
        } 
    }

    void WriteChar(unsigned char _data)//Writing a character
    {
        if (PIR1bits.TXIF == 1)// if TXIF = 1 The EUSART Transmit Buffer 
       TXREG is empty and its ready to send the next data
        {
            while(!TXSTAbits.TRMT);
            TXREG = _data;
        }
    }   

    void WriteText (unsigned char *text) //Writing Text
    {
        for(int i=0; text[i]!='\0'; i++)
        {
            WriteChar(text[i]);
        }
    }

    void Configuration_EUSART()
    {

        //--------------------------------------------PIC18F13K50 Configuration USART------------------------------------------------//

        // Configuration Signal Tx & Rx
        BAUDCON1bits.CKTXP = 0;// 0 = NO - invert the polarity data and prevent the incorrect function in the PIC using the UART in the Transmision Data TX  
        BAUDCON1bits.DTRXP = 0;// 0 = NO - invert the polarity data and prevent the incorrect function in the PIC using the UART in the Receive Data RX
        // Oscillator Configuration
        //OSCCONbits.IRCF = 0b110; // internal Oscillator Selection IRCF <2:0> 8 Mhz
        //OSCCONbits.SCS = 0b00; // 0b00 = Prevent the modification and select de Oscillator FOSC in configuration bits
        //Configuration PIN TX and RX
        //ANSELHbits.ANS11 = 0; //Digital input buffer  RB5 (Rx) is enabled
        //ANSELHbits.ANS10 = 0; //Digital Output buffer  RB4 (SDA) is enabled
        TRISCbits.TRISC4 = 1; // RB5 (Rx) Input Mode
        TRISCbits.TRISC5 = 1; // RB7 (Tx) Input Mode, SPEN will reconfigured the pin RB7 as Output when is required

        //-------Baud Rate --------
        // BAUDCONbits.BRG16 = 1; //Times of 16 bits
        // TXSTAbits.BRGH = 1; // if BRHG is clear is (low) and if is set is (High) 
        // SPBRG = 25; // This Decimal Number (25) represent the Baud rate 115200 at 12 Mhz (see the datasheet)

        //Serial Port Configuration
        TXSTAbits.SYNC = 0; // Async Mode
        TXSTAbits.TXEN = 1; // Enable Transmission
        RCSTAbits.CREN = 1; // Enable reception bit Rx
        RCSTAbits.SPEN = 1; // Enable Serial Port
   }

// -------------------------------------- SPI CONFIG --------------------------------------------//
void SPI_Init(){
    //set tris bits for serial port pins
    SPI_CLK = 0; //output
    SPI_SDI = 1; //input
    SPI_SDO = 0; //output
    SPI_SS = 0; //output

    PIR1bits.SSPIF = 0; // Clear interrupt flag
    PIE1bits.SSPIE = 0; // Disable MSSP interrupt
    INTCONbits.PEIE = 0; // Disable perriferal interrupts


    SSPSTATbits.SMP = 0; //input samled at middle of interval
    SSPSTATbits.CKE = 0; //Data transmitted from low to high clock

    //Enable SSP, Master mode, clock Fosc/4
    SSPCON1 |= 0b00100000;

}

void SPI_Write(unsigned char data){
    unsigned char TempVar;
    TempVar = SSPBUF; // Clears BF
    PIR1bits.SSPIF = 0; // Clear interrupt flag
    SSPBUF = data; // write byte to SSPBUF register
    while(!PIR1bits.SSPIF) {}; // wait until bus cycle complete
        //return ( 0 ); // if WCOL bit is not set return non-negative#
}

void SPI_Read()
{
     WriteText(" Here2");
     PIR1bits.SSPIF = 0; // Clear interrupt flag

     for (int i=0; i<=256;i++)
     {   
         WriteText(" Here3");
         SPI_Recv[i] = SSPBUF; // Save the data in SSPBUFF
         WriteText(" Here4");
         while(!PIR1bits.SSPIF) {}; // wait until bus cycle complete
         WriteText(" Here5");

     }


     WriteText(" Here6");
     WriteText(SPI_Recv);
     WriteText(" Here7");

}

0 个答案:

没有答案