• AVR Freaks

AnsweredHot!PIC18F45K80 - USART baudrate calculation

Page: 12345 > Showing page 1 of 5
Author
Djsarkar
Senior Member
  • Total Posts : 175
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
2020/10/03 20:07:16 (permalink)
0

PIC18F45K80 - USART baudrate calculation

Hi,
I have PIC18F45K80. I am using MPLABX 5.40 and XC8 2.30. I want to send data from PIC18F45K80 to laptop using USART.
Desired baud rate = 9600
Internal oscillator frequency = 8Mhz
 
How to  calculate desired baud rate?
 
 
 
post edited by Djsarkar - 2020/10/03 20:12:10
#1
mbrowning
USNA79
  • Total Posts : 1811
  • Reward points : 0
  • Joined: 2005/03/16 14:32:56
  • Location: Melbourne, FL
  • Status: offline
Re: PIC18F45K80 - USART baudrate calculation 2020/10/03 20:28:28 (permalink)
+3 (5)
I have a trick I use to do this that not everyone knows about. I read the datasheet and apply the formulas and tables I find within.

Now that you know the secret you have this power also
#2
ric
Super Member
  • Total Posts : 28713
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC18F45K80 - USART baudrate calculation 2020/10/03 20:35:21 (permalink)
+2 (2)
Agree.
As a leg up, here is the start of the chapter.
http://ww1.microchip.com/downloads/en/DeviceDoc/PIC18F66K80%20FAMILY%20Enhanced%20Flash%20MCU%20with%20ECAN%20XLP%20Technology%2030009977G.pdf#page=330
and you don't even have to use a calculator, the table on page 332 shows the correct value to use to get 9600 baud with an 8MHz clock if BRGH and BRG16 are both zero.
There are also alternative options to use (BRGH = 1, BRG16 = 0) or (BRGH = 0, BRG16 = 1) or (BRGH = 1, BRG16 = 1). Any of them can be used.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#3
Djsarkar
Senior Member
  • Total Posts : 175
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: PIC18F45K80 - USART baudrate calculation 2020/10/03 22:00:37 (permalink)
0
(BRGH = 0, BRG16 = 1)
For a device with FOSC of 8 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
                = ((8000000/9600)/16 -1
                = 833.33/16-1
                = 52-1 = 51
#4
1and0
Access is Denied
  • Total Posts : 11353
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC18F45K80 - USART baudrate calculation 2020/10/03 22:09:30 (permalink)
0
Djsarkar
(BRGH = 0, BRG16 = 1)
For a device with FOSC of 8 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
               = ((8000000/9600)/16 -1
               = 833.33/16-1
               = 52-1 = 51

That should be ...
 
Desired Baud Rate =  FOSC/(16 ([SPBRGHx:SPBRGx] + 1)) Solving for SPBRGHx:SPBRGx:
X = ((FOSC/Desired Baud Rate)/16) – 1
 
... and the value is given already in Table 22-4 in the datasheet. :)
#5
Djsarkar
Senior Member
  • Total Posts : 175
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: PIC18F45K80 - USART baudrate calculation 2020/10/03 22:39:04 (permalink)
0
1and0 
... and the value is given already in Table 22-4 in the datasheet. :)


Yes I know but I was trying to understand formula and I got right value as shown in table 
 
What should be in TRANSMIT STATUS AND CONTROL REGISTER?
 
    //TXSTAx: TRANSMIT STATUS AND CONTROL REGISTER
    CSRC = 1; //Asynchronous mode: Don’t care
    TX9 = 0; //Selects 8-bit transmission bit
    TEN = 1; // Transmit is enabled
    SYNC  = 0;    // Asynchronous mode
    SENDB = 1;// Sends Sync Break on next transmission (cleared by hardware upon completion)
    BRGH  = 1; //High speed
    TRMT = 1; //TSR is empty
    TX9D = 0; // No
#6
ric
Super Member
  • Total Posts : 28713
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC18F45K80 - USART baudrate calculation 2020/10/03 22:42:00 (permalink)
0
Which bit don't you understand?
One of those bits is wrong, but I really am getting tired of reading the datasheet for you.
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#7
Djsarkar
Senior Member
  • Total Posts : 175
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: PIC18F45K80 - USART baudrate calculation 2020/10/03 23:00:37 (permalink)
0
ric
Which bit don't you understand?
One of those bits is wrong, but I really am getting tired of reading the datasheet for you.

I have doubt with SENDB 
SENDB = 1;// Sends Sync Break on next transmission (cleared by hardware upon completion)
 
#8
1and0
Access is Denied
  • Total Posts : 11353
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC18F45K80 - USART baudrate calculation 2020/10/03 23:21:03 (permalink)
+1 (1)
Djsarkar
I have doubt with SENDB 
SENDB = 1;// Sends Sync Break on next transmission (cleared by hardware upon completion)

Read about the Break Character in the EUSART chapter of your PIC datasheet. It is used by the LIN bus standard.
#9
ric
Super Member
  • Total Posts : 28713
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC18F45K80 - USART baudrate calculation 2020/10/03 23:54:26 (permalink)
+1 (1)
Djsarkar
ric
Which bit don't you understand?
One of those bits is wrong, but I really am getting tired of reading the datasheet for you.

I have doubt with SENDB 
SENDB = 1;// Sends Sync Break on next transmission (cleared by hardware upon completion)

So why don't you use your initiative, and keep reading?
There are only a few references to SENDB in the datasheet.
It's so easy to type Ctrl-F (for "Find"), and look at each of them to find out what that bit does for yourself. That's how we had to do it.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#10
vexorg
Senior Member
  • Total Posts : 176
  • Reward points : 0
  • Joined: 2019/09/27 10:59:40
  • Location: 0
  • Status: online
Re: PIC18F45K80 - USART baudrate calculation 2020/10/04 07:18:57 (permalink)
0
You should really use interrupts to handle the incoming data, but as you see, ric likes to explain things, so will let them handle that :D
#11
Djsarkar
Senior Member
  • Total Posts : 175
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: PIC18F45K80 - USART 2020/10/10 00:20:10 (permalink)
0
Here is my complete code for Uart. But when I run code I don't get message on hyper terminal 
 

 
#define _XTAL_FREQ 8000000
#include <xc.h>
// PIC18F45K80 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF       // Extended Instruction Set (Enabled)
// CONFIG1H
#pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)
// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)
// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)
// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)
// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)
// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)
// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)
// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)
// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)

 
void Port_Initialized (void)
{
    LATA = LATB = LATC = LATD = LATE =  0;
    TRISA = 0b0000000;// all are output, Unused
    TRISB = 0b0000000;// all are output, Unused
    TRISC = 0b1000000;
    TRISD = 0b0000000;//all are output, Unused
    TRISE = 0b0000000;// All are output, Unused
 
    ANCON0 = 0; // digital port
    ANCON1 = 0; // digital port
    CM1CON = 0; // Comparator off
    CM2CON = 0; // Comparator off
    ADCON0 = 0; // A/D conversion Disabled
}
   
void send(char message)
{
    while(TXIF==0);  // Wait till the transmitter register becomes empty
    TXIF=0;          // Clear transmitter flag
    TXREG = message;  
}

 

void string(char *p)
{
   while(*p != '\0') { 
     send(*p); 
     p++;
   }
}

 
void Uart_Initialized(void)
{
    //TXSTAx TRANSMIT STATUS AND CONTROL REGISTER
   
   TXSTAbits.CSRC = 0; //: Don’t care.
   TXSTAbits.TX9 = 0; //Selects 8-bit transmissionbit
   TXSTAbits.TXEN = 1; // Transmit Enable
   TXSTAbits.SYNC  = 0;   // Asynchronous mode
   TXSTAbits.SENDB = 0;
   TXSTAbits.BRGH =  1;   //  High speed mode
   TXSTAbits.TRMT = 1; //TSR is empty
   TXSTAbits.TX9D = 0;

 
   //RCSTAx: RECEIVE STATUS AND CONTROL REGISTER
   RCSTAbits.SPEN  = 1 ;   //Serial port enabled
   RCSTAbits.RX9 = 0; //Selects 8-bit reception
   RCSTAbits.SREN  = 1 ;//Don’t care.
   RCSTAbits.CREN  = 1 ;// Enables receiver
   RCSTAbits.ADDEN = 0; //
   RCSTAbits.FERR  = 0 ;// No framing error
   RCSTAbits.OERR  = 1 ;   // Overrun error
   RCSTAbits.RX9D   = 0 ;   //Selects 8-bit reception   
  
   SPBRG = 51;
  
}
 
void main(void)
{
   char message[]= {"Hello world"};
   LATCbits.LATC6 = 0; // TX Pin set as output
   PORTCbits.RC6 = 1; // RX Pin set as input
   Port_Initialized ();
   Uart_Initialized ();
   string(message);
  
    while (1)
    {
        
        
    }
}

 

#12
ric
Super Member
  • Total Posts : 28713
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC18F45K80 - USART 2020/10/10 00:36:35 (permalink)
+1 (1)
Get rid of this line
    TXIF=0;          // Clear transmitter flag
from your send() function.
That is a read only flag, it is impossible to clear it that way.
The only way to clear it is to write to TXREG, which is what you do next.
 
Also get rid of
   TXSTAbits.TRMT = 1; //TSR is empty
That is another read-only flag, so it's pointless to try to write to it.
 
This line does NOT set the pin as an output
   LATCbits.LATC6 = 0; // TX Pin set as output
You set RC6 as an input in your Port_Initialized() function.
Luckily it doesn't matter. The USART forces the pin to be an output.
 
This line also does NOT set RC6 as an input, and it's the wrong pin anyway. RX1 is on RC7
  PORTCbits.RC6 = 1; // RX Pin set as input


How exactly have you connected RC6 to your PC?
If you have a USB to RS232 adaptor, then you need an RS232 style inverting buffer on TX (like a MAX232 chip)
You can avoid the buffer if you are using a USB-TTL serial adaptor on your PC.
 
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#13
Djsarkar
Senior Member
  • Total Posts : 175
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: PIC18F45K80 - USART 2020/10/10 01:17:35 (permalink)
0
ric
Also get rid of
   TXSTAbits.TRMT = 1; //TSR is empty
That is another read-only flag, so it's pointless to try to write to it.

 
Thank you ric to point out so many error in code 
 
TRMT = 1;
 error: use of undeclared identifier 'TRMT'
What should be write to remove error ?
 
new version of code 

 
#define _XTAL_FREQ 8000000
#include <xc.h>
// PIC18F45K80 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF       // Extended Instruction Set (Enabled)
// CONFIG1H
#pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)
// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)
// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)
// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)
// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)
// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)
// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)
// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)
// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)

 
void Port_Initialized (void)
{
    LATA = LATB = LATC = LATD = LATE =  0;
    TRISA = 0b0000000;// all are output, Unused
    TRISB = 0b0000000;// all are output, Unused
    TRISC = 0b1000000; // TX Pin set as output, RX Pin set as input
    TRISD = 0b0000000;//all are output, Unused
    TRISE = 0b0000000;// All are output, Unused
 
    ANCON0 = 0; // digital port
    ANCON1 = 0; // digital port
    CM1CON = 0; // Comparator off
    CM2CON = 0; // Comparator off
    ADCON0 = 0; // A/D conversion Disabled
}
   
void send(char message)
{
    while(TXIF==0);  // Wait till the transmitter register becomes empty
    TXREG = message;  
}

 

void string(char *p)
{
   while(*p != '\0') { 
     send(*p); 
     p++;
   }
}

 
void Uart_Initialized(void)
{
    //TXSTAx TRANSMIT STATUS AND CONTROL REGISTER
   
   TXSTAbits.CSRC = 0; //: Don?t care.
   TXSTAbits.TX9 = 0; //Selects 8-bit transmissionbit
   TXSTAbits.TXEN = 1; // Transmit Enable
   TXSTAbits.SYNC  = 0;   // Asynchronous mode
   TXSTAbits.SENDB = 0;
   TXSTAbits.BRGH =  1;   //  High speed mode
   TRMT = 1; //TSR is empty
   TXSTAbits.TX9D = 0;

 
   //RCSTAx: RECEIVE STATUS AND CONTROL REGISTER
   RCSTAbits.SPEN  = 1 ;   //Serial port enabled
   RCSTAbits.RX9 = 0; //Selects 8-bit reception
   RCSTAbits.SREN  = 1 ;//Don?t care.
   RCSTAbits.CREN  = 1 ;// Enables receiver
   RCSTAbits.ADDEN = 0; //
   RCSTAbits.FERR  = 0 ;// No framing error
   RCSTAbits.OERR  = 1 ;   // Overrun error
   RCSTAbits.RX9D   = 0 ;   //Selects 8-bit reception   
  
   SPBRG = 51;
  
}
 
void main(void)
{
   char message[]= {"Hello world"};
   Port_Initialized ();
   Uart_Initialized ();
   string(message);
  
    while (1)
    {
        
        
    }
}

 
 

 

 
ricHow exactly have you connected RC6 to your PC?
If you have a USB to RS232 adaptor, then you need an RS232 style inverting buffer on TX (like a MAX232 chip)
You can avoid the buffer if you are using a USB-TTL serial adaptor on your PC.

 
I have a USB to RS232 adaptor. I have attached screenshot for rs232 connection 
post edited by Djsarkar - 2020/10/10 01:19:18

Attached Image(s)

#14
ric
Super Member
  • Total Posts : 28713
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC18F45K80 - USART 2020/10/10 01:28:16 (permalink)
0
You have the connections between the PIC and the MAX232 swapped.
The PIC TX should connect to T1IN (pin 11)
and PIC RX should connect to R1OUT (pin 12)
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#15
Djsarkar
Senior Member
  • Total Posts : 175
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: PIC18F45K80 - USART 2020/10/10 02:05:50 (permalink)
0
ric
Also get rid of
   TXSTAbits.TRMT = 1; //TSR is empty
That is another read-only flag, so it's pointless to try to write to it.


TRMT = 1;
 error: use of undeclared identifier 'TRMT'
What should be write to remove error ?
#16
ric
Super Member
  • Total Posts : 28713
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC18F45K80 - USART 2020/10/10 02:07:04 (permalink)
0
Why are you trying to write to TRMT at all?
I already said it's read-only, and to get rid of that line.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#17
Djsarkar
Senior Member
  • Total Posts : 175
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: PIC18F45K80 - USART 2020/10/10 02:38:04 (permalink)
0
ric
You have the connections between the PIC and the MAX232 swapped.
The PIC TX should connect to T1IN (pin 11)
and PIC RX should connect to R1OUT (pin 12)
 



I am connecting port C pin R7 to RX pin on board https://www.nskelectronics.com/pic_development_board.html
When I run code, Nothing is showing on hyper terminal on PC
 
Hyper terminal settings 
 
baud rate 9600
Data bit 8
parity none
stop bit 1 
#18
vexorg
Senior Member
  • Total Posts : 176
  • Reward points : 0
  • Joined: 2019/09/27 10:59:40
  • Location: 0
  • Status: online
Re: PIC18F45K80 - USART 2020/10/10 02:41:58 (permalink)
0
Another silly question, do you actually have power on the vcc line of the max232?
 
Also is the uart 3.3v on that pic, appears to be from a quick glance at the datasheet, if so you need the max3232
post edited by vexorg - 2020/10/10 07:15:22
#19
Djsarkar
Senior Member
  • Total Posts : 175
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: PIC18F45K80 - USART 2020/10/10 03:24:31 (permalink)
+1 (1)
Now I am getting garbage  value on hyper terminal 
 
 #define _XTAL_FREQ 8000000
#include <xc.h>
// PIC18F45K80 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF       // Extended Instruction Set (Enabled)
// CONFIG1H
#pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)
// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)
// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)
// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)
// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)
// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)
// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)
// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)
// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)
 
void Port_Initialized (void)
{
    LATA = LATB = LATC = LATD = LATE =  0;
    TRISA = 0b0000000;// all are output, Unused
    TRISB = 0b0000000;// all are output, Unused
    TRISC = 0b1000000; // TX Pin set as output, RX Pin set as input
    TRISD = 0b0000000;//all are output, Unused
    TRISE = 0b0000000;// All are output, Unused
 
    ANCON0 = 0; // digital port
    ANCON1 = 0; // digital port
    CM1CON = 0; // Comparator off
    CM2CON = 0; // Comparator off
    ADCON0 = 0; // A/D conversion Disabled
}
   
void send(char message)
{
    while(TXIF==0);  // Wait till the transmitter register becomes empty
    TXREG = message;  
}
 
void string(char *p)
{
   while(*p != '\0') { 
     send(*p); 
     p++;
   }
}
 
void Uart_Initialized(void)
{
    //TXSTAx TRANSMIT STATUS AND CONTROL REGISTER
   
   TXSTAbits.CSRC = 0; //: Don?t care.
   TXSTAbits.TX9 = 0; //Selects 8-bit transmissionbit
   TXSTAbits.TXEN = 1; // Transmit Enable
   TXSTAbits.SYNC  = 0;   // Asynchronous mode
   TXSTAbits.SENDB = 0;
   TXSTAbits.BRGH =  1;   //  High speed mode
   TXSTAbits.TX9D = 0;
 
   //RCSTAx: RECEIVE STATUS AND CONTROL REGISTER
   RCSTAbits.SPEN  = 1 ;   //Serial port enabled
   RCSTAbits.RX9 = 0; //Selects 8-bit reception
   RCSTAbits.SREN  = 1 ;//Don?t care.
   RCSTAbits.CREN  = 1 ;// Enables receiver
   RCSTAbits.ADDEN = 0; //
   RCSTAbits.FERR  = 0 ;// No framing error
   RCSTAbits.OERR  = 1 ;   // Overrun error
   RCSTAbits.RX9D   = 0 ;   //Selects 8-bit reception   
  
   SPBRG = 51;
  
}
 
void main(void)
{
   char message[]= {"Hello world"};
   Port_Initialized ();
   Uart_Initialized ();
   string(message);
  
    while (1)
    {
        
        
    }
}

 
 

Attached Image(s)

#20
Page: 12345 > Showing page 1 of 5
Jump to:
© 2020 APG vNext Commercial Version 4.5