/******************************************************************
**
** File : EEPROM.c | Eeprom write and read |
** Version : 1.0
** Description : Eeprom write and read
** Author : LightWu
** Date : 2013-4-15
**
*******************************************************************/
#include //注意:两次发送间隔必须要有延时,否则不能再次发送,串口发送格式: unsigned char PTxData[250]; // Pointer to TX data unsigned char pHeadT; unsigned char pTailT; unsigned char pHeadR; unsigned char pTailR; unsigned char TXByteCtr; const unsigned char TxData[] = // Table of data to transmit { 0x01, 0x02, 0x03, 0x04, 0x05 }; unsigned char RxData[] ; void UartInit(void) { if (CALBC1_1MHZ==0xFF) // If calibration constant erased { while(1); // do not load, trap CPU!! } DCOCTL = 0; // Select lowest DCOx and MODx settings BCSCTL1 = CALBC1_1MHZ; // Set DCO DCOCTL = CALDCO_1MHZ; P3SEL |= 0x30; // P3.4,5 = USCI_A0 TXD/RXD UCA0CTL1 |= UCSSEL_2; // SMCLK UCA0BR0 = 104; // 1MHz 9600; (104)decimal = 0x068h UCA0BR1 = 0; // 1MHz 9600 UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1 UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine** } void UartSend( unsigned char Data ) { UCA0TXBUF = Data; // TX -> RXed character while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?,发送缓冲区空 } void IICInit(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 = 12; // fSCL = SMCLK/12 = ~100kHz UCB0BR1 = 0; UCB0I2CSA = 0xA0>>1; // Slave Address is ,注意地址需要右移一位,24C02地址为0XA0,故要写入0X50 // 7位地址模式,器件会发送一位读写位,正好8位。 UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation IE2 |= UCB0TXIE; // Enable TX interrupt IE2 |= UCB0RXIE; } void IICSend( void ) { int i; for(i=3000;i>0;i--); //两次发送间隔必须要有延时,否则不能再次发送 while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts } void IICRecv( void ) { int i; for(i=3000;i>0;i--); //两次发送间隔必须要有延时,否则不能再次发送 while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent UCB0CTL1 &= ~UCTR; UCB0CTL1 |= UCTXSTT; __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts } //写入一个字节数据 void EepromWrite( unsigned char Addr, unsigned char Data ) { int i; pHeadT = 0; pTailT = 2; //一个地址,一个数据 PTxData[0] = Addr; PTxData[1] = Data; for(i=3000;i>0;i--); //两次发送间隔必须要有延时,否则不能再次发送 while (UCB0STAT & UCBUSY); // wait until I2C module has // finished all operations. while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts //进入低功耗状态 while( pHeadT < pTailT ); //等待直到发送完成 pHeadT = 0; //清零 UCB0CTL1 |= UCTXSTP; // I2C stop condition,产生终止信号 while(UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent } //读N个字节数据 unsigned char EepromRead( unsigned char Addr , unsigned char Num ) { int i; pHeadT = 0; pTailT = 1; //写入1个地址 pHeadR = 0; pTailR = Num; //读取个数 PTxData[0] = Addr; for(i=3000;i>0;i--); //两次发送间隔必须要有延时,否则不能再次发送 while (UCB0STAT & UCBUSY); // wait until I2C module has // finished all operations. while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts //进入低功耗状态 //设置为读取接收 for(i=3000;i>0;i--); //两次发送间隔必须要有延时,否则不能再次发送 UCB0CTL1 &= ~UCTR; UCB0CTL1 |= UCTXSTT; //start Read __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts return 1; } int main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT P3SEL |= 0x06; // Assign I2C pins to USCI_B0 IICInit(); UartInit(); while (1) { UartSend('M'); EepromWrite(0x13,'A'); EepromWrite(0x11,'B'); EepromWrite(0x12,'C');
上一篇:MSP430F5438A 内存Flash 读写操作
下一篇:MSP430平台下实现AT24C02的读写操作
推荐阅读最新更新时间:2024-11-11 18:34
设计资源 培训 开发板 精华推荐
- MIC3202YME EV,评估板 MIC3202 是迟滞降压、恒流、高亮度 LED 驱动器
- DN05097/D,低成本直接 120 VAC LED 照明电路
- MB3771 双(正/负)电源电压监测的典型应用电路(VCC = 5V,VEE = 负电源)
- 51+sl811读写U盘的源程序+原理图
- 555_无线充电
- 使用 Micro Commercial Components 的 MC79L09F 的参考设计
- 复刻萝莉8通道接收机
- MIC5166 3A高速低VIN DDR终端典型应用
- 3.3V足够给力的ESP32开发板,仿制官方的
- 用于 MC9S08JM60 8 位 HCS08 MCU 的 3PHASELV-KIT、3 相 BLDC/PMSM 低压电机控制驱动器,用于开发 BLDC/PMSM 电机控制应用