|
|
View previous topic :: View next topic |
Author |
Message |
Khansokhua
Joined: 06 Nov 2021 Posts: 92
|
rs232 LED BLINK pıc-to-pıc communication |
Posted: Fri Mar 10, 2023 10:49 am |
|
|
2xpıc16f877a
tx
Code: | #include <16F877A.h>
#fuses PUT, HS, NOWDT, NOLVP, NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N, errors)
void main()
{
set_tris_d(0x00);
output_d(0x00);
output_high(pin_D1);
delay_ms(1000);
output_low(pin_D1);
delay_ms(1000);
output_high(pin_D1);
while(true)
{
putc('g');
delay_ms(1000);
putc('y');
delay_ms(1000);
putc('r');
delay_ms(1000);
}
} |
rx
Code: | #include <16f877a.h>
#fuses PUT, HS, NOWDT, NOLVP, NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N, errors)
void main()
{
setup_adc(adc_off);
set_tris_d(0x00);
output_d(0x00);
output_high(pin_D1);
delay_ms(1000);
output_low(pin_D1);
delay_ms(1000);
output_high(pin_D1);
char data;
while(true)
{
while(true)
{
data = getc();
switch(data){
case 'g':output_high(PIN_A1);
output_low(PIN_A2);
output_low(PIN_A3);
break;
case 'y':output_low(PIN_A1);
output_high(PIN_A2);
output_low(PIN_A3);
break;
case 'r':output_low(PIN_A1);
output_low(PIN_A2);
output_high(PIN_A3);
break;
default:output_low(PIN_A1);
output_low(PIN_A2);
output_low(PIN_A3);
break;
}
}
}
}
|
I would like to ask you, here leds blink but issue is it is not reliable. I track the tx, seems always high. I wonder what causes that led to not going to run properly? There is no exact frequency.
Another point is: when just receiver pic has power supply, leds blink fast as flash except A1 led.
What can I do for stable running? |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 480 Location: Montenegro
|
|
Posted: Fri Mar 10, 2023 11:58 am |
|
|
Why are you not using RDA interrupt on the RX side? It is so much easier and also failsafe. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19536
|
|
Posted: Fri Mar 10, 2023 12:28 pm |
|
|
\if the LED's are flashing when the serial is not connected, I'd be very
suspicious that you have something fundamentally wrong with the wiring,
and the chip is resetting.
1) have you got all the supply and ground pins connected on the chips?.
2) Have you got good HF decoupling capacitors close to the chip supply
pins?.
3) have all the LED connections got suitable current limiting resistors
to them?.
4) How is the MCLR wired?.
5) What is your supply?.
With the serial pulled low (so master not powered), the slave should
be continuously executing the default option, but this will only lead to
a flash, if doing this is causing a reset. |
|
|
Khansokhua
Joined: 06 Nov 2021 Posts: 92
|
|
Posted: Sat Mar 11, 2023 12:39 am |
|
|
Ttelmah wrote: | \if the LED's are flashing when the serial is not connected, I'd be very
suspicious that you have something fundamentally wrong with the wiring,
and the chip is resetting.
1) have you got all the supply and ground pins connected on the chips?.
2) Have you got good HF decoupling capacitors close to the chip supply
pins?.
3) have all the LED connections got suitable current limiting resistors
to them?.
4) How is the MCLR wired?.
5) What is your supply?.
With the serial pulled low (so master not powered), the slave should
be continuously executing the default option, but this will only lead to
a flash, if doing this is causing a reset. |
1) Yes
2) I have one 100nF cap connected both Vdd
3) No, I just connected directly
4)10k pull-up resistor
5) DC 5V |
|
|
Khansokhua
Joined: 06 Nov 2021 Posts: 92
|
|
Posted: Sat Mar 11, 2023 1:08 am |
|
|
PrinceNai wrote: | Why are you not using RDA interrupt on the RX side? It is so much easier and also failsafe. |
Do I just need to take this into the rda function?
Code: | switch(data){
case 'g':output_high(PIN_A1);
output_low(PIN_A2);
output_low(PIN_A3);
break;
case 'y':output_low(PIN_A1);
output_high(PIN_A2);
output_low(PIN_A3);
break;
case 'r':output_low(PIN_A1);
output_low(PIN_A2);
output_high(PIN_A3);
break;
default:output_low(PIN_A1);
output_low(PIN_A2);
output_low(PIN_A3);
break; |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Sat Mar 11, 2023 6:20 am |
|
|
!!! #3 Q&A....LEDS !!
You NEED a current limiting resistor between the LEDS and the PIC !! Something like 1k, 680r, 470r, 330r or 270r. The actual value depends on type of LED(red,yel,grn and current (say 5ma ). VDD-vLED/.005= r value, VDD is 5, vLED (red ) is 1.6 so roughly... 5-1.6=3.4, 3.4/.005 is 680r.
re: RDA
The serial interrupt service routine ( ISR) should only be used to capture theh incoming serial data and store in a 'buffer'. ISRs need to need small, short and quick so no PRINTing, 'math' or 'complicated' logic trees.You get the data, store it and set a 'flag',then exit. In main() you test that 'flag' and do 'something'.
Historically 'buffers' were twice as large as the length of incoming data and some binary size (8,16,32, 1024 bytes). 'Size' allows for already received data to be used and still receive more data, 'binary' allows FAST access to the buffer, say for sorting or parsing.
CCS supplies an generic example of an ISR called 'SISR.C', it's located in the examples folder.
also to make code reading easier, you can use #DEFINE myname pinnumber.
#define grnled pin_a3;
so output_high(grnled); helps is 2 ways
1st.it SHOWS you that something should happen to the GREEN LED.
2nd,you only need to change one line of code(the define) to change the pin usage
3rd, it really,really helps others as to what an I/O pin is for !
I'm sure others can explain better... lucky me, I have 10 inches(25 sillymeters) of snow to 'relocate'....... |
|
|
Khansokhua
Joined: 06 Nov 2021 Posts: 92
|
|
Posted: Sat Mar 11, 2023 6:24 am |
|
|
receiver Code: | #include <16f877a.h>
#fuses PUT, HS, NOWDT, NOLVP, NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N, errors)
#use standard_io(A)
#use standard_io(B)
void main()
{
setup_adc(adc_off);
set_tris_d(0x00);
output_d(0x00);
output_high(pin_D1);
delay_ms(1000);
output_low(pin_D1);
delay_ms(1000);
output_high(pin_D1);
char data;
while(true)
{
data=getc();
if(data=='g')
{
output_high(pin_A1);
output_low(pin_A2);
output_low(pin_B1);
}
else if(data=='r')
{
output_low(pin_A1);
output_high(pin_A2);
output_low(pin_B1);
}
else if(data=='b')
{
output_low(pin_A1);
output_low(pin_A2);
output_high(pin_B1);
}
}
}
|
transmitter Code: | #include <16F877A.h>
#fuses PUT, HS, NOWDT, NOLVP, NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N, errors)
#define button1 pin_b1
#define button2 pin_b2
#define button3 pin_b4
void main()
{
set_tris_d(0x00);
output_d(0x00);
output_high(pin_D1);
delay_ms(1000);
output_low(pin_D1);
delay_ms(1000);
output_high(pin_D1);
while(true)
{
if(input(button1)){
putc('g');
}
if(input(button2)){
putc('r');
}
if(input(button3)){
putc('b');
}
}
}
|
Greetings,
I also added push button for each led.It worked well but when I pressed long button3, pin_b1 led is flashing with other two led.Why? For other two leds do not occur such thing
(I have resistors here for leds) |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19536
|
|
Posted: Sat Mar 11, 2023 7:35 am |
|
|
You need to add the current limiting resistors. What is happening is the
LED's have different forward voltages according to their colour. Certain
combinations have a low enough voltage, that they are making the chip
reset when you turn them on. Result disaster.....
Understand that an LED, once the voltage gets above it;s Vf, is effectively
a short. You can never directly connect this to a power source without
smething to limit the current flowing. |
|
|
Khansokhua
Joined: 06 Nov 2021 Posts: 92
|
|
Posted: Sat Mar 11, 2023 9:00 am |
|
|
Thank you for clear explanation,
yet what is the problem above?
My current limiting resistors OK |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19536
|
|
Posted: Sun Mar 12, 2023 3:12 am |
|
|
Seriously. Hardware.
Now one 0.1uF is not enough. You have two chips, with two pairs of Vdd/Vss
pins, each needs a 0.1uF capacitor within 12mm of these pins, so 4*0.1uF.
You say you have a 5v supply, but have given no details of this. It may
well have it's own internal capacitor, but if this is a distance from the board,
another larger capacitor is needed at the board. How is this generated, and
connected?.
Then the layout of the supply lines. The ground lines need to be laid out
radially from a single point, using good thick wire.
How far apart are the processors, how are the connections done?.
Now you say 'current limiting resistors are OK'. what size are these, how
have you connected these?. The fact that you omitted these originally tells
us you do not really understand how things need to be wired, so tell us
everything about how the boards are built and wired. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Sun Mar 12, 2023 10:01 am |
|
|
could be a transmitter problem.....
depending on the signal conditioning of the 'buttons'. |
|
|
mdemuth
Joined: 16 Apr 2007 Posts: 71 Location: Stuttgart, Germany
|
|
Posted: Mon Mar 13, 2023 1:01 am |
|
|
Your receiver should check first if there is something in the UART and then take out the character:
Code: |
while (!kbhit()) // wait for character
{
delay_us(20);
counter++;
if (bit_test(counter,7)) output_high(LED); // blinking LED indicates that we are in this loop
else output_low(LED);
}
rx_char = getc(); // put received character into buffer
// Now you can check what has been received
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19536
|
|
Posted: Mon Mar 13, 2023 3:36 am |
|
|
Honestly, I'd say he needs to start with the 'slave' unit, and run just a basic
flash an LED program. However on all three LED's, with a pattern, so he can
see if the unit resets at any point. Once this is running correctly, the same
with the master unit. Then have this with buttons to control the LED's on
this. Again using patterns, so he can see that the unit responds correctly
to the buttons. Only once both are running on their own, then start testing
with a communication link. |
|
|
Khansokhua
Joined: 06 Nov 2021 Posts: 92
|
|
Posted: Mon Mar 13, 2023 8:12 am |
|
|
Ttelmah wrote: | Seriously. Hardware.
Now one 0.1uF is not enough. You have two chips, with two pairs of Vdd/Vss
pins, each needs a 0.1uF capacitor within 12mm of these pins, so 4*0.1uF.
You say you have a 5v supply, but have given no details of this. It may
well have it's own internal capacitor, but if this is a distance from the board,
another larger capacitor is needed at the board. How is this generated, and
connected?.
Then the layout of the supply lines. The ground lines need to be laid out
radially from a single point, using good thick wire.
How far apart are the processors, how are the connections done?.
Now you say 'current limiting resistors are OK'. what size are these, how
have you connected these?. The fact that you omitted these originally tells
us you do not really understand how things need to be wired, so tell us
everything about how the boards are built and wired. |
That is true, you are right sir.
2 Supply voltage seperate for each pic 5V DC <700mA,
tx (push buttons) side //////////// rx (leds) side
pin_C6/TX connected with //////////// pin_C7/RX pin
Leds have 390 ohm resistors.
|
|
|
Khansokhua
Joined: 06 Nov 2021 Posts: 92
|
|
Posted: Tue Mar 14, 2023 8:51 am |
|
|
Greetings,
here I changed program into this and it is working smoothly.No more multiple blink.So what was the problem? What would you say about that? I send 8 bit data and it prevents another action?
Code: | #include <16f877a.h>
#fuses PUT, HS, NOWDT, NOLVP, NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N, errors)
#use standard_io(B)
void main()
{
char data;
while(1)
{
data=getc();
if(data==0b00000010)
{
output_high(pin_B1);
output_low(pin_B2);
output_low(pin_B3);
}
if(data==0b00000100)
{
output_low(pin_B1);
output_high(pin_B2);
output_low(pin_B3);
}
if(data==0b00001000)
{
output_low(pin_B1);
output_low(pin_B2);
output_high(pin_B3);
}
}
}
|
|
|
|
|
|
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
|