View previous topic :: View next topic |
Author |
Message |
ozgok
Joined: 14 Apr 2014 Posts: 5
|
18f4550 serial communication error |
Posted: Mon Apr 14, 2014 8:59 am |
|
|
hi all;
i design a board with 18f4550. it communicates with pc by HC-05 bluetooth card. But it cant :( . Can anyone check my codes and help me what is wrong with it?
Code: |
#include <18F4550.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#fuses HSPLL,PLL5,CPUDIV1,NOWDT,PUT,BROWNOUT,NOLVP,NOXINST
#use delay(clock=48000000,crystal=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8,STOP=1,ERRORS)
#USE FAST_IO(D)
char gelen;
#int_rda
void seri_haberlesme_kesmesi()
{
output_high(pin_d5);
while (kbhit())
{
gelen=getc();
}
}
void main()
{
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);
enable_interrupts(int_rda);
enable_interrupts(global);
set_tris_d(0x00);
output_high(pin_d5);
delay_ms(1000);
output_high(pin_d6);
delay_ms(1000);
output_high(pin_d7);
delay_ms(1000);
output_d(0);
while(true)
{
output_low(pin_d5);
putc(gelen);
delay_ms(300);
if(gelen=='A')
{
output_high(pin_d7);
output_low(pin_d6);
}
if(gelen=='B')
{
output_low(pin_d7);
output_high(pin_d6);
}
}
}
|
|
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon Apr 14, 2014 9:07 am |
|
|
Hi,
What is the Vdd voltage of your PIC? what is the Vdd voltage of your bluetooth modem?
Start simple. Can you flash an LED connected to an unused I/O pin at a predictable rate, say one second On, and one second Off? Can you connect a MAX232 IC to the PIC UART and send data back and forth to your PC?
My hunch is that your #1 issue is a hardware one, ie. you are trying to interface a 3.3V peripheral to a +5V PIC. This topis has been covered many, many, many times before!
Also, post your schematic for us to look at. You have to upload it somewhere (a free file hosting service works!), and then post the link to the file here.
John |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Apr 14, 2014 9:10 am |
|
|
Start with the physical. How have you got the HC05 connected to the PIC?. Do you have the required level translator?. The PIC's serial input is a _Schmitt_ level input. Requires the signal to go up to 4v. Without a suitable level translator you won't receive anything....
Separately, there is nothing in the code to say that a character has actually arrived. The outputs will be continuously updating even when a character has not changed. You need something to say 'a character has arrived', and ro only change the outputs when this goes true. |
|
|
ozgok
Joined: 14 Apr 2014 Posts: 5
|
|
Posted: Mon Apr 14, 2014 11:34 am |
|
|
Ezflyr and Ttelmah..You are very nice people thanks alot for all of your reply. Now i connect hc05 tx pin to pic rx pin directly and rx pin to pic tx pin via 1k resistor. There is also a 2k ground resistor after 1k. Hc05 run with 3.3v. I think that 3.3v is logic 1 for pic at tx pin. Sometimes pic response after a dozen try. Sometimes not. Is it necessary using a schmitt or max232? Ezflyr can you add the old topic about this subject if easy? If not i will search it. I am waiting both of your instructions? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Apr 14, 2014 12:01 pm |
|
|
That won't work reliably.
The problem is the input threshold of the PIC pin. Normal 'TTL' pins have a 2.4v threshold so can be directly driven by 3v chips, but the serial input on the PIC has a Schmitt input (designed to improve noise immunity), so requires the signal to be taken up to nearly 4v.
You need to add something like a 5v TTL buffer between the HC, and the PIC, which will then give a 5v output to drive the PIC input.
A MAX232 is for a different job. It generates/receives the even higher voltage RS232 standard. Inverts the signals as well. Your unit doesn't use this. |
|
|
ozgok
Joined: 14 Apr 2014 Posts: 5
|
|
Posted: Mon Apr 14, 2014 12:12 pm |
|
|
Ttelmah,
I understand that i must not use max232 from your reply. So can i use 74hc14? Is it solve the problem? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Apr 14, 2014 1:32 pm |
|
|
Unfortunately, no.
This has Schmitt inputs as well, so has the same problem as the PIC, and is also inverting which you don't want.
The classic buffer is something like the 74HCT125. Wire the enable to ground, run the chip from 5v, and each gate merrily adapts a signal from a 3.3v chip to drive a 5v chip. |
|
|
ozgok
Joined: 14 Apr 2014 Posts: 5
|
|
Posted: Thu Apr 17, 2014 12:59 am |
|
|
Hi again
Ttelmah i tried what you said. and nothing change. do you have a knowledge about bluetooth signals? i measure tx pin of hc05 and see 3.32V always while no pairing. 3.32V after pairing. and 3.29V while sending data to pic. Is this a normal running condition. Is HC05 broken or failed?
Or is there another problem?
I am lost. I cant sleep. Please help me. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Thu Apr 17, 2014 6:30 am |
|
|
Hi,
Well, of course there is still a problem, otherwise it would work, right?
In my first reply, I asked you if you could flash an LED at a predictable rate. Verifying your #Fuse settings and your crystal is an important first step. You ignored this question. I also asked you to post a schematic. Verifying that your hardware is correct is another important first step. You ignored this question.
So, I would say that if you want further help, and you want to obtain it here, you should follow the suggestions/requests that are offered to you. Many of us have gone down this road many, many times before, so we know the common pitfalls and, most importantly, we know the *proper* troubleshooting/debugging procedures!
Good Luck!
John |
|
|
ozgok
Joined: 14 Apr 2014 Posts: 5
|
SOLVED |
Posted: Mon Apr 21, 2014 2:44 am |
|
|
Thanks all,
i solve the problem. the problem is at my pc side. A serial hardware problem. |
|
|
|