• AVR Freaks

Hot!MAX6675 with PIC18F45k22 Data transmission via UART Problem

Author
Ayman Morsy
New Member
  • Total Posts : 25
  • Reward points : 0
  • Joined: 2020/02/12 14:35:33
  • Location: 0
  • Status: offline
2020/09/28 17:15:57 (permalink)
0

MAX6675 with PIC18F45k22 Data transmission via UART Problem

I am using MPLAB XC8 to program pic18f45k22 to read data from 2 thermocouples. The code showed 2 problems, The first is that during simulation, the data from the sensor is shifted (sent to the second address) for Example: if I have 2 addresses 0x0D and 0x0F, and 2 values 100 and 95, and 100 should got to 0x0D and 95 to 0x0F. However 100 goes to 0x0F and 95 to 0x0D. **The second ** appears after uploading the code to the MCU, and it is that the MCU cannot Read any data from the sensors. this is the main code:
void main(void) { 
   
  //SPI configuration
   spiInit();
    ANSELB = 0x00; //PORTB bits as digital
   TRISB3 = 0; //GPIO as CS output
   TRISB4 = 0;
   TRISB5 = 0;
   LATBbits.LATB3 = 1;
   LATBbits.LATB4 = 1;
   LATBbits.LATB5 = 1;
   timer1init(); //initiate timer1
   uartinit(9600); //initiate UART
   __delay_ms(100);

   while(1){
       switch (cnt){
         //thermocouple code
           case 50:
        LATB3 = 0; //turn on CS
        thercoup1 = spiRead(0)<<8; //Read first byte
        thercoup1 |= spiRead(0); //add second byte with first one
        LATB3 = 1; //turn off CS1
        thercoup1 = (thercoup1>>4);
         //thercoup1 = (thercoup1>>3)*0.25; //another method for calculating thermocouple value
        
        LATB4 = 0; //turn on CS1
        thercoup2 = spiRead(0)<<8; //Read first byte
        thercoup2 |= spiRead(0); //add second byte with first one
        LATB4 = 1; //turn off CS1
        thercoup2 = (thercoup2>>4);
       
      HMT_WriteVPN16(0x08000C, thercoup1);
      HMT_WriteVPN16(0x08000E, thercoup2);
        cnt = 0;
        break;
   }
        
    }
     }

void __interrupt() ISR (void){
    timer();
    UART_Read();
    
}

 
And This is the configuration code of SPI module.
 
void spiInit(void)
{
    ANSELC = 0x00;
    SSP1STATbits.SMP = 0; //input Data is sampled at the middle
    SSP1STATbits.CKE = 0; //Transmission occurs from Idle to Active
    //Master mode Fosc/4
    SSP1CON1bits.SSPM0 = 0;
    SSP1CON1bits.SSPM1 = 0;
    SSP1CON1bits.SSPM2 = 0;
    SSP1CON1bits.SSPM3 = 0;
    //
    SSP1CON1bits.CKP = 1; //clock polarity is high
    SSP1CON1bits.SSPEN = 1;//Serial communication is Enabled
    SSP1CON1bits.SSPOV = 0;//Overflow is off
    SSP1CON1bits.WCOL = 0;//Write collision is off
    TRISCbits.RC5 = 1; //SDO is Disabled
    TRISCbits.RC4 = 1; //SDI is Enabled
    TRISCbits.RC3 = 0; //SCK is Enabled
    
}

unsigned short spiRead(unsigned char dat) //REad the received data
{
    SSPBUF= dat; //the data loaded on the SSPBUF are not important.
    while(!SSPSTATbits.BF); //save the received data from the slave.
    return(SSPBUF);
}

Attached Image(s)

#1

5 Replies Related Threads

    davea
    Super Member
    • Total Posts : 384
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: Tampa Bay FL USA
    • Status: online
    Re: MAX6675 with PIC18F45k22 Data transmission via UART Problem 2020/09/28 18:01:16 (permalink)
    0
    while(!SSPSTATbits.BF); //save the received data from the slave. no compile error ?
    SSP1STATbits.BF
    should be thercoup>>3
    there a better way to scale it..
    where is ++cnt
    still looking..
    post edited by davea - 2020/09/28 18:17:14
    #2
    ric
    Super Member
    • Total Posts : 28660
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: MAX6675 with PIC18F45k22 Data transmission via UART Problem 2020/09/28 18:22:11 (permalink)
    +2 (2)
    SSPSTATbits and SSP1STATbits are equivalent in this PIC.
    (Both are defined in the header file.)
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #3
    davea
    Super Member
    • Total Posts : 384
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: Tampa Bay FL USA
    • Status: online
    Re: MAX6675 with PIC18F45k22 Data transmission via UART Problem 2020/09/28 18:34:10 (permalink)
    0
    ric
    SSPSTATbits and SSP1STATbits are equivalent in this PIC.
    (Both are defined in the header file.)
     

    did not know as there are 2 MSSP
    it looks like cnt is the/one of the problems never gets to 50
    #4
    Ayman Morsy
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2020/02/12 14:35:33
    • Location: 0
    • Status: offline
    Re: MAX6675 with PIC18F45k22 Data transmission via UART Problem 2020/09/29 02:35:11 (permalink)
    0
     
     
    davea
    did not know as there are 2 MSSP
    it looks like cnt is the/one of the problems never gets to 50

    cnt is in the timer code 



    unsigned int cnt; //count variable for timer1
    void timer1init(void){
    TMR1ON = 1;
    TMR1IE = 1;
    T1RD16 = 1;
    T1SYNC = 0;
    T1SOSCEN = 0;
    T1CKPS0 = 0;
    T1CKPS1 = 0;
    TMR1CS0 = 0;
    TMR1CS1 = 0;
    TMR1GE = 0;
    TMR1H = 0x3C;
    TMR1L = 0xB0;
    GIE = 1;
    PEIE = 1;
    }
    void timer(void){
    if (TMR1IF){
    cnt++;
    TMR1H = 0x3C;
    TMR1L = 0xB0;
    TMR1IF = 0;
    }
    }

     
     
    #5
    Ayman Morsy
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2020/02/12 14:35:33
    • Location: 0
    • Status: offline
    Re: MAX6675 with PIC18F45k22 Data transmission via UART Problem 2020/09/30 02:28:18 (permalink)
    0
    Should I turn off any other module during configuring SPI? 
    #6
    Jump to:
    © 2020 APG vNext Commercial Version 4.5