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.

Hardware serial sample XC8

Status
Not open for further replies.

be80be

Well-Known Member
I been looking for a sample of hardware serial just to get a idea of how to in xc8
Thanks to anyone who wants to share there code.
 
Are you looking for just simple unbuffered reads/write and initialization or a proper buffered library?

I don't have anything right now, but I actually need to make one up for the 12f1840 light string project I'm doing. I can share it. I'll make up something in the next day or two.
 
That be great thanks I was looking for the old high tech librarys I though xc8 had them so my old code don't work but I think I need to look at how you guys write your own for xc8 so that be great
 
Okay, I'll just port over an interrupt driven buffered library from another microcontroller. Looking over the EUSART module it looks pretty straight forward, though the transmit interrupt is a 'transmit buffer empty' interrupt rather than than a 'transmit complete interrupt'. The datasheet states you need to turn it on and off when needed, which adds a bit of complexity.

I'm sure other people have routines, so someone might beat me to it.
 
I'll probably complete this for tomorrow morning. I noticed that the MPLAB Code Configurator actually produces a interrupt driven library that is very similar to mine and very usable. I've already stolen it's intialisation routine
 
I thought about using that but the chip I'm using is not listed but it still be good to learn with
 
I'm using an interrupt driven ( I found polling RX flag did not work ) FIFO for UART RX with PIC24EP and XC16 , don't remember where it came from or how it works o_O seems to , will post, if of interest. ( needs a clean up )
 
More examples would be better. I made mine up. I just need to test it. I'll connect it to a serial bridge and just type some things back and forth sometime today.

I've been distracted by trying to get the sleep current of my LCD remote down.
 
Ok this FIFO is working with PIC24EP X16 v125 MPLABX 3.26 . and UART 2
Code:
int SBR,SBW,SBC; //Serial buffer pointers

//Read , Write ,Current

int SerialBuf_size = 20 ;

char RX_FIFO[21] ; // U2RX character FIFO buffer

int HC12_input=0;


char pro_buffer() // called if HC12_input

{

char serial_in;

serial_in = RX_FIFO[SBR];

SBC++;

SBR++;

SBR %= SerialBuf_size;

SBC %= SerialBuf_size;

HC12_input = 0;

return serial_in ;

}

void __attribute__((__interrupt__, auto_psv))_U2RXInterrupt(void)

{

while(U2STAbits.URXDA) // data available

{

RX_FIFO[SBW] = U2RXREG ;

if ((SBW+1)% SerialBuf_size != SBR)

{

SBW++;

SBW %= SerialBuf_size;

}

}

IFS1bits.U2RXIF = 0;

HC12_input = 1; // flag SERIAL DATA IN

}

looking at it now I am not sure usefulness of SBC ( current pointer ) .
 
Tried testing with my 12f1840 RGB boards, but I realize I am using the same pins for rx/tx as the debug pins. Something is odd with that and not sure how to get it working.

Moved it over to a 16F1933 test board I have. No code changes were needed. I re-generated the config bits, but that was it. Tested it out and it worked well enough. I have gets (get string) and puts (put string) functions implemented. It looks like it is triggering on both the carriage return and line feed sent by my editor so I'm getting an extra blank line entered. You might want to edit it to ignore line feed characters. A fix would be easy. gets also does not send the characters back, so if it's used for data entry, you will want to update it to transmit the characters back so the user can see what they are typing.

This code works with the "Enhanced Universal Synchronous Asynchronous Receiver Transmitter" (EUSART) module in the mid range 8 bits. It won't work with anything else. (I assume, haven't done a whole lot with PICs)
The Initialize is not a smart initialize. The baud rate registered are just set with a hard coded value. I'm sure there is sample initialize code out there that will generate the baud rate registers correctly. So this is hardcoded 9600bps 8-N-1 sample.
I included a main sample test to show how it works and the lib files eusart_lib.c and eusart_lib.h. It uses a ring buffer to store the data received and the data to be transmitted. You can change the buffer size in the header define.
Tested on a 16F1933. I just made this up based on my own routines from other uC's and stealing an idea from the MCC code (I liked the buffer count tracking variables), so this is not well tested code. If there are any issues or updates, post them and I will implement them, or just post new code.

**broken link removed**

**broken link removed**
 

Attachments

  • 16f1933_eusart_test.zip
    3.1 KB · Views: 185
If you need more pins the pic16f1825 is 100% code compatible with the pic12f1840... It's just got a port c..

I needed just one more output pin on my pic12f1840 so I moved to the pic16f1825 and all that is different is the port registers.. ( As it happens I didn't need to move as I ended up multiplexing the pin anyway!!)...
 
I should not need more pins. I should still be able to use these two pins for RX and TX. I just need to figure out how. Maybe I'm missing something or I just plugged in the RX/TX backwards, I haven't looked at it too hard.

I just find it bizarre; all the uC's I've used you would use a register to specifically assign an alternate function to pins. In these PIC's you basically just use whatever function you want and the modules have an assigned priority. In another uC, I would wait 3 seconds or so, unassign the debug functionality to the pins, assign the UART functionality and move on.
 
I'm going to have to oder some of the newer chips I'm playing with a 16f876a I found in the dumpster at work it came from a heater pid controller so I figured I mess around with it and xc8 blink worked good Adc is what I'm going to send just something to do for a crazy old man lol
 
I'm going to have to oder some of the newer chips I'm playing with a 16f876a I found in the dumpster at work it came from a heater pid controller so I figured I mess around with it and xc8 blink worked good Adc is what I'm going to send just something to do for a crazy old man lol

Liken the others have mentioned, the new enhanced 14 bit range are really good to use, I'm using the 16F1827 as my PIC of choice these days. And as Ian mentioned, it's trivial to port code to the tiny 8 pin 12F1840 - as long as you don't need many pins - I did exactly that for a cheap and simple data logger, using a 12F1840 and an FTDI USB/Serial converter.
 
Need more pins ? stick on an MCP23008 or even 23017 (16 IO) , good with key pads , LCD , switches. leds.....
 
Status
Not open for further replies.

Latest threads

Back
Top