#include<msp430f149.h>
#define OP_READ 0xa1 //器件地址读
#define OP_WRITE 0xa0 //器件地址写
#define uchar unsigned char
#define uint unsigned int
#define SDA_OUT P1DIR |= BIT3
#define SDA_IN P1DIR &= ~BIT3
#define SCL_OUT P1DIR |= BIT2
#define SCL_IN P1DIR &= ~BIT2
#define SDA_H P1OUT |= BIT3
#define SDA_L P1OUT &= ~BIT3
#define SCL_H P1OUT |= BIT2
#define SCL_L P1OUT &= ~BIT2
#define wei_h P5OUT |= BIT5
#define wei_l P5OUT &= BIT5
#define duan_l P6OUT &= ~BIT6
#define duan_h P6OUT |= BIT6
uchar table[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar miao;
uchar write;
int num;
void delayus(uint z)
{
uint i,j;
for(i = 0;i<20;i++)
for(j=0;j<z;j++)
{;;};
}
void start()
{
SDA_OUT;
SCL_OUT;
SDA_H;
SCL_H;
delayus(1);
SDA_L;
delayus(2);
SCL_L;
}
void stop()
{
SDA_OUT;
SCL_OUT;
SDA_L;
delayus(2);
SCL_H;
delayus(2);
SDA_H;
}
void Ack24()
{
SDA_OUT;
SCL_OUT;
SDA_H;
SCL_H;
delayus(2);
SDA_IN;
while(P1IN & BIT3)
SCL_L;
delayus(2);
}
//mcu读取数据
uchar read_byte()
{
uchar i,read_data;
SDA_OUT;
SCL_OUT;
for(i = 0;i<8;i++)
{
SDA_H;
delayus(1);
SCL_H;
SDA_IN;
read_data <<= 1;
if(P1IN & BIT3)
read_data |= 0x01;
SDA_OUT;
SCL_L;
}
return(read_data);
}
void write_byte(uint dat)
{
uchar i;
SDA_OUT;
SCL_OUT;
for(i = 0;i<8;i++)
{
if(dat&0x80)
SDA_H;
else
SDA_L;
delayus(2);
SCL_H;
delayus(2);
SCL_L;
delayus(2);
dat <<= 1;
}
SCL_L;
delayus(2);
SDA_H;
delayus(2);
}
//向指定地址写数据
void write_dat(uchar addr,uchar write_data)
{
SDA_OUT;
SCL_OUT;
SDA_H;
SCL_H;
start();
write_byte(OP_WRITE);
Ack24();
write_byte(addr);
Ack24();
write_byte(write_data);
Ack24();
stop();
delayus(1000);
}
//从指定地址读数据
uchar read_random(uint random_addr)
{
uchar i;
SDA_OUT;
SCL_OUT;
SDA_H;
SCL_H;
start();
write_byte(OP_WRITE);
Ack24();
write_byte(random_addr);
Ack24();
start();
write_byte(OP_READ);
Ack24();
i = read_byte();
stop();
return i;
}
void display(uchar dat)
{
P4OUT = table[dat];
duan_h;
duan_l;
P4OUT = ~(1 << num);
wei_h;
wei_l;
}
void Clock(void)
{
miao++;
if(miao==10)
miao=0;
}
void main( void )
{
P1DIR = 0XFF,P1OUT = 0XFF;
P2DIR = 0XFF,P2OUT = 0XFF;
P3DIR = 0XFF,P3OUT = 0XFF;
P4DIR = 0XFF,P4OUT = 0XFF;
P5DIR = 0XFF,P5OUT = 0XFF;
P6DIR = 0XFF,P6OUT = 0XFF;
SDA_OUT;
SCL_OUT;
SDA_H;
SCL_L;
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
CCR0 = 32768 -1;
TACTL = TASSEL_1 + MC_1;
CCTL0 |= CCIE;
_EINT();
miao = read_random(3);
if(miao>9) miao=0;
while(1)
{
display(miao);
if(write==1)
{
write = 0;
write_dat(3,miao);
}
LPM3;
}
}
#pragma vector = TIMERA0_VECTOR
__interrupt void TimerA_ISR(void)
{
Clock();
write = 1;
LPM3_EXIT;
}
|