#define _XTAL_FREQ 80000000
#include <p18f4455.h>
#include <adc.h>
#include <stdio.h>
#include <stdlib.h>
#include <delays.h>
#include <portb.h>
#include <usart.h>
#include <timers.h>
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config BORV = 3 // Brown Out Reset Voltage bits (Minimum setting)
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
#pragma config LPT1OSC = OFF // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTRB = OFF
#pragma config FOSC = INTOSC_EC // Interno, puerto en RA6, EC usado por USB
#pragma config VREGEN = OFF // USB voltage regulator disabled
#pragma config WDT = OFF //detecta si el micro esta en ciclo infinito
#pragma config IESO = OFF // Habilita cambio intclk OUTCLK
#pragma config PWRT = OFF // demora al Power Up
#pragma config MCLRE = OFF // MCLR se vuelve la entrada digital RE3
void high_isr(void);
typedef unsigned char bool;
#define true 1
#define false 0
unsigned int data = 0;
bool got_data_bool = false;
//UART
void uart_init (unsigned int gen_reg, unsigned int sync, unsigned int brgh, unsigned int brg16) {
// pines de tx y rx como entradas
TRISCbits.RC7=1; TRISCbits.RC6=1; // Eso vimos que no es necesario por que al activar el modulo eusart ya se configuran pero si lo dejas no afecta al programa, simplemente no es necesario
//*******************************
//Generar reloj para uart, baud rate
SPBRGH = (gen_reg & 0xFF00 >>8); // No usas brg16, por lo que no es necesario iniciar SPBRGH
SPBRG = (gen_reg & 0x00FF); // Sería suficiente con poner SPBRG = gen_reg; Asi, sin más.
//**********************************
BAUDCONbits.ABDEN = 1; // Prueba al añadir esta linea aquí, esto habilitará autobaud. Cuando conectes el movil al HC06 lo primero que tienes que enviar es una 'U' que sería un 0x55. Si hay suerte lo que sucederá será
// que te devuelva tu '1' que tienes abajo en la llamada a uart_send(unsigned char *c), que por cierto, si le quitas la asignación del '1' a dato, el programa debería hacer un ECO permanente.
//Habilitar rece ptor
RCSTAbits.CREN=1; RCSTAbits.SPEN=1; BAUDCONbits.BRG16=brg16;
//***********************************
//Transmision
TXSTAbits.SYNC=sync; TXSTAbits.BRGH=brgh; TXSTAbits.TXEN=1;
//*********************************
//Habilitar interrupciones para la recepción
IPR1bits.RCIP=1; //prioridad
PIE1bits.RCIE=1; //habilitar
}
void uart_send(unsigned int *c) {
TXREG=*c;
//esperar a que el buffer de TX se vacie (TXREG se vacie)
while (TXSTAbits.TRMT==0); }
void uart_receiver(unsigned int *c, bool *rx_flag){
if(RCSTAbits.FERR){unsigned int er = RCREG;} // Framing error
else if(RCSTAbits.OERR){ //Overrun error
RCSTAbits.CREN=0;
RCSTAbits.CREN=1;}
else{*c = RCREG; *rx_flag = true;}}
//INTERRUPCION
#pragma code high_vetor=0x8
void interrupt_at_high_vector(void)
{ _asm GOTO high_isr _endasm }
#pragma code
#pragma interrupt high_isr
void high_isr (void){
INTCONbits.GIEH = 0;
if (PIR1bits.RCIF){
uart_receiver(&data,&got_data_bool);
PIR1bits.RCIF=0;}
INTCONbits.GIEH = 1;}
#pragma code
void main( void ){
OSCCONbits.IRCF = 0x07;
OSCCONbits.SCS = 0x03;
while (OSCCONbits.IOFS!=1); //wait for oscillator to stabilize
uart_init(51,0,1,0); // Aqui puedes probar poniendo BRGH a cero (Es el mismo valor SPBRG) tanto para BRGH1 que a cero.
//cad
TRISAbits.RA0 = 1 ;//entrada
ADCON1bits.VCFG0 = 0 ;// referencia positiva Vdd
ADCON1bits.VCFG1 = 0 ; // referencia positiva Vss
ADCON1bits.PCFG = 0x0E ; // rconfig como digitales An12-An01
ADCON2bits.ADFM = 1 ; // justificado derecha
ADCON2bits.ACQT = 0x04; // tiempo de adquisicion 8 TAD
ADCON2bits.ADCS = 0x04; // clock source fosc/4
ADCON0bits.CHS = 0; // canal analógico An0
ADCON0bits.ADON = 1; // enable ADC*/
RCONbits.IPEN = 1;
/* INTCONbits.GIEH = 1;
INTCONbits.GIEL = 1;*/
INTCONbits.PEIE = 1;
while(1){
ADCON0bits.GO = 1;
while (ADCON0bits.GO);
//Para enviar el resultado del ADC
/*data=(ADRESH << 6) | (ADRESL >> 2);
uart_send (&data);*/
if(got_data_bool){
data='1'; //para probar solamente // Esto lo puedes quitar, si lo quitas se enviará lo que hayas recibido por RX. (ECO)
uart_send(&data);
got_data_bool = false;}}}