天堂草原最受欢迎的角色,天堂动漫,天堂在线,色天堂下载,天堂中文在线资源,亚洲男人天堂

技術熱線: 4007-888-234

技術支持

三個源程序(設計風格的比較)

更新時間: 2019-03-23

十年專注單片機方案開發的方案公司英銳恩,分享三個源程序(設計風格的比較)。英銳恩現提供服務產品涉及主控芯片:8位單片機、16位單片機、32位單片機及各類運算放大器等。

一:DS1302與PIC16F877接口程序

;================================================ ;DS1302控制子程序 ;================================================= ;程序包括:DS1302初始化,時間數據寫入和讀取程序 ;DS1302_init ;init ds1302 ;Set_DS1302 ;set time to ds1302 ;Get_DS1302 ;get time from ds1302 ;=================================================

;============== 1302定義 =========================================== #DEFINE T_IO PORTE,2 ;1302 I_O #DEFINE T_CLK PORTE,1 ;1302時鐘 #DEFINE T_RST PORTE,0 ;1302使能位 #DEFINE DS1302_RX 20H ;保存接受的1個數據 #DEFINE DS1302_TX 28H ;準備寫入到DS1302的一個數據

counter equ xxxh temp_cnt equ xxxh

;************************************************** ; DS1302初始化程序 ;************************************************** DS1302_init ;未寫 return ;********************************************************** ;子程序名:Set_DS1302 ;功 能:設置DS1302 初始時間,并啟動計時。 ;說 明: ;調 用:Write_byte ;入口參數:初始時間在:W_Second,W_Minute,W_Hour,W_Day,W_Month,W_Week.W_YearL(地址連續) ;出口參數:無 ;消耗資源:counter,SecAddr(預定義) ;設 計:zhengYanbo 日 期:2005.4.21 ;修 改: 日 期: ;********************************************************** Set_DS1302: bcf T_RST bcf T_CLK bsf T_RST movlw 8eh movwf DS1302_TX ;數據發送寄存器 call Write_byte ;發送字節 movlw 00h ;WP=0 movwf DS1302_TX call Write_byte bsf T_CLK bcf T_RST movlw W_Second movwf FSR movlw counter,D'7' movlw 80h movwf SecAddr ;秒寫地址 S13021: bcf T_RST bcf T_CLK bsf T_RST movf SecAddr,W movwf DS1302_TX call Write_byte ;寫秒地址 movf INDF,W movwf DS1302_TX call Write_byte ;寫秒數據 incf FSR incf SecAddr incf SecAddr bsf T_CLK bcf T_RST decfsz counter,F goto S13021 bcf T_RST bcf T_CLK bsf T_RST movlw 8eh ;控制寄存器 movwf DS1302_TX call Write_byte movlw 80h ;控制WP=1,寫保護 movwf DS1302_TX call Write_byte bsf T_CLK bcf T_RST return

;********************************************************** ;子程序名:Get_DS1302 ;功 能:從DS1302 讀時間 ;說 明: ;調 用:Write_byte,Read_byte ;入口參數:時間保存在:R_Second,R_Minute,R_Hour,R_Day,R_Month,R_Week.R_YearL ;出口參數:無 ;消耗資源: counter,SecAddr(預先定義) ;設 計:zhengYanbo 日 期:2005.4.21 ;修 改: 日 期: ;********************************************************** Get_DS1302: movlw R_Second ;準備地址 movwf FSR movlw D'7' movwf counter movlw 81h ;秒讀地址 movwf SecAddr G13021 bcf T_RST bcf T_CLK bsf T_RST movf 

SecAddr,W movwf DS1302_TX call Write_byte call Read_byte movf DS1302_RX,W movwf INDF incf FSR incf SecAddr incf SecAddr bsf T_CLK bcf T_RST decfsz counter,F ;接受7個數據 goto G13021 return ;********************************************************** ;功 能:寫1302一字節 (內部子程序) ;入口:數據預先在DS1302_TX中 ;出口:無 ;消耗資源:temp_cnt(預先定義) ;********************************************************** Write_byte movlw D'8' movwf tmp_cnt W_shift rrf DS1302_TX,F ;帶C移位(低位在前) btfsc STATUS,C goto send_1 bcf T_IO goto send_0 send_1 bsf T_IO send_0 bsf T_CLK ;上升沿寫 nop bcf T_CLK decfsz temp_cnt,F goto W_shift return ;********************************************************** ;功 能:讀1302一字節 (內部子程序) ;入口:無 ;出口:數據保存在DS1302_RX中 ;消耗資源:temp_cnt(預先定義) ;********************************************************** Read_byte bsf STATUS,RP0 ;bank1 bsf TRISE,2 ;設置串行數據口為輸入 bcf STATUS,RP0 ;bank0 movlw D'8' movwf temp_cnt R_shift btfsc T_IO goto get_1 bcf STATUS,C ;clr c goto get_0 get_1 bsf STATUS,C get_0 rrf DS1302_RX,F bsf T_CLK nop bcf T_CLK ;下降沿讀 decfsz temp_cnt,F goto R_shift

;reset output bsf STATUS,RP0 ;bank1 bcf TRISE,2 ;設置T_IO為輸出 bcf STATUS,RP0 ;bank0 return 這個程序一定要,PIC默認PORTE為A/D口,切記,切記!改為其他口時候請參考PIC的DATASHEET ;============================================= ; 初始化端口 ;============================================= Port_Init bsf STATUS,RP0 ;bank1 bcf ADCON1,PCFG0 bsf ADCON1,PCFG1 bsf ADCON1,PCFG2 bcf ADCON1,PCFG3 ;PORTA and PORTE:digital I/O bcf STATUS,RP0 ;bank0 return 二:紅外線發射與接收一例 ; include

;------------------ STATUS equ 0X03 PORTA equ 0x05 PORTB equ 0x06 W equ 0x00 F equ 0x01 Z equ 0x02 C equ 0x00 ;------------------ n55ks equ 0x0C ; nchks equ 0x0D ; avgIR equ 0x0E ; nsamp equ 0x0F ; tdetect equ 0x10 ; ;------------------------------------ org 0x000 goto start org 0x004 start

movlw b'00000001' tris PORTA ; RA0 紅外輸入 movlw b'00000000' tris PORTB ;RB0 紅外輸出 ; 2us in goto at end of loop blip call doblip ; +6.248ms = 6.250ms total call seeblip ; 6.250ms total call seeblip ; 6.250ms total call seeblip ; 6.250ms total call seeblip ; 6.250ms total call seeblip ; 6.250ms total call seeblip ; 6.250ms total call seeblip ; 6.250ms total nop nop call doblip call seeblip call seeblip call seeblip call seeblip call seeblip call seeblip call seeblip goto blip ; 

;============ 產生 55.5kHz鋸形波(346*18us=6.228ms)====== doblip movlw 0xFF movwf n55ks osc nop call make55k ;RB0 紅外輸出 decfsz n55ks,F goto osc ;-------------- morblip ;6253us DELAY movlw 0x5B movwf n55ks oscmore call make55k ;RB0 紅外輸出 nop decfsz n55ks,F goto oscmore ;------------- call delay8u nop nop nop nop nop bcf PORTB,2 ; RB2 LED指示 return 

;--------------------------- make55k bsf PORTB,0 ;RB0 紅外輸出 call delay8u bcf PORTB,0 return delay8u nop nop nop nop return ;========= 6.250ms =================== seeblip clrf avgIR movlw 0xF9 movwf nchks chk call chkIR ;// decfsz nchks,F goto chk movf avgIR,W ; sublw 0X78 btfss STATUS,C goto detectd bcf PORTB,0 ;RB0 紅外輸出 OFF goto chkdone detectd bsf PORTB,0 nop chkdone call delay8u nop nop nop nop return ;======================== chkIR clrf nsamp btfss PORTA,0 incf nsamp,F btfss PORTA,0 ; RA0 紅外輸入 incf nsamp,F btfss PORTA,0 ; RA0 紅外輸入計數值存到nsamp incf nsamp,F btfss PORTA,0 incf nsamp,F btfss PORTA,0 incf nsamp,F btfss PORTA,0 ;共六次 nsamp》5,avgIR增一 incf nsamp,F nop movf nsamp,W sublw 0X04 btfss STATUS,C incf avgIR,F return ;============================ end 

三:18B20通訊程序

這是我在16F877,18F1320,18F1220上通過的18B20程序,18B20主要是延時問題,這個解決了,什么都可以通過。 # include # define uch unsigned char # define unint unsigned int # define DQ RB3 //定義18B20數據端口 # define DQ_DIR TRISB3 //定義18B20D口方向寄存器 # define W1_INPUT 1 # define W1_OUTPUT 0 # define FALSE 0 # define TRUE !FALSE # define DQ_HIGH() DQ_DIR = W1_INPUT # define DQ_LOW() DQ = 0; DQ_DIR = W1_OUTPUT void delay(unint x) { unint d; d=x; while(--d) {;} } bit reset(void) //初始化18B20 { static bit presence; //定義一個應答信號 DQ_LOW(); delay(70); //置總線為低電平并保持至少480us DQ_HIGH(); //等電阻拉高總線并保持15-60us

delay(5); presence=DQ; //接受應答信號 delay(20); //延時60-240us return(presence); //返回應答信號 }

//*************** 讀一位函數******************//

bit read_bit(void) { static bit i;

DQ_LOW(); DQ_LOW(); DQ_HIGH(); asm("nop"); asm("nop"); asm("nop"); i=DQ;

delay(3); return(i); }

//*********************寫一位函數****************//

void write_bit(uch bitval) { DQ_LOW(); delay(1);

if (bitval==1) { DQ_HIGH(); 

}

delay(3); DQ_HIGH(); }

//************** 從18B20中讀一個字節**************//

uch read_byte(void) { uch i; uch j; uch value=0; for (i=0;i<8;i++) { j=read_bit(); //調讀位函數 if (j) //如果是 1 置1 { value|=(0x01<<i);>

//*********************向18B20中 寫一個字節**************//

void write_byte(uch val) { uch i; uch temp; for (i=0;i<8;i++) { temp=val>>i; temp&=0x01; write_bit(temp); //調寫位函數 } asm("nop"); asm("nop"); asm("nop"); } main() { uch teml,temh; 

GIE=0; OSCCON=0X6E; //這是18F1320的頻率選擇寄存器 ADCON1=0X7F;

do{ ; }while (reset()) ; //復位等待從機應答 write_byte(0XCC); //忽略ROM匹配 write_byte(0X44); //發送溫度轉化命令 delay(25000); //延時100-300us do { ; }while( reset()); //再次復位,等待從機應答 write_byte(0XCC); //忽略ROM匹配 write_byte(0XBE); //發送讀溫度命令 teml =read_byte(); //讀出溫度低8 temh=read_byte(); //讀出溫度高8位 DQ_HIGH(); //釋放總線 }

404
返回首頁 |  返回上一頁
联系我们: 苍溪县| 醴陵市| 汨罗市| 驻马店市| 工布江达县| 吴江市| 固原市| 雷山县| 沂源县| 缙云县| 新田县| 华安县| 渝北区| 南昌县| 隆昌县| 崇阳县| 大埔县| 武功县| 渑池县| 庆云县| 上饶县| 岱山县| 专栏| 农安县| 双鸭山市| 江津市| 长兴县| 岑溪市| 论坛| 元阳县| 原平市| 松滋市| 年辖:市辖区| 鄂托克旗| 井研县| 天门市| 五原县| 金湖县| 乐都县| 河间市| 岢岚县|