?? futer_2.c
字號(hào):
#include <intrins.h>
#include "STC12C2052AD.H"
#define uchar unsigned char
#define uint unsigned int
#define _nop() _nop_()
#define sector_day_addrh 0x10 //日存儲(chǔ)扇區(qū)首地址
#define sector_day_addrl 0x00
#define enable_isp 0x83
#define isp_byte_read 1
#define isp_byte_write 2
#define isp_sector_erase 3
#define isp_wait_time 0x03 //0x02 //0x03
#define sector_night_addrh 0x12 //夜存儲(chǔ)扇區(qū)首地址
#define sector_night_addrl 0x00
/********************************************
命令字
********************************************/
#define SYNC 0x80
#define V_Phase 0x81 //兩字節(jié)數(shù)據(jù)
#define Shutter 0x83
#define AES_MAX 0x84
#define AEC_ALC 0x85 //Bright_Level_Night
#define IRIS 0x86 //Bright_Level_Day
#define BLCC 0x87
#define BLC_LEVEL 0x88
#define AGC 0x89
#define Aperture_H 0x8a
#define Aperture_V 0x8b
#define Gamma 0x8c
#define Pedestal_Level 0x8d
#define Chroma 0x8e
#define CLR_SUPR 0x8f //Day & Night State
#define White_BAL 0x90
#define WB_ATW 0x91
#define MWB_R_Gain 0x92
#define MWB_B_Gain 0x93
#define Color_Killer 0x95
#define IR_Filter 0x96
#define CS_Level 0x97 //day_night_level
sbit re_485 = P3^3;
sbit press_key = P3^2;
sbit runidle_key = P3^7;
sbit sample = P1^7;
sbit blk_key = P1^6;
sbit mem_key = P1^4;
sbit day_key = P1^5;
sbit blk_led = P1^3;
sbit day_led = P1^2;
sbit mem_led = P1^1;
sbit run_idle_led = P1^0;
//uchar code futer_read[] ={0xA1,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x55};
//uchar code futer_write[] ={0xA1,0x00,0x00,0x0F,0x00,0x00,0x00,0x00,0x55};
static uchar idata rxd_buf[30]; //{0:A1起始碼 1:資料串長(zhǎng)度 2:版本號(hào) 3:SYNC 4:V_phase_L
// 5:V_phase_m 6:shutter 7:AES_MAX 8:Bright Level-Night 9:Bright Level-Day
// 10:blc 8:blc_level 9:AGC 10:Aperture-H 11:Aperure-V
//
static uchar idata txd_buf[9];
static uchar data data_buf=0;
static uint data timer_cnt;
//uchar data func_dat1,func_dat2;
//bit data_noavail=0;
//bit busy=0;
bit day_time=0;
bit blk_time=0;
bit trig_flag=0;
bit day_set_time=0;
bit key_flag=0;
bit over_flag=0;
void data_array(uchar cmd_code,uchar data1,uchar data2)
{
txd_buf[0]=0xa1;
txd_buf[1]=0x00;
txd_buf[2]=0x00;
txd_buf[3]=cmd_code;
txd_buf[4]=data1;
txd_buf[5]=data2;
txd_buf[6]=0x00;
txd_buf[7]=0x00;
txd_buf[8]=0x55;
}
void DelayMs(unsigned int i1)
{
unsigned int k1;
WDT_CONTR=0x3c;
for(;i1!=0;i1--)
{
k1=1500;
WDT_CONTR=0x3c;
while(k1--);
//WDI=~WDI;
}
}
void dlay(unsigned int x1)
{
for(;x1>0;x1--);
}
void wait_ri(unsigned char cnt)
{
uchar i,j;
WDT_CONTR=0x3c;
for(i=0;i<=cnt;i++)
{
for(j=0;j<=0xfd;j++)
{
//WDI=~WDI;
//wdi=~wdi;
//WDT_CONTR=wdt;
_nop();
if(RI==1)break;
}
if(RI==1)break;
}
//wdi=~wdi;
}
void sio_txd(uchar *txd_data)
{
uchar i;
re_485=1;
//wdi=~wdi;
TI=0;
for(i=0;i<=8;i++) //<=4
{
SBUF=*(txd_data+i);
dlay(500); //**********************************************************8
while(!TI)WDT_CONTR=0x3c; //wdi=~wdi;
TI=0;
}
re_485=0;
}
void send_data(void)
{
WDT_CONTR=0x3c;
data_array(SYNC,rxd_buf[3],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(V_Phase,rxd_buf[4],rxd_buf[5]);
sio_txd(txd_buf);
DelayMs(200);
data_array(Shutter,rxd_buf[6],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(AES_MAX,rxd_buf[7],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(AEC_ALC,rxd_buf[8],0x00); //Bright_Level_Night
sio_txd(txd_buf);
DelayMs(200);
data_array(IRIS,rxd_buf[9],0x00); //Bright_Level_Day
sio_txd(txd_buf);
DelayMs(200);
data_array(BLCC,rxd_buf[10],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(BLC_LEVEL,rxd_buf[11],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(AGC,rxd_buf[12],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(Aperture_H,rxd_buf[13],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(Aperture_V,rxd_buf[14],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(Gamma,rxd_buf[15],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(Pedestal_Level,rxd_buf[16],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(Chroma,rxd_buf[17],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(CLR_SUPR,rxd_buf[18],0x00); //Day & Night State
sio_txd(txd_buf);
DelayMs(200);
data_array(White_BAL,rxd_buf[19],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(WB_ATW,rxd_buf[20],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(MWB_R_Gain,rxd_buf[21],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(MWB_B_Gain,rxd_buf[22],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(Color_Killer,rxd_buf[24],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(IR_Filter,rxd_buf[25],0x00);
sio_txd(txd_buf);
DelayMs(200);
data_array(CS_Level,rxd_buf[26],0x00);
sio_txd(txd_buf);
DelayMs(500);
DelayMs(500);
DelayMs(500);
DelayMs(500);
//DelayMs(500);
//DelayMs(500);
//DelayMs(500);
//DelayMs(500);
//data_array(0x0f,0x00,0x00); //指定攝像機(jī)SAVE
//sio_txd(txd_buf);
//DelayMs(200);
}
//串口接收程序:
bit sio_receive(uchar *dat_rev)
{
uchar i;
bit sio_flag=0;
if(T1==1)TI=0;
do
{
wait_ri(200);
wait_ri(200);
wait_ri(200);
wait_ri(200);
wait_ri(200);
wait_ri(200);
wait_ri(200);
wait_ri(200);
wait_ri(200);
if(!RI)
{
sio_flag=0;
break;
}
else {
RI=0;
data_buf=SBUF;
//WDI=~WDI;
sio_flag=1;
}
}while ((data_buf^0xa1)!=0);
if(!sio_flag)return 0;
for(i=1;i<=29;i++)
{
wait_ri(200);
wait_ri(200);
wait_ri(200);
if(!RI){sio_flag=0;break;}
data_buf=SBUF;
RI=0;
dat_rev[i]=data_buf;
if(data_buf==0x55)break;
}
if(sio_flag&&(dat_rev[29]==0x55))return 1;
else return 0;
}
void ISP_IAP_enable(void)
{
EA = 0; /* 關(guān)中斷 */
ISP_CONTR = ISP_CONTR & 0x18; /* 0001,1000 */
ISP_CONTR = ISP_CONTR | isp_wait_time;
ISP_CONTR = ISP_CONTR | 0x80; /* 1000,0000 */
}
void close_isp(void)
{
ISP_CONTR=ISP_CONTR & 0x7f;
ISP_TRIG=0x00;
EA=1;
}
void eeprom_read(bit time_mode_r,uchar cnt_r)
{
//uchar data i=0;
if(time_mode_r)
{
ISP_ADDRH=sector_day_addrh;
ISP_ADDRL=sector_day_addrl+cnt_r;
}
else
{
ISP_ADDRH=sector_night_addrh;
ISP_ADDRL=sector_night_addrl+cnt_r;
}
ISP_IAP_enable();
ISP_CMD=ISP_CMD&0xf8;
ISP_CMD|=isp_byte_read;
ISP_TRIG=0x46;
ISP_TRIG=0xb9;
_nop();
data_buf=ISP_DATA;
rxd_buf[cnt_r]=data_buf;
//close_isp();
close_isp();
}
void eeprom_write(bit time_mode_w,uchar cnt_w)
{
if(time_mode_w)
{
ISP_ADDRH=sector_day_addrh;
ISP_ADDRL=sector_day_addrl+cnt_w;
}
else
{
ISP_ADDRH=sector_night_addrh;
ISP_ADDRL=sector_night_addrl+cnt_w;
}
ISP_IAP_enable();
ISP_CMD=ISP_CMD&0xf8;
ISP_CMD|=isp_byte_write;
data_buf=rxd_buf[cnt_w];
ISP_DATA=data_buf;
ISP_TRIG=0x46;
ISP_TRIG=0xb9;
_nop();
//close_isp();
close_isp();
}
void erase_sector(bit time_mode_e)
{
if(time_mode_e)
{
ISP_ADDRH=sector_day_addrh;
ISP_ADDRL=sector_day_addrl;
}
else
{
ISP_ADDRH=sector_night_addrh;
ISP_ADDRL=sector_night_addrl;
}
ISP_IAP_enable();
ISP_CMD=ISP_CMD&0xf8;
ISP_CMD|=isp_sector_erase;
ISP_TRIG=0x46;
ISP_TRIG=0xb9;
_nop();
close_isp();
}
void eeprom_data_r(bit tt_mode_r)
{
uchar j_r;
for(j_r=0;j_r<=29;j_r++)
{
eeprom_read(tt_mode_r,j_r);
DelayMs(5);
}
}
void eeprom_data_w(bit tt_mode_w)
{
uchar j_w;
for(j_w=0;j_w<=29;j_w++)
{
eeprom_write(tt_mode_w,j_w);
DelayMs(5);
}
}
void sio_init(void)
{
//wdi=~wdi;
TMOD=0x21;
SCON=0x50;
PCON=0x00;
TH0=TL0=0;
TH1=0xfd;
TL1=0xfd;
TR1=1;
EX0=1;
EA=1;
//wdi=~wdi;
}
void init_run(void)
{
if(over_flag)
{
day_time=blk_time=0;
over_flag=0;
}
if(!day_time&&!blk_time&&!runidle_key)
{
if(sample)
{
day_time=1; //
blk_time=0;
}
else
{
day_time=0;
blk_time=1;
}
timer_cnt=0;
TR0=1;
ET0=1;
}
}
void run_set(void)
{
if(runidle_key)
{
run_idle_led=1;
ET0=0;
TR0=0;
timer_cnt=0;
day_time=0;
blk_time=0;
}
else run_idle_led=0;
}
void go_set(void)
{
if(key_flag)
{
key_flag=0;
//day_time=0;
//blk_time=0;
day_led=1;
blk_led=1;
if(!mem_key)
{
if(runidle_key)
{
if(!day_key&&blk_key){day_set_time=1;day_led=0;}
else if(day_key&&!blk_key){day_set_time=0;blk_led=0;}
erase_sector(day_set_time);
data_array(0x10,0x00,0x00);
sio_txd(txd_buf);
if(sio_receive(rxd_buf))
{
eeprom_data_w(day_set_time);
mem_led=0;
DelayMs(500);
mem_led=1;
}
else
{
data_array(0x10,0x00,0x00);
sio_txd(txd_buf);
if(sio_receive(rxd_buf))
{
eeprom_data_w(day_set_time);
mem_led=0;
DelayMs(500);
mem_led=1;
}
else mem_led=1;
}
//init_run();
}
}
while(!press_key)WDT_CONTR=0x3c;
EX0=1;
}
}
void go_run(void)
{
//測(cè)試程序 eeprom_data_r(0);
if(sample&&!runidle_key) //運(yùn)行模式 //夜間采樣 //day_time=0
{
if(day_time&&!blk_time)
{
timer_cnt=0;
day_time=0;
blk_time=1;
eeprom_data_r(day_time);
send_data();
send_data();
blk_led=0;
day_led=1;
}
}
if(!sample&&!runidle_key) //運(yùn)行模式 //白天采樣 //day_time=1
{
if(blk_time&&!day_time)
{
timer_cnt=0;
day_time=1;
blk_time=0;
eeprom_data_r(day_time);
send_data();
send_data();
blk_led=1;
day_led=0;
}
}
}
void time0_int(void) interrupt 1
{
WDT_CONTR=0x3c;
TH0=TL0=0;
timer_cnt++;
if(timer_cnt<=16105)over_flag=0; //400 //16105=half of hour //32210=one hour //200=9 seconds //6000=7minuts
else {
timer_cnt=0;
over_flag=1;
//sio_fail_led=0; //*************test timer
ET0=0;TR0=0;
}
}
void E_int0(void) interrupt 0 //using 1
{
DelayMs(20);
if(!press_key)
{
if(runidle_key)
{
key_flag=1;
ET0=0;
TR0=0;
EX0=0;
}
else
{
timer_cnt=0;
over_flag=0;
blk_time=0;
day_time=0;
while(!press_key)WDT_CONTR=0x3c;
}
}
WDT_CONTR=0x3c;
}
void main(void)
{
P1=0xff;
P3=0xf7;
re_485=0;
P1&=0xf0;
DelayMs(500);
DelayMs(500);
DelayMs(500);
DelayMs(500);
DelayMs(500);
DelayMs(500);
DelayMs(500);
P1|=0x0f;
sio_init();
if(!runidle_key)init_run();
while(1)
{
WDT_CONTR=0x3c;
run_set();
init_run();
go_set();
go_run();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -