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.

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
 
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.
 
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.
 
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...
 
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:
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?
 
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..
 
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

  • uartdata.c
    56 bytes · Views: 216
  • sw_uart.c
    4.6 KB · Views: 273
  • openuart.c
    1.3 KB · Views: 242
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;
  }
}
 
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.
 
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...
 
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

New Articles From Microcontroller Tips

Back
Top