View previous topic :: View next topic |
Author |
Message |
karimpain
Joined: 15 Feb 2012 Posts: 39 Location: italia
|
Problem with pic and serial port! |
Posted: Mon Feb 20, 2012 3:35 pm |
|
|
Hello Everyone!
I want to receive characters from my serial port using matlab. I connect the tx of the serial port to my max232 and then to rx of my pic 16f877a.
Using Proteus I saw that if I send for example '1' after the max232 it becomes 'g'. But putting a not on the output of the max232 I obtain the '1'.
My code is :
Code: |
#include <16f877a.h>
#Fuses HS,NOPROTECT,NOWDT,NOLVP
#use delay(Clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
void main()
{
int u;
set_tris_b(0x00);
set_tris_d(0x00);
output_b(0x00);
output_d(0x04);
while(1)
{
if (kbhit())
{
u=getch();
if (u=='1')
output_b(0x10);
}
}
}
|
Do I have to put something else in my code ? Because it doesn't work. My Proteus simulation works good but I soldered everything without results. Even if my pic and max232 are working good.
Thanks for reading! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 20, 2012 3:52 pm |
|
|
Your program is too complicated. Try a more simple program that
sends back to the PC the same character that it receives:
Code: |
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=8M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//============================
void main()
{
char c;
while(1)
{
c = getc();
putc(c);
}
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Mon Feb 20, 2012 3:53 pm |
|
|
first. Get RID of Proteus ! It is a very poor 'simulator', full of bugs, errors and faulty DRCs !!
second: Try the CCS examples in the Examples folder before you try your own code. In the FAQ they have detailed both a circuit and code that WORKS! ALSO be sure to add 'errors' to the USE RS232().. if using the hardware UART !
third: Be sure to setup MATLAB correctly.Try using Hyperterminal ,RealTerm, or another 'terminal ' program and be sure the PIC code works fine and that the PC side is correct. A simple 'loopback' will prove all is good.
fourth: If using a USB<-> RS232 'adapter' is is very, very important to check it out using a loopback test.Not ALL adapters will work properly. |
|
|
karimpain
Joined: 15 Feb 2012 Posts: 39 Location: italia
|
|
Posted: Tue Feb 21, 2012 6:55 am |
|
|
Really, ook it 's not that great software simulation but do u have one better?!?!?
i downloaded the hyper terminal bt i can't send data my keyboard doesn't respond maybe cuz i ve win7!!!.. don't know!!!
i 'm going to use 'errors'..I'm going Crazy..cuz this is the first step of my project.
Thanks 4 responding |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Tue Feb 21, 2012 7:57 am |
|
|
Did you try PCMs program? If so, what happened?
Quote: | Your program is too complicated. Try a more simple program that
sends back to the PC the same character that it receives:
|
Code:
Code: | #include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=8M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//============================
void main()
{
char c;
while(1)
{
c = getc();
putc(c);
}
}
|
_________________ Google and Forum Search are some of your best tools!!!! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Tue Feb 21, 2012 8:07 am |
|
|
After 30 years, I found there isn't ANY simulator that will work correctly.
Use real hardware in the real world.
I use Realterm in XP Pro,check and see if it'll work under 7.
Simple test is to connect pin 2 to 3 of the comport and see if KBD data is echoed onto the Screen.That will confirm the PC terminal program is working.
Then run PCM's program. Just be sure you've set comport options right(baudrate, no flowcontrol,etc.) |
|
|
|