#include <16F877A.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#define EEPROM_SDA PIN_c4
#define EEPROM_SCL PIN_c3
#include "24256.c"
#include "external_eeprom.c"
#include <stdlib.h>
// #priority rda,ad,timer1,timer0
// #use fast_io(A)
#use fast_io(B)
// #use fast_io(C) /// con esta instruccion evitamos que
#use fast_io(D) /// se este configurando cada vez que usamos
// #use fast_io(E) /// alguna instruccion de entrada o salida
// #byte porta = 5
#byte portb = 6
// #byte portc = 7 /// se definen direcciones de memoria
#byte portd = 8
// #byte porte = 9
int nums[7];
int temporal;
int cont_letras; //variable para contar los 8 numeros que se puede entrar por Tx serie
int16 time; //hasta 300 segundos entre recuperaciones de muestras int16 cant_var; //hasta 2000 muestras que es necesario recojer
int cont_seg;
int16 cont_time;
int16 cont_var;
int estado_leds;
int var_in_rand;
int16 lectura_ad;
int canal_ad;
int Tabla(int a) //tabla para visualizar numeros por el 7segmentos
{
switch(a)
{
CASE 0:RETURN 0b00111111;
CASE 1:RETURN 0B00000110;
CASE 2:RETURN 0B01011011;
CASE 3:RETURN 0B01001111;
CASE 4:RETURN 0B01100110;
CASE 5:RETURN 0B01101101;
CASE 6:RETURN 0B01111101;
CASE 7:RETURN 0B00000111;
CASE 8:RETURN 0B01111111;
CASE 9:RETURN 0B01100111;
}
}
int Is_num(int a)
{
if(a>=48 && a<=57)
return 0; //si es 0 es un numero sino es otro
// else // Falta este else?
return 1;
}
void actualiza_led(int i)
{
switch(i)
{
case 0: portb= 0B11111110; //PC
portd= 0B01110011;
delay_ms(50);
portb= 0B11111101;
portd= 0B00111001;
break;
case 1: portb= 0B11111110; //Ad
portd= 0B01110111;
delay_ms(50);
portb= 0B11111101;
portd= 0B01011110;
break;
case 2: portb= 0B11111110; //Sd
portd= 0B01101101;
delay_ms(50);
portb= 0B11111101;
portd= 0B01011110;
break;
}
}
void write_int16_eeprom(int16 direccion, int16 dato)
{
int dato_0,dato_1;
dato_0 = make8(dato,0); //MSB
dato_1 = make8(dato,1);
write_ext_eeprom(direccion,dato_0);
direccion++;
write_ext_eeprom(direccion,dato_1);
return;
}
int16 read_int16_eeprom(int16 direccion)
{
int16 dato;
int dato_0,dato_1;
dato_0=read_ext_eeprom(direccion);
direccion++;
dato_1=read_ext_eeprom(direccion);
dato=make16(dato_0,dato_1);
return(dato);
}
void LeerAD(int canal)
{
lectura_ad = 0;
set_adc_channel(canal);
lectura_ad = read_adc();
delay_ms(20);
setup_adc (adc_off);
}
void configurar(void) //configuraciones
{
setup_adc_ports(ALL_ANALOG);
setup_adc( ADC_CLOCK_INTERNAL );
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
// setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(INT_RDA);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
set_timer0(81);
// set_timer1(3063);
set_tris_d(0x00);
set_tris_b(0x00);
init_ext_eeprom();
}
#int_RDA
void RDA_isr(void)
{
if(cont_letras < 7)
{
if(Is_num(temporal)==0)
{
temporal =temporal-48;
nums[cont_letras] = temporal;
printf ("%d",nums
[cont_letras
]); cont_letras++;
}
else
{
cont_letras = 0;
}
}
}
#int_TIMER1
void TIMER1_isr(void)
{
if(cont_seg > 9)
{
cont_seg = 0;
cont_time++;
else
{
cont_time = 0;
canal_ad = 0;
while (canal_ad>0);
{
LeerAD(canal_ad);
write_ext_eeprom(_mul(cont_var,2),lectura_ad);
canal_ad++;
}
}
}
cont_seg++;
set_timer1(3063);
}
#int_TIMER0
void TIMER0_isr(void)
{
actualiza_led(estado_leds);
set_timer0(81);
}
void main()
{
cont_seg = 0;
estado_leds = 0;
cont_letras = 0;
var_in_rand = 0;
cont_time = 0;
canal_ad = 0;
configurar();
while(var_in_rand == 0)
{
while(cont_letras < 7){}
cant_var = _mul(nums[0],1000) + _mul(nums[1],100) + _mul(nums[2],10) + nums[3];
time = _mul
(nums
[4],100) + _mul
(nums
[5],10) + nums
[6]; var_in_rand = 1;
if(time>300 || time<1)
{
printf (" Tiempo fuera de rango. "); cont_letras = 0;
var_in_rand = 0;
}
else
if(cant_var>2000)
{
printf (" Cantidad de muestras fuera de rango. "); cont_letras = 0;
var_in_rand = 0;
}
}
clear_interrupt(int_rda);
estado_leds = 1;
LeerAD(0);
//read_ext_eeprom(dir)
//write_ext_eeprom(0,(read_ext_eeprom(0))+1);
//printf(" %2x",lectura_ad);
/*write_ext_eeprom(0,lectura_ad);
printf(" %d",read_ext_eeprom(0));
delay_ms(20);*/
lectura_ad=0;
write_int16_ext_eeprom(0, lectura_ad);
delay_ms(100);
lectura_ad = read_int16_ext_eeprom(0);
delay_ms(100);
while(1){}
}