PDA

View Full Version : HELP! Serial input between Pic and PC (urgent)



cmyew
- 4th December 2007, 14:28
Hello,

I was wrote a simple program to test on the hyperterminal but it cannot function. I am using max232 to communicate the pic (16F877A)and pc. So, can somebody pls help me to check about it? I was try to figure out where is the problem for a few day already.

**remark: I'm not using the USART function is due to the pin was reserved for others function.

Source Code:
'################################################# #########
Define OSC 10
include "Modedefs.bas"
ADCON1 = 6

TRISA = %111000
TRISB = 0

B0 var byte

PORTB = 0

Main:

serin PORTA.3,T9600,B0
pause 1000

if (B0==1) then PORTB = %00010001
if (B0==2) then PORTB = %00100010
if (B0==3) then PORTB = %00110011
if (B0==4) THEN PORTB = %01000100
If (B0==5) THEN PORTB = %01010101
if (B0==6) then PORTB = %01100110
if (B0==7) then PORTB = %01110111
if (B0==8) then PORTB = %10001000
if (B0==9) THEN PORTB = %10011001
if (B0=="A") THEN PORTB = %10101010
if (B0=="B") then PORTB = %10111011
if (B0=="C") then PORTB = %11001100
if (B0=="D") then PORTB = %11011101
if (B0=="E") THEN PORTB = %11101110
if (B0=="F") THEN PORTB = %11111111
PAUSE 1000


GOTO MAIN

'################################################# #######
Thanks in advance

Ioannis
- 4th December 2007, 15:59
For starters, you are using already 2 seconds delay inthe loop. I am sure you are going to loose many data between this time spent on waiting.

Please provide more info on the program at PC side. What is sending and at what rate?

Also == has to be replaced by = and the () are not needed here.

One recommendation would be to have the PC send a start character like : and PIC wait for this character and then grab the next one.

like this:
serin PORTA.3,T9600,[":"],B0

Also ged rid off the pause commands.

Ioannis

cmyew
- 5th December 2007, 02:27
Hello,

The setting for the hyperterminal are:
#################
bit per second = 9600
Data bits = 8
Parity = None
Stop bits = 0
Flow Control = 0
#################

I need to send the character form pc to pic, say i type the 1 into the hyperterminal then the led on the pic side will on %00010001

Ioannis
- 5th December 2007, 07:34
The code as you wrote it will respond only to characters "A" to "F" and only if you have caps lock!

1, 2, 3 etc are coded as binary in your program, not ASCII. You have to use even for them the " ", e.g. "1", "2" etc.

Also as I have told you on the previous post, at least the first delay should go away.

A very good example can be found here:

http://www.picbasic.co.uk/forum/showthread.php?t=573&highlight=parsing

and a bit confusing here:

http://www.picbasic.co.uk/forum/showthread.php?t=717&highlight=Serial

Ioannis

cmyew
- 6th December 2007, 03:27
Hello,

1. Mr Ioannis, thanks for your reply. I was modified my program but still have no result.

Source Code:
'################################################# #########
Define OSC 10
include "Modedefs.bas"
ADCON1 = 6

TRISA = %111000
TRISB = 0

B0 var byte

PORTB = 0

Main:

serin PORTA.3,T9600,B0

if B0="1" then PORTB = %00010001
if B0="2" then PORTB = %00100010
if B0="3" then PORTB = %00110011
if B0="4" THEN PORTB = %01000100
If B0="5" THEN PORTB = %01010101
if B0="6" then PORTB = %01100110
if B0="7" then PORTB = %01110111
if B0="8" then PORTB = %10001000
if B0="9" THEN PORTB = %10011001
if B0="A" THEN PORTB = %10101010
if B0="B" then PORTB = %10111011
if B0="C" then PORTB = %11001100
if B0="D" then PORTB = %11011101
if B0="E" THEN PORTB = %11101110
if B0="F" THEN PORTB = %11111111

GOTO MAIN
############################################

2. What is the difference between T9600 and N9600? If I'm using a max232 IC then which type should i use?

mister_e
- 6th December 2007, 04:07
With SERIN and a MAX232 you need to use T9600.

Try Sending Data fom your PIC to your PC to see what happen.

I feel some hardware and configuration fuse problem here...

You may also try with DEBUG/DEBUGIN...

sougata
- 6th December 2007, 05:11
Hi,

Since you are using a PIC with hardware serial peripheral I suggest you use the hardware port if (your port pins are not used up). Even if your global interrupt is not set to handle interrupts (INTCON.7) the hardware flags inside the PIC always responds to an interrupt event. So you can just

1. Poll the bit PIR1.5 (RCIF) for a 1 and if yes then
2. Read the RCREG register to your own variable MySerialData = RCREG
(Note the interrupt flag RCIF gets cleared once you read the data)
3. Do your stuff
4. Go back to the polling.

Important to not that while you are processing and data can be grabbed by the module but if it is flooded again with data and you have not read the RCREG register then the overrun bit would be set which can be cleared by resetting and setting the CREN (continuos receive enable) bit.

With Mister_e's PICMULTICALC utility configuring the registers for the hardware USART peripheral should not be any problem.

Ioannis
- 6th December 2007, 08:50
May be the ADCON1=6 must be =7 ?

I am not familiar with the A version of the 877. Steve?

Ioannis

Melanie
- 6th December 2007, 09:23
For five years I've been shouting DATASHEET at people. In the end it all falls on deaf ears...

How the hell can you program for ANY device if you don't know and don't understand what's in the box? There are MAJOR differences between the A and non-A of this particular PIC. Oh and look... they just happen to affect PortA.3 as well...

If you can't be bothered to do that, take a different class in school, get another hobby, change your job etc...

T.Jackson
- 6th December 2007, 11:18
For five years I've been shouting DATASHEET at people. In the end it all falls on deaf ears...

How the hell can you program for ANY device if you don't know and don't understand what's in the box? There are MAJOR differences between the A and non-A of this particular PIC. Oh and look... they just happen to affect PortA.3 as well...

If you can't be bothered to do that, take a different class in school, get another hobby, change your job etc...

That's considerably way too harsh! There's four ways we learn.

1. Kinetic (doing / practical hands-on)
2. Visually (charts, diagrams, mental problem solving)
3. Verbally (Usually a person speaking)
4. Reading & writing

You're one very special creature if you're good at all four! Most mortals (including myself) - generally have strength in only about two of the aforementioned list. If you can solve all your problems by skimming over data sheets, then you have a nice strength with reading & writing and possibly also with visual. Most highly introverted people (like myself) tend to have a strength with visual & kinetic. So, to write someone off just because they haven't, or rather can't!!! - follow a data sheet terribly well is absurd!

cmyew
- 6th December 2007, 11:38
Hi Ioannis,
I will try to check about it..

Hi mister e,
as u mentioned on the previous post about the hardware and configuration fuse problem, can u tell me more about that? I really want to learn about it...

Thanks in advance

Hi Melanie,
First of all, thanks for your reply.

I not sure what u answer me is related to the question that i asked about. But i can confirm that not every person come here and ask question without reading "DATASHEET". Maybe they are not understanding on some of the topic (include me) and try to looking for someone to help.

T.Jackson
- 6th December 2007, 11:51
Not all 877s are clock able @ 10MHz (some only go up to 4) - something to keep in mind.

T.Jackson
- 6th December 2007, 12:38
But i can confirm that not every person come here and ask question without reading "DATASHEET". Maybe they are not understanding on some of the topic (include me) and try to looking for someone to help.

The data sheet for the novice is generally out of the question. Much like a modern-day engine for a car that is entirely pre-assembled prior to fitting (they don't care about how awkward it is for the novice to change the starter motor) - data sheets are written primarily for an audience of individuals that have experience! If a data sheet was more like an "instruction manual" - I'd reserve little hesitation in telling you to go and read it.

Ioannis
- 6th December 2007, 13:11
Not all 877s are clock able @ 10MHz (some only go up to 4) - something to keep in mind.

I can confirm that every chip marked -4 was running perfectly doyble that, at 8 that is. Did not test more though...


For five years I've been shouting DATASHEET at people. In the end it all falls on deaf ears...


Melanie, I can understand your response, but this forum is not restricted to pro's that know what to search for. Unless there is a restriction about the people that ask questions here, I think we have to tolerate also the absolute newbies. Even the lazy ones...

Many times I did not reply just because the answer was so obvious, almost shouting from within the pages of the DS. Anyways...

Ioannis

T.Jackson
- 6th December 2007, 13:18
Melanie, I can understand your response, but this forum is not restricted to pro's that know what to search for. Unless there is a restriction about the people that ask questions here, I think we have to tolerate also the absolute newbies. Even the lazy ones...

Many times I did not reply just because the answer was so obvious, almost shouting from within the pages of the DS. Anyways...

Ioannis

I think Melanie has had one of those "days" to be honest. Surely she must realize what sort of people she's dealing with here. Half have INTJ personalities and live in their "own world".

mister_e
- 6th December 2007, 14:41
Hi mister e,
as u mentioned on the previous post about the hardware and configuration fuse problem, can u tell me more about that? I really want to learn about it...

When possible, i suggest all forum member to post their schematic to see if it's done correctly. Psu filtering (0.1 uF + 10uF tantalum as close as possible of all Vdd/Vss jucntions), MLCR tied to Vcc directly or via resistor. etc etc

Configuration fuses. Those need to be set before you program your PIC. Some feature might need to be disable, and some might need to be set properly depending of your OSC speed. Hence if there's any LVP, usually we set it to OFF, if the OSC/Crystal speed is >4 Mhz, OSC mode have to be set to HS.

Look at the FAQ, there's whole thread about how to set the configuration fuses into your code. At least read post 1-5

Presetting Configuration Fuses (PIC Defines) into your Program
http://www.picbasic.co.uk/forum/showthread.php?t=543

HTH

Melanie
- 6th December 2007, 17:50
I'm sorry I have to disagree...

You can look at an internal combustion engine and figure which end turns and which end doesn't. You can figure where to bolt it down and couple it up. You can figure where to plumb the volts (if any), and fuel (and what kind) and where to plumb the water. But if you want to adjust timing, set spacing and clearances or other critical functions - guess what? You READ the manufacturers DATASHEET.

But a PIC is just a plain black box with a heap of legs sticking out of it...

Until you consult the Datasheet, you don't even know which legs need volts on them or even how much...

Until you consult the Datasheet, you don't know where to plug-in your Xtal, or Resonator, or what kind will work or how to enable the internal oscillator...

Until you consult your Datasheet, you don't know where the MCLR pin is, or what to do with it or how to disable it...

Until you consult the Datasheet, you don't know what pins have bi-directional I/O and what pins haven't, what pins have other functions multiplexed with them, and how to enable or disable those functions, or how to use them.

Until you consult the Datasheet, you don't realise that the 877A (keeping the thread in-topic) has COMPARATORS and a VOLTAGE-REFERENCE MODULE stuffed inside whilst the plain 877 hasn't - and how to recognise if the PIC on your desk can actually perform to do the task you're giving it, or if you should have bought a different variant.

So tell me - how are you going to discover those features UNLESS you READ the Datasheet? Crystal Ball? Telepathy? Oh... I forgot, of course - go the Datasheet substitute route and ask the forum!

C'mon, are you seriously suggesting that all the amateurs and hobbyists and students are excused from Datasheets because the experts and professionals - and those that can read - can provide all the answers?

Ioannis
- 6th December 2007, 19:27
...
C'mon, are you seriously suggesting that all the amateurs and hobbyists and students are excused from Datasheets because the experts and professionals - and those that can read - can provide all the answers?

I meant that may be many read or "read", but few understand. Yes I know, it is very annoying though. New people arrive daily and the story repeats.

And usually you can see if one that is asking has read the DS before.

Ioannis

T.Jackson
- 7th December 2007, 05:08
I'm sorry I have to disagree...

You can look at an internal combustion engine and figure which end turns and which end doesn't. You can figure where to bolt it down and couple it up. You can figure where to plumb the volts (if any), and fuel (and what kind) and where to plumb the water. But if you want to adjust timing, set spacing and clearances or other critical functions - guess what? You READ the manufacturers DATASHEET.


Learning how to read a data sheet (or similar / vehicle service manual) - is like learning a new language in my humble opinion. Applying what you've just read is a different story again. Why spend time pulling your hair out when there's plenty of people around willing to lend a hand! I personally feel that you take too much for granted, you've been a design engineer for what; 20 years? To appreciate what I'm saying, you need to fully recap yourself to a time & place when you just started out. But on top of that, you need to consider that not everyone is as smart or technical minded as yourself. You really need to learn how to bring yourself down to another person's level is probably what I'm trying to say.

Ioannis
- 7th December 2007, 07:00
... you've been a design engineer for what; 20 years?
...


Exactly that. 20 years ago I did not had any internet to help out. So what I did? Read and practice a lot.


... You really need to learn how to bring yourself down to another person's level is probably what I'm trying to say.

I think not she or me or anyone else, "really" need to do that. Others have to work and get up to a proper level. For example I have seen your work on VB6, how good you are and I am trying hard to understand that lanquage with the programs you have posted. I could say that I am areally newbie but will not annoy you to teach me how to write a simple command line...

Of course we will help, but they have their share too.

Ioannis

T.Jackson
- 7th December 2007, 07:48
I've had heaps of assistance learning VB6. I don't take any of it for granted and the people that helped me out online didn't either. The programs that I wrote in VB6 this time two years ago were absolute rubbish until I discovered Planet Source Code on the world wide web. If you need any help I will point you in the right direction. No problems.

cmyew
- 7th December 2007, 08:11
...... I am areally newbie but will not annoy you to teach me how to write a simple command line...


Ioannis

Hi Ioannis,

Do u meant i was annoyed you to teach me how to write a simple command line? If yes, then i think i should apologize to you. Sorry for disturbing...

Melanie
- 7th December 2007, 08:14
you've been a design engineer for what; 20 years?

Actually not quite six... hmmm... I hope I still look like my picture in 14 years time...

Ioannis
- 7th December 2007, 08:31
Hi Ioannis,

Do u meant i was annoyed you to teach me how to write a simple command line? If yes, then i think i should apologize to you. Sorry for disturbing...

I don't understand the ironic attitude. Did not the answer #2, #4, #8 really helped you solve the problem at the utmost polite way? Ohh, people...


Actually not quite six... hmmm... I hope I still look like my picture in 14 years time...

I think you will look great even after 30 years! Engineers are doing proper service's at the set intervals!

Ioannis

cmyew
- 7th December 2007, 08:48
Hi Mr.Ioannis,

Actually i really very thankful and appreciate the solution that u given to me. I also not meant that u dint help me but i just scared that i was disturbing you. Now i still try to troubleshooting for the code and my hardware. Hopefully it can be working one day.

Thank you very much

mackrackit
- 7th December 2007, 14:30
Project instructions:
1. Read the Data Sheet.
2. Read the PIC Basic manual.
3. Read this forum.
4. Use FIRE extinguisher.

5. Post question stating:
A. Every thing you tried
B. Post Your CODE.
C. Post schematic if applicable.

Back to the topic....

Now i still try to troubleshooting for the code and my hardware. Hopefully it can be working one day.

What problems are you still having?

cmyew
- 7th December 2007, 16:56
Hi Dave,

1. The attachment is my circuit diagram, pls comment to me if there are something wrong.
Thanks

2. Source Code:
'################################################# #########
Define OSC 10
include "Modedefs.bas"
ADCON1 = 7

TRISA = %010000 'RA4 as serial input, RA3 as serial output
TRISB = 0 'PortB used to ON the LED

CHAR var byte

PORTB = 0

Main:

SERIN PORTA.3,T9600,CHAR

PORTB = CHAR

SEROUT PORTA.3,T9600,[CHAR,10] 'I used hyperterminal to received the data
'that sent to pic

GOTO MAIN
############################################

3. I was try to change the value of OSC but still get the same result. For Example: when i sent "1" from hyperterminal to pic but the result that returned to hyperterminal is " ' " then the result on LED is "11111010". I was checked the ASCII table, the result is not same with the data i sent.

ruijc
- 7th December 2007, 16:57
Hi guys,

I'm a new guy where pics are concern, but i'm more experienced with my other hobbys.

Since i dont have anyone that i can learn with i have to teach my self and learn with my mistakes and experiments.

For each hobby i have i also participate in the related forums.

I try to share my experience and try to give my knowledge with the new guys as i remember myself some time before asking for it or came across a dead end as the new guys do now.

Sure for me can be obvious some issue but it can be a hard step for a new guy to go through.

And i know how fustrating can be when hitting a dead end and not having a little help to get arround it. I know some people that leave their hobbys for that.

This is the point of the foruns. For people to share information and experiences. To help the other guy regardless of the question.

Me for example i use datasheet but because i'm a new guy in pic world there are many thing i dont undestand in them !

I've been helped many times here and i believe that if it wasnt for the guys here i wouldnt be able to continue with my learning curve and getting stuck with a flat line !

Sorry for the long post...just wanted to share my humble opinion ;)

.

mackrackit
- 7th December 2007, 17:25
The first thing that I see is you have SERIN and SEROUT on the same pin.


SERIN PORTA.3,

SEROUT PORTA.3,

I have never tried A4 for serial
http://www.picbasic.co.uk/forum/showthread.php?t=562
You may have to use another pin.

Right now do not worry about TRISA. The SERIN/SEROUT takes care of that.

sinoteq
- 7th December 2007, 18:40
If you just want the short answer.....

I think you may have made a misstake with the pins
PORTA.3 is Data PIC>>>>PC (serout in pbp)
PORTA.4 is Data PC>>>>PIC (serin in pbp)
using PORTA.4 as an input is not a problem at all, if you want to use it as output you need a pull-up resistor to make it work.
Change is your sw and it might work better.

If you want a longer answer......
Yes datasheet can be really strange to read and understand, this we all have to accept. Training and experience will make it easier and even simple. This is fact.

Now to a different thing, making electronics with PIC inside is not that different from sport, training makes a difference and not all people have the same skills. It is easy to wish for a cool electronic gadget but much harder to make it. I am not writing this to put someone down, no I am writing this to explain how to get there and that is One Small Step at the Time. Don't expect to write a missileguiding SW the first day but eventually you will.

So I want to make a thing that can turn on and off LED from a PC, how would I do that?

A, Make a plan, with a good plan you will save much time later. Figure out what you want the circuit to do and what hardware you will need. You can always make the plan better later but you honestly need a plan before you start.

B, Test the hardware. Here is the best thing the undervalued Flash a LED program most usefull. Time spent verifying the hardware you will get back 1000 times later because when you see strange things you can rule out HW problems. If you can't flash a led you can not expect anything else to work ever....

C, If you want to connect to a PC do not start with a SERIN problem. Start with a SEROUT so you can see that you can send data to the PC. Even if you will not use it later?? YES!! Because it is easier to recieve data with a PC compared with a PIC, if you can send data to the PC it is more likely that the other way is working also. I almost always include a line early in the program to send SEROUT......... " PIC STARTED " so when I see this is hyperterminal I understand that the PIC is happy. It is also a great way of troubleshooting later because you can always send serial data to the PC to display arrays and other variables. When you can see the data on the PC you have proof that Baud and Mode are correct and you also know you are using the correct serial port on the PC.

D, Make sure you know where the PIC is in your progam before you do a Serin. Like other people said before use a start char to start the Serin function for example ":" or "X" or any other you want to use, just make sure you use one.... so do a serout first in your code
Serout............"PIC waiting for data" followed by a SERIN ........... that has a start char. Now you are sure that the PIC is waiting for char from the PC, now use the PC to send 2 char (start char + data char). After the Serin line I would put a new Serout line that has Serout.......... "Data recieved",#Data_Char Now I can see that the PIC moved forward in the program and what data was recieved. Please remember that ASCII and dec numbers can be confusing so double check or force the data to be in DEC fromat using the # function. IF you are not using a start char you are not sure that the data char is correct, serin will take any data and you will be in the land of trouble.

This is one small step at the time, the key to R&D and the best way to make progress. Later when you have everything working you can remove lines in the code that you are not using and the code becomes smaller and cleaner.

The good thing with PIC is that they are small computers, they do exactly what you tell them, the bad thing with them is they are small computers, they do exactly what you tell them. :) You can not expect the PIC to be forgiving in anyway, there are no help files incuded just the data sheet written by nerds for nerds.

To make the a circuit that turns on a LED is a very good project but it is not a simple project, it will take time, skill and patience but eventually you will get it to work.

Define OSC 10
include "Modedefs.bas"
ADCON1 = 7

TRISA = %010000 'RA4 as serial input, RA3 as serial output
TRISB = 0 'PortB used to ON the LED

CHAR var byte
PORTA.3=1
PORTB = 0
SEROUT PORTA.3,T9600,[" PIC started",10,13] 'I used hyperterminal to received the data
GOTO MAIN


PC_IN:
SEROUT PORTA.3,T9600,["Use : as start char",10,13] ' write :A in hyperteminal...
SERIN PORTA.4,T9600,[":"],CHAR
SEROUT PORTA.3,T9600,["Data from PC ",CHAR,10,13] '
RETURN

Main:
GOSUB PC_IN

GOTO MAIN




GOTO MAIN

Ioannis
- 7th December 2007, 22:37
And after all that, I still cannot understand why you don't use the USART of the PIC? Are you going to use those pin to connect something else?

Ioannis

cmyew
- 8th December 2007, 02:46
Hi sinoteq,

Thanks for your advice, I will try this.

Hi Ioannis,

Yes, I was using the PORTC to do another function.

Ioannis
- 8th December 2007, 09:31
If you can reassign the port pins, I strongly suggest to do so. The gain is enormous in processing speed/power.

Ioannis

cmyew
- 10th December 2007, 12:14
Hi all,

Finally my program can work already. So, thank you to those people that helping me to solve this problem exspecially Mr. Ioannis, sinoteq and Mister e. Thank you very much

Ioannis
- 10th December 2007, 13:05
It would be nice to post the solutions that made it work.

Ioannis