#include"msp430x16x.h"
#define SDA_IN P4DIR&=~BIT0
#define SDA_OUT P4DIR|=BIT0
#define SDA_LOW P4OUT&=~BIT0
#define SDA_HIGH P4OUT|=BIT0
#define SCL_LOW P4OUT&=~BIT1
#define SCL_HIGH P4OUT&=~BIT1
#define LED1_ON P3OUT|=0x01 //LED1电源控制信号
#define LED2_ON P3OUT|=0x02 //LED2电源控制信号
#define LED3_ON P3OUT|=0x04 //LED3电源控制信号
#define LED_all_OFF P3OUT&=0xF8 //LED电源全部灭
const unsigned char disp_led[16]={ //7段显示笔划
0X77,0X11,0X5E,0X5B,0X39,0X6B,0X6F,0X51,0X7F,0X7B,
0X08,0X00,0X66,0X3D,0X26,0X7D};//0123456789- CHLA;
void delay_ms(unsigned char ms)
{
unsigned char t;
while(ms--)
{
for(t=0;t<5000;t++);
}
_NOP();
}
void delay_us(unsigned char us)
{
unsigned char t;
while(us--)
{
for(t=0;t<4;t++);
}
_NOP();
}
void start(void)
{
SDA_OUT;
delay_us(20);
SDA_HIGH;
delay_us(3);
SCL_HIGH;
delay_us(2);
SDA_LOW;
delay_us(5);
SCL_LOW;
delay_us(10);
}
void stop(void)
{
SDA_OUT;
SDA_LOW;
delay_us(2);
SCL_HIGH;
delay_us(2);
SDA_LOW;
delay_us(2);
SDA_HIGH;
}
void ack(void)
{
SDA_OUT;
SDA_LOW;
delay_us(2);
SCL_LOW;
delay_us(2);
SCL_HIGH;
delay_us(2);
SCL_LOW;
SDA_HIGH;
}
void noack(void)
{
SDA_OUT;
SDA_HIGH;
delay_us(2);
SCL_LOW;
delay_us(2);
SCL_HIGH;
delay_us(2);
SCL_LOW;
}
unsigned char check_ack(void)
{
unsigned char i;
SDA_IN;
SCL_HIGH;
delay_us(2);
if(P4IN&0X01)
{
i=0;
}
else
{
i=1;
}
SCL_LOW;
delay_us(2);
SDA_OUT;
return i;
}
void write(unsigned char data)
{
unsigned char tem;
SDA_OUT;
SCL_LOW;
for(tem=0;tem<8;tem++)
{
if(data&0x80)
{
SDA_HIGH;
}
else
{
SDA_LOW;
}
delay_us(2);
SCL_HIGH;
delay_us(2);
SCL_LOW;
delay_us(2);
data<<=1;
}
SDA_HIGH;
delay_us(15);////////////////////////需要加SDA_HIGH;
}
unsigned char read(void)
{
unsigned char tem;
unsigned char dat;
SDA_IN;
SCL_LOW;
delay_us(2);
for(tem=0;tem<8;tem++)
{
SCL_HIGH;
delay_us(2);
dat<<=1;
if(P4IN&0X01)
{
dat|=0x01;
}
else
{
dat&=0xfe;
}
SCL_LOW;
}
SDA_LOW;
SDA_OUT;///////////////////////需要加SDA_LOW;
return dat;
}
unsigned char write_init(unsigned int addr,unsigned char data)
{
start(); //////////////////////////
write(0XA0);/////////////////////////
if(check_ack==0)
{
return 0x55;
}
write(0X00);//////////////////////////
if(check_ack==0)
{
return 0x55;
}
write(addr);///////////////////////
if(check_ack==0)
{
return 0x55;
}
write(data);///////////////////////
if(check_ack==0)
{
return 0x55;
}
stop();////////////////////////////
delay_ms(10);
return 0;
}
unsigned char read_init(unsigned char addr)
{
unsigned char ch;
start();/////////////////////////////
write(0XA0);////////////////////////
if(check_ack==0)
{
return 0x55;
}
//delay_ms(5);
write(0X00);////////////////////////
if(check_ack==0)
{
return 0x55;
}
write(addr);///////////////////////位地址
if(check_ack==0)
{
return 0x55;
}
delay_ms(5);
////////////////////////////////////////////////////////
start();
write(0XA1);
if(check_ack==0)
{
return 0x55;
}
//delay_ms(5);
ch=read();
noack();
stop();
delay_us(100);
return ch;
}
void port(void)
{
P1DIR =0x0f;
P1SEL = 0;
P1OUT = 0xff;
P2DIR=0xff;
P2SEL=0x00;
P2OUT=0x00;
P3DIR=0x17;
P3SEL=0x30;
P3OUT=0x00;
P4DIR = 0x03;
P4SEL = 0;
}
void main(void)
{
unsigned char value=0;
unsigned char val;
WDTCTL = WDTPW + WDTHOLD;
port();
while(1)
{
LED_all_OFF;
value++;
write_init(10,value);
if(value==10)
value=0;
delay_ms(300);
val=read_init(10);
LED1_ON;
P2OUT=disp_led[val];
delay_ms(300);
}
}
|