karan29
Newbie level 5
- Joined
- Jan 30, 2012
- Messages
- 10
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Location
- baroda
- Activity points
- 1,445
i have msp430fg4618 controller. i trying to integrate hyt-221 sensor with it my connection is ok and i provide pull up at slave(sensor).but its not responding.any one who previously integrate hyt-221 with msp plz help!!!!!!!! here is code but not works
#include "msp430fG4618.h"
unsigned char *prxdata,ct,temp,humidity,mr,rt;
unsigned char rxbuff[128],slvad=0x28;
double val_a;
unsigned int h,t,k,v,hex;
void delay(int i);
void mrrequest (void)
{
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 |= UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 =5; // fSCL = SMCLK/5 = 200kHz
UCB0BR1 = 0;
UCB0I2CSA =slvad >>1; // Slave Address is 028h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0TXIE;
UCB0CTL1 |= UCTR + UCTXSTT;
//while((IFG2 & UCB0TXIFG)==0);
//UCB0CTL1 |= UCTXSTP;
mr=1;
rt=1;
__bis_SR_register(CPUOFF + GIE);
// while (UCB0CTL1 & UCTXSTP);
}
void sendfetchcommand(void)
{
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 |= UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 =5; // fSCL = SMCLK/5 = 200kHz
UCB0BR1 = 0;
UCB0I2CSA = 0x28; // Slave Address is 028h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0TXIE;
UCB0CTL1 |= UCTR + UCTXSTT;
//while((IFG2 & UCB0TXIFG)==0);
//UCB0CTL1 |= UCTXSTP;
mr=3;
rt=1;
__bis_SR_register(CPUOFF + GIE);
}
void datafetch(void)
{
//UCB0CTL1 |= UCSWRST; // Enable SW reset
//UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
//UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
//UCB0BR0 = 5; // fSCL = SMCLK/11 = 95.3kHz
//UCB0BR1 = 0;
//UCB0I2CSA = 0x29;
//UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0RXIE;
IE2 &= ~UCB0TXIE;
UCB0CTL1 = UCSSEL_2 + UCTXSTT;
ct=4;
mr=2;
__bis_SR_register(CPUOFF + GIE);
__no_operation();
}
void convert (void)
{
prxdata -=0X03;
k=*prxdata;
k=k<<8;
prxdata++;
k += *prxdata;
v=k;
v=k && 0x4000;
if(v!=0)
{
k=k &0x3FFF;
h=hex2bcd(k);
val_a=0.0061035*h;
humidity=h;
prxdata++;
k=*prxdata;
k=k<<8;
prxdata++;
k += *prxdata;
t=hex2bcd(k);
val_a=0.0100708 * t;
temp=t;
}
}
int hex2bcd (int x)
{
unsigned int dummy;
temp=x;
hex=x;
dummy=hex/0x3E8;
hex=hex%0x3E8;
dummy = dummy<<12;
x=dummy;
dummy=hex/0x64;
hex=hex%0x64;
dummy = dummy<<8;
x = x + dummy;
dummy=hex/0xA;
hex=hex%0xA;
dummy = dummy<<4;
x = x + dummy;
dummy=hex;
x = x + dummy;
return x;
}
void delay(int i)
{
while(i!=0)
{
i--;
}
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P3SEL |= 0x06;
// P2SEL =0x00;
// P2DIR = 0x06;
while(1)
{
//P2OUT=0x04;
mrrequest();
sendfetchcommand();
datafetch();
convert();
}
}
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
switch(mr)
{
case 1:
if (rt) // Check TX byte counter
{
UCB0TXBUF = 0x14;
UCB0CTL1 |= UCTXSTP;
// while(UCB0CTL1 & UCTXSTT);// Load TX buffer
rt--; // Decrement TX byte counter
}
else
{
// I2C stop condition
IFG2 &= ~UCB0TXIFG;
mr=0;// Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
break;
case 2:
ct--;
if(ct)
{
P2OUT |=BIT1;
*prxdata++ = UCB0RXBUF; // Move RX data to address PRxData
if (ct== 1) // Only one byte left?
UCB0CTL1 |= UCTXSTP; //Generate I2C stop condition
P2OUT &= ~BIT1;
}
else
{
*prxdata = UCB0RXBUF; // Move final RX data to PRxData
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
mr=0;
// UCB0STAT &= ~(UCSTPIFG + UCSTTIFG);
break;
case 3:
if (rt) // Check TX byte counter
{
UCB0TXBUF = 0x29;
// while(UCB0CTL1 & UCTXSTT);// Load TX buffer
rt--; // Decrement TX byte counter
}
else
{
//UCB0CTL1 |= UCTXSTP; // I2C stop condition
IFG2 &= ~UCB0TXIFG;
mr=0;// Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
break;
}
}
#include "msp430fG4618.h"
unsigned char *prxdata,ct,temp,humidity,mr,rt;
unsigned char rxbuff[128],slvad=0x28;
double val_a;
unsigned int h,t,k,v,hex;
void delay(int i);
void mrrequest (void)
{
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 |= UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 =5; // fSCL = SMCLK/5 = 200kHz
UCB0BR1 = 0;
UCB0I2CSA =slvad >>1; // Slave Address is 028h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0TXIE;
UCB0CTL1 |= UCTR + UCTXSTT;
//while((IFG2 & UCB0TXIFG)==0);
//UCB0CTL1 |= UCTXSTP;
mr=1;
rt=1;
__bis_SR_register(CPUOFF + GIE);
// while (UCB0CTL1 & UCTXSTP);
}
void sendfetchcommand(void)
{
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 |= UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 =5; // fSCL = SMCLK/5 = 200kHz
UCB0BR1 = 0;
UCB0I2CSA = 0x28; // Slave Address is 028h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0TXIE;
UCB0CTL1 |= UCTR + UCTXSTT;
//while((IFG2 & UCB0TXIFG)==0);
//UCB0CTL1 |= UCTXSTP;
mr=3;
rt=1;
__bis_SR_register(CPUOFF + GIE);
}
void datafetch(void)
{
//UCB0CTL1 |= UCSWRST; // Enable SW reset
//UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
//UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
//UCB0BR0 = 5; // fSCL = SMCLK/11 = 95.3kHz
//UCB0BR1 = 0;
//UCB0I2CSA = 0x29;
//UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0RXIE;
IE2 &= ~UCB0TXIE;
UCB0CTL1 = UCSSEL_2 + UCTXSTT;
ct=4;
mr=2;
__bis_SR_register(CPUOFF + GIE);
__no_operation();
}
void convert (void)
{
prxdata -=0X03;
k=*prxdata;
k=k<<8;
prxdata++;
k += *prxdata;
v=k;
v=k && 0x4000;
if(v!=0)
{
k=k &0x3FFF;
h=hex2bcd(k);
val_a=0.0061035*h;
humidity=h;
prxdata++;
k=*prxdata;
k=k<<8;
prxdata++;
k += *prxdata;
t=hex2bcd(k);
val_a=0.0100708 * t;
temp=t;
}
}
int hex2bcd (int x)
{
unsigned int dummy;
temp=x;
hex=x;
dummy=hex/0x3E8;
hex=hex%0x3E8;
dummy = dummy<<12;
x=dummy;
dummy=hex/0x64;
hex=hex%0x64;
dummy = dummy<<8;
x = x + dummy;
dummy=hex/0xA;
hex=hex%0xA;
dummy = dummy<<4;
x = x + dummy;
dummy=hex;
x = x + dummy;
return x;
}
void delay(int i)
{
while(i!=0)
{
i--;
}
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P3SEL |= 0x06;
// P2SEL =0x00;
// P2DIR = 0x06;
while(1)
{
//P2OUT=0x04;
mrrequest();
sendfetchcommand();
datafetch();
convert();
}
}
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
switch(mr)
{
case 1:
if (rt) // Check TX byte counter
{
UCB0TXBUF = 0x14;
UCB0CTL1 |= UCTXSTP;
// while(UCB0CTL1 & UCTXSTT);// Load TX buffer
rt--; // Decrement TX byte counter
}
else
{
// I2C stop condition
IFG2 &= ~UCB0TXIFG;
mr=0;// Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
break;
case 2:
ct--;
if(ct)
{
P2OUT |=BIT1;
*prxdata++ = UCB0RXBUF; // Move RX data to address PRxData
if (ct== 1) // Only one byte left?
UCB0CTL1 |= UCTXSTP; //Generate I2C stop condition
P2OUT &= ~BIT1;
}
else
{
*prxdata = UCB0RXBUF; // Move final RX data to PRxData
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
mr=0;
// UCB0STAT &= ~(UCSTPIFG + UCSTTIFG);
break;
case 3:
if (rt) // Check TX byte counter
{
UCB0TXBUF = 0x29;
// while(UCB0CTL1 & UCTXSTT);// Load TX buffer
rt--; // Decrement TX byte counter
}
else
{
//UCB0CTL1 |= UCTXSTP; // I2C stop condition
IFG2 &= ~UCB0TXIFG;
mr=0;// Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
break;
}
}
Last edited: