#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:寫入數據(先送地址,再寫數據)***************************/ 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晶體為例,不同的晶體速度可能需要調整延時的時間//sbit DQ =P2^1;//根據實際情況定義端口 typedef unsigned char byte;typedef unsigned int word; //延時VOID delay(word useconds){ for(;useconds>0;useconds--);} //復位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 總線上讀取一個字節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 總線上寫一個字節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系列編程器//實現編程的讀,寫,擦等細節//AT89C2051的特殊處:給XTAL一個脈沖,地址計數加1;P1的引腳排列與AT89C51相反,需要用函數轉換#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()//編程前的準備工作{ SetVpp0V(); P0=0xff; P1=0xff; C2051_P3_5=1; C2051_XTAL=0; Delay_ms(20); nAddress=0x0000; SetVpp5V();} VOID ProOver01()//編程結束后的工作,設置合適的引腳電平{ SetVpp5V(); P0=0xff; P1=0xff; C2051_P3_5=1; C2051_XTAL=1;} BYTE GetData()//從P0口獲得數據{ 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)//轉換并設置P0口的數據{ 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);//----------------------------------------------------------------------------- //根據器件的DataSheet,設置相應的編程控制信號 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();//----------------------------------------------------------------------------- //根據器件的DataSheet,設置相應的編程控制信號 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)//寫器件{//----------------------------------------------------------------------------- //根據器件的DataSheet,設置相應的編程控制信號 //寫一個單元 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)//效驗:循環讀,直到讀出與寫入的數相同 { nTimeOut++; if(nTimeOut>1000)//超時了 { return 0; } } C2051_XTAL=1; C2051_XTAL=0;//一個脈沖指向下一個單元//----------------------------------------------------------------------------- return 1;} BYTE Read01()//讀器件{ BYTE Data;//----------------------------------------------------------------------------- //根據器件的DataSheet,設置相應的編程控制信號 //讀一個單元 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();//先設置成編程狀態//----------------------------------------------------------------------------- //根據器件的DataSheet,設置相應的編程控制信號 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()//設置pw中的函數指針,讓主程序可以調用上面的函數{ 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寫的普通拼音輸入法源程序代碼:原作使用了一個二維數組用以查表,我認為這樣比較的浪費空間,而且每個字表的索引地址要手工輸入,效率不高。所以我用結構體將其改寫了一下。就是大家現在看到的這個。 因為代碼比較的大,共有6,000多漢字,這樣就得要12,000 byte來存放GB內碼,所以也是沒辦法的.編譯結果約為3000h,因為大部分是索引表,代碼優化幾乎無效。 在Keil C里仿真芯片選用的是華邦的W77E58,它有32k ROM, 256B on-chip RAM, 1K on-chip SRAM (用DPTR1指針尋址,相當于有1K的片上xdata)。條件有限,沒有上片試驗,仿真而已。 打算將其移植到AVR上,但CodeAVRC與IAR EC++在結構體、指針的定義使用上似乎與C51不太一樣,現在還未搞定。還希望在這方面有經驗的網友能給予指導。 #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寫入一個字節輸入:E2ROM地址,字節數據******************************************************/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();}/**********************************發送開始*****************************************/VOID start(VOID){a_scl=1;a_sda=1;a_sda=0;a_scl=0;a_scl=1;}/********************************************發送停止*******************************************/VOID stop(VOID){a_scl=0;a_sda=0;a_scl=1;a_sda=1;} /*********************************************發送反饋************************************************/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)}/**************************************發送無反饋********************************************/bit noack(VOID){int a_ack;a_scl=1;a_scl=1;a_scl=0;}/*******************************************發送****************************************************/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讀一個字節;輸入:EEROM地址;輸出:EEROM數據;********************************************/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讀寫驅動程序,//=-------------------------------------------------------------------------------/*模塊調用:讀數據:read(unsigned int address)寫數據:write(unsigned int address,unsigned char dd) dd為要寫的 數據字節*///------------------------------------------------------------------------------ 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) //起始函數{_nop_(); scl=0; sda=1; scl=1; _nop_(); sda=0; _nop_(); _nop_(); scl=0; _nop_(); _nop_(); sda=1;} //------------------------------------------------------------------------------#pragma disableVOID p24(VOID) //停止函數{sda=0; scl=1; _nop_(); _nop_(); sda=1;} //-----------------------------------------------------------------------------#pragma disableunsigned char rd24(VOID) /////////////////從24c16讀一字節數據{ 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寫一字節數據{ 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; ///////////////////////////////////接收器件地址確認信號 wd24(address); //////////////////////////// 寫數據地址 _nop_();_nop_();_nop_();_nop_(); scl=0;s24(); ///////////////////////////////////開始條件 wd24(0xa1); /////////////////////////////寫器件地址(讀命令) scl=0; dd=rd24(); //////////////////////////////////讀 一字節 p24(); ////////////////////////////////////停止條件 return(dd);}//------------------------------------------------------------------------------#pragma disableVOID write(unsigned int address,unsigned char dd){s24(); /////////////////開始條件 wd24(0xa0); ////////////////////////寫器件地址; scl=0; wd24(address); /////////////////////寫數據地址 scl=0; wd24(dd); //////////////////////////寫dd數據 scl=0; p24(); /////////////////////////停止條件; }
上傳時間: 2013-11-18
上傳用戶:墻角有棵樹
C51控制并口打印機實例:/* 沈陽新榮達電子 *//* 2004-12-7 */#include <reg52.h>#define uchar unsigned char#define uint unsigned int#define data_8 P0sbit BUSY = P1^2; //打印機 BUSY 接P1.2sbit STB = P1^0; //打印機 STB 接P1.0VOID print(uchar j) //打印子程序{ uchar i;while(BUSY){}; //BUSY=1,打印機忙,等待BUSY 為0 再發數data_8=j;STB=0;i++;i--;STB=1; //給出數據鎖存時鐘BUSY=1;}VOID main(VOID){BUSY = 1; //忙信號置高STB = 1; //選通信號置高print(0x1b); //打印機初始化命令print(0x38);print(0x04);for(;;){print(0xd0); //發送漢字內碼“新榮達”print(0xc2);print(0xc8);print(0xd9);print(0xb4);print(0xef);print(0x0d); //換行}}
上傳時間: 2013-11-13
上傳用戶:lwq11
微型打印機的C語言源程序:微型打印機的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) //打印機初始化子程序 { 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;//定義解碼的個數 unsigned long use_data=0;//定義16位的用戶碼,只用到13位 unsigned long use_code=0;//定義16位的用戶反碼,只用到13位 unsigned long data=0;//定義16位數據碼,包括8位數據碼和反碼 unsigned char data_h=0;//數據反碼 unsigned char data_l=0;//數據碼 _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//////// ////數據碼解碼開始////data_l為用戶碼,data_h為數據碼反碼//////////// 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); } ////數據碼解碼結束//////////////////////////////////////////////// data_l=data; data_h=data>>8; ///用戶碼////// use_data>>=3; use_code>>=3; use_code=~use_code; //////// ////如果用戶碼等與0x11c并且數據碼和數據反碼都校驗一致,解碼成功 //if((~data_h==data_l)&&use_data==0x11c)//使用用戶碼 //跳過用戶碼 if(~data_h==data_l)//如果數據碼和數據反碼(取反后)相等,解碼正確 { _nop(); r_data=data_l;//r_data為解出的最終數據碼 } //否則解碼不成功 _nop(); _nop();error: //r_data=nocode; _nop(); _nop(); _nop();}
上傳時間: 2014-03-27
上傳用戶:shenlan