#include<reg52.h>
#define LED_PORT P0
#define uchar unsigned char
#define uint unsigned int
sbit hc138_A = P2^2;
sbit hc138_B = P2^3;
sbit hc138_C = P2^4;
sbit Trig = P2^1;
sbit Echo = P3^1;
unsigned char ledx8_value[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar count,time;
bit flag;
void led_disy(uint temp_data);
void delay(uint time_t);
void timer();
void main(void)
{
Trig=0;
timer();
Trig=1;
delay(500);
Trig=0;
while(1)
{;}
}
void timer()
{
TMOD=0x01;
TH0=0xfc;
TL0=0x18;
EA=1;
ET0=1;
TR0=1;
}
void timer_interrupt() interrupt 1
{
TR0=0;
TH0=0xfc;
TL0=0x18;
if(Echo==1)
{
count++;
}
if(Echo==0)
{
time=count*17;
led_disy(time);
}
TR0=1;
}
void led_disy(uint temp)
{
uchar ge_data,shi_data,bai_data;
bai_data=temp/100;
temp=temp%100;
shi_data=temp/10;
temp=temp%10;
ge_data=temp;
LED_PORT=0x00;delay(1);hc138_C=0;hc138_B=1;hc138_A=0;LED_PORT=ledx8_value[bai_data];delay(100);
LED_PORT=0x00;delay(1);hc138_C=0;hc138_B=0;hc138_A=1;LED_PORT=ledx8_value[shi_data];delay(100);
LED_PORT=0x00;delay(1);hc138_C=0;hc138_B=0;hc138_A=0;LED_PORT=ledx8_value[ge_data];delay(100);
}
void delay(uint time_t)
{
while(time_t-->0);
}
以上是我写的程序,我知道问题大,思路明确,用1ms中断计数嘛,但是我就写成这样的,肯定不能完成预定效果,感觉头都大了,搞了一下午,求大神救救我,我c语言这么烂,是不是没救了,不适合玩单片机了,哎……
打赏帖 | |
---|---|
C语言函数宏的三种封装方式被打赏50分 | |
【STM32F769】记一次由于开启D-Cache之后DMA数据传输出错的问题查找与解决被打赏35分 | |
嵌入式LinuxC语言程序调试和宏使用技巧被打赏50分 | |
让代码中包含最新的编译时间信息被打赏50分 | |
【分享开发笔记,赚取电动螺丝刀】STM32F769LVGL优化显示被打赏26分 | |
rtthread硬件加密--2crc加密分析被打赏10分 | |
【分享开发笔记,赚取电动螺丝刀】STM32F769驱动ST7789以及显示优化被打赏36分 | |
【分享开发笔记,赚取电动螺丝刀】S32K146 PAL模拟I2C驱动适配被打赏23分 | |
我想要一部加热台+电源硬件设计规范被打赏16分 | |
我想要一部加热台+LED背光驱动芯片RT9293知识被打赏18分 |