首页 > 代码库 > PIC18F4520 UART模板

PIC18F4520 UART模板

如题

MPLAB V8.53   PIC18F4520 8bit MCU

#define USE_AND_MASKS

#include <stdio.h> 
#include <stdlib.h> 
#include <delays.h> 
#include <usart.h> 

#pragma config OSC = INTIO67 //internal oscillator 
#pragma config WDT = OFF //watchdog timer off 
#pragma config LVP = OFF //... 
#pragma config PBADEN = OFF //Analog functions on PORTB OFF 


void initUART(void); 
void setClock(void); 

unsigned char msg[] = "Hello"; 

/* 
 * main function 
 */ 
void main() { 
    setClock(); 
    initUART(); 

    while(1) //infinite loop 
    { 
       putsUSART(msg); 
       Delay10KTCYx(125); 
    } 
} 
/** 
 * Initialise registers and pins for uart 
 */ 
void initUART() { 
    int baud = 51; // set baud rate to 9600 
	unsigned char UART1Config = USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_BRGH_HIGH;
    //TRISCbits.RC6 = 0; //TX pin set as output 
    //TRISCbits.RC7 = 1; //RX pin set as input 
	TRISC = 0x00 + (1 << 7);
    
    OpenUSART(UART1Config,baud); 
} 
/* 
 * Setup the clock 
 */ 
void setClock() { 
    OSCCONbits.IRCF0 = 1; //internal oscillator to 8MHz 
    OSCCONbits.IRCF1 = 1; 
    OSCCONbits.IRCF2 = 1; 
}