CHƯƠNG TRÌNH 2 CẢM BIẾN
/*
* File: newmain.c
* Author:
*
* Created on April 13, 2015,
*
* dung pic16f877a, thach anh 4MH, dien tro keo len portb.
* 2 cam bien kich muc thap:RBO, RB1
* khi co nguoi vao bat den, co nguoi ra tat den: RB7
* bao so nguoi ra vao len led7 doan: PORTD quet led RC0,RC1.
* LED ANOT
* RB4 lay max ra den va ra switch
*/
#include <htc.h>
#include <math.h>
__CONFIG(WDTDIS & PWRTEN & UNPROTECT & BORDIS & LVPDIS);
#define _XTAL_FREQ 4000000
#define DEN
RB7
#define S1
RB0
#define S2
RB1
int N=0,CNN;
int chuc_dec,donvi_dec,chuc_hex,donvi_hex;
void tach_so(int CNN)
{
chuc_dec=CNN/10;
donvi_dec=CNN%10;
}
void dec_to_hex()
{
int i;
const unsigned char a[10]= {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
i=chuc_dec;
chuc_hex=a[i];
i=donvi_dec;
donvi_hex=a[i];
}
void quetled_7doan()
{
unsigned char a[2];
int mabandau,i;
a[1]=chuc_hex;
a[0]=donvi_hex;
mabandau = 0b00000001;
for (i=0;i<=1;i++)
{
PORTC = mabandau;
PORTD = a[i];
__delay_ms(1);
PORTC = 0X00; //SAU KHI DELAY DE MAT NHIN THAY ROI XOA DE TAT,
//NEU KO XOA THI TIEP TUC CHUONG TRINH SE QUET QUA 7SEG KHAC NHUNG
// CHUA DUA MANG VAO NEN LED 7SG THU 2 SE CO KI TU GIONG LED 7SEG THU1.
mabandau = mabandau<<1;
}
}
void portb_init()
{
TRISB=0X0F;
PORTB=0X00;
RBPU=0;
}
void portc_init()
{
TRISC=0X00;
PORTC=0X00;
}
void portd_init()
{
TRISD=0X00;
PORTD=0X00;
}
void main(void)
{
portb_init();
portc_init();
portd_init();
tach_so(CNN);
dec_to_hex();
while(1)
{tach_so(CNN);
dec_to_hex();
quetled_7doan();
switch(N)
{
case 0:
if(!S1)
//DI VAO
{
//S1=1;
N=1;
}
if(!S2)
{
//S2=1;
N=5;
}
break;
case 1:
if(!S2)
{
// S2=1;
N=2;
}
if(S1)
{
// S1=0;
N=0;
}
break;
case 2:
if(S1)
//DI RA
{
// S1=0;
N=3;
}
if(S2)
{
// S2=0;
N=1;
}
break;
case 3:
if(S2)
{
//S2=0;
N=0;
CNN++;
DEN=1;
}
if(!S1)
{
// S1=0;
N=2;
}
break;
case 5:
if(!S1)
{
// S1=1;
N=6;
}
if(S2)
{
// S2=0;
N=0;
}
break;
case 6:
if(S2)
{
// S2=0;
N=7;
}
if(S1)
{
// S2=1;
N=5;
}
break;
case 7:
if(S1)
{
// S1=0;
N=0;
CNN--;
if(CNN==0)
DEN=0;
}
if(!S2)
{
// S1=0;
N=6;
}
break;
}
}
}