精品国产人成在线_亚洲高清无码在线观看_国产在线视频国产永久2021_国产AV综合第一页一个的一区免费影院黑人_最近中文字幕MV高清在线视频

您好,歡迎來電子發燒友網! ,新用戶?[免費注冊]

您的位置:電子發燒友網>源碼下載>匯編編程>

16F877單片機與PC機通訊的源程序

大小:2 人氣: 2010-02-25 需要積分:0
的空間

用戶級別:注冊會員

貢獻文章:

貢獻資料:

elecfans.com- 16F877單片機與PC機通訊的源程序.rar

;****************************************
;*?????? 單片機與PC機通訊的源程序?????? *
;****************************************

???????;※※※※※※※※※※※※※※※※※
???????;※資料提供:上海齊濟電子有限公司
???????;※聯系電話: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????? ;發送允許
????? BCF????? STATUS,RP0
WAIT?? BTFSS?? PIR1,RCIF????? ;接收PC機命令
????? GOTO?? WAIT
????? MOVF?? RCREG,0
????? MOVWF?? TEMP
????? MOVWF?? INDF
????? INCF?? FSR
????? DECFSZ?? TEMP???????? ;接收到的數據為1,則接收
????? GOTO?? TXW??????????? ;否則發送數據
????? MOVLW?? 0X06
????? MOVWF?? TEMP
WAIT1?? BTFSS?? PIR1,RCIF????? ;接收一個數據
????? GOTO?? WAIT1
????? MOVF?? RCREG,0
????? MOVWF?? COUNT
????? MOVWF?? TXREG
LOOPTX?? BTFSS?? PIR1,TXIF????? ;將接收到的數據送回PC機
????? GOTO?? LOOPTX
????? MOVF?? COUNT,0
????? MOVWF?? INDF??
????? INCF????? FSR
????? DECFSZ?? TEMP
????? GOTO?? WAIT1
????? GOTO?? OVER???????? ;處理完畢
TXW????? MOVLW?? 0X09??????????? ;向PC機發送9個數據
????? 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????? ;通信任務完成,關接受和發送允許
????? BSF????? STATUS,RP0
????? BCF????? TXSTA,TXEN
????? BCF????? STATUS,RP0
????? CALL?? LED??????????? ;調用顯示子程序,將發送(或接收)的數據顯示出來
????? 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位數據位,奇偶檢驗,1位停止位*/
?outportb(port+1,0x00);?????????????? /*關中斷*/
?inportb(port+5);????????????????? /*讀一次線路狀態寄存器,使其復位*/
?{
? 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));???????? /*發送保持器滿則等待*/
??? outportb(port,0x01);??????????? /*否則發送數據01通知單片機準備接收*/
??? for(i=0;i<6;i++)?????????????? /*共發送6個數據*/
?? {
??? a=b[i];
??? while(!(inportb(port+5)&0x20))
??? delay(100);????????????????? /*發送保持器滿,等待*/
??? outportb(port,a);?????????????? /*發送a*/
??? printf("%d\n",a);?????????????? /*顯示a*/
??? while(!(inport(port+5)&1));???????? /*接收單片機送回的數據*/
??? ch[i]=inport(port);?????????????? /*保存*/
??? }
??? delay(10);
??? for(j=0;j<8;j++)?????????????? /*顯示接收的回送數據*/
??? printf("\n%d\n",ch[j]);
??? getch();
??? break;
??? }
??? case'r':???????????????????? /*接收數據*/
??? case'R':
??? {
???? while(!(inportb(port+5)&0x20));?????
???? outportb(port,0x02);??????????? /*發送數據02通知單片機發送數據*/
???? for(j=0;j<9;j++)?????????????? /*共接收9個數據*/
???? {
????? 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%

      發表評論

      用戶評論
      評價:好評中評差評

      發表評論,獲取積分! 請遵守相關規定!

      ?