|
|
View previous topic :: View next topic |
Author |
Message |
JEliecer
Joined: 08 Feb 2005 Posts: 17 Location: Bucaramanga - Colombia
|
Routine for RS232 |
Posted: Sat Apr 16, 2005 1:07 pm |
|
|
I need your help.
I have a RS232 routine, but when I send a data by the RS232 to a PIC, the program is Stopped.
This is my code:
#include<18f448.h>
#include<stdlib.h>
#use delay(clock=10000000, RESTART_WDT)
#use RS232(BAUD=9600, BITS=8, PARITY=N, XMIT=PIN_C6, RCV=PIN_C7, RESTART_WDT)
#byte PORTC=0xf82
#include<math.h>
byte year,mes,dow,m_dia,hora,min,seg,sig;
char UNO,DOS,TRES,RAEY,MOY,cmd,x;
int t,i;
char c[];
#include<RTCDS12C887.C>// This is a library for a Real time clock
#include<DRIVERLCD.C>
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)
#int_rda
serial_recibido()
{
lcd_enviar(LCD_COMANDO,LCD_CURSOR_BLINK);
do
{
c[i]=getc(); // Get a char from the PC
delay_ms(10);
i+=1;
}
while (c[i]!=0);
lcd_putc("\f"); // Display it on the LCD
lcd_putc(c); // Display it on the LCD
delay_ms(8000);
lcd_enviar(LCD_COMANDO,LCD_DISPLAY_OFF);
lcd_enviar(LCD_COMANDO,LCD_DISPLAY_ON);
disable_interrupts(global);
disable_interrupts(int_rda);
return;
}
void main()
{
i=0;
rtc_inicio();
lcd_init();
restart_wdt();
set_tris_c(191);
while(1)
{
restart_wdt();
enable_interrupts(global);
enable_interrupts(int_rda);
leer_f( m_dia, mes, year, sig,dow);
leer_h( hora, min, seg );
switch (dow)
{
case 1:printf(lcd_putc,"\f SUN"); break;
case 2: printf(lcd_putc,"\f MON"); break;
case 3: printf(lcd_putc,"\f TUE"); break;
case 4: printf(lcd_putc,"\f WED"); break;
case 5: printf(lcd_putc,"\f THU"); break;
case 6: printf(lcd_putc,"\f FRI"); break;
case 7: printf(lcd_putc,"\f SAT"); break;
}
printf(lcd_putc," %2X/%2X/20%2X\",m_dia,mes,year);
printf(lcd_putc,"\n %2X:%2X:%",hora,min,seg);
delay_ms(800);
}
} |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Sat Apr 16, 2005 1:31 pm |
|
|
Spotted several issues.
1) Why do you keep calling enable_interrupts() inside your main's while(1) loop? Call these once before the while loop. The compiler generates the necessary code for entering and exiting the ISR to disable/re-enable interrupts.
2) DON'T do so much in your receive character interrupt. Interrupts should be very short, quick routines. Calling out to other functions, calling delay_ms().
3) When you leave your interrupt don't need to be disabling the interrupts. It makes no sense. See #1.
4) The "return" keyword is not necessary at the end of your uart receive ISR.
5) Your statment char c[] at the beginning of your program isn't allocating more than one byte for that array. Later in your ISR when you are indexing that array you are GUARANTEED to be stepping on other data. Not only did you not allocate memory for the array (so the compiler knows where to put other variables in RAM) you aren't testing the index variable "i" for boundary conditions.
6) USE THE "CODE" BUTTON. This preserves the formating of your code when posting. Just pasting it into your post, you loose the leading spaces/tabs on each line. Makes it much harder to read. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
JEliecer
Joined: 08 Feb 2005 Posts: 17 Location: Bucaramanga - Colombia
|
Routine for Rs232 |
Posted: Mon Apr 18, 2005 8:25 am |
|
|
This is my new code:
Code: |
#include<16f877a.h>
#include<stdlib.h>
#use delay(clock=10000000, RESTART_WDT)
#use RS232(BAUD=9600, BITS=8, PARITY=N, XMIT=PIN_C6, RCV=PIN_C7, RESTART_WDT)
#include<LCDPIC16E.C> //Driver for lcd 2*24
#byte PORTC=7
#int_rda
serial_recibido()
{
unsigned char c;
c=getc();
delay_ms(10); //Send the char to the lcd
lcd_putc(c);
}
void main()
{
lcd_init();
set_tris_c(176);
enable_interrupts(int_rda);
enable_interrupts(global);
lcd_putc("\ HOLA JORGE\n");
while(1)
{
restart_wdt();
}
}
|
but this code only allows me to see the first three characters of the received string.
What should I do to be able to see all the received characters? |
|
|
Darren Rook
Joined: 06 Sep 2003 Posts: 287 Location: Milwaukee, WI
|
Re: Routine for Rs232 |
Posted: Mon Apr 18, 2005 8:55 am |
|
|
JEliecer wrote: | This is my new code:
but this code only allows me to see the first three characters of the received string.
What should I do to be able to see all the received characters? |
Get rid of the delay() inside your interrupt. While you are delaying inside your interrupt there are characters being received by your PICs usart peripheral, but since you are delaying and not pulling it out of the buffer there is a buffer overrun and the peripheral is shut off. |
|
|
JEliecer
Joined: 08 Feb 2005 Posts: 17 Location: Bucaramanga - Colombia
|
Routine for Rs232 |
Posted: Mon Apr 18, 2005 5:45 pm |
|
|
Thanks boys. Now, I have a routine for receive the characters from the rs232.
But now, I need to convert streams of numbers to whole numbers of 8 bits.
As I can record the data received in a variable, to transform it later into integers or long integers numbers....???? _________________ Jorge Eliécer Castro
"THE UNIVERSE FOREVER CONSPIRES SO THAT OUR DESIRES ARE WILL BE REALITY"
"EL UNIVERSO SIEMPRE CONSPIRA PARA QUE NUESTROS DESEOS SE HAGAN REALIDAD" |
|
|
Darren Rook
Joined: 06 Sep 2003 Posts: 287 Location: Milwaukee, WI
|
|
Posted: Mon Apr 18, 2005 8:02 pm |
|
|
Yes, look at atoi() |
|
|
JEliecer
Joined: 08 Feb 2005 Posts: 17 Location: Bucaramanga - Colombia
|
|
Posted: Tue Apr 19, 2005 9:42 am |
|
|
I renewed my code in this way:
Code: |
#include<18f448.h>
#include<stdlib.h>
#use delay(clock=20000000, RESTART_WDT)
#use RS232(BAUD=9600, BITS=7, PARITY=N, XMIT=PIN_C6, RCV=PIN_C7, RESTART_WDT)
#include<DRIVERLCD.C>
//byte hora,min,seg;
//#include<DRIVERRTC.C>
#byte PORTC=0Xf82
char c;
int VECT[];
int i=0;
#int_rda
void RDA_isr()
{
c=getc();
lcd_putc(c);
VECT[i]=atoi(c); //this part
lcd_putc(VECT[i]); //isn't
i+=1; //functional
}
void main()
{
lcd_init();
set_tris_c(176);
enable_interrupts(int_rda);
enable_interrupts(global);
lcd_putc("\f HOLA JORGE\n");
while(1)
{
restart_wdt();
}
}
|
I'm transmit strings of numbers and I need to convert it into integers.
But now, I can't watch the received data in the LCD.
How I can use the atoi(),atol(), and the atoi32() functions. to converts strings into integers.
I need your help.
Thanks in advanced by your help. _________________ Jorge Eliécer Castro
"THE UNIVERSE FOREVER CONSPIRES SO THAT OUR DESIRES ARE WILL BE REALITY"
"EL UNIVERSO SIEMPRE CONSPIRA PARA QUE NUESTROS DESEOS SE HAGAN REALIDAD" |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Apr 19, 2005 10:08 am |
|
|
Um, atoi() converts a piece of text to a number. You usually write text to displays otherwise you might not see it. Also, atoi() takes a pointer to a null terminated string. You are passing it a char which is incorrect. I think what you probably want to do it receive the chars into an array and then convert that array to an integer. |
|
|
DragonPIC
Joined: 11 Nov 2003 Posts: 118
|
|
Posted: Tue Apr 19, 2005 11:50 am |
|
|
JEliecer wrote: |
I need your help. |
Come on vominos, everybody let's go. Come one lets get to it. Come one there's nothing to it.
Sorry, my daughter is always watching that Dora show.
You didn't listen to rwyoung "5) Your statment char c[] at the beginning of your program isn't allocating more than one byte for that array."
When initializing "int VECT[];", allocate some memory like "int VECT[16];" with the largest expected size string in mind for the array size. _________________ -Matt |
|
|
JEliecer
Joined: 08 Feb 2005 Posts: 17 Location: Bucaramanga - Colombia
|
|
Posted: Tue Apr 19, 2005 6:07 pm |
|
|
This is a new code....
Code: |
#include<18f448.h>
#include<stdlib.h>
#use delay(clock=20000000, RESTART_WDT)
#use RS232(BAUD=9600, BITS=7, PARITY=N, XMIT=PIN_C6, RCV=PIN_C7, RESTART_WDT)
#include<DRIVERLCD.C>
#byte PORTC=0Xf82
byte c;
int j,i=0;
byte X[];
#int_rda
void RDA_isr()
{
c=getc();
if ((c>=0X30)&&(c<=0X39))
{
i+=1;
j=c&0b00001111;
printf(lcd_putc,"%u",j);
X[i]=c;
}
}
void main()
{
i=0;
lcd_init();
set_tris_c(176);
enable_interrupts(int_rda);
enable_interrupts(global);
lcd_putc("\f HOLA JORGE\n");
delay_ms(3000);
while(1)
{
restart_wdt();
}
}
|
With this code I can watch in the LCD all the characters that I send by the RS232 to the PIC. But it doesn't allow me to stay all the numbers in an array.
It alone allows to save two digits.
That I can make to save, for example 20 data in an array...???
Thanks you guys. _________________ Jorge Eliécer Castro
"THE UNIVERSE FOREVER CONSPIRES SO THAT OUR DESIRES ARE WILL BE REALITY"
"EL UNIVERSO SIEMPRE CONSPIRA PARA QUE NUESTROS DESEOS SE HAGAN REALIDAD" |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Apr 19, 2005 8:05 pm |
|
|
So you convert it from a string, to a number, and then back to a string? Kind of inefficient don't you think.
Quote: | I think what you probably want to do it receive the chars into an array and then convert that array to an integer. |
|
|
|
JEliecer
Joined: 08 Feb 2005 Posts: 17 Location: Bucaramanga - Colombia
|
Routine for Rs232 |
Posted: Wed Apr 20, 2005 7:45 am |
|
|
Yes, I want to do receive the chars into an array and then convert that array to an integer.
The string only have numbers.
But I can't do it. How can I do...???
Please help me boys. _________________ Jorge Eliécer Castro
"THE UNIVERSE FOREVER CONSPIRES SO THAT OUR DESIRES ARE WILL BE REALITY"
"EL UNIVERSO SIEMPRE CONSPIRA PARA QUE NUESTROS DESEOS SE HAGAN REALIDAD" |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Apr 20, 2005 9:23 am |
|
|
As we "boys" have told you, use the atoi() function on the array of chars! Don't do it on every character, do it on the whole string. Clear as mudd yet? |
|
|
DragonPIC
Joined: 11 Nov 2003 Posts: 118
|
|
Posted: Thu Apr 21, 2005 9:26 am |
|
|
Code: |
char c[4];
int num;
...
gets(c); //gets a string from rs232 and will insert null termination at end of string with a \r sent at end of # from data supplier
num = atoi(c); //converts string to int
printf(lcd_putc,"%u",num); //sends number to lcd
....
|
and adjust the size of the array and size of num to fit the highest expected # received from RS232 port.
Notice that whatever is sending the data needs to send a return when all data is finished transmission. For example, on the other side of the rs232 (the transmitter) the code could be wriitten like this:
or
Code: | printf("%u\r",number); |
Did this help?[/code] _________________ -Matt |
|
|
JEliecer
Joined: 08 Feb 2005 Posts: 17 Location: Bucaramanga - Colombia
|
Routine for Rs232 |
Posted: Thu Apr 21, 2005 2:55 pm |
|
|
Hi,
I need your help again....
I wrote the code with yours indications, but it doesn't still work correctly...
I no know where I should define the char C[4]
Code: | #include<18f448.h>
#include<stdlib.h>
#use delay(clock=20000000, RESTART_WDT)
#use RS232(BAUD=9600, BITS=7, PARITY=N, XMIT=PIN_C6, RCV=PIN_C7, RESTART_WDT)
#include<DRIVERLCD.C>
#byte PORTC=0Xf82
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
byte i;
int indice;
static unsigned int V[20];
#int_rda
void RDA_isr()
{
char c[20];
int dir=0;
do
{
gets(c);
if ((c>=0X30)&&(c<=0X39))
{
i=c&0X0F;
lcd_putc(i);
V[dir]=i;
dir++;
}
}
while(c!=0);
indice=dir;
}
void main()
{
lcd_init();
set_tris_a(0);
set_tris_c(176);
enable_interrupts(int_rda);
enable_interrupts(global);
lcd_putc("\f HOLA JORGE\n");
while(1)
{
restart_wdt();
for(i=0;i<=indice;i++)
{lcd_putc("\f"); lcd_putc(V[i]); delay_ms(1500);}
}
}
|
I am sending the strings with the calculating HP 48G. How I can insert the character '\ r' at the end of the string with the calculator.
In the forever loop, I can't visualize the characters of the V [i] array.
When I test this program the "ASCII 127" appears in the first position of the Lcd.
How I can make to solve this problem...???? _________________ Jorge Eliécer Castro
"THE UNIVERSE FOREVER CONSPIRES SO THAT OUR DESIRES ARE WILL BE REALITY"
"EL UNIVERSO SIEMPRE CONSPIRA PARA QUE NUESTROS DESEOS SE HAGAN REALIDAD" |
|
|
|
|
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
|