Học liệu

Lập trình thời gian thực với MSP430

  • 17/07/2019
  • Học liệu

#include "string.h"
#include "msp430g2452.h"
#include <intrinsics.h>

union reg {
struct bit {
unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
}_BIT;
unsigned char _BYTE;
};
union reg* _P2_DIRECT = (union reg*)0x2A ;
union reg* _P2_OUT = (union reg*)0x29 ;
union reg* _P2_IN = (union reg*)0x28 ;
union reg* _P1_DIRECT = (union reg*)0x22 ;
union reg* _P1_IN = (union reg*)0x20 ;
union reg* _P1_OUT = (union reg*)0x21 ;
union reg* _P2_SEL = (union reg*)0x2E ;

//#define LCDdata (P2OUT)
#define SEC 0x00
#define MIN 0x01
#define HOUR 0x02
#define DAY 0x03
#define DATE 0x04
#define MONTH 0x05
#define YEAR 0x06
#define A_MIN 0x08
#define A_HOUR 0x09
#define ON_OFF 0x0A
#define DQ_V 0x0B //bien so sanh voi temp
#define OF_DQ 0x0C //bien bat tat chuc nang canh bao
#define MODE_DQ 0x0D //bien cho che do neu lon thi bao dong hoc nho hon thi bao dong

#define SCLOUT (_P1_OUT->_BIT.b6)
#define SDAIN (_P1_IN->_BIT.b7)
#define SDAOUT (_P1_OUT->_BIT.b7)

#define DQIN (_P2_IN->_BIT.b0)
#define DQOUT (_P2_OUT->_BIT.b0)

#define Set (_P2_IN->_BIT.b3)
#define Up (_P2_IN->_BIT.b4)
#define Sel (_P2_IN->_BIT.b5)

#define RS _P1_OUT->_BIT.b5
#define EN _P1_OUT->_BIT.b4

#define DATA_4 _P1_OUT->_BIT.b3
#define DATA_5 _P1_OUT->_BIT.b2
#define DATA_6 _P1_OUT->_BIT.b1
#define DATA_7 _P1_OUT->_BIT.b0

#define LOA _P2_OUT->_BIT.b1
#define LOA1 _P2_OUT->_BIT.b2

unsigned char sec,hour,min,day,date,month,year,year_20,set,a_min,a_hour;
int a,b,c,d;
char *s[]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
unsigned char chop=0,dem=0,sel,disp,on_off,dk,sw;
unsigned char s_hour,s_min,s_sec,tsec,go,bien;
unsigned int dq_v,of_dq,mode_dq;

void delay_1(void)
{
int i=20;
while(i--);
}
//*******************************************************************

//DS18B20
void delay_18B20(unsigned int i)
{
while(i--);
}

//=KHOI TAO CAM BIEN(RESET)
void Init_DS18B20()
{
_P2_DIRECT->_BIT.b0=1;
DQOUT = 1;
delay_18B20(8);
DQOUT = 0;
delay_18B20(80);
DQOUT = 1;
delay_18B20(14);
_P2_DIRECT->_BIT.b0=0;
delay_18B20(20);
}
//=DOC MOT BYTE
unsigned char Read_byte(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
_P2_DIRECT->_BIT.b0=1;
DQOUT = 0; //CHO DUONG DQ XUONG MUC THAP
dat>>=1;
DQOUT = 1; //CHO DUONG DQ LEN MUC CAO
_P2_DIRECT->_BIT.b0=0;
if(DQIN)
dat|=0x80;
delay_18B20(4);
}
return(dat);
}
//========================
//=CHUONG TRINH GHI 1BYTE
void Write_byte(unsigned char dat)
{
_P2_DIRECT->_BIT.b0=1;
unsigned char i=0;
for (i=8; i>0; i--)
{
DQOUT = 0;
DQOUT = dat&0x01;
delay_18B20(5);
DQOUT = 1;
dat>>=1;
}
}
//========================
//=CHUONG TRINH DOC NHIET DO TU CAM BIEN
unsigned int ReadTemperature(void)
{
unsigned int t;
unsigned char a;
unsigned char b;

Init_DS18B20();
Write_byte(0xCC);
Write_byte(0x44);
delay_18B20(100);
Init_DS18B20();
Write_byte(0xCC);
Write_byte(0xBE);
a=Read_byte();
b=Read_byte();
t = (b*256+a)*25;
return( t >> 2 );
}

//****************************************************************
//DS1307
//****************************************************************
void I2C_start(void)
{
_P1_DIRECT->_BIT.b6 =1;
_P1_DIRECT->_BIT.b7 =1;
SDAOUT = 1; SCLOUT = 1;
delay_1();
SDAOUT = 0; delay_1();
SCLOUT = 0; delay_1();
}
void I2C_stop(void)
{
_P1_DIRECT->_BIT.b6 =1;
_P1_DIRECT->_BIT.b7 =1;
SCLOUT = 1; SCLOUT = 0;
SDAOUT = 0; delay_1();
SCLOUT = 1; delay_1();
SDAOUT = 1;
}
unsigned char I2C_write(unsigned char dat)
{
_P1_DIRECT->_BIT.b6 =1;
_P1_DIRECT->_BIT.b7 =1;

unsigned char i;
for (i=0;i<8;i++)
{
SDAOUT = (dat & 0x80) ? 1:0;
SCLOUT=1;SCLOUT=0;
dat<<=1;
}
SCLOUT = 1; delay_1();
SCLOUT = 0;
return dat;

Các tin khác