当前位置:首页 > 单片机 > 单片机
[导读]实现串口输出,以及将接收到的信息发送回。利用PC机串口测试,正常。kit3、MPLAB X IDE Version 1.85;target PIC16F877A最小系统。#include #define uchar unsigned char#define uint unsigned int// CONFIG#pragma

实现串口输出,以及将接收到的信息发送回。

利用PC机串口测试,正常。

kit3、MPLAB X IDE Version 1.85;target PIC16F877A最小系统。

#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的方向控制寄存器(

//对应的比特为0是输出口,比特为1输入口)

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

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

ADCON1= 0x06; //端口都为数字口

TRISA = 0x00; //设置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(;;) //程序主循环

{

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); //将字符发回PC

uart_last_data=uart_current_data; //暂存当前的字符

}

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) //中断服务程序

{

if(T0IE && T0IF)

{

T0IF = 0; //在此加入TMR0中断服务

}

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

{

TMR1IF=0; //在此加入TMR1中断服务

}

} //中断结束并返回

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

void uart_init(void)

{

SPBRG=0x4d; //设置通讯波特率为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)

{ //等待发送完毕

if(TRMT==1) break ; // 使用if(TXIF == 1) break 时,会导致发送数据的丢失

}

}

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

char uart_receive_byte(void)

{

char result=0;

if(RCIF==1) //判是否收到了数据?

{

result=RCREG; //读取接收到的数据

}

return(result);

}

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

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

//软件思路:选择RAO做为模拟输入通道; // 连续转换4次再求平均值做为转换结果 // 最后结构只取低8位 // 结果送数码管的低3位显示 #include //包含内部资源预定义...

关键字: ad转换 pic16f877a picc

unsigned char eeprom_read(unsigned char addr);void eeprom_write(unsigned char addr, unsigned char value);这两个函数...

关键字: pic16f877a eeprom使用

//*****************电子园PIC16F877A开发板 SPI总线驱动74595实验***************////CKP=0; 空闲为低电平//CKP=1; 空闲为高电平//STAT_CKE=0;...

关键字: pic16f877a spi总线 驱动74595

用c语言写的一个时闹钟程序单片机用16F877,主时钟用20MHz,用32768作定时时间。可以实现2路定闹,每一路都可分别设置和开关,采用4x4键盘,16x2的字符型LCD显示。连线在程序开头有...

关键字: pic16f877a 时钟 程序 语言

/****************************************************标题:万年历**说明:包含文件,*****************************...

关键字: pic16f877a 万年历 程序

//数码管显示//★★★★★★★★★数码管显示★★★★★★★★★★★\\\\ //单征机型号:PIC16F877A //功能描述:采用动态扫描方式,初始时显示123,当按下RB0时,数值加一,当按下RB1时,数值减一...

关键字: pic16f877a 数码管显示

/************************************************PIC16F877A 内部EEPROM读写实验*******Author:hnrainDate:2010-12-26Ver...

关键字: pic16f877a 内部eeprom 内部函数 读写实验

/*Capture mode时,外部CCP1事件触发后,CCPR1H和CCPR1L将获取Timer1的TMR1H和TMR1L中的数值对于CCP1的触发事件,设置键CCP1Con中的相应位CCP1M3--CCP1M0CCP...

关键字: capture mode pic16f877a

PIR1 Register The PIR1 register contains the individual flag bits for the peripheralinterrupts.PIR1 REGISTERPS...

关键字: pic16f877a 单片机 spi学习
关闭