Học liệu

ĐỒNG HỒ THÔNG MINH VỚI SÓNG RADIO

  • 13/01/2021
  • Học liệu

 

#include "msp430x22x4.h"
#define XTAL 55000
char sec_,sec_c,sec_dv,min_,min_c,min_dv,h_dv,h_c,h_;
char mode=0;
const char tab[]={0x40,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
void write(char dat);
void latch(void);

//=================================START MAIN=========================
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
//---------------------cau hinh xung clock------------------

BCSCTL1=CALBC1_1MHZ;// CHON TAN SO CLOCK DCLOCK_1MHZ
DCOCTL=CALDCO_1MHZ;
BCSCTL3&=~XTS;
BCSCTL1 |=SELM_1;
//---------------------cau hinh timer_a--------------------
TACCTL0 |= CCIE;
TACCR0 = XTAL;
TAR = 0;
TACTL |= TASSEL_2 + MC_2+ID_3;
//---------------------khai bao ngat port1---------------------------
P1DIR = 0x07; // dinh huong port
P1OUT |=0x78; //Chon keo len hay keo xuog
P1IE |= 0x08; // chon p1.4 lam ngat
P1IES |= 0x08; // chon p1.4 lam ngat canh
P1REN |= 0x78; //chon dien tro keo len
P1IFG &= ~0x08; // P1.4 IFG cleared

//---------------------khai bao bien h,m,s-----------------------------
min_=0;
min_c=0;
min_dv=0;
sec_=0;
sec_c=0;
sec_dv=0;
h_=0;
h_dv=0;
h_c=0;
while(1)
{
__bis_SR_register(LPM1_bits+GIE); // cai dat MCU che do tiet liem nag luong
if(++sec_>59)
{sec_=0;
if(++min_>59)
{min_=0;
if(++h_>12)
{h_=0;
}
}
}
//------------------tach hang chuc va don vi-------------------------
h_c=h_/10;
h_dv=h_%10;
min_c=min_/10;
min_dv=min_%10;
sec_c=sec_/10;
sec_dv=sec_%10;
//---------------------hien thi----------------------------------
write(tab[sec_dv]);
write(tab[sec_c]);
write(tab[min_dv]);
write(tab[min_c]);
write(tab[h_dv]);
write(tab[h_c]);
latch();
}
}
//=================================END MAIN========================================

// ----------------------thiet lap ngat TIMER---------------------------------
#pragma vector= TIMERA0_VECTOR
__interrupt void Timer_A(void)
{
TACCR0+=XTAL;
__bic_SR_register_on_exit(CPUOFF);
}
//---------------------end ngat timer------------------------
//-----------------------------Chuong trinh ngat port1.4-----------------------
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
char p1_i=(P1IN&0x20);
char p1_d=(P1IN&0x40);
while(mode<3)
{ P1IFG&= ~0x08;
char p1_s=(P1IN&0x10);
if(p1_s==0)
mode++;
switch(mode)
{case 0:

{if(p1_i==0)
min_=min_+1;
if(p1_d==0)
min_=min_-1;
else
{
min_c=min_/10;
min_dv=min_%10;
write(tab[sec_dv]);
write(tab[sec_c]);
write(tab[min_dv]);
write(tab[min_c]);
write(tab[h_dv]);
write(tab[h_c]);
latch();
}

}
break;
case 1:
{mode=4;
}
break;
case 2:
{mode=4;
}
break;
}
}


P1IFG&= ~0x08;
}
//-----------------------------ket thuc hcuong trinh ngat port1-----------------
//--------------------------DK HC595--------------------------------------------
void write(char dat)
{
char i;
P1DIR |= 0x07;
for(i=0;i<8;i++)
{
P1OUT &=0xFE;
P1OUT |=(dat&0x80)>>7;
dat<<=1;
//------------TAO XUNG CK---------------
P1OUT &=~0x02;
P1OUT |= 0x02;
}
}
void latch(void)
{
P1OUT &=~0x04;
P1OUT |= 0x04;
}
//--------------------------------end dk 595--------------------------
//========================================THE END==============================

Các tin khác