您的位置 首页 应用

PIC196F877A串口通讯程序

今天上午完成PIC16F877A与上位机的串口通信程序!注意:使用MPLABIDEC语言编程时,自定义头文件要使用包含不能使用>串口与单片机的…

今天上午完结PIC16F877A与上位机的串口通讯程序!

留意:运用MPLAB IDE C言语编程时,自界说头文件要运用””包括不能运用<>

串口与单片机的连线原理图

串口通讯头文件

#ifndef T232_H
#define T232_H

#include “main.h”
//界说一帧的开端和完毕
#define FRAME_BEGIN 0x28//开端帧标志
#define FRAME_END 0x29//完毕帧标志

void init_232() ;
void send_str(const char *str) ;
char get_char() ;
void get_string(char *temp) ;
void put_char(char temp) ;
#endif

串行通讯子程序

//根据TPDEM1,经过串口调试帮手等串口调查软件调查程序,
//将TPDEM1经过%&&&&&%D2配送的串口延长线与PC的串口衔接,设置PC的串口为默认设置,如波特率9600,数据位8,无校验位,运用FIFO
#include “t232.h”
/*
*Function:init seriel port
*/
void init_232()
{
INTCON=0;
TRISC7=1; //RX置输入
TRISC6=0; //TX清成输出
RCSTA=0x90;//接连承受多位数据
TXSTA=0x24;
SPBRG=25; //9600=4000000/(16*(X+1))->X=25,high speed mode
// INTCON=0xC0;//开GIE,外围中止PEIE
//RCIE=1; //开接纳中止
}
/*
*Fuction :send const string to seriel port
/

void send_str(const char *p)
{
while((*p)!=/0)
{
put_char(*p++) ;
}
}
/
Function:get charactor from serial port
*/
char get_char()
{
char temp ;
while(!RCIF) ; /* set when register is not empty */
temp = RCREG ;
return RCREG; /* RXD9 and FERR are gone now */
}

/*
*Function:send charactot to serial port
/
void put_char(char temp)
{
TXREG=temp ;
while(TRMT==0) ;
}

/*
*Function:get num bytes charactot
/
void get_string(char *t)
{
uchar i ;
char temp ;

//有或许承受一帧数据后由于去处理数据,导致承受下一帧数据失利
SPEN=1 ;//有必要留意在接连承受完一帧数据时,必定要把这两位使能,
CREN=1 ;//我便是由于没有使能查了一上午的错,在找出过错地点;
while(1)
{
temp=get_char() ;
if(temp==FRAME_BEGIN)
{
i=0 ;
continue ;
}
if(temp==FRAME_END)
{
t[i]=/0 ;
break ;
}
t[i++]=temp ;
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/325921.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部