微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MAX6675与PIC18F45k22的UART数据传输问题

如何解决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 举报,一经查实,本站将立刻删除。