//USART(非同期)評価用ソースプログラム(受信側) #include #pragma config PLLDIV = 5 // (20 MHz crystal on PICDEM FS USB board) #pragma config CPUDIV = OSC1_PLL2 #pragma config USBDIV = 2 // Clock source from 96MHz PLL/2 #pragma config FOSC = HSPLL_HS #pragma config FCMEN = OFF #pragma config IESO = OFF #pragma config PWRT = OFF #pragma config BOR = ON #pragma config BORV = 3 #pragma config VREGEN = ON //USB Voltage Regulator #pragma config WDT = OFF #pragma config WDTPS = 32768 #pragma config MCLRE = ON #pragma config LPT1OSC = OFF #pragma config PBADEN = OFF //#pragma config CCP2MX = ON #pragma config STVREN = ON #pragma config LVP = OFF //#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming #pragma config XINST = OFF // Extended Instruction Set #pragma config CP0 = OFF #pragma config CP1 = OFF //#pragma config CP2 = OFF //#pragma config CP3 = OFF #pragma config CPB = OFF //#pragma config CPD = OFF #pragma config WRT0 = OFF #pragma config WRT1 = OFF //#pragma config WRT2 = OFF //#pragma config WRT3 = OFF #pragma config WRTB = OFF // Boot Block Write Protection #pragma config WRTC = OFF //#pragma config WRTD = OFF #pragma config EBTR0 = OFF #pragma config EBTR1 = OFF //#pragma config EBTR2 = OFF //#pragma config EBTR3 = OFF #pragma config EBTRB = OFF #define _XTAL_FREQ 48000000 //__delay_ms #define SW1 PORTEbits.RE0 #define SW2 PORTEbits.RE1 #define LED PORTEbits.RE2 void init(void); char UART_Init(const long int baudrate); void UART_Write(char data); char UART_TX_Empty(); void UART_Write_Text(char *text); char UART_Data_Ready(); char UART_Read(); void UART_Read_Text(char *Output, unsigned int length); int n; void init(void) { ADCON1 = 0b00001111; TRISA = 0b00000000; TRISB = 0b00000000; TRISC = 0b00110000; //D-,D+ TRISD = 0b00000000; TRISE = 0b00000011; //SW1,2=INPUT LED=OUTPUT LATA = 0b00000000; LATB = 0b00000000; LATC = 0b00000000; LATD = 0b00000000; LATE = 0b00000000; } void main(void) { init();//初期設定 UART_Init(9600);//UART初期設定、ボーレイト設定 //割り込み使用時は以下を有効にします。 //RCIF = 0; //reset RX pin flag //RCIP = 1; //high priority //RCIE = 1; //Enable RX interrupt //PEIE = 1; //Enable pheripheral interrupt (serial port is a pheripheral) //INTCONbits.GIE = 1;//Global Interrupt Enable bit while(1) { if(UART_Data_Ready()) { char c = UART_Read(); if(c=='j'){LED=1;} else{LED=0;} __delay_ms(10); } } } char UART_Init(const long int baudrate) { unsigned int x; BRGH = 1; //Setting High Baud Rate x = (_XTAL_FREQ - baudrate*16)/(baudrate*16); //SPBRG for High Baud Rate if(x>255) //If High Baud Rage Required { BRGH = 0; x = (_XTAL_FREQ - baudrate*64)/(baudrate*64); //SPBRG for Low Baud Rate } if(x<256) { SPBRG = x; //Writing SPBRG Register SYNC = 0; //Setting Asynchronous Mode, ie UART SPEN = 1; //Enables Serial Port TRISC7 = 1; //As Prescribed in Datasheet TRISC6 = 1; //As Prescribed in Datasheet CREN = 1; //Enables Continuous Reception TXEN = 1; //Enables Transmission return 1; //Returns 1 to indicate Successful Completion } return 0; //Returns 0 to indicate UART initialization failed } void UART_Write(char data) { while(!TRMT); TXREG = data; } char UART_TX_Empty() { return TRMT; } void UART_Write_Text(char *text) { int i; for(i=0;text[i]!='\0';i++) UART_Write(text[i]); } char UART_Data_Ready() { return RCIF; } char UART_Read() { while(!RCIF); return RCREG; } void UART_Read_Text(char *Output, unsigned int length) { unsigned int i; for(int i=0;i