#include
#include "i2c.h"
extern unsigned char RXData,TXData,TXByteCtr,ATXData[2];
void I2C_Init(uint8_t SlaveAddress)
{
P3SEL |= 0x03; // Assign I2C pins to USCI_B0
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST+UCTR; // Use SMCLK
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = SlaveAddress; // Slave Address is 048h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
UCB0IE |= UCTXIE+UCRXIE+UCSTPIE;
}
void I2C_WriteReg(unsigned char RegAddr, unsigned char Data) //////////涓婁笅灞侫PI闇?瑕佺敤鍒?
{
TXByteCtr = 2; // Load TX byte counter
ATXData[0]=RegAddr;
ATXData[1]=Data;
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0IE |= UCTXIE;
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0 w/ interrupts
__no_operation();
}
uint8_t I2C_ReadByte() //////////涓婁笅灞侫PI闇?瑕佺敤鍒?
{
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 &= ~UCTR;
UCB0IE &=~ UCTXIE;
UCB0CTL1 |= UCTXSTT; // I2C start condition
while(UCB0CTL1 & UCTXSTT); // Start condition sent?
UCB0CTL1 |= UCTXSTP;
return RXData;
}
#pragma vector = USCI_B0_VECTOR
__interrupt void USCI_B0_ISR(void)
{
switch(__even_in_range(UCB0IV,12))
{
case 0: break; // Vector 0: No interrupts
case 2: break; // Vector 2: ALIFG
case 4: break; // Vector 4: NACKIFG
case 6: break; // Vector 6: STTIFG
case 8:
UCB0IFG &= ~UCSTPIFG; // Clear stop condition int flag
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0 if data was transmitted
break; // Vector 8: STPIFG
case 10:
RXData=UCB0RXBUF;
// UCB0CTL1 |= UCTXNACK+UCTXSTP;
UCB0IFG &= ~UCRXIFG;
__bic_SR_register_on_exit(LPM0_bits);
break; // Vector 10: RXIFG
case 12: // Vector 12: TXIFG
if (TXByteCtr) // Check TX byte counter
{
UCB0TXBUF = ATXData[2-TXByteCtr]; // Load TX buffer
TXByteCtr--; // Decrement TX byte counter
}
else
{
UCB0CTL1 |= UCTXSTP; // I2C stop condition
UCB0IFG &= ~UCTXIFG; // Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
}
break;
default: break;
}
}
上一篇:利用MSP430定时器A测量脉冲宽度范例
下一篇:关于MSP430单片机数字与字符转换的问题
推荐阅读最新更新时间:2024-03-16 15:05
设计资源 培训 开发板 精华推荐
- 首都医科大学王长明:针对癫痫的数字疗法已进入使用阶段
- 非常见问题解答第223期:如何在没有软启动方程的情况下测量和确定软启动时序?
- 兆易创新GD25/55全系列车规级SPI NOR Flash荣获ISO 26262 ASIL D功能安全认证证书
- 新型IsoVu™ 隔离电流探头:为电流测量带来全新维度
- 英飞凌推出简化电机控制开发的ModusToolbox™电机套件
- 意法半导体IO-Link执行器电路板为工业监控和设备厂商带来一站式参考设计
- Melexis采用无磁芯技术缩小电流感测装置尺寸
- 千丘智能侍淳博:用数字疗法,点亮“孤独症”儿童的光
- 数药智能冯尚:ADHD数字疗法正为儿童“多动症”提供更有效便捷服务
- Vicor高性能电源模块助力低空航空电子设备和 EVTOL的发展