View previous topic :: View next topic |
Author |
Message |
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
Strings on RS232 |
Posted: Tue Apr 30, 2013 4:17 pm |
|
|
Dear All,
I have an issue when coming to send and process RS232 strings and integers. Please refer to the program below:
Code: | #include "C:\Users\anthea\Documents\PICC\pic18f4520_serial.h"
#include <string.h>
#include <stdlib.h>
#include <input.c>
#define LCD_ENABLE_PIN PIN_A1
#define LCD_RS_PIN PIN_A3
#define LCD_RW_PIN PIN_A2
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#include <LCD.C>
#define delay4led 3
int count, count2 = 0;
char text1[30]
#int_TIMER1
void TIMER1_isr(void)
{
count++;
if (count == 50)
{
gets(text1);
count = 0;
}
}
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
lcd_init();
printf("\f");
lcd_gotoxy(1,2);
printf("welcom1e");
while(1)
{
output_high(Pin_B1);
printf(text1);
}
} |
I would like to send a string, let say "Country" or Integer "2000" and store it in an eeprom. for my first test, I tried to send a string and print it back on the terminal, but nothing is happening. Can someone tell me where is the problem. I tried to solve the problem alone but I found no more ideas. Your help is highly appreciated.
To tell you the whole story, at first I tried to display the whole integer on LCD and the LCD started to display character by character. I am assuming that this is because I used LCD_putc. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Apr 30, 2013 5:26 pm |
|
|
Seriously reduce the size of your code to test one thing only.
You ask for help with RS232, but your code is littered with LCD, SPI, etc.
I'm confused about what you are trying to do.
Post code with no more than 10 lines.
CCS provides examples to do the jobs you're asking for.
Mike |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Apr 30, 2013 6:14 pm |
|
|
can you imagine a case where
TIMER1_isr() will hang indefinitely ??
i can !!
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed May 01, 2013 1:40 am |
|
|
When I said post code I meant the shortest possible complete and compilable we can copy and paste to test.
Mike |
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
|
Posted: Wed May 01, 2013 2:00 am |
|
|
I tried but no success. Can someone tell me where is the problem? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Wed May 01, 2013 2:43 am |
|
|
I'll list a few problems:
1) Using gets in an interrupt. This is asmboy's comment, and is exactly right.
2) setup_spi. Wrong command. The command to disable the port is 'FALSE'. You are enabling the port.
3) You don't show your #USE RS232 line, but unless this contains ERRORS, the UART will hang if data arrives between the occasions when count==50;
4) Once something has been received, it'll print continuously.
Look at the CCS examples. They show a variety of ways of doing this.
How often are you intending to change the EEPROM?. Remember it's write life limitations. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed May 01, 2013 2:59 am |
|
|
Quote: | I tried but no success. Can someone tell me where is the problem? |
Simple answer. We can't tell easily from your posted code.
I, for one, don't intend to decipher it.
The CCS example EX_STR.C passes strings both ways between PIC & PC.
It's not a good example, but you should be able to pick out the comms part.
Mike |
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
|
Posted: Wed May 01, 2013 3:18 am |
|
|
I took your advice and went back to basics. this is my code so far:
Code: | #include "C:\Users\anthea\Documents\PICC\pic18f4520_serial.h"
#include <string.h>
#include <stdlib.h>
#include <input.c>
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#define STRING_SIZE 51
int count = 0;
char input_str[STRING_SIZE];
#int_TIMER1
void TIMER1_isr(void)
{
}
void main()
{
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
printf("\fwelcome\n");
while(1)
{
printf("\n\r Please enter some text: ");
get_string(input_str, STRING_SIZE);
printf("\n\n You entered: %S", input_str);
delay_ms(2000);
output_toggle(PIN_B2);
}
} |
but the screen just execute "Welcome" and "Please enter some text:" and that's it. I also inserted a toggle Led to check if the code is being executed but still no suceess. |
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
|
Posted: Wed May 01, 2013 3:19 am |
|
|
by the way i referred to ex_str.c example to make this program. As a terminal I am using the hyperterminal. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Wed May 01, 2013 3:38 am |
|
|
Comment:
get_string(input_str, STRING_SIZE);
A string is always _one character longer_ than it's size.
You have 51 characters allocated to the array, so can store a string that is a maximum of 50 characters long.
You need to limit the input size to 'STRING_SIZE-1'.
Almost certainly it is working, but is not seeing the 'end of line'. input_str, 'out of the box', looks for code 13 as the end of line marker. Using standard settings in Hyperterminal, it sends just line feed (code 10) when you hit the 'enter' key.
In Hyperterminal.
File
Properties.
ASCII setup
Top tickbox 'send line ends with line feeds'.
OK
OK
OK
Best Wishes |
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
|
Posted: Wed May 01, 2013 4:19 am |
|
|
thanks Ttelmah. It is working. I also re-open teh hyperterminal because for some reasons it stops responding. I am using Windows 7.
I also succeed to print the whole string on LCD Display as well. This is my final code which is 100% working just in case that another person has the same problem.
Code: | #include "C:\Users\anthea\Documents\PICC\pic18f4520_serial.h"
#include <string.h>
#include <stdlib.h>
#include <input.c>
#define LCD_ENABLE_PIN PIN_A1
#define LCD_RS_PIN PIN_A3
#define LCD_RW_PIN PIN_A2
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
//!
#include <LCD.C>
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#define STRING_SIZE 51
int count = 0;
char input_str[STRING_SIZE];
#int_TIMER1
void TIMER1_isr(void)
{
}
void main()
{
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
lcd_init();
output_B(0x00);
printf("\fwelcome\n");
while(1)
{
printf("\n\r Please enter some text: ");
output_toggle(PIN_B1);
get_string(input_str, STRING_SIZE);
output_toggle(PIN_B0);
printf("\n\n You entered: %S", input_str);
printf(lcd_putc, "%S", input_str);
delay_ms(2000);
output_toggle(PIN_B2);
}
} |
|
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
|
Posted: Thu May 02, 2013 12:57 pm |
|
|
dear all, continued to work on this project. Now I would like to make an interrupt to avoid continuous polling when the slave needs to communicate with my Master PIC.
Am I right that the ISR for RS232 is the RDA? This one can be used also for other protocols such as RS485?
Can I have more that one RDA ISRs such as?
Code: | #int_rda
void rda_isr()
{
}
#int_rda_2
void rda_2_isr()
{
} |
I am assuming that I can distiguish between one RDA to another by include the Stream tag when stating the RS232 definition.
Really thanks for your help and support |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Thu May 02, 2013 1:36 pm |
|
|
The int_rda will only work with hardware serial ports. So yes if your PIC has 2 hardware ports you will have 2 interrupts otherwise just one.
Regards |
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
|
Posted: Thu May 02, 2013 1:45 pm |
|
|
thanks. yes, infact i was going to mention, i i have software uart, what interrupt i can use? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Thu May 02, 2013 2:29 pm |
|
|
Unless already used, you could use INT_EXT (B0) for the software interrupt. There is code here(either Ttelmah or PCM programmer) donated by one of them..to show how to do it.
If possible, use it for the slowest speed serial port.
Other options are using external UART<>I2C, UART<>SPI, 18F46k22,etc.
hth
jay |
|
|
|