View previous topic :: View next topic |
Author |
Message |
umutso
Joined: 23 Jan 2013 Posts: 39 Location: turkey
|
16F877A restart Problem |
Posted: Fri Jun 21, 2013 9:11 am |
|
|
Hello All,
There is a digital scale which is sending the weight information through directly its RS232 port. I test it with a PC and hyperterminal that I can receive the data clean and continuous from the digital scale properly.
Also I prepared a circuit (on the bread board), which I have a 16F877A and 2 x MAX232. On 16F877A I'm using UART with one of the MAX232 to collect serial info from a barcode reader and then the digital weight device. With the second MAX232 I use it with the second UART (I define B0 and B1 pins) and write to the hyper terminal on the PC.
So it is simple, barcode reader reads the barcode, PIC gets the data and writes it to the PC, after the data arrives it waits for the data from the digital scale and writes it to the PC again.
When I use another PC with hyperterminal as the digital weight device, I can send the data to the PIC (Copy a sample data from the Weight device and paste it to the hyperterminal) and write it to the other PC hyperterminal properly. Everything is good.
When I disconnect the PC (that I use instead of the weight device) and connect the weight device (which it supposed to be) the the PIC starts, but never receives the data and whenever the weight device sends the data PIC restarts.
Weight device and PIC power supplies are different but ground are connected.
All communication with all devices I use, barcode reader, PC, weight device and PIC of course are set to 1200 Baud. Do you have any idea how can I fix this? So if I use a PC instead of weight system all works fine no problem until I replace the PC with the weight system. BTW when I connect the weight system to the same PC I can receive the data on hyperterminal. Weirdly it works as well separately.
Really need help...
Thanks, _________________ Umut Sonkurt |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Jun 21, 2013 9:42 am |
|
|
You've got me confused with what works and what does not.
Can you explain in diagram form, so that we can see a picture?
Show us where you've got hardware / software RS232 etc.
Mike |
|
|
umutso
Joined: 23 Jan 2013 Posts: 39 Location: turkey
|
|
Posted: Fri Jun 21, 2013 9:47 am |
|
|
Mike i will send a diagram in couple of hours.
Basically there are 2 serial devices sends data to pic in order. One is serial barcode reader other is digital weight scale. İ collect these two serial data with a max232 to my pic using both max232 ports. And then i write these data to a pc over the second uart (b0 and b1) with a second max232 to a pc hypertrm.
When i use a pc to send same data to the pic instead of weight scale all works. When i connect weight scale everything stops. _________________ Umut Sonkurt |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sun Jun 23, 2013 1:08 am |
|
|
You need to be looking at data.
Does the weight scale actually use 'RS232'. Possibly not. You may find some critical phrase like 'TTL asynch serial'. This is actually what the PIC uses, which the MAX 232 turns 'into' RS232. If so, you don't want the MAX232 to talk to the scale.
Data, data, data. You need to read the data sheet for the scale.
One critical thing you will find here is that the 'professional' programmers always _start_ with data. Reading or even translating data sheets _before_ the first connection is made.
Then you seem to be talking about using three serial connections. If so, do you understand the _limitations_ of software UART's versus hardware. Do a search on the forum about this. If these devices are sending data when 'they want', rather than 'when asked', then what you want may well be relatively hard to do, without using either an interrupt driven software UART, or another hardware UART. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Jun 23, 2013 2:51 am |
|
|
umutso wrote: | Mike i will send a diagram in couple of hours.
Basically there are 2 serial devices sends data to pic in order. One is serial barcode reader other is digital weight scale. İ collect these two serial data with a max232 to my pic using both max232 ports. And then i write these data to a pc over the second uart (b0 and b1) with a second max232 to a pc hypertrm.
When i use a pc to send same data to the pic instead of weight scale all works. When i connect weight scale everything stops. |
I was not expecting a couple of hours to become two days.
Something in ASCII art will suffice.
We can take in a picture at a glance.
Can't do that with several paragraphs of text. |
|
|
umutso
Joined: 23 Jan 2013 Posts: 39 Location: turkey
|
|
Posted: Mon Jun 24, 2013 1:21 am |
|
|
Hello All,
Sorry for the latency. Here is the schematic of the circuit board. and here comes how it works:
- I have 2 RS232 pins defined as C0 & C1 (main Communication) and B0 & B1 as debug to the screen
Definitions ;
Code: | #include <16f877a.h>
#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
#use delay (clock=4000000)
#use rs232(baud=1200, xmit=PIN_C6, rcv=PIN_C7, parity=N, stop=1, STREAM=con1)
#use rs232(baud=1200, xmit=PIN_B0, rcv=PIN_B1, parity=N, stop=1, STREAM=con2)
#include <string.h>
#define BUFFER_SIZE 14
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
|
- When starts MCU sends selection to the HCT4052 Mux/demux S0=0 & S1=0 (Pins B2 & B4 goes to low)
- Means ports 12&1 are selected and connected to MCU C0 & C1(UART) and waits for a single character with getc() from the SCALE. ///PROBLEM : When I connect a PC Hyperterminal and paste the same data supposed from Scale , everything Works like charm. When I connect the SCALE Serial port MCU stucks, restarts all time and nothing Works.
Code: | #int_rda
void serial_isr() {
int t;
buffer[next_in]=fgetc(con1);
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
#define bkbhit (next_in!=next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
void main()
{
delay_ms(2000);
enable_interrupts(int_rda);
enable_interrupts(global);
delay_ms(1000);
while(true)
{
output_low(PIN_B4);
output_low(PIN_B2); // pin 1-12
fprintf(con2,"<");
fgetc(con1);
delay_ms(1000); |
- When a character received then MCU delays 1000ms and then buffers what received with the first single character and writes to the hyperterminal via pins B0&B1 (my debug output)
Code: | while(bkbhit)
fputc(bgetc(),con2);
delay_ms(1000);
fprintf(con2,";");
|
- then MCU selects the second port and switches to the barcode reader on C0&C1. and Works again the same as the previous one. and after writing to the screen again switches to start:
Code: | delay_ms(400);
output_high(PIN_B4);
output_high(PIN_B2);
fgetc(con1);
delay_ms(900);
while(bkbhit)
fputc(bgetc(),con2);
delay_ms(1000);
fprintf(con2,">");
delay_ms(1500);
}
} |
PS : All Grounds are commonly connected.
Schematic is as follows:
http://s13.postimg.org/e3p9f4tsn/Scale.jpg _________________ Umut Sonkurt |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Mon Jun 24, 2013 2:05 am |
|
|
Tell us what the 'scale' is. Part number, location of data sheet etc.. We have no hope at all of helping without some knowledge....
There are some comments though. You are using a 74HCT4052, to multiplex the serial, but have done nothing to ensure that the devices receive 'idle' signals, when they are deselected. |
|
|
umutso
Joined: 23 Jan 2013 Posts: 39 Location: turkey
|
|
Posted: Mon Jun 24, 2013 2:16 am |
|
|
the scale is a weight scale which has a RS232 output and when you take on the scale and as soon as it weights a person it sends a specific data including weight like:
07$17779$03
when I connect this scale to a pc hyperterminal I can receive this data properly. Only when I connect this scale to my circuit RS232, then MCU stops. _________________ Umut Sonkurt |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon Jun 24, 2013 4:58 am |
|
|
Hi,
I doubt the 'mcu stops', how do you know that? More likely is that your UART becomes hung due to an over-run which is not being cleared. Add 'Errors' to your #use rs232 statement to do this automatically. Read the manual, and search the forum for more info if this advise is not clear.....
John |
|
|
umutso
Joined: 23 Jan 2013 Posts: 39 Location: turkey
|
|
Posted: Mon Jun 24, 2013 5:05 am |
|
|
Since I have sort of Debug output on pins B0 & B1, When MCU starts I put specific characters on the screen. So continuously specific start from the beginning character appears so I know it restarts. _________________ Umut Sonkurt |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Jun 24, 2013 2:36 pm |
|
|
I'm still confused as what works and what doesn't.
Your being singularly vague with statements like Quote: | Since I have sort of Debug output on pins B0 & B1 | We can't know what's going on.
Like ezflyr hints, it's not usual for MCUs to just stop.
Add code to wobble pins to indicate which sections of code are being called.
Failing that, try using MPLAB in debug mode to tell you where the code is stuck.
Mike |
|
|
Markdem
Joined: 24 Jun 2005 Posts: 206
|
|
Posted: Tue Jun 25, 2013 6:06 am |
|
|
Hmm, might be on the wrong track here but can I ask what all the delays are for?
Code: |
fgetc(con1);
delay_ms(900);
while(bkbhit)
fputc(bgetc(),con2);
delay_ms(1000);
fprintf(con2,">");
delay_ms(1500); |
Doing things like that just don't make sense.
If I was you, i would try to do one thing at a time. Connect the scale then try to collect the data, turning LEDs on to show that it is working.
Also, why not use a PIC with 2 UARTS? would make this so much simpler.
What is the model number of the scale you are using? it will help us to look at the datasheet.
Mark |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Tue Jun 25, 2013 8:20 am |
|
|
Just a guess here, but have you verified your supply voltage for the MCU is staying stable ? I have seen things before where connections to the external world were causing strange things on the supply if there were missing grounds etc. Make sure the supply is staying stable. If the ground for the scale and the ground for your circuit are not the same and you connect them together all sorts of odd things can happen - if the scale is running off a wall-wart, see if it has the option to run on batteries instead.
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
Andrew2
Joined: 09 Apr 2014 Posts: 1
|
|
Posted: Thu Apr 10, 2014 12:39 am |
|
|
I think some time your weighting instruments have not compatibility with your pc hardware.But it don't set off your pc. I think may be there are some power issues with that pc so that's why it don't supply proper power to all these devices and go off. |
|
|
|