/**
* @file main.c
* @author greedyhao (hao_ke@163.com)
* @brief
* @version 0.1
* @date 2019-11-07
*
* @copyright Copyright (c) 2019
*
*/
#include <STC12C5A60S2.H>
#include <math.h>
/* macro */
#define sound_velocity 34300 /* sound velocity in cm per second */
#define period_in_us pow(10,-6)
#define Clock_period 1.085*period_in_us /* period for clock cycle of 8051*/
#define SEG_PORT P1
sbit SEG_BIT0 = P2^3;
sbit SEG_BIT1 = P2^2;
sbit SEG_BIT2 = P2^1;
sbit SEG_BIT3 = P2^0;
sbit BEEP_PIN = P2^4;
sbit TRIG_PIN = P2^5;
sbit ECHO_PIN = P2^6;
/* user define */
code unsigned char seg_table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
unsigned int num = 0; /* the number displayed on 7-segment */
unsigned char time_out = 0; /* ECHO port */
unsigned char beep_frq = 0; /* counting the beep pin toggle time */
unsigned char beep_lv = 0; /* level of rapid (0-4) */
unsigned char beep_flag = 0; /* status of beep 0: no work 1: rapid beep 2: long beep */
/* static func */
/* public func */
void init_timer(void);
void delay_10us(void);
void delay_ms(unsigned char n);
void seg_dis(unsigned int num);
void uw_sen_start(void);
/* ------------- main ---------------- */
void main(void){
unsigned long tmp = 0;
float distance_measurement, value;
P2 = 0x0f;
P2M1 = 0x40; //0100 0000
P2M0 = 0x30; //0011 0000
init_timer();
while (1)
{
uw_sen_start();
time_out = 0;
while (ECHO_PIN == 0 && time_out < 2);
time_out = 0;
TR0 = 1;
ET1 = 0;
while (ECHO_PIN == 1);
TR0 = 0;
/* calculate distance using timer */
value = Clock_period * sound_velocity;
distance_measurement = (TL0|(TH0<<8)); /* read timer register for time count */
distance_measurement = (distance_measurement*value)/2.0; /* find distance(in cm) */
num = (int)(distance_measurement*10)%10000;
if (num <= 800 && num > 300) {
beep_flag = 1;
beep_lv = (num - 300)/100; /* (x - 300)/500*5 (level 5) */
}
else if (num <= 300) beep_flag = 2;
else beep_flag = 0;
ET1 = 1;
delay_ms(100);
}
}
/* ------------ delay --------------- */
static void _delay_1ms(void) //误差 -0.651041666667us
{
unsigned char a,b;
for(b=4;b>0;b--)
for(a=113;a>0;a--);
}
static void _delay_10ms(void) //@11.0592MHz
{
unsigned char a,b;
for(b=151;b>0;b--)
for(a=29;a>0;a--);
}
void delay_ms(unsigned char n) //误差 -0.000000000002us
{
do
{
_delay_1ms();
} while (--n);
}
/* ------------ ultrasonic --------------- */
void init_timer(void)
{
TMOD = 0x11;
TF0 = 0;
TR0 = 0;
TH1 = 0xB8; /* 20ms */
TL1 = 0x00;
EA = 1;
ET1 = 1;
TR1 = 1;
}
void delay_10us(void)
{
TL0=0xF7;
TH0=0xFF;
TR0=1;
while (TF0!=1);
TR0=0;
TF0=0;
}
/**
* @brief ultrasonic sensor start working
*
*/
void uw_sen_start(void)
{
TRIG_PIN = 1;
delay_10us();
TRIG_PIN = 0;
}
/* ------------ segment --------------- */
/**
* @brief display number on 7-segment
*
* @param num number format: xxx.x
* @note num must less than 9999
*/
void seg_dis(unsigned int num)
{
unsigned char _bit0, _bit1, _bit2, _bit3;
_bit0 = _bit1 = _bit2 = _bit3 = 0;
_bit0 = num % 10;
_bit1 = num / 10 % 100;
_bit2 = num / 100 % 1000;
_bit3 = num / 1000;
P2 = P2 | 0x0f;
SEG_BIT0 = 0;
SEG_PORT = seg_table[_bit0%10];
delay_ms(1);
SEG_BIT0 = 1;
// SEG_PORT = 0xff;
SEG_BIT1 = 0;
SEG_PORT = seg_table[_bit1%10]&0x7F;
delay_ms(1);
SEG_BIT1 = 1;
// SEG_PORT = 0xff;
SEG_BIT2 = 0;
SEG_PORT = seg_table[_bit2%10];
delay_ms(1);
SEG_BIT2 = 1;
// SEG_PORT = 0xff;
SEG_BIT3 = 0;
SEG_PORT = seg_table[_bit3%10];
delay_ms(1);
SEG_BIT3 = 1;
// SEG_PORT = 0xff;
}
void Timer1Interrupt(void) interrupt 3
{
TH1 = 0xB8; /* 20ms */
TL1 = 0x00;
seg_dis(num);
++beep_frq;
++time_out;
switch (beep_flag)
{
case 0:
BEEP_PIN = 0;
break;
case 1:
switch (beep_lv)
{
case 0:
if (beep_frq >= 1) {BEEP_PIN = !BEEP_PIN; beep_frq = 0;}
break;
case 1:
if (beep_frq >= 5) {BEEP_PIN = !BEEP_PIN; beep_frq = 0;}
break;
case 2:
if (beep_frq >= 10) {BEEP_PIN = !BEEP_PIN; beep_frq = 0;}
break;
case 3:
if (beep_frq >= 15) {BEEP_PIN = !BEEP_PIN; beep_frq = 0;}
break;
case 4:
if (beep_frq >= 20) {BEEP_PIN = !BEEP_PIN; beep_frq = 0;}
break;
default:
if (beep_frq >= 20) {BEEP_PIN = !BEEP_PIN; beep_frq = 0;}
break;
}
break;
case 2:
BEEP_PIN = 1;
break;
default:
break;
}
}
mediapipe
- 粉丝: 1672
- 资源: 163
最新资源
- 19 工资发放明细表-可视化图表.xlsx
- 27 员工工资表(图表分析).xlsx
- 23 财务报告工资数据图表模板.xlsx
- 22 财务报告工资数据图表模板.xlsx
- 24 工资表-年度薪资可视化图表.xlsx
- 26 财务分析部门工资支出图表.xlsx
- Python爬虫技术详解:从基础到实战.zip
- 25 工资费用支出表-可视化图表.xlsx
- 30公司各部门工资支出数据图表1.xlsx
- 29 员工月度工资支出数据图表.xlsx
- 28 工资表(自动计算,图表显示).xlsx
- 31 财务分析工资年度开支图表.xlsx
- 33 年度工资预算表(可视化看板).xlsx
- 32 公司年度工资成本数据图表.xlsx
- 34 年度工资汇总-数据可视化看板.xlsx
- 36 财务报表新年度部门工资预算表.xlsx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈