📄 3130.c
字号:
#include"msp430x14x.h"
#include"math.h"
#define uchar unsigned char
#define uint unsigned int
//=============================
#define uchar unsigned char
#define uint unsigned int
#define IIC_READ 0XA1 //
#define IIC_WRITE 0XA0 //
#define REG_READ 0XD1
#define REG_WRITE 0XD0
#define RUN 1
#define STOP 0
//#define NOP _NOP()
//===============================
#define SERIALNUM 0X11
#define COUNTER2M 0X10
#define COUNTER2L 0X0F
#define COUNTER1M 0X0E
#define COUNTER1L 0X0D
#define COUNTCON 0X0C //ALARM MINUTES
#define COMPANION 0X0B //ALARM SECONDS
#define WDCON 0X0A
#define WDFLAG 0X09 //WATCHDOG
#define YEAR 0X08 //YEARS
#define MONTH 0X07 //MONTH
#define DATE 0X06 //DATE
#define DAY 0X05 //DAY
#define HOURS 0X04 //HOURS
#define MINUTES 0X03 //MINUTES
#define SECONDS 0X02 //SECONDS
#define CONTROL 0x01 //USER-NV
#define FLAGS 0x00 //FLAGS/CONTROL
//=====================================
//==============================
//uchar buff[32]; //save the byte read out form iic device in test operation
//uchar reg[24];
//uchar numtime;
//===============================
#define SDA_1 P4OUT|=BIT1
#define SDA_0 P4OUT&=~BIT1
#define SCL_1 P4OUT|=BIT2
#define SCL_0 P4OUT&=~BIT2
//#define SDA P4IN&=BIT1
//sbit SDA=P1^4;//P1^1;//P1^2;//P1^1;//P1^2;//P2^7; //THE SDA BIT IS PORT 2 BIT 7
//sbit SCL=P1^7;//P1^0;//P3^5;//P1^3;//P2^6; //THE SCL BIT IS PORT 2 BIT 6
//sbit WP=P2^5;
//sbit test=P1^0;
//===============================
//define a bit_operation byte to use in shift operation
//use this mode can achieve high operation speed
uchar bbyte;
/*
sbit a0=bbyte^0;
sbit a1=bbyte^1;
sbit a2=bbyte^2;
sbit a3=bbyte^3;
sbit a4=bbyte^4;
sbit a5=bbyte^5;
sbit a6=bbyte^6;
sbit a7=bbyte^7;*/
//=================================================
//bit IFACK; //record the SDA state to confirn if ACK has happened
//=================================================
/*
;* Description: define the struct value to save time
*/
/*
struct str_time//时间结构变量
{
uchar year;
uchar month;
uchar date;
uchar day;
uchar hour;
uchar minute;
uchar secend;
};*/
uchar inbyte(void);
void start(void);
void stop(void);
void ack(void);
void nack(void);
uchar hex_to_bcd(uchar condata);
uchar bcd_to_hex(uchar condata);
void time_control(uchar type);
void read_time(uchar *temp);
void write_time(uchar *temp);
uchar inbyte(void);
void outbyte(uchar outdata) ;
void writebyte(uint add,uchar wbyte);
uchar readbyte (uint add);
void write_reg(uchar add,uchar wbyte);
uchar read_reg (uchar add);
void RTC_init();
void delay_1us()
{
_NOP(); _NOP();_NOP();_NOP(); _NOP();_NOP();_NOP(); _NOP();_NOP();
_NOP(); _NOP();_NOP();_NOP(); _NOP();_NOP();_NOP(); _NOP();_NOP();
_NOP(); _NOP();_NOP();_NOP(); _NOP();_NOP();_NOP(); _NOP();_NOP();
}
//======================================
//struct str_time time;
//======================================
/*
;* Name: hex_to_bcd
;* Description: 十六进制数转换成BDC码格式
;* Calls: None
;* Input: 需要转换的十六进制数
;* Outputs: 转换后的BCD码
*/
//========================================
//FUNCTION:ROUTES TO PROVIDE A START SIGNAL
void start(void)
{
delay_1us();SCL_0;SDA_1;delay_1us();SCL_1;SDA_0;delay_1us();SCL_0;
}
//=======================================
//FUNCTION:ROUTES TO PROVIDE A STOP SIGNAL
void stop(void)
{
//SCL=0;
delay_1us();SDA_0;SCL_1;delay_1us();SDA_1;SCL_0;
}
//=====================================
//FUNCTION:ROUTES TO PROVIDE ACK SINGAL
void ack(void)
{
//SCL=0;
SDA_0;SCL_1;delay_1us();SCL_0;
}
//=====================================
//FUNCTION:ROUTES TO RELEASE THE SDA TO RECEIVE A ACK SIGNAL
// OR TO PROVIDE A NO_ACK SIGNAL
void nack(void)
{
uint i;
//SCL=0;
SDA_1;
SCL_1;
delay_1us();
i=5;
while(i--){;}
//IFACK=SDA;
i=0;
//while(((P4IN&BIT1)==BIT1)&&(i++<20000));
delay_1us();
SCL_0;delay_1us();
//IFACK=1;
}
uchar hex_to_bcd(uchar condata)
{
uchar temp;
uchar temp0;
uchar temp10;
temp10=condata/10;
temp0=condata%10;
temp=(temp10<<4)|temp0;
return(temp);
}
//======================================
/*
;* Name: bcd_to_hex
;* Description: BCD码格式转换成十六进制数
;* Calls: None
;* Input: 需要转换的BCD码
;* Outputs: 转换后的十六进制数
*/
//======================================
uchar bcd_to_hex(uchar condata)
{
uchar temp;
uchar temp0;
uchar temp10;
temp10=(condata&0xF0)>>4;
temp0=condata&0x0F;
temp=temp10*10+temp0;
return(temp);
}
//============================================
void time_control(uchar type)
{
if(type==RUN)
write_reg(CONTROL, 0X00);
else
write_reg(CONTROL, 0X80);
}
//============================================
/*
;* Name: read_time
;* Description:
;* Calls: None
;* Input: None
;* Outputs: None
temp[6]--年
temp[5]--月
temp[4]--日
temp[3]--星期
temp[2]--时
temp[1]--分
temp[0]--秒
*/
//==============================================
void read_time(uchar *temp)
{
uchar i6;
write_reg(0,1);
for(i6=2;i6<9;i6++)
{
temp[i6-2]=read_reg(i6);
}
write_reg(0,0);
//uchar temp[7];
/*uchar i,flagstempa,flagstempb;
P4DIR|=BIT1;
P4DIR|=BIT2;
//flagstempa=read_reg(FLAGS);
//flagstempb=(flagstempa|0x01)&0xFD;
//=========================
write_reg(FLAGS, 1);
start();
outbyte(REG_WRITE);
nack();
outbyte(SECONDS);
nack();
start();
outbyte(REG_READ);
nack();
for(i=0;i<6;i++)
{
temp[i]=inbyte();
ack();
}
temp[6]=inbyte();
nack();
stop();
write_reg(FLAGS, 0);
//return (temp);
//==========================
//time.year=bcd_to_hex(temp[6]);
//time.month=bcd_to_hex(temp[5]);
//time.date=bcd_to_hex(temp[4]);
//time.day=bcd_to_hex(temp[3]);
//time.hour=bcd_to_hex(temp[2]);
//time.minute=bcd_to_hex(temp[1]);
//time.secend=bcd_to_hex(temp[0]);
P4DIR&=~BIT1;
P4DIR&=~BIT2;
/*SCL_0;
SDA_0;*/
}
//======================================
/*
;* Name: write_time
;* Description: 设置FM3808时钟寄存器
;* Calls: None
;* Input: None
;* Outputs: None
temp[6]--年
temp[5]--月
temp[4]--日
temp[3]--星期
temp[2]--时
temp[1]--分
temp[0]--秒
*/
//======================================
void write_time(uchar *temp)
{
write_reg(0,2);
write_reg(2,temp[0]);//秒
write_reg(3,temp[1]);
write_reg(4,temp[2]);
write_reg(5,temp[3]);
write_reg(6,temp[4]);
write_reg(7,temp[5]);
write_reg(8,temp[6]);//年
write_reg(0,temp[0]);
//uchar temp[7];
/*uchar i,flagstempa,flagstempb;
P4DIR|=BIT1;
P4DIR|=BIT2;
//flagstempa=read_reg(FLAGS);
//flagstempb=(flagstempa|0x02)&0xFE;
write_reg(0,2);
//temp[6]=hex_to_bcd(time.year);
//temp[5]=hex_to_bcd(time.month);
//temp[4]=hex_to_bcd(time.date);
//temp[3]=hex_to_bcd(time.day);
//temp[2]=hex_to_bcd(time.hour);
//temp[1]=hex_to_bcd(time.minute);
//temp[0]=hex_to_bcd(time.secend);
start();
outbyte(REG_WRITE);
nack();
outbyte(SECONDS);
nack();
for(i=0;i<7;i++)
{
outbyte(temp[i]);
nack();
}
stop();
write_reg(0,0);
P4DIR&=~BIT1;
P4DIR&=~BIT2;
/* SCL_0;
SDA_0;*/
}
//======================================
/*
;* Name: rst_wdg
;* Description: 看门狗复位
;* Calls: None
;* Input: None
;* Outputs: None
*/
//======================================
/*
void rst_wdg(void)
{
WDOG=0xFF;
}*/
//======================================
/*
;* Name: rst_wdg
;* Description: 设置看门狗超时值
;* Calls: None
;* Input: 超时值参数,范围为0X00-0X3F
;* Outputs: None
*/
//======================================
/*
void set_wdgvalue(uchar wdgval)
{
wdgval=wdgval&0x3f;
WDOG=0X00;//clr /wdw
WDOG=wdgval;
WDOG=(wdgval|0x40);//set /wdw
}*/
//======================================
//=======================================================
//FUNCTION:THE IIC DEVICE SHIFT OUT A BYTE TO THE MASTER
uchar inbyte(void) {
SDA_1;
P4DIR&=~BIT1;
SCL_1;
if ((P4IN&BIT1)==BIT1) bbyte|=BIT7;
else bbyte&=~BIT7;
delay_1us();
SCL_0;
SCL_1;
if ((P4IN&BIT1)==BIT1) bbyte|=BIT6;
else bbyte&=~BIT6;
delay_1us();
SCL_0;
SCL_1;
if ((P4IN&BIT1)==BIT1) bbyte|=BIT5;
else bbyte&=~BIT5;
delay_1us();
SCL_0;
SCL_1;
if ((P4IN&BIT1)==BIT1) bbyte|=BIT4;
else bbyte&=~BIT4;
delay_1us();
SCL_0;
SCL_1;
if ((P4IN&BIT1)==BIT1) bbyte|=BIT3;
else bbyte&=~BIT3;
delay_1us();
SCL_0;
SCL_1;
if ((P4IN&BIT1)==BIT1) bbyte|=BIT2;
else bbyte&=~BIT2;
delay_1us();
SCL_0;
SCL_1;
if ((P4IN&BIT1)==BIT1) bbyte|=BIT1;
else bbyte&=~BIT1;
delay_1us();
SCL_0;
SCL_1;
if ((P4IN&BIT1)==BIT1) bbyte|=BIT0;
else bbyte&=~BIT0;
delay_1us();
SCL_0;
P4DIR|=BIT1;
return(bbyte);
}
//=======================================================
//FUNCTION:THE IIC DEVICE SHIFT IN A BYTE FROM THE MASTER
void outbyte(uchar outdata) {
bbyte=outdata;
if((bbyte&BIT7)==BIT7) SDA_1;
else SDA_0;
SCL_1; delay_1us();SCL_0;
if((bbyte&BIT6)==BIT6) SDA_1;
else SDA_0;
SCL_1; delay_1us();SCL_0;
if((bbyte&BIT5)==BIT5) SDA_1;
else SDA_0;
SCL_1; delay_1us();SCL_0;
if((bbyte&BIT4)==BIT4) SDA_1;
else SDA_0;
SCL_1; delay_1us();SCL_0;
if((bbyte&BIT3)==BIT3) SDA_1;
else SDA_0;
SCL_1; delay_1us();SCL_0;
if((bbyte&BIT2)==BIT2) SDA_1;
else SDA_0;
SCL_1; delay_1us();SCL_0;
if((bbyte&BIT1)==BIT1) SDA_1;
else SDA_0;
SCL_1; delay_1us();SCL_0;
if((bbyte&BIT0)==BIT0) SDA_1;
else SDA_0;
SCL_1; delay_1us();SCL_0;
}
//======================================================
//FUNCTION:BYTE WRITE. 'add' THE WRITE ADDRESS, 'wbyte' THE DATA WANT TO WRITE
void writebyte(uint add,uchar wbyte)
{
P4DIR|=BIT1;
P4DIR|=BIT2;
start();
outbyte(IIC_WRITE);
nack();
outbyte(add>>8);
nack();
outbyte(add);
nack();
outbyte(wbyte);
nack();
stop();
P4DIR&=~BIT1;
P4DIR&=~BIT2;
/* SCL_0;
SDA_0;*/
}
//=====================================================
//FUNCTION:RANDOM READ.'add' IS THE ADDRESS WANT TO READ
uchar readbyte (uint add)
{
uchar temp;
P4DIR|=BIT1;
P4DIR|=BIT2;
start();
outbyte(IIC_WRITE);
nack();
outbyte(add>>8);
nack();
outbyte(add);
nack();
start();
outbyte(IIC_READ);
nack();
temp=inbyte();
nack();
stop();
P4DIR&=~BIT1;
P4DIR&=~BIT2;
/* SCL_0;
SDA_0;*/
return(temp);
}
//======================================================
void write_reg(uchar add,uchar wbyte)
{ P4DIR|=BIT1;
P4DIR|=BIT2;
start();
outbyte(REG_WRITE);
nack();
outbyte(add);
nack();
outbyte(wbyte);
nack();
stop();
P4DIR&=~BIT1;
P4DIR&=~BIT2;
/* SCL_0;
SDA_0;*/
}
//=================================================
uchar read_reg (uchar add)
{
uchar temp;
P4DIR|=BIT1;
P4DIR|=BIT2;
start();
outbyte(REG_WRITE);
nack();
outbyte(add);
nack();
start();
outbyte(REG_READ);
nack();
temp=inbyte();
nack();
stop();
P4DIR&=~BIT1;
P4DIR&=~BIT2;
/* SCL_0;
SDA_0;*/
return(temp);
}
void RTC_init()
{
write_reg(0,0x00);//消除标志位
write_reg(1,0);//启动晶体
}
/*
void one_msc(void) interrupt 1 using 2 //time 0
{
uint i;
TH0=-(100/256);
TL0=-(100%256);
test=~test;
for(i=0;i<23500;i++){;}
}*/
//=======================================================
//THIS IS A TEST OPERATION
/*
void main(void)
{
uchar i;
numtime=0;
/*TMOD=0X21;
TL1=0XF3;
TH1=0XF3;
TH0=-(100/256);
TL0=-(100%256);
TCON=0X50;
SCON=0X50;
PCON=0X00;//0X80;//2400bps/4800bps
IE=0X96;
WP=0;*/
/*
write_reg(0,0x00);
write_reg(1,0); //启动震荡器
//==================
write_reg(0,2); //写RTC设置 w==1
write_reg(2,1); //秒
write_reg(3,2); //分
write_reg(4,3); //小时
write_reg(5,4); //星期
write_reg(6,5); //日
write_reg(7,6); //月
write_reg(8,7); //年
write_reg(0,0); //启动
//======================
write_reg(0x0b,0x03);
while(1)
{
//num++;
if(++numtime>200) numtime=0;
for(i=0;i<32;i++)//WRITE DATA
{
writebyte(i,numtime);
//i=10;
//while(i--){;}
}
for(i=0;i<32;i++)//READ DATA AND SAVE IN buff ARRAY
{
buff[i]=readbyte(i);
}
/*for(i=0x11;i<0x19;i++)
{
write_reg(i,i*2);
}*/
/*
write_reg(0,1);
for(i=0;i<0x19;i++)
{
reg[i]=read_reg(i);
}
write_reg(0,0);
//for(;;){;}
}
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -