|
|
View previous topic :: View next topic |
Author |
Message |
Koketso
Joined: 05 May 2011 Posts: 4
|
using gets() |
Posted: Mon Nov 07, 2011 5:46 pm |
|
|
Hi guys, I am trying to read a string using gets(). The string is from a pic16f690 to a pic18f4520, which are connected with a wired communication on the uarts. The problem is that I am not receiving anything. When I test if my source microcontroller is transmitting I connect it to my PC and view the transmitted string on terminal. But when connecting the destination microcontroller to PC to view if I am receiving a string there is nothing showing. I have checked everything including my connections and its ok, please help asap!!
Thanx in advance!!
Source code.
Code: |
#include <16f690.H>
#fuses INTRC_IO, NOWDT, NOBROWNOUT, PUT, NOMCLR
#DEVICE ADC=10
#use delay (clock=8000000)
#use rs232(baud=9600, xmit=PIN_C4,rcv=PIN_C5)
#include <ds1307.c>
#include <MATH.H>
#include <string.h>
int light();
int humidity();
int temperature();
int Temp2,Humid2,Intens2;
int Temp,Humid,Intens;
char Message[32];
BYTE sec;
BYTE min;
BYTE hrs;
BYTE day;
BYTE month;
BYTE yr;
BYTE dow;
/////////////////FUNTION MAIN BEGINS//////////////////////////
void main ()
{
ds1307_init();
while(1)
{
if(hrs==00 && min==00 && sec==00)
{
ds1307_set_datetime(7,11,11,2,6,55,50);
}
else
{ null;
}
ds1307_get_date(day,month,yr,dow);
ds1307_get_time(hrs,min,sec);
// printf("Date : %02d/%02d/%02d",day,month,yr);
//printf("Time : %02d:%02d:%02d",hrs,min,sec);
delay_ms(1000);
Temp=temperature();
Humid=humidity();
Intens=light();
sprintf(Message,"<,%02d/%02d/%02d,%02d:%02d:%02d,%d,%d,%d,>\r",day,month,yr,hrs,min,sec,Temp,Humid,Intens);
puts(Message);
//printf(Message);
}
}
///////////////////////////////////LIGHT FUNCTION////////////////////////////////////////////////
int light()
{
float value2;
float Intens1;
setup_adc_ports(sAN2);
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel( 2 );
value2=read_adc();
Intens1= (value2/1024)*(500.0)/100.0;
Intens1= (Intens1+1.6258)/3.25;
Intens2=ceil(Intens1);
return Intens2;
}
//////////////////////////////////END OF LIGHT FUNCTION///////////////////////////////////////////
//////////////////////////////////TEMPERATURE FUNCTION////////////////////////////////////////////
int temperature()
{
float value0;
float Temp1;
setup_adc_ports(sAN0);
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );
value0=read_adc();
Temp1= (value0/1024)*((500.0)/100.0);
Temp1=100*Temp1;
Temp2=ceil(Temp1);
return Temp2;
}
///////////////////////////////END OF TEMPERATURE FUNCTION//////////////////////////////////////////
///////////////////////////////HUMIDITY FUNCTION////////////////////////////////////////////////////
int humidity()
{
float value1;
float Humid1;
setup_adc_ports(sAN1);
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel( 1 );
value1=read_adc();
Humid1= (value1/1024)*((500.0)/100.0);
Humid1=100*Humid1;
Humid2=ceil(Humid1);
return Humid2;
}
/////////////////////////////////END OF HUMIDITY FUNCTION/////////////////////////////////////////////
|
RECEIVER CODE:
Code: |
#include <18f4520.h> // Define PIC Microcontroller Header
#fuses INTRC_IO, NOWDT, NOBROWNOUT, PUT, NOMCLR
#use delay (clock=8000000) // Set 8MHz Oscillator For Microcontroller
#use rs232(baud=9600, xmit=PIN_B2,rcv=PIN_B1)
//#include <ctype.h>
#include <lcd.c> // Define LCD Header
#include <MATH.H>
#include <string.h>
#include <stdlib.h>
char Message[32];
char arTemp[2];
char arHumid[2];
char arIntens;
int Temp;
int Humid;
int Intens;
void main()
{
lcd_init();
lcd_putc("\fReady...\n");
while(1)
{
delay_ms(1000);
gets(Message);
// delay_ms(500);
puts(Message);
printf("%s",Message);
sprintf(arTemp,"%d%d",Message[20],Message[21]);
sprintf(arHumid,"%d%d",Message[23],Message[24]);
sprintf(arIntens,"%d",Message[26]);
Temp=atoi(arTemp);
Humid=atoi(arHumid);
Intens=atoi(arIntens);
// if(Temp<15)
// {
// lcd_gotoxy(1,1);
// lcd_putc("TEMPERATURE TOO LOW!!");
// //delay_ms(1000);
// output_toggle(PIN_B5);
// }
// else if(Temp>40)
// { lcd_gotoxy(1,1);
// output_toggle(PIN_B5);
// lcd_putc("TEMPERATURE TOO HIGH!!");
// }
// else{null;}
//
//if(Humid<35)
// {
// lcd_gotoxy(1,2);
// lcd_putc("HUMIDITY TOO LOW!!");
// //delay_ms(1000);
// output_toggle(PIN_B5);
// }
// else if(Humid>60)
// { lcd_gotoxy(1,2);
// output_toggle(PIN_B5);
// lcd_putc("HUMIDITY TOO HIGH!!");
// }
// else{null;}
//
//if(Intens<1)
// {
// lcd_gotoxy(1,3);
// lcd_putc("INTENSITY TOO LOW!!");
// // delay_ms(1000);
// output_toggle(PIN_B5);
// }
// else if(Temp>2)
// { lcd_gotoxy(1,3);
// output_toggle(PIN_B5);
// lcd_putc("INTENSITY TOO HIGH!!");
// }
// else{null;}
}
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Tue Nov 08, 2011 6:05 am |
|
|
quick comments..
1) always add 'errors' to the USE RS232(....) options. Right now the PIC's UART is halting , due to framing errors( too much data at once)
2) add a delay( delay_ms(2000) ? ) in the transmitter program main 'loop' to allow the receiver time to digest the incoming data...
3) use an ISR for the receiver's gets() to buffer incoming data.
4) if possible increase clock to 20MHz or slow the baudrate down to say 1200. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Nov 08, 2011 7:38 am |
|
|
A few more comments: Code: | char arTemp[2];
.
.
sprintf(arTemp,"%d%d",Message[20],Message[21]); | Here you have a major bug where you are overwriting memory.
%d will try to print an int8 variable in the range 0 - 255. You have this two times. And the resulting string will be terminated by a null character.
Total result will be maximum: "255255" + terminating 0 = 7 characters.
You only declared space for two characters.
Same bug is present a few more times.
What you want to do is to convert the received text to integers, for this you best can use the opposite function of sprintf: the sscanf function.
Replace: Code: | sprintf(arTemp,"%d%d",Message[20],Message[21]);
sprintf(arHumid,"%d%d",Message[23],Message[24]);
sprintf(arIntens,"%d",Message[26]);
Temp=atoi(arTemp);
Humid=atoi(arHumid);
Intens=atoi(arIntens);
| by: Code: | sscanf(&Message[20], "%d,%d,%d", &Temp, &Humid, &Intens); |
Not tested, so there might be a problem left in the above example code but it should get you going.
O yes, in the receiver code get rid of the long delay functions. Gets() will wait till a newline character is read, so no need for confusing extra delays. |
|
|
|
|
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
|