Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

hc05 bluetooth and pic 16f1847

Status
Not open for further replies.

Gopi Vh

Newbie level 4
Newbie level 4
Joined
Jan 10, 2015
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
44
hi
i used the below code for 18f4520 it was perfectly working ,but its not working in 16f1847 .I am confused what is wrong ,description about the project is
rx/dt -rb1, tx/ck-rb2, if i recieve char it indicates by turning on or off led .
I am using 16f1847 , hc05 bluettoth, android mobile app


Code:
  unsigned char uart_rd;
 void main()
{
PORTA =0;
PORTB =0; 
ADRESL = 0;
ADRESH = 0;
 TRISB = 0;
 TRISA = 0;
 TRISB.F1 =1;
 TRISB.F2 =0;
  CM1CON0 = 0;

  CM2CON1= 0;


UART1_Init(9600 ); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize*/

 while (1)
 {
  if(UART1_Data_Ready())
    { // If data is received,
     uart_rd = UART1_Read();
     UART1_Write(uart_rd);
    }  
      Delay_ms(100);
     if(uart_rd == 'a')
     {
     LATB.F3 = 0;
        LATB.F4 = 0;

     }

      if(uart_rd == 'b')
     {
     LATB.F3 = 0;
        LATB.F4 = 0;

     }

      if(uart_rd == 'c')
     {
     LATB.F3 = 0;
        LATB.F4 = 0;

     }

      if(uart_rd == 'd')
     {
     LATB.F3 = 0;
        LATB.F4 = 0;

     }

      if(uart_rd == 'e')
     {
     LATB.F3 = 0;
        LATB.F4 = 0;

     }

      if(uart_rd == 'f')
     {
     LATB.F3 = 0;
        LATB.F4 = 0;

     }

      if(uart_rd == 'g')
     {
     LATB.F3 = 0;
        LATB.F4 = 0;

     }

      if(uart_rd == 'h')
     {
     LATB.F3 = 0;
        LATB.F4 = 0;

     }

       if(uart_rd == 'B')
     {
     LATB.F3 = 1;
        LATB.F4 = 1;

     }

       if(uart_rd == 'C')
     {
     LATB.F3 = 1;
        LATB.F4 = 1;

     }

       if(uart_rd == 'D')
     {
     LATB.F3 = 1;
        LATB.F4 = 1;

     }

       if(uart_rd == 'E')
     {
     LATB.F3 = 1;
        LATB.F4 = 1;

     }

       if(uart_rd == 'F')
     {
     LATB.F3 = 1;
        LATB.F4 = 1;

     }

       if(uart_rd == 'G')
     {
     LATB.F3 = 1;
        LATB.F4 = 1;

     }

       if(uart_rd == 'H')
     {
     LATB.F3 = 1;
        LATB.F4 = 1;

        LATA.F7 = 1;
          LATA.F6 = 1;
     }

       if(uart_rd == 'A')
     {
     LATB.F3 = 1;
        LATB.F4 = 1;

     }
      if(uart_rd == 'S')
        {  LATB.F3 = 1;
        LATB.F4 = 1;

          }
              Delay_ms(1000); //RCSTA  = 0x00;
             }

}
 
Last edited by a moderator:

hello,


16F1847 is a bit particular...*
to use UART on RB1,RB2


Code:
 APFCON1.TXCKSEL=0; //  TX/CK function is on RB2
before UART1_Init(9600);
 

Please Zip and post the complete mikroC PRO PIC project files. I will see what is the problem.
 

As you are using mikroC PRO PIC Compiler, I have re written the code using the same compiler. I have used serial interrupt to receive UART data. I think the problem with your code was the below registers were not configured as shown.

Code:
APFCON0 = 0x00;
APFCON1 = 0x00;

ANSELA = 0x00;
ANSELB = 0x00

If you have enabled MCLR pin then you have to pull it to 5V using a 10k resistor. If you have enabled WDT then you have to clear the WDT as required.

If you still want a code which doesn't use serial interrupt then I can make a code for you using

Code:
UART1_Data_Ready()

function. Test the working of my code in hardware and reply. I have compiled the code for INTOSC 4 MHz. I am not sure how stable it is and whether it can be used to generate proper baudrate for UART communication. Try sending some text from PIC to PC using UART. If it prints the text properly then you can make sure that baudrate is generated properly.

I have used INTOSC because when character 'S' is received you are turning on the pins

Code:
LATA.F6

and

LATA.F7

which also have OSCx functions.

Code


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
char uart_rd;
 
void interrupt() {
    if(RCIF_bit) {
        if(OERR_bit) {
             OERR_bit = 0;
             CREN_bit = 1;
             CREN_bit = 0;
        }
        
        uart_rd = UART1_Read();
    }
}
 
void main() {
 
    CM1CON0 = 0x00;
    CM2CON0 = 0x00;
    
    ANSELA = 0x00;
    ANSELB = 0x00;
 
    APFCON0 = 0x00;
    APFCON1 = 0x00;
    
    TRISA = 0xC0;
    TRISB = 0x02;
 
    PORTA = 0x00;
    PORTB = 0x00;
 
    LATA = 0x00;
    LATB = 0x00;
 
    UART1_Init(9600);
    Delay_ms(200);
    
    RCIE_bit = 0;
    RCIF_bit = 0;
    
    PEIE_bit = 0;
    GIE_bit = 0;
 
 
    while(1) {
 
          if(uart_rd) {
               if((uart_rd == 'a') || (uart_rd == 'b') || (uart_rd == 'c') || (uart_rd == 'd') ||
                  (uart_rd == 'e') || (uart_rd == 'f') || (uart_rd == 'g') || (uart_rd == 'h')) {
                          LATB.F3 = 0;
                          LATB.F4 = 0;
               }
               else if((uart_rd == 'A') || (uart_rd == 'B') || (uart_rd == 'C') || (uart_rd == 'D') ||
                  (uart_rd == 'E') || (uart_rd == 'F') || (uart_rd == 'G') || (uart_rd == 'S')) {
                          LATB.F3 = 1;
                          LATB.F4 = 1;
               }
               else if(UART_rd == 'H') {
                          LATB.F3 = 1;
                          LATB.F4 = 1;
                          LATA.F6 = 1;
                          LATA.F7 = 1;
               }
               
               uart_rd = '\0';
          }
     }
}

 

Attachments

  • UART.rar
    19.9 KB · Views: 65

Thanks for your reply .It is not responding ,data is receiving but no led on /off .I have tried with your code also
 

Try using external crystal on RA6 and RA7 and see if it works. Compile the code for 4MHz XT Oscillator. Show your circuit. Have you connected PIC to PC ?

If yes, how ? What is the baudrate set in PC for the COM port used for communication ?

Try the attached project with INTOSC. RA6 and RA7 are used for LEDs. Connect the PIC to PC using a TTL2USB adapter. Set baudrate in PC to 9600 bps. The serial Terminal software should display a string. I have done changes in the way the if() conditions are used but that will not affect the working of the code.

The problem was RCIF_bit was not cleared in ISR. So, when character was received in ISR the program execution was stucking in ISR and remain there. I have now cleared the RCIF_bit in ISR. Test this and reply.
 

Attachments

  • UART rev1.rar
    21.8 KB · Views: 64
Last edited:

hello,

The problem was RCIF_bit was not cleared in ISR

RCIF is an exception in ISR treatment
RCIF can not be cleared by software (put zero in it)
a reading of UART clear this bit ...


Code:
  RCIE_bit =[B] [COLOR=#ff0000]1[/COLOR][/B];    //  must be set  to valide UART RX interrupt
  RCIF_bit = 0;
  
  PEIE_bit =[COLOR=#ff0000][/COLOR][COLOR=#ff0000][B]1[/B][/COLOR];  //   must be set  to valide peripheral interrupt
  GIE_bit = [COLOR=#ff0000][B]1[/B][/COLOR];  // must be set to valide every interrupt

Treat also Frame error (RCSTA.FERR) in ISR..
 
Last edited:

Yes, paulfjujo is right. I made a mistake.

This is wrong.

Code:
RCIE_bit = 0;

This is right

Code:
RCIE_bit = 1;
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top