万年历星期的算法
发布时间:2024-11-08
发布时间:2024-11-08
万年历星期的算法
万年历星期的算法(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
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++) 位AD转换器驱动程序函数(C语言) 12
下一篇:软件测试之职业发展方向652