//****************************************************************************************
//
// barrer_teclado: realiza un barrido del teclado y detecta si hay alguna tecla pulsada. La
// variable "Tecla" retorna con el código de la tecla pulsada o el 0x80 en caso
// de no haber ninguna.
//
// esperar_teclado: Esta funcion espera a que se presione una tecla y no retorna hasta que se halla pulsado una tecla.
//
//
//
// TECLA CODIGO
// --------- -----
// |---|---|---|--- 0 11101011 => tecla=PORTA&0b00001111 = 13
// RA5 -->| 1 | 2 | 3 | A | 1 11101110 => tecla=PORTA&0b00001111 = 14
// |---|---|---|--- 2 11011101 => tecla=PORTA&0b00001111 = 13
// RE0 -->| 4 | 5 | 6 | B | 3 10111011 => tecla=PORTA&0b00001111 = 11
// |---|---|---|--- 4 11101110 => tecla=PORTA&0b00001111 = 14
// RE1 -->| 7 | 8 | 9 | C | 5 11011101 => tecla=PORTA&0b00001111 = 13
// |---|---|---|--- 6 10111011 => tecla=PORTA&0b00001111 = 11
// RE2 -->|0n | 0 | = | D | 7 11101110 => tecla=PORTA&0b00001111 = 14
// |---|---|---|--- 8 01111101 => tecla=PORTA&0b00001111 = 13
// ^ ^ ^ ^ 9 10111011 => tecla=PORTA&0b00001111 = 11
// | | | | A 01110111 => tecla=PORTA&0b00001111 = 7
// RA0 RA1 RA2 RA3 B 01110111 => tecla=PORTA&0b00001111 = 7
// C 01110111 => tecla=PORTA&0b00001111 = 7
// D 01110111 => tecla=PORTA&0b00001111 = 7
// # 10111011 => tecla=PORTA&0b00001111 = 11
// * 11101110 => tecla=PORTA&0b00001111 = 14
//
#include <18F47J53.h>
#device ADC=12
#FUSES NOWDT //WDT disabled (enabled by SWDTEN bit)
#FUSES PLL3 //Divide by 3 (12 MHz oscillator input)
#FUSES NOPLLEN //PLL Disabled
#FUSES NOSTVREN //stack overflow/underflow reset enabled
#FUSES NOXINST //Extended instruction set disabled
#FUSES NOCPUDIV //No CPU system clock divide
#FUSES NOPROTECT //Program memory is not code-protected
#FUSES HSPLL //HS oscillator, PLL enabled, HSPLL used by USB
#FUSES SOSC_HIGH //High Power T1OSC/SOSC circuit selected
#FUSES CLOCKOUT //CLKO output enabled on the RA6 pin
#FUSES NOFCMEN //Fail-Safe Clock Monitor disabled
#FUSES NOIESO //Two-Speed Start-up disabled
#FUSES WDT32768 //Watchdog Postscaler 1:32768
#FUSES DSWDTOSC_INT //DSWDT uses INTOSC/INTRC as clock
#FUSES RTCOSC_INT //RTCC uses INTRC as clock
#FUSES NODSBOR //Zero-Power BOR disabled in Deep Sleep
#FUSES NODSWDT //Deep Sleep Watchdog Timer Disabled
//#FUSES DSWDT8192 //Deep Sleep Watchdog Postscaler: 1:8,192 (8.5 seconds)
#FUSES NOIOL1WAY //IOLOCK bit can be set and cleared
#FUSES ADC12 //ADC 10 or 12 Bit Select:12 - Bit ADC Enabed
#FUSES MSSPMSK7 //MSSP 7 Bit address masking
#FUSES NOWPFP //Write Protect Program Flash Page 0
#FUSES NOWPCFG //Write/Erase last page protect Disabled
#FUSES WPDIS //WPFP[5:0], WPEND, and WPCFG bits ignored
#FUSES WPEND //Start protection at page 0
#FUSES LS48MHZ //Low Speed USB mode with 48 MHz System clock at 48 MHz USB CLKEN divide-by is set to 8
#use delay(clock=48000000)
#define LOADER_END 0xFFF
#build(reset=LOADER_END+1, interrupt=LOADER_END+9) //Protege posiciones de memoria desde la 0x0000 hasta la 0x1000
#org 0, LOADER_END {}
#bit PLLEN = 0xf9b.6
//#use rs232(baud=9600,parity=N,UART1,bits=8,timeout=30)
#include "Configura_LCD_4-8_bits.c"
/********************************************************/
/*------- Espacio para declaracion de constantes ------*/
/********************************************************/
#define F1 PIN_A5
#define F2 PIN_E0
#define F3 PIN_E1
#define F4 PIN_E2
#define C4 PIN_B0
#define C3 PIN_A1
#define C2 PIN_A2
#define C1 PIN_A3
#BIT Data_Pin= 0xF82.0 // PORTC
#BIT Data_Pin_Direction = 0xF94.0 //TRIS C
/********************************************************/
/*--- Espacio para declaracion de variables globales --*/
/********************************************************/
//char i=0,n=0 ;
unsigned char tecla=0;
int i=0,menu=0;
int32 td=0,p4=0,frecuencia=0,potenciometro,lecturafinal,duty1=0;
//!unsigned char Check;
//!unsigned char uniT=0;,decT=0,uniHR=0,decHR=0;
//!unsigned char T_byte1,T_byte2,RH_byte1,RH_byte2;
//!unsigned sum;
char message1[] = "Tm=00.0C";
char message2[] = "Td=00.0C";
short Time_out;
unsigned int8 T_byte1, T_byte2, RH_byte1, RH_byte2, CheckSum ;
//!int32 conconclave=0,clave=0,contiempo=0,contiempo1=0,x;
//!int32 retardo=0,ceros=0000;clave=0,clave=0,contiempo=0,contiempo1=0,x;
//!int32 retardo=0,ceros=0000;
/********************************************************/
/********************************************************/
/*-------------- Espacio para funciones ---------------*/
/********************************************************/
#include <stdlib.h> // for atoi32
/********************************************************/
/********************************************************/
unsigned char barrer_teclado (void)
{
unsigned char tecla=15;
delay_ms(30);
output_low(F1);
tecla= (input_b()&0x01)+(input_a()&0x0e);
if(tecla!=15){
output_high(F1);
delay_ms(200);
if(tecla==7)return('+');
if(tecla==11)return(3);
if(tecla==13)return(2);
if(tecla==14)return(1);
}
output_high(F1);
output_low(F2);
tecla= (input_b()&0x01)+(input_a()&0x0e);
if(tecla!=15){
output_high(F2);
delay_ms(200);
if(tecla==7)return('-');
if(tecla==11)return(6);
if(tecla==13)return(5);
if(tecla==14)return(4);
}
output_high(F2);
output_low(F3);
tecla= (input_b()&0x01)+(input_a()&0x0e);
if(tecla!=15){
output_high(F3);
delay_ms(200);
if(tecla==7)return('/');
if(tecla==11)return(9);
if(tecla==13)return(8);
if(tecla==14)return(7);
}
output_high(F3);
output_low(F4);
tecla= (input_b()&0x01)+(input_a()&0x0e);
if(tecla!=15){
output_high(F4);
delay_ms(200);
if(tecla==7)return('X');
if(tecla==11)return('=');
if(tecla==13)return(0);
if(tecla==14)return('.');
}
output_high(F4);
return(0x80);
}
/********************************************************/
/********************************************************/
unsigned char esperar_teclado (void)
{
unsigned char tecla=0x80;
while(tecla==0x80)tecla=barrer_teclado ();
return(tecla);
}
void start_signal(){
Data_Pin_Direction = 0; // Configure connection pin as output
Data_Pin = 0; // Connection pin output low
delay_ms(25);
Data_Pin = 1; // Connection pin output high
delay_us(30);
Data_Pin_Direction = 1; // Configure connection pin as input
}
short check_response(){
delay_us(40);
if(!Data_Pin){ // Read and test if connection pin is low
delay_us(80);
if(Data_Pin){ // Read and test if connection pin is high
delay_us(50);
return 1;}
}
}
unsigned int8 Read_Data(){
unsigned int8 i, k, _data = 0; // k is used to count 1 bit reading duration
if(Time_out)
break;
for(i = 0; i < 8; i++){
k = 0;
while(!Data_Pin){ // Wait until pin goes high
k++;
if (k > 100) {Time_out = 1; break;}
delay_us(1);}
delay_us(30);
if(!Data_Pin)
bit_clear(_data, (7 - i)); // Clear bit (7 - i)
else{
bit_set(_data, (7 - i)); // Set bit (7 - i)
while(Data_Pin){ // Wait until pin goes low
k++;
if (k > 100) {Time_out = 1; break;}
delay_us(1);}
}
}
return _data;
}
/******************************************************************************/
/******************************************************************************/
/*--------------------- Espacio de codigo principal --------------------------*/
/******************************************************************************/
#zero_ram
void main(){
PLLEN = 1;
lcd_init_4bits(); // Comandos de inicialización del LCD.
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(sAN0);
set_adc_channel(0);
setup_timer_2(T2_DIV_BY_16,29,1); // PWM
setup_ccp4(CCP_PWM);
for(;;){
lcd_init_4bits(); // Comandos de inicialización del LCD.
printf(lcd_putc_4bits,"\fBienvenido\nquerido usuario");
delay_ms(1000);
printf(lcd_putc_4bits,"\fIngrese la\ntemperatura");
delay_ms(1000);
//Capturar temperatura deseada
while(menu==0){
tecla= esperar_teclado ();
delay_ms(100);
if (tecla>=0 && tecla<=9) // condicion para cuando se oprime un numero en el teclado
{
td=(td*10)+tecla; //acumula y va guardando el numero
printf(lcd_putc_4bits,"\f %Lu",td);
delay_ms(200);
}
if (tecla=='/')
{
lcd_putc_4bits(tecla);
menu=1;
delay_ms(200);
}
}
printf(lcd_putc_4bits,"\fIngrese la\nfrecuencia");
delay_ms(1000);
// Capturar frecuencia
while(menu==1){
tecla= esperar_teclado ();
delay_ms(100);
if (tecla>=0 && tecla<=9) // condicion para cuando se oprime un numero en el teclado
{
frecuencia=(frecuencia*10)+tecla; //acumula y va guardando el numero
printf(lcd_putc_4bits,"\f %Lu",frecuencia);
delay_ms(200);
}
if (tecla=='/')
{
lcd_putc_4bits(tecla);
menu=2;
delay_ms(200);
}
}
//Control del bombillo y ventilador
//Mostrar en la LCD
while(menu==2){
delay_ms(1000);
Time_out = 0;
Start_signal();
//lectura de temperatura
if(check_response()){ // If there is response from sensor
RH_byte1 = Read_Data(); // read RH byte1
RH_byte2 = Read_Data(); // read RH byte2
T_byte1 = Read_Data(); // read T byte1
T_byte2 = Read_Data(); // read T byte2
Checksum = Read_Data();
if(Time_out){ // If reading takes long time
lcd_ubicaxy_4bits(1,1);
printf(lcd_putc_4bits,"\fTime out!");
}
else{
//Control de potencia
if(T_byte1>td){
set_pwm4_duty(0);
output_high(PIN_B3);
duty1=0;
}
if(T_byte1<td){
output_low(PIN_B3);
#USE PWM(OUTPUT=PIN_B4,FREQUENCY=10kHz,Duty=100)
setup_ccp4(CCP_PWM);
pwm_set_frequency(frecuencia);
set_pwm4_duty(100);
duty1=100;
}
if(T_byte1==td){
output_low(PIN_B3);
set_pwm4_duty(0);
duty1=0;
}
//mostrar mensaje
if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)){
message1[3] = T_Byte1/10 + 48;
message1[4] = T_Byte1%10 + 48;
message2[3] = td/10 + 48;
message2[4] = td%10 + 48;
message1[7] = 223; // Degree symbol
message2[7] = 223;
lcd_ubicaxy_4bits(1,1);
printf(lcd_putc_4bits,"%s",message1);
delay_ms(200);
lcd_ubicaxy_4bits(9,1);
printf(lcd_putc_4bits,"%s",message2);
delay_ms(200);
lcd_ubicaxy_4bits(1,2);
printf(lcd_putc_4bits,"Fr=%Lu",frecuencia);
delay_ms(200);
lcd_ubicaxy_4bits(9,2);
printf(lcd_putc_4bits,"Dut=%Lu",duty1);
delay_ms(200);
lcd_ubicaxy_4bits(16,2);
printf(lcd_putc_4bits,"%c",37);
//! printf(lcd_putc_4bits,"\f%s",message2);
//! delay_ms(500);
}
else{
lcd_ubicaxy_4bits(1,1);
printf(lcd_putc_4bits,"\fCheck error");
}
}
}
else{
lcd_ubicaxy_4bits(1,1);
printf(lcd_putc_4bits,"\fNo respuesta");
lcd_ubicaxy_4bits(1,2);
printf(lcd_putc_4bits,"\fDel sensor");
}
}
}
}