Skip to content

Instantly share code, notes, and snippets.

@mipsparc
Created April 13, 2018 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mipsparc/0ab54251c6525cf06def5de06db4c24a to your computer and use it in GitHub Desktop.
Save mipsparc/0ab54251c6525cf06def5de06db4c24a to your computer and use it in GitHub Desktop.
PIC16F1579向け UARTで受信するテスト。RCREGbits.RCREGって書き方、完全に思いつかなかった
// PIC16F1579 Configuration Bit Settings
// 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 = ON // Power-up Timer Enable (PWRT enabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#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 = OFF // 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 PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (PPSLOCKED Bit Can Be Cleared & Set Once)
#pragma config PLLEN = ON // PLL Enable (4x PLL enabled)
#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 LPBOREN = OFF // Low Power Brown-out Reset enable bit (LPBOR is disabled)
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
#define _XTAL_FREQ 16000000
#include <xc.h>
void writeSerial(unsigned char write_char)
{
while(!(PIR1bits.TXIF && TXSTAbits.TRMT));
TXREG = write_char;
}
void delayms(int ms)
{
for(int i = 0; i <= ms; i++) {
__delay_ms(1);
}
}
void main(void)
{
OSCCON = 0b01111011; // 16MHz
ANSELC = 0b00000000;
TRISC = 0b00111111; // RC0 to RC5 input
PORTC = 0b00000000;
RA0PPS = 0b1001; // RA0 UART TX / RB5 RX
TXSTA = 0b00100000; // 8bit UART
RCSTA = 0b10010000;
SPBRG = 25; // 16MHz 9600bps
INTCONbits.GIE = 1; // accept interrupt
PIE1bits.RCIE = 1; // accept RX interrupt
ANSELB = 0b00000000;
TRISBbits.TRISB5 = 1; //input for RX
PORTCbits.RC5 = 0; //init for RX
while(1) {
if (PIR1bits.RCIF) {
writeSerial(RCREGbits.RCREG);
}
while(!TXSTAbits.TRMT);
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment