Home>

I am trying to implement SPI and Uart in the interrupt routine of Timer0 interrupt (every 200uS) by using MCC with PIC microcomputer (PIC18F23k22), but when I put SPI in the interrupt routine, it doesn't work. SPI works if it is in the main statement.
main() is the following description. ――
SYSTEM_Initialize();
TMR0_SetInterruptHandler(inttmr0);
TMR0_StopTimer();
INTERRUPT_GlobalInterruptEnable();
INTERRUPT_PeripheralInterruptEnable();
The following is described in the inttmr0 interrupt routine.
SPI1_WriteByte(0xC0);
while(!SSP1STATbits.BF);
var = SSP1BUF;
EUSART2_Write(0x38);

With inttmr0, only EUSART2_Write will work, but if SPI_WriteByte is entered, EUSART2_Write will also not work. Uart operation is confirmed by logic analyzer and Tera Term.
Since SPI does not use interrupts, I masked SPI1 related interrupts at the beginning of inttmr0, but the situation remains the same.
PIE1bits.SSP1IE = 0;
PIE2bits.BCL1IE = 0;
PIR1bits.SSP1IF = 0;

We would appreciate it if you could teach us about SPI timer interrupt using MCC with PIC microcomputer.

Specifically, the main source is attached.

/**
  Generated Main Source File
  Company:
    Microchip Technology Inc.
  File Name:
    main.c
  Summary:
    This is the main file generated using PIC10/PIC12/PIC16/PIC18 MCUs
  Description:
    This header file provides implementations for driver APIs for all modules selected in the GUI.
    Generation Information:
        Product Revision: PIC10/PIC12/PIC16/PIC18 MCUs-1.81.4
        Device: PIC18F23K22
        Driver Version: 2.00
*/
/*
    (c) 2018 Microchip Technology Inc. and its subsidiaries.
    Subject to your compliance with these terms, you may use Microchip software and any
    derivatives exclusively with Microchip products.It is your responsibility to comply with third party
    license terms applicable to your use of third party software (including open source software) that
    may accompany Microchip software.
    THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
    EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
    IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
    FOR A PARTICULAR PURPOSE.
    IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
    WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
    HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
    THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
    CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
    OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
    SOFTWARE.
*/
#include "mcc_generated_files/mcc.h"
#include "L_i2cLCD.h"
    unsigned char var;
    unsigned char aa[2];

uint8_t count=0;
void inttmr0(void)
{
#if 1
        LATCbits.LATC6 = 0;
        PIE1bits.SSP1IE = 0;
        PIE2bits.BCL1IE = 0;
        PIR1bits.SSP1IF = 0;
        SPI1_WriteByte(0xC0);
        while(!SSP1STATbits.BF);
        var = SSP1BUF;
#if 0
        SPI1_WriteByte(0x00);
        while(!SSP1STATbits.BF);
        var = SSP1BUF;
        SPI1_WriteByte(0x00);
        while(!SSP1STATbits.BF);
        aa[0] = SSP1BUF;
        SPI1_WriteByte(0x00);
        while(!SSP1STATbits.BF);
        aa[1] = SSP1BUF;
        LATCbits.LATC6 = 1;
#endif
#endif
#if 0 // OK
   //EUSART2_Write(0x81);// 0729
   EUSART2_Write(0x38);// 0729
#endif
#if 0 // OK
    EUSART2_Write(0x30+(count++));
   if (count==10)
       count=0;//printf("now");
#endif
}
/*
                         Main application
 */
void main(void)
{
    // Initialize the device
    SYSTEM_Initialize();
    TMR0_SetInterruptHandler(inttmr0);
    TMR0_StopTimer();
    //TMR0_StartTimer();
    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts
    // Use the following macros to:
    // Enable the Global Interrupts
    INTERRUPT_GlobalInterruptEnable();
    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();
    // Enable the Peripheral Interrupts
    INTERRUPT_PeripheralInterruptEnable();
    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();
#if 0
    char msgStart[] = "LCD Disp Test";
    LCD_int();// Initialize LCD
    LCD_posyx(0,0);// move the cursor to the lower row
    LCD_ROMstr(" iDS8688 sensing");// Add "interface" to the bottom row
    LCD_posyx(1,0);// move the cursor to the lower row
    //LCD_ROMstr("ABCDEFGH=");// Add "interface" to the bottom row
    LCD_dat('N');// "I2C" is displayed in the lower row
    LCD_dat('O');
    LCD_dat('W');
#endif
    TMR0_StartTimer();
    while (1)
    {
        // Add your application code
    }
}
/**
 End of File
*/