|
|
View previous topic :: View next topic |
Author |
Message |
dragontran
Joined: 27 Feb 2014 Posts: 3
|
Need help for PICDEM 2 Plus |
Posted: Thu Mar 27, 2014 3:20 am |
|
|
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
|
|
Posted: Thu Mar 27, 2014 6:22 am |
|
|
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
|
|
Posted: Tue Apr 08, 2014 1:09 am |
|
|
Thank you, it works, but how can i do to get out of INT_RDA? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19590
|
|
Posted: Tue Apr 08, 2014 2:37 am |
|
|
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
|
|
|
dragontran
Joined: 27 Feb 2014 Posts: 3
|
|
Posted: Wed Apr 16, 2014 8:35 am |
|
|
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 |
|
|
|
|
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
|