📄 mcu.h
字号:
#include <c8051F000.h>
#include "c:\keil\c51\inc\stdlib.h"
#include "c:\keil\c51\inc\stdio.h"
#include "c:\keil\c51\inc\math.h"
//--------------------------- define constants --------------------
#define SUM_METAL 98.1 // The threshold of sum signal
#define SUM_LIQUID 149.7
#define MIRROR_METAL 2 // The mirror is metal
#define MIRROR_LIQUID 3
#define MIRROR_OUT 0
#define DEL_LENGTH 2 // The period of Time_Delay is 5ms
#define CONSTANT_X 0.56678
#define CONSTANT_Y 0.48766
#define ZERO_X 340
#define ZERO_Y 328
//--------------------------- define variables -----------------------
char m_nLEDData[12]; // The symbols to be displayed stored in this array
float m_fCoorX,m_fCoorY; // The final coordinate of X and Y
int m_nSampDataX[6]; // The array of sampling datas
int m_nSampDataY[6];
float xdata m_fRelX,m_fRelY; // To store the zero position in relative mode
unsigned char temp_key; // To check which key is pushed
sbit m_bSelMode = P3^0;
sbit USB_SWR = P3^3;
sbit USB_SCS = P3^4;
sbit USB_END = P3^5;
sbit USB_FLB = P3^6;
sbit USB_FLC = P3^7;
sbit USB_SRD = P2^4;
sbit m_bDisX = P1^4; // The /WR of 7218(1)
sbit m_bDisY = P1^5; // The /WR of 7218(2)
sbit m_bDisMode = P0^7; // The Mode of 7218(1,2)
sbit m_bKeyMode = P3^1; // The key input of mode
sbit m_bKeyURT = P3^2; // The key input of URT
sbit m_bKeyHold = P1^3; // The key input of Hold
sbit m_bBuzzer = P2^3; // The output to drive buzzer
sfr16 ADC0HL = 0xBE; // The result of ADC conversion
//---------------------------define functions--------------------------
// ================Configure the system to the required status======================
void System_Config()
{
WDTCN = 0xDE; // Disable Watchdog Timer
WDTCN = 0xAD;
EA = 0; // Disable global interrupt
OSCICN = 0x96; // Configure internal clock as system clock and f=8.0MHz
OSCXCN = OSCXCN & 0x9F; // Set XTAL1 to internal Gnd
XBR0 = 0x00; // I/O configure: P0.0=TX and P0.1=RX
XBR1 = 0x00;
XBR2 = 0xc0; // Enalbe cross-bar and weak-up
PRT0CF = 0xff; // Configure P0 as push-pull
PRT1CF = 0xff; // Configure P0 as push-pull
PRT2CF = 0xef; // Configure P1 as push-pull
PRT3CF = 0x3f; // Configure P3.1,2,3 as open-drain and P3.4,5 as push-pull
AMX0SL = 0x08; // The default channel of ADC is temperature sensor
}
//=================== Initialize the internal resources =========================
void System_Init()
{
m_fRelX=0;
m_fRelY=0;
m_bBuzzer=0;
m_bSelMode=1;
m_bKeyHold=1;
m_bKeyURT=1;
temp_key=1;
USB_SCS=1; //disable usb
}
//============================== LED display====================================
void LED_Dis()
{
unsigned char i;
m_bDisX = 1; // The /WR of 7218(X) is Invalid
m_bDisY = 1; // The /WR of 7218(Y) is Invalid
//............. Output X ................
m_bDisMode = 1; // Load control bits
m_bDisX = 0; // The /WR of 7218(X) is valid
P1 = 0xB0; // Control bits :1011 0000
m_bDisX = 1; // The /WR of 7218(X) is invalid
m_bDisMode = 0; // Load X datas
for (i=0;i<6;i++)
{
m_bDisX = 0;
P1 = m_nLEDData[i];
m_bDisX = 1;
}
m_bDisX = 0; // The /WR of 7218(X) is valid
P1 = 0x00; // 7218 needs to input 8 numbers
m_bDisX = 1;
m_bDisX = 0;
P1 = 0x00;
m_bDisX = 1;
//............. Output Y ................
m_bDisMode = 1; // Load control bits
m_bDisY = 0; // The /WR of 7218(Y) is valid
P1 = 0xB0; // Control bits :1011 0000
m_bDisY = 1; // The /WR of 7218(X) is invalid
m_bDisMode = 0; // Load Y datas
for (i=6;i<12;i++)
{
m_bDisY = 0;
P1 = m_nLEDData[i];
m_bDisY = 1;
}
m_bDisY = 0; // The /WR of 7218(Y) is valid
P1 = 0x00; // 7218 needs to input 8 numbers
m_bDisY = 1;
m_bDisY = 0;
P1 = 0x00;
m_bDisY = 1;
}
//============================== LED Outputs HH ====================================
void LED_DisHH()
{
unsigned char i;
for (i=0;i<12;i++)
{
m_nLEDData[i]= 0xBE; // 7218 Outputs "H"
}
LED_Dis();
}
//============================== LED Outputs HL ====================================
void LED_DisHL()
{
unsigned char i;
for (i=0;i<6;i++)
{
m_nLEDData[i]= 0xBE; // X outputs "H"
}
for (i=6;i<12;i++)
{
m_nLEDData[i]= 0x8B; // Y outputs "L"
}
LED_Dis();
}
//============================== LED Outputs LH ====================================
void LED_DisLH()
{
unsigned char i;
for (i=0;i<6;i++)
{
m_nLEDData[i]= 0x8B; //display "L"
}
for (i=6;i<12;i++)
{
m_nLEDData[i]= 0xBE; //display "H"
}
LED_Dis();
}
//============================== LED Outputs LL ====================================
void LED_DisLL()
{
unsigned char i;
for (i=0;i<12;i++)
{
m_nLEDData[i]= 0x8B; // X and Y output "L"
}
LED_Dis();
}
//============================== LED Outputs -- ====================================
// When the laser beam is out of PSD,LED outputs "--"
void LED_DisInit()
{
unsigned char i;
for (i=0;i<12;i++)
{
m_nLEDData[i]= 0x84; // X and Y output "-"
}
LED_Dis();
}
//=================== LED displays the coordinates of X and Y ===============================
//This function transforms the float number to a string
void LED_DisXY()
{
unsigned char i;
char m_sStr[5],m_sTemp[1];
char LEDSEG[10] = { 0xFB,0xB0,0xED,0xF5,0xB6,0xD7,0xDF,0xF0,0xFF,0xF7 };
// LED segment of digital '0'-'9'
//.......... judge the signs of X and Y ..............
if(m_fCoorX<0)
m_nLEDData[0]=0x84; // The sign of m_fCoorX is '-'
else
m_nLEDData[0]=0x80; // The sign of m_fCoorX is '+'
if(m_fCoorY<0)
m_nLEDData[6]=0x84; // The sign of m_fCoorY is '-'
else
m_nLEDData[6]=0x80; // The sign of m_fCoorY is '+'
m_fCoorX=fabs(m_fCoorX); // Absolute value
m_fCoorY=fabs(m_fCoorY);
//.......... Convert float to string and output X ..............
sprintf(m_sStr,"%5.1f",m_fCoorX); // Convert float to a string as "xxx.x"
m_sTemp[0]=m_sStr[0]; // The 1st bit of X
i=atoi(m_sTemp);
m_nLEDData[1]=LEDSEG[i];
m_sTemp[0]=m_sStr[1]; // The 2nd bit of X
i=atoi(m_sTemp);
m_nLEDData[2]=LEDSEG[i];
m_sTemp[0]=m_sStr[2]; // The 3rd bit of X with a radix point
i=atoi(m_sTemp);
m_nLEDData[3]=LEDSEG[i]-0x80;
m_sTemp[0]=m_sStr[4]; // The decimal fraction of X
i=atoi(m_sTemp);
m_nLEDData[4]=LEDSEG[i];
//.......... Convert float to string and output Y ..............
sprintf(m_sStr,"%5.1f",m_fCoorY);
m_sTemp[0]=m_sStr[0];
i=atoi(m_sTemp);
m_nLEDData[7]=LEDSEG[i];
m_sTemp[0]=m_sStr[1];
i=atoi(m_sTemp);
m_nLEDData[8]=LEDSEG[i];
m_sTemp[0]=m_sStr[2];
i=atoi(m_sTemp);
m_nLEDData[9]=LEDSEG[i]-0x80;
m_sTemp[0]=m_sStr[4];
i=atoi(m_sTemp);
m_nLEDData[10]=LEDSEG[i];
if(m_bSelMode)
{
m_nLEDData[5]=0xA2; // Output unit sign ,'' in abs_mode and .. in rel _mode
m_nLEDData[11]=0xA2;
}
else
{
m_nLEDData[5]=0x98;
m_nLEDData[11]=0x98;
}
LED_Dis();
}
//=================== Signal sampling ===============================
int Signal_Samp(char m_sChannel,int m_nGain)
// When m_sChannel = 'X','Y','S,','T' ,the current selection separately is X,Y,Sum and temperature
// m_nGain is ADC internal amplifier gain
// Return the result of the conversion
{
ADC0CN = 0xC0; // Enable ADC;Tracking begins with a write
// of 1 to ADBUSY and lasts for 3 SAR clocks;
// ADC conversion started upon every write of 1 to ADBUSY
// Data in ADC0H:ADC0L Registers is right justified
REF0CN =0x07; // Enable temperature sensor and internal voltage reference
ADC0CF = 0x40;
AMX0CF = 0x00;
if (m_sChannel == 'X') // Channel selection
AMX0SL = 0x00;
else if (m_sChannel =='Y')
AMX0SL = 0x02;
else if (m_sChannel == 'S')
AMX0SL = 0x04;
else
AMX0SL = 0x08; // Temperature is selected;
AMX0CF = AMX0CF & m_nGain; // Set the internal amplifier gain
ADC0CN = 0xD0; // Start ADC conversion
while(!ADCINT); // Judge the end of the conversion
return ADC0HL;
}
//=================== Time Delay ===============================
// The period of this delay is 5ms
void Time_Delay()
{
unsigned char i;
char j;
for(j=0;j<DEL_LENGTH;j++)
{
for(i=0;i<253;i++)
;
}
}
//=================== Evaluate the average ===============================
float Samp_Average(int m_nSampData[])
{
unsigned char i;
int m_nSampMax=0,m_nSampMin=4096;
float m_fSampAver=0;
for(i=0;i<6;i++)
{
m_fSampAver+=m_nSampData[i];
if(m_nSampData[i]>m_nSampMax)
m_nSampMax=m_nSampData[i];
if(m_nSampData[i]<m_nSampMin)
m_nSampMin=m_nSampData[i];
}
m_fSampAver=(m_fSampAver-m_nSampMax-m_nSampMin)/4;
return m_fSampAver;
}
//=================== Evaluate the position of laser beam ===============================
char Beam_Position()
{
unsigned char i;
float m_nSampAver;
for(i=0;i<6;i++)
{
m_nSampDataX[i]=Signal_Samp('X',1);
}
m_nSampAver=Samp_Average(m_nSampDataX)/10;
if(m_nSampAver<SUM_LIQUID+1.5 && m_nSampAver>SUM_LIQUID-5)
return MIRROR_LIQUID;
else if(m_nSampAver>SUM_LIQUID+1.5 )
return MIRROR_OUT;
else
return MIRROR_METAL;
}
//=================== The buzzer makes a sound ===============================
void Buzzer_On()
{
unsigned char i;
for(i=0;i<254;i++)
m_bBuzzer=1;
for(i=0;i<10;i++)
m_bBuzzer=0;
}
//=================== The Mode key is pushed ===============================
void OnKeyMode()
{
unsigned char i;
Time_Delay();
if(m_bKeyMode==1)
return; //Delay to avoid noise and dithering
while(!m_bKeyMode) // until the key is free
;
if(m_bSelMode) // switch the absolute mode to the relative mode
{
m_fRelX=m_fCoorX;
m_fRelY=m_fCoorY;
}
else // switch the absolute mode to the relative mode
{
m_fRelX=0;
m_fRelY=0;
}
m_bSelMode=!m_bSelMode; //switch the mode for relative or absolute
for(i=0;i<120;i++)
Buzzer_On();
}
//=================== The Hold key is pushed ===============================
void OnKeyHold()
{
unsigned char i;
Time_Delay();
if(m_bKeyHold==1)
return;
while(!m_bKeyHold)
;
if(temp_key==0)
temp_key=1;
else if(temp_key==1)
temp_key=0;
for(i=0;i<120;i++)
Buzzer_On;
for(i=0;i<120;i++)
Buzzer_On();
for(i=0;i<120;i++)
Buzzer_On();
for(i=0;i<120;i++)
Buzzer_On();
for(i=0;i<120;i++)
Buzzer_On();
}
//=================== The URT key is pushed ===============================
void OnKeyURT()
{
unsigned char i;
Time_Delay();
if(m_bKeyURT==1)
return;
for(i=0;i<126;i++)
Buzzer_On();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -