MAX6675与PIC18F45k22的UART数据传输问题

问题描述

我正在使用MPLAB XC8对pic18f45k22进行编程,以从2个热电偶读取数据。该代码显示了2个问题,一个是:在仿真过程中,传感器的数据被移位(发送到第二个地址) 例如:如果我有2个地址0x0D和0x0F,以及2个值100和95,而100应该是0x0D,而95应该是0x0F。但是100转到0x0F,95转到0x0D。 **第二个**在将代码上传到MCU后出现,这是因为MCU无法从传感器读取任何数据。 这是主要代码

 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){ //timer interrupt routine every 1 second
     //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); //right shift of the reading
   
  HMT_WriteVPN16(0x08000C,thercoup1); //send thermocouple1 data to 0x08000C
  HMT_WriteVPN16(0x08000E,thercoup2); //send thermocouple2 data to 0x08000E
    cnt = 0; //reset timer
    break; 
  }  
    
   }
     }

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

这是SPI模块的配置代码

 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);
} 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)