CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PIC to PC data send problem, via Bluetooth module
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
iceman999



Joined: 13 Dec 2012
Posts: 15

View user's profile Send private message

PIC to PC data send problem, via Bluetooth module
PostPosted: Thu Dec 13, 2012 8:25 am     Reply with quote

Hello,
I'm having a problem about my project, that i can't solve it... I'm using PIC 18F2550, compiler 4.083 and bluetooth module RF-BT0417C.

I'm trying to send data via hardware UART, but i get strange signs in hyperterminal.

This is my code (main.c):
Code:
#include "C:\Users\Jan\Documents\Nova mapa\main.h"


//bootloader
#define _bootload
#ifdef _bootload
#define LOADER_END 0x7FF
#define LOADER_SIZE 0x6FF
#build(reset=LOADER_END+1, interrupt=LOADER_END+9)
#org 0, LOADER_END {}
#endif




void main()
{
   
   setup_adc_ports(AN0_TO_AN3|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
   setup_timer_2(T2_DIV_BY_1,0,1);
   setup_timer_3(T3_INTERNAL|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts( global );
   enable_interrupts( int_timer0 );
   enable_interrupts( int_timer1 );
   

   // TODO: USER CODE!!
   
       
while(1){


printf("1");
delay_ms(100);
}

}


main.h:

Code:
#include <18F2550.h>
#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES XT                       //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES MCLR                     //Master Clear pin enabled
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL12                    //Divide By 12(48MHz oscillator input)
#FUSES CPUDIV4                  //System Clock by 4
#FUSES USBDIV                   //USB clock source comes from PLL divide by 2
#FUSES VREGEN                   //USB voltage regulator enabled

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)


Baudrate is 9600, parity none, bits 8 on bluetooth module and PC, values match...

PIC's output level is 5V, but bluetooth works with 3.3V so i leveled it with resistors, if that is okay...

If i want to send "1" i get "ý" in hyperterminal and i don't know what is wrong.

Hyperterminal:


Please help Wink
Thanks.
temtronic



Joined: 01 Jul 2010
Posts: 9182
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Dec 13, 2012 8:37 am     Reply with quote

comments...
1) no it's not 'alright' to just resistors ! You MUST use proper 'level translation' chips to effect proper signalling.

2) the data coming from the PIC is TTL,is this acceptable to the bluetooth module?

3) Remove the bluetooth modules, instead use a MAX232 or equal chip and connect to PC to confirm you can communicate properly,providing the PC has a real comport(RS232).If this works, then you have an 'issue' with the bluetooth installation,whether it's PIC side ot PC side,that is unkown to us.

4) always add 'errors' to the Use RS232(....) preprocessor directive.This will allow the program to continue should an overrun or other UART error occours.
iceman999



Joined: 13 Dec 2012
Posts: 15

View user's profile Send private message

PostPosted: Thu Dec 13, 2012 9:24 am     Reply with quote

Thanks for reply.

1) So i need special chip or is there alternative, like zener or transistor shifting?
2) TTL is acceptable for this bluetooth module.
3) First i will try to level this right, if that doesn't do the trick, i will try with RS232.
4) I added errors.
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Thu Dec 13, 2012 9:38 am     Reply with quote

Hi,

A couple of comments:

1. Get rid of the bootloader code in your test program - it's not needed.
2. Get rid of the setup code for the peripherals - it's not needed.
3. Get rid of the interrupt code - enabling interrupts without the appropriate
handlers will cause your code to run erratically.

Do you know your PIC is operating correctly? Attach an LED to an I/O pin and
see if you can blink it at a predictable rate, ie. 1/2 second ON, and 1/2 second
off....

A resistive divider will work when connecting the PIC Tx pin to the BT Rx pin.
What resistor values are you using?

You will need to use a proper level shifter if you plan to receive data by the
PIC as well. A pair of transistors will work nicely.

Is the BT module successfully "paired" with the PC??

My guess is that your PIC is not running at the speed you think it is, and thus
you have a baud rate issue.

Good Luck!

John
Ttelmah



Joined: 11 Mar 2010
Posts: 19389

View user's profile Send private message

PostPosted: Thu Dec 13, 2012 9:51 am     Reply with quote

Other comment. You may not be running at 20MHz. XT, is _not_ the right oscillator fuse for a 20MHz crystal.
Then you tell the system to divide this by 12 - will this give the required 4MHz to the PLL?. Then you have the master crystal divided by four to feed the CPU. Even if the crystal is working, you'd then be at 5MHz....

Best Wishes
iceman999



Joined: 13 Dec 2012
Posts: 15

View user's profile Send private message

PostPosted: Thu Dec 13, 2012 10:06 am     Reply with quote

Hello,
thanks for reply.

1) I need bootloader code for my USB programmer. PIC wizard generated setup codes, so i left them. I will try without them.

2) PIC is operating correctly, i have LED on I/O pin and i can control ON and OFF time without problem.

3) I have resistor divider connected from Tx on PIC to Rx on BT, with values 3,3K and 1,8K. So you think that would be ok, if i'm using just resistors?

4) At this point i don't need to recive any data, but when i do, i will use level shifter.

5) BT is paired with PC, i used default code and it connected fine...

6) I'm using 20MHz crystal, like you can see in program. How can i make sure that baud rate is correct?


Thanks.
iceman999



Joined: 13 Dec 2012
Posts: 15

View user's profile Send private message

PostPosted: Thu Dec 13, 2012 10:20 am     Reply with quote

So i have to use another fuses? That is funny because PIC wizard generated them automatically.

Thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19389

View user's profile Send private message

PostPosted: Thu Dec 13, 2012 10:40 am     Reply with quote

The Wizard generates things according to what _you_ tell it. For instance, you manually have to select the 'errors' box, or it leaves this out of the hardware RS232 configuration, where it is _required_ unless you are handling error conditions yourself. On the clock, it does not know the implications of the fuses you select, and you can tell it to use a PLL12, and it won't care that this cannot work with the other settings you select. Similarly, you can select an oscillator, and it _will_ tell you in the selection what frequencies this is for, but it assumes you know what you are doing, if you have selected a frequency outside this range. The wizard needs you to drive it....

What is your crystal?.

Best Wishes
iceman999



Joined: 13 Dec 2012
Posts: 15

View user's profile Send private message

PostPosted: Thu Dec 13, 2012 10:42 am     Reply with quote

My crystal is 20MHz...
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Thu Dec 13, 2012 11:05 am     Reply with quote

Hi,

So, using a stopwatch, you have confirmed that your LED is turning Off and
On at the desired rate?

John
Ttelmah



Joined: 11 Mar 2010
Posts: 19389

View user's profile Send private message

PostPosted: Thu Dec 13, 2012 11:58 am     Reply with quote

So, you need:
Code:

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                           //needed for 20MHz
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOIESO                     //Internal External Switch Over mode enabled
#FUSES NOFCMEN                  //No fail safe
#FUSES NOPBADEN                //You are turning off the ADC
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES MCLR                       //Master Clear pin enabled
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL5                        //Divide By 5(20MHz oscillator input)
#FUSES CPUDIV1                  //System Clock by 1
#FUSES USBDIV                   //USB clock source comes from PLL divide by 2
#FUSES VREGEN                   //USB voltage regulator enabled

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)

//you also need one other change

   setup_spi(FALSE);


If you look at the remarks inserted by the wizard, it is telling you (for instance), that XT is not for a 20MHz crystal, and that PLL12 is for a 48MHz clock input. The setup_spi line is a fault in the wizard...

At least you then have a chance of the chip running. If it doesn't run, then your oscillator is not starting (this is why I've turned off the 'fail safe' fallback, since this way you will see it doesn't work).

Best Wishes
iceman999



Joined: 13 Dec 2012
Posts: 15

View user's profile Send private message

PostPosted: Thu Dec 13, 2012 12:54 pm     Reply with quote

First, thanks for corrections. Now i know what is important, but hyperterminal still gets wrong data... I'm turning LED on and off every 500ms, but it is blinking very fast, so half a second is like 100ms. Frequency is still wrong...
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Thu Dec 13, 2012 1:05 pm     Reply with quote

Hi,

Earlier you said:

Quote:

2) PIC is operating correctly, i have LED on I/O pin and i can control ON and OFF time without problem.


Which was actually not correct. As I (and Ttelmah) suspected, your PIC is
not running at the speed you think it's running at..... Your serial COMMs will
not work correctly until you resolve this issue..... There is lots of 18F2550
code on the forum. Do a search and find the fuses that have worked for
other projects.

BTW, it's a good idea to do the "LED test" on every project you work on
to ensure the PIC is (1) executing code, and (2) it's operating at the
intended speed. This simple test would have immediately told you that
you have a problem.....

John
iceman999



Joined: 13 Dec 2012
Posts: 15

View user's profile Send private message

PostPosted: Thu Dec 13, 2012 1:20 pm     Reply with quote

You are right, i thought that PIC is working at right speed, but this speed was approximately right for my eye, but not for controller or PC.

But now the question is, why now LED flashes so fast, after i changed code from Ttelmah...
Ttelmah



Joined: 11 Mar 2010
Posts: 19389

View user's profile Send private message

PostPosted: Thu Dec 13, 2012 3:40 pm     Reply with quote

5:1, is not possible. The maximum speed of the CPU is 48MHz....

Now, what is the actual version number of the compiler?. Will check whether fuses are being set right by this version if you tell us.
It sounds as if the chip is behaving as it it is using HSPLL, rather than HS, which with the other settings as given, would give 48MHz operation.
Are you sure you are putting the fuses, and clock rate 'as posted', and not changing anything?.
Triple check you haven't got another set of fuse statements somewhere. For instance, if you are including the USB files, some of these assume you want to run at 48MHz, and set HSPLL, if you include the example parts, rather than just the drivers.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
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