Buenas, he creado este hilo para poder aprende como se graban datos en la eeprom de un atmega8 utilizando el ide bascom. les presento mi codigo con el que no logro usar bien la eeprom para ver si alguien sabe como podria solucionarlo y asi ya queda en el foro una solucion mas sobre bascom
Código:
'*******************************************************************************
'*TITULO: CONTROL CAR *
'*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*
'* *
'*FUNCION: CERRADURA CON CLAVE DE 4 DIGITOS Y CON SALIDA A RELE *
'*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*
'* *
'*******************************************************************************
$regfile = "m8def.dat" 'MICRO A UTILIZAR
$crystal = 8000000 'CRISTAL 8Mhz
'*******************************************************************************
'******************CONFIGURACIONES**********************************************
'*******************************************************************************
Config Kbd = Portd , Debounce = 60
Cls
Waitms 50
Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portb.0 , Db5 = Portb.1 , Db6 = Portb.2 , Db7 = Portb.3 , E = Portb.4 , Rs = Portb.5
Config Lcdbus = 4
Waitms 50
Cls
Config Portc.0 = Output
Config Portc.1 = Output
Config Portc.2 = Output
Config Portc.3 = Output
'*******************************************************************************
'******************CONFIGURACION DEL TIMER0*************************************
'*******************************************************************************
Config Timer0 = Timer , Prescale = 256
On Ovf0 Tim0_isr
Enable Timer0
Enable Interrupts
'*******************************************************************************
'******************CONFIGURACION DEL ADC****************************************
'*******************************************************************************
Config Adc = Single , Prescaler = Auto , Reference = Avcc
'*******************************************************************************
'********************DECLARACIONES**********************************************
'*******************************************************************************
Declare Sub Teclado()
Declare Sub Borrar()
Declare Sub Ingreso()
Declare Sub Comprobar()
Declare Sub Clave_vieja()
Declare Sub Clave_nueva()
Declare Sub Confirmar()
Declare Sub Guardar()
Dim Cont As Integer
Dim Key_a As Byte
Dim Key_b As Byte
Dim Clave(4) As Integer
Dim Clavenueva(4) As Integer
Dim Claveprom(4) As Eram Integer
Dim Clave_eeprom(4) As Byte
Dim A As Byte
Dim I As Integer
Dim J As Integer
Dim Adc0 As Word
Dim Temp1 As Long
Dim Temp2 As Long
Dim W As Word
Dim R As Word
Dim S As Single
Dim T As Single
Dim Cont_timer0 As Integer
Dim Minuto As Byte
Const K = 5 / 1023
Cont_timer0 = 0
$eeprom
$data
Stop Timer0
'*******************************************************************************
'*******************************************************************************
'***************************GRABAR CLAVE AUTOMATICA*****************************
'*******************************************************************************
Cont = 0
Readeeprom A , 10
If A = 100 Then
Goto Vec1
Else
For I = 1 To 4
Clave_eeprom(i) = I
Next
For I = 1 To 4
Writeeeprom Clave_eeprom(i) , I
Next
A = 100
End If
'*******************************************************************************
'**************MENSAJE PRINCIPAL************************************************
'*******************************************************************************
Vec1:
Locate 1 , 5
Lcd "*MicroG*"
Waitms 1000
For A = 1 To 2
Display Off
Waitms 250
Display On
Waitms 250
Next
Cls
Waitms 1500
'*******************************************************************************
'******************************PROGRAMA PRINCIPAL*******************************
'*******************************************************************************
Inicio:
Start Timer0
Disable Interrupts
Do
Locate 1 , 1
Lcd "1-INSERTE CLAVE"
Locate 2 , 1
Lcd "2-CAMBIAR CLAVE"
Enable Interrupts
Leer_tecla:
Gosub Teclado
If Key_b = 1 Then
Gosub Borrar
Gosub Ingreso
Gosub Comprobar
End If
If Key_b = 2 Then
Gosub Borrar
Gosub Clave_vieja
Gosub Clave_nueva
Gosub Confirmar
Gosub Guardar
End If
Goto Leer_tecla
Loop
'*******************************************************************************
'**************RUTINA PARA BORRAR CLAVE*****************************************
'*******************************************************************************
Borrar:
Stop Timer0
Cls
Waitms 500
For I = 1 To 4
Clave(i) = 0
Next
Return
'*******************************************************************************
'**************INGRESAR CLAVE***************************************************
'*******************************************************************************
Ingreso:
Cls
Cont_timer0 = 0
Waitms 500
I = 6
J = 1
Locate 1 , 2
Lcd "INGRESE CLAVE"
Locate 2 , I
For A = 1 To 4
Teclanueva:
Gosub Teclado
Waitms 300
If Key_b < 10 Then
Locate 2 , I
Lcd Key_b
I = I + 1
Clave(j) = Key_b
Else
Goto Teclanueva
End If
J = J + 1
Next
Waitms 300
Cls
Return
'*******************************************************************************
'**************COMPROBAR CLAVE**************************************************
'*******************************************************************************
Comprobar:
V1:
Gosub Teclado
If Key_b = 13 Then
For I = 1 To 4
Readeeprom Clave_eeprom(i) , I
If Clave(i) = Clave_eeprom(i) Then
'Next
If I = 4 Then
Cls
Waitms 500
Portc.0 = 1
Locate 1 , 2
Lcd "CLAVE CORRECTA"
Locate 2 , 5
Lcd "ENCENDER"
Wait 4
Cls
Portc.0 = 0
End If
Next
Else
If Cont = 2 Then
Cont = 0
Cls
Locate 1 , 5
Lcd "ALARMA!"
Portc.1 = 1
Wait 8
Portc.1 = 0
Cls
Start Timer0
Goto Inicio
End If
Portc.0 = 0
Portc.2 = 1
Locate 1 , 1
Lcd "CLAVE INCORRECTA"
Locate 2 , 1
Lcd "INTENTE DE NUEVO"
Waitms 500
Portc.2 = 0
Waitms 1000
Cls
Cont = Cont + 1
End If
Else
Goto V1
End If
Start Timer0
Goto Inicio
Return
'*******************************************************************************
'**************RUTINA PARA LEER EL TECLADO**************************************
'*******************************************************************************
Teclado:
Volver:
Key_a = Getkbd()
If Key_a < 16 Then
Key_b = Lookup(key_a , Dta )
Else
Goto Volver
End If
Return
'*******************************************************************************
'**************CLAVE VIEJA******************************************************
'*******************************************************************************
Clave_vieja:
Locate 1 , 1
Lcd "CLAVE ANTIGUA"
Waitms 1500
Gosub Ingreso
Q1:
Gosub Teclado
If Key_b = 13 Then
For I = 1 To 4
Readeeprom Clave_eeprom(i) , I
If Clave(i) = Clave_eeprom(i) Then
'Next
If I = 3 Then
Cls
Wait 1
Locate 1 , 2
Lcd "CLAVE CORRECTA"
Waitms 700
Cls
End If
Else ' verificar como o mando al inicio del programa
Cls
Portc.0 = 0
Portc.2 = 1
Locate 1 , 1
Lcd "CLAVE INCORRECTA"
Waitms 500
Portc.2 = 0
Waitms 200
Cls
Start Timer0
Goto Inicio
End If
Next
Else
Goto Q1
End If
Return
'*******************************************************************************
'**************CLAVE NUEVA******************************************************
'*******************************************************************************
Clave_nueva:
I = 6
J = 1
Waitms 100
Locate 1 , 3
Lcd "NUEVA CLAVE"
Locate 2 , I
For A = 1 To 4
Teclanueva1:
Gosub Teclado
Waitms 250
If Key_b < 10 Then
Locate 2 , I
Lcd Key_b
I = I + 1
Clave(j) = Key_b
Else
Goto Teclanueva1
End If
J = J + 1
Next
Waitms 200
Cls
Return
'*******************************************************************************
'**************RUTINA PARA CONFIRMAR CLAVE *************************************
'*******************************************************************************
Confirmar:
I = 6
J = 1
Wait 1
Locate 1 , 1
Lcd "CONFIRMAR CLAVE"
Locate 2 , I
For A = 1 To 4
Teclanueva2:
Gosub Teclado
Waitms 250
If Key_b < 10 Then 'poner un retardo tb, fijarse
Locate 2 , I
Lcd Key_b
I = I + 1
Clavenueva(j) = Key_b
Else
Goto Teclanueva2
End If
J = J + 1
Next
Waitms 200
Cls
W1:
Gosub Teclado
If Key_b = 13 Then
For I = 1 To 4
If Clave(i) = Clavenueva(i) Then
If I = 4 Then
Cls
Wait 1
Locate 1 , 8
Lcd "OK"
Waitms 500
Cls
End If
Else
Cls
Portc.0 = 0
Locate 1 , 1
Lcd "CLAVE INCORRECTA"
Waitms 500
Cls
Start Timer0
Goto Inicio
End If
Next
Else
Goto W1
End If
Return
'*******************************************************************************
'**************RUTINA PARA GUARDAR CLAVE EN EEPROM******************************
'*******************************************************************************
Guardar:
Portc.3 = 1
Locate 1 , 1
Lcd "espere..."
For I = 1 To 4
Clave_eeprom(i) = Clave(i)
Writeeeprom Clave_eeprom(i) , I
Next
Waitms 1500
Portc.3 = 0
Start Timer0
Goto Inicio
Return
End
'*******************************************************************************
'****************SERVICIO DE INTERRUPCION DEL TIMER*****************************
'*******************************************************************************
Tim0_isr:
Stop Timer0
If Cont_timer0 = 1 Then
Cont_timer0 = 0
Start Adc
W = Getadc(4)
R = Getadc(5)
S = W * K
T = R * K
Temp1 = S * 100
Temp2 = T * 100
Stop Adc
Locate 4 , 1
Lcd "Tin:" ; Temp1 ; "C"
Locate 4 , 10
Lcd "Tout" ; Temp2 ; "C"
Waitms 20
Set Tifr.1
Else
Cont_timer0 = Cont_timer0 + 1
End If
Set Tifr.1
Start Timer0
Return
'*******************************************************************************
Dta:
Data 1 , 2 , 3 , 10 , 4 , 5 , 6 , 11 , 7 , 8 , 9 , 12 , 13 , 0 , 14 , 15
Última edición por un moderador: