Skip to content

Instantly share code, notes, and snippets.

@zeroxia
Created July 3, 2015 05:16
Show Gist options
  • Save zeroxia/2a1a293d60e031a957a6 to your computer and use it in GitHub Desktop.
Save zeroxia/2a1a293d60e031a957a6 to your computer and use it in GitHub Desktop.
Using GPIO pin to emulate "TX" function of UART communication (TTL levels)
#include <xc.h>
/* Microcontroller MIPs (FCY) */
#define SYS_FREQ 4000000L
#define _XTAL_FREQ SYS_FREQ
/* #define FCY SYS_FREQ/4 */
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = ON // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
#include <stdint.h>
#include <stdbool.h>
static void ConfigureOscillator(void)
{
OSCCON = 0xFF;
while (! HFIOFR)
continue;
while (! HFIOFS)
continue;
return;
}
static void hsp_delay_ms(unsigned int n)
{
while (n > 0) {
__delay_ms(1);
--n;
}
}
static void hsp_output_n_bit(int num, int n_bits)
{
int i, bb;
#define HSP_PIN_OUTPUT RC2
// Start
HSP_PIN_OUTPUT = 1;
hsp_delay_ms(50);
HSP_PIN_OUTPUT = 0;
hsp_delay_ms(50);
HSP_PIN_OUTPUT = 1;
hsp_delay_ms(200);
HSP_PIN_OUTPUT = 0;
hsp_delay_ms(200);
HSP_PIN_OUTPUT = 1;
hsp_delay_ms(250);
HSP_PIN_OUTPUT = 0;
hsp_delay_ms(250);
for (i = n_bits - 1; i >= 0; --i) {
bb = (num & (1 << i)) != 0 ? 1 : 0;
HSP_PIN_OUTPUT = bb;
hsp_delay_ms(500);
}
// Stop
HSP_PIN_OUTPUT = 0;
hsp_delay_ms(250);
HSP_PIN_OUTPUT = 1;
hsp_delay_ms(250);
HSP_PIN_OUTPUT = 0;
hsp_delay_ms(200);
HSP_PIN_OUTPUT = 1;
hsp_delay_ms(200);
HSP_PIN_OUTPUT = 0;
hsp_delay_ms(50);
HSP_PIN_OUTPUT = 1;
hsp_delay_ms(50);
HSP_PIN_OUTPUT = 0;
}
static unsigned int hsp_get_adc_value(void)
{
unsigned int v;
ADCON0bits.GO = 1;
__delay_ms(2);
while (ADCON0bits.GO)
continue;
v = ADRESH & 0x3;
v <<= 8;
return (v | ADRESL);
}
void main(void)
{
/* Configure the oscillator for the device */
ConfigureOscillator();
ANSELC = 0xFF;
TRISC = 0xFF;
// DEBUG PIN
TRISCbits.TRISC2 = 0;
ANSELCbits.ANSC2 = 0;
/* ADC */
ADCON0 = 0x1C;
ADCON1 = 0xE0;
ADCON2 = 0;
// Turn on ADC
ADCON0bits.ADON = 1;
/*
while (1) {
RC2 = 1;
hsp_delay_ms(250);
RC2 = 0;
hsp_delay_ms(750);
}
*/
while (1) {
/* TODO <INSERT USER APPLICATION CODE HERE> */
unsigned int val = 0xFFFF;
val = hsp_get_adc_value();
hsp_delay_ms(500);
hsp_output_n_bit(val, 10);
hsp_delay_ms(500);
}
}
#define HSP_UART_BAUDRATE 1200
#define HSP_UART_1BITDELAY (1000000L / HSP_UART_BAUDRATE)
#define HSP_UART_DATABITS 8
#define HSP_UART_TX RC1
#define HSP_UART_TX_DIR TRISbits.RC1
#define HSP_UART_TX_ANSEL ANSELbits.RC1
void hsp_uart_init(void)
{
HSP_UART_TXD_ANSEL = 0; // Set as digital IO pin
HSP_UART_TXD_DIR = 0; // Set as output
HSP_UART_TXD = 1; // UART idle state: HIGH
}
void hsp_uart_tx_byte(unsigned char c)
{
unsigned char i;
// Start bit
HSP_UART_TX = 0;
__delay_us(HSP_UART_1BITDELAY);
for (i = 0; i < HSP_UART_DATABITS; ++i) {
if ( (c & 0x1) == 0x1 ) {
HSP_UART_TX = 1;
} else {
HSP_UART_TX = 0;
}
c >>= 1;
__delay_us(HSP_UART_1BITDELAY);
}
// Stop bit
HSP_UART_TX = 1;
__delay_us(HSP_UART_1BITDELAY);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment