قد تكون هناك مشكلة في برنامجك او الدائرة , ارفعهما ليتسنى لنا مساعدتك
|
برنامج المشروع بالكامل
program New_Keypad_Access
' Keypad module connections
dim keypadPort as byte at PORTD ' End Keypad module connections
const TEMP_RESOLUTION as byte = 9 ' Constant TEMP_RESOLUTION is of byte type
dim text as char[9] ' Variable text is of char type
temp as word ' Variable temp is of word type
dim ii,oldstate,oldstate1 as byte
output as char[2]
const att = "OK"
' Lcd module connections
dim LCD_RS as sbit at RB4_bit
LCD_EN as sbit at RB5_bit
LCD_D4 as sbit at RB0_bit
LCD_D5 as sbit at RB1_bit
LCD_D6 as sbit at RB2_bit
LCD_D7 as sbit at RB3_bit
LCD_RS_Direction as sbit at TRISB4_bit
LCD_EN_Direction as sbit at TRISB5_bit
LCD_D4_Direction as sbit at TRISB0_bit
LCD_D5_Direction as sbit at TRISB1_bit
LCD_D6_Direction as sbit at TRISB2_bit
LCD_D7_Direction as sbit at TRISB3_bit
' End Lcd module connections
dim kp ,i,k ,PIN_OPERATION_LOOP ,PIN_OK ,OPERATION as byte
PIN_STORE as byte[4]
m1 as sbit at PORTE.0
m2 as sbit at PORTC.5
sub procedure sendsms()
UART1_Write_Text("AT")
UART1_Write(13) 'CR mean (Enter)
UART1_Write(10)'LF mean (ctrl+H)
Delay_ms(1000)
UART1_Write_Text("AT+CMGF=1")
UART1_Write(13) 'CR
UART1_Write(10) ' LF
Delay_ms(1000)
UART1_Write_Text("AT+CMGS=")
UART1_Write(34) ' "
UART1_Write_Text("95080305")
UART1_Write(34) ' "
UART1_Write(13) 'CR
UART1_Write(10) 'LF
Delay_ms(1000)
UART1_Write_Text("Hello, Door is Open")
UART1_Write(26) ' mean (ctrl+Z)
UART1_Write(13)
UART1_Write(10)
Delay_ms(1000)
end sub
sub procedure Display_Temperature( dim temp2write as word )
const RES_SHIFT = TEMP_RESOLUTION - 8
dim temp_whole as byte ' Variable temp_whole is of byte type
temp_fraction as word ' Variable temp_fraction is of word type
text = "000.0000"
if (temp2write and 0x8000) then ' Check if temperature is negative
text[0] = "-"
temp2write = not temp2write + 1
end if
temp_whole = word(temp2write >> RES_SHIFT) ' Extract temp_whole
if ( temp_whole div 100 ) then ' Convert temp_whole to characters
text[0] = temp_whole div 100 + 48
else
text[0] = "0"
end if
text[1] = (temp_whole div 10) mod 10 + 48 ' Extract tens
text[2] = temp_whole mod 10 + 48 ' Extract ones
temp_fraction = word(temp2write << (4-RES_SHIFT)) ' Extract temp_fraction
temp_fraction = temp_fraction and 0x000F ' and convert it to
temp_fraction = temp_fraction * 625 ' unsigned int
text[4] = word(temp_fraction div 1000) + 48 ' Extract thousands
text[5] = word((temp_fraction div 100) mod 10 + 48) ' Extract hundreds
text[6] = word((temp_fraction div 10) mod 10 + 48) ' Extract tens
text[7] = word(temp_fraction mod 10) + 48 ' Extract ones
Lcd_Out(2, 5, text) ' Print temperature on Lcd
end sub
SUB PROCEDURE READ_KEYPAD
i = 0
while i < PIN_OPERATION_LOOP
kp = 0 ' Reset key code variable
' Wait for key to be pressed and released
while kp = 0
kp = Keypad_Key_Click() ' Store key code in kp variable
wend
' Prepare value for output, transform key to it"s ASCII value
select case kp
case 1
kp = 49 ' 1 ' Uncomment this block for keypad4x4
case 2
kp = 50 ' 2
case 3
kp = 51 ' 3
'case 4
'kp = 65 ' A
case 5
kp = 52 ' 4
case 6
kp = 53 ' 5
case 7
kp = 54 ' 6
'case 8
'kp = 66 ' B
case 9
kp = 55 ' 7
case 10
kp = 56 ' 8
case 11
kp = 57 ' 9
'case 12
'kp = 67 ' C
case 13
kp = 42 ' *
if PIN_OPERATION_LOOP = 1 then
OPERATION = 1
end if
case 14
kp = 48 ' 0
case 15
kp = 35 ' #
if PIN_OPERATION_LOOP = 1 then
OPERATION = 1
end if
'case 16
'kp = 68 ' D
end select
IF PIN_OPERATION_LOOP = 4 THEN
PIN_STORE[i] = kp
Lcd_Out(1,i+12, "*") ' Print * for pin number on Lcd
END IF
i = i + 1
wend
END SUB
SUB PROCEDURE PIN_CHECK
IF EEPROM_Read(0x00) = PIN_STORE[0] THEN
IF EEPROM_Read(0x01) = PIN_STORE[1] THEN
IF EEPROM_Read(0x02) = PIN_STORE[2] THEN
IF EEPROM_Read(0x03) = PIN_STORE[3] THEN
PIN_OK = 1
ELSE
PIN_OK = 0
END IF
ELSE
PIN_OK = 0
END IF
ELSE
PIN_OK = 0
END IF
ELSE
PIN_OK = 0
END IF
END SUB
SUB PROCEDURE WRONG_PIN
PORTE.1 = 1
m2 = 0
Lcd_Cmd(_LCD_CLEAR)
lcd_out(1,1," WRONG PIN ")
delay_ms(1000)
PORTE.1 = 0
m2 = 1
delay_ms(500)
PORTE.1 = 1
m2 = 0
delay_ms(500)
PORTE.1 = 0
m2 = 1
delay_ms(500)
END SUB
SUB PROCEDURE PIN_CHANGE
for i = 0 to 3
EEPROM_Write(i+0x00, PIN_STORE[i])
next i
PORTE.1 = 0
Lcd_Cmd(_LCD_CLEAR)
lcd_out(1,1," PIN CHANGED ")
delay_ms(1000)
PORTE.1 = 1
m2 = 0
delay_ms(500)
PORTE.1 = 0
m2 = 1
delay_ms(500)
PORTE.1 = 1
m2 = 0
delay_ms(500)
PORTE.1 = 0
m2 = 1
delay_ms(500)
PORTE.1 = 1
m2 = 0
delay_ms(500)
PORTE.1 = 0
m2 = 1
END SUB
sub procedure acc()
m1 = 1
m2 = 0
Lcd_Cmd(_LCD_CLEAR)
Lcd_Out(1, 1, " ACCESS ")
portc.2=1
ch_sw1: if portc.0=1 then
m1=0
m2=0
delay_ms(5000)
goto done1
end if
goto ch_sw1
done1:
m1=0
m2=1
ch_sw2: if portc.1=1 then
m1=0
m2=0
goto done2
end if
goto ch_sw2
done2:
end sub
main:
ADCON1 = 0x000F ' Configure AN pins as digital I/O
CMCON = 0x0007
'ANSEL = 0
'ANSELH = 0
TRISE = 0
PORTE = 0
TRISC = %000011
PORTC = 0
'DELAY_MS(1000)
Keypad_Init() ' Initialize Keypad
Lcd_Init() ' Initialize Lcd
Lcd_Cmd(_LCD_CURSOR_OFF) ' Cursor off
Lcd_Cmd(_LCD_CLEAR) ' Clear Lcd
UART1_Init(9600) ' Initialize UART module 9600 bps
Delay_ms(100)
MAIN_MENU:
Lcd_Out(1, 4, " Wellcome ")
text = "000.0000"
Lcd_Out(2, 0, " Temp ")
Lcd_Chr(2,13,223) ' Print degree character, "C" for Centigrades
' Different LCD displays have different char code for degree
Lcd_Chr(2,15,"C") ' If you see greek letter 'alpha' type 178 instead of 223
'while 1 ' Temperature is read in the main loop
for i = 0 to 2
Ow_Reset(PORTE, 2) ' Onewire reset signal
Ow_Write(PORTE, 2, 0xCC) ' Issue command SKIP_ROM
Ow_Write(PORTE, 2, 0x44) ' Issue command CONVERT_T
Delay_us(120)
Ow_Reset(PORTE, 2)
Ow_Write(PORTE, 2, 0xCC) ' Issue command SKIP_ROM
Ow_Write(PORTE, 2, 0xBE) ' Issue command READ_SCRATCHPAD
temp = Ow_Read(PORTE, 2)
temp = (Ow_Read(PORTE, 2) << 8) + temp
Display_Temperature(temp) ' Format and display result on Lcd
Delay_ms(520) ' 520 mS delay
next i
if portb.6 = 1 then ' for inside SW
acc
SENDSMS
END IF
if portb.7=1 then
PIN_OPERATION_LOOP = 4
PIN_OK = 0
Lcd_Cmd(_LCD_CLEAR) ' Clear display
Lcd_Out(1, 1, "ENTER PIN: ") ' Write message text on Lcd
READ_KEYPAD
PIN_CHECK
PIN_OPERATION_LOOP = 1
OPERATION = 0
while OPERATION <> 1
READ_KEYPAD
wend
IF (PIN_OK = 1) THEN ' PIN IS OK
IF (kp = 42) THEN ' * IS PRESSED = CHANGE PIN
PORTE.1 = 1
Lcd_Cmd(_LCD_CLEAR)
Lcd_Out(1, 1, "NEW PIN: ")
PIN_OPERATION_LOOP = 4
READ_KEYPAD
PIN_CHANGE
GOTO MAIN_MENU
END IF
IF (kp = 35) THEN ' # IS PRESSED = ACCESS
acc 'call acc sub procedure
sendsms
End if
ELSE
IF (kp = 42) THEN ' * IS PRESSED = CHANGE PIN
WRONG_PIN
GOTO MAIN_MENU
END IF
IF (kp = 35) THEN ' # IS PRESSED = ACCESS
WRONG_PIN
GOTO MAIN_MENU
END IF
GOTO MAIN_MENU
END IF
END IF
GOTO MAIN_MENU
end.
'while j = 4
' Lcd_Cmd(_LCD_CLEAR) ' Clear display
' Lcd_Out(1, 3, "TO OPEN PRESS (#)") ' Write message text on Lcd
' Lcd_Out(2, 1, "TO CHANGE PIN PRESS (*)") ' Write message text on Lcd
' delay_ms(500)
' while TRUE ' Endless loop
' for i=0 to 7 ' Move text to the left 8 times
' Lcd_Cmd(_LCD_SHIFT_LEFT)
' Delay_MS(500)
' next i
'
' for i=0 to 7 ' Move text to the right 8 times
' Lcd_Cmd(_LCD_SHIFT_RIGHT)
' Delay_MS(500)
' next i
' wend