我想学学430单片机自带的IIC模块使用,现在编了段小程序,可是运行不正确,程序如下,问题在最后面。
//初始化IIC模块
UCB0CTL1 |= UCSWRST;
UCB0CTL0 |=UCMST+UCMODE1+UCMODE0+UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCMODE1+UCMODE0+UCSWRST; // Use SMCLK, keep software reset
P3SEL |=0x06; //p3.1为UCB0SDA,P3.2为UCB0SCL
UCB0CTL1 = UCSSEL_3+UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 =10; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0CTL1 &= ~UCSWRST; // Clear software reset, resume operation
//写函数
UCB0CTL0 |=UCMST+UCMODE_3+UCSYNC; // I2C Master, synchronous mode
//UCB0CTL1 = UCMODE1+UCMODE0+UCSWRST; // Use SMCLK, keep software reset
UCB0I2CSA = 0x3a; //这个地址是真实地址后面加读/写位组成
UCB0CTL0 &=~UCSLA10; //从机地址为7位
UCB0CTL1 |= UCTR;//transmitter mode 发送模式
UCB0CTL1 |= UCTXSTT; //start condition,
while((IFG2 & UCB0TXIFG) != UCB0TXIFG);//检测UCB0TXBUF是否为空,为空则UCB0TXIFG为1.
for(i=0;i<1;i++)
{
UCB0TXBUF = 0x0d;//IIC设备中寄存器的地址
while((UCB0CTL1 & UCTXSTT) == UCTXSTT);//检测从机ACK信号,有ACK信号,则UCTXSTT自动清零//以上顺序不能改变
while((IFG2 & UCB0TXIFG) != UCB0TXIFG);//检测UCB0TXBUF是否为空,
}
UCB0CTL1 |= UCTXSTP;//发送STOP信号
我的问题是 :
1. UCB0CTL1 |= UCTXSTT; 也就是发送START后示波器上显示的IIC从设备的地址信息不对;
2. UCB0TXBUF = 0x0d,运行后,示波器没反应更谈不上有ACK信号,可是却通过了while((UCB0CTL1 & UCTXSTT) == UCTXSTT判断是否有确认信号的这句话;
3. 程序停在了倒数第二句话while((IFG2 & UCB0TXIFG) != UCB0TXIFG);//检测UCB0TXBUF是否为空,既然已经把数据0x0d送到发送缓冲中去,就应该有输出,而且UCB0TXBUF为空才对啊?
向大家请教了!!
此帖出自
小平头技术问答
- //******************************************************************************
- // MSP430G2xx3 Demo - USCI_B0 I2C Master Interface to DAC8571, Write
- //
- // Description: Using UCB0TXIE, a continuous sine wave is output to the
- // external DAC using a 16-point look-up table. Only one start condition
- // is executed. Data is handled by the ISR and the CPU is normally in LPM0.
- // ACLK = n/a, MCLK = SMCLK = BRCLK = default DCO = ~1.2MHz
- //
- // MSP430G2xx3 DAC8571
- // ------------------ ------------
- // -|XIN P1.7/UCB0SDA|<--------------->|SDA |
- // | P1.6/UCB0SCL|---------------->|SCL I2C |
- // -|XOUT | | SLAVE |
- // | I2C MASTER | GND|A0 |
- //
- //
- // DAC8571 I2C address = 0x4C (A0 = GND)
- //
- // D. Dang
- // Texas Instruments Inc.
- // February 2011
- // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10
- //******************************************************************************
- #include "msp430g2553.h"
- const unsigned char Sine_Tab[] = // 16 Point 16-bit Sine Table
- {
- 0xFF, // MSB Word 0
- 0xFF, // LSB
- 0xF6, // MSB Word 1
- 0x40, // LSB
- 0xDA, // MSB Word 2
- 0x81, // LSB
- 0xB0, // MSB Word 3
- 0xFA, // LSB
- 0x7F, // MSB Word 4
- 0xFF, // LSB
- 0x4F, // MSB Word 5
- 0x03, // LSB
- 0x25, // MSB Word 6
- 0x7C, // LSB
- 0x09, // MSB Word 7
- 0xBD, // LSB
- 0x00, // MSB Word 8
- 0x00, // LSB
- 0x09, // MSB Word 9
- 0xBD, // LSB
- 0x25, // MSB Word 10
- 0x7C, // LSB
- 0x4F, // MSB Word 11
- 0x03, // LSB
- 0x7F, // MSB Word 12
- 0xFE, // LSB
- 0xB0, // MSB Word 13
- 0xFA, // LSB
- 0xDA, // MSB Word 14
- 0x81, // LSB
- 0xF6, // MSB Word 15
- 0x40 // LSB
- };
- void main(void)
- {
- WDTCTL = WDTPW + WDTHOLD; // Stop Watchdog Timer
- P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0
- P1SEL2|= BIT6 + BIT7; // Assign I2C pins to USCI_B0
- UCB0CTL1 |= UCSWRST; // Enable SW reset
- UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
- UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
- UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
- UCB0BR1 = 0;
- UCB0I2CSA = 0x4c; // Set slave address
- UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
- IE2 |= UCB0TXIE; // Enable TX ready interrupt
- UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
- UCB0TXBUF = 0x010; // Write DAC control byte
- __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
- }
- // USCI_B0 Data ISR
- #pragma vector = USCIAB0TX_VECTOR
- __interrupt void USCIAB0TX_ISR(void)
- {
- static unsigned char ByteCtr;
- UCB0TXBUF = Sine_Tab[ByteCtr++]; // Transmit data byte
- ByteCtr &= 0x1f; // Do not exceed table
- }
复制代码“把IIC从设备的sub-address送人UCB0TXBUF之后”,看到这句话,我猜测可能操作流程上有些小问题。观察上面这段示例代码,其实写从地址的操作不需要你发到TXBUF中,这个过程会由MSP430自动完成。
一周热门 更多>