/*------------------------------------------------------------------+
| *Chuong trinh PIC16F887 va VU Meter __BK_PAW |
| *Ngay viet: 12/2010 |
+-------------------------------------------------------------------+---------------+
+Mo ta phan cung: |
| Mach hien thi do lon 3 dai tan so am thanh dung phuong phap chot led |
| Dai tan so thu nhat:
| nhan tin hieu analog tu pin RE0
| xuat ra 8 bit du lieu: pin RA0 va 7 pin port B gom RB1->RB7
| Dai tan so thu hai:
| nhan tin hieu analog tu pin RE1
| xuat ra cac chan PORTC
| Dai tan so thu hai:
| nhan tin hieu analog tu pin RE2
| xuat ra cac chan PORTD
| Pin RB0 dung lam ngat ngoai de chuyen doi kieu hien thi |
| Xuat muc 1 den sang, muc 0 den tat
+Mach co 8 trang thai hien thi gom: |
| 0: am thanh cang lon thi cang nhieu den sang
| 1: giong tthai 1 nhung sau 1,5s thi den se dich phai 1 bit
| su dung bien k de kiem soat so lan dich nghia la cu sau 2s vao chuong trinh ngat timer1
| thi bien k se duoc tang len 1. Moi lan vao chuong trinh doi thi du lieu se duoc dich lai
| tu dau su dung bien k.
| 2: giong tthai 2 nhung den se dich trai 1 bit
| 3: am thanh cang manh thi den nhay cang nhanh (ca 8 den nhay 1 luc)
| 4: giong tthai 3 nhung den se sang tat xen ke
| 5: am thanh cang lon thi cang it den sang (nguoc lai tthai 1)
| 6: giong tthai 3 nhung tin hieu doc duoc phai lon hon 1/2 tam toan thang thi moi sang
| 7: ket hop 3 tthai so 4,1,3 |
+----------------------------------------------------------------------------------*/
#include <htc.h>
__CONFIG(XT & WDTDIS & PWRTEN & MCLREN & UNPROTECT & SWBOREN & IESODIS & FCMDIS & LVPDIS & DEBUGDIS); //1st config. Word
__CONFIG(BORV21); //2st config. Word
#define _XTAL_FREQ 4000000 //tan so thach anh 4Mhz
//khai bao tan so thach anh dung cho lenh delay
/***********************************************************************+
| VARIABLES |
+-----------------------------------------------------------------------*/
unsigned char k; //luu so lan da dich trai/phai
unsigned char trangthai; //kieu chay
unsigned char laptm1; //luu so lan lap timer 1
unsigned int gtthap,gtgiua,gtcao; //luu gia tri de hien thi dung trong trang thai 3 va 4
unsigned int tgthap,tggiua,tgcao; //luu thoi gian sang tat den dung trong trang thai 3 va 4
/***********************************************************************+
| SUBROUTINES |
+-----------------------------------------------------------------------*/
void init_port();
void init_adc();
void init_int();
unsigned int ReadADC();
unsigned char doi(unsigned int tmp, unsigned char trangthaitam);
unsigned char dichtrai(unsigned char dulieut);
unsigned char dichphai(unsigned char dulieup);
/***********************************************************************+
| MAIN PROGRAM |
+-----------------------------------------------------------------------*/
void main()
{
unsigned int thap,giua,cao; //cac bien luu du lieu doc duoc tu cac pin ADC
unsigned char xthap,xgiua,xcao; //cac bien luu du lieu se xuat ra port
unsigned char tgthaptam,tggiuatam,tgcaotam; /*cac bien tam luu thoi gian de so sanh voi
thoi gian dang su dung dung trong tthai 3 va 4*/
init_port();
init_int();
init_adc();
k=0; //chua dich lan nao
while(1){
/*-----------------------------------------------------------------------
TRANG THAI 0 1 2 5
-----------------------------------------------------------------------*/
if(trangthai==0|trangthai==1|trangthai==2|trangthai==5) //trang thai 0,1,2,5 xu ly so den sang
{
//-------------------------xu ly dai tan so thu nhat------------------------
CHS0=1; //doc chan AN5
CHS1=0;
thap=ReadADC();
xthap=doi(thap,trangthai);
if((xthap%2)==1) RA0=1; //chuyen RB0 thanh RA0
else RA0=0;
PORTB=xthap;
__delay_ms(10);
//-------------------------xu ly dai tan so thu hai------------------------
CHS0=0; //doc chan AN6
CHS1=1;
giua=ReadADC();
xgiua=doi(giua,trangthai);
PORTC=xgiua;
__delay_ms(10);
//-------------------------xu ly dai tan so thu ba------------------------
CHS0=1; //doc chan AN7
cao=ReadADC();
xcao=doi(cao,trangthai);
PORTD=xcao;
__delay_ms(10);
}
/*-----------------------------------------------------------------------
TRANG THAI 3 VA 4
-----------------------------------------------------------------------*/
else if(trangthai==3|trangthai==4) //trang thai 3 va 4 xu ly thoi gian
{
//-------------------------xu ly dai tan so thu nhat------------------------
CHS0=1; //doc chan AN5
CHS1=0;
thap=ReadADC();
tgthaptam=doi(thap,trangthai);
if(tgthaptam<tgthap)
tgthap=tgthaptam;
if(tgthap==0)
{ tgthap=19;
gtthap=~(gtthap); //lay dao portB
if((gtthap%2)==1) RA0=1; //chuyen RB0 thanh RA0
else RA0=0;
PORTB=gtthap;
}
else
{ tgthap--;
__delay_ms(20);
};
//-------------------------xu ly dai tan so thu hai------------------------
CHS0=0; //doc chan AN6
CHS1=1;
giua=ReadADC();
tggiuatam=doi(giua,trangthai);
if(tggiuatam<tggiua)
tggiua=tggiuatam;
if(tggiua==0)
{ tggiua=19;
gtgiua=~(gtgiua); //lay dao portC
PORTC=gtgiua;
}
else
{tggiua--;
__delay_ms(20);
};
//-------------------------xu ly dai tan so thu ba------------------------
CHS0=1; //doc chan AN7
cao=ReadADC();
tgcaotam=doi(cao,trangthai);
if(tgcaotam<tgcao)
tgcao=tgcaotam;
if(tgcao==0)
{ tgcao=19;
gtcao=~(gtcao); //lay dao portD
PORTD=gtcao;
}
else
{tgcao--;
__delay_ms(20);
};
}
/*-----------------------------------------------------------------------
TRANG THAI 6
-----------------------------------------------------------------------*/
else if(trangthai==6) //trang thai 6
{
//-------------------------xu ly dai tan so thu nhat------------------------
CHS0=1; //xu ly xuat port B
CHS1=0;
thap=ReadADC();
tgthaptam=doi(thap,trangthai);
if((tgthaptam<10)|(tgthap!=10))
{
if(tgthaptam<10)
{if(tgthaptam<tgthap) /*neu thoi gian lay duoc nho hon thoi gian dang su dung
thi dung thoi gian moi lay duoc de su dung*/
tgthap=tgthaptam;
};
if((gtthap%2)==1) RA0=1; //chuyen RB0 thanh RA0
else RA0=0;
PORTB=gtthap;
if(tgthap==0)
{ tgthap=10;
gtthap=~(gtthap); //lay dao portB
if((gtthap%2)==1) //chuyen RB0 thanh RA0
RA0=1;
else
RA0=0;
PORTB=gtthap;
}
else
{ tgthap--;
__delay_ms(21);
};
}
else
{
PORTB=0;
RA0=0;
gtthap=0xff;
};
//-------------------------xu ly dai tan so thu hai------------------------
CHS0=0; //xu ly xuat port C
CHS1=1;
giua=ReadADC();
tggiuatam=doi(giua,trangthai);
if((tggiuatam<10)|(tggiua!=10))
{
if(tggiuatam<10)
{
if(tggiuatam<tggiua)
tggiua=tggiuatam;
};
PORTC=gtgiua;
if(tggiua==0)
{ tggiua=10;
gtgiua=~(gtgiua); //dao port C
PORTC=gtgiua;
}
else
{tggiua--;
__delay_ms(21);
};
}
else
{
PORTC=0;
gtgiua=0xff;
};
//-------------------------xu ly dai tan so thu ba------------------------
CHS0=1; //xu ly xuat port D
cao=ReadADC();
tgcaotam=doi(cao,trangthai);
if((tgcaotam<10)|(tgcao!=10))
{
if(tgcaotam<10)
{
if(tgcaotam<tgcao)
tgcao=tgcaotam;
};
PORTD=gtcao;
if(tgcao==0)
{ tgcao=10;
gtcao=~(gtcao); //lay dao portD
PORTD=gtcao;
}
else
{tgcao--;
__delay_ms(21);
};
}
else
{
PORTD=0;
gtcao=0xff;
};
} //ket thuc trang thai 6
/*-----------------------------------------------------------------------
TRANG THAI 7
-----------------------------------------------------------------------*/
else if(trangthai==7) //bat dau trang thai 7 (hon hop)
{
CHS0=1;
CHS1=0;
thap=ReadADC();
tgthaptam=doi(thap,4);
if(tgthaptam<tgthap)
tgthap=tgthaptam;
if(tgthap==0)
{ tgthap=19;
gtthap=~(gtthap); //lay dao portB
if((gtthap%2)==1) //chuyen RB0 thanh RA0
RA0=1;
else
RA0=0;
PORTB=gtthap;
}
else
{tgthap--;
__delay_ms(20);
};
CHS0=0;
CHS1=1;
giua=ReadADC();
xgiua=doi(giua,1);
PORTC=xgiua;
__delay_ms(10);
CHS0=1;
cao=ReadADC();
tgcaotam=doi(cao,3);
if((tgcaotam<10)|(tgcao!=10))
{
if(tgcaotam<10)
{
if(tgcaotam<tgcao)
tgcao=tgcaotam;
};
PORTD=gtcao;
if(tgcao==0)
{ tgcao=10;
gtcao=~(gtcao); //lay dao portD
PORTD=gtcao;
}
else
{tgcao--;
__delay_ms(21);
};
}
else
{
PORTD=0;
gtcao=0xff;
};
}; //ket thuc trang thai 7
}; //ket thuc vong lap while
}