Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

pic 18 esuart to rn42

Status
Not open for further replies.

Joe G

Member
I have been learning to use the uart to bluetooth to turn an led on/ off for now, I had it working on a 44pin p18f45k20 demo brd. I have tranferd the code to a Pic18f46k80 but I am missing or incorrectly adjusted it?? It's not recieving anything, I just can't figure out what I'm missing? any help or guidance would be appreciated. I can still connect to the RN42 module with teraterm and configure it but can't seem to get the pic to hear it?
Code:
void setupuart()  // ==========================================================================
{
   LATEbits.LATE1=1;
//   OSCCON = 0b01010010;  //4meg   
//   PIC18F46K80


SPBRGH1 = 1; 
SPBRG1 = 25;  // 4mhz@9600bps
BAUDCON1bits.BRG16 = 1;
TRISC = 0b11000000; // RC6/TX/CK, RC7/RX/DT
TXSTA1=0b00100100;

ZeroRegs();
RCSTA1=0b10010000;   // bit7 spen enables the EUSART1
       // bit2 = set = Framing error (can be cleared by reading the RCREGx register and receiving next valid byte) 
       // bit1 = set = Overrun error (can be cleared by clearing bit, CREN)
RCSTA1bits.CREN = 1;   // rec on
//TXSTA1bits.TXEN = 0;   // tx off

   if(PIR1bits.RC1IF==1)   // set if full
     {READ=RCREG1;   //reading the RCREG
     READ=RCREG1;   //read the RCREG twice to make sure it's empty.
     }
     LATEbits.LATE1=0; 


}   // end of setupuart
//   ++++++++++++++++++  uart  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

void bluetooth()
{
while(1)
   {

while(PIR1bits.RC1IF==0)
   { } //RCIF interrupt flag bit will be set when a character is transferred
       //  clears when its read.
   if(RCSTA1bits.FERR==1)   //  Read the RCSTA register to get the error flags
     {
     READ=RCREG1;   //reading the RCREG
     if(RCSTA1bits.FERR==1)  // is it still set
     RCSTA1bits.SPEN=0;
     }

   if(RCSTA1bits.OERR==1)
     {RCSTA1bits.SPEN=0;
     RCSTA1bits.CREN = 0;   // rec back off
     }
   Delay10TCYx(1);
   RCSTA1bits.SPEN=1;
   RCSTA1bits.CREN = 1;   // rec back on
   
   READ=RCREG1;   //reading the RCREG  ASCII
LATCbits.LATC0=0;
     if(READ==0x31)   //  1
       {
     LATEbits.LATE1=1;
     LATCbits.LATC0=1;
     /*   LATD = 0xff;
       n = 0x59;   //  y FOR YES
       write ();
       n = 0x45;   //  e FOR YES
       write ();
       n = 0x53;   //  s FOR YES
       write ();

       n = 0x3A; 
       write ();
       n = 0x29; 
       write ();
       n = 0x20;   // space
       write ();
     */
       }

     else if(READ==0x30)   //  0
       {
       /*LATD = 0b01001000;
       n= 0x4F;     // off
       write ();
       n= 0x46;   
       write ();
       write ();
       n = 0x20;   // space
       write ();
       */
     LATEbits.LATE1=0;
LATCbits.LATC0=0;
       }





     else
       {
       LATCbits.LATC0=1;
/*
       LATD=0b00000001;
       n = 0x45;   //  ? FOR ERROR
       write ();
       n = 0x52;   //  ? FOR ERROR
       write ();
       write ();
       n = 0x4F;   //  ? FOR ERROR
       write ();
       n = 0x52;   //  ? FOR ERROR
       write ();
       n = 0x3f;   //  ? FOR ERROR
       write ();
       n = 0x20;   // space
       write ();
     
*/
       }

   }
 
}
 
Last edited by a moderator:
looks like the 18F46k80 has a CAN module. A quick view of the datasheet says the default register values mean it is enabled on reset, and uses the same pins as EUSART1 (RC6/7). You'll need to write 0x20, to 'CANCON' register, which either disables the CAN module, or puts it in sleep mode. Only checked the datasheet for like 20 seconds, so could be wrong :)

Ok, another quick check, and it seems that the CAN module is on RB2/2 by default, and a config bit determines if they are on RC6/7
 
Last edited:
I tried the 0x20, to 'CANCON' register, turns it off/sleeps, but to no-aval....it was worth a try, Thanks. I know it's something stupid but untill I figure it out :banghead:
 
I hear ya, its always the very simple things that take the longest to find >.< Are you just testing it with bare bones EUSART code and using terminal? The standard 'if character recieved, send 'a' or something would work, a loop back, or just adding one to the received character and echoing it.

More things to try!..
The bit in the BAUDCON register that inverts the recieved data is unknown on reset, so might be worth clearing that to be sure **broken link removed** (pg 336).

And, looking at it .. the following:

Code:
SPBRGH1 = 1;
SPBRG1 = 25;  // 4mhz@9600bps
BAUDCON1bits.BRG16 = 1;

You've got BRG16 - so 16-bit baud value, and you set BGRH bit, for high speed. For a 4MHz clock, and 9600 baud, SPBRGH1 should be 0 and 103 .

It seems that you could just use 8-bit baud, with BRGH set, and use your value of 25.

Code:
//SPBRGH1 = 1;  // this should be 0 anyway because.....
SPBRG1 = 25; 
//BAUDCON1bits.BRG16 = 1; // we're not using 16-bit baud gen

Pg 339 of the datasheet has a nice table with many possible values (too many..). Pg 340 has the SPBRGH/SPBRG1 values for 16-bit mode with BRGH set, same error, so might as well just use your 25 value, and go with8-bit baud gen.
 
Last edited:
I'm using it for a larger project. It's the last part I'm working on to finish it. I coded and tested it with the pickit3 demo board, and worked great. but adding it to the other pic is the headack right now,
On the demo brd, I had it turn on/off/blink leds and return "on, off, error" which worked great from tera-term and from my android phone.
and yes, I have just the usart and blue tooth code running on this board, untill I get it straightend out. then I'll incorperate the interupts and the rest of the code.
 
Sorry, edited the above post, probably should have posted a new reply so you get notifications >.< Although its been a while since I've used the slightly more complicated EUSART in the 18F series, I reckon that should get it working.
 
I did it again, I had the RN-42 Rx going to the Pic's Rx, ooooops:arghh:, I knew it was something stuped.....lol, thanks you again for the help.
 
ahh well, it happens, good luck man and cheers for posting the solution, nice to have threads wrapped up eh
 
Status
Not open for further replies.

Latest threads

Back
Top