#include<reg51.h>/*************************ds1302與at89s52引腳連接********************/sbit T_RST=P3^5; sbit T_CLK=P3^6; sbit T_IO=P3^7; sbit ACC0=ACC^0;sbit ACC7=ACC^7;unsigned char seg[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09}; //0~~9段碼 /******************DS1302:寫入操作(上升沿)*********************/ voID write_byte(unsigned char da){ unsigned char i; ACC=da; for(i=8;i>0;i--) { T_IO=ACC0; T_CLK=0; T_CLK=1; ACC=ACC>>1; }} /******************DS1302:讀取操作(下降沿)*****************/unsigned char read_byte(voID){ unsigned char i; for(i=0;i<8;i++) { ACC=ACC>>1; T_CLK = 1; T_CLK = 0; ACC7 = T_IO; } return(ACC); } /******************DS1302:寫入數(shù)據(jù)(先送地址,再寫數(shù)據(jù))***************************/ voID write_1302(unsigned char addr,unsigned char da){ T_RST=0; //停止工作 T_CLK=0; T_RST=1; //重新工作 write_byte(addr); //寫入地址 write_byte(da); T_RST=0; T_CLK=1;}
上傳時間: 2014-01-17
上傳用戶:sglccwk
//芯片資料請到www.elecfans.com查找 //DS1820 C51 子程序//這里以11.0592M晶體為例,不同的晶體速度可能需要調(diào)整延時的時間//sbit DQ =P2^1;//根據(jù)實際情況定義端口 typedef unsigned char byte;typedef unsigned int word; //延時voID delay(word useconds){ for(;useconds>0;useconds--);} //復(fù)位byte ow_reset(voID){ byte presence; DQ = 0; //pull DQ line low delay(29); // leave it low for 480us DQ = 1; // allow line to return high delay(3); // wait for presence presence = DQ; // get presence signal delay(25); // wait for end of timeslot return(presence); // presence signal returned} // 0=presence, 1 = no part //從 1-wire 總線上讀取一個字節(jié)byte read_byte(voID){ byte i; byte value = 0; for (i=8;i>0;i--) { value>>=1; DQ = 0; // pull DQ low to start timeslot DQ = 1; // then return high delay(1); //for (i=0; i<3; i++); if(DQ)value|=0x80; delay(6); // wait for rest of timeslot } return(value);} //向 1-WIRE 總線上寫一個字節(jié)voID write_byte(char val){ byte i; for (i=8; i>0; i--) // writes byte, one bit at a time { DQ = 0; // pull DQ low to start timeslot DQ = val&0x01; delay(5); // hold value for remainder of timeslot DQ = 1; val=val/2; } delay(5);} //讀取溫度char Read_Temperature(voID){ union{ byte c[2]; int x; }temp; ow_reset(); write_byte(0xCC); // Skip ROM write_byte(0xBE); // Read Scratch Pad temp.c[1]=read_byte(); temp.c[0]=read_byte(); ow_reset(); write_byte(0xCC); //Skip ROM write_byte(0x44); // Start Conversion return temp.x/2;}
上傳時間: 2013-11-03
上傳用戶:hongmo
串行編程器源程序(Keil C語言)//FID=01:AT89C2051系列編程器//實現(xiàn)編程的讀,寫,擦等細(xì)節(jié)//AT89C2051的特殊處:給XTAL一個脈沖,地址計數(shù)加1;P1的引腳排列與AT89C51相反,需要用函數(shù)轉(zhuǎn)換#include <e51pro.h> #define C2051_P3_7 P1_0#define C2051_P1 P0//注意引腳排列相反#define C2051_P3_0 P1_1#define C2051_P3_1 P1_2#define C2051_XTAL P1_4#define C2051_P3_2 P1_5#define C2051_P3_3 P1_6#define C2051_P3_4 P1_7#define C2051_P3_5 P3_5 voID InitPro01()//編程前的準(zhǔn)備工作{ SetVpp0V(); P0=0xff; P1=0xff; C2051_P3_5=1; C2051_XTAL=0; Delay_ms(20); nAddress=0x0000; SetVpp5V();} voID ProOver01()//編程結(jié)束后的工作,設(shè)置合適的引腳電平{ SetVpp5V(); P0=0xff; P1=0xff; C2051_P3_5=1; C2051_XTAL=1;} BYTE GetData()//從P0口獲得數(shù)據(jù){ B_0=P0_7; B_1=P0_6; B_2=P0_5; B_3=P0_4; B_4=P0_3; B_5=P0_2; B_6=P0_1; B_7=P0_0; return B;} voID SetData(BYTE DataByte)//轉(zhuǎn)換并設(shè)置P0口的數(shù)據(jù){ B=DataByte; P0_0=B_7; P0_1=B_6; P0_2=B_5; P0_3=B_4; P0_4=B_3; P0_5=B_2; P0_6=B_1; P0_7=B_0;} voID ReadSign01()//讀特征字{ InitPro01(); Delay_ms(1);//----------------------------------------------------------------------------- //根據(jù)器件的DataSheet,設(shè)置相應(yīng)的編程控制信號 C2051_P3_3=0; C2051_P3_4=0; C2051_P3_5=0; C2051_P3_7=0; Delay_ms(20); ComBuf[2]=GetData(); C2051_XTAL=1; C2051_XTAL=0; Delay_us(20); ComBuf[3]=GetData(); ComBuf[4]=0xff;//----------------------------------------------------------------------------- ProOver01();} voID Erase01()//擦除器件{ InitPro01();//----------------------------------------------------------------------------- //根據(jù)器件的DataSheet,設(shè)置相應(yīng)的編程控制信號 C2051_P3_3=1; C2051_P3_4=0; C2051_P3_5=0; C2051_P3_7=0; Delay_ms(1); SetVpp12V(); Delay_ms(1); C2051_P3_2=0; Delay_ms(10); C2051_P3_2=1; Delay_ms(1);//----------------------------------------------------------------------------- ProOver01();} BOOL Write01(BYTE Data)//寫器件{//----------------------------------------------------------------------------- //根據(jù)器件的DataSheet,設(shè)置相應(yīng)的編程控制信號 //寫一個單元 C2051_P3_3=0; C2051_P3_4=1; C2051_P3_5=1; C2051_P3_7=1; SetData(Data); SetVpp12V(); Delay_us(20); C2051_P3_2=0; Delay_us(20); C2051_P3_2=1; Delay_us(20); SetVpp5V(); Delay_us(20); C2051_P3_4=0; Delay_ms(2); nTimeOut=0; P0=0xff; nTimeOut=0; while(!GetData()==Data)//效驗:循環(huán)讀,直到讀出與寫入的數(shù)相同 { nTimeOut++; if(nTimeOut>1000)//超時了 { return 0; } } C2051_XTAL=1; C2051_XTAL=0;//一個脈沖指向下一個單元//----------------------------------------------------------------------------- return 1;} BYTE Read01()//讀器件{ BYTE Data;//----------------------------------------------------------------------------- //根據(jù)器件的DataSheet,設(shè)置相應(yīng)的編程控制信號 //讀一個單元 C2051_P3_3=0; C2051_P3_4=0; C2051_P3_5=1; C2051_P3_7=1; Data=GetData(); C2051_XTAL=1; C2051_XTAL=0;//一個脈沖指向下一個單元//----------------------------------------------------------------------------- return Data;} voID Lock01()//寫鎖定位{ InitPro01();//先設(shè)置成編程狀態(tài)//----------------------------------------------------------------------------- //根據(jù)器件的DataSheet,設(shè)置相應(yīng)的編程控制信號 if(ComBuf[2]>=1)//ComBuf[2]為鎖定位 { C2051_P3_3=1; C2051_P3_4=1; C2051_P3_5=1; C2051_P3_7=1; Delay_us(20); SetVpp12V(); Delay_us(20); C2051_P3_2=0; Delay_us(20); C2051_P3_2=1; Delay_us(20); SetVpp5V(); } if(ComBuf[2]>=2) { C2051_P3_3=1; C2051_P3_4=1; C2051_P3_5=0; C2051_P3_7=0; Delay_us(20); SetVpp12V(); Delay_us(20); C2051_P3_2=0; Delay_us(20); C2051_P3_2=1; Delay_us(20); SetVpp5V(); }//----------------------------------------------------------------------------- ProOver01();} voID PreparePro01()//設(shè)置pw中的函數(shù)指針,讓主程序可以調(diào)用上面的函數(shù){ pw.fpInitPro=InitPro01; pw.fpReadSign=ReadSign01; pw.fpErase=Erase01; pw.fpWrite=Write01; pw.fpRead=Read01; pw.fpLock=Lock01; pw.fpProOver=ProOver01;}
上傳時間: 2013-11-12
上傳用戶:gut1234567
用C51寫的普通拼音輸入法源程序代碼:原作使用了一個二維數(shù)組用以查表,我認(rèn)為這樣比較的浪費空間,而且每個字表的索引地址要手工輸入,效率不高。所以我用結(jié)構(gòu)體將其改寫了一下。就是大家現(xiàn)在看到的這個。 因為代碼比較的大,共有6,000多漢字,這樣就得要12,000 byte來存放GB內(nèi)碼,所以也是沒辦法的.編譯結(jié)果約為3000h,因為大部分是索引表,代碼優(yōu)化幾乎無效。 在Keil C里仿真芯片選用的是華邦的W77E58,它有32k ROM, 256B on-chip RAM, 1K on-chip SRAM (用DPTR1指針尋址,相當(dāng)于有1K的片上xdata)。條件有限,沒有上片試驗,仿真而已。 打算將其移植到AVR上,但CodeAVRC與IAR EC++在結(jié)構(gòu)體、指針的定義使用上似乎與C51不太一樣,現(xiàn)在還未搞定。還希望在這方面有經(jīng)驗的網(wǎng)友能給予指導(dǎo)。 #include<stdio.h> char * py_ime(char *); voID main(voID){ while(1) { char input_string[]="yI"; xdata char chinese_string[255]; sprintf(chinese_string,"%s",py_ime(input_string)); }}
上傳時間: 2013-10-30
上傳用戶:cainaifa
#include <reg51.h>#include<intrins.h> #define BUSY1 (DQ1==0) sbit DQ1 = P0^4; unsigned char idata TMP; unsigned char idata TMP_d; unsigned char f; voID wr_ds18_1(char dat);unsigned char rd_ds18_1(); /***************延時程序,單位us,大于10us*************/voID time_delay(unsigned char time){ time=time-10; time=time/6; while(time!=0)time--;} /*****************************************************//* reset ds18b20 *//*****************************************************/voID ds_reset_1(voID){ unsigned char idata count=0; DQ1=0; time_delay(240); time_delay(240); DQ1=1; return;}
上傳時間: 2013-10-29
上傳用戶:sssnaxie
#include <at24c01a.h>/*************************************************向24C01A寫入一個字節(jié)輸入:E2ROM地址,字節(jié)數(shù)據(jù)******************************************************/voID write24c01a(uchar uadd_1,uchar udata_1){sendbyte=0xa0;start();send(sendbyte);if (!ack())continue;send(uadd_1);if (!ack())continue;send(udata_1)if (!ack())continue;stop();}/**********************************發(fā)送開始*****************************************/voID start(voID){a_scl=1;a_sda=1;a_sda=0;a_scl=0;a_scl=1;}/********************************************發(fā)送停止*******************************************/voID stop(voID){a_scl=0;a_sda=0;a_scl=1;a_sda=1;} /*********************************************發(fā)送反饋************************************************/bit ack(voID){int a_acka_scl=0;a_scl=0;a_scl=0;a_scl=1;a_ack=a_sda;a_scl=0;return(a_ack)}/**************************************發(fā)送無反饋********************************************/bit noack(voID){int a_ack;a_scl=1;a_scl=1;a_scl=0;}/*******************************************發(fā)送****************************************************/voID send(uchar undata){uchar i;sendbyte=undatafor(i=8;i>0;i--){a_sda=sendbyte7;a_scl=0;a_scl=1;sendbyte=sendbyte<<1}}/********************************************接受****************************************************/ voID receive(voID){int i;uchar data;for(i=8;i>0;i--){ a_scl=1;receivebyte7=a_sda;a_scl=0;receivebyte=receivebyte>>1}receivedata=receivebyte;}/********************************************向 24c01a讀一個字節(jié);輸入:EEROM地址;輸出:EEROM數(shù)據(jù);********************************************/voID read24c01a(uchar counter){receivebyte=0xa1;start();send(receivebyte);if (!ack())continue;send(counter);if (!ack())continue;receive()noack();stop();}
上傳時間: 2013-12-23
上傳用戶:wxhwjf
24c16讀寫驅(qū)動程序,//=-------------------------------------------------------------------------------/*模塊調(diào)用:讀數(shù)據(jù):read(unsigned int address)寫數(shù)據(jù):write(unsigned int address,unsigned char dd) dd為要寫的 數(shù)據(jù)字節(jié)*///------------------------------------------------------------------------------ sbit sda=P3^0;sbit scl=P3^1; sbit a0=ACC^0; //定義ACC的位,利用ACC操作速度最快sbit a1=ACC^1;sbit a2=ACC^2;sbit a3=ACC^3;sbit a4=ACC^4;sbit a5=ACC^5;sbit a6=ACC^6;sbit a7=ACC^7; //------------------------------------------------------------------------------#pragma disablevoID s24(voID) //起始函數(shù){_nop_(); scl=0; sda=1; scl=1; _nop_(); sda=0; _nop_(); _nop_(); scl=0; _nop_(); _nop_(); sda=1;} //------------------------------------------------------------------------------#pragma disablevoID p24(voID) //停止函數(shù){sda=0; scl=1; _nop_(); _nop_(); sda=1;} //-----------------------------------------------------------------------------#pragma disableunsigned char rd24(voID) /////////////////從24c16讀一字節(jié)數(shù)據(jù){ ACC=0x00;sda=1;scl=1;a7=sda;_nop_();_nop_();_nop_();_nop_();scl=0;scl=1;a6=sda;_nop_();_nop_();_nop_();_nop_();scl=0;scl=1;a5=sda;_nop_();_nop_();_nop_();_nop_();scl=0;scl=1;a4=sda;_nop_();_nop_();_nop_();_nop_();scl=0;scl=1;a3=sda;_nop_();_nop_();_nop_();_nop_();scl=0;scl=1;a2=sda;_nop_();_nop_();_nop_();_nop_();scl=0;scl=1;a1=sda;_nop_();_nop_();_nop_();_nop_();scl=0;scl=1;a0=sda;_nop_();_nop_();_nop_();_nop_();scl=0;sda=1;scl=1;_nop_();_nop_();_nop_();_nop_();scl=0; /// ///////////////24c16的一位回答位。return(ACC);}//------------------------------------------------------------------------------#pragma disablevoID wd24(unsigned char dd) ////////////////向24c16寫一字節(jié)數(shù)據(jù){ sda=1;ACC=dd;sda=a7;scl=1;_nop_();_nop_();_nop_();_nop_();scl=0;sda=a6;scl=1;_nop_();_nop_();_nop_();_nop_();scl=0;sda=a5;scl=1;_nop_();_nop_();_nop_();_nop_();scl=0;sda=a4;scl=1;_nop_();_nop_();_nop_();_nop_();scl=0;sda=a3;scl=1;_nop_();_nop_();_nop_();_nop_();scl=0;sda=a2;scl=1;_nop_();_nop_();_nop_();_nop_();scl=0;sda=a1;scl=1;_nop_();_nop_();_nop_();_nop_();scl=0;sda=a0;scl=1;_nop_();_nop_();_nop_();_nop_();scl=0;sda=0;scl=1;//scl=0;(在下面程序中)}//---------------------------------------------------------------------------#pragma disableunsigned char read(unsigned int address){unsigned char dd; s24(); ////////////////////////開始條件 wd24(0xa0); /////////////////////////寫器件地址(寫命令) _nop_();_nop_();_nop_();_nop_(); scl=0; ///////////////////////////////////接收器件地址確認(rèn)信號 wd24(address); //////////////////////////// 寫數(shù)據(jù)地址 _nop_();_nop_();_nop_();_nop_(); scl=0;s24(); ///////////////////////////////////開始條件 wd24(0xa1); /////////////////////////////寫器件地址(讀命令) scl=0; dd=rd24(); //////////////////////////////////讀 一字節(jié) p24(); ////////////////////////////////////停止條件 return(dd);}//------------------------------------------------------------------------------#pragma disablevoID write(unsigned int address,unsigned char dd){s24(); /////////////////開始條件 wd24(0xa0); ////////////////////////寫器件地址; scl=0; wd24(address); /////////////////////寫數(shù)據(jù)地址 scl=0; wd24(dd); //////////////////////////寫dd數(shù)據(jù) scl=0; p24(); /////////////////////////停止條件; }
上傳時間: 2013-11-18
上傳用戶:墻角有棵樹
C51控制并口打印機(jī)實例:/* 沈陽新榮達(dá)電子 *//* 2004-12-7 */#include <reg52.h>#define uchar unsigned char#define uint unsigned int#define data_8 P0sbit BUSY = P1^2; //打印機(jī) BUSY 接P1.2sbit STB = P1^0; //打印機(jī) STB 接P1.0voID print(uchar j) //打印子程序{ uchar i;while(BUSY){}; //BUSY=1,打印機(jī)忙,等待BUSY 為0 再發(fā)數(shù)data_8=j;STB=0;i++;i--;STB=1; //給出數(shù)據(jù)鎖存時鐘BUSY=1;}voID main(voID){BUSY = 1; //忙信號置高STB = 1; //選通信號置高print(0x1b); //打印機(jī)初始化命令print(0x38);print(0x04);for(;;){print(0xd0); //發(fā)送漢字內(nèi)碼“新榮達(dá)”print(0xc2);print(0xc8);print(0xd9);print(0xb4);print(0xef);print(0x0d); //換行}}
上傳時間: 2013-11-13
上傳用戶:lwq11
微型打印機(jī)的C語言源程序:微型打印機(jī)的C51源程序#define uchar unsigned char#define uint unsigned int#include <reg52.h>#include <stdio.h>#include <absacc.h>#include <math.h>#include <string.h>#include <ctype.h>#include <stdlib.h>#define PIN XBYTE[0x8000]#define POUT XBYTE[0x9000]sbit PRINTSTB =P1^6;sbit DOG=P1^7;bdata char pin&#118alue;sbit PRINTBUSY=pin&#118alue^7;sbit PRINTSEL =pin&#118alue^6;sbit PRINTERR =pin&#118alue^5;sbit PRINTACK =pin&#118alue^4; voID PrintString(uchar *String1,uchar *String2);voID initprint(voID);voID print(uchar a); voID initprint(voID) //打印機(jī)初始化子程序 { pin&#118alue=PIN; if((PRINTSEL==1)&&(PRINTERR==1)) { print(0x1b); print(0x40); print(0x1b); print(0x38); print(0x4); }}voID print(uchar a) //打印字符a{ pin&#118alue=PIN; if((PRINTSEL==0)||(PRINTERR==0)) return; for(;;) { DOG=~DOG; pin&#118alue=PIN; if(PRINTBUSY==0) break; } DOG=~DOG; POUT=a; PRINTSTB=1; PRINTSTB=1; PRINTSTB=1; PRINTSTB=1; PRINTSTB=0; PRINTSTB=0; PRINTSTB=0; PRINTSTB=0; PRINTSTB=1;}voID PrintString(uchar *String) //打印字符串后回車{ uchar CH; for (;;) { DOG=~DOG; CH=*String; if (CH==0) { print(0x0d); break; } print(CH); String++; } initprint();}
上傳時間: 2013-10-18
上傳用戶:hasan2015
//遙控解碼子程序,LC7461,用戶碼為11C//external interrupt0voID isr_4(){ unsigned char r_count;//定義解碼的個數(shù) unsigned long use_data=0;//定義16位的用戶碼,只用到13位 unsigned long use_code=0;//定義16位的用戶反碼,只用到13位 unsigned long data=0;//定義16位數(shù)據(jù)碼,包括8位數(shù)據(jù)碼和反碼 unsigned char data_h=0;//數(shù)據(jù)反碼 unsigned char data_l=0;//數(shù)據(jù)碼 _clrwdt();// _delay(7000);//7461解碼,延時7000// _delay(7000);//7461解碼,延時7000//_delay(7000);//7461解碼,延時7000 if(remote==1) goto error; while(remote==0);//wait to high //_delay(9744);count_delay=0; while(count_delay<143); if(remote==1) goto error; /////用戶碼解碼use_data//////////add////////////////////////// for(r_count=13;r_count>0;r_count--) { while(remote==0);//wait to high count_delay=0; while(count_delay<24);//_delay(1680); _c=remote; if(_c==1) { _lrrc(&use_data); count_delay=0; while(count_delay<32);//_delay(2200);//wait to low } else _lrrc(&use_data); } _nop(); //if(remote==1) //_delay(1680);//wait to low while(remote==1);//wait to low _nop(); ////////用戶碼解碼finish/////////add/////////add//////// /////用戶碼反碼解碼use_code//////////add////////////////////////// for(r_count=13;r_count>0;r_count--) { while(remote==0);//wait to high count_delay=0; while(count_delay<24);//_delay(1680); _c=remote; if(_c==1) { _lrrc(&use_code); count_delay=0; while(count_delay<32);//_delay(2200);//wait to low } else _lrrc(&use_code); } _nop(); //if(remote==1) // _delay(1680);//wait to low while(remote==1);//wait to low _nop(); ////////用戶碼反碼解碼finish/////////add/////////add//////// ////數(shù)據(jù)碼解碼開始////data_l為用戶碼,data_h為數(shù)據(jù)碼反碼//////////// for(r_count=16;r_count>0;r_count--) { while(remote==0);//wait to high count_delay=0; while(count_delay<24);//_delay(1680); _c=remote; if(_c==1) { _lrrc(&data); count_delay=0; while(count_delay<32);//_delay(2200);//wait to low } else _lrrc(&data); } ////數(shù)據(jù)碼解碼結(jié)束//////////////////////////////////////////////// data_l=data; data_h=data>>8; ///用戶碼////// use_data>>=3; use_code>>=3; use_code=~use_code; //////// ////如果用戶碼等與0x11c并且數(shù)據(jù)碼和數(shù)據(jù)反碼都校驗一致,解碼成功 //if((~data_h==data_l)&&use_data==0x11c)//使用用戶碼 //跳過用戶碼 if(~data_h==data_l)//如果數(shù)據(jù)碼和數(shù)據(jù)反碼(取反后)相等,解碼正確 { _nop(); r_data=data_l;//r_data為解出的最終數(shù)據(jù)碼 } //否則解碼不成功 _nop(); _nop();error: //r_data=nocode; _nop(); _nop(); _nop();}
上傳時間: 2014-03-27
上傳用戶:shenlan
蟲蟲下載站版權(quán)所有 京ICP備2021023401號-1