View previous topic :: View next topic |
Author |
Message |
john822179
Joined: 22 Jun 2011 Posts: 7 Location: Tandragee, Northern Ireland
|
Problem receiving RS232 data |
Posted: Sat Apr 19, 2014 10:45 pm |
|
|
Hello everyone,
I wonder if someone can tell me what I am doing wrong here.
I need to send and receive data via RS232.
I am using:
PIC16F1829
MPLab 8.92
CCS PCM Compiler 5.025
MAX232 level shifter chip
I have a laptop connected to my system and can clearly see data being received on the RX pin of the PIC, using a Digital Storage' Scope.
However, the PIC studiously ignores the communications.
The code below only shows what I am using to Receive data.
I can build strings within the PIC and transmit them with no problems, so it appears that my problem is in Receiving data.
I have tried a second PIC, just in case I have a problem with the chip, but that made no difference.
Code: |
// Read RS232 from Laptop and Transmit back to Laptop
#include "16f1829.h"
#include <stdio.h>
#include <stdlib.h>
#use DELAY(internal=4mhz)
#use RS232 (STREAM = Modem, baud = 2400, xmit = PIN_B7, rcv = PIN_B5, RECEIVE_BUFFER=20,TIMEOUT = 100,ERRORS)
char c;
void main()
{
here:;
{
if( kbhit(Modem))
{
c = fgetc(Modem);
fputc( c, Modem );
}
}
goto here;
}
|
This is driving me NUTS!!!!!!!
Any help would be more welcome than a Chocolate Easter Egg.
Many thanks for reading this
John822179 |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Apr 20, 2014 1:46 am |
|
|
Does your PIC run an LED flasher at the correct speed?
Is the internal clock accurate enough?
(Maybe just borderline with rounding off error.)
Mike |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Apr 20, 2014 3:45 am |
|
|
First of all I'm missing the #fuses definitions. Now you are depending on whatever the compiler sets as default. You never know what these settings will be and might even change from one compiler version to the next.
Figuring out the correct clock fuse settings for the newer chips has become more difficult with the extra features being available but leaving them to chance is a disaster waiting to happen.
Study the datasheet, use the CCS project wizard and search this forum for more info.
The next step is to test whether your PIC is running and running at the correct speed. A very simple test but it rules out so many other problems later on in development that I consider it a 'must do'.
As Mike already said, this is is a very simple program to flash a LED every second. Time this for 10 seconds. Does the number of flashes match?
A note on programming style: in C it is recommended to never use the 'goto' command. It leads to difficult to read programs. There are other language constructs like the 'while' and 'for-next' loops that show at a higher design level what your intention of the code part is. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sun Apr 20, 2014 6:53 am |
|
|
and there is a very real danger using goto.
There are occasions (few) inside perhaps some things like interrupt code, where a direct jump is the best thing. But for 99.99% of the time this is not the case. Problem is that with a jump, there is no guarantee that the stack is in balance. Result can easily be crashing code.
Treat goto in C like a live uninsulated electrical wire dangling in a room. A child may be tempted to touch it, but will soon learn (hopefully), that this is not a sensible thing to do. An expert may well touch it (wearing insulated gloves), but everyone else should give it a wide berth.... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 20, 2014 9:33 am |
|
|
I made it work. I installed CCS vs. 5.025. I used a 16F1829 in a
Microchip Low Pin Count board, running at +5v with an external power
supply attached to the board. I jumpered the board to a Sparkfun
RS232 Shifter SMD board (Tx-O pin on that board goes to Rx pin on
the PIC, and Rx-I goes to the Tx pin on the PIC).
https://www.sparkfun.com/products/449
I programmed it with a Pickit 3. I used a much more simple program
as shown below. I used TeraTerm on my Windows desktop to
send and receive the serial bytes. I made sure that Local Echo was
turned off in TeraTerm. I can type "asdfasdf" and it works perfectly.
Code: |
#include <16F1829.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
//===================================
void main()
{
int8 c;
while(1)
{
c = getc();
putc(c);
}
} |
Ttelmah wrote: |
Problem is that with a jump, there is no guarantee that the stack is in balance. Result can easily be crashing code. |
Just to make it clear, there is no stack balance threat in his code.
Even though it's bad programming practice, his use of goto produces
the same ASM code as using a while(1) statement. |
|
|
john822179
Joined: 22 Jun 2011 Posts: 7 Location: Tandragee, Northern Ireland
|
Problem Receiving RS232 data |
Posted: Fri Apr 25, 2014 12:34 am |
|
|
Hello all,
My apologies for the delay in responding to your replys. As always happens, just when I am ready to get started, I get distracted!
As PCM Programmer and Ckielstra said, the problem was missing #fuses.
The real problem was the missing INTRC_IO. Putting this in just waved the magic wand and everything just started!!!
Talk about a Magic Spell!
Regarding other matters, I am NOT going to get involved in the GO TO issue, except to say:
Programmers have been moaning about GO TO since God was a lad.
If it shouldn't be used, then REMOVE it from C. They've had long enough!
SIMPLES!!
Many thanks for all your help
John822179 |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Fri Apr 25, 2014 5:46 am |
|
|
hmm..missing fuse... yeah that'd kinda make things 'go funny'....
New PICs have MORE fuses than instructions these days.
As you've found out , it's best to specify EVERY fuse and NOT rely on the 'default' values. I got stung with the 46K22 so after I got it 'up and running' , I created a 'fuse' file and #include it in projects. One..I KNOW that set of fuses works, two..makes 'main() 'cleaner', less 'cluttered' to read, three.. NO chance of 'typos' making things 'go funny'.
As for the 'goto' debate..well, goto IS a valid opcode for the PIC(probably 99% of all computers) and when used correctly is never a problem.You're right about 'they' should have eliminated from C long ago IF it 'they' wanted too.I'm still upset Pluto isn't a planet anymore.
sigh
jay |
|
|
pingnu
Joined: 21 Feb 2008 Posts: 2 Location: UK
|
PIC fuses |
Posted: Fri Apr 25, 2014 6:47 am |
|
|
Hi
I have the same problem but with a PIC24F32KA304 Explorer 16 I can not receive but can transmit
How do I find all the relevent fuses do I just look in the IDE for all configuration bits.
I am using the xc16 compiler the C30 would not build missing header files for this chip.
Carl |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
Re: PIC fuses |
Posted: Fri Apr 25, 2014 6:54 am |
|
|
pingnu wrote: | Hi
I am using the xc16 compiler the C30 would not build missing header files for this chip.
Carl |
Errr, Carl, this forum is for the CCS C Compiler...... Your question really belongs on one of the Microchip forums!
John |
|
|
|