#if defined(__PCM__)
#include <16f877.h>
#FUSES NOWDT, XT, PUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use delay(clock=4M)
#DEFINE use_portd_lcd TRUE
#include <lcd.c>
#include <stdio.h>
#use standard_io(A) //puerto A como salida
#BYTE PORTD=0X08
#BYTE TRISD=0X88 //1000 1000
int1 ladra[5];
unsigned int8 peti,cliente,salir,nume;
int1 tarje,ser,nu;
int1 target[20];//TRUE cliente registrado
signed int32 mony; //signed int32 (-)(+)
signed int32 M32;//M32 reservado para trabajar con "union"
union UNI {
signed int32 dat32;
int8 bytes[4]; };
void protSlave();
void COMBERSION(int1 Soy);
int1 MASTER;
#INT_EXT
void EXT_isr() {//función de interrupción
if(!MASTER)
{
output_low(PIN_C6);///S.envio 0 = pic2_B7_MASTER/E.envio
output_high(PIN_C1);//RS TRUE
int8 co;
int1 gp1,gp2,muta=0;
gp1=spi_read();
protSlave();
gp2=spi_read();
protSlave();
if (gp1)
{////**per 1 (estado-salidas pic1)
for (co=0;5>co;co++)
{
ladra[co]=spi_read();
protSlave();
}
}////**per 1
if (gp2)
{////**per 2
salir=spi_read();// salir
protSlave();
cliente=spi_read();// cliente
protSlave();
target[cliente]=spi_read();// int1 targe[ ]
protSlave();
tarje=spi_read();// int1 tarje
protSlave();
union UNI M32;//int32 mony
for(co=0;co<4;co++)
{
M32.bytes[co] = spi_read();
protSlave();
}
mony=M32.dat32;
}////**per 2
peti=spi_read();// int8 peti
protSlave();
muta=spi_read();
output_high(PIN_C6);//S.envio=true -----> pic2_b7_MASTER/E.envio
if (gp1)
{
if (ladra[0]) { output_high(PIN_a0); } else { output_low(PIN_a0); } delay_ms(1); //delay_ms(2);
if (ladra[1]) { output_high(PIN_a1); } else { output_low(PIN_a1); } delay_ms(1);
if (ladra[2]) { output_high(PIN_a2); } else { output_low(PIN_a2); } delay_ms(1);
if (ladra[3]) { output_high(PIN_a3); } else { output_low(PIN_a3); } delay_ms(1);
if (ladra[4]) { output_low(PIN_a4); } else { output_high(PIN_a4); } delay_ms(1);//neg
}
else
{
delay_ms(5);
}
output_low(PIN_C6);
output_low(PIN_C1);//RS FIN
if (muta) { COMBERSION(1); }
}
clear_interrupt(INT_EXT);
}
unsigned int8 lee,atra,falloMF;
unsigned int16 contra[20];// contraseña////WRITE SOLO MICRO1
signed int32 saldo[20];//dinero almacenado////WRITE SOLO MICRO1
signed int32 total,acumula;
int32 presta;
int16 meses=10;
int8 decimal,vari;
float prestamo,teres=2;
void lectura();
void borratotal();
void ponsea();//escribe contraseña
void ponme();//escribe
void skri();
void recogmateria();
void reembolso(int8 cli);
void refresco(int1 muta,int1 envi);//gp1,int1 gp2);//,int1 gp4);
void tiempo(int8 espe);//
void mensaje(int8 men);
void mensa(int8 me);
void sajesal(int1 acet,int1 line);
void OPER(int8 dar);
void main(){
int8 con,nume;
output_low(PIN_B7);
output_low(PIN_C1);//RS FIN
output_low(PIN_c2);//RW FIN
//output_low(PIN_b4);
saldo[1]=2015; saldo[5]=1400; saldo[9]=550; saldo[17]=50;
contra[1]=1111; contra[5]=2345; contra[9]=1500; contra[17]=9871;
lcd_init();//port_b_pullups(false);
COMBERSION(0);
ext_int_edge(l_to_h);//alto
clear_interrupt(INT_EXT); //#INT_EXT ///X CamBio en RB0
enable_interrupts(int_EXT);
enable_interrupts(global);
set_tris_b(0b00011111);
//setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
while(true)
{///WIL main()
#if defined(__PCM__)
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
lcd_gotoxy(13,1);
lcd_putc("PROYECTO CAJERO");
tarje=0; // recoge();
if (peti==2)
{
//peti=2 detecto tarjeta
//salir=acogetargeta();// return 21 = fallo F.C Acoge targeta 1 =cliente registrado 2=sinregistrar 11 = cliente saco la targeta si realizar operacion alguna
if (salir<3 && MASTER)
{
// recoge();
}
else
{
//while (Input(PIN_C0)==0);
//lcd_putc("\finicia espera");
//tiempo(1);
}
//else
//{
//registro();
nu=salir;//(1,0,0,0) 1 o 2 normal
borratotal();//acumula=0; total=0; lee=0; decimal=1;
mensa(1);
if (target[cliente])
{//TRUE cliente reconocido
while(nu)
{////whil cliente registrado
lectura();
if (lee!=0)
{///se pulso buton
if (lee==10)
{//ACEPTAR comprobar contraseña
if (total==contra[cliente])
{
lcd_putc("\f CORRECTO");
tiempo(3);
nu=0;
}
else
{
atra+=1;
if (atra>2)
{
nu=0;
salir=4;
} //salir=4= alarma
else
{
printf(lcd_putc
,"\f CONTRASERÑA INCORREPTA fallo %d",atra
); borratotal();
tiempo(3);
mensa(1);
}
}
//printf(lcd_putc,"\f ESCRIBA %lu CONTRASEÑA",contra[cliente]); } }
}
else
{//comprobar contraseña//NO ACEPTAR
if (lee==13)
{
nu=0;
salir=22;
}
else
{
ponsea();
}//SALIR sospechoso
}
}////se pulso buton
}////whil cliente registrado
}//cliente reconocido
else
{///cliente nuevo //salir=2
lcd_gotoxy
(2,2); printf(lcd_putc
,"( TarJeta %d desconocida REGISTRESE )",cliente
); while(salir==2)
{
lectura();
if (lee!=0)
{///se pulso buton
if (lee==10)
{//ACEPTAR comprobar contraseña
if (decimal==4)
{//new contr adecuada
salir=1;
mensaje(1); //mensaje + operaciones
}
else
{//new contr NO adecuada
mensa(2);
tiempo(4);
mensa(1);
}
}
else
{//comprobar contraseña//NO ACEPTAR
ponsea();
}
if (lee==13) salir=10;
}///se pulso buton
}///cliente nuevo
mensaje(2);
}
nume=1;
nu=0;
while(salir==1)
{////wil (!salir=0)
while(lee==10) lectura(); //antirrebote ACEPTAR
lcd_putc("\f");
while(lee!=10 && !nu)
{ ///ACEPTAR OPERACIONES DE CLIENTE
lectura();
if (lee!=0 && lee!=10 && lee!=14) nume=lee;
if (lee==13)
{
salir=10;
nume=10;
break;
}
lcd_gotoxy
(1,1); printf(lcd_putc
,"SELECCIONE OPERACION %d PULSE ACEPTAR",nume
); lcd_gotoxy(1,2);
if (nume>3) { lcd_putc("4 prestamo 5 traspaso 6 cuenta fija"); } else { lcd_putc("1 consulta saldo 2 Sacar 3 ingresar"); } //35carat
}
while(lee==10) lectura(); //(antirrebote)ACEPTAR OPERACIONES DE CLIENTE
borratotal();
nu=0;
mony=0;
lcd_putc("\f"); //lcd_gotoxy(1,1);
switch (nume)
{ //swit 1
case 1: /// consulta saldo
do
{// && lee!=13) {
lcd_gotoxy
(8,1); printf(lcd_putc
,"Cliente %d Su saldo es de %ld "cliente
,saldo
[cliente
]); lcd_gotoxy(1,2);
lcd_putc("ACEPTAR/MENU cualkier otra tecla paSALIR");
lectura();
if (lee>0)
{
if (lee!=10 && lee!=14)
{
salir=10;
break;
}
}
} while(lee!=10 && lee!=14); //nume=10; lee=0; //lee=0;...no meterse..................
break;
case 2:
OPER(10);//sacar pasta
if (lee<13)
{
mensaje(11);
//tarje=0; //peti=10 mony>0 ladra[1]
//output_high(PIN_a1); //tarje=1 ladra[4]neg s suelta dinero tarje=1
//output_low(PIN_a4);// s suelta tarjeta
recogmateria();
}//OJO recogmateria(); trata solo a (tarje || (mony y saldo[cliente]))
break;
case 3://meter pasta
OPER(12);
if (lee<13)
{
saldo[cliente]=saldo[cliente]+total;
nu=1;
nume=1;
} //if (salir==1) //todo fue normal
break;
case 4://prestamo
total=meses;
OPER(13);
total=teres;
OPER(14);//interes mensual
total=presta;
OPER(15);
OPER(16); //salir=2 micro2 sabra k se trata de un prestamo
if (lee<13)
{
salir=2;
recogmateria();
salir=1;
} //if (salir==1) //todo fue normal
break;
default :
if (salir<3)
{
printf(lcd_putc
,"\fERROR NO EXISTE OPERACION %d",nume
); tiempo(6);
}
break;
}//swit 1
if (salir==10 || salir==12)
{
mensa(salir);
tiempo(8);
} //lcd_putc("\f ASTA OTRA"); }
}//while salir=1 operaciones cliente registrado//////*/
switch (salir)
{//swit 2
case 4: //case 4://alarma contraseña cliente incorrepta
peti=4;
refresco(1,0); //delay_ms(5000);///fuera
output_high(PIN_a2);///s Traga Targeta
output_high(PIN_b7);
for (con=0;6>con;con++)
{
mensa(4);
mensa(5);
}//alarma
break;////4
case 10 || 12://operacion de retiro tarjeta
lcd_putc("\fRetire su tarjeta");
tarje=1;
recogmateria(); //fin master
break;
case 22://sospecoso no introdujo contraseña
peti=22;
lcd_putc("\fAnulo introducir contraseña (sospechoso)");// tiempo(3);
refresco(1,0);//lcd_gotoxy(1,2);//recoge(); recoge(); recoge();// COMBERSION(1);//fin master
break;
case 41:
peti=11;
refresco(1,0);//recoge(); recoge();//fin master
break;
}//swit 2 //acionSalida();
do{
if (MASTER)
{
peti=101;
salir=0;
tiempo(2);
refresco(1,0);
break;
}
}while(1);
clear_interrupt(INT_EXT);
lcd_putc("\f");
}//se introdujo tarjeta
}//se detecto targeta//
}//WIL main()
}
///////////////////////////FUNCIONES/////////////////////////////////////////////FUNCIONES/////////////////////////////////////////////FUNCIONES//////////////////
void tiempo(int8 espe) {
for(nume=0;espe>nume;nume++) delay_ms(400);
}
void mensaje(int8 men) {
lcd_gotoxy(1,1);
switch (men)
{
case 2:
contra[cliente]=total;
target[cliente]=1;
printf(lcd_putc
,"\fREGISTRO CORRECTO como Cliente numer %d"cliente
); lcd_gotoxy(1,2);
printf(lcd_putc
,"Guarde su contraseña %lu",contra
[cliente
]); tiempo(15); //lee=0;
sajesal(1,0);
break;////*/
case 10:
printf(lcd_putc
,"IMPORTE A SACAR %lu.00 EUROS ",total
); lcd_gotoxy(1,2);
printf(lcd_putc
,"Su saldo actual es de %ld.00 Euros",saldo
[cliente
]); mony=total;
break;
case 11:
saldo[cliente]=saldo[cliente]-mony;
printf(lcd_putc
,"\fRETIRE SUS %ld.00 EUROS",mony
); lcd_gotoxy(1,2);//OJO recogmateria(); trata solo a mony y saldo[cliente]
printf(lcd_putc
,"Su saldo ahora es de %ld.00 Euros",saldo
[cliente
]); break;///*/
case 12:
lcd_putc("HAORA ERES EL CAJERO Escrive el total a");
lcd_gotoxy(1,2);
printf(lcd_putc
,"ingresar %lu.00 Euros en cuenta %d",total
,cliente
); break;
case 13:///PRESTAMO.....
lcd_putc("ERES EL CAJERO cuanto dura el prestamo");
lcd_gotoxy(14,2);
printf(lcd_putc
,"%lu meses ",total
); meses=total;
if (total==1)
{
lcd_gotoxy(19,2);
lcd_putc(" ");
}
break; ///meses
case 14:
lcd_putc("ERES CAJERO cual es interes mensual ");
lcd_gotoxy(14,2);
printf(lcd_putc
,"%lu/100 ",total
); teres=total;
break;//interes mensual float
case 15:
lcd_putc("ERES EL CLIENTE cuanto quieres de");
lcd_gotoxy(10,2);
printf(lcd_putc
,"prestamo %lu.00 Euros ",total
); presta=total;
break;//prestamo
case 16:///.....PRESTAMO
prestamo=presta+(((presta*teres)/100)*meses);
vari=teres;
printf(lcd_putc
,"ACEPTA? %Lu Euros a %d/100 interesXmes",presta
,vari
); lcd_gotoxy(1,2);
printf(lcd_putc
,"dentro de %lu meses nos debera %f Euros ",meses
,prestamo
); break;//futuro pago float
}
}
void OPER(int8 dar) {
if (lee<13)
{ //13 salir 14 menu (pasar de todo)
lee=0;
ser=0;
do{
mensaje(dar);//mensaje + operaciones
skri();
if (lee>12) break; //lee=13 SALIR 14 MENU
if (total==0 && lee==10)
{
lcd_gotoxy(8,2);
lcd_putc("NO ES POSIBLE operar con CERO");
lee=0;
tiempo(5);
lcd_putc("\f");
}
} while(lee!=10); //if (lee<13) { borratotal(); }
while(lee==10)
{
if (lee>12) break;
else lectura();
}//anti-rebote
}
}
void skri() {
delay_ms(400);
lectura();
if (lee!=0)
{
if (lee==13) salir=12;
else ponme();
}
}
void ponme() {
if (lee!=10)
{ //buton ACEPTAR y MENU PRINCIPAL(14) no son validos
if (lee==11) lee=0;////11 = 0
if (lee==13) salir=12; //salir sin elegir ninguna operacion
if (lee==14) nu=0; //volver a menu No operar nada
if (lee==12) borratotal(); ///BORRAR
else
{
ser=1;
total=acumula+lee;
acumula=total*10;
}//lo normal
}
}
void mensa(int8 me){
lcd_gotoxy(1,1);
switch (me){
case 0:
lcd_putc("\fERROR EL PRIMER DIGITO NO PUEDE SER CERO");
borratotal();
break;
case 1:
printf(lcd_putc
,"\f ESCRIBA %ld CONTRASEÑA",contra
[cliente
]); break;
case 2:
lcd_putc("\fERROR CONTRASEÑA A DE SER DE 4 DIGITOS");
borratotal();
tiempo(4); //ponsea();// dela
break;
case 4:
output_high(PIN_b7); lcd_putc("\f alarma");
tiempo(1);///s Alarma
break;
case 5:
output_low(PIN_b7); lcd_putc("\f CAJERO TRAGA TARJETA CREDITO");
tiempo(1);
break;
case 10:
lcd_putc("\f ASTA OTRA");
break;
case 12:
printf(lcd_putc
,"\fcancelo operacion %d",nume
); break;
}
}
void sajesal(int1 acet,int1 line) {
lee=0;
if (!line) lcd_gotoxy(1,1);
else lcd_gotoxy(1,2);
if (acet)
{
lcd_putc(" -PULSA ACEPTAR PARA SEGIR- ");
do{
delay_ms(400);
lectura();
}while(lee!=10);
}
else {
lcd_putc(" -PULSA CUALKIER BOTON PARA SALIR- ");
do{
delay_ms(400);
lectura();
}while(lee!=0);
}
}
void borratotal() {
acumula=0;
total=0;
lee=0;
decimal=0;
ser=0;
}
void ponsea(){
//if (lee==13) { salir=22; break; }//salir sospechoso
int8 co;
if (lee==11)
{
ser=1;
lee=0;
}////11 = 0
total=acumula+lee;//numer;
acumula=total*10;
mensa(1);
//printf(lcd_putc,"\f ESCRIBA %lu CONTRASEÑA",contra[cliente]);
if (total>0) { if (lee>11) borratotal(); } ///BORRAR//printf(lcd_putc,"\f ESCRIBA %lu CONTRASEÑA",contra[cliente]);
else
{
decimal++;
ser=1;
lcd_gotoxy(28,1);
if (decimal
>1) for (co
=1;decimal
>co
;co
++) printf(lcd_putc
,"* "); }
if (decimal>4)
{
mensa(2);
tiempo(4);
mensa(1);
}//digitos
if (ser && total<1)
{
mensa(0);
tiempo(4);
mensa(1);
} //no al cero
delay_ms(600);
}
void recogmateria(){
peti=10;
salir=0;
//COMBERSION(1);
refresco(1,1); //COMBERSION(0);
do{
//switch (peti){
if (salir>30)
{
// printf(lcd_putc,"\fEnvio mensaje a movil Cliente %d",cliente);
lcd_gotoxy(8,2);
switch (salir)
{
case 40:
peti=101;
lcd_putc("Olvido TARJETA");
break;
case 41:
lcd_putc("Olvido DINERO");
break;
}
} //////*/
}while(!MASTER);
}
void COMBERSION(int1 Soy) {
if (Soy)
{
MASTER=1;
set_tris_c(208);
delay_ms(2);// 1101 0000 /// "C5_S C4_E C3_S 01 0"
setup_spi(spi_master | spi_l_to_h | spi_clk_div_16);
delay_ms(2);//| SPI_XMIT_L_TO_H); //);// | SPI_XMIT_L_TO_H);
output_high(PIN_c7);
}
else
{
MASTER=0;
set_tris_c(240);
delay_ms(2); // 1111 0000 /// "C5_E C4_E C3_S 11 0"
setup_spi(spi_slave | spi_l_to_h | spi_clk_div_16);
delay_ms(2);//| SPI_XMIT_L_TO_H); //);// | SPI_XMIT_L_TO_H);
output_low(PIN_c7);
}
}
void lectura(){
lee=Input_B();
if (Input(PIN_b4)) { lee-=15;}
}//return leer;
void reembolso(int8 cli){
saldo[cli]=saldo[cli]+mony;
printf(lcd_putc
,"\fReemvolso de %lu Euros",mony
); lcd_gotoxy(1,2);
if (target
[cli
]) { printf(lcd_putc
,"Ciente numero %d",cli
); } else { printf(lcd_putc
,"En cuenta %d NO REGISTRADA",cli
); } //mony=0; }//reembolso dinero a cliente
////////////////////////funciones NEW////////////////////////funciones NEW////////////////////////funciones NEW
void protMaster() {/// protocolo de envio vajo SPI MAESTRO
atra=0;
output_high(PIN_c6); // s.envio=true afecta PIN_B7_SLAVE/E.envio
delay_ms(4);//4
while (Input(PIN_c0)==0)
{
delay_ms(1); //Impide + envios asta SLAVE lo diga con su S.pin_a0_(S.recivido) -----> E.pin_c0_MASTER(E.recivido)
if (atra>15)
{
falloMF++;
lcd_gotoxy(33,2);
printf(lcd_putc
,"MF %d",falloMF
); break;
}
atra++;
}
output_low(PIN_c6); // s.envio=false afecta PIN_B7_SLAVE/E.envio
delay_ms(4);//4
}
///Hay un jaleo en la combersion pic1-master--->pic2-slave CONCEPTO RECIVIDO es ENVIO ENVIO es RECIVIDO
void protSlave(){///protocolo de recivo vajo SPI pic2SLAVE
atra=0;
output_high(PIN_C6);//S.envio=true -----> pic2_b7_MASTER/E.envio
delay_ms(7); ///valido (8)
output_low(PIN_C6);
delay_ms(7);
}
///////////////REFRESCO///////////////REFRESCO///////////////REFRESCO///////////////REFRESCO///////////////REFRESCO
void refresco(int1 muta,int1 envi){//,int1 gp3){//,int1 gp4){//,int1 gp5){
int8 co;
falloMF=0;
output_low(PIN_c6);//S.envio incial 0
output_high(PIN_C2);//RW TRUE
spi_write(envi);
protMaster();
if (envi) {
spi_write(salir); /// int8 salir
protMaster();
spi_write(tarje); /// int1 tarje
protMaster();
//target[cliente]
spi_write(target[cliente]); /// int1 target[cliente]
protMaster();
union UNI M32;//int32 mony
M32.dat32 =mony;
for(co=0;co<4;co++) {
spi_write(M32.bytes[co]);
protMaster();
}
}
spi_write(peti); /// int8 peti
protMaster();
spi_write(muta);//
protMaster();
output_low(PIN_c2);//RW FIN
if (muta) { COMBERSION(0); }
//delay_ms(70);
}