CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PIC18F4550 and INT_EXT2(RB2)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
neo19



Joined: 14 Aug 2012
Posts: 30
Location: France

View user's profile Send private message

PIC18F4550 and INT_EXT2(RB2)
PostPosted: Mon May 19, 2014 6:06 am     Reply with quote

Hello,

I'm working on PIC18F4550 with PCW Ver 4.134

I would like to use RB2 as an external interrupt wich I detect the edge Low to high or high to low.

My software is doing some voltage and current measurements, generates fault and warning and commands

This code is working well without interrupt but the timing is not good. Normal!
If I use the interrupt INT_EXT2, it's very strange because if I power my board with RB2=0, it's OK.
But If I power my board with RB2=1, it's like BIAS_ON() was executed an half: Only one command is ON on the 6 commands. After if RB2 goes LOW and after high, it's OK.

Does someone have an idea to explain that and to help me?

Thanks,

Fabrice

main:
Code:

#include <P405-R04.h>

#include <usb_cdc.h>
#include <string.h>

float    M_I[7];                       // 6 mesures de courant 0 à 4 IMOS1 à 5; 5: ASGA
float    M_V[8];                       // 0 à 4 : VccMOS; 5:VccASGA; 6:+10V; 7:temp
int16    REF_I[7];                     // 6 consignes de courant 0 à 4 IMOS1 à 5; 5: ASGA
int16    L_I[7];                       // 6 niveaux de defauts de courant 0 à 4 IMOS1 à 5; 5: ASGA
int16    O_I[7];                       // 6 Offset de courant 0 à 4 IMOS1 à 5; 5: ASGA
int16    LV_MOS=0,LV_ASGA=0;           // level alim MOS, Alim ASGA
int16    LV_TEMP=0;                    // level alim MOS, Alim ASGA
byte     EnTrans_Shunt=0;              // ASGA seul b0=shunt 0=0.1R 1=0.05R, b1=M1, b2=M2, b3=M3, b4=M4, b5=1, b6=M5, b7=ASGA 1=ENABLE
byte     STATUS1=0b00100000;           // valeur 32 par defaut : caractere ASCII de 32 à 255. On evite les caracteres speciaux
byte     STATUS2=0b00100000;
byte     STATUS3=0b00100000;

byte     nb_trans_actif=0;

byte     sec=0;
short    val_sec=0;

#define  INTS_PER_MIN 76                     // (20000000/(4*256*256))
byte     int_count;                          // Number of interrupts left before a second has elapsed
short    clignote=0;

#define  size_COM   25
char     buffer_COM[size_COM];

#define  size_adr  5 
byte     adr[size_adr];

#define  size_ID   25
byte     ID[size_ID];

byte     pointeur_COM=0;

#include "subfunc.c"

#int_rtcc                                   // This function is called every time
void clock_isr()
    {                                       // the RTCC (timer0) overflows (255->0).
    set_rtcc(65279);

    if(--int_count<=(INTS_PER_MIN/2))
     clignote=1;
    else
     clignote=0;

    if(int_count==0)
     int_count=INTS_PER_MIN;
    }

/*
#INT_EXT2                                 
void ext2_isr()
   {
   disable_interrupts(INT_EXT2);
   if(input(TRIG))
    {
    bias_ON();
    bit_set(status3,0);                   // STATUS TRIG =1
    ext_int_edge(2,H_TO_L);
    }
   else
    {
    All_bias_OFF();                           // Force toutes les regulations à 0
    bit_clear(status3,0);                 // STATUS TRIG =0 
    ext_int_edge(2,L_TO_H);
    }
   enable_interrupts(INT_EXT2);
   clear_interrupt(INT_EXT2);
   }*/


void main()
   {
   setup_adc_ports(AN0_TO_AN6|VSS_VREF);                //VREF=4.096V. Attention à la mesure de AN0 à A11.
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_psp(PSP_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);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   usb_init();
   init_LED();                               // Test LED ON puis OFF 100ms
   All_bias_OFF();                           // Force toutes les regulations à 0A
   init_var();                               // Recuperation données EEPROM
   init_BUFF();                              // init buffer RS232/USB
   init_CNA();

   int_count=INTS_PER_MIN;                             // Timers pour clignotement
   set_rtcc(65279);
   setup_counters( RTCC_INTERNAL, RTCC_DIV_256);
   enable_interrupts(INT_RTCC);

/*   clear_interrupt(INT_EXT2);
   if(input(TRIG))
    {
    bias_ON();
    bit_set(status3,0);                   // STATUS TRIG =1
    ext_int_edge(2,H_TO_L);// etat initial si TRIG=1 -> detecte un front descendant
    }
   else
    {
    All_bias_OFF();                       // Force toutes les regulations à 0
    bit_clear(status3,0);                 // STATUS TRIG =0 
    ext_int_edge(2,L_TO_H);// etat initial si TRIG=1 -> detecte un front montant
    }
   
   enable_interrupts(INT_EXT2);*/
   enable_interrupts(global);

   while(true)
   {
   EnTrans_Shunt=0b10111110;
   
   if(input(TRIG))
    {
    bias_ON();
    bit_set(status3,0);                   // STATUS TRIG =1
    }
   else
    {
    All_bias_OFF();                       // Force toutes les regulations à 0
    bit_clear(status3,0);                 // STATUS TRIG =0 
    }

   lecture_U();                           // + gestion defauts alims
   lecture_I();
   LEDS();
   usb();
   gestion_COM();
   }
}


P405-R05.h:
Code:

#include <18F4550.h>
#device adc=10
#device ICD = TRUE

#fuses HSPLL, NOWDT, NOPROTECT, NOLVP, NODEBUG, USBDIV, PLL5, CPUDIV1, VREGEN
#use delay (clock=100M)

#use i2c(Master,Fast,sda=PIN_B0,scl=PIN_B1)

#define DELAY 1000

#define TRIG      PIN_B2
#define PULSE     PIN_C1
#define MOS1      PIN_A4
#define MOS2      PIN_E2
#define MOS3      PIN_C0
#define MOS4      PIN_D7
#define MOS5      PIN_B3
#define ASGA      PIN_B5

#define L_MOS1    PIN_D0
#define L_MOS2    PIN_D1
#define L_MOS3    PIN_D2
#define L_MOS4    PIN_D3
#define L_MOS5    PIN_D4
#define L_ASGA    PIN_D5
#define LEDR      PIN_D6

#define LEDV_ON output_high
#define LEDV_OFF output_low

#define LEDR_ON output_low
#define LEDR_OFF output_high

#define CMDE_OFF output_high
#define CMDE_ON output_low


subfun.c
Code:


//////////////////////////////////////////////////////////////////////////////////
void init_LED()
   {
   LEDV_ON(L_MOS1);
   LEDV_ON(L_MOS2);
   LEDV_ON(L_MOS3);
   LEDV_ON(L_MOS4);
   LEDV_ON(L_MOS5);
   LEDV_ON(L_ASGA);
   LEDR_ON(LEDR);
   
   delay_ms(100);
   
   LEDV_OFF(L_MOS1);
   LEDV_OFF(L_MOS2);
   LEDV_OFF(L_MOS3);
   LEDV_OFF(L_MOS4);
   LEDV_OFF(L_MOS5);
   LEDV_OFF(L_ASGA);
   LEDR_OFF(LEDR);
   }

void init_var()
   {
   byte i;
   
   strcpy (ID,"P405-R05,V1.0,0000001");

   for (i=0;i<4;i++)
    ADR[i]=read_EEPROM(i);

   for (i=0;i<=6;i++)
    M_I[i]=0;                                      // IMOSX=0A

   for (i=0;i<=7;i++)
    M_V[i]=0;                                      // VMOSX=0A

   EnTrans_Shunt=read_EEPROM(5);
   LV_MOS=read_EEPROM(11)+read_EEPROM(12)*256;
   LV_ASGA=read_EEPROM(13)+read_EEPROM(14)*256;
   LV_TEMP=read_EEPROM(15)+read_EEPROM(16)*256;

   for (i=0;i<=5;i++)
    {
    REF_I[i]=read_EEPROM(5*i+20)+read_EEPROM(5*i+21)*256;
    O_I[i]=read_EEPROM(5*i+22)+read_EEPROM(5*i+23)*256;
    L_I[i]=read_EEPROM(2*i+50)+read_EEPROM(2*i+51)*256;
   
    if(REF_I[i]>4095) REF_I[i]=0;
    if(O_I[i]>4095) O_I[i]=0;
    if(L_I[i]>4095) L_I[i]=0;
    }
   }

void init_CNA()
   {
   byte i;
   
   for (i=0;i<=6;i++)
    Write_CNA(i+1,REF_I[i]);                     // IMOS1 = 4.8A
   }


void bias_ON()                                  // Bias ON si Enable
   {
   if (bit_test(EnTrans_Shunt,1)) CMDE_ON(MOS1);
   if (bit_test(EnTrans_Shunt,2)) CMDE_ON(MOS2);
   if (bit_test(EnTrans_Shunt,3)) CMDE_ON(MOS3);
   if (bit_test(EnTrans_Shunt,4)) CMDE_ON(MOS4);
   if (bit_test(EnTrans_Shunt,6)) CMDE_ON(MOS5);
   if (bit_test(EnTrans_Shunt,7)) CMDE_ON(ASGA);
   }


void All_bias_OFF()
   {
   CMDE_OFF(MOS1);
   CMDE_OFF(MOS2);
   CMDE_OFF(MOS3);
   CMDE_OFF(MOS4);
   CMDE_OFF(MOS5);
   CMDE_OFF(ASGA);
   }

void LED_clignote(byte Trans)
   {
   switch (trans)
    {
    case 0: if (clignote)  LEDV_ON(L_MOS1);    // clignotage LEDV MOS1
            else  LEDV_OFF(L_MOS1);
            break;
    case 1: if (clignote)  LEDV_ON(L_MOS2);    // clignotage LEDV MOS2
            else  LEDV_OFF(L_MOS2);
            break;
    case 2: if (clignote)  LEDV_ON(L_MOS3);    // clignotage LEDV MOS3
            else  LEDV_OFF(L_MOS3);
            break;
    case 3: if (clignote)  LEDV_ON(L_MOS4);    // clignotage LEDV MOS4
            else  LEDV_OFF(L_MOS4);
            break;
    case 4: if (clignote)  LEDV_ON(L_MOS5);    // clignotage LEDV MOS5
            else  LEDV_OFF(L_MOS5);
            break;
    case 5: if (clignote)  LEDV_ON(L_ASGA);    // clignotage LEDV ASGA
            else  LEDV_OFF(L_ASGA);
            break;
    case 6: if (clignote)  LEDR_ON(LEDR);      // clignotage LEDR
            else  LEDR_OFF(LEDR);
            break;
    }
   }


void LEDS()
   {
   byte  i;
   byte tmp=0, tmpV=0, tmpI=0;
 
   if(bit_test(status3,0))
    {
    if (bit_test(EnTrans_Shunt,1) && !bit_test(STATUS2,0))      LEDV_ON(L_MOS1);                 //LEDX allumé si OK
    else                                LEDV_OFF(L_MOS1);
    if (bit_test(EnTrans_Shunt,2) && !bit_test(STATUS2,1))      LEDV_ON(L_MOS2);
    else                                LEDV_OFF(L_MOS2);
    if (bit_test(EnTrans_Shunt,3) && !bit_test(STATUS2,2))      LEDV_ON(L_MOS3);
    else                                LEDV_OFF(L_MOS3);
    if (bit_test(EnTrans_Shunt,4) && !bit_test(STATUS2,3))      LEDV_ON(L_MOS4);
    else                                LEDV_OFF(L_MOS4);
    if (bit_test(EnTrans_Shunt,6) && !bit_test(STATUS2,4))      LEDV_ON(L_MOS5);
    else                                LEDV_OFF(L_MOS5);
    if (bit_test(EnTrans_Shunt,7) && !bit_test(STATUS2,6))      LEDV_ON(L_ASGA);
    else                                LEDV_OFF(L_ASGA);
    }
   else
    {
    if (bit_test(EnTrans_Shunt,1))      LED_clignote(0);
    else                                LEDV_OFF(L_MOS1);
    if (bit_test(EnTrans_Shunt,2))      LED_clignote(1);
    else                                LEDV_OFF(L_MOS2);
    if (bit_test(EnTrans_Shunt,3))      LED_clignote(2);
    else                                LEDV_OFF(L_MOS3);
    if (bit_test(EnTrans_Shunt,4))      LED_clignote(3);
    else                                LEDV_OFF(L_MOS4);
    if (bit_test(EnTrans_Shunt,6))      LED_clignote(4);
    else                                LEDV_OFF(L_MOS5);
    if (bit_test(EnTrans_Shunt,7))      LED_clignote(5);
    else                                LEDV_OFF(L_ASGA);
    }


   tmpV=0;
   tmpI=0;
   nb_trans_actif=0;
   
   for(i=0;i<=3;i++)                                   
    {
    if(bit_test(EnTrans_Shunt,i+1)) nb_trans_actif++;          // compte le nb de transistors actifs de 1-4
    if (bit_test(STATUS2,i)) tmpV++;                           // compte le nb de transistors en warning de 1-4
    if (bit_test(STATUS1,i)) tmpI++;                           // compte le nb de transistors en warning de 1-4
    }

   for(i=0;i<=5;i++)                                           // si Warning I ou V, LEDX clignote qqs etat du TRIG
    {
    if (i<=4)  tmp=i;
    else       tmp=6;
    if(bit_test(STATUS2,tmp) || bit_test(STATUS1,tmp) )
     LED_clignote(i);
    }
   

   if(bit_test(STATUS3,1) || ((tmpV<nb_trans_actif)&& tmpV>=1 && nb_trans_actif>1) || (tmpI==1 && nb_trans_actif==4))         // warning temp ou VMOSX ou IMOS
    bit_set(STATUS3,2);                                        // warning
   else
    bit_clear(STATUS3,2);                                      // non warning

   if (((tmpV==nb_trans_actif)&&nb_trans_actif>0) || bit_test(STATUS2,4) || bit_test(STATUS2,6) || bit_test(STATUS2,7) || bit_test(STATUS1,4) || bit_test(STATUS1,6) || ((nb_trans_actif==1)&&tmpI==1) || ((nb_trans_actif==2)&&tmpI>=1) || ((nb_trans_actif>=3)&&tmpI>=2))   // defaut ts VMOS1-4, VMOS5, VASGA, +10V
    {
    bit_set(STATUS3,3);          // FAULT=1
    bit_clear(STATUS3,2);         // WARNING=0
//    All_bias_OFF();              // Force toutes les regulations à 0A
    }
   else
    {
    bit_clear(STATUS3,3);        // FAULT=0
    }

   if (bit_test(STATUS3,2))      LED_clignote(6);              // si warning clignotement LEDR
   if (bit_test(STATUS3,3))      LEDR_ON(LEDR);                // si FAULT allumage LEDR
   
   if(!bit_test(STATUS3,2) && !bit_test(STATUS3,3))            // si pas de defaut ni warning : extinction LEDR
    LEDR_OFF(LEDR);
   }
   

void Lecture_I()
   {
   byte i, tmp1=0, tmp2=0;
   int16   VCAN=0, tmp_LI=0;

   for(i=0;i<=5; i++)                  // lecture 6 mesures de courant 0 à 4 IMOS1 à 5; 5: ASGA
    {
    if (i<3)
     set_adc_channel(i);
    else
     set_adc_channel(i+1);
   
    delay_us(10);
    VCAN=read_adc();

    if (VCAN<10)       VCAN=0;
    else
     VCAN=read_adc()+O_I[i];

    if (i<=3)    tmp1=i+1;
    else         tmp1=i+2;

    if (i<=4)    tmp2=i;
    else         tmp2=i+1;

    if (bit_test(status3,0) && bit_test(EnTrans_Shunt,tmp1))       // MOS actif?
     {
     if (bit_test(EnTrans_Shunt,0)) tmp_LI=L_I[i]/2;
     else   tmp_LI=L_I[i];

     if((VCAN<=(tmp_LI*0.75)) || (VCAN>=(tmp_LI*1.25)))    // defaut si I > I+25%I ou <I-25%I de la consigne   L_I
      bit_set(STATUS1,tmp2);               // defaut IMOSi
     else
      bit_clear(STATUS1,tmp2);   
     }
    else   bit_clear(STATUS1,tmp2);

    M_I[i]=(float)VCAN*8.19/1023;

    if (bit_test(EnTrans_Shunt,0)) M_I[i]=M_I[i]/2;
    }
   }


void lecture_U()
   {
   byte  i;
   byte tmp=0;
   byte  VHCAN=0,VLCAN=0;
   int16 VCAN=0;   
   
   for (i=0;i<=7;i++)
    {
    i2c_start();
    i2c_write(0b00010000);              // ADRESSE physique du CNA =AD1=AD0=0 b0=0 pour ecriture
    switch (i)
     {
     case 0: i2c_write(0b10001000);    // Selection de Vcc MOS1
            break;
     case 1: i2c_write(0b11001000);    // Selection de Vcc MOS2
            break;
     case 2: i2c_write(0b10011000);    // Selection de Vcc MOS3
            break;
     case 3: i2c_write(0b11011000);    // Selection de Vcc MOS4
            break;
     case 4: i2c_write(0b10101000);    // Selection de Vcc MOS5
            break;
     case 5: i2c_write(0b11101000);    // Selection de Vcc ASGA
            break;
     case 6: i2c_write(0b10111000);    // Selection de +10V
            break;
     case 7: i2c_write(0b11111000);    // Selection de Temp
            break;
     }
   i2c_stop();

   i2c_start();
   i2c_write(0b00010001);              // ADRESSE physique du CNA =AD1=AD0=0 b0=1 pour lecture
   VHCAN=i2c_read();                   // lecture des 8 premiers bits
   VLCAN=i2c_read();                   // lecture des 4 derniers bits
   i2c_stop();

   VCAN=(int16)VHCAN*16+(int16)VLCAN/16;
   M_V[i]=((float)VCAN)/1000;    // 2^12-1=4095 4.096V=valeur max en entrée   /1000=/4095*4.095 en theorie c'est /4095*4.096. 1000 pour simplifier pour le PIC

   switch (i)
    {
    case 6: if(VCAN<=1019)                        // +10V < 1860=8V
             bit_set(STATUS2,7);                  // defaut internal +10V = 1
            else
             bit_clear(STATUS2,7);                // defaut internal +10V = 0 
           break;
    case 7: if(VCAN<=LV_TEMP)                     // Temperature > seuil programmé - Thermistance -> tendance vers 0, temp augmente
             bit_set(STATUS3,1);                  // defaut temp=1   
            else
             bit_clear(STATUS3,1);                // defaut temp=0   
           break;

    default:
            if(i<=4)                              // cas de MOS1 à 5 pour i=0 à 4
             {
             if (i<=3)  tmp=i+1;
             else       tmp=6;
             
             if (bit_test(EnTrans_Shunt,tmp))     // MOS actif?
              {
              if((VCAN<=(LV_MOS-256)) || (VCAN>=(LV_MOS+256)))         // defaut si VMOS > 2V ou <2V de la consigne   2V=256
               bit_set(STATUS2,i);                // defaut VMOSi
              else
               bit_clear(STATUS2,i);
              }
             else   bit_clear(STATUS2,i);
             }
            else
             {
             if (bit_test(EnTrans_Shunt,7))       // ASGA actif?
              {
              if((VCAN<=(LV_ASGA-256)) || (VCAN>=(LV_ASGA+256)))         // defaut si VASGA > 2V ou <2V de la consigne   2V=256
               bit_set(STATUS2,6);                // defaut ASGA
              else
               bit_clear(STATUS2,6);
              }
             else bit_clear(STATUS2,6);
             }
           break;
    }
    M_V[i]=M_V[i]*7.8;  //+10V
   }
  }


void init_BUFF()
   {
   byte i;
   
   for (i=0;i<size_COM;i++)                           // Init buffer RS232
    buffer_COM[i]='\0';
   pointeur_COM=0;
   }

int16 Conv_ASCII_int16 (byte start, byte adr_eeprom)  // fonction qui va lire le tableau buffer_COM[] de @start à @start+3 et sauvegarde dans EEPROM à @eeprom et eeprom+1
   {
   int16 tmp=0;
   
   tmp=((int16)buffer_COM[start]-0x30)*0x3E8+((int16)buffer_COM[start+1]-0x30)*0x64+((int16)buffer_COM[start+2]-0x30)*0xA+((int16)buffer_COM[start+3]-0x30);

   write_EEPROM(adr_eeprom,make8(tmp,0));
   write_EEPROM(adr_eeprom+1,make8(tmp,1));

   return(tmp);
   }


void USB()                                            //Fonction qui permet de remplir le Buffer_COM par USB
 {
 if(usb_enumerated())
  {
  if(usb_cdc_kbhit())
   {
   buffer_COM[pointeur_COM]=usb_cdc_getc();
   pointeur_COM++;

   if (pointeur_COM==size_COM)                        //Buffer plein
    init_BUFF();                                      // init buffer RS232/USB       
   }
  }
 }

void gestion_COM()
   {
   byte i,j;
   char tmp[20];


   if (buffer_COM[1]!='@')                            // si l'@ ne correspond pas à celle de la carte
     {
     if (pointeur_COM>4)
     if(usb_enumerated())
      {
      for (i=0;i<4;i++)
       {
       tmp[i]=buffer_COM[i+1];
       }
      if (!strncmp(ADR,tmp,4))                        // lecture *IDN?
      {
       }
      else
       init_BUFF();                                   // Init Buffer de communication
      }
     }


   strcpy (tmp,"R----*IDN?");
   for (i=0;i<4;i++)
    tmp[i+1]=ADR[i];
   if (!strncmp(buffer_COM,tmp,10))                   // lecture *IDN?
    {
    if(usb_enumerated())
     {
     usb_cdc_putc("\r\n");
     for (i=0;i<=20;i++)
      usb_cdc_putc(ID[i]);
     usb_cdc_putc("\r");
     }
    else
     {
     }
    init_BUFF();                                      // Init Buffer de communication
    }


   strcpy (tmp,"W@");
   if (!strncmp(buffer_COM,tmp,2))                    // ecriture @ carte
    if (pointeur_COM>=6)
     {
     for (i=0;i<4;i++)
      {
      ADR[i]=buffer_COM[i+2];
      write_EEPROM(i,buffer_COM[i+2]);
      }
     init_BUFF();                                     // Init Buffer de communication
     }


   strcpy (tmp,"R@");
   if (!strncmp(buffer_COM,tmp,2))                    // lecture @ carte
    {
    if(usb_enumerated())
     {
     usb_cdc_putc("\r\n");
     for (i=0;i<=size_ADR-2;i++)
      usb_cdc_putc(ADR[i]);
     usb_cdc_putc("\r");
     }
    else
     {
     }
    init_BUFF();                                      // Init Buffer de communication
    }


   strcpy (tmp,"R----MC");
   for (i=0;i<4;i++)
    tmp[i+1]=ADR[i];
   if (!strncmp(buffer_COM,tmp,7))                    // lecture mesures en courant
    {
    if(usb_enumerated())
     {
     usb_cdc_putc("\r\n");
     for (i=0;i<5;i++)
      printf(usb_cdc_putc,"%03.1fA ",M_I[i]);
     printf(usb_cdc_putc,"%03.1fA",M_I[5]);
     usb_cdc_putc("\r");
     }
    else
     {
     }
    init_BUFF();                                      // Init Buffer de communication
    }


   strcpy (tmp,"R----MV");
   for (i=0;i<4;i++)
    tmp[i+1]=ADR[i];
   if (!strncmp(buffer_COM,tmp,7))                    // lecture mesures en tension
    {
    if(usb_enumerated())
     {
     usb_cdc_putc("\r\n");
     for (i=0;i<=6;i++)
      printf(usb_cdc_putc,"%2.1fV ",M_V[i]);
     usb_cdc_putc("\r");
     }
    else
     {
     }
    init_BUFF();                                      // Init Buffer de communication
    }


   strcpy (tmp,"R----MT");
   for (i=0;i<4;i++)
    tmp[i+1]=ADR[i];
   if (!strncmp(buffer_COM,tmp,7))                    // lecture mesure de la tempzerature
    {
    if(usb_enumerated())
     {
     usb_cdc_putc("\r\n");
     printf(usb_cdc_putc,"%2.2fV ",M_V[7]);
     usb_cdc_putc("\r");
     }
    else
     {
     }
    init_BUFF();                                      // Init Buffer de communication
    }


   strcpy (tmp,"W----L");
   for (i=0;i<4;i++)
    tmp[i+1]=ADR[i];
   if (!strncmp(buffer_COM,tmp,6))                    // ecriture shunt et Enable trans
    if (pointeur_COM>=19)
    {
    EnTrans_Shunt=buffer_COM[6];
    write_EEPROM(5,EnTrans_Shunt);
    bias_ON();                            // met à ON toutes les reguls Enabelés
    LV_MOS=Conv_ASCII_int16(7,11);
    LV_ASGA=Conv_ASCII_int16(11,13);
    LV_TEMP=Conv_ASCII_int16(15,15);
    init_BUFF();                                      // Init Buffer de communication
    }


   strcpy (tmp,"R----L");
   for (i=0;i<4;i++)
    tmp[i+1]=ADR[i];
   if (!strncmp(buffer_COM,tmp,6))                    // lecture shunt et Enable trans
    {
    if(usb_enumerated())
     {
     usb_cdc_putc("\r\n");
     printf(usb_cdc_putc,"%C",(char)EnTrans_Shunt);
     printf(usb_cdc_putc,"%04lu",LV_MOS);
     printf(usb_cdc_putc,"%04lu",LV_ASGA);
     printf(usb_cdc_putc,"%04lu",LV_TEMP);
     usb_cdc_putc("\r");
     }
    else
     {
     }
    init_BUFF();                                      // Init Buffer de communication
    }


   strcpy (tmp,"W----l");
   for (i=0;i<4;i++)
    tmp[i+1]=ADR[i];
   if (!strncmp(buffer_COM,tmp,6))                    // ecriture consigne en courant
    if (pointeur_COM>=11)
    {
    j=buffer_COM[6]-0x30;                             // j de 1 à 6  1 à 5 pour MOS, 6 pour ASGA
    if (j>6)   j=6;
    if(j==0)   j=1;
    REF_I[j-1]=Conv_ASCII_int16(7,5*j+15);              // ecriture ds EEPROM @ 20, 25 30 35 40 45
    Write_CNA(j,REF_I[j-1]);                            // ecriture consigne sur CNA
    init_BUFF();                                      // Init Buffer de communication
    }


   strcpy (tmp,"R----l");
   for (i=0;i<4;i++)
    tmp[i+1]=ADR[i];
   if (!strncmp(buffer_COM,tmp,6))                    // lecture consigne en courant
    if (pointeur_COM>=7)
     {
     j=buffer_COM[6]-0x30;                             // j de 1 à 6  1 à 5 pour MOS, 6 pour ASGA
     if (j>6)   j=6;
     if(j==0)   j=1;
     if(usb_enumerated())
      {
      usb_cdc_putc("\r\n");
      printf(usb_cdc_putc,"%04lu",REF_I[j-1]);
      usb_cdc_putc("\r");
      }
     else
      {
      }
     init_BUFF();                                      // Init Buffer de communication
     }


   strcpy (tmp,"W----O");
   for (i=0;i<4;i++)
    tmp[i+1]=ADR[i];
   if (!strncmp(buffer_COM,tmp,6))                    // ecriture consigne en courant
    if (pointeur_COM>=11)
    {
    j=buffer_COM[6]-0x30;                             // j de 1 à 6  1 à 5 pour MOS, 6 pour ASGA
    if (j>6)   j=6;
    if(j==0)   j=1;
    O_I[j-1]=Conv_ASCII_int16(7,5*j+17);              // ecriture ds EEPROM @ 22, 27 32 37 42 47
    init_BUFF();                                      // Init Buffer de communication
    }


   strcpy (tmp,"W----SETREF");
   for (i=0;i<4;i++)
    tmp[i+1]=ADR[i];
   if (!strncmp(buffer_COM,tmp,11))                   // ecriture consigne en courant
    if (pointeur_COM>=11)
    {
    for (i=0;i<=5;i++)
     {
     L_I[i]=M_I[i]*1023/8.19;                         // Sauvegarde des 6 niveaux de defauts de courant 0 à 4 IMOS1 à 5; 5: ASGA
     write_EEPROM(2*i+50,make8(L_I[i],0));
     write_EEPROM(2*i+51,make8(L_I[i],1));
     }


    if(usb_enumerated())
     {
     usb_cdc_putc("\r\n");
     for (i=0;i<=5;i++)
      printf(usb_cdc_putc,"%04lu ",L_I[i]);
     usb_cdc_putc("\r");
     }
    init_BUFF();                                      // Init Buffer de communication
    }


   strcpy (tmp,"R----O");
   for (i=0;i<4;i++)
    tmp[i+1]=ADR[i];
   if (!strncmp(buffer_COM,tmp,6))                    // lecture consigne en courant
    if (pointeur_COM>=7)
     {
     j=buffer_COM[6]-0x30;                             // j de 1 à 6  1 à 5 pour MOS, 6 pour ASGA
     if (j>6)   j=6;
     if(j==0)   j=1;
     if(usb_enumerated())
      {
      usb_cdc_putc("\r\n");
      printf(usb_cdc_putc,"%04lu",O_I[j-1]);
      usb_cdc_putc("\r");
      }
     else
      {
      }
     init_BUFF();                                      // Init Buffer de communication
     }


   strcpy (tmp,"R----S");
   for (i=0;i<4;i++)
    tmp[i+1]=ADR[i];
   if (!strncmp(buffer_COM,tmp,6))                    // lecture Status carte
    {
    if(usb_enumerated())
     {
     usb_cdc_putc("\r\n");
     printf(usb_cdc_putc,"%c%c%c",STATUS1, STATUS2, STATUS3);
     usb_cdc_putc("\r");
     }
    else
     {
     }
    init_BUFF();                          // Init Buffer de communication
    }


   if(buffer_COM[0]!='R' && buffer_COM[0]!='W')       // filtre sur buffer_COM
    init_BUFF();                                      // Init Buffer de communication

   for (i=0;i<size_COM-4;i++)                         // Init buffer RS232
    if(buffer_COM[i]=='R')
     if(buffer_COM[i+1]=='S')
      if(buffer_COM[i+2]=='T')
       init_BUFF();
   }
Ttelmah



Joined: 11 Mar 2010
Posts: 19369

View user's profile Send private message

PostPosted: Mon May 19, 2014 6:49 am     Reply with quote

Classic 'read the data sheet'....

Setting the 'edge' for an interrupt, can always potentially result in the interrupt being triggered.
The sequence has to be:

1) set the edge.
2) clear the interrupt.
3) enable the interrupt.

As a further comment, disabling the interrupt inside the handler is pointless. The hardware disables the interrupts.
neo19



Joined: 14 Aug 2012
Posts: 30
Location: France

View user's profile Send private message

PostPosted: Mon May 19, 2014 7:22 am     Reply with quote

Thanks for your answer,

So I changed I little bit my code in the #INT_EXT2 :
I deleted disable and enable interrupt,

In the main, in the init, I changed the order of initiate the interrupt:
Code:

   clear_interrupt(INT_EXT2);
   enable_interrupts(INT_EXT2);
   enable_interrupts(global);

but it's not working.

Code:

#INT_EXT2                                 
void ext2_isr()
   {
//   disable_interrupts(INT_EXT2);
   if(input(TRIG))
    {
    bias_ON();
    bit_set(status3,0);                   // STATUS TRIG =1
    ext_int_edge(2,H_TO_L);
    }
   else
    {
    All_bias_OFF();                           // Force toutes les regulations à 0
    bit_clear(status3,0);                 // STATUS TRIG =0 
    ext_int_edge(2,L_TO_H);
    }
   clear_interrupt(INT_EXT2);
//   enable_interrupts(INT_EXT2);
   }

void main()
   {
   setup_adc_ports(AN0_TO_AN6|VSS_VREF);                //VREF=4.096V. Attention à la mesure de AN0 à A11.
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_psp(PSP_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);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   usb_init();
   init_LED();                               // Test LED ON puis OFF 100ms
   All_bias_OFF();                           // Force toutes les regulations à 0A
   init_var();                               // Recuperation données EEPROM
   init_BUFF();                              // init buffer RS232/USB
   init_CNA();

   int_count=INTS_PER_MIN;                             // Timers pour clignotement
   set_rtcc(65279);
   setup_counters( RTCC_INTERNAL, RTCC_DIV_256);
   enable_interrupts(INT_RTCC);

   if(input(TRIG))
    {
    bias_ON();
    bit_set(status3,0);                   // STATUS TRIG =1
    ext_int_edge(2,H_TO_L);// etat initial si TRIG=1 -> detecte un front descendant
    }
   else
    {
    All_bias_OFF();                       // Force toutes les regulations à 0
    bit_clear(status3,0);                 // STATUS TRIG =0 
    ext_int_edge(2,L_TO_H);// etat initial si TRIG=1 -> detecte un front montant
    }
   clear_interrupt(INT_EXT2);
   enable_interrupts(INT_EXT2);
   enable_interrupts(global);

   while(true)
   {
   EnTrans_Shunt=0b10111110;
   
/*   if(input(TRIG))
    {
    bias_ON();
    bit_set(status3,0);                   // STATUS TRIG =1
    }
   else
    {
    All_bias_OFF();                       // Force toutes les regulations à 0
    bit_clear(status3,0);                 // STATUS TRIG =0 
    }*/

   lecture_U();                           // + gestion defauts alims
   lecture_I();
   LEDS();
   usb();
   gestion_COM();
   }
}

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 19, 2014 3:13 pm     Reply with quote

Quote:
#include <18F4550.h>
#device adc=10
#device ICD = TRUE

#fuses HSPLL, NOWDT, NOPROTECT, NOLVP, NODEBUG, USBDIV, PLL5, CPUDIV1, VREGEN
#use delay (clock=100M)

How does the 18F4550 run at 100 MHz when the data sheet says it
runs at 48 MHz max ?
neo19



Joined: 14 Aug 2012
Posts: 30
Location: France

View user's profile Send private message

PostPosted: Tue May 20, 2014 12:36 am     Reply with quote

You are right,

I started with CCS USB development kit and I don't know where I've found this. It's a big mistake.

So I correct my software :

Quote:
#fuses HSPLL, NOWDT, NOPROTECT, NOLVP, NODEBUG, USBDIV, PLL5, CPUDIV1, VREGEN
#use delay (clock=48M)


But the result is the same...
Ttelmah



Joined: 11 Mar 2010
Posts: 19369

View user's profile Send private message

PostPosted: Tue May 20, 2014 1:05 am     Reply with quote

Realistically, you need to learn how to debug....

Nobody is going to actually read through your code in it's entirety (far too large). That includes _you_!....

The key with debugging is to cut code down into smaller chunks, till problems are isolated, allowing you to find errors.

Now scanning through your initialisation, two problems have already been spotted by posters here, but the core fault still remains....
Start much smaller. Learn to write in sections, and get each part working on it's own. Then build using these smaller 'bricks'.
neo19



Joined: 14 Aug 2012
Posts: 30
Location: France

View user's profile Send private message

PostPosted: Tue May 20, 2014 1:09 am     Reply with quote

That's what I'm trying to do this morning.

To start with a small code and after finding the bug...
neo19



Joined: 14 Aug 2012
Posts: 30
Location: France

View user's profile Send private message

PostPosted: Wed May 28, 2014 2:00 am     Reply with quote

So now it's ok. The trouble was elsewhere not in the interrupt.

Thanks for your help

Fabrice
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group