生成一个用C52单片机,超声波测距,要求:oled屏幕
时间: 2023-05-29 10:03:14 浏览: 194
基于51单片机的超声波测距、OLED显示_51单片机_C#_oled_超声波测距
5星 · 资源好评率100%
显示距离值的程序
以下是使用C语言编写的超声波测距程序,使用C52单片机和OLED屏幕显示距离值:
```c
#include <reg52.h>
#include <intrins.h>
#define FREQ 11059200UL
sbit TRIG = P1^0;
sbit ECHO = P1^1;
unsigned char code font_6x8[95][6];
unsigned int distance = 0;
void delay_us(unsigned int us)
{
while(us--)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
void delay_ms(unsigned int ms)
{
while(ms--)
{
delay_us(1000);
}
}
void init()
{
TMOD = 0x01;
TH0 = 0;
TL0 = 0;
EA = 1;
ET0 = 1;
TRIG = 0;
}
void display(unsigned char x, unsigned char y, unsigned char ch)
{
unsigned char i, j;
ch -= 32;
for(i = 0; i < 6; i++)
{
j = font_6x8[ch][i];
OLED_SetPos(x, y + i);
OLED_WrDat(j);
}
}
void OLED_Init()
{
OLED_WR_Byte(0xAE,OLED_CMD);//--display off
OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address
OLED_WR_Byte(0xB0,OLED_CMD);//--set page address
OLED_WR_Byte(0x81,OLED_CMD); // contract control
OLED_WR_Byte(0xFF,OLED_CMD);//--128
OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap
OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
OLED_WR_Byte(0x3F,OLED_CMD);//--1/64 duty
OLED_WR_Byte(0xC8,OLED_CMD);//Com scan direction
OLED_WR_Byte(0xD3,OLED_CMD);//set display offset
OLED_WR_Byte(0x00,OLED_CMD);//
OLED_WR_Byte(0xD5,OLED_CMD);//set osc division
OLED_WR_Byte(0x80,OLED_CMD);//
OLED_WR_Byte(0xD8,OLED_CMD);//set area color mode off
OLED_WR_Byte(0x05,OLED_CMD);//
OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period
OLED_WR_Byte(0xF1,OLED_CMD);//
OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion
OLED_WR_Byte(0x12,OLED_CMD);//
OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh
OLED_WR_Byte(0x30,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//set charge pump enable
OLED_WR_Byte(0x14,OLED_CMD);//
OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
}
void OLED_SetPos(unsigned char x, unsigned char y)
{
OLED_WR_Byte(0xb0 + y, OLED_CMD);
OLED_WR_Byte(((x & 0xf0) >> 4) | 0x10, OLED_CMD);
OLED_WR_Byte((x & 0x0f), OLED_CMD);
}
void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
unsigned char i;
if(cmd)
{
OLED_DC = 1;
}
else
{
OLED_DC = 0;
}
OLED_CS = 0;
for(i = 0; i < 8; i++)
{
OLED_SCLK = 0;
if(dat & 0x80)
{
OLED_SDIN = 1;
}
else
{
OLED_SDIN = 0;
}
OLED_SCLK = 1;
dat <<= 1;
}
OLED_CS = 1;
}
void OLED_Clear()
{
unsigned char i, j;
for(i = 0; i < 8; i++)
{
OLED_SetPos(0,i);
for(j = 0; j < 128; j++)
{
OLED_WrDat(0);
}
}
}
void OLED_WrDat(unsigned char dat)
{
OLED_WR_Byte(dat, OLED_DATA);
}
void Timer0_ISR() interrupt 1
{
static unsigned int counter = 0;
if(counter > 50)
{
TRIG = 1;
delay_us(10);
TRIG = 0;
while(ECHO == 0);
TH0 = 0;
TL0 = 0;
while(ECHO == 1);
distance = (unsigned int)(TH0 << 8 | TL0) / 58;
counter = 0;
}
counter++;
}
void main()
{
init();
OLED_Init();
OLED_Clear();
while(1)
{
display(0, 0, 'D');
display(6, 0, 'i');
display(12, 0, 's');
display(18, 0, 't');
display(24, 0, 'a');
display(30, 0, 'n');
display(36, 0, 'c');
display(42, 0, 'e');
display(48, 0, ':');
display(54, 0, ' ');
display(72, 0, distance / 100 + '0');
display(78, 0, distance % 100 / 10 + '0');
display(84, 0, distance % 10 + '0');
delay_ms(100);
OLED_Clear();
}
}
```
该程序通过超声波测距模块测量距离,并在OLED屏幕上显示距离值。程序中使用了定时器0来计数,每50个计数就触发一次超声波测距,并计算距离值。距离值通过数字的方式在OLED屏幕上显示。
阅读全文