基于STC15F104W的单片机示波器
代码如下,利用PCF8591和iic采集模拟信号然后通过iic接口的OLED显示出来 代码不完善有BUG,望大神斧正! 这个示波器推荐输入电压0-5V,显示的时候右上角会显示实时电压(单位毫伏) 但是由于前置硬件的存在输入超过5V的电压甚至是负电压也是可以的,但是超过±5V的电压会被钳位二极管钳住到±5V
主要思路还是画点函数画出图像
这段代码我是直接抄B站UP主@x麟枫 的作业的,比较懒23333
void Setpos(unsigned char x0,unsigned char y0)
{
unsigned char y,mod,pos;
if(y0%8==0){
y=y0/8;
pos = 0x01;
}
else{
y = y0/8;
mod = y0%8;
switch(mod)
{
case 1:
pos = 0x02;
break;
case 2:
pos = 0x04;
break;
case 3:
pos = 0x08;
break;
case 4:
pos = 0x10;
break;
case 5:
pos = 0x20;
break;
case 6:
pos = 0x40;
break;
case 7:
pos = 0x80;
break;
default:
break;
}
}
oled_set_pos(x0,y);
oled_wr_byte(pos,1);
}
然后主代码如下:
#include<stc15.h>
#include <I2C.H>
#define uchar unsigned char
#define PCF8591 0x90
int xx=0;int y11,y22;
unsigned char AD_CHANNEL;
unsigned int D[32];
int ii,hh;
void delay(uchar i);
bit DACconversion(unsigned char sla,unsigned char c, unsigned char Val);
bit ISendByte(unsigned char sla,unsigned char c);
unsigned char IRcvByte(unsigned char sla);
int main()
{
oled_init();
while(1)
{
oled_DrawLine(0,0,0,63);
oled_DrawLine(0,63,127,63);
for(xx=0;xx<128;xx++)
{
switch(AD_CHANNEL)
{
case 0: ISendByte(PCF8591,0x41);
D[0]=IRcvByte(PCF8591);
break;
case 1: ISendByte(PCF8591,0x42);
D[1]=IRcvByte(PCF8591);
break;
case 2: ISendByte(PCF8591,0x43);
D[2]=IRcvByte(PCF8591);
break;
case 3: ISendByte(PCF8591,0x40);
D[3]=IRcvByte(PCF8591);
break;
case 4: DACconversion(PCF8591,0x40, D[4]);
break;
}
D[4]=D[2];
if(++AD_CHANNEL>4) AD_CHANNEL=0;
ii=map(D[2],0,255,63,9);
hh=map(D[2],0,255,0,5000);
oled_shownum(64,0,hh,4,32);
Setpos(xx,ii);
delay(200);
}
oled_clear();
}
}
void delay(uchar i)
{
uchar j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
bit DACconversion(unsigned char sla,unsigned char c, unsigned char Val)
{
Start_I2c();
SendByte(sla);
SendByte(c);
SendByte(Val);
Stop_I2c();
return(1);
}
bit ISendByte(unsigned char sla,unsigned char c)
{
Start_I2c();
SendByte(sla);
SendByte(c);
Stop_I2c();
return(1);
}
unsigned char IRcvByte(unsigned char sla)
{ unsigned char c;
Start_I2c();
SendByte(sla+1);
c=RcvByte();
Ack_I2c(1);
Stop_I2c();
return(c);
}
|