51/AVR单片机技术驿站!  <在线翻译>     便利工具    特色网址   无弹窗、无插件的绿色站点...  英才招聘   学历查询  喜欢>>收藏我站 

当前位置:首页 > 取经杂谈 > 详细内容
几个常见的C语言例程【精】
发布时间:2009/6/4  阅读次数:643  字体大小: 【】 【】【

作者:admin        文章来源:非本站原创

万年历星期的算法(C语言)

#include <stdio.h>

code unsigned char WeekTab[] = {//闰年月星期表
(3 << 5) + 31,//1月
(6 << 5) + 29,//2月
(0 << 5) + 31,//3月
(3 << 5) + 30,//4月
(5 << 5) + 31,//5月
(1 << 5) + 30,//6月
(3 << 5) + 31,//7月
(6 << 5) + 31,//8月
(1 << 5) + 30,//9月
(4 << 5) + 31,//10月
(0 << 5) + 30,//11月
(2 << 5) + 31 //12月
};

/*------------------------------------------------------------------------------
----------
2000年~2099年星期算法
--------------------------------------------------------------------------------
---------*/
unsigned char WeekDay20(unsigned char y, unsigned char m, unsigned char d)
{
unsigned char week, day;
day = WeekTab[m - 1];//月表
week = day >> 5;//月星期数
day &= 0x1f;//月天数
if ((m < 3) && (y & 0x03)){//平年
if (m == 2) day--;//平年月天数
week++;//平年月表+1
}
y = y + (y >> 2);//年+年/4
week = (week + y + d + 2) % 7;//(星期=年+年/4+月表+2日)%7
return (week << 5) | day;//返回星期和月天数
}

/*------------------------------------------------------------------------------
----------
0000年~9999年星期算法
--------------------------------------------------------------------------------
---------*/
unsigned char WeekDay(unsigned char c, unsigned char y, unsigned char m,
unsigned char d)
{
unsigned char week, day;
c &= 0x03;//百年%4
c = c | (c << 2);//百年%4*5
day = WeekTab[m - 1];//月表
week = day >> 5;//月星期数
day &= 0x1f;//月天数
if ((m < 3) && !((c == 0) | (y & ((y & 0x03) == 0)))){//平年
if (m == 2) day--;//平年月天数
week++;//平年月表+1
}
y = y + (y >> 2);//年+年/4
week = (week + c + y + d + 2) % 7;//(星期=百年%4*5+年+年/4+月表+日+2)%7
return (week << 5) | day;//返回星期和月天数
}

unsigned char BcdToBin(unsigned char val)
{
val = (val >> 4) * 10 + (val & 0x0f);//将BCD码转换为10进制数
return val;//返回10进制数
}

void main(void)
{
unsigned char c, y, m, d;
unsigned char cx, yx, mx, dx;
unsigned char WDay, Week, Day;
/*---------------------------------------------------------
0001年1月1日 星期天
---------------------------------------------------------*/
c = 0x00;
y = 0x01;
m = 0x01;
d = 0x01;
cx = BcdToBin(c);//百年
yx = BcdToBin(y);//年
mx = BcdToBin(m);//月
dx = BcdToBin(d);//日
WDay = WeekDay(cx, yx, mx, dx);//取星期和月天数
Week = WDay >> 5;//得到星期
Day = WDay & 0x1f;//得到最大月天数
/*---------------------------------------------------------
1918年1月21日 星期一
---------------------------------------------------------*/
c = 0x19;
y = 0x18;
m = 0x01;
d = 0x21;
cx = BcdToBin(c);//百年
yx = BcdToBin(y);//年
mx = BcdToBin(m);//月
dx = BcdToBin(d);//日
WDay = WeekDay(cx, yx, mx, dx);//取星期和月天数
Week = WDay >> 5;//得到星期
Day = WDay & 0x1f;//得到最大月天数
/*---------------------------------------------------------
9999年12月31日 星期天
---------------------------------------------------------*/
c = 0x00;
y = 0x01;
m = 0x01;
d = 0x01;
cx = BcdToBin(c);//百年
yx = BcdToBin(y);//年
mx = BcdToBin(m);//月
dx = BcdToBin(d);//日
WDay = WeekDay(cx, yx, mx, dx);//取星期和月天数
Week = WDay >> 5;//得到星期
Day = WDay & 0x1f;//得到最大月天数
/*---------------------------------------------------------
2004年6月18日 星期五
---------------------------------------------------------*/
c = 0x20;
y = 0x04;
m = 0x06;
d = 0x18;
cx = BcdToBin(c);//百年
yx = BcdToBin(y);//年
mx = BcdToBin(m);//月
dx = BcdToBin(d);//日
WDay = WeekDay20(yx, mx, dx);//取星期和月天数
Week = WDay >> 5;//得到星期
Day = WDay & 0x1f;//得到最大月天数
/*---------------------------------------------------------
2018年3月8日 星期四
---------------------------------------------------------*/
c = 0x20;
y = 0x18;
m = 0x03;
d = 0x08;
cx = BcdToBin(c);//百年
yx = BcdToBin(y);//年
mx = BcdToBin(m);//月
dx = BcdToBin(d);//日
WDay = WeekDay20(yx, mx, dx);//取星期和月天数
Week = WDay >> 5;//得到星期
Day = WDay & 0x1f;//得到最大月天数
while(1);
}

AM12864系列测试程序(C语言)

/********************************************/
/* AM12864系列测试程序 1.0 */
/* Designed by Andorin LCM R&D Group */
/********************************************/

#include <reg51.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
/********************************************/
/* AM12864系列测试程序 1.0 */
/* Designed by Andorin LCM R&D Group */
/********************************************/

/* Define the register command code */
#define Disp_On 0x3f
#define Disp_Off 0x3e
#define Col_Add 0x40
#define Page_Add 0xb8
#define Start_Line 0xc0
#define Lcd_Bus P1 //MCU P1<------> LCM
sbit Mcs=P2^3; //Master chip enable
sbit Scs=P2^4; //Slave chip enable
sbit Enable=P2^2; //6800 mode Enable single
sbit Di=P2^0; //Data or Instrument Select
sbit RW=P2^1; //Write or Read
sbit Lcd_Rst=P2^5; //Lcm reset
sbit Test_Key=P3^1;

char code ce[]={/*-- 文字: 测 --*/0x08,0x31,0x86,0x60,0x00,0xFE,0x02,0xF2,0x02,0xFE,0x00,0xF8,0x00,0x00,0xFF,0x00,
0x04,0xFC,0x03,0x00,0x80,0x47,0x30,0x0F,0x10,0x67,0x00,0x07,0x40,0x80,0x7F,0x00};
char code shi[]={/*-- 文字: 试 --*/0x40,0x42,0xDC,0x08,0x00,0x90,0x90,0x90,0x90,0x90,0xFF,0x10,0x12,0x1C,0x10,0x00,
0x00,0x00,0x7F,0x20,0x10,0x20,0x20,0x1F,0x10,0x10,0x01,0x06,0x18,0x20,0x78,0x00};
char code kai[]={/*-- 文字: 开 --*/0x40,0x42,0x42,0x42,0x42,0xFE,0x42,0x42,0x42,0x42,0xFE,0x42,0x42,0x42,0x42,0x00,
0x00,0x40,0x20,0x10,0x0C,0x03,0x00,0x00,0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00};
char code shi2[]={0x10,0x90,0x70,0x1F,0x12,0xF0,0x00,0x20,0x70,0x28,0x27,0x22,0x28,0x70,0x20,0x00,
0x40,0x21,0x12,0x0C,0x06,0x09,0x30,0x00,0x7F,0x21,0x21,0x21,0x21,0x7F,0x00,0x00};//始
char code wan[]={0x00,0x90,0x8C,0xA4,0xA4,0xA4,0xA5,0xA6,0xA4,0xA4,0xA4,0xA4,0x94,0x8C,0x04,0x00,
0x00,0x80,0x40,0x20,0x18,0x07,0x00,0x00,0x00,0x3F,0x40,0x40,0x40,0x70,0x00,0x00};//完
char code bi[]={0x00,0x00,0xFF,0x88,0x48,0x28,0x08,0x80,0x7F,0x88,0x88,0x88,0x84,0x84,0xE0,0x00,
0x04,0x04,0x04,0x04,0x04,0x04,0x04,0xFF,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00};//毕
char code yang[]={0x20,0x10,0x2C,0x23,0x6A,0xAA,0x2A,0xAA,0x6A,0x2A,0x2A,0x2A,0xEA,0x02,0x00,0x00,
0x00,0x10,0x11,0x15,0x15,0x15,0xFF,0x15,0x15,0x15,0x11,0x10,0x3F,0x40,0x30,0x00};
char code qi[]={0x40,0x20,0x58,0x47,0x54,0x54,0x54,0x54,0x54,0x54,0xD4,0x14,0x06,0x04,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x40,0x80,0x70,0x00};//气
char code nong[]={0x10 ,0x21 ,0x62 ,0x06 ,0x90 ,0x0C ,0x04 ,0x84,0x64 ,0x9F ,0x04 ,0x04 ,0x94 ,0xCC ,0x00 ,0x00,
0x04 ,0x04 ,0xFE ,0x01 ,0x04 ,0x02 ,0x01 ,0xFF, 0x40 ,0x21 ,0x06 ,0x09 ,0x10 ,0x30 ,0x10 ,0x00 };//浓
char code du[]={0x00 ,0x00 ,0x0FC ,0x24 ,0x24 ,0x24 ,0x0FC ,0xA5, 0xA6 ,0xA4 ,0x0FC ,0x24 ,0x34 ,0x26 ,0x04 ,0x00,
0x40 ,0x20 ,0x9F ,0x80 ,0x42 ,0x42 ,0x26 ,0x2A, 0x12 ,0x2A ,0x26 ,0x42 ,0x40 ,0xC0 ,0x40 ,0x00};//度
char code bi2[]={0x00, 0x00, 0xFE, 0x40, 0x40, 0x60, 0x40, 0x00,0xFF, 0x80, 0x40, 0x20, 0x30, 0x00, 0x00, 0x00,
0x00, 0x40, 0x7F, 0x20, 0x20, 0x10, 0x10, 0x00,0x3F, 0x40, 0x40, 0x40, 0x40, 0x40, 0x78, 0x00};//比
char code fen[]={0x00 ,0x80 ,0x40 ,0x20 ,0x98 ,0x86 ,0x80 ,0x80,0x83 ,0x8C ,0x90 ,0x20 ,0xC0 ,0x80 ,0x80 ,0x00,
0x01 ,0x00 ,0x80 ,0x40 ,0x20 ,0x1F ,0x00 ,0x40, 0x80 ,0x40 ,0x3F ,0x00 ,0x00 ,0x01 ,0x00 ,0x00 };// 分
char code bai[]={0x02 ,0x02 ,0x02 ,0xE2 ,0x22 ,0x32 ,0x2A ,0x26,0x22 ,0x22 ,0x22 ,0xF2 ,0x22 ,0x03 ,0x02 ,0x00,
0x00 ,0x00 ,0x00 ,0xFF ,0x42 ,0x42 ,0x42 ,0x42, 0x42 ,0x42 ,0x42 ,0xFF ,0x00 ,0x00 ,0x00 ,0x00};// 百
char code wei[]={

0x20 ,0x20 ,0x20 ,0x22 ,0x24 ,0x2C ,0x20 , 0xE0,
0x3F ,0x20 ,0x20 ,0x20 ,0x20 ,0xF0 ,0x20 ,0x00,
0x00 ,0x40 ,0x20 ,0x10 ,0x08 ,0x04 ,0x03 ,0x00,
0x02 ,0x04 ,0x4C ,0x80 ,0x40 ,0x3F ,0x00 ,0x00
};//为

  


/*------------------延时子程序-----------------------------*/
void delay(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++) ;
for(j=0;j<10;j++) ;
}
/*------------------写命令到LCD------------------------------*/
void write_com(unsigned char cmdcode)
{
do{delay(0);}while(P0^7==1);
Di=0;
RW=0;
Lcd_Bus=cmdcode;
delay(0); //原来是delay(0)
Enable=1;
delay(0);
Enable=0;

}
/*-------------------写数据到LCD----------------------------*/
void write_data(unsigned char Dispdata)
{ do{delay(10);}while(P0^7==1);
Di=1;
RW=0;
Lcd_Bus=Dispdata;
delay(0);
Enable=1;
delay(0);
Enable=0;
}
/*-------------------读LCD数据----------------------------*/
unsigned char read_data()
{ //do{delay(100);}while(P0^7==1);
unsigned char tmpin;
Di=1;
RW=1;
delay(0);
Enable=1;
delay(0);
Enable=0;do{delay(100);}while(P0^7==1);
tmpin=Lcd_Bus;
return tmpin;
}
/*------------------显示边框-------------------------------*/
void frame()
{
unsigned char i;
Mcs=1;
Scs=1;
write_com(Page_Add+0);
write_com(Col_Add+0);
for(i=0;i<64;i++) write_data(0x01);
write_com(Page_Add+7);
write_com(Col_Add+0);
for(i=0;i<64;i++) write_data(0x80);
for(i=0;i<8;i++)
{
Mcs=0;
Scs=1;
write_com(Page_Add+i);
write_com(Col_Add+63);
write_data(0x55);
Mcs=1;
Scs=0;
write_com(Page_Add+i);
write_com(Col_Add+0);
write_data(0x55);
}
}
/*------------------清除内存---------------*/
void Clr_Scr()
{
unsigned char j,k;
Mcs=1;Scs=1;
write_com(Page_Add+0);
write_com(Col_Add+0);
for(k=0;k<8;k++){
write_com(Page_Add+k);
for(j=0;j<64;j++)write_data(0x00);
}
}
/*------------------显示12864图片---------------------------*/
void Disp_Img(unsigned char code *img)
{
unsigned char j,k;
for(k=0;k<8;k++){
Mcs=1;Scs=0;delay(0);
write_com(Page_Add+k);
write_com(Col_Add+0);

for(j=0;j<64;j++) write_data(img[k*128+j]);
Mcs=0;Scs=1; delay(0);
write_com(Page_Add+k);
write_com(Col_Add+0);
for(j=64;j<128;j++) write_data(img[k*128+j]);
}
}
/*------------------显示横---------------------------*/
void Disp_H_Line()
{
unsigned char i,j;
Mcs=1;
Scs=1;
write_com(Disp_On);
write_com(Page_Add);
write_com(Start_Line);
write_com(Col_Add);
for(j=0;j<8;j++)
{
write_com(Page_Add+j);
write_com(Col_Add+0);
// write_com(Start_Line+0);
for(i=0;i<64;i++)write_data(0x00);
}
}

/*------------------显示列-----------------------------*/
void Disp_V_Line()
{
unsigned char i,j;
write_com(Disp_On);
write_com(Page_Add);
write_com(Start_Line);
write_com(Col_Add);
for(j=0;j<8;j++)
{
write_com(Page_Add+j);
write_com(Col_Add+0);
write_com(Start_Line+0);
for(i=0;i<32;i++)
{
write_data(0x00);
write_data(0x55);
}
}
}
/*------------------棋盘-----------------------------*/
void Disp_Cross_Line(unsigned char mode)
{
unsigned char i,j;
unsigned char data1,data2;
if(mode==0) {data1=0xaa;data2=0x55;}
else if(mode==1) {data1=0x55;data2=0xaa;}
else return;
Mcs=1;Scs=1;
write_com(Disp_On);
write_com(Page_Add);
write_com(Start_Line);
write_com(Col_Add);
for(j=0;j<8;j++)
{
write_com(Page_Add+j);
write_com(Col_Add+0);
write_com(Start_Line+0);
for(i=0;i<32;i++){
write_data(data1);
write_data(data2);
}
}
}

/*---------------------指定位置显示汉字16*16-----------------------*/
void hz_disp16(unsigned char pag,unsigned char col, unsigned char code *hzk)
{
unsigned char j=0,i=0;
for(j=0;j<2;j++){
write_com(Page_Add+pag+j);
write_com(Col_Add+col);
for(i=0;i<16;i++) write_data(hzk[16*j+i]);
}
}

/*------------------初始化LCD屏--------------------------*/
void init_lcd()
{ Lcd_Rst=0;
delay(100);
Lcd_Rst=1;
delay(100);
Mcs=1;
Scs=1;/////////////////////////////////////////////////////////
delay(100);
write_com(Disp_Off); /////////////////////
write_com(Page_Add);
write_com(Start_Line);
write_com(Col_Add);
write_com(Disp_On);
}

/*------------------------------信息显示------------------------*/
void Msg(int flg)
{
Clr_Scr();
frame();
if(flg==0){ //显示"测试开始"
Mcs=1;Scs=0;
hz_disp16(3,32,ce);
delay(2000);
hz_disp16(3,48,shi);
delay(2000);
Mcs=0;Scs=1;
hz_disp16(3,0,kai);
delay(2000);
hz_disp16(3,16,shi2);
delay(2000);
}
else if(flg==1){ //显示"测试完毕"
Mcs=1;Scs=0;
hz_disp16(0,32,ce);
hz_disp16(0,48,shi);
Mcs=0;Scs=1;
hz_disp16(0,0,wan);
hz_disp16(0,16,bi);
Mcs=1;Scs=0;
hz_disp16(3,0,yang);
hz_disp16(3,16,qi);
hz_disp16(3,32,nong);
hz_disp16(3,48,du);
Mcs=0;Scs=1;
hz_disp16(3,0,bai);
hz_disp16(3,16,fen);
hz_disp16(3,32,bi2);
hz_disp16(3,48,wei);


}
}

/*--------------------------------键盘扫描-------------------------*/
void Key_Scan()
{
while(Test_Key);
while(~Test_Key);
}

/*--------------------主程序---------------------------------*/

main()
{
unsigned char i=0;
while(1){
// Key_Scan();
init_lcd();
//Disp_Img(FirstPage);
//delay(10000);
// Key_Scan();
Msg(0);
delay(10000);
Disp_H_Line();
delay(10000);
Disp_V_Line();
delay(10000);

Msg(1);
delay(10000);
Clr_Scr();
//while(1);
}
}

51单片机串口调试程序一列

/*定义为中断方式串口处理*/
#define INTERSENDSTR
/*不为8032系列芯片 */
//#define CHIP_8032

#include <absacc.h>
#include <reg51.h>
#include <string.h>
#include "err.h"


#ifdef INTERSENDSTR
unsigned char xdata sSendComBuf[256],sRecComBuf[256];
unsigned char data bOut,bIn,bSout,bSin;
#else
#define DEFSENDCHAR TI=0;SBUF=ACC;while(!TI);TI=0;
#endif
//bps设置 9600就是 PSetBps(96)(11.0592Mhz)

unsigned char PSetBps(unsigned int bps) SMALL
{
unsigned int t1;
unsigned char t2;
#ifdef INTERSENDSTR
/*如果使用中断方式发送数据,必须等待发送数据完毕才能更换波特率 */
while(bSin!=bSout)
{
;
}
ES=0;
#endif

#ifdef CHIP_8032
if(3456%bps)
{
return ERR_SET_BPS;
}
t1=3456/bps;
t1--;t1=~t1;
RCAP2H=t1/256;RCAP2L=t1;
T2MOD=0x00;
/*使用定时器2做波特率发生器 */
T2CON=0x34;/*TF2=0;EXF2=0;RCLK=1;TCLK=1;EXEN2=0;TR2=1;C/T2=0;CP/RL2=0
*/
#else
t1=576/bps;
if((576%bps)||((t1==0))||(t1>=2*256))
{
return ERR_SET_BPS;
}
if(t1>256)
{
PCON=00;
t2=256-(288/bps);
}
else
{
PCON=0x80;
t2=256-t1;
}
TH1=t2;TL1=t2;
TR1=1;
#endif
/*模式3*/
SM0=0;
RI=0;TI=0;
REN=1;
SM1=1;
SM2=1;
#ifdef INTERSENDSTR
bOut=bIn;
bSout=bSin;
ES=1;
#endif
return OK;
}
void PSendChar(unsigned char ch) SMALL
{

#ifdef INTERSENDSTR
unsigned char tch;

tch=bSin+1;
while(tch==bSout)
{
;
}
sSendComBuf[bSin]=ch;
ES=0;
if((bSout==bSin))
{
SBUF=sSendComBuf[bSin];
bSin++;
}
else
{
bSin++;
}
ES=1;
#else
ACC=ch;
DEFSENDCHAR;
#endif
}
unsigned char PGetChar() SMALL
{
unsigned char ch;
#ifdef INTERSENDSTR
ch=sRecComBuf[bOut];
bOut++;
return ch;
#else
ch=SBUF;
RI=0;
#endif
}
bit PCharInCom() SMALL
{
#ifdef INTERSENDSTR
if(bIn!=bOut)
{
return 1;
}
return 0;
#else
return RI;
#endif
}
void PSendString(unsigned char *st) SMALL
{
while(*st)
{
PSendChar(*st);
st++;
}
}
void PSendArray(unsigned char *st,unsigned int len) SMALL
{
unsigned int ii;
for(ii=0;ii<len;ii++)
{
PSendChar(st[ii]);
}
}
#ifdef INTERSENDSTR
void PSerialInt() interrupt 4
{
if(RI)
{
sRecComBuf[bIn]=SBUF;
bIn++;
RI=0;
}
if(TI)
{
TI=0;
if(bSout!=bSin)
{
bSout++;
}
if(bSout!=bSin)
{
SBUF=sSendComBuf[bSout];
}
}
}
#endif

TLC5618 12位D/A转换器驱动函数(C语言)
哈尔滨理工大学测通学院电学实验中心   2005-9-16    


void TLC5618(unsigned int vcon) //TLC5618DAC子程序,三线串行方式
{char i;
dac_val=vcon;
svcon=vcon|0x8000; //vcon最高位置1,选择TLC5618的A通道
CS=0; // 置5618的CS=0,允许片选
for(i=0;i<16;i++)
{ CLK=1;
if (svcon&0x8000)
{
DIN=1;
}
else
{
DIN=0;
}
svcon<<=1;
CLK=0;
}
CS=1; //禁止片选

}

ADS7816 12位AD转换器驱动程序函数(C语言)

uint ads7816()
{
uint result;
uchar i;
uchar al=0,ah=0;
DOUT=1;
DCS=0;

while(DOUT==1)
{ DCLK=1;
DCLK=0;
}
for(i=0;i<4;i++)
{
DCLK=1;
DCLK=0;
ah=(ah<<1);
if(DOUT)
{
ah=ah|0x01;
}
}
for(i=0;i<8;i++)
{
DCLK=1;
DCLK=0;
al=(al<<1);
if(DOUT)
{
al=al|0x01;
}
}
DCS=1;
result=ah;result<<=8;result|=al;//得到AD值
return (result);
}

HD7279显示驱动演示(C语言)

#include <reg51.h>
//*** 函数定义 ***
void long_delay(void); // 长延时
void short_delay(void); // 短暂延时
void delay10ms(unsigned char); // 延时10MS
void write7279(unsigned char, unsigned char); // 写入到HD7279
unsigned char read7279(unsigned char); // 从HD7279读出
void send_byte(unsigned char); // 发送一个字节
unsigned char receive_byte(void); // 接收一个字节
//*** 变量及I/O口定义 ***
unsigned char digit[5];
unsigned char key_number, j, k,m0,m1,m2,m3;
unsigned char n31=4,n01,sure;
unsigned int set,tmr;
sbit cs=P0^4; // cs 连接 P1.4
sbit clk=P0^5; // clk 连接于 P1.5
sbit dat=P0^6; // dat 连接于 P1.2
sbit key=P0^7; // key 连接于 P1.3
//****** HD7279A 指令 ******
#define CMD_RESET 0xa4 // 复位
#define CMD_TEST 0xbf // 测试
#define DECODE0 0x80 // 方式0译码
#define DECODE1 0xc8 // 方式1译码
#define CMD_READ 0x15 // 读键盘指令
#define UNDECODE 0x90 // 方式2译码
#define RTL_CYCLE 0xa3 // 循环左移
#define RTR_CYCLE 0xa2 // 循环右移
#define RTL_UNCYL 0xa1 // 左移指令
#define RTR_UNCYL 0xa0 // 右移指令
#define ACTCTL 0x98 // 消隐指令
#define SEGON 0xe0 // 段亮指令
#define SEGOFF 0xc0 // 段关闭指令
#define BLINKCTL 0x88 // 闪烁指令
void write7279(unsigned char cmd, unsigned char dta) //写入7279
{
send_byte (cmd);
send_byte (dta);
}

unsigned char read7279(unsigned char command) //读7279
{
send_byte(command);
return(receive_byte());
}

void send_byte( unsigned char out_byte) //发送字节
{
unsigned char i;
cs=0;
long_delay();
for (i=0;i<8;i++)
{
if (out_byte&0x80)
{
dat=1;
}
else
{
dat=0;
}
clk=1;
short_delay();
clk=0;
short_delay();
out_byte=out_byte*2;
}
dat=0;
}

unsigned char receive_byte(void) //接收字节
{
unsigned char i, in_byte;
dat=1;
long_delay();
for (i=0;i<8;i++)
{
clk=1;
short_delay();
in_byte=in_byte*2;
if (dat)
{
in_byte=in_byte|0x01;
}
clk=0;
short_delay();
}
dat=0;
return (in_byte);
}

void long_delay(void) //长延时
{
unsigned char i;
for (i=0;i<0x30;i++);
}

void short_delay(void)
{
unsigned char i;
for (i=0;i<8;i++);
}

// ************************* 延时 n*10ms *************************
void delay10ms(unsigned char time)
{
unsigned char i;
unsigned int j;
for (i=0;i<time;i++)
{ for(j=0;j<0x390;j++);

/* {
if (!key)
{
key_int();
}
} */
}
}


//////////////////////////////////////////////////////////////
//7279显示4位数//
void disp4(unsigned int dcode)
{
unsigned char sl;
sl=dcode/1000;
write7279(0x80,0x00+sl);
sl=dcode/100%10;
write7279(0x81,0x00+sl);
sl=dcode/10%10;
write7279(0x82,0x00+sl);
sl=dcode%10;
write7279(0x83,0x00+sl);
}
/////////最后一位带小数点四位数显示///////////////
void disp4p(unsigned int dcode)
{
unsigned char sl;
sl=dcode/1000;
write7279(0x80,0x00+sl);
sl=dcode/100%10;
write7279(0x81,0x00+sl);
sl=dcode/10%10;
write7279(0x82,0x00+sl);
sl=dcode%10;
write7279(0x83,0x00+(sl-0x10));
}
//////按键设置/////////////////////////////////////////////////


//*** 主程序 ***
main()
{
while (1)
{ for (tmr=0;tmr<0x2000;tmr++) ; // 上电延时
send_byte(CMD_RESET); // 复位HD7279A

write7279(0x80,0x00); // 显示0000
write7279(0x81,0X00); //
write7279(0x82,0x00); //
write7279(0x83,0x00);

while(1)
{ unsigned int i;
disp4(888);
for(i=0;i<2;i++)
{
delay10ms(100);
}
disp4p(888);
for(i=0;i<2;i++)
{
delay10ms(100);
}


}
}
}

TLC0831A8位A/D转换器驱动程序(C语言)

#include "reg51.h"
#include "intrins.h"
sbit T0831_CS=P2^0;//片选I/O端口
sbit T0831_SK=P1^0;//时钟I/O端口
sbit T0831_DO=P3^6;//数据I/O端口


unsigned char T0831Read()
{
unsigned char i,DataSoute;
T0831_CS=1;
Delay(10);
T0831_SK=1;
T0831_CS=0;
T0831_SK=0;
_nop_();
for(i=0;i<8;i++)
{
T0831_SK=1;
T0831_SK=0;
if(T0831_DO) DataSoute++;
DataSoute<<=1;
}
T0831_CS=1; T0831_SK=1;
Delay(20);
return(DataSoute);
}
/*分析:
该芯片采用传统的SPI口的规约形式,是一个非标准的SPI封装。
注:使用该程序时注意改变芯片各个接口的修改。注意屏蔽主函数。
*/
main()
{
unsigned char Data;
Data=T0831Read();
while(1);
}


DS18B20驱动程序(C语言)

//#include"reg51.h"

sbit DQ =P1^4; //定义通信端口

//延时函数
/*
void delay(unsigned int i)
{
while(i--);
}
*/
//初始化函数
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
delay(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay(80); //精确延时 大于 480us
DQ = 1; //拉高总线
delay(14);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay(20);
}

//读一个字节
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}

//写一个字节
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(5);
DQ = 1;
dat>>=1;
}
//delay(4);
}

//读取温度
ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
//t= tt*10+0.5; //放大10倍输出并四舍五入---此行没用
return(t);
}

main()
{
unsigned char i=0;
while(1)
{
i=ReadTemperature();//读温度
}
}

  

我要评论
  • 匿名发表
  • [添加到收藏夹]
  • 发表评论:(匿名发表无需登录,已登录用户可直接发表。) 登录状态:未登录
最新评论
所有评论[0]
    暂无已审核评论!

网站导航 管理登陆 ┊ 免责声明 问题反馈  友链说明
本站部分内容来自网络共享资源,如有冒犯您的权利请来信告之删除或纠正!
不得对本站进行复制、盗链或镜像,转载内容须获得同意或授权;欢迎友情链接、站务合作!

    我要报警 Alexa
 mcusy_cn#126.com (请把#改成@) 交流:522422171
本站学习交流群:138..158(高级群1-)、77930286(高级群2)、61804809(群3)
Copyright© MCUSY All Rights Reserved
本站网警备案号: WZ36040002485
  ICP备案证书号:粤ICP备09034963号