Code: Select all
//Declare var
unsigned temp1, temp_res,temp_set;
unsigned short dvi, chuc;
unsigned t;
unsigned short hour, minute, sec;
const unsigned short a[10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void interrupt (void) {
t++ ;
PIR1.TMR1IF = 0; // clear TMR1IF
TMR1H = 0x00;
TMR1L = 0x00;
}
// Init
void khoi_tao () {
//khoi tao PORTA
TRISA = 0xFF; // PORTA is input
ADCON1 = 0x8E; // Configure analog inputs and Vref AN0: Input analog
PORTA = 0; //Fo/4; RA7-RA1 Digital input
// Khoi tao PORTB
PORTB = 0;
TRISB = 0;
//Khoi tao PORTD
PORTD = 0;
TRISD = 0x00; //PORTD output
// PORTC
TRISC = 0x00;
PORTC = 0x00;
// Khoi tao TMR1
T1CON = 1; // Timer1 settings
PIR1.TMR1IF = 0; // clear TMR1IF
TMR1H = 0x00; // TMR1 = 0;
TMR1L = 0x00;
PIE1.TMR1IE = 1; // enable Timer1 interrupt
INTCON = 0xC0; // Set GIE, PEIE
// Varial
t = 0;
hour = 0;
minute = 0;
sec = 0;
dvi = 0;
chuc = 0;
temp1 = 0;
temp_res = 0;
}
// Read Temp
void read_temp () {
unsigned short i;
unsigned temp_T;
unsigned short temp_2;
unsigned short temp_new;
temp_T = 0;
for (i = 0; i<10; i++) {
temp_res = Adc_Read(0);
Delay_1us();
temp_2 = (temp_res / 2)-1;
temp_T = temp_T + temp_2;
}
temp_new = temp_T/10; //
temp1 = (temp1 + temp_new)/2; //Lay trung binh lan doc dau va lan sau
}
// Convert BCD to display on LED7
void convert_bcd (unsigned short x) {
dvi = x % 10;
chuc = x/10;
delay_us (10);
}
// Display
void hien_thi() {
//temp:
if (!(PORTA.F1)) convert_bcd (temp_set);
else convert_bcd (temp1);
PORTB = a[chuc];
PORTD.F0 = 1;
Delay_500us ();
PORTD.F0 = 0;
PORTB = a[dvi];
PORTD.F1 = 1;
Delay_500us ();
PORTD.F1 = 0;
// second
convert_bcd (sec);
PORTB = a[chuc];
PORTD.F2 = 1;
Delay_500us ();
PORTD.F2 = 0;
PORTB = a[dvi];
PORTD.F3 = 1;
Delay_500us ();
PORTD.F3 = 0;
// minute
convert_bcd (minute);
PORTB = a[chuc];
PORTD.F4 = 1;
Delay_500us ();
PORTD.F4 = 0;
PORTB = a[dvi];
PORTD.F5 = 1;
Delay_500us ();
PORTD.F5 = 0;
// Hour
convert_bcd (hour);
PORTB = a[chuc];
PORTD.F6 = 1;
Delay_500us ();
PORTD.F6 = 0;
PORTB = a[dvi];
PORTD.F7 = 1;
Delay_500us ();
PORTD.F7 = 0;
}
// Set the temp
void set_temp () {
/* UP */
delay_ms (10);
if (Button(&PORTA, 2, 100, 1)) {
temp_set++;
delay_ms (10);
}
//down
if (Button(&PORTA, 3, 100, 1)) {
temp_set --;
}
}
/* PWM */ //dieu che xung ra
void dieu_che () {
//Level 1
if (temp1 <=(temp_set)) {
delay_ms (10);
PORTC.F3 = 1;
PORTC.F4 = 1; // Enable Motor1
PORTC.F5 = 0; // DisEnable Motor2
}
//Level 2
if ((temp1 > temp_set)&& (temp1 <= (temp_set + 2))) {
PORTC.F3 = 0; // Dung sinh nhiet
PORTC.F4 = 1; // Enable Motor1
PORTC.F5 = 0; // DisEnable Motor2
}
// Level 3
if (temp1 > (temp_set + 2)) {
// Qua nhiet, mo quat hut, dung sinh nhiet
PORTC.F3 = 0; // Dung sinh nhiet
PORTC.F4 = 1; // mo quat thoi
PORTC.F5 = 1; // mo quat hut
}
}
// Caculate the time
void Cal_Time () {
if (t == 38) { // each interrupt TMR1 = 26ms
sec++;
t = 0;
}
if (sec == 60) {
minute ++;
sec = 0;
}
if (minute == 60) {
hour ++;
minute = 0;
}
if (hour == 99) {
hour = 99;
minute = 99;
sec = 99;
}
}
//
/////////////////////////////////////////////////////
void main() {
khoi_tao ();
delay_us (10);
temp_set = 60;
temp_res = Adc_Read(0);
delay_us (5);
temp1 = temp_res/2;
temp1 = temp1 - 1;
while (1) {
if (!(PORTA.F1)) set_temp ();
else {
read_temp ();
hien_thi();
}
Cal_Time ();
Delay_10us;
hien_thi();
Delay_10us;
dieu_che ();
}
}