View previous topic :: View next topic |
Author |
Message |
picgirl08 Guest
|
Sending and Receiving Strings between 18F8720 and 18F4320 |
Posted: Thu Apr 03, 2008 12:17 pm |
|
|
I am trying to send a string from the 18F4320 development board to the 18F8720 and then transmit that string to hyperterminal to display what is being read.
When I look at the output line of the transmitting micro (without them connected) on hyperterminal the string is being sent every few milliseconds.
When I look at the transmit line of the receiving micro to hyperterminal (when they are connected) It appears as though the string is only being received periodically. I was just wondering if I have to change the delay times or is it something else? I have used the kbhit function in the receiving string program.
Any help would be great! Thanks so much!! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
picgirl08 Guest
|
Receiving Microcontroller is a PIC18F8720 |
Posted: Thu Apr 03, 2008 7:25 pm |
|
|
This is the code for the receiving microcontroller, which is a PIC18F8720, 1 rs232 port is receiving a string from the transmitting Micro which is a PIC18F4320 and the other rs232 port of this micro is transmiting the string to hyperterminal. I am using the CCS C compiler Version 4.066
Code: |
char inputstring[10];
void clearstr(void);
void main()
{
long timeout=0;
int i=0; //string count variable
do
{
while(!kbhit()&&(++timeout<25000)) //while nuthing at serial port
// and quarter sec time < 25000
{
delay_ms(10);
}
if((timeout<25000)&&(kbhit()))
{
gets(inputstring); //read in a string from serial port
//store it under inputstring
timeout=0;
}
if(inputstring[i]!='$') //ck to see $ in lead character
{
i++; //if not increment the string index
}
if(inputstring[i]=='$') //when the $ is found do this
{
printf("Found String\r\n %s\r\n", inputstring);
OUTPUT_LOW(PIN_B4); //turn yellow on
i=0;
delay_ms(500);
OUTPUT_HIGH(PIN_B4); //turn off yellow LED
}
clearstr(); //clear the input string buffer
delay_ms(500); //delay 1/2 a second
}while(true);
}//end of main program
void clearstr(void)
{
inputstring[0]=0;
inputstring[1]=0;
inputstring[2]=0;
inputstring[3]=0;
inputstring[4]=0;
return;
}
|
|
|
|
picgirl08 Guest
|
Transmitting Microcontroller is a PIC18F4320 |
Posted: Thu Apr 03, 2008 7:32 pm |
|
|
Here is the code for the transmitting Micro that is sending the string to the PIC18F8720. Basically we are trying to send a continuous string that the receiving micro can look at the characters and respond according to what the characters are.
Code: |
#include <string.h>
int i=0;
char inputstring[10]; //array string of 10 characters long
void main()
{
do
{
i=0;
inputstring[i]='$';
i++;
inputstring[i]='O';
i++;
inputstring[i]='K';
i++;
inputstring[i]=0;
//puts(inputstring);
printf("%s\r\n",inputstring);
}while(inputstring[i]!=0);
inputstring[0]=0;
inputstring[1]=0;
inputstring[2]=0;
inputstring[3]=0;
inputstring[4]=0;
inputstring[5]=0;
}while(true);
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 03, 2008 7:34 pm |
|
|
It's not compilable. It's missing the #include, #fuses, #use delay()
and #use rs232() statements. |
|
|
picgirl.08 Guest
|
sorry |
Posted: Thu Apr 03, 2008 7:38 pm |
|
|
Do I Copy all the Fuses from the .h file? |
|
|
picgirl09 Guest
|
receiving code |
Posted: Thu Apr 03, 2008 7:50 pm |
|
|
Code: | #include <18F8720.h>
#device ICD=TRUE
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOOSCSEN //Oscillator switching is disabled, main oscillator is source
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV25 //Brownout reset at 2.5V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOWAIT //Wait selections unavailable for Table Reads or Table Writes
#FUSES MCU //Microcontroller Mode
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_G1,rcv=PIN_C7,bits=8)
char inputstring[10];
void clearstr(void);
void main()
{
long timeout=0;
int i=0; //string count variable
do
{
i=0;
Output_Low(PIN_A5);
delay_ms(250);
Output_HIGH(PIN_A5);
delay_ms(250);
do
{
while(!kbhit()&&(++timeout<25000)) //while nuthing at serial port
// and quarter sec time < 25000
{
delay_ms(10);
}
if((timeout<25000)&&(kbhit()))
{
gets(inputstring); //read in a string from serial port
//store it under inputstring
timeout=0;
}
if(inputstring[i]!='$') //ck to see $ in lead character
{
i++; //if not increment the string index
}
if(inputstring[i]=='$') //when the $ is found do this
{
printf("Found String\r\n %s\r\n", inputstring);
OUTPUT_LOW(PIN_B4); //turn yellow on
i=0;
delay_ms(500);
OUTPUT_HIGH(PIN_B4); //turn off yellow LED
}
delay_ms(500); //delay 1/2 a second
}while(inputstring[i]!=0);
clearstr(); //clear the input string buffer
}while(true);
}//end of main program
void clearstr(void)
{
inputstring[0]=0;
inputstring[1]=0;
inputstring[2]=0;
inputstring[3]=0;
inputstring[4]=0;
return;
} |
|
|
|
picgirl08 Guest
|
transmitting Code |
Posted: Thu Apr 03, 2008 7:52 pm |
|
|
Code: | #include <18F4320.h>
#device ICD=TRUE
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES XT //Crystal osc <= 4mhz
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES MCLR //Master Clear pin enabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPB //No Boot Block code protection
#FUSES NOWRTB //Boot block not write protected
#FUSES NOWRTC //configuration not registers write protected
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
int i=0;
char inputstring[10]; //array string of 10 characters
void main()
{
do
{
i=0;
inputstring[i]='$';
i++;
inputstring[i]='O';
i++;
inputstring[i]='K';
i++;
inputstring[i]=0;
//puts(inputstring);
printf("%s\r\n",inputstring);
}while(inputstring[i]!=0);
inputstring[0]=0;
inputstring[1]=0;
inputstring[2]=0;
inputstring[3]=0;
inputstring[4]=0;
inputstring[5]=0;
}while(true);
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 03, 2008 8:39 pm |
|
|
For 18F8720:
Quote: |
#use rs232(baud=9600,parity=N,xmit=PIN_G1,rcv=PIN_C7,bits=8) |
This line will generate a software UART. You shouldn't mix pins between
the two hardware UARTs. Either use C6 and C7, or use G1 and G2,.
Also add the ERRORS parameter. Your code has large delays in it
and this parameter will prevent lockups due to receiver overrun errors.
Example:
Code: | #use rs232(baud=9600, xmit=PIN_G1, rcv=PIN_G2, ERRORS) |
With this setting, if the PGM pin (pin B5) goes high, your PIC will lockup.
Change it to this:
Do this in all your programs from now on. (same with ERRORS).
--------------------------
For the 18F4320:
Again, change this to NOLVP. Do this in all future programs as well.
If you're not using Port B for analog inputs, it's best to change this fuse
to NOPBADEN. This configures them as digital i/o pins.
Quote: | #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) |
Again, add the ERRORS parameter. Always do this. |
|
|
picgirl08 Guest
|
thanks but still not receiving string! |
Posted: Thu Apr 03, 2008 9:27 pm |
|
|
I made all of your suggested changes and Both programs are running, but when connected through rs232 cable the receiving end is never entering into the loop below, it just in continously going through the program without returning that true and blinking the yellow LED
Code: | if((timeout<25000)&&(kbhit()))
{
OUTPUT_LOW(PIN_B4); //turn yellow on
delay_ms(250);
OUTPUT_HIGH(PIN_B4); //turn off yellow LED
//printf("Getting String\r\n");
gets(inputstring); //read in a string from serial port
//store it under inputstring
timeout=0;
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 04, 2008 12:18 am |
|
|
Quote: | do
{
i=0;
inputstring[i]='$';
i++;
inputstring[i]='O';
i++;
inputstring[i]='K';
i++;
inputstring[i]=0;
//puts(inputstring);
printf("%s\r\n",inputstring);
}while(inputstring[i]!=0); |
Your code is too complicated.
The following loop sends "$OK", followed by carriage return and linefeed
characters, once every 500 ms.
Code: |
while(1)
{
puts("$OK");
delay_ms(500);
} |
Change your receiver code so it's done in the same simple way.
(Get rid of all the timeout stuff for the initial testing). |
|
|
|