📄 freescale
字号:
#include "IO_Map.h"
#include "Variables.h"
#include "Key1.h"
#include "Key2.h"
#include "Key3.h"
#include "Receiver.h"
#include "LCD1602.h"
void Ultrasonic_Receive_Process(void)
{
if((ovrflwcount>500)&&(received==0))
{
Lcd_Disp_Str(0,0,"d =");
received = 1;
Lcd_Disp_Str(0,4,"out of range");
}
if( (~Receiver_GetVal())&&(wait == 0)&&(received==0)) //if the pin goes high we received
{
stoptime = TPM1CNT; //count the # of ticks
stopcount = ovrflwcount; //count # of times counter over flowed before receiving
received = 1; //received the pulse
Lcd_Disp_Str(0,0,"d =");
ticks = ((stopcount*256)+stoptime); //calculate total ticks
distance = (ticks * 136 * (tpm / ctpm)) / 200;
//# of ticks * (1.36 mm pertick)*(ticks per 5 cm/calibrated)
//all over 2 to compensate for sending and receiving
prevdist = currdist; //store previous dist
currdist = distance; //store current dist
Lcd_Disp_Str(0,4," ");
Lcd_Disp_Str(0,4,&lcd_buffer);
if(normal)
{
if(rcount>0)
{
delayedsend = 60000; // delay enough before sending next pulse
sendpulse = 1;
count = 2*pulsecount;
wait = waittime;
distsum = distsum + distance; // add up the samples
if(rcount == (samples/2)) //take the middle and last samples
{
speedsum = distance; //for speed calculation
}
else if(rcount == 1)
{
speedsum = speedsum - distance;
}
rcount--;
}
else
{
distsum = distsum/(samples); //take the average
speedsum = (speedsum/(samples/100)); //calculate speed between first and last
Lcd_Disp_Str(0,4," ");
Lcd_Disp_Str(0,4,&lcd_buffer);
Lcd_Disp_Str(1,0,"spd = ");
Lcd_Disp_Str(1,9,&lcd_buffer);
distsum = 0; //reset variables
speedsum = 0;
normal = 0;
}
}
else if(calibrate)
{
/*takes 200 samples of how many ticks for 5cm
*averages it and sets it as the calibrated ticks per 5 cm
*/
if(rcount>0)
{
delayedsend = 60000;
sendpulse = 1;
count = 2*pulsecount;
wait = waittime;
distsum = distsum + distance; //sum up the distances
tsum = tsum + ticks; //sum up the ticks
rcount--;
}
else
{
distsum = distsum / samples;//average the distances
tsum = tsum / samples; //average the ticks
Lcd_Disp_Str(0,4," ");
Lcd_Disp_Str(0,4,&lcd_buffer);
Lcd_Disp_Str(1,0,"avgtick = ");
Lcd_Disp_Str(1,10,&lcd_buffer);
distsum = 0; //reset variables
ctpm = tsum;
tsum = 0;
calibrate = 0;
}
}
}
}
void Key_Process(void) //standard debounce state machine as given in lecture
{
switch(pushstate)
{
case nopush:
if((~Key1_GetVal()) || (~Key2_GetVal()) || (~Key3_GetVal())) // button pushed?
pushstate = maybepush;
else pushstate = nopush;
break;
case maybepush:
if((~Key1_GetVal()) || (~Key2_GetVal()) || (~Key3_GetVal()))
pushstate = pushed;
else pushstate = nopush;
break;
case pushed:
if((~Key1_GetVal()) || (~Key2_GetVal()) || (~Key3_GetVal())) // button pushed?
{
pushstate = pushed;
if(~Key1_GetVal()) key1 = 1; //pushflags for each button
else if(~Key2_GetVal()) key2 = 1;
else if(~Key3_GetVal()) key3 = 1;
}
else pushstate = maybenopush;
break;
case maybenopush:
if((~Key1_GetVal()) || (~Key2_GetVal()) || (~Key3_GetVal())) // button pushed?
pushstate = pushed;
else
{
pushstate = nopush;
/*
D1 is normal operation
D2 is speed detection mode
D3 is calibration mode
This checks which button was pressed and sets
the appropriate flags for each mode
*/
if(received)
{
if(!mode)
{
if(key1)
{
sendpulse = 1; //sends a pulse
count = 2*pulsecount;
wait = waittime; //wait for receive
rcount = samples; //count for # of samples
key1 = 0;
normal = 1; //mode of operation
}
else if(key2)
{
ctpm = 73; //reset the calibration
mode = 1; //set mode to change samples mode
Lcd_Disp_Str(1,0,"b1- ");
Lcd_Disp_Str(1,0,"b2+ b3 back");
key2 = 0;
}
else if(key3)
{
sendpulse = 1; //sends pulse
count = 2*pulsecount;
wait = waittime;
calibrate = 1; //calibrate mode on
key3 = 0;
rcount = samples;
}
}
else if(mode) //mode two for changing sample number
{
if(key1)
{ //decrement sample #
if(samples>50)
{
samples = samples - 50; //minimum number 50
}
key1 = 0;
Lcd_Disp_Str(0,0," ");
Lcd_Disp_Str(0,0,&lcd_buffer);
}
else if(key2) //increment sample #
{
samples = samples+50;
Lcd_Disp_Str(0,0," ");
Lcd_Disp_Str(0,0,&lcd_buffer);
key2 = 0;
}
else if(key3)
{
ctpm = 73; //reset the calibration
mode = 0; //set mode to change samples mode
Lcd_Disp_Str(0,0,"b1 sendpulse");
Lcd_Disp_Str(1,0,"b2 mode2 ");
Lcd_Disp_Str(1,0,"b3 cal");
key3 = 0;
}
}
}
}
break;
}
}
void Ultrasonic_Send_Pulse(void) //sends a certain number of pulses depending on what the count is
{
if(sendpulse)
{
received = 0;
if(count>0)
{
setReg8(TPM2SC, 0x48); //Enable device
count--;
}
else
{
sendpulse = 0;
setReg8(TPM2SC, 0x00); //Disable device
setReg8(TPM1CNT, 0x00);
ovrflwcount = 0;
}
}
}
void System_Initialize(void)
{
key1 = 0; //按键标识初始化
key2 = 0;
key3 = 0;
normal = 0;
prevdist = 0;
currdist = 0;
rcount =0;
distsum = 0;
speedsum = 0;
tsum = 0;
Lcd_Init();
Lcd_Disp_Str(0,0,"b1 sendpulses");
Lcd_Disp_Str(1,0,"b2 mode2 ");
Lcd_Disp_Str(1,0,"b3 cal");
sendpulse = 0;
//变量初始化
stoptime = 0;
displayspeed = 0;
speedmode = 0;
loopcount = 0;
speed = 0;
mode = 0;
received = 1;
samples = 200;
calibrate = 0;
pushstate = nopush;
ticks = 0;
ctpm= 73.0; //init ticks per 5 cm
tpm = 73.0;
//初始化定时器变量
time1=t1;
time2=t2;
wait = 0;
delayedsend = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -