• Resolved

MSP430F249-EP: MSP430F249-Ep SPI To Ms5607

Prodigy 40 points

Replies: 4

Views: 97

Part Number: MSP430F249-EP

Hi guys I need help for so spi communicating, 

I have ms5607 ,it's communicating spi or i2c, I use spi  but I can't do this. I have one example code for msp430f149 , I change a lot of word for 249ep but its can't communicating..

This is example code

#include<MSP430x14x.h> 
#include <stdio.h> 
#include <string.h> 
#include <math.h> 
 
 
#define TRUE 1 
#define FALSE 0 
 
 
typedef unsigned char uchar; 
typedef unsigned int uint;  
 
 
#define CMD_RESET    0x1E  // ADC reset command 
#define CMD_ADC_READ 0x00  // ADC read command 
#define CMD_ADC_CONV 0x40  // ADC conversion command 
#define CMD_ADC_D1   0x00  // ADC D1 conversion 
#define CMD_ADC_D2   0x10  // ADC D2 conversion 
#define CMD_ADC_256  0x00  // ADC OSR=256 
#define CMD_ADC_512  0x02  // ADC OSR=512 
#define CMD_ADC_1024 0x04  // ADC OSR=1024 
#define CMD_ADC_2048 0x06  // ADC OSR=2056 
#define CMD_ADC_4096 0x08  // ADC OSR=4096 
#define CMD_PROM_RD  0xA0  // Prom read command 
 
#define csb_lo()  {P4DIR=BIT5;P4OUT &=~BIT5;}  
#define csb_hi()  {P4DIR=BIT5;P4OUT |= BIT5;} 
 
 
void spi_send(uchar cmd); 
 
void cmd_reset(void); 
unsigned long cmd_adc(uchar cmd); 
unsigned int cmd_prom(uint coef_num); 
 
void Initsys(void); 
void SPIInit(void); 
void Init(void); 
 
void Putstring(uchar *ptr); 
int i_putc(char ch); 
void i_puts(char *str); 
void i_printf(double ff); 
 
//��ʱ 
void tickdelay(uint tick)    //delay(0)Ϊ10��ʱ������, 
{                            //����tickÿ����1����������һ����תָ���i=i+1,   
  uint y=0;                    //������ָ�ռ6��ʱ������ 
  for(y=0;y < tick;y=y+1); 
} 
 
void _delay_900us(void) 
{ 
  tickdelay(1195) ; 
  tickdelay(0) ; 
} 
 
void _delay_3ms(void) 
{ 
  tickdelay(4000) ; 
} 
 
void _delay_10ms(void) 
{ 
  tickdelay(13335) ; 
} 
 
void spi_send(uchar cmd) 
{ 
  TXBUF1=cmd;   
  while((UTCTL1 & 0x01)!= 0x01);  
} 
 
double D2,D1;  
double a,b,c,d,e,f,g,h; 
double T,m,n,l; 
double dT;  
uint x; 
double OFF,SENS,tt1; 
//long long OFF1,TCO,SENS1,TCS,P1,P2,P3; 
double P; 
double PP; 
//������ 
void main (void) 
{ 
  WDTCTL = WDTPW + WDTHOLD;           //�ؿ��Ź� 
  uchar *tishi1 = "temperature:"; 
  uchar *tishi2 = "pressure:"; 
  uchar *tishi3 = "\n"; 
  Initsys(); 
  cmd_reset();  
  a=cmd_prom(0); 
  b=cmd_prom(1); 
  c=cmd_prom(2); 
  d=cmd_prom(3); 
  e=cmd_prom(4); 
  f=cmd_prom(5); 
  g=cmd_prom(6); 
  h=cmd_prom(7); 
 
  while(TRUE)  
  { 
    SPIInit(); 
    D1=cmd_adc(CMD_ADC_D1 + CMD_ADC_2048);  
    D2=cmd_adc(CMD_ADC_D2 + CMD_ADC_2048);  
    dT=f; 
    m=g/10000.0; 
    dT=D2-(dT*256); 
    l=(dT*m); 
    n=(l/8388608.0); 
    T=2000+n*10000; 
    T=T/100; 
    /* 
    //*************************** 
    OFF1=c; 
    OFF1=OFF1*131072; 
    TCO=e; 
    TCO=(long long)(TCO*dT/64); 
    OFF=OFF1+TCO; 
    //*************************** 
    SENS1=b; 
    SENS1=SENS1*65536; 
    TCS=d; 
    TCS=(long long)(TCS*dT/128); 
    SENS=SENS1+TCS; 
    //*************************** 
    P1=(long long)(SENS/2097152); 
    P2=(long long)(D1*P1/32768); 
    P3=(long long)(OFF/32768); 
    P=(signed long)(P2-P3); 
    */ 
    tt1=(e/1024)*(dT/2048); 
    OFF=c*4+tt1; 
    SENS=b*65536+d*(dT/128); 
    P=(D1/32768)*(SENS/2097152); 
    P=P-OFF; 
    PP=P/100.0; 
    //*************************** 
    Init(); 
    Putstring(tishi1); 
    i_printf(T); 
    //Putstring(tishi3); 
    Putstring(tishi2); 
    i_printf(PP); 
    Putstring(tishi3);     
  } 
} 
 
//ϵͳ��ʼ��   
void Initsys(void) 
{ 
  unsigned int i; 
   
  BCSCTL1 &= ~XT2OFF;            //��XT2���� 
  do  
  { 
     IFG1 &= ~OFIFG;             //�������ʧЧ��־ 
     for(i = 0xff; i > 0; i--);  //��ʱ���ȴ�XT2�� 
   } 
   while ((IFG1 & OFIFG) != 0);    //�ж�XT2�Ƿ����� 
    
   BCSCTL2 |= SELM_2;            //ѡ��MCLKΪXT2 
   SPIInit(); 
}  
 
//SPI��ʼ�� 
void SPIInit(void) 
{ 
  UCTL1 |= SWRST;                          //SWRST���USART���� 
 
  ME2 &= ~UTXE1; 
  ME2 &= ~URXE1; 
  UTCTL1 &= ~SSEL0;  
  P3SEL = 0x00; 
 
   
  UCTL1 |= CHAR + SYNC + MM;                       //SPI��ģʽ��8Ϊ���� 
  UTCTL1 |= STC + SSEL0 ;             //ѡ��ACLK(32KHZ����Ϊʱ��Դ��3��SPIģʽ (CKPH,CKPL)=10 
  UTCTL1 |=CKPL; 
  U1BR0=0X02; 
  U1BR1=0X00;                                   //�����ʷ�Ƶ����Ϊ2 
  UMCTL1 = 0x00;                                  //SPIģʽ�±������� 
  ME2 |= USPIE1;                                   //SPI0ģ������ 
 
  P5SEL = 0x0E;                                  //P5.1~P5.3Ϊģ�鹦�� 
  UCTL1 &= ~SWRST;   
  //P5DIR |= 0x0A;                                  //P5.1(SIMO)��P5.3(UCLK)��Ϊ���ģʽ��P5.2(SOMI)Ϊ����ģʽ//P5.0~P5.3Ϊģ�鹦��,P5.1(SIMO)��P5.3(UCLK1)��P5.4��DACS1����Ϊ���ģʽ 
  P4DIR |= BIT5; 
  P4OUT |= BIT5;                                  //CS=1,��ѡ��AD 
} 
 
//ϵͳ��ʼ�� 
void Init(void) 
{ 
  U1CTL|=SWRST;//����� 
   
  ME2 &= ~USPIE1; 
  U1CTL&=~SYNC; //ѡ��UARTģʽ 
  U1CTL&=~MM; 
  UTCTL1 &=~CKPL; 
  UTCTL1 &= ~SSEL0; 
  UTCTL1 &= ~STC;  
  P5SEL = 0x00; 
   
  P3SEL |= 0xc0;            //P3.6��P3.7�ֱ�����ΪUSART1�ķ��ͺͽ��� 
  ME2 |= UTXE1 + URXE1;        //ʹ��USART1 TXD/RXD 
  UCTL1 |= CHAR;            //8Ϊ����λ 
  UTCTL1 |= SSEL0;          //UCLK=ACLK 
  //���ò�����Ϊ2400 
  UBR01 = 0x0D;      //�������� 
  UBR11 = 0x00; 
  UMCTL1 = 0x6B;     //С�����ֵ���   
  UCTL1 &= ~SWRST;   //��ʼ��USART1״̬ 
  //IE2 |= URXIE1;     //ʹ��USART1 RX�ж� 
 
} 
 
//******************************************************** 
//! @brief send reset sequence 
//! 
//! @return 0 
//******************************************************** 
void cmd_reset(void) 
{ 
  P4OUT &= ~BIT5; 
  tickdelay(1000); 
  spi_send(CMD_RESET); 
  _delay_3ms();  
  //_delay_3ms(); 
  P4OUT |= BIT5; 
  //tickdelay(1000); 
} 
//******************************************************** 
//! @brief preform adc conversion 
//! 
//! @return 24bit result 
//******************************************************** 
unsigned long cmd_adc(uchar cmd) 
{ 
  unsigned int ret; 
  unsigned long temp=0; 
  P4OUT &= ~BIT5; 
  tickdelay(1000); 
  spi_send(CMD_ADC_CONV+cmd);  
  tickdelay(14000); 
  P4OUT |= BIT5; 
  tickdelay(1000); 
  P4OUT &= ~BIT5; 
  tickdelay(1000); 
  spi_send(CMD_ADC_READ);  
  tickdelay(1000); 
  spi_send(0x00);  
  tickdelay(100); 
  ret=RXBUF1; 
  tickdelay(1000); 
  temp=65536*ret; 
  spi_send(0x00); 
  tickdelay(100); 
  ret=RXBUF1; 
  tickdelay(1000); 
  temp=temp+256*ret; 
  spi_send(0x00);  
  tickdelay(100); 
  ret=RXBUF1; 
  tickdelay(1000); 
  temp=temp+ret; 
  P4OUT |= BIT5; 
  //tickdelay(1000); 
  return temp; 
} 
//******************************************************** 
//! @brief Read calibration coefficients 
//! 
//! @return coefficient 
//******************************************************** 
 
unsigned int cmd_prom(uint coef_num) 
{ 
  unsigned int ret; 
  unsigned int rC=0; 
  P4OUT &= ~BIT5; 
  tickdelay(100); 
  spi_send(CMD_PROM_RD+coef_num*2);  
  tickdelay(100); 
  spi_send(0x00);  
  tickdelay(100); 
  ret=RXBUF1; 
  tickdelay(100); 
  rC=256*ret; 
  spi_send(0x00); 
  tickdelay(100); 
  ret=RXBUF1; 
  tickdelay(100); 
  rC=rC+ret; 
  P4OUT |= BIT5; 
  //tickdelay(1000); 
  return rC; 
} 
 
/*********************************** 
�������ƣ�putsrting 
��    ��:��PC�������ַ� 
��    ������ 
����ֵ  ���� 
************************************/ 
void Putstring(uchar *ptr) 
{ 
  while(*ptr !='\0') 
  { 
      while(!(IFG2 & UTXIFG1)); 
      TXBUF1 = *ptr++; 
   } 
} 
/*�ַ����ͺ���*/ 
int i_putc(char ch) 
{ 
 TXBUF1 = ch; 
 while(!(IFG2 & UTXIFG1));   
 return(1);            
} 
 
/* �ַ������ͺ��� */ 
void i_puts(char *str) 
{     
     uchar   i;       
     uchar   sz;     
     sz = strlen(( char *)str);  
     for(i=0; i < sz ; i++)  
      {  
        while(!i_putc(*(str + i))); 
      } 
} 
void i_printf(double ff) 
{ 
    char str_temp[20]; 
    sprintf(str_temp,"%lf",ff);  
    i_puts(*(&str_temp)); 
} 
 
 
 
  • > #include<MSP430x14x.h>

    This is telling the compiler that you have an F1-series device, which you don't -- yours is an F2-series. You should set the MCU type in the build settings and use:

    > #include <MSP430.h>  // Include the appropriate register definitions

    Unfortunately, when you do that you'll find many differences in the serial port configurations between the two series. You may do better starting with one of the Example suite at:

    http://dev.ti.com/tirex/explore/node?node=ADdWKD9xlN3BIRnrFHYtcg__IOGqZri__LATEST

     

     

  • In reply to Bruce McKenney47378:

    Hello,

    I agree with Bruce.  I would start with the correct MSP430F249 code examples for the SPI that he linked.   I would recommend first building and running them as they are, then customize them for your application.  

    Good luck!

    JD

    _____________________________________________________________________________


  • In reply to JD Crutchfield:

    Hİ GUYS 
    I change this code but now, i have last one problem.

    I can read spi bus line for fabrication calibration data ( A,b,c,d,e,f,g,h) but I cant reading ADC data ( D1-D2) İts my code and simple working codes



    My code


    #include <msp430.h> #define TRUE 1 #define FALSE 0 typedef unsigned char uchar; typedef unsigned int uint; #define aktif() {P5DIR=BIT0;P5OUT &=~BIT0;} #define pasif() {P5DIR=BIT0;P5OUT |= BIT0;} #define CMD_RESET 0x1E // ADC reset command #define CMD_ADC_READ 0x00 // ADC read command #define CMD_ADC_CONV 0x40 // ADC conversion command #define CMD_ADC_D1 0x00 // ADC D1 conversion #define CMD_ADC_D2 0x10 // ADC D2 conversion #define CMD_ADC_256 0x00 // ADC OSR=256 #define CMD_ADC_512 0x02 // ADC OSR=512 #define CMD_ADC_1024 0x04 // ADC OSR=1024 #define CMD_ADC_2048 0x06 // ADC OSR=2056 #define CMD_ADC_4096 0x08 // ADC OSR=4096 #define CMD_PROM_RD 0xA0 // Prom read command char sicaklik[]={"Sicaklik:"}; char basinc[]={"Basinc:"}; double a,b,c,d,e,f,g,h; int i,x; double D2,D1; double T,m,n,l; double dT; double OFF,SENS,tt1; double P; double PP; int main(void) { volatile unsigned int i; spinit(); P5OUT |= BIT0; cmd_reset(); a=cmd_prom(0); b=cmd_prom(1); c=cmd_prom(2); d=cmd_prom(3); e=cmd_prom(4); f=cmd_prom(5); g=cmd_prom(6); h=cmd_prom(7); UC1IFG &=~UCB1RXIFG; __delay_cycles(9000); while(1) { spinit(); D1=cmd_adc(CMD_ADC_D1 + CMD_ADC_2048); __delay_cycles(100); D2=cmd_adc(CMD_ADC_D2 + CMD_ADC_2048); dT=f; m=g/10000.0; dT=D2-(dT*256); l=(dT*m); n=(l/8388608.0); T=2000+n*10000; T=T/100; tt1=(e/1024)*(dT/2048); OFF=c*4+tt1; SENS=b*65536+d*(dT/128); P=(D1/32768)*(SENS/2097152); P=P-OFF; PP=P/100.0; } } unsigned long cmd_adc(uchar cmd) { UC1IFG &=~UCB1RXIFG; unsigned int ret; unsigned long temp=0; spi_send(CMD_ADC_CONV+cmd); for(i=0;i < 8000;i=i+1); pasif(); aktif(); UC1IFG &=~UCB1RXIFG; spi_send(CMD_ADC_READ); spi_send(0x00); ret=UCB1RXBUF; temp=65536*ret; spi_send(0x00); ret=UCB1RXBUF; temp=temp+256*ret; spi_send(0x00); ret=UCB1RXBUF; temp=temp+ret; return temp; } unsigned int cmd_prom(uint coef_num) { unsigned int ret; unsigned int rC=0; spi_send(CMD_PROM_RD+coef_num*2); spi_send(0x00); ret=UCB1RXBUF; rC=256*ret; for(i=0;i < 100;i=i+1); spi_send(0x00); ret=UCB1RXBUF; for(i=0;i < 100;i=i+1); rC=rC+ret; //tickdelay(1000); UC1IFG &=~UCB1RXIFG; return rC; } void spi_send(char cmd) { UCB1TXBUF=cmd; __delay_cycles(10); } void cmd_reset(void) { int y=0; for(i=0;i < 100;i=i+1); spi_send(CMD_RESET); for(i=0;i < 4000;i=i+1); //_delay_3ms(); //tickdelay(1000); } void _delay(int tick) //delay(0)?10?????????, { //????tickÿ????1??????????h??????????i=i+1, int y=0; //??????????6????????? for(y=0;y < tick;y=y+1); } void _delay_900us(void) { for(i=0;i < 1195;i=i+1); } void _delay_3ms(void) { for(i=0;i < 4000;i=i+1); } void _delay_10ms(void) { for(i=0;i < 13335;i=i+1); } void spinit() { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer if (CALBC1_1MHZ==0xFF) // If calibration constant erased { while(1); // do not load, trap CPU!! } DCOCTL = 0; // Select lowest DCOx and MODx settings BCSCTL1 = CALBC1_1MHZ; // Set DCO DCOCTL = CALDCO_1MHZ; for(i=2100;i>0;i--); // Wait for DCO to stabilize. // P5OUT = 0x01; // Set slave reset - P3.5 P5SEL |= 0x0E; // P3.3,2 USCI_B0 option select P5DIR |= 0x01; // P3.0 output direction UCB1CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI master UCB1CTL1 |= UCSSEL_2; // SMCLK UCB1BR0 |= 0x02; // BRCLK = SMCLK/2 UCB1BR1 = 0; // UCB1CTL1 &= ~UCSWRST; // **Initialize USCI state machine** UC1IE |= UCB1RXIE; // Enable USCI1 RX interrupt P5OUT &= ~BIT0; }



    SAMPLE 1 for Msp430f1xx






    //MS5607???£?SPI????? 
    #include<MSP430x14x.h> 
    #include <stdio.h> 
    #include <string.h> 
    #include <math.h> 
     
     
    #define TRUE 1 
    #define FALSE 0 
     
     
    typedef unsigned char uchar; 
    typedef unsigned int uint;  
     
     
    #define CMD_RESET    0x1E  // ADC reset command 
    #define CMD_ADC_READ 0x00  // ADC read command 
    #define CMD_ADC_CONV 0x40  // ADC conversion command 
    #define CMD_ADC_D1   0x00  // ADC D1 conversion 
    #define CMD_ADC_D2   0x10  // ADC D2 conversion 
    #define CMD_ADC_256  0x00  // ADC OSR=256 
    #define CMD_ADC_512  0x02  // ADC OSR=512 
    #define CMD_ADC_1024 0x04  // ADC OSR=1024 
    #define CMD_ADC_2048 0x06  // ADC OSR=2056 
    #define CMD_ADC_4096 0x08  // ADC OSR=4096 
    #define CMD_PROM_RD  0xA0  // Prom read command 
     
    #define csb_lo()  {P4DIR=BIT5;P4OUT &=~BIT5;}  
    #define csb_hi()  {P4DIR=BIT5;P4OUT |= BIT5;} 
     
     
    void spi_send(uchar cmd); 
     
    void cmd_reset(void); 
    unsigned long cmd_adc(uchar cmd); 
    unsigned int cmd_prom(uint coef_num); 
     
    void Initsys(void); 
    void SPIInit(void); 
    void Init(void); 
     
    void Putstring(uchar *ptr); 
    int i_putc(char ch); 
    void i_puts(char *str); 
    void i_printf(double ff); 
     
    //??? 
    void tickdelay(uint tick)    //delay(0)?10?????????, 
    {                            //????tickÿ????1??????????h??????????i=i+1,   
      uint y=0;                    //??????????6????????? 
      for(y=0;y < tick;y=y+1); 
    } 
     
    void _delay_900us(void) 
    { 
      tickdelay(1195) ; 
      tickdelay(0) ; 
    } 
     
    void _delay_3ms(void) 
    { 
      tickdelay(4000) ; 
    } 
     
    void _delay_10ms(void) 
    { 
      tickdelay(13335) ; 
    } 
     
    void spi_send(uchar cmd) 
    { 
      TXBUF1=cmd;   
      while((UTCTL1 & 0x01)!= 0x01);  
    } 
     
    double D2,D1;  
    double a,b,c,d,e,f,g,h; 
    double T,m,n,l; 
    double dT;  
    uint x; 
    double OFF,SENS,tt1; 
    //long long OFF1,TCO,SENS1,TCS,P1,P2,P3; 
    double P; 
    double PP; 
    //?????? 
    void main (void) 
    { 
      WDTCTL = WDTPW + WDTHOLD;           //????Z? 
      uchar *tishi1 = "temperature:"; 
      uchar *tishi2 = "pressure:"; 
      uchar *tishi3 = "\n"; 
      Initsys(); 
      cmd_reset();  
      a=cmd_prom(0); 
      b=cmd_prom(1); 
      c=cmd_prom(2); 
      d=cmd_prom(3); 
      e=cmd_prom(4); 
      f=cmd_prom(5); 
      g=cmd_prom(6); 
      h=cmd_prom(7); 
     
      while(TRUE)  
      { 
        SPIInit(); 
        D1=cmd_adc(CMD_ADC_D1 + CMD_ADC_2048);  
        D2=cmd_adc(CMD_ADC_D2 + CMD_ADC_2048);  
        dT=f; 
        m=g/10000.0; 
        dT=D2-(dT*256); 
        l=(dT*m); 
        n=(l/8388608.0); 
        T=2000+n*10000; 
        T=T/100; 
        /* 
        //*************************** 
        OFF1=c; 
        OFF1=OFF1*131072; 
        TCO=e; 
        TCO=(long long)(TCO*dT/64); 
        OFF=OFF1+TCO; 
        //*************************** 
        SENS1=b; 
        SENS1=SENS1*65536; 
        TCS=d; 
        TCS=(long long)(TCS*dT/128); 
        SENS=SENS1+TCS; 
        //*************************** 
        P1=(long long)(SENS/2097152); 
        P2=(long long)(D1*P1/32768); 
        P3=(long long)(OFF/32768); 
        P=(signed long)(P2-P3); 
        */ 
        tt1=(e/1024)*(dT/2048); 
        OFF=c*4+tt1; 
        SENS=b*65536+d*(dT/128); 
        P=(D1/32768)*(SENS/2097152); 
        P=P-OFF; 
        PP=P/100.0; 
        //*************************** 
        Init(); 
        Putstring(tishi1); 
        i_printf(T); 
        //Putstring(tishi3); 
        Putstring(tishi2); 
        i_printf(PP); 
        Putstring(tishi3);     
      } 
    } 
     
    //????'??   
    void Initsys(void) 
    { 
      unsigned int i; 
       
      BCSCTL1 &= ~XT2OFF;            //????XT2?????? 
      do  
      { 
         IFG1 &= ~OFIFG;             //??????????????? 
         for(i = 0xff; i > 0; i--);  //????????XT2???? 
       } 
       while ((IFG1 & OFIFG) != 0);    //???XT2??????? 
        
       BCSCTL2 |= SELM_2;            //???MCLK?XT2 
       SPIInit(); 
    }  
     
    //SPI??'?? 
    void SPIInit(void) 
    { 
      UCTL1 |= SWRST;                          //SWRST?????USART???? 
     
      ME2 &= ~UTXE1; 
      ME2 &= ~URXE1; 
      UTCTL1 &= ~SSEL0;  
      P3SEL = 0x00; 
     
       
      UCTL1 |= CHAR + SYNC + MM;                       //SPI??g???8????? 
      UTCTL1 |= STC + SSEL0 ;             //???ACLK(32KHZ?????????????3??SPIg? (CKPH,CKPL)=10 
      UTCTL1 |=CKPL; 
      U1BR0=0X02; 
      U1BR1=0X00;                                   //?????????????2 
      UMCTL1 = 0x00;                                  //SPIg??±??????? 
      ME2 |= USPIE1;                                   //SPI0g?????? 
     
      P5SEL = 0x0E;                                  //P5.1~P5.3?g???? 
      UCTL1 &= ~SWRST;   
      //P5DIR |= 0x0A;                                  //P5.1(SIMO)??P5.3(UCLK)???????g???P5.2(SOMI)?????g?//P5.0~P5.3?g????,P5.1(SIMO)??P5.3(UCLK1)??P5.4??DACS1?????????g? 
      P4DIR |= BIT5; 
      P4OUT |= BIT5;                                  //CS=1,?????AD 
    } 
     
    //????'?? 
    void Init(void) 
    { 
      U1CTL|=SWRST;//??????? 
       
      ME2 &= ~USPIE1; 
      U1CTL&=~SYNC; //???UARTg? 
      U1CTL&=~MM; 
      UTCTL1 &=~CKPL; 
      UTCTL1 &= ~SSEL0; 
      UTCTL1 &= ~STC;  
      P5SEL = 0x00; 
       
      P3SEL |= 0xc0;            //P3.6??P3.7????????USART1?k??????? 
      ME2 |= UTXE1 + URXE1;        //'??USART1 TXD/RXD 
      UCTL1 |= CHAR;            //8?????? 
      UTCTL1 |= SSEL0;          //UCLK=ACLK 
      //???ò??????2400 
      UBR01 = 0x0D;      //???????? 
      UBR11 = 0x00; 
      UMCTL1 = 0x6B;     //??????????   
      UCTL1 &= ~SWRST;   //??'??USART1?? 
      //IE2 |= URXIE1;     //'??USART1 RX??? 
     
    } 
     
    //******************************************************** 
    //! @brief send reset sequence 
    //! 
    //! @return 0 
    //******************************************************** 
    void cmd_reset(void) 
    { 
      P4OUT &= ~BIT5; 
      tickdelay(1000); 
      spi_send(CMD_RESET); 
      _delay_3ms();  
      //_delay_3ms(); 
      P4OUT |= BIT5; 
      //tickdelay(1000); 
    } 
    //******************************************************** 
    //! @brief preform adc conversion 
    //! 
    //! @return 24bit result 
    //******************************************************** 
    unsigned long cmd_adc(uchar cmd) 
    { 
      unsigned int ret; 
      unsigned long temp=0; 
      P4OUT &= ~BIT5; 
      tickdelay(1000); 
      spi_send(CMD_ADC_CONV+cmd);  
      tickdelay(14000); 
      P4OUT |= BIT5; 
      tickdelay(1000); 
      P4OUT &= ~BIT5; 
      tickdelay(1000); 
      spi_send(CMD_ADC_READ);  
      tickdelay(1000); 
      spi_send(0x00);  
      tickdelay(100); 
      ret=RXBUF1; 
      tickdelay(1000); 
      temp=65536*ret; 
      spi_send(0x00); 
      tickdelay(100); 
      ret=RXBUF1; 
      tickdelay(1000); 
      temp=temp+256*ret; 
      spi_send(0x00);  
      tickdelay(100); 
      ret=RXBUF1; 
      tickdelay(1000); 
      temp=temp+ret; 
      P4OUT |= BIT5; 
      //tickdelay(1000); 
      return temp; 
    } 
    //******************************************************** 
    //! @brief Read calibration coefficients 
    //! 
    //! @return coefficient 
    //******************************************************** 
     
    unsigned int cmd_prom(uint coef_num) 
    { 
      unsigned int ret; 
      unsigned int rC=0; 
      P4OUT &= ~BIT5; 
      tickdelay(100); 
      spi_send(CMD_PROM_RD+coef_num*2);  
      tickdelay(100); 
      spi_send(0x00);  
      tickdelay(100); 
      ret=RXBUF1; 
      tickdelay(100); 
      rC=256*ret; 
      spi_send(0x00); 
      tickdelay(100); 
      ret=RXBUF1; 
      tickdelay(100); 
      rC=rC+ret; 
      P4OUT |= BIT5; 
      //tickdelay(1000); 
      return rC; 
    } 
     
    /*********************************** 
    ?????????putsrting 
    ??    ??:??PC????????? 
    ??    ?????? 
    ?????  ???? 
    ************************************/ 
    void Putstring(uchar *ptr) 
    { 
      while(*ptr !='\0') 
      { 
          while(!(IFG2 & UTXIFG1)); 
          TXBUF1 = *ptr++; 
       } 
    } 
    /*??????????*/ 
    int i_putc(char ch) 
    { 
     TXBUF1 = ch; 
     while(!(IFG2 & UTXIFG1));   
     return(1);            
    } 
     
    /* ???????????? */ 
    void i_puts(char *str) 
    {     
         uchar   i;       
         uchar   sz;     
         sz = strlen(( char *)str);  
         for(i=0; i < sz ; i++)  
          {  
            while(!i_putc(*(str + i))); 
          } 
    } 
    void i_printf(double ff) 
    { 
        char str_temp[20]; 
        sprintf(str_temp,"%lf",ff);  
        i_puts(*(&str_temp)); 
    } 
     
     
     

    Sample 2
    Atmel Atmega644p 
    


    #define TRUE 1 #define FALSE 0 #define F_CPU 4000000UL // 4 MHz XTAL #define CMD_RESET 0x1E // ADC reset command #define CMD_ADC_READ 0x00 // ADC read command #define CMD_ADC_CONV 0x40 // ADC conversion command #define CMD_ADC_D1 0x00 // ADC D1 conversion #define CMD_ADC_D2 0x10 // ADC D2 conversion #define CMD_ADC_256 0x00 // ADC OSR=256 #define CMD_ADC_512 0x02 // ADC OSR=512 #define CMD_ADC_1024 0x04 // ADC OSR=1024 #define CMD_ADC_2048 0x06 // ADC OSR=2056 #define CMD_ADC_4096 0x08 // ADC OSR=4096 #define CMD_PROM_RD 0xA0 // Prom read command #define csb_hi() (_SFR_BYTE(PORTA) &= ~_BV(3)) // setting CSB low #define csb_lo() (_SFR_BYTE(PORTA) |= _BV(3)) // setting CSB high //_____ I N C L U D E S #include <stdio.h> #include <util/delay.h> #include <avr/io.h> #include <math.h> //_____ D E F I N I T I O N S void spi_send(char cmd); void cmd_reset(void); unsigned long cmd_adc(char cmd); unsigned int cmd_prom(char coef_num); unsigned char crc4(unsigned int n_prom[]); //******************************************************** //! @brief send 8 bit using SPI hardware interface //! //! @return 0 //******************************************************** void spi_send(char cmd) { SPDR= cmd; // put the byte in the SPI hardware buffer and start sending while (bit_is_clear(SPSR, 7)); // wait that the data is sent } //******************************************************** //! @brief send reset sequence //! //! @return 0 //******************************************************** void cmd_reset(void) { csb_lo(); // pull CSB low to start the command spi_send(CMD_RESET); // send reset sequence _delay_ms(3); // wait for the reset sequence timing csb_hi(); // pull CSB high to finish the command } //******************************************************** //! @brief preform adc conversion //! //! @return 24bit result //******************************************************** unsigned long cmd_adc(char cmd) { unsigned int ret; unsigned long temp=0; csb_lo(); // pull CSB low spi_send(CMD_ADC_CONV+cmd); // send conversion command switch (cmd & 0x0f) // wait necessary conversion time { case CMD_ADC_256 : _delay_us(900); break; case CMD_ADC_512 : _delay_ms(3); break; case CMD_ADC_1024: _delay_ms(4); break; case CMD_ADC_2048: _delay_ms(6); break; case CMD_ADC_4096: _delay_ms(10); break; } csb_hi(); // pull CSB high to finish the conversion csb_lo(); // pull CSB low to start new command spi_send(CMD_ADC_READ); // send ADC read command spi_send(0x00); // send 0 to read 1st byte (MSB) ret=SPDR; temp=65536*ret; spi_send(0x00); // send 0 to read 2nd byte ret=SPDR; temp=temp+256*ret; spi_send(0x00); // send 0 to read 3rd byte (LSB) ret=SPDR; temp=temp+ret; csb_hi(); // pull CSB high to finish the read command return temp; } //******************************************************** //! @brief Read calibration coefficients //! //! @return coefficient //******************************************************** unsigned int cmd_prom(char coef_num) { unsigned int ret; unsigned int rC=0; csb_lo(); // pull CSB low spi_send(CMD_PROM_RD+coef_num*2); // send PROM READ command spi_send(0x00); // send 0 to read the MSB ret=SPDR; rC=256*ret; spi_send(0x00); // send 0 to read the LSB ret=SPDR; rC=rC+ret; csb_hi(); // pull CSB high return rC; } //******************************************************** //! @brief calculate the CRC code for details look into AN521 //! //! @return crc code //******************************************************** unsigned char crc4(unsigned int n_prom[]) { int cnt; // simple counter unsigned int n_rem; // crc reminder unsigned int crc_read; // original value of the crc unsigned char n_bit; n_rem = 0x00; crc_read=n_prom[7]; //save read CRC n_prom[7]=(0xFF00 & (n_prom[7])); //CRC byte is replaced by 0 for (cnt = 0; cnt < 16; cnt++) // operation is performed on bytes { // choose LSB or MSB if (cnt%2==1) n_rem ^= (unsigned short) ((n_prom[cnt>>1]) & 0x00FF); else n_rem ^= (unsigned short) (n_prom[cnt>>1]>>8); for (n_bit = 8; n_bit > 0; n_bit--) { if (n_rem & (0x8000)) { n_rem = (n_rem << 1) ^ 0x3000; } else { n_rem = (n_rem << 1); } } } n_rem= (0x000F & (n_rem >> 12)); // // final 4-bit reminder is CRC code n_prom[7]=crc_read; // restore the crc_read to its original place return (n_rem ^ 0x00); } //******************************************************** //! @brief main program //! //! @return 0 //******************************************************** int main (void) { unsigned long D1; // ADC value of the pressure conversion unsigned long D2; // ADC value of the temperature conversion unsigned int C[8]; // calibration coefficients double P; // compensated pressure value double T; // compensated temperature value double dT; // difference between actual and measured temperature double OFF; // offset at actual temperature double SENS; // sensitivity at actual temperature int i; unsigned char n_crc; // crc value of the prom DDRA = 0xFE; // prepare the port A DDRB = 0xBF; // SDO input DDRC = 0x00; // I2C pins as input DDRD = 0x82; // prepare the port D, RX out and TX out; PORTA= 0x10; PORTD= 0x20; //SPI settings:master, mode 0, fosc/4 SPCR=(1<<SPE)|(1<<MSTR); //alternative SPI settings: master, mode 3, fosc/4 //SPCR=(1<<SPE)|(1<<MSTR)|(1<<CPOL)|(1<<CPHA); cmd_reset(); // reset the module after powerup for (i=0;i<8;i++){ C[i]=cmd_prom(i);} // read calibration coefficients n_crc=crc4(C); while(TRUE) // loop without stopping { D1=cmd_adc(CMD_ADC_D1+CMD_ADC_256); // read uncompensated pressure D2=cmd_adc(CMD_ADC_D2+CMD_ADC_4096); // read uncompensated temperature // calcualte 1st order pressure and temperature (MS5607 1st order algorithm) dT=D2-C[5]*pow(2,8); OFF=C[2]*pow(2,17)+dT*C[4]/pow(2,6); SENS=C[1]*pow(2,16)+dT*C[3]/pow(2,7); T=(2000+(dT*C[6])/pow(2,23))/100; P=(((D1*SENS)/pow(2,21)-OFF)/pow(2,15))/100; // place to use P, T, put them on LCD, send them trough RS232 interface... } return 0; }

  • In reply to user6200751:

    > I cant reading  ADC data ( D1-D2)

    What result do you get?

    -------------------------

    > P5OUT |= BIT0;

    CSB is active low, so you should be clearing it ("&=~") before each transaction and setting it ("|=") after. I suggest you move this into the beginning/end of each of your "cmd_xxx" functions.

    -------------------------

      for(i=0;i < 8000;i=i+1);

    Are you sure these for-loop delays are effective? Doing a delay with a for-loop is notoriously inaccurate. I recommend __delay_cycles (or a timer) instead. The MS5607 really cares about the ADC->result delay, and doesn't give you any hints. 

    -------------------------

    >    UC1IE |= UCB1RXIE;                        // Enable USCI1 RX interrupt

    Remove this line. You aren't set up to take any interrupts, so as soon as you enable interrupts (GIE) your program will crash.