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.

PIC18F and USART

Status
Not open for further replies.
This will not work.... I found this out myself a while ago.... I will still drop characters.

Use this..
C:
while(!TXSTAbits.TRMT );// Wait until empty
TXREG = RCREG;

I did it as you suggested:

Code:
while(1)
{
  if(PIR1bits.RCIF == 1) // if the USART receive interrupt flag has been set TO ZERO
  {
        while(!TXSTAbits.TRMT );// Wait until TXREG is empty
        {
            TXREG = RCREG; // echo received data back to sender
        }
  }
}

.. but it is still not working.
 
I did it as you suggested:

Code:
while(1)
{
  if(PIR1bits.RCIF == 1) // if the USART receive interrupt flag has been set TO ZERO
  {
        while(!TXSTAbits.TRMT );// Wait until TXREG is empty
        {
            TXREG = RCREG; // echo received data back to sender
        }
  }
}

.. but it is still not working (echoing the characters mentions before only).
 
You can't say that "these characters work", but "these characters do not work". The communication either works or does not work.
 
When using Hyperterminal, only the characters e, t, d, f, w, m, o, u, g, v are being echoed correctly. The rest are echoing other symbols.

What kind of incorrect symbols? Can you give few samples (sent - received). Maybe there is a bit-pattern that reveals the problem.
 
What kind of incorrect symbols? Can you give few samples (sent - received). Maybe there is a bit-pattern that reveals the problem.
Hi misterT.

attached is a screenshot showing the echoed characters/symbols. a,b,c... are the characters i types followed by the echo.

Also,

I took the f character (since it is being echoed well) and did the following modification to the code so that when f is received, the PIC will turn on two LEDs. But this is also not working. Why is that if the character f is echoed well?

Code:
    SPBRG = 25;            // "25" calculated using formula from table 20-1 in the PIC18F4550 datasheet
    RCSTAbits.SPEN = 1;    // enable serial port (configures RX/DT and TX/CK pins as serial port pins)
    TXSTAbits.TXEN = 1;    // transmit enabled
    RCSTAbits.CREN = 1;    // enable receiver

    LATBbits.LATB0 = 0;  // RB-0 to High  //turn LEDs of on start up
    LATBbits.LATB1 = 0;  // RB-1 to High

while(1)
{
  if(PIR1bits.RCIF == 1) // if the USART receive interrupt flag has been set TO ZERO
  {
        while(!TXSTAbits.TRMT );// Wait until TXREG is empty
        {
            TXREG = RCREG; // echo received data back to sender

if(a == 'f')
{
  LATBbits.LATB0 = 1;  // RB-0 to High 
  LATBbits.LATB1 = 1;  // RB-1 to High
}
        }
  }
}

NOTE: a was declared before the main function ( char a; )
 

Attachments

  • Untitled.png
    Untitled.png
    13.9 KB · Views: 196
Why is that if the character f is echoed well?

This is just a guess.. The bit pattern of character f is '01100110'. If your baudrate is slightly off, that bit-pattern will transfer correctly, but maybe some other bit-patterns (01010101) do not transfer correctly.

The screenshot you posted does not help, because it only shows what is received. It does not show what is sent.
 
The screenshot you posted does not help, because it only shows what is received. It does not show what is sent.
Yes!! But I think we can assume the baud rate is out anyway... but not by much!!

SPBRGH = '129'; //calculated

What's this by the way..... '129' cannot be correct... a number in apostrophe's will be cast to ascii..
 
What's this by the way..... '129' cannot be correct... a number in apostrophe's will be cast to ascii..

Well spotted.. I stared at the code for a long time hoping to find a mistake. Did not spot that one. That is one mistake that I have never seen before. I consider that a "valuable mistake". I learned from it.
 
Thanks for your feedback. The '129' was fixed in the last code that I uploaded.

I found a mistake in the MAX232 circuit. the capacitor at pin 2 was connected to ground instead to Vcc. I tried the code again, but still not working. Can the IC be damaged?

Also, when setting the baud rate, it it placed into the SPBRG register or into another register?
 
In your code that checks for 'f', you probably want to replace this:

Code:
TXREG = RCREG; // echo received data back to sender
if(a == 'f')

with something like this:

Code:
a = RCREG;
TXREG = a; // echo received data back to sender
if(a == 'f')

I would also move reading outside of the waiting cycle, but this probably doesn't matter.

It would be helpful to check for bad characters and see if you receive them right. This will give you a clue on whether the problem is on the sending or receiving end.
 
when i had a problem like that its was start/stop bit related, either in my code or in terminal settings, or in the time delay(as Ian mentioned) , when i was debugging my own code, i found it helps to send characters 01010101, 11001100, and 11110000, then you can see where the binary is getting lost.
 
Its a framing error.... You have set SPBRGH to 129.... You need to set SPBRG to 129 and SPBRGH to 0....

Its working form me now

C:
// INCLUDE PIC18F4550 HEADERS (LIBRARIES) -------------------------------------------------------------------------------------------------------------------------------------------------------------

#include<p18f4550.h>
#include<delays.h>  //Library for delays IMPORTANT TO INCLUDE THIS LIBRARY FOR DELAYS ()()()()()()()()()()()()()()()()()()()()()()()()()()                        

// COMPILER DIRECTIVES FOR CHIP CONFIGURATION BITS ---------------------------------------------------------------------------------------------------------------------------------------------------------------------

#pragma config FOSC  = HSPLL_HS  // Since a 20MHz Xtal is used, set FOSC to HSPLL_HS (HS stands for high speed)
#pragma config PLLDIV = 5          // Divide the 20MHz by 5 to provide 4 MHz input to the 96 MHz PLL
#pragma config CPUDIV = OSC1_PLL2 // Divide 96 MHz PLL output by 2 to get 48 MHz system clock
#pragma config USBDIV = 2          // USB clock coming from 96 MHz PLL output is divided by 2
#pragma config FCMEN  = OFF      // Fail-Safe Clock Monitor disabled (this is used to stop operation when frequency gets out of tollerance--usually used for medical applications)
#pragma config IESO  = OFF      // Oscillator Switchover mode disabled
#pragma config PWRT  = OFF      // Enable Power-up timer (This causes the PIC to delay until full operating voltage is reached before the program starts)
#pragma config BOR    = ON        // Enable Brown-out reset (This automatically put the PIC in reset if the power coming into the chip isn't sufficient to work reliably, hence preventing malfunctions)
#pragma config VREGEN = ON        // Use internal USB voltage regulator from 5V to 3.3V
#pragma config WDT    = OFF      // Disable Watchdog timer (This is used so when the system see that it may not be working as expected, it automatically resets the PIC)
#pragma config MCLRE  = ON        // Enable MCLR Enable (MCLR is pin 1 of the PIC. When ground is applied it resets the PIC)
#pragma config LVP    = OFF      // Disable low voltage ICSP (This is another way to program the PIC. Since not used - Disable)
#pragma config ICPRT  = OFF      // Disable dedicated programming port (44-pin devices) (This is a Dedicated In-Circuit Debug/Programming Port)
#pragma config CP0    = OFF      // Disable code protection (Used to protect the program from being coppied)


// INITIALIZE FUNCTIONS (Prototyping the functions) ---------------------------------------------------------------------------------------------------------------------------------------------------------------------


// MAIN FUNCTION -----------------------------------------------------------------------------------------------------------------------------------------------------------------------


void Configurate_USART (void)
{

  //LEDs

  TRISB = 0xF0 ; // PORT B Setting: Set all the pins in port B to Output.

  // USART configuration

  TXSTAbits.TX9  = 0;    // 8-bit transmission
  TXSTAbits.TXEN = 1;    // Transmit enabled
  TXSTAbits.SYNC = 0;    // Asynchronous mode
  TXSTAbits.BRGH = 1;    // High speed

  RCSTAbits.SPEN = 1;    // Enable serial port - configures RX/DT and TX/CK pins as serial port pins
  RCSTAbits.RX9  = 0;    // 8-bit reception
  RCSTAbits.CREN = 1;    // Enable receiver

  BAUDCONbits.BRG16 = 0; // 8-bit baud rate generator

  SPBRG = 129;          //calculated
  SPBRGH = 0;   
  TRISCbits.RC6 = 1;    // make the TX pin a digital output
  TRISCbits.RC7 = 1;    // make the RX pin a digital input

}

void main(void)
{
    char Temp_Instruction;
    Configurate_USART();

while(1)
{

  if(PIR1bits.RCIF == 1) // if the USART receive interrupt flag has been set
    { 
         Temp_Instruction = RCREG;
        /*
        if(Temp_Instruction == 'a')
        {
            LATBbits.LATB0 = 1;  // RB-0 to High 
            LATBbits.LATB1 = 1;  // RB-1 to High 

            RCREG = 0; //Empty register 
        }

        if(Temp_Instruction == 'b')
        {
            LATBbits.LATB0 = 0;  // RB-0 to Low 
            LATBbits.LATB1 = 0;  // RB-1 to Low

            RCREG = 0; //Empty register 
        }*/
        while(!TXSTAbits.TRMT);
            TXREG =Temp_Instruction; // echo received data back to sender
        }
    }
 
This is the fourth day trying to make this work
Adrianvon, don't get discouraged. Within 24hrs of posting it here you are almost there. I wish I could have anything even close to that at work.
 
According to the tables in the datasheet I have to hand for the 18F4550 (page 249), for 9600 baud and Fosc=20Mhz, Sync=0, BRG16=0, BRGH=1, SPBRG=129 :)
 
Status
Not open for further replies.

Latest threads

Back
Top