• 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.

software uart

Status
Not open for further replies.

sathya s

New Member
hii... I have to implement a software UART in PIC24HJ128GP310A to transfer data through its I/O pins,..any one please give me the related code either in C or in Assembly language
 

NorthGuy

Well-Known Member
Software UART can only be done by bit-banging, in which case it ties up the processor for the length of exchange (which could be a while if you wait for reception), or through interrupts, in which case only slow baud rates are achievable.

That's why bigger PICs, which are designed to be used for other things except bit-banging UART 24/7, have hardware UART modules with in and out buffers, which can also be handled by DMA.
 

ronsimpson

Well-Known Member
Most Helpful Member
I have to implement a software UART in PIC24HJ128GP310A ......any one please give me the related code either in C or in Assembly language
Home work?
PIC and others have assembly code examples all over the internet. Probably not PIC24 but for little PICs. Search the internet and find examples so you can learn how it works.
 

djsfantasi

Member
Let's see. A search for your topic yields thousands of hits. I'm assuming one has code. Or perhaps you could add the word code to your search terms. And get thousands of hits.

The question is, will you be able to understand the code well enough to use it?

What is it that you really need? I suspect it is more than code even though that's what you think now...
 

sathya s

New Member
Let's see. A search for your topic yields thousands of hits. I'm assuming one has code. Or perhaps you could add the word code to your search terms. And get thousands of hits.

The question is, will you be able to understand the code well enough to use it?

What is it that you really need? I suspect it is more than code even though that's what you think now...
I'll explain what I actually need...I have to transfer a data from PIC24H to a external device through UART. The UART in the PIC is already used. So the option available is, by using the I/O port I have to transfer the file. for that it is necessary to implement a software UART. can you help me to do this?
 
Last edited:

NorthGuy

Well-Known Member
I'll explain what I actually need...I have to transfer a data from PIC24H to a external device through UART. The UART in the PIC is already used. So the option available is, by using the I/O port I have to transfer the file. for that it is necessary to implement a software UART. can you help me to do this?
So it's transmit-only, right? And you do realize that there are 2 UART modules on your PIC and both are already used, correct?

What baud rate do you need? At what speed your PIC is running?
 

sathya s

New Member
So it's transmit-only, right? And you do realize that there are 2 UART modules on your PIC and both are already used, correct?

What baud rate do you need? At what speed your PIC is running?
not transmit only.. it has to receive acknowledgement signal back.. baud rate is 9600bps..
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Software uart can work here, but you'll have to sit and wait for the acknowledge.... Software uarts can have interrupts but the take up different I/O resources..

These are for the Pic18 but just need adjusting to suit Pic24... They are from the Plib library bundled with XC8
 

Attachments

NorthGuy

Well-Known Member
not transmit only.. it has to receive acknowledgement signal back.. baud rate is 9600bps..
Create a timer interrupt which happens once every 104us. Make it relatively high priority.

During startup, intitalize the output pin for digital output and drive it high

From inside the interrupt call this:

C:
void SendUART() {
  static int state = 0;
  static char mask = 0;
  static char data = 0;

  switch (state) {
    case 0: if (has_byte_to_send()) { // replace the call according to your needs; must be quick
        OUTPUT = 0; // start bit
        data = get_byte_to_send(); // replace the call according to your needs
        state = 1;
        mask = 1;
      }
      break;
    case 1: OUTPUT = (data & mask) != 0; // data bit
      mask <<= 1;
      if (mask == 0) {
        state = 2;
      }
      break;
    case 2: OUTPUT = 1; // stop bit
      state = 0;
      break;
  }
}
 

sathya s

New Member
Software uart can work here, but you'll have to sit and wait for the acknowledge.... Software uarts can have interrupts but the take up different I/O resources..

These are for the Pic18 but just need adjusting to suit Pic24... They are from the Plib library bundled with XC8
can you please give me the code of "sw_uart.h" defined in the code you given.
 

sathya s

New Member
Create a timer interrupt which happens once every 104us. Make it relatively high priority.

During startup, intitalize the output pin for digital output and drive it high

From inside the interrupt call this:

C:
void SendUART() {
  static int state = 0;
  static char mask = 0;
  static char data = 0;

  switch (state) {
    case 0: if (has_byte_to_send()) { // replace the call according to your needs; must be quick
        OUTPUT = 0; // start bit
        data = get_byte_to_send(); // replace the call according to your needs
        state = 1;
        mask = 1;
      }
      break;
    case 1: OUTPUT = (data & mask) != 0; // data bit
      mask <<= 1;
      if (mask == 0) {
        state = 2;
      }
      break;
    case 2: OUTPUT = 1; // stop bit
      state = 0;
      break;
  }
}
thank you...
 

Jon Wilder

Active Member
Not sure what you mean by UARTs are already used. UARTs are designed to drive a serial bus. You can have multiple devices on this bus and implement some hardware or software addressing with some clever design work.

Can you post a schematic of your circuit so we can see what you're doing?
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top