您的位置 首页 电源

用430写的IIC总线协议:24C02掉电回忆程序(数码管显现)

includemsp430x14xh>defineOP_READ0xa1器件地址以及读取操作defineOP_WRITE0xa0器件地址以及写入操作define

#include <msp430x14x.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
//数码管7位段码:0–f
uchar table[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//记载显现位数的大局变量
uchar miao;
uchar write;
//8M晶振时大约20us延时程序
void delayus(uint z)
{
uint i,j;
for(i = 0;i<20;i++)
for(j=0;j {;;};
}
// 开端位
void start()
{
SDA_OUT;
SCL_OUT;
SDA_H;
SCL_H;
delayus(1); //20
SDA_L;
delayus(2);//40
SCL_L;
}
// 中止位
void stop()
{
SDA_OUT;
SCL_OUT;
SDA_L;

delayus(2);//20
SCL_H;
delayus(2);
SDA_H;
}
// 从AT24Cxx移入数据到MCU
uchar shin()
{
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);
}
// 从MCU移出数据到AT24Cxx
uchar shout(uint write_data)
{
uchar i;
uchar ack_bit;
SDA_OUT;
SCL_OUT;
for(i = 0; i < 8; i++) // 循环移入8个位
{
if(write_data&0x80)
SDA_H;
else
SDA_L;
delayus(2);//2
SCL_H;
delayus(2);//2
SCL_L;
delayus(2);//2
write_data <<= 1;
}

SDA_H; // 读取应对
delayus(2);
SCL_H; //留意拉高
delayus(2);
SDA_IN;
if(P1IN&BIT3)
ack_bit=1;
else ack_bit=0;
SCL_L;
return ack_bit; // 回来AT24Cxx应对位
}
// 在指定地址addr处写入数据write_data
void write_byte(uchar addr, uchar write_data)
{
SDA_OUT;
SCL_OUT;
SDA_H;
SCL_H;
start();
shout(OP_WRITE);
shout(addr);
shout(write_data);
stop();
delayus(1000);// 写入周期
}
// 在当时地址读取
uint read_current()
{
uint read_data;
start();
shout(OP_READ);
read_data = shin();
stop();
return read_data;
}

// 在指定地址读取
uchar read_random(uint random_addr)
{
SDA_IN;
SCL_OUT;
SDA_H;
SCL_H;
start();
shout(OP_WRITE);
shout(random_addr);
return(read_current());
}

void display(uchar dat)
{
P4OUT = table[dat];//输出段选信号
duan_h;
duan_l;
P4OUT = 0x00; //设置P4,P5的输出初值
wei_h;
wei_l;
}

void Clock(void)
{
miao++;
if(miao==10)
miao=0;
}

int main( void )
{
/*下面六行程序封闭一切的IO口*/
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_H;

WDTCTL = WDTPW + WDTHOLD;
CCR0 = 32768 – 1;//设置定时器A的中止时刻为1S
TACTL = TASSEL_1 + MC_1; //计数时钟ACLK, 增计数模
CCTL0 |= CCIE; //使能CCR0比较中止
_EINT();//翻开大局中止

miao=read_random(3) ;//从地址2中读出曾经保存的数据
if(miao>9) miao=0; //避免初次读出过错数据

while(1)
{
display(miao);
if(write==1)
{
write=0;
write_byte(3,miao);
}

LPM3;//进入LPM3低功耗形式

}
}

/*******************************************
函数称号:TimerA_ISR
功 能:定时器A的中止服务函数
********************************************/
#pragmavector=TIMERA0_VECTOR
__interrupt void TimerA_ISR(void)
{
Clock();
write=1;
LPM3_EXIT;
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/dianyuan/254051.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部