Cho em hỏi là:Phần code về quét ma trận phím.Em dùng PORTD, đã mô phỏng quét thành công.Đến khi ra mạch thật cắm led vào.Nó sáng hết cả 4 bit cao dùng để quét.Em không hiểu phần set fuse có chỗ nào liên quan đến cái PORT D này không a.Em đã đặt IN OUT cho PORT D rồi.Thôi.EM post cả code luôn.Các bác chịu khó xem dùm em a.Em thanks nhiệt tình
/************************************************** ***
This program was produced by the
CodeWizardAVR V1.24.8d Professional
Automatic Program Generator
© Copyright 1998-2006 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 16/02/2009
Author : F4CG
Company : F4CG
Comments:
Chip type : ATmega16
Program type : Application
Clock frequency : 8,000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
************************************************** ***/
#include <mega16.h>
#include <delay.h>
#asm
.equ __w1_port=0x15
.equ __w1_bit=6
#endasm
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
#include <delay.h>
#include <ds18b20.h>
#include <stdio.h>
unsigned char lock= 0;
#define MAX_DEVICES 8
unsigned char rom_code[MAX_DEVICES][9];
// c ok start input new
const unsigned char key_map[] = // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{0x84,0x18,0x14,0x12,0x28,0x24,0x22,0x48,0x44,0x42 ,0x88,0x82,0x11,0x21,0x41,0x81 };
/*mang nay chua gia tri cua phim nhap vao. chuoi ma nay la ma khoa
trong do gia tri dau tien cua mang la truong ID. Cho biet so luong so trong mang*/
unsigned char key_data [17]= {0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char key_id[17] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff ,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
//-------------------------------------------------------------
unsigned char key_scan()
{
// chuong trinh con nhan phim tu matrix phim 16 phim
unsigned char i = 4,KEY_ID; // gia tri i la thu tu bit = 1
PORTD = 0x00; // gia tri mac dinh la 0x00;
DDRD = 0xF0; //nua 4 bit cao la OUT, 4 bit thap la IN
while (1)
{
PORTD = 1<<i; //tien hanh quet bit cua 4 bit cao trong portA
i++;
delay_ms(30);
if (i==8) i = 4;
if (PIND !=PORTD)
{
KEY_ID = PIND; // ma cua phim duoc bam
while (PIND != PORTD); // chong dinh phim
for (i = 0; i<16; i++) // thuc hien tim ma key
if (key_map[i] == KEY_ID) return i ;//Neu ma giong nhau thi tra lai ma cua key
}
}
}
void lcd_view()
/* Hien thi mang key_data len LCD
chi Hien thi gia tri tu 0 - 9 . gia tri rong~ la 0xFF
*/
{
unsigned char j;
lcd_clear();
lcd_putsf("KEY:");
lcd_gotoxy(0,1);
for (j = 1; j<=16; j++)
{
if (key_data [j] == 0xFF) break;
lcd_putchar(key_data[j]+48);
}
}
unsigned char compare_array()
/* ham compare_array lam nhiem vu so sanh 2 xau key_data va key_id
neu 2 xau bang nhau thi gia tri tra ve la 1,
neu khac nhau thi gia tri tra ve = 0 */
{
unsigned char i;
for (i =0 ; i<=16; i++)
if (key_data[i] != key_id[i]) return 0;
return 1;
}
void clear_array()
//xoa toan bo key_data de nhap cho bo gia tri moi
{
unsigned char i;
key_data[0]= 0;
for (i = 1; i<= 16; i++)
key_data[i] = 0xff;
}
//-------------------------------------------------
void main(void)
{
// Declare your local variables here
unsigned char key_code, i,key_count=0, new_key_flag= 0;
int eeprom *pointer;
PORTA=0x00;
DDRA=0xFF;
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0xF0;
// LCD module initialization
lcd_init(16);
i = 0;
//Khi chuong trinh bat dau chay ta phai LOAD du lieu tu eeprom ra mang key_id
pointer = 0x0000;// dia chi dau trong eeprom
for (i = 0; i<=16; i++)
{
key_id[i] = *pointer;//tien hanh load gia tri ra
delay_ms(20);
pointer++;
}
//-----------------------
//LCD hien len dong chu Starting...
lcd_putsf("STARTING10");
delay_ms(500);//thuc hien tre
lcd_putchar ('.'); //phan hien thi giao dien
delay_ms(500);//thuc hien tre
lcd_putchar ('.');
delay_ms(500);//thuc hien tre
lcd_putchar ('.');
delay_ms(500);//thuc hien tre
lcd_clear();
lcd_putsf("LOCK DIGITAL !");
while (1)
{
key_code = key_scan();
switch (key_code)
{
case 0: // button num 0 -9
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
{
key_count = key_data[0]; //key_count luon tro den o gia tri cuoi cung trong mang.
if (key_count== 16) break;
key_count++; //mang trong thi key_count = 0
key_data[key_count]=key_code;
key_data[0] = key_count;
lcd_view();//hien thi xau ra man hinh luon
break;
}
case 10: // button DELETE
{
key_count = key_data[0];//lay gia tri cuoi cua mang
if (key_count == 0)break;
key_data[key_count] = 0xFF;//thiet lap gia tri rong
key_count--; //giam bien con tro cuoi
key_data[0] = key_count;//ghi vao truong ID
lcd_view();
break;
}
case 11: //button Enter
{
lcd_clear();//xoa man hinh lcd
if (new_key_flag == 1)//Neu dang trong che do nhap ma Key_id moi thi ta ghi key_id vao trong eeprom
{
if (key_data[0] <5)
{
lcd_putsf("NOT ENOUGH CODE !");
delay_ms(1000);
lcd_view();
break; //tro ve cho chon phim
}
pointer = 0x0000; //thiet lap con tro eeprom ve dia chi dau
for (i= 0; i<=16; i++)
{
key_id[i] = key_data[i];
*pointer = key_id[i];
pointer++;
}
clear_array();//xoa bo dem key_data;
new_key_flag = 0;
PORTD.0 = 0; //led thong bao New Key_ID
//--------------
lcd_putsf("SUCCESS !");
break; //tro ve cho chon phim
}
if (lock == 0) //Neu khoa dang mo thi lap tuc khoa lai
{
lock = 1;
lcd_putsf("LOCKED !");
clear_array();
break;
}
else //khi khoa dang dong thi tien hanh xem ma~ key. Neu dung thi mo khoa, Neu sau thi bao loi
{
if (compare_array())//ham so sanh 2 mang. true = 1, false = 0
{
lock = 0; //dung khoa mo ra
PORTA=0;
lcd_putsf("UNLOCKED !");//THONG bao khoa da mo
clear_array();
break;
}
else
{
//sai thi bao loi key khong dung
lcd_putsf("KEY INVALID !\n PLEASE PRESS AGAIN!");
clear_array();
break;
}
}
}
case 14: //button New Key_ID
//Neu khoa chua mo -> bao loi, yeu cau mo khoa
//Neu khoa mo roi -> thong bao nhap khoa moi
//hien thi 1 led bao dang nhap khoa moi
{
lcd_clear();
if (lock == 1)
{
lcd_clear();
lcd_putsf("locked!\n PLEASE UNLOCK");
break;
}
//nguoc lai thi khoa dang mo
lcd_putsf("PLEASE PRESS NEW\n KEY_ID!");
new_key_flag = 1;
PORTA.0 = 1; //led thong bao new key_id
break;
}
case 13: //button input key
{
lcd_clear();
lcd_putsf("INPUT KEY");
clear_array();
break;
}
case 15:
{
unsigned char j,devices;
int temp;
lcd_init(16);
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("NHIET DO PHONG:");
lcd_gotoxy(4,1);
//lcd_putchar(0xDF);
lcd_gotoxy(5,1);
lcd_putsf("C");
/************************************************** ************/
devices=w1_search(0xf0,rom_code);
delay_ms(700);
/************************************************** ************/
/************************************************** ************/
while (1)
{ //lcd_clear();
for (j=0;j<devices;j++)
{
lcd_gotoxy(1,1);
temp=ds18b20_temperature(&rom_code[j][0]);
lcd_putchar(temp/100+48);
lcd_putchar((temp%100)/10+48);
lcd_putchar(temp%10+48);
delay_ms(1200);
};
lcd_clear();
lcd_putsf("LOCK DIGITAL !");
break;
};
break;
}
case 12: //button start
{
unsigned char j;
lcd_clear();
lcd_putsf("System on");
lcd_clear();
lcd_putsf("STARTING");
delay_ms(500);//thuc hien tre
lcd_putchar ('.'); //phan hien thi giao dien
delay_ms(500);//thuc hien tre
lcd_putchar ('.');
delay_ms(500);//thuc hien tre
lcd_putchar ('.');
delay_ms(500);//thuc hien tre
lcd_clear();
lcd_putsf("ACTIVE !");
delay_ms(5000);
while(1)
{
if(PINC.0==1)
{
lcd_clear();
lcd_putsf("ALARM!");
lcd_putsf("\nRoom 1");
delay_ms(300);
PORTA.0=1;
}
if(PINC.1==1)
{
lcd_clear();
lcd_putsf("ALARM!");
lcd_putsf("\nRoom 2");
delay_ms(300);
PORTA.1=1;
}
if(PINC.2==1)
{
lcd_clear();
lcd_putsf("\nALARM!");
lcd_putsf("Room 3");
delay_ms(300);
PORTA.2=1;
}
if(PINC.7==1)
{
lcd_clear();
lcd_putsf("LOCK DIGITAL !");
break;
}
}
break;
}
}
}
}
This program was produced by the
CodeWizardAVR V1.24.8d Professional
Automatic Program Generator
© Copyright 1998-2006 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 16/02/2009
Author : F4CG
Company : F4CG
Comments:
Chip type : ATmega16
Program type : Application
Clock frequency : 8,000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
************************************************** ***/
#include <mega16.h>
#include <delay.h>
#asm
.equ __w1_port=0x15
.equ __w1_bit=6
#endasm
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
#include <delay.h>
#include <ds18b20.h>
#include <stdio.h>
unsigned char lock= 0;
#define MAX_DEVICES 8
unsigned char rom_code[MAX_DEVICES][9];
// c ok start input new
const unsigned char key_map[] = // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{0x84,0x18,0x14,0x12,0x28,0x24,0x22,0x48,0x44,0x42 ,0x88,0x82,0x11,0x21,0x41,0x81 };
/*mang nay chua gia tri cua phim nhap vao. chuoi ma nay la ma khoa
trong do gia tri dau tien cua mang la truong ID. Cho biet so luong so trong mang*/
unsigned char key_data [17]= {0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char key_id[17] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff ,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
//-------------------------------------------------------------
unsigned char key_scan()
{
// chuong trinh con nhan phim tu matrix phim 16 phim
unsigned char i = 4,KEY_ID; // gia tri i la thu tu bit = 1
PORTD = 0x00; // gia tri mac dinh la 0x00;
DDRD = 0xF0; //nua 4 bit cao la OUT, 4 bit thap la IN
while (1)
{
PORTD = 1<<i; //tien hanh quet bit cua 4 bit cao trong portA
i++;
delay_ms(30);
if (i==8) i = 4;
if (PIND !=PORTD)
{
KEY_ID = PIND; // ma cua phim duoc bam
while (PIND != PORTD); // chong dinh phim
for (i = 0; i<16; i++) // thuc hien tim ma key
if (key_map[i] == KEY_ID) return i ;//Neu ma giong nhau thi tra lai ma cua key
}
}
}
void lcd_view()
/* Hien thi mang key_data len LCD
chi Hien thi gia tri tu 0 - 9 . gia tri rong~ la 0xFF
*/
{
unsigned char j;
lcd_clear();
lcd_putsf("KEY:");
lcd_gotoxy(0,1);
for (j = 1; j<=16; j++)
{
if (key_data [j] == 0xFF) break;
lcd_putchar(key_data[j]+48);
}
}
unsigned char compare_array()
/* ham compare_array lam nhiem vu so sanh 2 xau key_data va key_id
neu 2 xau bang nhau thi gia tri tra ve la 1,
neu khac nhau thi gia tri tra ve = 0 */
{
unsigned char i;
for (i =0 ; i<=16; i++)
if (key_data[i] != key_id[i]) return 0;
return 1;
}
void clear_array()
//xoa toan bo key_data de nhap cho bo gia tri moi
{
unsigned char i;
key_data[0]= 0;
for (i = 1; i<= 16; i++)
key_data[i] = 0xff;
}
//-------------------------------------------------
void main(void)
{
// Declare your local variables here
unsigned char key_code, i,key_count=0, new_key_flag= 0;
int eeprom *pointer;
PORTA=0x00;
DDRA=0xFF;
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0xF0;
// LCD module initialization
lcd_init(16);
i = 0;
//Khi chuong trinh bat dau chay ta phai LOAD du lieu tu eeprom ra mang key_id
pointer = 0x0000;// dia chi dau trong eeprom
for (i = 0; i<=16; i++)
{
key_id[i] = *pointer;//tien hanh load gia tri ra
delay_ms(20);
pointer++;
}
//-----------------------
//LCD hien len dong chu Starting...
lcd_putsf("STARTING10");
delay_ms(500);//thuc hien tre
lcd_putchar ('.'); //phan hien thi giao dien
delay_ms(500);//thuc hien tre
lcd_putchar ('.');
delay_ms(500);//thuc hien tre
lcd_putchar ('.');
delay_ms(500);//thuc hien tre
lcd_clear();
lcd_putsf("LOCK DIGITAL !");
while (1)
{
key_code = key_scan();
switch (key_code)
{
case 0: // button num 0 -9
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
{
key_count = key_data[0]; //key_count luon tro den o gia tri cuoi cung trong mang.
if (key_count== 16) break;
key_count++; //mang trong thi key_count = 0
key_data[key_count]=key_code;
key_data[0] = key_count;
lcd_view();//hien thi xau ra man hinh luon
break;
}
case 10: // button DELETE
{
key_count = key_data[0];//lay gia tri cuoi cua mang
if (key_count == 0)break;
key_data[key_count] = 0xFF;//thiet lap gia tri rong
key_count--; //giam bien con tro cuoi
key_data[0] = key_count;//ghi vao truong ID
lcd_view();
break;
}
case 11: //button Enter
{
lcd_clear();//xoa man hinh lcd
if (new_key_flag == 1)//Neu dang trong che do nhap ma Key_id moi thi ta ghi key_id vao trong eeprom
{
if (key_data[0] <5)
{
lcd_putsf("NOT ENOUGH CODE !");
delay_ms(1000);
lcd_view();
break; //tro ve cho chon phim
}
pointer = 0x0000; //thiet lap con tro eeprom ve dia chi dau
for (i= 0; i<=16; i++)
{
key_id[i] = key_data[i];
*pointer = key_id[i];
pointer++;
}
clear_array();//xoa bo dem key_data;
new_key_flag = 0;
PORTD.0 = 0; //led thong bao New Key_ID
//--------------
lcd_putsf("SUCCESS !");
break; //tro ve cho chon phim
}
if (lock == 0) //Neu khoa dang mo thi lap tuc khoa lai
{
lock = 1;
lcd_putsf("LOCKED !");
clear_array();
break;
}
else //khi khoa dang dong thi tien hanh xem ma~ key. Neu dung thi mo khoa, Neu sau thi bao loi
{
if (compare_array())//ham so sanh 2 mang. true = 1, false = 0
{
lock = 0; //dung khoa mo ra
PORTA=0;
lcd_putsf("UNLOCKED !");//THONG bao khoa da mo
clear_array();
break;
}
else
{
//sai thi bao loi key khong dung
lcd_putsf("KEY INVALID !\n PLEASE PRESS AGAIN!");
clear_array();
break;
}
}
}
case 14: //button New Key_ID
//Neu khoa chua mo -> bao loi, yeu cau mo khoa
//Neu khoa mo roi -> thong bao nhap khoa moi
//hien thi 1 led bao dang nhap khoa moi
{
lcd_clear();
if (lock == 1)
{
lcd_clear();
lcd_putsf("locked!\n PLEASE UNLOCK");
break;
}
//nguoc lai thi khoa dang mo
lcd_putsf("PLEASE PRESS NEW\n KEY_ID!");
new_key_flag = 1;
PORTA.0 = 1; //led thong bao new key_id
break;
}
case 13: //button input key
{
lcd_clear();
lcd_putsf("INPUT KEY");
clear_array();
break;
}
case 15:
{
unsigned char j,devices;
int temp;
lcd_init(16);
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("NHIET DO PHONG:");
lcd_gotoxy(4,1);
//lcd_putchar(0xDF);
lcd_gotoxy(5,1);
lcd_putsf("C");
/************************************************** ************/
devices=w1_search(0xf0,rom_code);
delay_ms(700);
/************************************************** ************/
/************************************************** ************/
while (1)
{ //lcd_clear();
for (j=0;j<devices;j++)
{
lcd_gotoxy(1,1);
temp=ds18b20_temperature(&rom_code[j][0]);
lcd_putchar(temp/100+48);
lcd_putchar((temp%100)/10+48);
lcd_putchar(temp%10+48);
delay_ms(1200);
};
lcd_clear();
lcd_putsf("LOCK DIGITAL !");
break;
};
break;
}
case 12: //button start
{
unsigned char j;
lcd_clear();
lcd_putsf("System on");
lcd_clear();
lcd_putsf("STARTING");
delay_ms(500);//thuc hien tre
lcd_putchar ('.'); //phan hien thi giao dien
delay_ms(500);//thuc hien tre
lcd_putchar ('.');
delay_ms(500);//thuc hien tre
lcd_putchar ('.');
delay_ms(500);//thuc hien tre
lcd_clear();
lcd_putsf("ACTIVE !");
delay_ms(5000);
while(1)
{
if(PINC.0==1)
{
lcd_clear();
lcd_putsf("ALARM!");
lcd_putsf("\nRoom 1");
delay_ms(300);
PORTA.0=1;
}
if(PINC.1==1)
{
lcd_clear();
lcd_putsf("ALARM!");
lcd_putsf("\nRoom 2");
delay_ms(300);
PORTA.1=1;
}
if(PINC.2==1)
{
lcd_clear();
lcd_putsf("\nALARM!");
lcd_putsf("Room 3");
delay_ms(300);
PORTA.2=1;
}
if(PINC.7==1)
{
lcd_clear();
lcd_putsf("LOCK DIGITAL !");
break;
}
}
break;
}
}
}
}
Comment