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.

[SOLVED] problems when configuring pic18f26j50

Status
Not open for further replies.

TiagoRibeiro

Full Member level 2
Full Member level 2
Joined
Oct 29, 2013
Messages
130
Helped
18
Reputation
36
Reaction score
17
Trophy points
28
Location
São Paulo/Brazil
Visit site
Activity points
765
hello everyone, I'm posting this really doubt it
*do not get an answer anywhere on the internet.
I am using a development kit that includes
one pic18f26j50 / SIM900 module / GPS.
played a lot with him through the AT command, using
the COM port of the PC. But now I need to do it
function using the pic. But then that's my problem, I
I do not know much of the language C and I'm using a
program called MPLABX, very good for working
however I do not know how to set the oscillator 12MHZ
*What's in the kit.

I've been looking at the datasheet PIC18F26J50 and found
this formula:

{Example
For a device with Fosc of 16 MHz Desired baud rate of 9600, Asynchronous mode, and
8-bit BRG:
Desired Baud Rate = Fosc / (64 ([SPBRGHx: SPBRGx] + 1))
Solving for SPBRGHx: SPBRGx:
X = ((Fosc / Desired Baud Rate) / 64) - 1
= ((16000000/9600) / 64) - 1
= [25,042] = 25
Calculated Baud Rate = 16000000 / (64 (25 + 1))
= 9615
Error = (Calculated Baud Rate - Desired Baud Rate) / Desired Baud Rate
= (9615 - 9600) / 9600 = 0,16%
}
I used this formula when I arrived at the value 18 and used like this:
Open1USART (USART_TX_INT_OFF
****************& USART_RX_INT_OFF
****************& USART_ASYNCH_MODE
****************& USART_EIGHT_BIT
****************& USART_CONT_RX
****************& USART_BRGH_HIGH, 18) ;/ / GSM 9600 Baudrate
But when using LOGIC(SALEAE) to see communication between the PIC and
the gsm module only saw frame error;
I made a program using MikroC and it worked, but he does it all, no need to manually configure the pic.
I would like to learn how to configure the baud rate and the pic MPLABX
Could someone explain to me how to do this?
 

I did what you tell me but dont works..any advise?
this is the new code..sorry but i really dont know how to do pic works:sad:

Code:
void ConfigUSART(void)
    {
    TRISA = 0b00101101;
    TRISB = 0b00011011;
    TRISC = 0b10000010;
  [COLOR="#FF0000"]  [U]OSCCON =111;
    OSCTUNE = 1;[/U][/COLOR]
    
      Open1USART(USART_TX_INT_ON
                &USART_RX_INT_ON
                &USART_ASYNCH_MODE
                &USART_EIGHT_BIT
                &USART_BRGH_HIGH,18);//Baudrate gps 9600
          baud1USART(BAUD_8_BIT_RATE
                     &BAUD_AUTO_OFF);
look at my header settings:

Code:
#pragma config XINST = OFF      // Extended Instruction Set (Enabled)
#pragma config WDTEN = OFF      // Watchdog Timer (Disabled - Controlled by SWDTEN bit)
#pragma config OSC = HSPLL         // Oscillator
#pragma config CPUDIV = OSC4_PLL6
#pragma config PLLDIV = 3       // PLL Prescaler Selection bits (Divide by 3 (12 MHz oscillator input))
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset  (Enabled)
#pragma config LPT1OSC = ON    // Low-Power Timer1 Oscillator (High-power operation)
 
Last edited by a moderator:

Remove all OSCCON. OSCTUNE and #pragma codes and just use these if you are not using USB and PLL.

If you are using XC8 Compiler then include this line...


Code C - [expand]
1
2
3
4
5
#define _XTAL_FREQ 12000000
 
#pragma config XINST = OFF // Extended Instruction Set (Enabled)
#pragma config WDTEN = OFF // Watchdog Timer (Disabled - Controlled by SWDTEN bit)
#pragma config FOSC = HS

 

There is some problem with my PIC18F26J50 library. C18 is giving error reading magic string of library. Try this code. See if this compiles fine for you. I think your SPBRG values are wrong. I have not checked that. You are using same baudrate for both UARTs but SPBRG values are different.


Edit SPBRG = 77 (77.125) is right for 9600 bps baudrate. 18 is wrong.


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
69
70
/* 
 * File:   main.c
 * Author: Tiago Ribeiro
 *
 * Created on 17 de Outubro de 2013, 14:17
 */
 
#include <p18F26J50.h>
#include <stdio.h>
#include <stdlib.h>
#include <delays.h>
#include <string.h>
#include <usart.h>
 
#define _XTAL_FREQ 12000000
 
#pragma config XINST = OFF // Extended Instruction Set (Enabled)
#pragma config WDTEN = OFF // Watchdog Timer (Disabled - Controlled by SWDTEN bit)
#pragma config OSC = HS
#pragma config STVREN = OFF
#pragma config DEBUG = OFF
#pragma config CP0 = OFF
#pragma config IESO = OFF
 
void UART1_Write_Text(unsigned char *uartData);
 
unsigned char *ligar = "\r\nATD02114997571575;\r\n";
 
void ConfigUSART(void){
 
    TRISA = 0b00101101;
    TRISB = 0b00011011;
    TRISC = 0b11000010;
       
    Open1USART(USART_TX_INT_ON
                &USART_RX_INT_ON
                &USART_ASYNCH_MODE
                &USART_EIGHT_BIT
                &USART_BRGH_HIGH,18);//Baudrate gps 9600
    baud1USART(BAUD_8_BIT_RATE
                     &BAUD_AUTO_OFF);
 
 
    Open2USART(USART_TX_INT_ON
                &USART_RX_INT_ON
                &USART_ASYNCH_MODE
                &USART_EIGHT_BIT
                &USART_BRGH_HIGH,77);//Baudrate SIM900 9600
 
    baud2USART(BAUD_8_BIT_RATE
                    &BAUD_AUTO_OFF);
 
    
}
 
void UART1_Write_Text(unsigned char *uartData){
        
    while(*uartData)
            Write1USART(*uartData++);
    
}
        
void main(void){
    
    ConfigUSART();
    
    UART1_Write_Text(ligar);
    
    while(1);
}

 
Last edited:

I did what you tell me..but nothing...
shall be a simple thing ... when i using the mikroC code works, but does not work in mplabx..
:|
 

this pic have two USART, the USART2 is used by GPS module..

- - - Updated - - -

Jayanth this is the formula to find BRG in the datasheet of pic18f46j50 family:
Do you use this to find 77.125 (77)?
{Example
For a device with Fosc of 16 MHz Desired baud rate of 9600, Asynchronous mode, and
8-bit BRG:
Desired Baud Rate = Fosc / (64 ([SPBRGHx: SPBRGx] + 1))
Solving for SPBRGHx: SPBRGx:
X = ((Fosc / Desired Baud Rate) / 64) - 1
= ((16000000/9600) / 64) - 1
= [25,042] = 25
Calculated Baud Rate = 16000000 / (64 (25 + 1))
= 9615
Error = (Calculated Baud Rate - Desired Baud Rate) / Desired Baud Rate
= (9615 - 9600) / 9600 = 0,16%
}
 

Attachments

  • piclib.rar
    154.9 KB · Views: 64

I will check later why C18 USART library is not working. Here is a working code that I wrote. Test it and reply.

https://coolcapengineer.wordpress.c...sart-communication-for-pic18-microcontroller/

Fosc / [16 (SPBRG + 1)] = baudrate

Fosc / (baudrate) * 16 = (SPBRG + 1)

[Fosc / (baudrate) * 16] - 1 = SPBRG

[12000000 / (9600 * 16)] - 1 = SPBRG

78.125 - 1 = SPBRG

77.125

approx 77


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
/* 
 * File:   main.c
 * Author: Tiago Ribeiro
 *
 * Created on 17 de Outubro de 2013, 14:17
 */
 
#include <p18F26J50.h>
#include <stdio.h>
#include <stdlib.h>
#include <delays.h>
#include <string.h>
#include <usart.h>
 
#define _XTAL_FREQ 12000000
 
#pragma config XINST = OFF // Extended Instruction Set (Enabled)
#pragma config WDTEN = OFF // Watchdog Timer (Disabled - Controlled by SWDTEN bit)
#pragma config OSC = HS
#pragma config STVREN = OFF
#pragma config DEBUG = OFF
#pragma config CP0 = OFF
#pragma config IESO = OFF
 
 
void UART1_Write(unsigned char uartData);
void UART1_Write_Text(unsigned char uartData[30]);
 
char ligar[30] = "\r\nATD02114997571575;\r\n";
 
void UART1_Write_Text(unsigned char uartData[30]){
        
    unsigned char i = 0;
 
    while(uartData[i])
            UART1_Write(uartData[i++]); 
}
 
void UART1_Write(unsigned char uartData){
 
      while(!PIR1bits.TX1IF); 
            TXREG = uartData;
            
}
        
void main(void){
    
    TRISCbits.TRISC6 = 0;
    TRISCbits.TRISC7 = 1;
 
    TXSTA1 = 0b00100110;
    RCSTA1 = 0b10010000;
    BAUDCONbits.BRG16 = 0;
 
    SPBRG = 77;
    
    UART1_Write('A');
    UART1_Write('A');
    
    
    while(1){
 
            UART1_Write_Text(ligar);
            
            Delay10KTCYx(50);
 
    }
}



98107d1383675994-uart1.png
 

Attachments

  • MPLAB C18 UART.rar
    53.5 KB · Views: 63
  • uart1.png
    uart1.png
    30.8 KB · Views: 103
Last edited:

No. In the left pane of MPLAB it shows directories like source, headers, libraries, etc... Right click 26j50.lib in that and select remove file then right click libraries folder and choose Add file... and select the 26j50_e.lib file. Compile and test. If thew file is not available then download the C18 v3.46 update and install.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top