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

Need help for PICDEM 2 Plus

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



Joined: 27 Feb 2014
Posts: 3

View user's profile Send private message

Need help for PICDEM 2 Plus
PostPosted: Thu Mar 27, 2014 3:20 am     Reply with quote

Hello,first sorry for my bad english, actually, I'm student and i have a project to complete with PICDEM 2 plus. My project is about to measure the volume of waste from a skip with SRF02 Ultrasonic range finder http://www.robot-electronics.co.uk/htm/srf02techI2C.htm.

I have to send a frame from my PICDEM to pc via RS232, i made a sample of code and it works well, but i don't know how to do to enter in a function without enter a loop.... Like when i send a command from Visual studio, the PIC will enter into a function automatic and send back a frame of measure.

Here is my code.

Code:
#include <18F452.H>

#fuses XT,NOWDT,NOLVP
#use delay (clock=4000000)
#use fast_io (B) //on garde la maîtrise des registres TRISB

#include "flex_lcd.c"
#include "button.c"


#define SRF02_SDA  PIN_C4
#define SRF02_SCL  PIN_C3

#use i2c(master, sda=SRF02_SDA, scl= SRF02_SCL)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

int8 A4 = 0;   // For the button on pin A4
int8 B0 = 0;   // For the button on pin B0

void Ecrire(BYTE P_Adresse_Capteur, BYTE P_Adresse_Reg,BYTE P_Valeur);
BYTE Lire(BYTE P_Adresse_Capteur,BYTE P_Adresse_Reg);
unsigned int16 Mesurer(BYTE P_adresse_capteur);
void i2c_init();
void button_init();
float Volume(unsigned int* p_distance);

void main()
{   
   ///////////////////////////////////////
   ///////Déclaration des variable ///////
   ///////////////////////////////////////
   BYTE L_adresse_Capteur;
   unsigned int L_Distance;
    int i=1;
   float resultat=0;
   char letter[30];
   lcd_init();              // Initialiser Ecran LCD
   i2c_init();
   button_init();

   lcd_putc("\fSonar Sensor\n");
   /*test send/receive//////////////////
   while(1)
   {
      gets(letter);
      printf(lcd_putc,"\f%s  \n",letter);
      printf("recu");
   }
   *////////////////////////////////////
   while(1)
   {
      for(L_adresse_Capteur = 0xE2;L_adresse_Capteur <=0xEC;L_adresse_Capteur+=2)
      {      
         delay_ms(200);
         L_Distance=Mesurer(L_adresse_Capteur);   
         printf(   lcd_putc,"dist %x %ucm\n",L_adresse_Capteur,L_Distance);
         printf("Distance %u cm %x\r\n ",L_Distance,L_adresse_Capteur);
         delay_ms(1000);
         resultat = resultat + L_Distance;   
         i++;      
      }
        //Les capteurs s'installent  2m au dessus du fond de la benne
          // resultat /200 x100 = % remplissage de la benne
        resultat = (resultat/i)/ 2;
        printf(lcd_putc,"\f%3.2f%%  \n",resultat);
        i=1; 
   };   
 
}



void button_init()
{
   set_tris_a(0x10);  // Set Pin A4 as input
   set_tris_b(0x01);  // Set Pin B0 as input
}
void i2c_init()
{
   output_float(SRF02_SDA);
   output_float(SRF02_SCL);   
}

void Ecrire(BYTE P_Adresse_Capteur, BYTE P_Adresse_Reg, BYTE P_Valeur)
{
   i2c_start();
   i2c_write(P_Adresse_Capteur); //Acces en écriture
   i2c_write(P_Adresse_Reg);    //Dans le registre de commande $00
   i2c_write(P_Valeur);    //Pour une mesure en cm $51
   i2c_stop();
}
BYTE Lire(BYTE P_Adresse_Capteur, BYTE P_Adresse_Reg)
{
   BYTE L_data;
   L_data = 0;
   i2c_start();
   i2c_write(P_Adresse_Capteur);
   i2c_write(P_Adresse_Reg);          //Acces en écriture du nume registre à lire
   i2c_start();
   i2c_write((P_Adresse_Capteur+1));    //Acces en lecture
   L_data = i2c_read(0);
   i2c_stop();
   return L_data;
}

unsigned int16 Mesurer(BYTE P_adresse_capteur)
{
   unsigned int16 L_mesure;
   BYTE L_Msb,L_Lsb;
   L_mesure = L_Msb = L_Lsb=0;
   
   Ecrire(P_adresse_capteur,0x00,0x51); //demande de mesure en cm
   delay_ms(100);
   L_Msb=Lire((P_adresse_capteur),0x02); //Acces au poids fort de la mesure
   L_Lsb=Lire((P_adresse_capteur),0x03); //Acces au foids faible de la mesure
   L_mesure = (L_Msb*256)+L_Lsb;
   return(L_mesure);
 
}

float Volume(unsigned int* p_distance)
{
    float resultat=0;
    short int i;
    for (i=0;i<6;i++)
    {
        resultat = p_distance[i]+ resultat;
    }
    resultat = resultat/6;
    //Les capteurs s'installent  2m au dessus du fond de la benne
    // resultat /200 x100 = % remplissage de la benne
    if(resultat >=195 )
    {
        resultat = 0;
    }
    else
    {
        resultat = resultat / 200 * 100;
    }
    return resultat;
}

ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Thu Mar 27, 2014 6:22 am     Reply with quote

Hi,

I'm not 100% sure what you are asking, but I think you want your PIC program to respond when queried by Visual Studio program running on your PC?

If so, I'd start off very simple. Create a serial interrupt (Int_RDA) that reads a character from the UART. If that character is the desired value, say a 'Q', then set a boolean flag, say 'bQueryRcvd', as True. Inside Main() in your While(1) loop, do a test for bQueryRcvd, and if it's true, first set this flag to False, and then send out your data using printf. The flag should be defined as a global variable, and you should also add 'Errors' to your #use rs232 statement!

Hope this helps!

John
dragontran



Joined: 27 Feb 2014
Posts: 3

View user's profile Send private message

PostPosted: Tue Apr 08, 2014 1:09 am     Reply with quote

Thank you, it works, but how can i do to get out of INT_RDA?
Ttelmah



Joined: 11 Mar 2010
Posts: 19436

View user's profile Send private message

PostPosted: Tue Apr 08, 2014 2:37 am     Reply with quote

You need to 'separate' your thinking.

First start by looking at ex_sisr.c. This shows how to receive data using the RDA interrupt, and store it into a buffer.

They get it clear in your head how thick a processor is, and how it has to approach your problem. There are a number of different things you could do, depending on how many different messages you want to send, and how complex the replies need to be. Examples of all have been posted here, I've done a 'state machine' based table parser, and other people have done similar approaches, and also simpler ones using string tests on carriage return terminated lines etc.. If your messages are really simple, then you could just use a single letter of the alphabet, to trigger the reply with a simple 'switch' statement. It all depends how complex your comms need to be.

You main loop, should sit looping doing whatever jobs the chip needs to be doing, and in the loop, test for a character having arrived. When this happens it then calls your parser, and if the message is complete, does what has been asked, or if it is incomplete, goes back to looping, waiting for the whole message to arrive.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 08, 2014 3:31 pm     Reply with quote

This thread has links to Ttelmah's #int_rda state machine parser
and to another one:
http://www.ccsinfo.com/forum/viewtopic.php?t=42637

This thread has links to multi-tasking sample code:
http://www.ccsinfo.com/forum/viewtopic.php?t=39502
dragontran



Joined: 27 Feb 2014
Posts: 3

View user's profile Send private message

PostPosted: Wed Apr 16, 2014 8:35 am     Reply with quote

Thank you, it works for me now, i've been sick for week so i didn't do anything for my program but the int_rda works well now
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