paulpthcom 19 Posted May 30, 2013 Share Posted May 30, 2013 So I'll start up by admitting this is horrible code, you probably shouldn't use it and it's just generally a bad idea, but someone might find it useful. I'm targeting a 2452 without hardware UART and wanted to be able to printf solely for debugging purposes. I know that there's at least one implementation that uses Timer A and T0.1 to implement a soft UART. That version is much more bullet proof and if you actually need to get every last byte you should use that, or better yet a chip with a hardware UART. That being said I only need this for debugging and can miss a character once in a while in exchange for being able to use any pin and having simpler interrupt logic. It's just a simple pure bit bang implementation but I wasn't able to find anything like it out there. Seems to work rather well at 57600 with only a junk character once in a great while. Anyways enjoy or laugh at, you won't hurt my feelings either way. //#define BIT_DELAY __delay_cycles((1000000/9600) - 12) // 9600 BAUD #define BIT_DELAY ; // 57600 BAUD unsigned char *g_port_out; unsigned char g_port_bit; void uart_init(unsigned char *port_out, unsigned char *port_dir, unsigned char port_bit) { g_port_out = port_out; g_port_bit = port_bit; *port_dir |= port_bit; *g_port_out |= port_bit; BIT_DELAY; // Clear screen printf("\033[2J\033[0;0f"); printf("\033[%d;%df", 0, 0); } int putchar(int c) { unsigned int tx_byte = (c | 0x100) << 1; // Add HIGH stop bit and low start bit while (tx_byte) { if (tx_byte & 0x1) { *g_port_out |= g_port_bit; } else { *g_port_out &= ~g_port_bit; } tx_byte = tx_byte >> 1; BIT_DELAY; } *g_port_out |= g_port_bit; if (c == '\n') putchar('\r'); return 1; } spirilis 1 Quote Link to post Share on other sites
Rei Vilo 695 Posted May 30, 2013 Share Posted May 30, 2013 I've developed a Very Basic Software Serial TX Only Library By very basic, I mean TX only, one single speed 9600 and one single configuration is 8N1. The library uses the Energia framework but shouldn't be difficult to adapt to another environment. Quote Link to post Share on other sites
lalalandrus 16 Posted June 26, 2013 Share Posted June 26, 2013 the timer way is probably the way to go, but if you are short on timers, you can always use the WDT in interval mode... Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.