|
|
View previous topic :: View next topic |
Author |
Message |
hoangkhuong
Joined: 16 Mar 2012 Posts: 31
|
dsPic30 Trouble with RS232 |
Posted: Tue Apr 10, 2012 10:41 am |
|
|
I use Hyperterminal to transfer and receive data between PC and dsPic. I want to transfer text such as "Hello" from PC to dsPic but so far,i can only transfer one character at a time.
Here is my code, hope somebody can help me adjust it so I can transfer and receive a string of value instead of one character at a time. Thanks very much.
Code: | #include <30F4011.h>
#DEVICE ADC=10
#include <stdlib.h>
#include <can-dsPIC30.h>
#include <can-dsPIC30.c>
#FUSES FRC,NOWDT
#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=pin_f3,rcv=pin_f2,bits=8,ERRORS)
char array[10];
int8 n=0;
int8 flag=0;
#INT_RDA
void receive()
{
array[n]=getc();
if (array[n]=='a')
{
flag = 1;
n = 0;
}
else n++;
if (n>9) n = 0;
}
void main()
{
set_tris_b(0x00);
set_tris_d(0x00);
while(true)
{
if (flag==1)
{
flag = 0;
for (n=0;n<9;n++)
{
printf("%c",array[n]);
}
}
}
} |
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Apr 10, 2012 2:43 pm |
|
|
(1) What are you expecting your code to do?
(2) What is it doing?
(3) What is it not doing?
(4) How do you think it works?
Mike |
|
|
hoangkhuong
Joined: 16 Mar 2012 Posts: 31
|
|
Posted: Tue Apr 10, 2012 7:56 pm |
|
|
Thanks for your concerns.
1/ I want to transfer a string like: "Hello" from PC to dsPic and the dsPic can turn the string back to PC.
2/ and 3/ if I just use the char c = getc() and putc(c) functions, both PC and dsPic can transfer and receive single character well, but when I apply an array to contains different characters of a string, it doesn't work.
4/ In my code, I use the interrupt RDA to receive character transferred from the PC, and assigned it to an array, then put it back to the PC.
Hope you can figure my problem. Thanks
P/S: I use a character 'a' as a stop bit and a flag as a indicator for when to put the string back to PC. And it appeared that the flag never change to 1, since the PC didn't receive anything back. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Apr 11, 2012 2:40 am |
|
|
I dont have 30F4011, so compiled on an 18F458.
When I enabled global and RDA interrupts, code worked as you described.
I think you may get strange behaviour if you don't initalise the array contents to known values.
Mike |
|
|
hoangkhuong
Joined: 16 Mar 2012 Posts: 31
|
|
Posted: Wed Apr 11, 2012 10:32 am |
|
|
Really ? I inserted an assignment for array[n] = ' ' or array[n] = 0 before the while loop but still nothing happen, I use LED to check whether flag goes onto 1, but it doesn't show that. I change the printf function to putc(array[n]) but it is also not working. I'm stuck now :( |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Wed Apr 11, 2012 10:44 am |
|
|
Did you enable both RDA and GLOBAL interrupts like Mike suggested? You don't have it in your code above. |
|
|
hoangkhuong
Joined: 16 Mar 2012 Posts: 31
|
|
Posted: Wed Apr 11, 2012 10:57 am |
|
|
Here is my complete code. Sorry for not posting enough:
Code: | #include <30F4011.h>
#DEVICE ADC=10
#include <stdlib.h>
#include <can-dsPIC30.h>
#include <can-dsPIC30.c>
#FUSES FRC,NOWDT
#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=pin_f3,rcv=pin_f2,bits=8,ERRORS)
#include "LCD_4bit.h"
char array[1];
int8 n=0;
int8 flag=0;
#INT_RDA
void receive()
{
array[n]=getc();
if (array[n]=='a')
{
flag = 1;
n = 0;
}
else n++;
if (n>9) n = 0;
}
void main()
{
char c;
set_tris_b(0x00);
set_tris_d(0x00);
enable_interrupts(INT_RDA);
enable_interrupts(INTR_GLOBAL);
lcd_init();
delay_ms(10);
lcd_gotoxy(1,1);
printf(lcd_putc,"Ready",);
delay_ms(1000);
for (n=0;n<9;n++)
{
array[n] = 0;
}
while(true)
{
if (flag==1)
{
flag = 0;
for (n=0;n<9;n++)
{
printf("%c",array[n]);
}
}
}
} |
Really appreciate your help |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Wed Apr 11, 2012 11:16 am |
|
|
Try something without the LCD, can, etc. first:
Code: |
#include <30F4011.h>
#FUSES FRC,NOWDT
#use delay(clock=8000000)
#use rs232(UART1,baud=9600,parity=N,bits=8,stop=1,ERRORS)
#define BUFFER_SIZE 10
char array[BUFFER_SIZE] = {0};
int8 n=0;
int8 flag=0;
#INT_RDA
void receive()
{
array[n]=getc();
if (array[n]=='a'){
flag = 1;
n = 0;
}
else{
n++;
}
if (n>=BUFFER_SIZE){
n = 0;
}
}
void main()
{
char c;
enable_interrupts(INT_RDA);
enable_interrupts(INTR_GLOBAL);
delay_ms(1000);
printf("Ready: ");
delay_ms(1000);
while(TRUE){
if (flag){
flag = 0;
for (n=0;n<BUFFER_SIZE;n++){
printf("%c",array[n]);
}
}
}
}
|
Type in fedbca slowly in hyperterminal and see if it prints it back. Also make sure hyperterminal has flow control turned off. Bear in mind, the code as is will dump the whole buffer out, not just those 3 characters. I assume you want it to do that as that is what you had before.
Tell us what the output on hyperterminal is when you run that test. |
|
|
hoangkhuong
Joined: 16 Mar 2012 Posts: 31
|
|
Posted: Wed Apr 11, 2012 8:52 pm |
|
|
When I use your code, here the word Ready appears on HyperTerminal after pressing reset button several times:
http://imageshack.us/photo/my-images/580/captureigt.jpg
I use another tool to communicate called Terminal v1.9. Here is the download link, it is easy to use and robust, but the result is not much different. If I transmit one character at a time, it display correctly for the first two or three times, then some unknown characters like "*" or "#" comes up. I don't know what happen, and of course, when I send the string u said "fedba" nothing show up on the receive area :(
Here is the download link for Terminal v1.9b:
http://hw-server.com/software/termv19b.html
or
https://sites.google.com/site/terminalbpp/
Thanks |
|
|
hoangkhuong
Joined: 16 Mar 2012 Posts: 31
|
|
Posted: Wed Apr 11, 2012 9:38 pm |
|
|
I am also need RS232 to read a ID number of a RFID tag. The RFID module is working fine, it transfers the ID number to PC like this:
ID:0008167338
Now I need my dsPic to be able to read this ID and do something with it. But so far, the LCD on dsPic kit only show unknown characters :(
Thanks and I appreciate any of your concerns
The code is simply like this:
Code: | for (i=0;i<13;i++)
{
c[i] = 0;
}
while(true)
{
lcd_gotoxy(1,1);
for (i=0;i<13;i++)
{
c[i] = getc();
printf(lcd_putc,"%c",c[i]);
}
} |
One more thing, on the RFID module, it uses 20MHz crystal, do I need to use the same frequency on dsPic kit, since I use 8Mhz on dsPic kit. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Thu Apr 12, 2012 5:58 am |
|
|
You should get the RS232 working before you try to tackle the LCD. If you haven't gotten RS232 working fully, then go to the "examples" folder in your compiler's main directory and look for ex_sisr.c. Make a copy of it and paste that in your project directory. Change the FUSE, chip, and #use rs232() calls along with any other initialization code (please leave out LCD, CAN, etc. until you have rs232 working). The example should work as long as your setup is correct and your hardware is correct. |
|
|
hoangkhuong
Joined: 16 Mar 2012 Posts: 31
|
|
Posted: Fri Apr 13, 2012 7:27 am |
|
|
Hi everyone, I tried RS232 for the whole day and still can not figure out why it went wrong. Here is my code, I just try to put the character 'A' (Hex value is 41), but as I use Terminal v1.9b to test it, it display not the character 'A' but something like this:
Or in Hex value:
Code: | 41 81 C1 C1 81 81 41 |
There is not pattern there, just different sequences every time.
Here is my code:
Code: | #include <30F4011.h>
#DEVICE ADC=10
#include <stdlib.h>
//#include <can-dsPIC30.h>
//#include <can-dsPIC30.c>
#FUSES FRC,NOWDT
#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=pin_f3,rcv=pin_f2,bits=8)
int8 count=0;
#INT_TIMER1
void timer1()
{
set_timer1(40535);
count = count + 1;
}
void main()
{
int8 c = 124;
setup_timer1(TMR_INTERNAL|TMR_DIV_BY_8);
set_timer1(40535);
enable_interrupts(INT_TIMER1);
enable_interrupts(INTR_GLOBAL);
while(true)
{
if (count >= 10)
{
count = 0;
putc('A');
//putc(c);
}
}
} |
I hope that someone could help me figure it out. Thanks |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Fri Apr 13, 2012 8:11 am |
|
|
The weird characters probably indicate either a hardware issue or that you aren't setting up your oscillator correctly. I don't know anything about the chip you are using, but it might be worth while to see if your oscillator settings are working.
Try this main and hook it up to an oscilloscope:
Code: |
//Change this to something free on your PIC that you can easily measure with an oscilloscope probe
#define DEBUG_PIN PIN_B0
void main(){
while(TRUE){
delay_ms(500);
output_toggle(DEBUG_PIN);
}
}
|
Measure the pulse widths and the period of the waveform that is produced.
EDIT: Also, describe how your PIC is connected to your PC for the RS232, including all components inbetween. |
|
|
hoangkhuong
Joined: 16 Mar 2012 Posts: 31
|
|
Posted: Fri Apr 13, 2012 8:43 pm |
|
|
Thanks for your quick response. I already made the communication works between 2 dsPic2. Maybe the failure is because of USB-COM adapter. I would like to ask you for help for another problem. I have bought a old RFID reader, the name of the chip is blurred so I don't know it. It has Rs232 for the output, I connected it with PC and it display the tag ID correctly: "ID:1234"
Now I need to transfer this ID to dsPic, but all it can display is a bunch of weird characters. Here is my code.
Code: |
#include <30F4011.h>
#DEVICE ADC=10
#include <stdlib.h>
#include <string.h>
//#include <can-dsPIC30.h>
//#include <can-dsPIC30.c>
#FUSES FRC,NOWDT
#use delay(clock=4000000)
#use rs232(baud=4800,parity=N,xmit=pin_f3,rcv=pin_f2,bits=8,errors)
#include "LCD_4bit.h"
int8 count=0;
void main()
{
char c;
int8 i;
lcd_init();
delay_ms(20);
lcd_gotoxy(1,1);
while(true)
{
c = getc();
printf(lcd_putc,"%c",c);
}
} |
I did made a communication between two dsPic using this code and it can transfer a whole string like "ABCD".
The connection is like this:
1/ The GND output of the two are connected together.
2/ The output pin of RFID reader which can be used to connect with pin 2 of the COM now is connected to pin 3 of the female DB9 COM on dsPic board. That's all.
Hope you can help me with this. Thanks very much |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Sat Apr 14, 2012 6:35 am |
|
|
I believe your problem is related to my previous post. |
|
|
|
|
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
|