EmbDev.net

Forum: µC & Digital Electronics I2C init error with the msp430f5529


Author: Alex A. (Company: Andi&Alex) (alextbg)
Posted on:

Rate this post
0 useful
not useful
Hey there, I have tried to make a program which can give me the data 
from my sensor SI7006-20A with I2C. The problem is, that when I run my 
project, nothing happens. I also used an Logic Analyzer and I saw that 
nothing happened. I really don't know what I could change. Maybe someone 
could help me, thanks a lot!

#include "main.h"
#include "msp430f5529.h"

unsigned char add = 0x80; // serial number of the sensor
unsigned char RHMeasure = 0xE5;
unsigned char TEMpMeasure = 0xE0;

double temp, rh;
int help1, help2;



void initI2C(){
   UCB0CTL1 |= UCSWRST;
  UCB1CTL0 = (1<<UCMST)|(1<<UCMODE0)|(1<<UCMODE1)|(1<<UCSYNC);
  UCB1CTL1=(1<<UCSSEL1)|(1<<UCTR);
  UCB1BR0 = 200;                               
  UCB1BR1 = 0;  
UCB1I2CSA = (add>>1);
UCB1CTL1 &= ~(1<<UCSWRST);
}

void sendDataI2C(unsigned char data){
   UCB1CTL1|= (1<<UCTXSTT); 
   UCB1TXBUF = data;
   while(UCB1IFG&(1<<UCTXIFG)==0x00);
   UCB1CTL1 |= (1<<UCB1IFG);
}

unsigned char readDataI2C(){
  
  
  
}

unsigned int startRead(){
  unsigned int data=0;
    UCB1CTL1&=~(0x10);
    UCB1CTL1 |=(1<<UCTXSTT);
  //  while(UCB1CTL1 &(1<<UCTXSTT));
    if(UCB1CTL1 &(1<<UCTXSTT)){
      P1DIR |= BIT0;
      P1OUT |= BIT0;
    }else{
      P4DIR |= BIT7;
      P4OUT |= BIT7;
    }
   // while(UCB1STAT&(1<<UCBBUSY) > 0x00);
    while(UCB1IFG&(1<<UCRXIFG)==0x00);
    data = UCB1RXBUF*256;
    
    while(UCB1IFG&(1<<UCRXIFG)==0x00);
    data += UCB1RXBUF;
    
    
    UCB1CTL1 |= (1<<UCTXNACK);
    UCB1CTL1 |= (1<<UCTXSTP); 
    
    
    
}

void startWrite(unsigned char cmd){
  UCB1CTL1|=(1<<UCTXSTT)|(0x10);
  //UCB1CTL1 |= (1<<UCTXSTP); 
  UCB1TXBUF = cmd;
  while(UCB1IFG&(1<<UCTXIFG)==0x00);
  
  
   
}

int main(){
    WDTCTL = WDTPW + WDTHOLD;                 
    P4DIR |= BIT2;
    P4DIR |= BIT1;
    initI2C();
    while(1){
    
    
    startWrite(RHMeasure);
   
    /* help2 = startRead();
    rh = (help2*125)/65536-6;
    
    startWrite(TEMpMeasure);
    help1 = startRead();
    temp = (help1*175.72)/65536-46.85;
    */
    __delay_cycles(100);
   
    }
}

: Moved by Moderator
Author: neuer PIC Freund (Guest)
Posted on:

Rate this post
0 useful
not useful
Hi
UCB1CTL1=(1<<UCSSEL1)|(1<<UCTR);

translates to
UCB1CTL1=(1<<0x80)|(1<<0x10);

which leads to a warning.

Dont use avr-stylish shift operations. Use instead
UCB1CTL1 = UCSSEL1 | UCTR;

Take the compiler warnings serious.

Author: Alex A. (Company: Andi&Alex) (alextbg)
Posted on:

Rate this post
0 useful
not useful
Thanks for your answer, but unfortunately nothing changed. I still see 
nothing on my Logic Analyzer

Author: stefanus (Guest)
Posted on:

Rate this post
0 useful
not useful
The only functiopn that you call from main() is startWrite(). Can that 
work without a prior call to initI2C()?

Author: Lothar M. (lkmiller) (Moderator)
Posted on:

Rate this post
0 useful
not useful
Alex A. wrote:
> but unfortunately nothing changed.
Same warnings/errors after compiling?
Show the changed code pls!

stefanus wrote:
> The only functiopn that you call from main() is startWrite(). Can that
> work without a prior call to initI2C()?
Try once more... ;-)

: Edited by Moderator
Author: Alex A. (Company: Andi&Alex) (alextbg)
Posted on:

Rate this post
0 useful
not useful
I have the init above the while and I have just warnings of missing 
return statement but I ignored this because i just want to check the 
init. And it isn't working...
#include "main.h"
#include "msp430f5529.h"

unsigned char add = 0x80;
unsigned char RHMeasure = 0xE5;
unsigned char TEMpMeasure = 0xE0;

double temp, rh;
int help1, help2;



void initI2C(){
   UCB0CTL1 |= UCSWRST;
  UCB1CTL0 = UCMST | UCMODE0 | UCMODE1 | UCSYNC; //MasterMode   I2C Mode
  UCB1CTL1= UCSSEL1 | UCTR;
  UCB1BR0 = 200;                               //fSCL = SMCLK/(40+0*256)
  UCB1BR1 = 0;  
UCB1I2CSA = (add>>1);
UCB1CTL1 &= ~UCSWRST;
}

void sendDataI2C(unsigned char data){
   UCB1CTL1|= UCTXSTT; 
   UCB1TXBUF = data;
   while((UCB1IFG & UCTXIFG)==0x00);
   UCB1CTL1 |= UCB1IFG;
}

unsigned char readDataI2C(){
  
  
  
}

unsigned int startRead(){
  unsigned int data=0;
    UCB1CTL1 &=~ (0x10);
    UCB1CTL1 |= UCTXSTT;
  //  while(UCB1CTL1 & UCTXSTT);
    if(UCB1CTL1 & UCTXSTT){
      P1DIR |= BIT0;
      P1OUT |= BIT0;
    }else{
      P4DIR |= BIT7;
      P4OUT |= BIT7;
    }
   // while((UCB1STAT & UCBBUSY) > 0x00);
    while((UCB1IFG & UCRXIFG) == 0x00);
    data = UCB1RXBUF*256;
    //ACK automatisch vermutlich 
    
    while((UCB1IFG & UCRXIFG) == 0x00);
    data += UCB1RXBUF;
    
    
    UCB1CTL1 |= UCTXNACK;
    UCB1CTL1 |= UCTXSTP; 
    
    
    
}

void startWrite(unsigned char cmd){
  UCB1CTL1 |= UCTXSTT | (0x10);
  //UCB1CTL1 |= UCTXSTP; 
  UCB1TXBUF = cmd;
  while((UCB1IFG & UCTXIFG) == 0x00);
  
  
   
}

int main(){
    WDTCTL = WDTPW + WDTHOLD;
    P4DIR |= BIT2;
    P4DIR |= BIT1;

    

    while(1){

    initI2C();
    
    startWrite(RHMeasure);
   
    /* help2 = startRead();
    rh = (help2*125)/65536-6;
    
    startWrite(TEMpMeasure);
    help1 = startRead();
    temp = (help1*175.72)/65536-46.85;
    */
    __delay_cycles(100);
   
    }
}


Author: Clemens L. (c_l)
Posted on:

Rate this post
0 useful
not useful
Alex A. wrote:
>    UCB0CTL1 |= UCSWRST;

This resets the wrong module; use UCB1CTL1.

> UCB1CTL1= UCSSEL1 | UCTR;

This clears the reset bit, and the following baud rate setting is not 
applied.

> UCB1CTL1 |= UCTXSTT | (0x10);

The 0x10 is the UCTXNACK bit. What's it doing here?

> UCB1TXBUF = cmd;

You can do that only after you have checked that the buffer is free 
(TXIFG).

> P4DIR |= BIT2;
> P4DIR |= BIT1;

This sets these two pins as GPIO outputs. To connect these pins to the 
I²C module instead, set the bits in P4SEL.


I would recommend to begin with a working example like 
MSP430F55xx_uscib0_i2c_08.c.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig