Vous êtes sur la page 1sur 11

89c51 code lock design and software

/*------------------------------------------------------------------*-
Main.C (v1.00)
------------------------------------------------------------------
Simple test program for hardware delay library.
-*------------------------------------------------------------------*/
#include <Main.h>
#include "Delay.h"
#include "LCD8d3c2x16.h"
#include "Keypad4x3.h"

int i,j;
char L;

void main(void)
{
P1=0xff;

Lcd_Init();

while(1)
{
L=scan();
if (L!=0)
{
P3=~P3;
lcd_sendchar(L+46);
Hardware_Delay_T0(500);
Lcd_command(0x0e);
Lcd_command(0x01);
}
//P0=~P0;
L=0;
}

}
//*********************************************************************
Delay.h
/*------------------------------------------------------------------*-
Delay_T0.C (vl.00)
------------------------------------------------------------------
Simple hardware delays based on T0.
-*------------------------------------------------------------------*/
#include "Main.H"
// ------ Private constants ----------------------------------------
// Timer preload values for use in simple (hardware) delays
// - Timers are 16-bit, manual reload ('one shot').
// NOTE: These values are portable but timings are *approximate*
// and *must* be checked by hand if accurate timing is required
//
// Define Timer 0 / Timer 1 reload values for ~1 msec delay
// NOTE: Adjustment made to allow for function call overheard etc.
#define PRELOAD01 (65536 - (tWord)(OSC_FREQ / (OSC_PER_INST * 1063)))
#define PRELOAD01H (PRELOAD01 / 256)
#define PRELOADO1L (PRELOAD01 % 256)
/*------------------------------------------------------------------*-
Hardware_Delay_T0()
Function to generate N millisecond delay (approx).
Uses Timer 0 (easily adapted to Timer 1).
-*------------------------------------------------------------------*/
void Hardware_Delay_T0(const tWord N)
{
tWord ms;
// Configure Timer 0 as a 16-bit timer
//TMOD &= 0x0F; // Clear all T0 bits (T1 left unchanged)
//TMOD |= 0x10; // Set required T0 bits (T1 left unchanged)
TMOD = 0x10; // Set required T0 bits (T1 left unchanged)
ET1 = 0; // No interupts
// Delay value is *approximately* 1 ms per loop
for (ms=0; ms < N; ms++)
{
TH1 = PRELOAD01H;
TL1 = PRELOADO1L;
TF1 = 0; // clear overflow flag
TR1 = 1; // start timer 0
while (TF1 == 0); // Loop until Timer 0 overflows (TF0 == 1)
TR1 = 0; // Stop Timer 0
}
}
/*------------------------------------------------------------------*-
------------ END OF FILE ----------------------------------------
//***********************************************************
// Keypad4x3.h
//***********************************************************
#include <main.h>
//sbit p20=0xA0;
#define KEYS P1 //********** Define which PORT is to be used ***************

sbit ROW0=KEYS^0;//********** Define Rows


***********************************
sbit ROW1=KEYS^1;
sbit ROW2=KEYS^2;
sbit ROW3=KEYS^3;

sbit COL0=KEYS^4; //********** Define Cols


***********************************
sbit COL1=KEYS^5;
sbit COL2=KEYS^6;
sbit COL3=KEYS^7;
//*************** Variables Declarations ********************
unsigned char col,row,press=0,button;

//*************** Function Declarations *********************


void Debounce(void);
unsigned char scan(void);
//unsigned char mykey(void);
//***********************************************************
/*unsigned char mykey(void)
{
if (scan()!='q')
return(scan());
else
return('q');
}
//*/
unsigned char scan(void)
{
press=0;
// while(d==0)
// {
//**************** Row * wali ka scan ***************
ROW0=0;
if (COL0==0)
{
col=1;//break;
press=1;
Debounce();
button=0xa;
}
if (COL1==0)
{
col=1;//break;
press=1;
Debounce();
button=0xb;
}
if (COL2==0)
{
col=1;//break;
press=1;
Debounce();
button=0xc;
}
//**************** Row 7 wali ka scan *****************

ROW0=1;
ROW1=0;
if (COL0==0)
{
col=1;//break;
press=1;
Debounce();
button=0x7;
}
if (COL1==0)
{
col=1;//break;
press=1;
Debounce();
button=0x8;
}
if (COL2==0)
{
col=1;//break;
press=1;
Debounce();
button=0x9;
}
//**************** Row 4 wali ka scan **********************
ROW0=1;
ROW1=1;
ROW2=0;
if (COL0==0)
{
col=1;//break;
press=1;
Debounce();
button=0x4;
}
if (COL1==0)
{
col=1;//break;
press=1;
Debounce();
button=0x5;
}
if (COL2==0)
{
col=1;//break;
press=1;
Debounce();
button=0x6;
}
//**************** Row 1 wali ka scan **********************
ROW0=1;
ROW1=1;
ROW2=1;
ROW3=0;
if (COL0==0)
{
col=1;//break;
press=1;
Debounce();
button=0x1;
}
if (COL1==0)
{
col=1;//break;
press=1;
Debounce();
button=0x2;
}
if (COL2==0)
{
col=1;//break;
press=1;
Debounce();
button=0x3;
}
/* if (COL3==0)
{
col=1;//break;
press=1;
Debounce();
button='3';
}//*/

ROW3=1;
// }
if (press==0) return(0);
if (press==1) return(button);
}

void Debounce(void)
{
Hardware_Delay_T0(80);

}
Main.h

/*------------------------------------------------------------------*-
Main.H (v1.00)
-----------------------------------------------------------------
'Project Header' (see Chap 9) for project DELAY_H (see Chap 11)
-*------------------------------------------------------------------*/
#ifndef _MAIN_H
#define _MAIN_H
//-----------------------------------------------------------------
// WILL NEED TO EDIT THIS SECTION FOR EVERY PROJECT
//-----------------------------------------------------------------
// Must include the appropriate microcontroller header file here
//#include <at89x51.h>
#include "reg51.h"
// Include oscillator / chip details here
// (essential if generic delays / timeouts are used)
// -
// Oscillator / resonator frequency (in Hz) e.g. (11059200UL)
#define OSC_FREQ (11059200UL)
// Number of oscillations per instruction (4, 6 or 12)
// 12 - Original 8051 / 8052 and numerous modern versions
// 6 - Various Infineon and Philips devices, etc.
// 4 - Dallas, etc.
//
// Take care with Dallas devices
// - Timers default to *12* osc ticks unless CKCON is modified
// - If using generic code on a Dallas device, use 12 here
#define OSC_PER_INST (12)
//------------------------------------------------------------------
// SHOULD NOT NEED TO EDIT THE SECTIONS BELOW
//------------------------------------------------------------------
typedef unsigned char tByte;
typedef unsigned int tWord;
typedef unsigned long tLong;
// Misc #defines
#ifndef TRUE
#define FALSE 0
#define TRUE (!FALSE)
#endif
#define RETURN_NORMAL (bit) 0
#define RETURN_ERROR (bit) 1
//------------------------------------------------------------------
// Interrupts
// - see Chapter 13.
//------------------------------------------------------------------
// Generic 8051 timer interrupts (used in most schedulers)
#define INTERRUPT_Timer_0_Overflow 1
#define INTERRUPT_Timer_1_Overflow 3
#define INTERRUPT_Timer_2_Overflow 5
// Additional interrupts (used in shared-clock schedulers)
#define INTERRUPT_UART_Rx_Tx 4
#define INTERRUPT_CAN_c515c 17
//------------------------------------------------------------------
// Error codes
// - see Chapter 14.
//------------------------------------------------------------------
#define ERROR_SCH_TOO_MANY_TASKS (1)
#define ERROR_SCH_CANNOT_DELETE_TASK (2)
#define ERROR_SCH_WAITING_FOR_SLAVE_TO_ACK (3)
#define ERROR_SCH_WAITING_FOR_START_COMMAND_FROM_MASTER (3)
#define ERROR_SCH_ONE_OR_MORE_SLAVES_DID_NOT_START (4)
#define ERROR_SCH_LOST_SLAVE (5)
#define ERROR_SCH_CAN_BUS_ERROR (6)
#define ERROR_I2C_WRITE_BYTE_AT24C64 (11)
#define ERROR_I2C_READ_BYTE_AT24C64 (12)
#define ERROR_I2C_WRITE_BYTE (13)
#define ERROR_I2C_READ_BYTE (14)
#define ERROR_USART_TI (21)
#define ERROR_USART_WRITE_CHAR (22)
#endif
/*------------------------------------------------------------------*-
--- END OF FILE -------------------------------------------------
-*------------------------------------------------------------------*/
/*------------------------------------------------------------------*-
LCD8d3c2x16.h (vl.00)
------------------------------------------------------------------
Simple hardware delays based on T0.
-*------------------------------------------------------------------*/
#include <Main.h>
//#include <Delay.h>

#define LCD_DATA P0
sbit BUSY_PIN = P0^7;

sbit LCD_RS = P2^7; // Display register select output [4]


sbit LCD_RW = P2^6; // Display Read/Write
sbit LCD_EN = P2^5; // Display enable output [6]

/***************************************************
#define LCD_INC_ADDR_NO_SCROLL 0x06
#define LCD_CURSOR_OFF 0x08
#define LCD_DISPLAY_ON 0x04
#define LCD_CLEAR_DISPLAY 0x01
#define LCD_8BIT_2LINE_5x8FONT 0x38 // 0011 1000
#define LCD_4BIT_2LINE_5x8FONT 0x28 // 0010 1000
/**************************************************
#define LCD_CLEAR 0x1
#define RETURN_HOME 0x80
#define DEC_CURSOR 0x4
#define INC_CURSOR 0x6
#define DISP_OFF_CUR_OFF 0x8
#define DISP_OFF_CUR_ON 0xA
#define DISP_ON_CUR_OFF 0xC
#define DISP_ON_CUR_BLINK 0xE
#define SHIFT_CUR_LEFT 0x10
#define SHIFT_CUR_RIGHT 0x14
#define SHIFT_DISP_LEFT 0x18
#define SHIFT_DISP_RIGHT 0x1C
//*************************************************/
void Lcd_Init();
void Lcd_command(tByte);
void Lcd_ready();
//void LCD_Delay(void);
void lcd_sendchar(tByte);
//****************************************
void Lcd_Init()
{
//LCD_DATA=0x38; //Initialize, 2-lines, 5X7 matrix.
Lcd_command(0x38);
Lcd_command(0x38);
Lcd_command(0x38);
Hardware_Delay_T0(40);
//LCD_DATA=0X06; //LCD on, cursor on
Lcd_command(0x06);

//LCD_DATA=0x0e; //Clear LCD Screen


Lcd_command(0x0e);

//LCD_DATA=0x01H; //Shift cursor right


Lcd_command(0x01);

}
//*************************************************
// COMMAND
//************************************************
void Lcd_command(tByte val2)
{
Lcd_ready();
LCD_DATA=val2;
LCD_RS=0;
LCD_RW=0;
Hardware_Delay_T0(100);
//LCD_Delay();
LCD_EN=1;
LCD_EN=0;
LCD_RS=1;
LCD_RW=1;
}

//*************************************************
// CHECK FOR READY
//************************************************
void Lcd_ready()
{
//P1_7=1;
BUSY_PIN=1;
LCD_RS=0;
LCD_RW=1;
while(BUSY_PIN!=0)
{
LCD_EN=0;
LCD_EN=1;
}
LCD_RS=1;
LCD_RW=0;
}
//*************************************************
// SEND CHARACTER
//************************************************

void lcd_sendchar(const tByte ch)


{
Lcd_ready();
LCD_DATA = ch;
LCD_RS=1;
LCD_RW=0;
Hardware_Delay_T0(10);
//LCD_Delay();

LCD_EN=1;
LCD_EN=0;
LCD_RS=1;
LCD_RW=0;
}

Vous aimerez peut-être aussi