Autor Tema: Error en código, ¿mal uso de la interrupcion Rda?  (Leído 1480 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado Dani27v

  • PIC12
  • **
  • Mensajes: 73
Error en código, ¿mal uso de la interrupcion Rda?
« en: 02 de Noviembre de 2008, 09:26:26 »
Hola, llevo ya bastante tiempo buscando información pero no consigo solucionar el problema que tengo con el siguiente código, cuando recivo algún dato por el puerto usart del pic no consigo que lo grabe en al eeprom, el código de interrupción y el grabado de la eeprom funcionan por si solos, ya que los saque de la respuesta que me dio REDpic hace tiempo,  esta comprobado y funciona correctamente, pero no encuentro que es lo que he añadido  que hace  que deje de funcionar, por mas cosas que  vaya anulando o descartando no encuentro el error. Un saludo y muchas gracias de antemano.

Código: [Seleccionar]
#include "D:\PIC\movil\lcdtouch18f4525.h"
#define FAST_GLCD;
#include <HDM64GS12_easypic4.c> //Esta es la misma libreria HDM64GS12.C que trae el
                               //ccs pero con la asignacion de pines para la tarjeta
                              //easypic4.
#include "D:\PIC\movil\IMAGENES.h"
#include "D:\PIC\movil\imagen_inicio.h"
#include "D:\PIC\movil\pantalla_configuracion.h"
#include <graphics.c>
#include <string.h>
#include <stdlib.h>
#use rs232 (baud=9600 , xmit=pin_c6, rcv=pin_c7 , bits=8)

#use standard_io(C)
#use standard_io(B)


int16 tmp;
int16 y_min,y_max,x_min,x_max;
int16 y_loc,x_loc,y_rng,x_rng;
int1 touch_flag,line_flag;
char valor_x[5];
char valor_y[4];
char valor_m[9];
char caract[9];
char rx_buffer[100];         // Buffer de recepción
int8 next_rx=0;             // siguiente posición a utilizar
int1 flag_rx_complete=0;     // flag que indica recepción completa   
int1  flag_Writing_INTERNAL_EEPROM; // flag que indica fin de escritura de EEPROM
int8 posicion_memo;



 #int_eeprom
   void eeprom_isr(void){
      flag_Writing_INTERNAL_EEPROM=0;
   }
   
   void escribe_byte_en_EEPROM( char data, int8 memAddress){
   
      flag_Writing_INTERNAL_EEPROM=1;
      enable_interrupts(int_eeprom);
      write_eeprom (memAddress, data);
      while(flag_Writing_INTERNAL_EEPROM==1){/*Espera hasta finalizar la escritura*/}
      disable_interrupts(int_eeprom);
   }
   
   void escribe_buffer_en_EEPROM(void){
      int8 i;
      for(i=0;i< next_rx;i++){
         escribe_byte_en_EEPROM(rx_buffer[i],i);
      }
   }
   




#int_RDA
   RDA_isr(void){
      char c;
     
     if(kbhit()){                // Si hay algo pendiente de recibir ...
     c=getc();                // lo recogemos sobre la variable c
        if(c==0x0D){
                                // Si lo recibido es el carácter especial de fin ...
           flag_rx_complete=1;   // marcamos el fin de la recepción y no hacemos nada mas.
         }
           }
     
         else{                    // En caso contrario ...
        (rx_buffer[next_rx++])=c; // lo guardamos en la posición next_rx de rx_buffer e incrementamos next_rx
         }
       
      }
       


void inicializar_movil()
{

printf("ATE=0\r");   //para quitar el eco
retraso();
printf("AT+CPMS=");  //at+cpms="me" indica al movil donde se guardaran los sms
putc(0x22);  //simbolo comilla "
printf("ME");
putc(0x22);  //simbolo comilla "
putc(0x0D);
retraso();
printf("AT+CNMI=2,3,0,1,0\r");
retraso();
next_rx=0;
while(kbhit()){                // Si hay algo pendiente de recibir ...  // para dejar limpio el buffer
   rx_buffer[next_rx]=getc();
   next_rx++;
}
}



/**************************************************
*******  Lee la coordenada y   ********************
**************************************************/
int16 y_pos(void)
{
   int16 result;
   set_tris_a(0x04);
   output_a(0x0A);      //Energize X plate, RA1 and RA3
   set_adc_channel(2);   //Set AN2 as input
   delay_us(10);
   result = read_adc(); //get Y axis value
   if (result != 0)
   {
      set_tris_a(0x01);
      set_adc_channel(0);
      delay_us(10);
      tmp = (1023 - read_adc());
      result = (result + tmp) >> 1;   
   }
   Return(result);
}

/**************************************************
*******  Lee la coordenada x   ********************
**************************************************/
int16 x_pos(void)
{
   int16 result;
   set_tris_a(0x02);
   output_a(0x05);      //Energize X plate, RA1 and RA3
   set_adc_channel(1);   //Set AN2 as input
   delay_us(10);
   result = read_adc(); //get Y axis value
   if (result != 0)
   {
      set_tris_a(0x08);
      set_adc_channel(3);
      delay_us(10);
      tmp = (1023 - read_adc());
      result = (result + tmp) >> 1;   
   }   
   Return(result);
}

/**************************************************
***  Funcion para determinar si la pantalla     ***
***  ha sudo pulsada o no...                    ***
**************************************************/
int1 touch(void)
{
   int1 resp;
   int16 tmp,ty,tx;
   resp=false;
   ty = y_pos();
   if (ty > 0)
   {
      delay_ms(30);
      ty=y_pos();
      tx=x_pos();
      if (ty > y_min)
      {
         if(ty < y_max)
         {
            resp = true;
            tmp=(tx - x_min) << 7;
            x_loc =(int16)(tmp/x_rng);
            if(x_loc > 127)  x_loc = 127;
            tmp = (ty - y_min) << 6;
            y_loc = (int16)(tmp/y_rng);
            if(y_loc > 63)   y_loc = 63;
         }
      }
   }
   return(resp);
}

/*************************************************************
*****  Rutina para calibrar la pantalla                *******
*************************************************************/
void calibracion()
{
   int1 sw;
   glcd_fillScreen(OFF);
   glcd_update();
   sw=1;
   while(x_pos()==0)
   {
     glcd_rect(0,0,2,2,YES,SW);
      glcd_update();
      delay_ms(100);
      sw=!sw;
   }
   
 
   delay_ms(30);
   y_min = y_pos();
   x_min = x_pos();
   
   glcd_rect(0,0,2,2,YES,OFF);
   glcd_update();
   while(x_pos() > 0);
   delay_ms(30);
   while(x_pos()==0)
   {
      glcd_rect(125,61,127,63,YES,SW);
      glcd_update();
      delay_ms(100);
      sw=!sw;
   }
   delay_ms(30);
   y_max = y_pos();
   x_max = x_pos();
   y_rng = y_max - y_min;
   x_rng = x_max - x_min;
   while(x_pos() > 0);
   glcd_fillScreen(OFF);
   glcd_update();
 
 
 
 



/**********************************************************
****  muestra las coordenadas   ***************************
**********************************************************/
void print_xy_val()
{
   glcd_rect(13,40,29,57,YES,OFF);
   if(Touch()==true)
   { 
      touch_flag = true;
      sprintf(valor_x,"%lu",x_loc);
      valor_x[4]='\0';
      glcd_text57(13,40,valor_x,1,ON);
      sprintf(valor_y,"%lu",y_loc);
      valor_y[3]='\0';
      glcd_text57(13,50,valor_y,1,ON);
   }
   else
   {
      touch_flag = false;
      glcd_rect(13,40,29,57,YES,OFF);
   }
      glcd_update();   
   
}

/*****************************************************************
******  etermina sobre que boton se ha pulsado            ********
*****************************************************************/
int8 boton_act(void)
{
   int8 button;
   if(((x_loc>3) &&(x_loc<17)) &((y_loc>31) &&(y_loc<44))) button = '1';
   if(((x_loc>22) &&(x_loc<37))&((y_loc>31) &&(y_loc<44))) button = '2';
   if(((x_loc>49)&&(x_loc<58))&((y_loc>31) &&(y_loc<44))) button = '3';
   if(((x_loc>65) &&(x_loc<78)) &((y_loc>31)&&(y_loc<44))) button = '4';
   if(((x_loc>85) &&(x_loc<100))&((y_loc>31)&&(y_loc<44))) button = '5';
   if(((x_loc>107)&&(x_loc<121))&((y_loc>31)&&(y_loc<44))) button = '6';
   if(((x_loc>1) &&(x_loc<15)) &((y_loc>49)&&(y_loc<62))) button = '7';
   if(((x_loc>21) &&(x_loc<37))&((y_loc>49)&&(y_loc<62))) button = '8';
   if(((x_loc>42)&&(x_loc<58))&((y_loc>49)&&(y_loc<62))) button = '9';
   if(((x_loc>64) &&(x_loc<78)) &((y_loc>49)&&(y_loc<62))) button = '0';
   if(((x_loc>84) &&(x_loc<101))&((y_loc>49)&&(y_loc<62))) button = 'X';
   if(((x_loc>105) &&(x_loc<124))&((y_loc>49)&&(y_loc<62))) button = 'O';
   return(button);
}


void pitido()
{
output_high(PIN_C5);
delay_ms(300);
output_low(PIN_C5);

}


void sin_calibrado()
{
y_max=640;
y_min=390;    //valores obtenidos con el archivo lcd_touch_prueba7.hex
x_max=722;    // valores leidos del adc
x_min=324;

y_rng =250;     // valor obtenido de y_max-ymin
x_rng =398;     //valor obtenido de x_max-xmin

glcd_fillScreen(OFF);
glcd_update();   
}   

void teclado()
{
int z;
int1 z2;
int z3;
int8 j;
int8 g2;
int8 g3;
int8 memAddress;

z = 10;
z2 = 0;
z3 = 19;
j=0;
g3=0;

 
  glcd_fillScreen(OFF);
  glcd_update();
  glcd_imagen();
  glcd_update();
  while(1){
 
  if(touch()==true){
  valor_x[0]=boton_act();
  valor_x[1]='\0';

if ((valor_x[0]=='X')&&((z>=13)&&(z<=130))&&((z3>19))&&(z3<=191)){
        delay_ms(100);// evita rebotes escritura     
        pitido();
        if(z2=1)
        {
        z=(z-13);
        z3=(z3-13);       
        z2=0;
         --j;
       
        }
       
        glcd_rect(z,9,z3,23,YES,OFF); //Borra el numero mostrado
               
               
     }
                 
      if ((valor_x[0]>='0')&&(valor_x[0]<='9'))
      {
                     
       if ((z<=117)&&(z3<=171)){
         pitido();                     
         glcd_text57(z,9,valor_x,2,ON); //escribir uno nuevo..
         valor_m[j] =valor_x;
                   
         delay_ms(100);// evita rebotes escritura
        caract[j]=valor_x[0];
        z2=1;
        z=(z+13);
        ++j;
       
        z3=(z3+13);
       
               
          }
      }
                 
      if (valor_x[0]=='O'){
        pitido();
        z=10;
        z3=19;
        if (j<9){
        j=0;
       
        glcd_fillScreen(OFF);
        glcd_update();
        glcd_imagen_error();
        glcd_update();
        while(touch()!=true){
        pitido();
        }     
        glcd_fillScreen(OFF);
        break;
        }
       
       for( g2 = 0 ; g2 <= 9 ; ++g2)
       {
      write_eeprom ((memAddress+g2),caract[g2]);
     
      delay_ms(100);
       }
      j=0;
      glcd_fillScreen(OFF);
      glcd_update();
      glcd_imagen_inicio();
      glcd_update();
     
      break;
      }
     glcd_update();
}
}
}
void modo_configuracion()
{

while(1)
{
if(touch()==true){

if(((x_loc>8) &&(x_loc<110))&((y_loc>10) &&(y_loc<22)))
{
pitido();
calibracion();
break;
}
if(((x_loc>8) &&(x_loc<110))&((y_loc>35) &&(y_loc<48)))
{
pitido();
teclado();
break;

}

}
}
glcd_fillScreen(OFF);
glcd_update();
glcd_imagen_inicio();
glcd_update();

}

void recepcion()
{
if ((rx_buffer[2]=0x2B)&(rx_buffer[3]=0x43)&(rx_buffer[5]=0x54))
{
posicion_memo=rx_buffer[14];

printf("AT+CMGR=");
putc(posicion_memo);
putc(0x0D);
}
}





void main()
{

   setup_adc_ports(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL );
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   inicializar_movil();
   glcd_init(ON);             //Inicializa la glcd
   glcd_fillScreen(OFF);      //Limpia la pantalla
   glcd_update();
   //calibracion();             //Calibra la pantalla
   sin_calibrado();
   glcd_imagen_inicio();
   glcd_update();
   next_rx=0;
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   pitido();
   do
   {
     if(flag_rx_complete==1){ // Si la recepción está completa ...
   
    escribe_buffer_en_EEPROM(); // escribimos el contenido del buffer en la EEPROM y ...
    next_rx=0;         // ponemos a cero la siguiente posición a utilizar y ...
    flag_rx_complete=0; // ponemos a cero el flag que indica recepción completa. 
    recepcion();
   
    }
     
      if(touch()==true)
      {
      //print_xy_val();
     
     if(((x_loc>27) &&(x_loc<90))&((y_loc>52) &&(y_loc<57)))
{
        pitido();
        glcd_fillScreen(OFF);
        glcd_update();
        glcd_pantalla_configuracion();
        glcd_update();
        modo_configuracion();
         
       
     
      }
     
      }
}while(true);
 
   
 
}