#include <p18f14k50.h>
#include <delays.h>
#include <portb.h>
#include <usart.h>
#include <io.h>
#include <adc.h>
#pragma config FOSC = XT,FCMEN = ON,IESO = OFF //, PLLDIV=1 //CPUDIV = OSC1_PLL,
//FOSC = XT_XT XT oscillator (XT), segundo XT usado por USB
//FCMEN = OFF
//Fail-Safe Clock Monitor Enable bit:
// FCMEN = OFF Fail-Safe Clock Monitor disabled
//Internal/External Oscillator Switchover bit:
// IESO = OFF Oscillator Switchover mode disabled
//System Clock Postscaler Selection bits:
//CPUDIV = OSC1_PLL2 [Primary Oscillator Src: /1][96 MHz PLL Src: /2]
//INTERRUPCIONES:
# pragma code Prioridad_normal= 0x0008
void Prioridad_normal(){ //interrupcion de baja interrupcion
}
# pragma code
/// FIN INTERRUPCIONES
#pragma config PWRTEN = ON, BOREN = OFF //, BORV = 1.9V
//Power-up Timer Enable bit:
//PWRT = ON PWRT enabled
//Brown-out Reset Enable bits:
//BOR = OFF Brown-out Reset disabled in hardware and software
//Brown-out Reset Voltage bits:
//BORV = 0 Maximum setting
#pragma config WDTEN = OFF,WDTPS = 32768
//Watchdog Timer Enable bit:
//WDT = OFF WDT disabled (control is placed on the SWDTEN bit)
//Watchdog Timer Postscale Select bits:
//WDTPS = 32768 1:32768
#pragma config MCLRE = OFF //, CCP2MX = OFF //LPT1OSC = OFF, PBADEN = OFF,
//MCLR Pin Enable bit:
//MCLRE = OFF RE3 input pin enabled; MCLR pin disabled
//MCLRE = ON MCLR pin enabled; RE3 input pin disabled
//Low-Power Timer 1 Oscillator Enable bit:
//LPT1OSC = OFF Timer1 configured for higher power operation
//PORTB A/D Enable bit:
//PBADEN = OFF PORTB<4:0> pins are configured as digital I/O on Reset
//CCP2 MUX bit:
//CCP2MX = OFF CCP2 input/output is multiplexed with RB3
#pragma config STVREN = OFF,LVP = OFF,XINST = OFF,DEBUG = OFF
//Stack Full/Underflow Reset Enable bit:
//STVREN = OFF Stack full/underflow will not cause Reset
//Extended Instruction Set Enable bit:
//XINST = OFF Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
//DEBUG = OFF ¿?
#pragma config CP0 = ON,CP1 = ON //,CP2 = ON
//Code Protection bit:
//CP0 = ON Block 0 (000800-001FFFh) is code-protected
//CP0 = OFF Block 0 (000800-001FFFh) is not code-protected
//Code Protection bit:
//CP1 = ON Block 1 (002000-003FFFh) is code-protected
//CP1 = OFF Block 1 (002000-003FFFh) is not code-protected
//Code Protection bit:
//CP2 = ON Block 2 (004000-005FFFh) is code-protected
//CP2 = OFF Block 2 (004000-005FFFh) is not code-protected
#pragma config CPB = OFF,CPD = OFF
//Boot Block Code Protection bit:
//CPB = ON Boot block (000000-0007FFh) is code-protected
//CPB = OFF Boot block (000000-0007FFh) is not code-protected
//Data EEPROM Code Protection bit:
//CPD = ON Data EEPROM is code-protected
//CPD = OFF Data EEPROM is not code-protected
#pragma config WRT0 = ON,WRT1 = ON //,WRT2 = ON
//Write Protection bit:
//WRT0 = ON Block 0 (000800-001FFFh) is write-protected
//WRT0 = OFF Block 0 (000800-001FFFh) is not write-protected
//Write Protection bit:
//WRT1 = ON Block 1 (002000-003FFFh) is write-protected
//WRT1 = OFF Block 1 (002000-003FFFh) is not write-protected
//Write Protection bit:
//WRT2 = ON Block 2 (004000-005FFFh) is write-protected
//WRT2 = OFF Block 2 (004000-005FFFh) is not write-protected
#pragma config WRTB = ON,WRTC = ON,WRTD = ON
//Boot Block Write Protection bit:
//WRTB = ON Boot block (000000-0007FFh) is write-protected
//WRTB = OFF Boot block (000000-0007FFh) is not write-protected
//Configuration Register Write Protection bit:
//WRTC = ON Configuration registers (300000-3000FFh) are write-protected
//WRTC = OFF Configuration registers (300000-3000FFh) are not write-protected
//Data EEPROM Write Protection bit:
//WRTD = ON Data EEPROM is write-protected
//WRTD = OFF Data EEPROM is not write-protected
#pragma config EBTR0 = ON,EBTR1 = ON //,EBTR2 = ON
//Table Read Protection bit:
//EBTR0 = ON Block 0 (000800-001FFFh) is protected from table reads executed in other blocks
//EBTR0 = OFF Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks
//Table Read Protection bit:
//EBTR1 = ON Block 1 (002000-003FFFh) is protected from table reads executed in other blocks
//EBTR1 = OFF Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks
//Table Read Protection bit:
//EBTR2 = ON Block 2 (004000-005FFFh) is protected from table reads executed in other blocks
//EBTR2 = OFF Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks
#pragma config EBTRB = ON
//Boot Block Table Read Protection bit:
//EBTRB = ON Boot block (000000-0007FFh) is protected from table reads executed in other blocks
//EBTRB = OFF Boot block (000000-0007FFh) is not protected from table reads executed in other blocks
int i; //Para contar 0 titilaciones.-
int anda=0;
int velocidad=5;
int total=200;
int parcial=5;
int iii=0;
int ioo=0;
char ch='a';
char armado[40]; //armado
// ADC
unsigned char unidad=0, decena=0, centena=0;
unsigned int conversion=0;
unsigned char x= 10;
unsigned char muestras =0;
unsigned int M0=0;
//fin adc
// 45 0º
// 245 180º
/*
ClosePORTB CloseRBxINT OpenPORTB OpenRBxINT
Disable the interrupts
and internal pull-up
resistors for PORTB.
Disable the interrupts for
the specified INTx pin.
Configure the interrupts and internal pullup resistors on PORTB.
Enable interrupts for the specified INTx
pin.
void ClosePORTB( void ); void CloseRB0INT( void );
void CloseRB1INT( void );
void CloseRB2INT( void );
void OpenPORTB( unsigned char config); void OpenRB0INT( unsigned char config );
void OpenRB1INT( unsigned char config );
void OpenRB2INT( unsigned char config );
*/
// ssc a c18
void inicializacionSIM900(void){
//LOS DELAY CALCULADOS PARA
// 12 MGHZ
int cont;
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150);
//Delay10KTCYx(i) -> 10000.Tcy.i genera
// una demora de 10000 ciclos de instrucciones * i
putrsUSART((const far rom char *)"A\n\r");//puts("A",SERIAL_SIM900); //para sincronizar el baudrate del SIM 900
Delay10KTCYx(100); // delay_ms(300);
putcUSART(0x0d); // putchar(0x0d,SERIAL_SIM900);
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150);
for(cont=0;cont<5;cont++) // mando varios AT para el autobauding
{
putrsUSART((const far rom char *)"AT\n\r"); // puts("AT",SERIAL_SIM900);
Delay10KTCYx(80); // delay_ms(200);
putcUSART(0x0d); //putchar(0x0d,SERIAL_SIM900);
Delay10KTCYx(80); //delay_ms(200);
LATCbits.LATC6=1; //PIN_ON(LED1);
LATCbits.LATC7=0;
Delay10KTCYx(80); //delay_ms(200);
LATCbits.LATC6=0; // PIN_OFF(LED1);
LATCbits.LATC7=1;
}
//-----------------------------------seteos de configuracion del SIM900------------------------
putrsUSART((const far rom char *)"AT+CMGF=1\n\r"); // puts("AT+CMGF=1\r",SERIAL_SIM900); // configuro para que trabaje en modo texto y no PDU
Delay10KTCYx(40); //delay_ms(100);
putcUSART(0x0d); //putchar(0x0d,SERIAL_SIM900);
Delay10KTCYx(40); //delay_ms(100);
LATCbits.LATC6=0; // PIN_OFF(LED1);
LATCbits.LATC7=0;
//return;
}
void lectura_ADC(void){
unsigned resto=0;
ConvertADC(); // Comienza la conversion
while (BusyADC()==1){} // Espera que finalice la conversion
conversion= ReadADC()>>2; // Guarda el estado del conversor en conversion
M0 = M0 + conversion; // y suma en M0 las conversiones para
muestras++; // promediarlas cuando se tomen 50 muestras
if(muestras == 50){
conversion = M0/50; // Se busca el promedio de las 50 muestras
centena = conversion / 100; // Se define la centena
resto = conversion % 100; // Recupera el resto de la div.
decena = resto /10; // Se define la decena
unidad = resto % 10; // El resto es la unidad
M0 = 0;
muestras =0;
}
}
void main(void){
/*
FAQ-4 Why is “Warning [2066] type qualifier mismatch in assignment” being issued?
The libraries distributed with MPLAB C18 are compiled using the large code model
(-ml command-line option). By default, MPLAB IDE and the compiler compile applications
for the small code model. For example, the printf function distributed with the compiler
expects to receive a “const far rom char *”, but the application is actually sending a
“const near rom char *” to the printf function when the large code model is not selected
for the application. This difference between far and near is causing the “type qualifier
mismatch in assignment” warning. To get rid of these
warnings, do one of three things:
1. Recompile the libraries distributed with MPLAB C18 using the small code model
(only recommended if all applications will be using the small code model);
2. Enable the large code model in the IDE for the particular application (may
increase code size);
3. Cast the constant character string to a constant far rom character pointer, as in:
printf ((const far rom char *)”This is a test\n\r”);
*/
ADCON0=0x0F;
ADCON1=0x0F;//Todos entrada/salida digitales.-
ADCON2=0x0F;
//-------Configurar puerto c
ANSEL=0x00; // Todo digital
/*ANSEL
bit 7 ANS7: RC3 Analog Select Control bit
1 = Digital input buffer of RC3 is disabled
0 = Digital input buffer of RC3 is enabled
bit 6 ANS6: RC2 Analog Select Control bit
1 = Digital input buffer of RC2 is disabled
0 = Digital input buffer of RC2 is enabled
bit 5 ANS5: RC1 Analog Select Control bit
1 = Digital input buffer of RC1 is disabled
0 = Digital input buffer of RC1 is enabled
bit 4 ANS4: RC0 Analog Select Control bit
1 = Digital input buffer of RC0 is disabled
0 = Digital input buffer of RC0 is enabled
bit 3 ANS3: RA4 Analog Select Control bit
1 = Digital input buffer of RA4 is disabled
0 = Digital input buffer of RA4 is enabled
bit 2-0 Unimplemented: Read as ‘0’
*/
ANSELH=0x00; // Todo digital
/*
ANSELH
bit 7-4 Unimplemented: Read as ‘0’
bit 3 ANS11: RB5 Analog Select Control bit
1 = Digital input buffer of RB5 is disabled
0 = Digital input buffer of RB5 is enabled
bit 2 ANS10: RB4 Analog Select Control bit
1 = Digital input buffer of RB4 is disabled
0 = Digital input buffer of RB4 is enabled
bit 1 ANS9: RC7 Analog Select Control bit
1 = Digital input buffer of RC7 is disabled
0 = Digital input buffer of RC7 is enabled
bit 0 ANS8: RC6 Analog Select Control bit
1 = Digital input buffer of RC6 is disabled
0 = Digital input buffer of RC6 is enabled
*/
PORTC=0X00; //IMPORTANTE
TRISC=0X0f; //como salidas/entradas
/*
SSPCON1=0x00;
T1CON=0x00;
T3CON=0x00;
CCP1CON=0x00;
ECCP1AS=0x00;
*/
TRISA=0xFF; //Todos como entrada
TRISB=0XFF; //Todos como entrada
//USBEN=0;
LATCbits.LATC4=0;
LATCbits.LATC6=0;
LATCbits.LATC7=0;
//LATC=0x00; //Leds apagados
//RCON= 0X00; // IPEN=0
//INTCON=0x88; // INTERRUPCIONES POR CAMBIO RB4-RB7
//INTCON2=0X00; // habilitar LOS PULL-UP
INTCON2=0X80; // DEShabilitar LOS PULL-UP
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150);
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150);
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150);
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150);
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150);
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150);
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150);
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150);
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150);
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150);
//-----------------COMUNICACION SERIE------------------------------
//1) pongo como salida RB7 TX y RB5 RX
TRISB=0;
PORTB=0; //IMPORTANTE digitalizo
//2) abrir comunicacion serie
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &
USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 19);
/*
OpenUSART(USART_TX_INT_OFF interrupcion de transmicion TX apagada
& USART_RX_INT_OFF interrupcion de transmicion RX apagada
& USART_ASYNCH_MODE modo asyncrono
& USART_EIGHT_BIT trbaja respcion trnsmicion en 8 bits
& USART_CONT_RX modo de recepsion continuo
& USART_BRGH_LOW, low baud rate
129);
// el numero se llama SPBRG
[8000000/(4800*16)] - 1 = 103
1000000/ (1200*8) -1=
*/
//-----------------INICIALIZO MODULO SIM900D------------------------
//1) devo encender el modulo sim con el powerkey, dado como diseñada
// la placa devo enviar el puso mediante el pin c4, `reviamente configurado
// como salida.
//LATCbits.LATC4=1;
//Delay10KTCYx(150); //delay_ms(1000);
//Delay10KTCYx(150);
LATCbits.LATC4=1;
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150); //delay_ms(1000);
LATCbits.LATC4=0;
Delay10KTCYx(150);
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150);
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150);
//LATCbits.LATC4=1;
//2) Llamo al iniciador del sim 900d
inicializacionSIM900();
//---------------------------------------------------------------------------------
while(1){
LATCbits.LATC7=0;
for(i=1;i<=5;++i){//Titila 5 veces
LATCbits.LATC7=1;
LATCbits.LATC6=1;
Delay10KTCYx(150);
LATCbits.LATC7=0;
LATCbits.LATC6=0;
Delay10KTCYx(150);
}
if(PORTCbits.RC3==0){ // led c7 envio sms
LATCbits.LATC7=1;
/*
canal10=readADC();
putrsUSART("canal 0= ");
itoa(canal10 . String); //convierto entero a string
putsUSART(String);
putrsUSART("\r");
*/
/*
Nombre completo Constante en C ASCII
sonido de alerta BEL \a 7
nueva línea NL \n 10
tabulador horizontal HT \t 9
retroceso BS \b 8
retorno de carro CR \r 13
salto de página FF \f 12
barra invertida \ \\ 92
apóstrofo ' \' 39
comillas " \" 34
carácter nulo NULL \0 0
*/
//Para mandar sms via comandos AT:
//putrsUSART("AT+CMGF=1\r");
inicializacionSIM900();
Delay10KTCYx(150);
Delay10KTCYx(150);
Delay10KTCYx(150);
Delay10KTCYx(150);
// putrsUSART( "AT+CMGS=\"336154627096\"\r");
//putrsUSART( fprintf( "AT+CMGS=\"336154627096\"\r"));
//fprintf( "AT+CMGS=\"336154627096\"\r");
Delay10KTCYx(120); //delay_ms(300);
// putrsUSART( "\r");
Delay10KTCYx(120); //delay_ms(300);
putrsUSART((const far rom char *)"ATD336154627096;\r");
//putrsUSART("\r");
}// fin led c7
if(PORTCbits.RC2==0){ // led c6
LATCbits.LATC7=1;
// putrsUSART((const far rom char *)"ATD336154627096;\n\r");
putrsUSART((const far rom char *)"ATD336154627096;\n");
//putrsUSART( "\r");
//putrsUSART("AT+CPOWD=1");
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150); //delay_ms(1000);
Delay10KTCYx(150); //delay_ms(1000);
}// fin led c6
} // fin while(1)
//---------------------------------------------------------------------------------
/*
spbrg
This is the value that is written to the baud rate generator register which
determines the baud rate at which the USART operates. The formulas
for baud rate are:
Asynchronous mode, high speed:
FOSC / (16 * (spbrg + 1))
Asynchronous mode, low speed:
FOSC / (64 * (spbrg + 1))
Synchronous mode:
FOSC / (4 * (spbrg + 1))
Where FOSC is the oscillator frequency
*/
/*
while(1){ // inicio while usart
if (DataRdyUSART()){
//ch = get_byte();
// send_byte(ch+1);
// send_byte(0x0D);
// send_byte(0x0A);
putrsUSART( "Hello World!" );
// PORTB++;
}// fin if
LATCbits.LATC1=1;
putrsUSART( "Hola Esteban!!!" );
Delay10KTCYx(200);
LATCbits.LATC1=0;
} // fin while usart
*/
// importante comunicacion serie
}// Fin Main