Problem

General discussion on mikroC.
Post Reply
Author
Message
flourette
Posts: 5
Joined: 17 May 2005 11:49

Problem

#1 Post by flourette » 18 May 2005 17:45

Hello i don't understand why my program don't work because the compilation is ok and all functions work independantely. I post you my program if you know what the errors i thank you in advance.

Code: Select all

#define adr_panneau 0





//Déclarations des messages de l'afficheur LCD----------------------------------

const char string_menu1[]="1: Reinit.";
const char string_menu2[]="2: Capture";
const char string_menu3[]="3: Envoi PC";
const char string_menu4[]="4: Cloture";

const char string_choix_calibre1[]="Choix calibre :";
const char string_choix_calibre2[]="1: 24V - 5A";
const char string_choix_calibre3[]="2: 24V - 2,5A";
const char string_choix_calibre4[]="Votre choix ?";

const char string_cloture1[]="Debranchez la";
const char string_cloture2[]="Puissance";
const char string_cloture3[]="Puis eteignez le";
const char string_cloture4[]="Caracteriseur";

const char string_init1[]="CARACTERISEUR";
const char string_init2[]="Version 1.0.0.1";
const char string_init3[]="IUT TARBES";
const char string_init4[]="Mr Demarcq";

const char string_branche1[]= "Branchez la";
const char string_branche2[]="Puissance";
const char string_branche3[]="Puis appuyez";
const char string_branche4[]="sur 1 bouton";

const char string_operation1[]="1: Lancer";
const char string_operation2[]="2: Annuler";
const char string_operation3[]="Votre choix ?";


//Déclarations de variables-----------------------------------------------------
char text[3];
char conductance;
char message_lcd[15];


//Sous-programmes et main-------------------------------------------------------

//Sous programme permettant d'envoyer un message sur le port série
void ecrire_msg_uart(char *message)
   {
   while(*message != 0)
   USART_write(*(message++));
   }

//Sous programme permettant l'envoi d'un nombre sur le port série
void envoi_nombre_uart(int nbconv)
        {
        unsigned short cent,diz,unit;
        cent =  nbconv /100;
        diz  = (nbconv - ((cent)*100))/10;
        unit = (nbconv - (diz*10));
        cent = cent + 48;
        Usart_write(cent);
        diz  = diz + 48;
        Usart_Write(diz);
        unit = unit + 48;
        Usart_Write(unit);
        }

//Sous programme de test de la communication par le port série du caractériseur
//avec l'ordinateur
void test_communication_RS232()
     {
     unsigned short i=0;
     do
         {
         if (USART_Data_Ready())
             {
             i = USART_Read();
             }
         }
     while(i!='c');
     ecrire_msg_uart("Caracteriseur OK\r\n");
     }

//Sous programme permettant de copier un 'const char' dans un 'char'
//'const char' est dans la ROM et 'char' est dans la ROM cette fonction est a
//utiliser avec LCD_OUT par exemple pour éviter d'avoir des chaines de coupées.
void text_ROM_to_RAM(char *str1, const char *cstr2)
     {
     unsigned short i = 0;
     do {
     str1[i] = cstr2[i];
     }
     while (str1[i++]);
     }

//Sous programme d'affichage de l'opération en cours ainsi que l'avancement
//Ligne 1 : Nom de l'opération
//Ligne 3 : Pourçentage de l'avancement
//Ligne 4 : Barre de progression
void barre_pourcent(char *ligne1,char col, char valeur1)
     {
     char i;
     LCD_Cmd(LCD_CLEAR);
     LCD_Cmd(LCD_CURSOR_OFF);
     LCD_Out(1,col, ligne1);
     LCD_Out(2,1, "");
     inttostr(((valeur1*100)/NBRE_CONV), text);
     LCD_Out(3,0,text);
     LCD_Out(3,7,"%");
     for (i=0; i<=((valeur1*16)/NBRE_CONV);i++)
            {
            LCD_Out(4,-3 + i, CARAC_POURCENT);
            }
     }


//Programme permettant d'afficher le menu de lancement des opérations et de
//capture. Valeur retournées :  1= Lancement de l'opération
//                              0= Annulation de l'opération
char lance_operation(char *operation)
     {
     char bouton_ok=0, lance;
     LCD_Cmd(LCD_CLEAR);
     LCD_Cmd(LCD_CURSOR_OFF);
     //Affichage "Debranchez la"
     text_ROM_to_RAM(message_lcd, operation) ;
     LCD_Out(1,1, message_lcd);
     //Affichage "Puissance"
     text_ROM_to_RAM(message_lcd, string_operation1) ;
     LCD_Out(2,1, message_lcd);
     //Affichage "Puis eteignez le"
     text_ROM_to_RAM(message_lcd, string_operation2) ;
     LCD_Out(3,-3,message_lcd);
     //Affichage "Caracteriseur"
     text_ROM_to_RAM(message_lcd, string_operation3) ;
     LCD_Out(4,-3,message_lcd);
     do
         {
         if (PORTB.F5==1)
              {
              bouton_ok = 1;
              lance = 1;
              }
          else if (PORTB.F4==1)
              {
              bouton_ok = 1;
              lance = 0;
              }
          else lance = 0;
         }
     while(bouton_ok!=1);
     return(lance);
     }

//Sous programme d'affichage du menu
void Affich_Menu()
     {
     LCD_Cmd(LCD_CLEAR);
     LCD_Cmd(LCD_CURSOR_OFF);
     //Affiche "1: Reinit"
     text_ROM_to_RAM(message_lcd, string_menu1) ;
     LCD_Out(1,1, message_lcd);
     //Affiche "2: Capture"
     text_ROM_to_RAM(message_lcd, string_menu2) ;
     LCD_Out(2,1, message_lcd);
     //Affiche "3: Envoi PC"
     text_ROM_to_RAM(message_lcd, string_menu3) ;
     LCD_Out(3,-3,message_lcd);
     //Affiche "4: Cloture"
     text_ROM_to_RAM(message_lcd, string_menu4) ;
     LCD_Out(4,-3,message_lcd);
     }


//Sous programme d'initialisation du caractériseur
void Init_Carac()
     {
     TRISB=0xFF; //Initialisation de l'afficheur sur le port D du PIC
     LCD_Init(&PORTD);  //Initialisation de l'afficheur LCD sur le portD

     //Affichage des informations du caractériseur
     LCD_Cmd(LCD_CLEAR);
     LCD_Cmd(LCD_CURSOR_OFF);
     //Affichage "CARACTERISEUR"
     text_ROM_to_RAM(message_lcd, string_init1) ;
     LCD_Out(1,2, message_lcd);
     //Affichage "Versioon 1.0.0.1"
     text_ROM_to_RAM(message_lcd, string_init2) ;
     LCD_Out(2,1, message_lcd);
     //Affichage "IUT TARBES"
     text_ROM_to_RAM(message_lcd, string_init3) ;
     LCD_Out(3,0,message_lcd);
     //Affichage "Mr Demarcq"
     text_ROM_to_RAM(message_lcd, string_init4) ;
     LCD_Out(4,0,message_lcd);
     delay_ms(5000);

     //Affichage du menu
     LCD_Cmd(LCD_CLEAR);
     LCD_Cmd(LCD_CURSOR_OFF);
     do
         {
         //Affichage "Branchez le"
         text_ROM_to_RAM(message_lcd, string_branche1) ;
         LCD_Out(1,3,message_lcd);
         //Affichage "puissance"
         text_ROM_to_RAM(message_lcd, string_branche2) ;
         LCD_Out(2,4,message_lcd);
         //Affichage "Puis appuyer"
         text_ROM_to_RAM(message_lcd, string_branche3) ;
         LCD_Out(3,-1,message_lcd);
         //Affichage "Sur un bouton"
         text_ROM_to_RAM(message_lcd, string_branche4) ;
         LCD_Out(4,-1,message_lcd);
         }
     while((PORTB.F1==0) & (PORTB.F2==0) & (PORTB.F4==0) & (PORTB.F5==0));
     delay_ms(100);
     Affich_Menu();
     }


//Sous programme de cloture. Il permet de débrancher le boitier puissance et de
//bloquer le caractériseur en fin de mesures.
void cloture()
     {
     LCD_Cmd(LCD_CLEAR);
     LCD_Cmd(LCD_CURSOR_OFF);
     //Affichage "Debranchez la"
     text_ROM_to_RAM(message_lcd, string_cloture1) ;
     LCD_Out(1,1, message_lcd);
     //Affichage "Puissance"
     text_ROM_to_RAM(message_lcd, string_cloture2) ;
     LCD_Out(2,1, message_lcd);
     //Affichage "Puis eteignez le"
     text_ROM_to_RAM(message_lcd, string_cloture3) ;
     LCD_Out(3,-3,message_lcd);
     //Affichage "Caracteriseur"
     text_ROM_to_RAM(message_lcd, string_cloture4) ;
     LCD_Out(4,-3,message_lcd);
     }

//Sous programme de choix du calibre courant du panneau à controller.
void choix_calibre()
     {
     char choix_calibre=0;
     LCD_Cmd(LCD_CLEAR);
     LCD_Cmd(LCD_CURSOR_OFF);
     //Afficage de "Choix calibre :"
     text_ROM_to_RAM(message_lcd, string_choix_calibre1) ;
     LCD_Out(1,1, message_lcd);
     //Affichage de "1: 24V - 5A"
     text_ROM_to_RAM(message_lcd, string_choix_calibre2) ;
     LCD_Out(2,1, message_lcd);
     //Affichage "2: 24V - 2,5A"
     text_ROM_to_RAM(message_lcd, string_choix_calibre3) ;
     LCD_Out(3,-3,message_lcd);
     //Affichage "Votre choix ?"
     text_ROM_to_RAM(message_lcd, string_choix_calibre4) ;
     LCD_Out(4,-3,message_lcd);
     do
         {
         if (PORTB.F5==1)
              {
              conductance = 2;
              choix_calibre = 1;
              }
          else if (PORTB.F4==1)
              {
              conductance = 1;
              choix_calibre = 1;
              }
          else choix_calibre = 0;
         }
     while(choix_calibre!=1);
     }


//Sous programme de capture. C'est ce programme qui permet de faire les captures
// et de les stocker dans l'eeprom du pic
void capture()
     {
     char lance_ok=0;
     unsigned short i,j = 0, k = 50;
     delay_ms(100);
     choix_calibre();
     lance_ok=lance_operation("Test lancement");
     while(lance_ok)
         {
         for (i=0;i<=NBRE_CONV-1;i++)
             {
             EEprom_Write(i, j++);
             delay_ms(500);
             EEprom_Write(i+50, k--);
             delay_ms(500);
             EEprom_Write(i+100, j++);
             delay_ms(500);
             EEprom_Write(i+150, k--);
             delay_ms(500);
             EEprom_Write(i+200, j++);
             delay_ms(500);
             barre_pourcent("Capture",4,i);
             delay_ms(500);
             }
        }
     delay_ms(100);
     Affich_Menu();
     }

//Sous programme de restitution. Ce programme permet de restituer les mesures
//sauvegardées dans l'eeprom du PIC
void Restitution()
     {
     unsigned short i,j = 0, k = 50, lecture;
     test_communication_RS232();
     ecrire_msg_uart("Caractériseur OK\n");
     for (i=0;i<=NBRE_CONV-1;i++)
         {
         envoi_nombre_uart(i+1);
         ecrire_msg_uart(":");
         lecture = EEprom_Read(i);
         envoi_nombre_uart(lecture);
         ecrire_msg_uart(":");
         lecture = EEprom_Read(i+50);
         envoi_nombre_uart(lecture);
         ecrire_msg_uart(":");
         lecture = EEprom_Read(i+100);
         envoi_nombre_uart(lecture);
         ecrire_msg_uart(":");
         lecture = EEprom_Read(i+150);
         envoi_nombre_uart(lecture);
         ecrire_msg_uart(":");
         lecture = EEprom_Read(i+200);
         envoi_nombre_uart(lecture);
         ecrire_msg_uart("\r\n");
         delay_ms(500);
         barre_pourcent("Envoi PC",3,i);
         }
     delay_ms(100);
     Affich_Menu();
     }


//Programme MAIN . C'est le programme principal du caractériseur, il gère les
//autres sous programmes.
void main()
     {
     char menu=1;
     init_carac();
     USART_Init(9600);
     affich_menu();
     while(menu)
          {
          if (PORTB.F5==1)    //bouton1 = 1
              {
              Init_Carac();   //Réinitialisation de l'afficheur
              }
          else if (PORTB.F4==1)   //bouton2 = 1
              {
              capture();   //Lancement des mesures
              }
          else if (PORTB.F2==1)   //bouton3 = 1
              {
              restitution();;  //Restitution des données sur le PC
              }
          else if (PORTB.F1==1)    //Bouton4 = 1
              {
              menu=0;
              Cloture();
              }
          }

     }

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

Re: Problem

#2 Post by pizon » 19 May 2005 09:24

Code: Select all

 LCD_Out(4,-3,message_lcd);
Lcd_Out() has row and col numbers as unsigned short, so -3 will be interpreted as 253. Pascal would not tolerate this, but hey, this is C(ool) :mrgreen:.

Then again, it might be your 4-row LCD that makes you do that :?:
Last edited by pizon on 19 May 2005 10:20, edited 1 time in total.
pizon

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

Re: Problem

#3 Post by pizon » 19 May 2005 10:18

Also, the array

Code: Select all

char message_lcd[15];
is too short to handle the messages you use - some of them are 16 chars long. So, let it be

Code: Select all

char message_lcd[20];
and you can sleep safely. We did this change, tested the code (on P16F877A), and it seems to work (it has some menu on it, then does some things, responds to keys etc.). Please, next time post the PIC you use, as well as short description (1-3 sentences) about what the programme does.
pizon

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

Re: Problem

#4 Post by pizon » 19 May 2005 10:33

Another issue that improves code performance. The expression

Code: Select all

while((PORTB.F1==0) & (PORTB.F2==0) & (PORTB.F4==0) & (PORTB.F5==0));
takes a lot of stack space (the entire stack amount used by the application goes to 19). Currently, mikroC doesn't do much optimization on such expressions, so you have to think of a more elegant solution yourself. For example, the former expression, might be written as

Code: Select all

while(!(PORTB.F1 | PORTB.F2 | PORTB.F4 | PORTB.F5)) ;
, which reduces application stack from 19 to 13 locations, or

Code: Select all

while(!(PORTB & 0b00110110)) ;
, which further reduces application stack to just 6 locations.
pizon

flourette
Posts: 5
Joined: 17 May 2005 11:49

#5 Post by flourette » 19 May 2005 11:40

Hello I tested your solution but don't work. I use a PIC16F877. My program is useful has to make aquisitions of measurements, to safeguard them then to restore them on the PC. I put messages to facilitate the operations. Is it possible the PIC don't work correctly?

User avatar
rajkovic
mikroElektronika team
Posts: 694
Joined: 16 Aug 2004 12:40

#6 Post by rajkovic » 19 May 2005 13:36

flourette wrote:Hello I tested your solution but don't work.
please be more specific:

1. Is it working at all? (does it write messsages to LCD)
2. Is it respond to commands via portb?
3 Which part of code does not work?

flourette wrote: Is it possible the PIC don't work correctly?
Well it is the last thing to check but it is not impossible.

flourette
Posts: 5
Joined: 17 May 2005 11:49

#7 Post by flourette » 19 May 2005 15:43

There are nothing who work but if i test all function separated they work.

flourette
Posts: 5
Joined: 17 May 2005 11:49

#8 Post by flourette » 20 May 2005 10:48

I have change the PIC but nothing work. I have a headhache because there 2 days i search a solution. If somebody have an idea i thank you in advance.

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

#9 Post by pizon » 20 May 2005 13:10

Here are some pictures from our testing of your source. We have sent you the entire project by mail. Please take a look at it, and send us back your project (at the same address).
Image
Image
Image
Image
pizon

Post Reply

Return to “mikroC General”