Bài 8: PIC Đồng hồ điện tử chính xác
Bài 8: PIC Đồng hồ điện tử chính xác
Bài 8: PIC Đồng hồ điện tử chính xác
Bài 8: PIC Đồng hồ điện tử chính xác
Bài 8: PIC Đồng hồ điện tử chính xác
Bài 8: PIC Đồng hồ điện tử chính xác
Tài liệu
Bài 8: PIC Đồng hồ điện tử chính xác
Yêu cầu: Thiết kế đồng hồ bằng led 7 đoạn
Thiết bị sử dụng
- PIC 16F887
- 6 IC 74HC595
- 6 Led7 đoạn
Mạch đi dây

Chương trình điều khiển
#define DATA PIN_C1 //DS
#define LATCH PIN_C3 //ST_CP
#define CLOCK PIN_C0 //SH_CP
#define SETTING PIN_D0
#define UP PIN_D1
int giaima7doan[]={63,6,91,79,102,109,125,7,127,111};
//0 1 2 3 4 5 6 7 8 9
unsigned int8 giay ,phut,gio,bdn, bdn1 ;
int tt1 = 0;
int tt2 = 0;
int phase = 0;
#int_timer1
void interrupt_timer1()
{
bdn++;
bdn1++;
set_timer1(3060);
}
void IC_74hc595(int dulieu) //dua du lieu vao 74hc595
{
int i;
output_low(CLOCK);
for(i=0;i<=7;i++)
{
if((dulieu & 0x80)==0)
output_low(DATA);
else
output_high(DATA);
dulieu=dulieu<<1;
output_high(CLOCK);
output_low(CLOCK);
}
}
void hienthi2so(int num) //dua du lieu vao 2 led7doan
{
int a = num/10;
int b = num%10;
IC_74hc595(giaima7doan[b]);
IC_74hc595(giaima7doan[a]);
}
void phim_up() //tang gio/phut/giay
{
if(input(UP)==0) tt1 =0;
if((input(UP)==1)&&(tt1==0))
{
delay_ms(10);
if(input(UP)==1)
{
tt1 = 1;
if( phase == 1)
{
giay = giay+1;
if (giay == 60) giay = 0;
}
if( phase == 2)
{
phut = phut+1;
if (phut == 60) phut = 0;
}
if( phase == 3)
{
gio = gio+1;
if (gio == 24) gio = 0;
}
}
}
}
void phim_setting() //chuyen che do cai dat
{
if(input(SETTING)==0) tt2 =0;
if((input(SETTING)==1)&&(tt2==0))
{
delay_ms(10);
if(input(SETTING)==1)
{
tt2 = 1;
phase++;
if (phase == 4) phase = 0;
}
}
}
void choptat() //chop tat khi cai dat
{
if (bdn1<2)
{
if (phase != 0)
{
hienthi2so(giay);
hienthi2so(phut);
hienthi2so(gio);
output_low(LATCH);
output_high(LATCH);
}
}
if (bdn1>=2)
{
if (phase == 1)
{
IC_74hc595(0);
IC_74hc595(0);
hienthi2so(phut);
hienthi2so(gio);
output_low(LATCH);
output_high(LATCH);
}
if (phase == 2)
{
hienthi2so(giay);
IC_74hc595(0);
IC_74hc595(0);
hienthi2so(gio);
output_low(LATCH);
output_high(LATCH);
}
if (phase == 3)
{
hienthi2so(giay);
hienthi2so(phut);
IC_74hc595(0);
IC_74hc595(0);
output_low(LATCH);
output_high(LATCH);
}
}
if (bdn1>4)
{
if (phase != 0)
{
bdn1=0;
}
}
}
void main()
{
setup_timer_1 (t1_internal|t1_div_by_8);
set_tris_c(0x00);
set_tris_d(0xff);
set_timer1(3036);
enable_interrupts(global);
enable_interrupts(int_timer1);
giay = phut = gio = 0;
bdn=0;
while(TRUE)
{
choptat();
phim_up();
phim_setting();
if (phase == 0)
{
if (bdn<10)
{
hienthi2so(giay);
hienthi2so(phut);
hienthi2so(gio);
output_low(LATCH);
output_high(LATCH);
}
else
{
bdn = bdn-10;
giay++;
if (giay==60)
{
giay=0;
phut++;
if (phut == 60)
{
phut = 0;
gio++;
if (gio == 24) gio = 0;
}
}
}
}
}
}







