?? dianzi.c
字號:
#include <reg51.h>
#include <intrins.h>
#define nop() _nop_()
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
#define USED_BYTE_QTY_IN_ONE_SECTOR 128
sfr ISP_DATA = 0xe2; /* ISP_DATA DATA 0E2H; 匯編程序定義 or EQU */
sfr ISP_ADDRH = 0xe3; /* ISP_ADDRH DATA 0E3H; 匯編程序定義 */
sfr ISP_ADDRL = 0xe4; /* ISP_ADDRL DATA 0E4H; 匯編程序定義 */
sfr ISP_CMD = 0xe5; /* ISP_CMD DATA 0E5H; 匯編程序定義 */
sfr ISP_TRIG = 0xe6; /* ISP_TRIG DATA 0E6H; 匯編程序定義 */
sfr ISP_CONTR = 0xe7; /* ISP_CONTR DATA 0E7H; 匯編程序定義 */
#define READ_Command 0x01 /* 字節讀應用程序區和數據存儲區 */
#define PROGRAM_Command 0x02 /* 字節編程應用程序區和數據存儲區 */
#define ERASE_Command 0x03 /* 扇區擦除應用程序區和數據存儲區 */
#define ERROR 0 //錯誤
#define OK 1 //正確
uchar xdata protect_buffer[USED_BYTE_QTY_IN_ONE_SECTOR];
//typedef unsigned char INT8U; /* 8 bit 無符號整型 */
//typedef unsigned int INT16U; /* 16 bit 無符號整型 */
uchar Write_flash(uint begin_addr, uint counter, uchar array[]);
uchar Read_flash(uint begin_addr, uint counter, uchar array[]); //讀FLASH
// #define DEBUG_AP_Memory_Begin_Sector_addr 0x0000
// #define DEBUG_AP_Memory_End_Sector_addr 0x7e00
// #define DEBUG_AP_Memory_End_Byte_addr 0x7fff
// #define DEBUG_Data_Memory_Begin_Sector_addr 0x8000
// #define DEBUG_Data_Memory_End_Sector_addr 0xf600
// #define DEBUG_Data_Memory_End_Byte_addr 0xf7ff
#define WAIT_TIME 0x00
sbit SCLK = P2^5;//1302端口設置
sbit IO = P2^6;
sbit RST = P2^7;
sbit RECLED = P2^0;//1420端口設置
sbit PLAYL = P2^1;
sbit ADCCS=P3^3; //AD0832端口設置
sbit ADCCLK=P3^4;
sbit ADCDO=P3^5;
sbit DIN = P2^2; //7219端口設置
sbit LOAD = P2^3;
sbit CLK = P2^4;
uint Con_Data = 0;
float Vref = 4.5;
void Display( void );
void delay( uchar n );
void WriteByte( uchar D );
void Keyboard();
void naozhong();
uchar ReadByte( void );
//sbit P3_4 = P3^4;
void WriteDate( uchar Add , uchar Date );
uchar ReadDate( uchar Add );
void SetTime( uchar Hour , uchar Min , uchar Sec );
uchar hour,min,sec,yuanma= 20,k;
bit keybuttflag=1,keybuttmima=0;
bit flag=0;
void GetTime( void );
uchar scan();
//code uchar DispCode[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
uchar DispBuf[8]={1,2,3,4,5,6,7,8};
uchar cunmima[6]={0,0,0,0,0,0};
uchar naoling[5][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
uchar fangyin[9];
//sbit LED = P1^0;
void Write7129( uchar Add, uchar Data );
void Init7219( void );
void desips7219();
void DelayMs( uchar n );
void temp1();
void int0();
void Init1302(void);
void Data_Process(void);
uchar Re_Data();
uint code Play_temp[19] = {0x00,0x04,0x08,0x0c,0x10,0x14,0x17,0x1b,0x1f,0x22,0x26,0x2a,
0x2d,0x31,0x35,0x40,0x44,0x48,0x4c};//"0~10,點、分、秒、現在時間是、零晨、上午,晚上、下午、"
uchar temp[10] = {14,1,2,3,4,5,6,0,0,0}; //'現在時間是' '上午/下午/晚上/零晨' '0~10'
//sbit PLAYL = P0^7;
uchar counter,Play_counter,ctr_temp=1,Play_num; //報時
void ProcessPlay();
main()
{
yuanma= 20;
delay( 100 );
Init1302( );
Init7219( );
GetTime();
delay( 100 );
int0();
temp1();
while(1)
{
Data_Process();
GetTime();
Keyboard();
naozhong();
}
}
void desips7219()
{
Write7129( 0x01 , DispBuf[0] );
Write7129( 0x02 , DispBuf[1]|0x80 );
Write7129( 0x03 , DispBuf[2] );
Write7129( 0x04 , DispBuf[3]|0x80 );
Write7129( 0x05 , DispBuf[4] );
Write7129( 0x06 , DispBuf[5]|0x80 );
Write7129( 0x07 , DispBuf[6] );
Write7129( 0x08 , DispBuf[7] );
}
void temp1()
{
EA = 1;
PT0 = 1;
TR0= 1;
TMOD = 0X19;
TL0 = 0x00;
TH0 = 0x00;
}
void int0()
{
EA = 1;
EX0 = 1;
PX0 = 1;
IT0 = 1;
}
/************************************************************
1302設置
************************************************************/
void delay( uchar n )
{
uchar i;
while( n-- )
{
for( i = 0 ; i < 125 ; i++ )
;
}
}
uchar ReadByte( void )
{
uchar i, sbuf = 0;
IO=1;
for( i = 0 ; i < 8 ; i++)
{
SCLK = 1;
_nop_();
SCLK = 0;
sbuf = sbuf >> 1;
if(IO == 1)
sbuf = sbuf | 0x80;
}
return sbuf;
}
void WriteByte( uchar D )
{
uchar i;
for( i = 0 ; i < 8 ; i++ )
{
IO = 1;
if( (D & 0x01) != 0x01)
IO = 0;
SCLK = 0;
_nop_();
_nop_();
SCLK = 1;
D >>= 1 ;
}
}
void WriteDate( uchar Add , uchar Date )
{
SCLK = 0 ;
RST = 0 ;
_nop_();
_nop_();
RST = 1 ;
_nop_();
WriteByte( Add );
WriteByte( Date );
}
uchar ReadDate( uchar Add )
{
uchar Rec;
SCLK = 0 ;
RST = 0 ;
_nop_();
_nop_();
RST = 1 ;
_nop_();
WriteByte( Add );
Rec = ReadByte();
return Rec;
}
void Init1302(void)
{
WriteDate( 0x8e , 0);
WriteDate( 0x90 , 0xab);
}
void SetTime( uchar Hour , uchar Min , uchar Sec )
{
WriteDate( 0x84 , Hour);
WriteDate( 0x82 , Min);
WriteDate( 0x80 , Sec);
WriteDate( 0x8c , 0x08);
WriteDate( 0x88 , 0x05);
WriteDate( 0x86 , 0x13);
}
void GetTime( void )
{
uchar Time;
Time = ReadDate( 0x85 );
DispBuf[0] = (Time&0xf0) >> 4;
DispBuf[1] = Time & 0x0f;
Time = ReadDate( 0x83 );
DispBuf[2] = (Time&0xf0) >> 4;
DispBuf[3] = Time & 0x0f;
Time = ReadDate( 0x81 );
DispBuf[4] = (Time&0xf0) >> 4;
DispBuf[5] = Time & 0x0f;
desips7219();
}
/********************************************************* 7219設置
*********************************************************/
void Write7129( uchar Add, uchar Data )
{
uint Send;
uchar i;
Send = Add*0x100 + Data;
LOAD = 1;
CLK = 0;
_nop_();
_nop_();
LOAD = 0;
for( i = 0 ; i < 16 ; i++ )
{
CLK = 0;
DIN = 0;
if( (Send & 0x8000) != 0 )
DIN = 1;
_nop_();
_nop_();
_nop_();
CLK = 1;
Send = Send << 1 ;
}
_nop_();
LOAD = 1;
}
void Init7219( void )
{
Write7129( 0x0c , 0x01 ); //開啟
// Write7129( 0x09 , 0x00 ); //譯碼方式 不譯碼
Write7129( 0x09 , 0xff ); //譯碼方式 B譯碼
Write7129( 0x0a , 0x08 ); //亮度
Write7129( 0x0b , 0x07 ); //掃描界限
}
void DelayMs( uchar n )
{
uchar i;
while( n-- )
{
for( i = 0 ; i < 125 ; i++ )
;
}
}
/*******************************************************
AD0832驅動
********************************************************/
uchar adc()
{
uchar i;
uint rec=0,rec1=0;
ADCCS=1;
ADCCLK=0;/*1*/
// ADCDO=1;
_nop_();
_nop_();
_nop_();
ADCCS=0;
_nop_();
_nop_();
_nop_();
ADCDO=1;
_nop_();
_nop_();
_nop_();
ADCCLK=1;/*1*/
_nop_();
_nop_();
ADCCLK=0;
_nop_();
_nop_();
_nop_();
ADCDO=1;
_nop_();
_nop_();
_nop_();
_nop_();
ADCCLK=1;/*2*/
_nop_();
_nop_();
ADCCLK=0;
_nop_();
_nop_();
ADCDO=0;
_nop_();
_nop_();
_nop_();
_nop_();
ADCCLK=1;/*3*/
_nop_();
_nop_();
ADCCLK=0;
_nop_();
_nop_();
ADCDO=1;
for(i=0;i<8;i++)
{
ADCCLK=1;
_nop_();
_nop_();
_nop_();
_nop_();
ADCCLK=0;
_nop_();
_nop_();
_nop_();
_nop_();
rec<<=1;
if( ADCDO == 1 )
rec=rec|0x01;
}
ADCDO=1;
for(i=0;i<8;i++)
{
ADCCLK=0;
_nop_();
_nop_();
_nop_();
_nop_();
rec1>>=1;
if( ADCDO == 1 )
rec1=rec1|0x80;
ADCCLK=1;
_nop_();
}
_nop_();
_nop_();
ADCCS=1;
if( rec1 == rec )
return rec;
else
return 0;
}
void Data_Process(void)
{
float tt;
uint dispnmber;
tt = ((adc()*Vref)/255.0-2)*6;//
dispnmber =0x0f- tt;
Write7129( 0x0a , dispnmber );
DispBuf[6]=dispnmber/10;
DispBuf[7]=dispnmber%10;
//desips7219();
}
/*****************************************************
1420
*****************************************************/
void write1420(uchar Add)
{
P0 = Add;
_nop_();
_nop_();
_nop_();
PLAYL = 0;
_nop_();
_nop_();
_nop_();
//PLAYL = 1;
}
/*******************************************************
矩形鍵盤
********************************************************/
uchar scan()
{
uchar a,b,c,i;
P1=0xf0;
if((P1&0xf0)!=0xf0)
{ delay(2);
if((P1&0xf0)!=0xf0)
{ c=0xfe;
a=0;
while((c&0x10)!=0)
{ P1=c;
if((P1&0x10)==0){b=0;break;}
else
{ if((P1&0x20)==0){b=4;break;}
else
{ if((P1&0x40)==0){b=8;break;}
else
{
if((P1&0x80)==0){b=12;break;}
}
}
}
a++;
c<<=1;
}
c=a+b;
delay(1);
P1=0xf0;
while((P1&0xf0)!=0xf0);
return(c);
}
}
if(yuanma != 20)
{
i=yuanma;
yuanma=20;
return i;
}
else return(20);
}
/**********************************************************
按鍵功能
***********************************************************/
void Keyboard()
{
uchar key10=0;
static i,Cnt=0,nex = 0;
static bit PassOk = 0;
k = scan();
switch( k )
{
case 0:
if((PassOk==1)||(key10==1))
{
k = 0;
}
else
{
ProcessPlay();
}break;
case 10: //輸入密碼
setcode:
DispBuf[0]= 10;
DispBuf[1]= 10;
DispBuf[2]= 10;
DispBuf[3]= 10;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -