I2C init error with the msp430f5529

OP #5866620
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!

1
#include "main.h"
2
#include "msp430f5529.h"
3

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

8
double temp, rh;
9
int help1, help2;
10

11

12

13
void initI2C(){
14
   UCB0CTL1 |= UCSWRST;
15
  UCB1CTL0 = (1<<UCMST)|(1<<UCMODE0)|(1<<UCMODE1)|(1<<UCSYNC);
16
  UCB1CTL1=(1<<UCSSEL1)|(1<<UCTR);
17
  UCB1BR0 = 200;                               
18
  UCB1BR1 = 0;  
19
UCB1I2CSA = (add>>1);
20
UCB1CTL1 &= ~(1<<UCSWRST);
21
}
22

23
void sendDataI2C(unsigned char data){
24
   UCB1CTL1|= (1<<UCTXSTT); 
25
   UCB1TXBUF = data;
26
   while(UCB1IFG&(1<<UCTXIFG)==0x00);
27
   UCB1CTL1 |= (1<<UCB1IFG);
28
}
29

30
unsigned char readDataI2C(){
31
  
32
  
33
  
34
}
35

36
unsigned int startRead(){
37
  unsigned int data=0;
38
    UCB1CTL1&=~(0x10);
39
    UCB1CTL1 |=(1<<UCTXSTT);
40
  //  while(UCB1CTL1 &(1<<UCTXSTT));
41
    if(UCB1CTL1 &(1<<UCTXSTT)){
42
      P1DIR |= BIT0;
43
      P1OUT |= BIT0;
44
    }else{
45
      P4DIR |= BIT7;
46
      P4OUT |= BIT7;
47
    }
48
   // while(UCB1STAT&(1<<UCBBUSY) > 0x00);
49
    while(UCB1IFG&(1<<UCRXIFG)==0x00);
50
    data = UCB1RXBUF*256;
51
    
52
    while(UCB1IFG&(1<<UCRXIFG)==0x00);
53
    data += UCB1RXBUF;
54
    
55
    
56
    UCB1CTL1 |= (1<<UCTXNACK);
57
    UCB1CTL1 |= (1<<UCTXSTP); 
58
    
59
    
60
    
61
}
62

63
void startWrite(unsigned char cmd){
64
  UCB1CTL1|=(1<<UCTXSTT)|(0x10);
65
  //UCB1CTL1 |= (1<<UCTXSTP); 
66
  UCB1TXBUF = cmd;
67
  while(UCB1IFG&(1<<UCTXIFG)==0x00);
68
  
69
  
70
   
71
}
72

73
int main(){
74
    WDTCTL = WDTPW + WDTHOLD;                 
75
    P4DIR |= BIT2;
76
    P4DIR |= BIT1;
77
    initI2C();
78
    while(1){
79
    
80
    
81
    startWrite(RHMeasure);
82
   
83
    /* help2 = startRead();
84
    rh = (help2*125)/65536-6;
85
    
86
    startWrite(TEMpMeasure);
87
    help1 = startRead();
88
    temp = (help1*175.72)/65536-46.85;
89
    */
90
    __delay_cycles(100);
91
   
92
    }
93
}
Guest #5866634
Hi
1
UCB1CTL1=(1<<UCSSEL1)|(1<<UCTR);

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

which leads to a warning.

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

Take the compiler warnings serious.
Guest #5866939
The only functiopn that you call from main() is startWrite(). Can that 
work without a prior call to initI2C()?
OP #5867008
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...
1
#include "main.h"
2
#include "msp430f5529.h"
3

4
unsigned char add = 0x80;
5
unsigned char RHMeasure = 0xE5;
6
unsigned char TEMpMeasure = 0xE0;
7

8
double temp, rh;
9
int help1, help2;
10

11

12

13
void initI2C(){
14
   UCB0CTL1 |= UCSWRST;
15
  UCB1CTL0 = UCMST | UCMODE0 | UCMODE1 | UCSYNC; //MasterMode   I2C Mode
16
  UCB1CTL1= UCSSEL1 | UCTR;
17
  UCB1BR0 = 200;                               //fSCL = SMCLK/(40+0*256)
18
  UCB1BR1 = 0;  
19
UCB1I2CSA = (add>>1);
20
UCB1CTL1 &= ~UCSWRST;
21
}
22

23
void sendDataI2C(unsigned char data){
24
   UCB1CTL1|= UCTXSTT; 
25
   UCB1TXBUF = data;
26
   while((UCB1IFG & UCTXIFG)==0x00);
27
   UCB1CTL1 |= UCB1IFG;
28
}
29

30
unsigned char readDataI2C(){
31
  
32
  
33
  
34
}
35

36
unsigned int startRead(){
37
  unsigned int data=0;
38
    UCB1CTL1 &=~ (0x10);
39
    UCB1CTL1 |= UCTXSTT;
40
  //  while(UCB1CTL1 & UCTXSTT);
41
    if(UCB1CTL1 & UCTXSTT){
42
      P1DIR |= BIT0;
43
      P1OUT |= BIT0;
44
    }else{
45
      P4DIR |= BIT7;
46
      P4OUT |= BIT7;
47
    }
48
   // while((UCB1STAT & UCBBUSY) > 0x00);
49
    while((UCB1IFG & UCRXIFG) == 0x00);
50
    data = UCB1RXBUF*256;
51
    //ACK automatisch vermutlich 
52
    
53
    while((UCB1IFG & UCRXIFG) == 0x00);
54
    data += UCB1RXBUF;
55
    
56
    
57
    UCB1CTL1 |= UCTXNACK;
58
    UCB1CTL1 |= UCTXSTP; 
59
    
60
    
61
    
62
}
63

64
void startWrite(unsigned char cmd){
65
  UCB1CTL1 |= UCTXSTT | (0x10);
66
  //UCB1CTL1 |= UCTXSTP; 
67
  UCB1TXBUF = cmd;
68
  while((UCB1IFG & UCTXIFG) == 0x00);
69
  
70
  
71
   
72
}
73

74
int main(){
75
    WDTCTL = WDTPW + WDTHOLD;
76
    P4DIR |= BIT2;
77
    P4DIR |= BIT1;
78

79
    
80

81
    while(1){
82

83
    initI2C();
84
    
85
    startWrite(RHMeasure);
86
   
87
    /* help2 = startRead();
88
    rh = (help2*125)/65536-6;
89
    
90
    startWrite(TEMpMeasure);
91
    help1 = startRead();
92
    temp = (help1*175.72)/65536-46.85;
93
    */
94
    __delay_cycles(100);
95
   
96
    }
97
}
#5867156
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

A question of your own? This thread is over 7 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.