16F877單片機與PC機通訊的源程序
elecfans.com- 16F877單片機與PC機通訊的源程序.rar
;****************************************
;*?????? 單片機與PC機通訊的源程序?????? *
;****************************************
???????;※※※※※※※※※※※※※※※※※
???????;※資料提供:上海齊濟電子有限公司※
???????;※聯(lián)系電話:21-65630199???????? ※
???????;※※※※※※※※※※※※※※※※※
;單片機與PC機通訊的源程序(詳細)??
;單片機的程序
LIST????? P=16F876
#INCLUDE??? P16F876.INC
CBLOCK 0X24
COUNT??
TEMP
ENDC
????? ORG?? 0X0000
????? NOP
START?? GOTO?? MAIN
????? ORG????? 0X020
MAIN?? MOVLW?? 0X30??????????? ;將RAM單元初始化
????? MOVWF?? FSR
????? BCF????? STATUS,RP0
????? MOVLW?? 0X22
????? MOVWF?? COUNT
INTRAM?? MOVF?? COUNT,0
????? MOVWF?? INDF
????? INCF????? COUNT,1
????? INCF????? FSR,1
????? BTFSS?? FSR,7
????? GOTO?? INTRAM
????? BANKSEL?? TXSTA???????? ;波特率設置為9 600 bps
????? MOVLW?? 0X19
????? MOVWF?? SPBRG???????? ;異步高速方式
????? MOVLW?? 0X04
????? MOVWF?? TXSTA
????? BCF?? STATUS,RP0
????? MOVLW?? 0X80??????????? ;串行口使能
????? MOVWF?? RCSTA
????? MOVLW?? 0X30
????? MOVWF?? FSR
????? BSF????? RCSTA,CREN????? ;接收允許
????? BSF????? STATUS,RP0
????? BSF????? TXSTA,TXEN????? ;發(fā)送允許
????? BCF????? STATUS,RP0
WAIT?? BTFSS?? PIR1,RCIF????? ;接收PC機命令
????? GOTO?? WAIT
????? MOVF?? RCREG,0
????? MOVWF?? TEMP
????? MOVWF?? INDF
????? INCF?? FSR
????? DECFSZ?? TEMP???????? ;接收到的數(shù)據(jù)為1,則接收
????? GOTO?? TXW??????????? ;否則發(fā)送數(shù)據(jù)
????? MOVLW?? 0X06
????? MOVWF?? TEMP
WAIT1?? BTFSS?? PIR1,RCIF????? ;接收一個數(shù)據(jù)
????? GOTO?? WAIT1
????? MOVF?? RCREG,0
????? MOVWF?? COUNT
????? MOVWF?? TXREG
LOOPTX?? BTFSS?? PIR1,TXIF????? ;將接收到的數(shù)據(jù)送回PC機
????? GOTO?? LOOPTX
????? MOVF?? COUNT,0
????? MOVWF?? INDF??
????? INCF????? FSR
????? DECFSZ?? TEMP
????? GOTO?? WAIT1
????? GOTO?? OVER???????? ;處理完畢
TXW????? MOVLW?? 0X09??????????? ;向PC機發(fā)送9個數(shù)據(jù)
????? MOVWF?? TEMP
????? MOVLW?? 0X30
????? MOVWF?? FSR
TXW0?? MOVF?? INDF,0
????? MOVWF?? TXREG
????? INCF????? FSR
TXW1?? BTFSS?? PIR1,TXIF
????? GOTO?? TXW1
????? DECFSZ?? TEMP
????? GOTO?? TXW0??
OVER?? BCF????? RCSTA,CREN????? ;通信任務完成,關接受和發(fā)送允許
????? BSF????? STATUS,RP0
????? BCF????? TXSTA,TXEN
????? BCF????? STATUS,RP0
????? CALL?? LED??????????? ;調(diào)用顯示子程序,將發(fā)送(或接收)的數(shù)據(jù)顯示出來
????? END????????????????? ;程序完
P C 機 程 序
PC采用Toubr C 進行編寫。程序如下:
#include
#define? port??? 0x3f8?????????????? /*利用串口1進行通信*/
int ch[15];
main ()
{
?int a;
?int i,j;
?int b[6]={88,15,38,26,20,0};
?char c;
?clrscr();
?outportb(port+3,0x80);?????????????? /*準備設置波特率*/
?outportb(port,0x0C);?????????????? /*波特率設置為9 600 bps*/
?outportb(port+1,0x00);
?outportb(port+3,0x03);?????????????? /*8位數(shù)據(jù)位,奇偶檢驗,1位停止位*/
?outportb(port+1,0x00);?????????????? /*關中斷*/
?inportb(port+5);????????????????? /*讀一次線路狀態(tài)寄存器,使其復位*/
?{
? printf("\t\tsend data or receive data: (s or r?)\n\n\n");
? c=getchar();
?? switch(c)
?? {
?? case 's':
?? case 'S':
?? {
?? while(!(inportb(port+5)&0x20));???????? /*發(fā)送保持器滿則等待*/
??? outportb(port,0x01);??????????? /*否則發(fā)送數(shù)據(jù)01通知單片機準備接收*/
??? for(i=0;i<6;i++)?????????????? /*共發(fā)送6個數(shù)據(jù)*/
?? {
??? a=b[i];
??? while(!(inportb(port+5)&0x20))
??? delay(100);????????????????? /*發(fā)送保持器滿,等待*/
??? outportb(port,a);?????????????? /*發(fā)送a*/
??? printf("%d\n",a);?????????????? /*顯示a*/
??? while(!(inport(port+5)&1));???????? /*接收單片機送回的數(shù)據(jù)*/
??? ch[i]=inport(port);?????????????? /*保存*/
??? }
??? delay(10);
??? for(j=0;j<8;j++)?????????????? /*顯示接收的回送數(shù)據(jù)*/
??? printf("\n%d\n",ch[j]);
??? getch();
??? break;
??? }
??? case'r':???????????????????? /*接收數(shù)據(jù)*/
??? case'R':
??? {
???? while(!(inportb(port+5)&0x20));?????
???? outportb(port,0x02);??????????? /*發(fā)送數(shù)據(jù)02通知單片機發(fā)送數(shù)據(jù)*/
???? for(j=0;j<9;j++)?????????????? /*共接收9個數(shù)據(jù)*/
???? {
????? while(!(inportb(port+5)&1));
???? ch[j]=inportb(port);
???? }
???? for(j=0;j<9;j++)
???? printf("\n %d\n",ch[j]);
???? getch();
???? break;
???? }
??? }
? }
}
非常好我支持^.^
(2) 100%
不好我反對
(0) 0%
下載地址
16F877單片機與PC機通訊的源程序下載
相關電子資料下載
- java源程序是由類定義組成的嗎 173
- 一個java源程序中允許有多個公共類 176
- c語言源程序的基本單位 346
- 如何把c語言源程序變成應用程序 451
- c語言編寫的源程序可以直接運行嗎 827
- 一個c源程序至少包括一個函數(shù)么 271
- c源程序的基本結(jié)構(gòu)是什么 190
- c語言源程序main函數(shù)的位置 296
- c語言源程序的基本單位 281
- e2 studio環(huán)境下添加源程序到當前工程的注意事項 341