AnsweredAssumed Answered

adas code

Question asked by Loris_85 on Mar 19, 2014
Latest reply on Mar 28, 2014 by Loris_85

I've tried to send data to ADAS without much luck. Here my code, I remember I've no signal on OSC1 OSC2 pin of oscillator.

In this code, I don't use any adas function but only to make simpler the code. I send a registry read command and then a dummy byte. No answer..I use  MICROCHIP dsPIC30F3013.

 

#include <xc.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <p30fxxxx.h>

#include <spi.h>

#define FCY 5000000UL

#include "libpic30.h"

 

 

#define LED LATBbits.LATB9

#define LED1 LATFbits.LATF4

#define BT_RESET LATBbits.LATB1

 

 

#define ADAS_CHIP_SELECT LATBbits.LATB2 //Chip Select on RB2 pin

#define ADAS_SDI LATFbits.LATF3 //SDI on RF3 pin

#define ADAS_SDO PORTFbits.RF2 //SDO on RF2 pin

#define ADAS_DRDY PORTDbits.RD8 //   /DRDY on RD8 pin

#define ADAS_PD LATBbits.LATB7 //   /DRDY on RD8 pin

#define ADAS_RESET LATBbits.LATB8 //   /DRDY on RD8 pin

 

 

// FOSC

#pragma config FOSFPR = HS              // Oscillator (HS)

#pragma config FCKSMEN = CSW_FSCM_OFF   // Clock Switching and Monitor (Sw Disabled, Mon Disabled)

 

 

// FWDT

#pragma config FWPSB = WDTPSB_16        // WDT Prescaler B (1:16)

#pragma config FWPSA = WDTPSA_512       // WDT Prescaler A (1:512)

#pragma config WDT = WDT_OFF            // Watchdog Timer (Disabled)

 

 

// FBORPOR

#pragma config FPWRT = PWRT_OFF         // POR Timer Value (Timer Disabled)

#pragma config BODENV = BORV27          // Brown Out Voltage (2.7V)

#pragma config BOREN = PBOR_OFF         // PBOR Enable (Disabled)

#pragma config MCLRE = MCLR_EN         // Master Clear Enable

 

 

// FGS

#pragma config GWRP = GWRP_OFF          // General Code Segment Write Protect (Disabled)

#pragma config GCP = CODE_PROT_OFF      // General Segment Code Protection (Disabled)

 

 

// FICD

#pragma config ICS = ICS_PGD            // Comm Channel Select (Use PGC/EMUC and PGD/EMUD)

 

 

 

 

void init_periferiche();

void init_SPI();

//void init_ECG();

//void write_SPI (unsigned char* data_transmit,unsigned char size);

 

 

unsigned int read[4] = {0,0,0,0};

unsigned int SPI_counter = 0;

 

 

int main(void) {

 

 

/*unsigned char PACEAMPTH[4] = {0,0,0,0};

 

 

PACEAMPTH[0] = 0x07;

PACEAMPTH[1] = 0x00;

PACEAMPTH[2] = 0x00;

PACEAMPTH[3] = 0x00;

*/

 

 

init_periferiche();

init_SPI();

 

 

LED = 1;

BT_RESET = 1;

 

 

__delay_ms(50);

 

 

SPI1STATbits.SPIEN = 0b1; // start SPI

 

 

while(1)

  {

 

 

ADAS_CHIP_SELECT = 0;

 

 

while(SPI1STATbits.SPITBF);//txB libero? Scrivo

SPI1BUF = 0x07;

while(SPI1STATbits.SPITBF);//txB libero? Scrivo

SPI1BUF = 0x00;

while(SPI1STATbits.SPITBF);//txB libero? Scrivo

SPI1BUF = 0x00;

while(SPI1STATbits.SPITBF);//txB libero? Scrivo

SPI1BUF = 0x00;

__delay_us(20);

 

 

ADAS_CHIP_SELECT = 1;

 

 

ADAS_CHIP_SELECT = 0;

 

 

while(SPI1STATbits.SPITBF);//txB libero? Scrivo

SPI1BUF = 0x00;

while(SPI1STATbits.SPITBF);//txB libero? Scrivo

SPI1BUF = 0x00;

while(SPI1STATbits.SPITBF);//txB libero? Scrivo

SPI1BUF = 0x00;

while(SPI1STATbits.SPITBF);//txB libero? Scrivo

SPI1BUF = 0x00;

ReadSPI1(); //READ SPI1BUF TO CLEAR FLAGS

__delay_us(20); // to adjust time window of SCLK and CS (correct when change SCLK)

 

 

ADAS_CHIP_SELECT = 1;

      } // while(1)

return(0);

} // MAIN

 

 

void init_periferiche()

 

 

{

   TRISBbits.TRISB1=0; // BT_RESET

   TRISBbits.TRISB2 = 0; // Chip Select an output on RB2 TRISBbits.TRISB6=1; // BUFFER_ALIM IN ANALOG

   TRISBbits.TRISB7=0; // /PD OUT

   TRISBbits.TRISB8=0; // /RESET OUT

   TRISBbits.TRISB9=0; // LED OUT

   TRISFbits.TRISF2 = 1; //SDO an input on RF2 pin

   TRISFbits.TRISF3 = 0; //SDI an output on RF3 pin

   TRISFbits.TRISF4 = 0; // LED1 OUT

   TRISFbits.TRISF6 = 0; //SCK OUT on RF6 pin

   TRISDbits.TRISD8 = 1; //  /DRDY an input on RD8 pin

 

 

   ADAS_PD = 1; // /PD

   ADAS_RESET = 1; // /RESET

   ADAS_CHIP_SELECT = 1; //inizializzo sensore disattivato

  

   ADPCFG = 0xFFBF; // disable AN fucntions, on portB RB6 analog signal in

}

 

 

void init_SPI()

 

 

{

   

    // IPC0bits.SPI1IP = 1; 

    //IEC0bits.SPI1IE = 0b0; // interrupt request enabled

    //IFS0bits.SPI1IF = 0b0; // interrupt request occurred

 

 

    SPI1STATbits.SPIEN = 0b0; // disabilito il modulo, in fase di config, configuro SCKx, SDOx, SDIx e SSx come porte seriali

    SPI1STATbits.SPISIDL = 0b0; // Continue module op in IDLE mode

    SPI1STATbits.SPIROV = 0b0; // No overflow

 

 

    SPI1CONbits.MSTEN = 0b1; // Master mode enabled

    SPI1CONbits.FRMEN = 0b0; // Framed SPI disabled

    SPI1CONbits.SPIFSD = 0b0; // Sync pulse direction output disabled

    SPI1CONbits.DISSDO = 0b0; // SDO pin controlled by the module

    SPI1CONbits.MODE16 = 0b0; // communication is byte-wide

    SPI1CONbits.SSEN = 0b0; // pin controlled by the  port function

 

 

    SPI1CONbits.SMP = 0b1; // sample phase: input data sampled at the end of data output time

    SPI1CONbits.CKE = 0b0; // SDO change on transition from idle CK state to active CK state

    SPI1CONbits.CKP = 0b1; //clockP

    SPI1CONbits.SPRE = 0b010; // 6:1

    SPI1CONbits.PPRE = 0b11; //  1:1 833 k

 

 

 

}

Outcomes