IIC总线 MSP430F149与24c16综合实验
主程序
//***************************************************************
// 描述:
// 开机发送数给24C16,然后将数据读出,并通过串口工具显示;
//
// 跳线: P6 P8 P9
//***************************************************************
#include "msp430x14x.h"
#include "Uart.h"
#include "24c16.h"
#define uchar unsigned char
unsigned char *D,*M,add,ACK_FLAG,flag;
unsigned char MPM[32];
//unsigned char DDT[32]=
// {
// 0xf3,0x16,0x69,0x21,0xd3,0x15,0xc5,0x23,
// 0xb6,0x28,0x85,0x25,0xc3,0x24,0xd7,0x16,
// 0xf3,0x16,0x69,0x21,0xd3,0x15,0xc5,0x23,
// 0xb6,0x28,0x85,0x25,0xc3,0x24,0xd7,0x16
// };
unsigned char DDT[32]=
{
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,//0 1 2 3 4 5 6 7
0x38,0x39,0x41,0x42,0x43,0x44,0x45,0x46,//8 9 A B C D E F
0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E, //G H I J K L M N
0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56, //O P Q R S T U V
};
void main(void)
{
unsigned int i;
WDTCTL=WDTPW+WDTHOLD;
uart_org();
UartStr("开机测试成功!
");
D=(unsigned char*)DDT;
add=0; //24C01内数据读写首地址
M=(unsigned char*)MPM;
delay(100);
for(i=0;i<4;i++)
{
send_word();
delay(1000);
add+=8;
}
add=10;
for(i=0;i<32;i++)
receive_word(i);
Uart_Str(32,&MPM[0]);
TXBUF0 = MPM[7];
}
子程序 24C16
#include <msp430x14x.h>
#include "24c16.h"
#define uchar unsigned char
extern uchar ACK_FLAG,add,*D,MPM[32];
void delay(unsigned int i)
{
while(i-- > 0);
}
void start(void)
{
SDA_OUT;
SCL_OUT;
SDA_0;
SCL_0;
delay(2);
SDA_1;
SCL_1;
delay(2);
SDA_0;
delay(2);
SCL_0;
delay(3);
SDA_1;
}
void stop(void)
{
SDA_OUT;
SCL_OUT;
SDA_0;
SCL_1;
delay(2);
SDA_1;
}
void send_byte(uchar data)
{
uchar bi,i;
bi=0x80;
SDA_OUT;
SCL_OUT;
for(i=0;i<8;i++)
{
if((data&bi)==bi) SDA_1;
else SDA_0;
SCL_1;
delay(50);
SCL_0;
bi>>=1;
}
}
uchar receive_byte(void)
{
uchar i,temp=0x80,tempdata;
tempdata=0;
SDA_IN;
SCL_OUT;
for(i=0;i<8;i++)
{
SCL_1;
if((P5IN&BIT1)==BIT1) tempdata|=temp;
temp>>=1;
SCL_0;
}
return(tempdata);
}
void ack(void)
{
SCL_OUT;
SDA_IN;
SCL_1;
ACK_FLAG=0;
if((P5IN&BIT1)) ACK_FLAG=1;
SCL_0;
delay(10);
}
void i2c_ack(uchar tm)
{
SDA_OUT;
SCL_OUT;
if(tm==0) SDA_1;
else SDA_0;
delay(10);
SCL_1;
delay(10);
SCL_0;
delay(10);
}
void send_word(void)
{
uchar i=0;
while(1)
{
start();
delay(20);
send_byte(0xa0);
ack();
if(ACK_FLAG) continue;
send_byte(add);
ack();
if(ACK_FLAG) continue;
for(i=0;i<8;i++)
{
send_byte(*D++);
ack();
if(ACK_FLAG)continue;
}
if(!ACK_FLAG) break;
}
stop();
}
void receive_word(uchar add)
{
uchar i;
while(1)
{
start();
send_byte(0xa0);
ack();
if(ACK_FLAG) continue;
send_byte(add);
ack();
if(ACK_FLAG) continue;
start();
send_byte(0xa1);
ack();
if(ACK_FLAG) continue;
MPM[i++]=receive_byte();
delay(5);
i2c_ack(1);
if(!ACK_FLAG) break;
}
stop();
}
子程序:uart
#include <msp430x14x.h>
#include "Uart.h"
/*****************串口初试化设置********************/
void uart_org(void)
{
ME1 |= UTXE0 + URXE0; // Enable USART0 TXD/RXD
UCTL0 |= CHAR; // 8-bit character
UTCTL0 |= SSEL0; // UCLK = ACLK
UBR00= 0x03; // 32768/9600
UBR10= 0x00;
UMCTL0= 0x4a;
P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD
P3DIR |= 0x10;
UCTL0 &= ~SWRST; // Initialize USART state machine
IE1 |= URXIE0; // Enable USART0 RX interrupt
}
/*****************串口发送字符串程序*****************/
void UartStr(unsigned char *p)
{
unsigned char i;
for (i=0;*p!=0;i++)//准备要发送的数据
{
while (!(IFG1 & UTXIFG0));// USART0 TX buffer ready?
TXBUF0 =*p++; // RXBUF0 to TXBUF0
while ((UTCTL0 & TXEPT) == 0);
}
_NOP();
}
/*****************串口发送字符串程序*****************/
void Uart_Str(unsigned char number,unsigned char *p)
{
unsigned char i;
for (i=0;i<number;i++)//准备要发送的数据
{
while (!(IFG1 & UTXIFG0));// USART0 TX buffer ready?
TXBUF0 =*p++; // RXBUF0 to TXBUF0
while ((UTCTL0 & TXEPT) == 0);
}
_NOP();
}
一周热门 更多>