• AVR Freaks

PIC24 - SPI doesn't work with MCP3301 ADC

Author
user_gm
New Member
  • Total Posts : 1
  • Reward points : 0
  • Joined: 2020/10/07 06:28:58
  • Location: 0
  • Status: offline
2020/10/09 04:31:22 (permalink)
0

PIC24 - SPI doesn't work with MCP3301 ADC

Hi,
 
I have connected PIC24FJ64GB004 with MCP3301 ADC. The MCP3301 gives data out on oscilloscope, but SPI2BUF stays 0 in the programm. I'm unable to figure out the problem. Can anyone help?
 
//Connections:
//pic24, pin 15 SS2OUT /CS --Output pim modul pin 7
//pic24, pin 19 SCK2OUT clk --Output pim modul pin 5
//pic24, pin 14 SDI2 sdi --INPUT pim-modul pin 68
//pic24, pin 3 RC7 pwmout --output pim-modul pin 44
/*
 * File: main.c
 * Author: G.M.
 *
 * Created on 27. August 2020, 12:07
 */
// PIC24FJ64GB004 Configuration Bit Settings

#ifndef CONFIGBITS_H
#define CONFIGBITS_H

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

#endif /* CONFIGBITS_H */

// PIC24FJ64GB004 Configuration Bit Settings

// CONFIG4
#pragma config DSWDTPS = DSWDTPSF // DSWDT Postscale Select (1:2,147,483,648 (25.7 days))
#pragma config DSWDTOSC = LPRC // Deep Sleep Watchdog Timer Oscillator Select (DSWDT uses Low Power RC Oscillator (LPRC))
#pragma config RTCOSC = SOSC // RTCC Reference Oscillator Select (RTCC uses Secondary Oscillator (SOSC))
#pragma config DSBOREN = ON // Deep Sleep BOR Enable bit (BOR enabled in Deep Sleep)
#pragma config DSWDTEN = OFF // Deep Sleep Watchdog Timer (DSWDT disabled)

// CONFIG3
#pragma config WPFP = WPFP63 // Write Protection Flash Page Segment Boundary (Highest Page (same as page 42))
#pragma config SOSCSEL = SOSC // Secondary Oscillator Pin Mode Select (SOSC pins in Default (high drive-strength) Oscillator Mode)
#pragma config WUTSEL = LEG // Voltage Regulator Wake-up Time Select (Default regulator start-up time used)
#pragma config WPDIS = WPDIS // Segment Write Protection Disable (Segmented code protection disabled)
#pragma config WPCFG = WPCFGDIS // Write Protect Configuration Page Select (Last page and Flash Configuration words are unprotected)
#pragma config WPEND = WPENDMEM // Segment Write Protection End Page Select (Write Protect from WPFP to the last page of memory)

// CONFIG2
#pragma config POSCMOD = HS // Primary Oscillator Select (HS Oscillator mode selected)
#pragma config I2C1SEL = PRI // I2C1 Pin Select bit (Use default SCL1/SDA1 pins for I2C1 )
#pragma config IOL1WAY = ON // IOLOCK One-Way Set Enable (Once set, the IOLOCK bit cannot be cleared)
#pragma config OSCIOFNC = OFF // OSCO Pin Configuration (OSCO pin functions as clock output (CLKO))
#pragma config FCKSM = CSDCMD // Clock Switching and Fail-Safe Clock Monitor (Sw Disabled, Mon Disabled)
#pragma config FNOSC = PRI // Initial Oscillator Select (Primary Oscillator (XT, HS, EC))
#pragma config PLL96MHZ = ON // 96MHz PLL Startup Select (96 MHz PLL Startup is enabled automatically on start-up)
#pragma config PLLDIV = NODIV // USB 96 MHz PLL Prescaler Select (Oscillator input used directly (4 MHz input))
#pragma config IESO = ON // Internal External Switchover (IESO mode (Two-Speed Start-up) enabled)

// CONFIG1
#pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768)
#pragma config FWPSA = PR128 // WDT Prescaler (Prescaler ratio of 1:128)
#pragma config WINDIS = ON // Windowed WDT (Windowed Watchdog Timer enabled; FWDTEN must be 1)
#pragma config FWDTEN = OFF // Watchdog Timer (Watchdog Timer is disabled)
#pragma config ICS = PGx1 // Emulator Pin Placement Select bits (Emulator functions are shared with PGEC1/PGED1)
#pragma config GWRP = OFF // General Segment Write Protect (Writes to program memory are allowed)
#pragma config GCP = OFF // General Segment Code Protect (Code protection is disabled)
#pragma config JTAGEN = OFF // JTAG Port Enable (JTAG port is disabled)


#include <p24FJ64GB004.h>
#include "xc.h"


#define TMR1_PERIOD 0x4E20

volatile uint16_t timertick;

uint16_t ticks;

volatile uint16_t rdata;


void spi2read(uint16_t millis);


void __attribute__((__interrupt__,__auto_psv__)) _T1Interrupt(void)
 {
 timertick++;
 IFS0bits.T1IF = 0;
 return;
 }



void InitializeTimer4For_PWM(void)
{
 LATCbits.LATC7 = 0;
 TRISCbits.TRISC7 = 0;
 RPOR11bits.RP23R = 18;
 T4CONbits.TON = 0;
 T4CONbits.TCS = 0;
 T4CONbits.TGATE = 0;
 T4CONbits.TCKPS = 0b00;
 TMR4 = 0x00;
 PR4 = 0x7FFF;
 T4CONbits.TON = 1;
}

void Init_PWM(void)
{
OC1R=0x7FFF;
OC1RS=0x0000;
OC1CON1bits.OCTSEL = 0b010;
OC1CON1bits.OCM = 0b111;
OC1CON2bits.SYNCSEL=0b01110;
OC1CON2bits.OCTRIG=0;
IEC0bits.OC1IE = 0;
IEC1bits.T4IE = 0;
}

void SetDutyCycle_PWM(uint16_t DutyCycle)
{
 OC1RS = DutyCycle;
}


 void reset_clock(void)
 {
 ticks = 0;
 TMR1 = 0;
 PR1 = TMR1_PERIOD;
 T1CONbits.TCS = 0;
 IPC0bits.T1IP = 4;
 IFS0bits.T1IF = 0;
 IEC0bits.T1IE = 1;
 SRbits.IPL = 3;
 T1CONbits.TON = 1;
 }

 
 void spi2_init(){
    TRISBbits.TRISB15=0;
    TRISAbits.TRISA0=0;
    TRISBbits.TRISB14=1;
    TRISAbits.TRISA1=0;
    RPINR22bits.SDI2R = 14;
    RPOR2bits.RP5R=11;
    SPI2CON1bits.MSTEN=1;
    SPI2CON1bits.MODE16=1;
    SPI2STATbits.SPIROV=0;
    SPI2CON1bits.DISSDO=1;
    SPI2STATbits.SPISIDL=0;
    SPI2CON1bits.CKE=1;
    SPI2CON1bits.CKP=0;
    SPI2CON1bits.SMP = 0;
    SPI2CON1bits.SPRE = 7;
    
    SPI2STATbits.SPIEN=1;
     }


 void spi2read(uint16_t millis){
    
     int16_t intervall = 80;
    static uint16_t millis_prev = 0;
        
    if((uint16_t)(millis-millis_prev)>intervall){
                SPI2STATbits.SPIROV=0;
                PORTBbits.RB15=0;
                SPI2BUF=0x00;
                while( !SPI2STATbits.SPIRBF);
                rdata = SPI2BUF;
                PORTBbits.RB15=1;
        millis_prev=millis;
    }
}
 

int main() {
    uint16_t DutyCycle = 0x0001;
    InitializeTimer4For_PWM();
    Init_PWM();
    SetDutyCycle_PWM(DutyCycle);
    reset_clock();
    spi2_init();
    while(1)
    {
              
        spi2read(timertick);
        
        SetDutyCycle_PWM(rdata+1);
    }
    return (0);
    
}


#1

0 Replies Related Threads

    Jump to:
    © 2020 APG vNext Commercial Version 4.5