📄 test.c
字号:
#include <At89x51.h>
#include <intrins.h>
#include <stdio.h>
/************************************************************************************
If the 2nd PCB connect as following ==> #define SECOND_PCB.
//key box 1-- P04
//key box 2-- P05
//key box 3-- P06
//key box 4-- P00
//key box 5-- P01
//key box 6-- P02
//key box 7-- P03
If the 2nd PCB connect as following ==> not define
//key box 1-- P06
//key box 2-- P05
//key box 3-- P04
//key box 4-- P03
//key box 5-- P02
//key box 6-- P01
//key box 7-- P00
**************************************************************************************/
#define DEBUG
#define FIRST_PCB
//#define SECOND_PCB
#define uchar unsigned char
#define uint unsigned int
/**********************************************
First pcb
**********************************************/
#ifdef FIRST_PCB
#define ZOOM_A P2_4
#define ZOOM_A_ P2_5
#define ZOOM_B P2_7
#define ZOOM_B_ P2_6
#define FOCUS_A P2_3
#define FOCUS_A_ P2_2
#define FOCUS_B P2_0
#define FOCUS_B_ P2_1
#define ZOOM_PI P1_1
#define FOCUS_PI P1_2
#endif
/**********************************************
Second pcb
**********************************************/
#ifdef SENCOND_PCB
#define ZOOM_A P2_5 //different
#define ZOOM_A_ P2_4 //different
#define ZOOM_B P2_6 //different
#define ZOOM_B_ P2_7 //different
#define FOCUS_A P2_3
#define FOCUS_A_ P2_2
#define FOCUS_B P2_0
#define FOCUS_B_ P2_1
#define ZOOM_PI P1_1
#define FOCUS_PI P1_2
#endif
#define LED P1_0
#define DELAY 10
#define ZOOM_WIDE 0
#define ZOOM_MID 510 //460 //650
#define ZOOM_TELE 1020 //920 //1300
#define FOCUS_WIDE 60 // //125
#define FOCUS_MID 120 //210//421
#define FOCUS_TELE 0
#define TEST_MODE_CNT 5
uchar KeyNum=0 ;
uchar Focus_state= 1;
uchar Zoom_state=1 ; // 1,2.3.4
uint Focus_Step=0 ;
uint Zoom_Step=0 ;
uchar RcvChar=0 ;
uchar TestKeyMode=0 ;
uchar LifeTestFlag=0 ;
void delay_ms(unsigned char n)
{
unsigned char i,j;
for(j=0;j<n;j++)
for (i=0;i<124;i++)
_nop_();
}
void DelayMs(unsigned int Delay)
{
unsigned int i;
unsigned char j;
for(i = 0; i < Delay; i++)
{
for(j = 0; j < 120; j++)
{
_nop_();
_nop_();
}
}
}
void led(uchar cnt)
{
uchar i=0 ;
for(i=0 ; i<cnt ;i++)
{
LED =0 ;
delay_ms(250) ;
delay_ms(250) ;
LED =1 ;
delay_ms(250) ;
delay_ms(250) ;
}
}
void Zoom_Motor_State(uchar state)
{
switch(state)
{
case 1 :
ZOOM_A =1 ;
ZOOM_B =0 ;
ZOOM_A_ =0 ;
ZOOM_B_ =1 ;
delay_ms(DELAY) ;
break ;
case 2:
ZOOM_A =1 ;
ZOOM_B =1 ;
ZOOM_A_ =0 ;
ZOOM_B_ =0 ;
delay_ms(DELAY) ;
break ;
case 3:
ZOOM_A =0 ;
ZOOM_B =1 ;
ZOOM_A_ =1 ;
ZOOM_B_ =0 ;
delay_ms(DELAY) ;
break ;
case 4:
ZOOM_A =0 ;
ZOOM_B =0 ;
ZOOM_A_ =1 ;
ZOOM_B_ =1 ;
delay_ms(DELAY) ;
break ;
}
Zoom_state=state ;
}
void Focus_Motor_State(uchar state)
{
switch(state)
{
case 1:
FOCUS_A =1 ;
FOCUS_B =0 ;
FOCUS_A_ =0 ;
FOCUS_B_ =1 ;
delay_ms(DELAY) ;
break ;
case 2:
FOCUS_A =1 ;
FOCUS_B =1 ;
FOCUS_A_ =0 ;
FOCUS_B_ =0 ;
delay_ms(DELAY) ;
break ;
case 3:
FOCUS_A =0 ;
FOCUS_B =1 ;
FOCUS_A_ =1 ;
FOCUS_B_ =0 ;
delay_ms(DELAY) ;
break ;
case 4:
FOCUS_A =0 ;
FOCUS_B =0 ;
FOCUS_A_ =1 ;
FOCUS_B_ =1 ;
delay_ms(DELAY) ;
break ;
}
Focus_state=state ;
}
void MoterSleep()
{
FOCUS_A =0 ;
FOCUS_B =0 ;
FOCUS_A_ =0 ;
FOCUS_B_ =0 ;
ZOOM_A =0 ;
ZOOM_B =0 ;
ZOOM_A_ =0 ;
ZOOM_B_ =0 ;
delay_ms(DELAY) ;
}
/**/
// cwFlag =1 means forward,otherwise means backward
void Zoom_Motor_Run(bit cwFlag,uint StepCnt)
{
uint cnt=0 ;
if(cwFlag)
{
while(cnt<StepCnt)
{
Zoom_state++ ;
if(Zoom_state==5) Zoom_state=1 ;
Zoom_Motor_State(Zoom_state) ;
cnt++ ;
Zoom_Step++ ;
}
}
else
{
while((cnt<StepCnt)&&(ZOOM_PI==0)) //&&(Zoom_Step>0))
{
Zoom_state-- ;
if(Zoom_state==0) Zoom_state=4 ;
Zoom_Motor_State(Zoom_state) ;
cnt++ ;
Zoom_Step-- ;
}
if(ZOOM_PI==1)
{
Zoom_Step =0 ;
}
}
}
/***************************************************
Focus cwFlag =0 means Focus_Step ++
***************************************************/
void Focus_Motor_Run(bit cwFlag,uint StepCnt)
{
uint cnt=0 ;
if(cwFlag)
{
while((cnt<StepCnt)&&(FOCUS_PI==0))
{
Focus_state++ ;
if(Focus_state==5) Focus_state=1 ;
Focus_Motor_State(Focus_state) ;
cnt++ ;
if(Focus_Step>0)
{
Focus_Step-- ;
}
}
if(FOCUS_PI==1)
{
Focus_Step =0 ;
}
}
else
{
while((cnt<StepCnt)) //&&(Focus_Step>0))
{
Focus_state-- ;
if(Focus_state==0) Focus_state=4 ;
Focus_Motor_State(Focus_state) ;
cnt++ ;
Focus_Step++ ;
}
}
}
void Zoom_Find_PI()
{
while(ZOOM_PI==0)
{
Zoom_state-- ;
if(Zoom_state==0) Zoom_state=4 ;
Zoom_Motor_State(Zoom_state) ;
}
Zoom_Step =0 ;
}
void Focus_Find_PI()
{
#if 0
while(FOCUS_PI==0)
{
Focus_state-- ;
if(Focus_state==0) Focus_state=4 ;
Focus_Motor_State(Focus_state) ;
}
Focus_Step=0 ;
#endif
while(FOCUS_PI==0)
{
Focus_state++ ;
if(Focus_state==5) Focus_state=1 ;
Focus_Motor_State(Focus_state) ;
}
Focus_Step=0 ;
}
void Zoom_Reset()
{
while(ZOOM_PI==0)
{
Zoom_state-- ;
if(Zoom_state==0) Zoom_state=4 ;
Zoom_Motor_State(Zoom_state) ;
}
Zoom_Step=50 ; // make following active
Zoom_Motor_Run(0,30) ; // PI-30
while(ZOOM_PI==1)
{
Zoom_state++ ;
if(Zoom_state==5) Zoom_state=1 ;
Zoom_Motor_State(Zoom_state) ;
}
Zoom_Step=0 ;
}
void SystemInit()
{
Zoom_Find_PI() ;
Focus_Find_PI() ;
Focus_Motor_Run(0,FOCUS_WIDE) ; // 1
}
void SystemPowerOff()
{
Zoom_Find_PI() ;
Focus_Find_PI() ;
}
void SystemLifeTest()
{
static uchar sleep=1 ;
while(LifeTestFlag==1)
{
Zoom_Find_PI() ;
Zoom_Motor_Run(1,ZOOM_TELE) ;
Zoom_Find_PI() ;
Focus_Find_PI() ;
Focus_Motor_Run(0,200) ;
Focus_Find_PI() ;
sleep=0 ;
}
if(sleep==0)
{
MoterSleep() ;
sleep=1 ;
}
}
// find lens PI
void int0_isr() interrupt 0 using 2
{
#if 0
EX0=0;
//IE0=0; //must be cleared before EX0 opened
EX0=1;
#endif
}
// for keys process
void int1_isr() interrupt 2 using 3
{
EX1=0 ;
if(P0_4==0)
{
P0^=0x0f ; //set P00~P03
P0_4=0 ;
if(P0_0==0)
{ KeyNum= 1; }
else if(P0_1==0)
{ KeyNum= 4; }
else if(P0_2==0)
{ KeyNum= 7; }
else if(P0_3==0)
{ KeyNum= 10; } // key*
}
else if(P0_5==0)
{
P0^=0x0f ; //set P00~P03
P0_5=0 ;
if(P0_0==0) KeyNum= 2;
else if(P0_1==0) KeyNum= 5;
else if(P0_2==0) KeyNum= 8;
else if(P0_3==0) {
KeyNum= 11;
if(LifeTestFlag==1) LifeTestFlag=0 ;
else LifeTestFlag=1 ;
} // 0
}
else if(P0_6==0)
{
P0^=0x0f ; //set P00~P03
P0_6=0 ;
if(P0_0==0) KeyNum= 3;
else if(P0_1==0) KeyNum= 6;
else if(P0_2==0) KeyNum= 9;
else if(P0_3==0) KeyNum= 12; // key#
}
#ifdef SECOND_PCB
switch(KeyNum)
{
case 1 : KeyNum=12 ;break ;
case 2 : KeyNum=11 ;break ;
case 3 : KeyNum=10 ;break ;
case 4 : KeyNum=9 ;break ;
case 5 : KeyNum=8 ;break ;
case 6 : KeyNum=7 ;break ;
case 7 : KeyNum=6 ;break ;
case 8 : KeyNum=5 ;break ;
case 9 : KeyNum=4 ;break ;
case 10 :KeyNum=3 ;break ;
case 11 :KeyNum=2 ;break ;
case 12 :KeyNum=1 ;break ;
default :break ;
}
#endif
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
P0=0xf0 ;
EX1=1 ;
}
void serial_isr() interrupt 4 using 3
{
EA=0;
if(RI)
{
RcvChar=SBUF ;
RI= 0;
}
if(TI)
{ TI=0; }
EA=1;
}
void KeysProcess(void)
{
if((KeyNum==0)||(KeyNum>12))
return ;
if(TestKeyMode==TEST_MODE_CNT)
{
led(KeyNum) ;
if(KeyNum==11)
{ TestKeyMode++ ; }
KeyNum=0 ;
return ;
}
switch(KeyNum)
{
case 1:
SystemInit() ;
MoterSleep() ;
break ;
case 2:
if(Zoom_Step>=ZOOM_MID) // backward
Zoom_Motor_Run(0,Zoom_Step-ZOOM_MID) ;
else
Zoom_Motor_Run(1,ZOOM_MID-Zoom_Step) ;
if(Focus_Step>=FOCUS_MID)
Focus_Motor_Run(1,Focus_Step-FOCUS_MID) ;
else
Focus_Motor_Run(0,FOCUS_MID-Focus_Step) ;
MoterSleep() ;
break ;
case 3:
if(Zoom_Step>=ZOOM_TELE) // backward
Zoom_Motor_Run(0,Zoom_Step-ZOOM_TELE) ;
else
Zoom_Motor_Run(1,ZOOM_TELE-Zoom_Step) ;
Focus_Find_PI() ; // to PI
MoterSleep() ;
break ;
case 4:
Focus_Motor_Run(0,5) ;
MoterSleep() ;
break ;
case 5:
Focus_Motor_Run(1,5) ;
MoterSleep() ;
break ;
case 6:
Zoom_Motor_Run(1,120) ;
MoterSleep() ;
break ;
case 9 :
Zoom_Motor_Run(0,120) ;
MoterSleep() ;
break ;
case 7:
Focus_Motor_Run(0,20) ;
MoterSleep() ;
break ;
case 8:
Focus_Motor_Run(1,20) ;
MoterSleep() ;
break ;
case 10 : // key *
SystemInit() ;
MoterSleep() ;
break ;
case 11 :
//led(3) ; // for keys test
//TestKeyMode++ ;
break ;
case 12 :
SystemPowerOff() ;
MoterSleep() ;
break ;
}
KeyNum=0 ;
}
#ifdef DEBUG
unsigned char getc()
{
unsigned char buff;
while(RI == 0);
buff = SBUF;
RI = 0;
return buff;
}
void putc(unsigned char ch)
{
SBUF = ch ;
_nop_();
_nop_();
while( TI==0 );
TI =0;
}
void putstr(uchar *str)
{
putc(10) ;
while(*str++)
{
putc(*str) ;
}
putc(10) ; // '\n '
}
void SerialProcess()
{
switch(RcvChar)
{
case '1' :
led(1) ;
//putc('1') ;
//putc('a') ;
break ;
case '2' :
led(2) ;
//putc('2') ;
//putc('b') ;
break ;
case '3' :
led(3) ;
//putc('c') ;
break ;
case '4' :
led(4) ;
//putc('d') ;
break ;
}
RcvChar=0 ;
}
#endif
void InitUart()
{
#if 0
SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 0xFD; /* TH1: reload value for 9600 baud @ 12MHz */
TR1 = 1; /* TR1: timer 1 run */
TI = 1; /* TI: set TI to send first char of UART */
RI=0 ;
#endif
PCON=0x80;
SCON=0xd0 ; //0xd0; //serial mode 1 .serial receive enable ;d0 =mode 3
TMOD=0x20; // timer1,mode 2 ;
TH1=0xf3 ; //baudrate=4800
TL1=0xf3;
TR1=1;
IE |=0x90 ; // ES =1 ;serial INT
}
void main()
{
delay_ms(250) ;
SP=0x50 ;
#ifdef DEBUG
InitUart() ;
#endif
IE0=0 ;
IE1=0 ;
IE |= 0x84 ; // INT 1 enable , EX1=1
IT1 =1 ; // H->L traggle
P0 =0xf0 ;
LED =0 ;
led(3) ;
LED =0 ;
while(1)
{
SerialProcess() ;
KeysProcess() ;
SystemLifeTest() ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -