日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 單片機 > 單片機
[導(dǎo)讀]實現(xiàn)串口輸出,以及將接收到的信息發(fā)送回。利用PC機串口測試,正常。kit3、MPLAB X IDE Version 1.85;target PIC16F877A最小系統(tǒng)。#include #define uchar unsigned char#define uint unsigned int// CONFIG#pragma

實現(xiàn)串口輸出,以及將接收到的信息發(fā)送回。

利用PC機串口測試,正常。

kit3、MPLAB X IDE Version 1.85;target PIC16F877A最小系統(tǒng)。

#include

#define uchar unsigned char

#define uint unsigned int

// CONFIG

#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)

#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)

#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)

#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)

#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)

#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)

#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)

#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

//main.c

void uart_init(void);

void uart_send_byte(char uart_data);

char uart_receive_byte(void);

void delay(void);

/***********************************************/

char uart_current_data;

char uart_last_data;

char evm_info[]="Welcom to use PIC16F877-EVM-BOARD-V1.0";

/*********************************************************************/

void main(void)

{

unsigned char i,j;

TRISB = 0x00+(0x01<<5); //初始化端口B,RB5為輸入口,其余為輸出口

TRISC = 0x80; // bit7 rx input bit6 tx output初始化端口C的方向控制寄存器(

//對應(yīng)的比特為0是輸出口,比特為1輸入口)

TRISD = 0x00; //初始化D口

TRISE = 0x00; //初始化E口

ADCON1= 0x06; //端口都為數(shù)字口

TRISA = 0x00; //設(shè)置A口均為輸出口

OPTION_REG &= (~(0x01<<7)); //

PORTA = 0xff; //初始化端口寄存器

PORTB = 0xff&(~(0x01<<5));

PORTC = 0xff;

PORTD = 0xff;

PORTE = 0xff;

uart_init(); //初始化PIC16F877串行通訊接口

for(i=0;i<38;i++)

{

uart_send_byte(evm_info[i]);

}

uart_send_byte(0x0d); //將回車鍵第一字符給PC

uart_send_byte(0x0a); //將回車鍵第二字符給PC

for(;;) //程序主循環(huán)

{

uart_current_data=uart_receive_byte(); //接收一個字符,沒有收到字符,返回0x00

if( uart_current_data!=0x00 && uart_current_data!=0x0d &&

uart_current_data!=0x0a)

{

uart_send_byte(uart_current_data); //將字符發(fā)回PC

uart_last_data=uart_current_data; //暫存當(dāng)前的字符

}

else if(uart_current_data==0x0d) //回車鍵字符判斷?

{

uart_last_data=uart_current_data; //暫存回車鍵字符第一字符

}

else if(uart_current_data==0x0a && uart_last_data==0x0d)//收到了正

//確的回車鍵?

{

uart_send_byte(0x0d); //將回車鍵第一字符給PC

uart_send_byte(0x0a); //將回車鍵第二字符給PC

uart_last_data=0x00;

}

}

}

/************************************************/

void delay(void)

{

unsigned int i;

for(i=0;i<500;i++);

}

void interrupt isr_service(void) //中斷服務(wù)程序

{

if(T0IE && T0IF)

{

T0IF = 0; //在此加入TMR0中斷服務(wù)

}

if (TMR1IE && TMR1IF) //判TMR1中斷

{

TMR1IF=0; //在此加入TMR1中斷服務(wù)

}

} //中斷結(jié)束并返回

/************************************************/

void uart_init(void)

{

SPBRG=0x4d; //設(shè)置通訊波特率為9600/秒

SPEN=1; //1 = Serial port enabled (configures RC7/RX/DT and

// RC6/TX/CK pins as serial port pins)

RX9=0; //0 = Selects 8-bit reception

SYNC=0; //Asynchronous mode

BRGH=1; //1 = High speed

TX9=0; //0 = Selects 8-bit transmission

TXEN=1; //1 = Transmit enabled

CREN=1;

}

/***********************************************/

void uart_send_byte(char uart_data)

{

TXREG=uart_data;

while(1)

{ //等待發(fā)送完畢

if(TRMT==1) break ; // 使用if(TXIF == 1) break 時,會導(dǎo)致發(fā)送數(shù)據(jù)的丟失

}

}

/***********************************************/

char uart_receive_byte(void)

{

char result=0;

if(RCIF==1) //判是否收到了數(shù)據(jù)?

{

result=RCREG; //讀取接收到的數(shù)據(jù)

}

return(result);

}

/***********************************************/

本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉