TA的每日心情 | 开心 2021-1-15 05:20 |
---|
签到天数: 800 天 [LV.10]以坛为家III
|
本帖最后由 lwq1947 于 2019-12-19 18:18 编辑
在ARDUINO UNO开发板上插上一片0.96寸OLED显示屏并上传已下简单的程序就能构成一个OLED心率示波仪,
#include <SPI.h>
#include <Wire.h>
#include <MsTimer2.h>
//#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_MOSI 11
#define OLED_CLK 12
#define OLED_DC 9
#define OLED_CS 8
#define OLED_RESET 10
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
int a,b,x,y,k,t1,t2,Vmin,Vmax,Vmid,t,BPM;
int Buffer[128];
static const uint8_t PROGMEM Heart_16x16[] = {
0x00,0x00,0x18,0x18,0x3C,0x3C,0x7E,0x7E,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0x7F,0xFE,0x3F,0xFC,0x1F,0xF8,0x0F,0xF0,0x07,0xE0,0x03,0xC0,0x00,0x00};
static const uint8_t PROGMEM x_16x16[] ={
0x00,0x00,0x02,0x00,0x01,0x00,0x00,0x80,0x00,0x80,0x04,0x00,0x04,0x08,0x24,0x04,
0x24,0x04,0x24,0x02,0x44,0x02,0x44,0x12,0x84,0x10,0x04,0x10,0x03,0xF0,0x00,0x00};
static const uint8_t PROGMEM l_16x16[] ={
0x02,0x00,0x01,0x00,0x7F,0xFC,0x02,0x00,0x44,0x44,0x2F,0x88,0x11,0x10,0x22,0x48,
0x4F,0xE4,0x00,0x20,0x01,0x00,0xFF,0xFE,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00};
static const uint8_t PROGMEM s_16x16[] ={
0x00,0x00,0x1F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFE,0x01,0x00,0x01,0x00,
0x11,0x20,0x11,0x10,0x21,0x08,0x41,0x0C,0x81,0x04,0x01,0x00,0x05,0x00,0x02,0x00};
static const uint8_t PROGMEM b_16x16[] ={
0x20,0x40,0x10,0x40,0x10,0x40,0x07,0xFE,0x84,0x44,0x54,0x40,0x54,0x40,0x17,0xF8,
0x25,0x08,0x24,0x90,0xE4,0x90,0x24,0x60,0x28,0x60,0x28,0x98,0x31,0x0E,0x26,0x04};
static const uint8_t PROGMEM y_16x16[] ={
0x08,0x80,0x08,0x40,0x10,0x68,0x12,0x48,0x22,0x08,0x62,0x10,0xA1,0x10,0x21,0x10,
0x20,0xA0,0x20,0xA0,0x20,0x40,0x20,0xA0,0x21,0x10,0x22,0x0E,0x2C,0x04,0x20,0x00};
void flash()
{
digitalWrite(5, !digitalRead(5));
}
void setup() {
Serial.begin(9600);
MsTimer2::set(500, flash);
MsTimer2::start();
pinMode(13,OUTPUT);
digitalWrite(13,HIGH);
pinMode(5,OUTPUT);
digitalWrite(5,LOW);
pinMode(A0, INPUT);
display.begin(SSD1306_SWITCHCAPVCC);
display.clearDisplay();
display.drawBitmap(20,0,x_16x16,16,16,WHITE);
display.drawBitmap(40,0,l_16x16,16,16,WHITE);
display.drawBitmap(60,0,s_16x16,16,16,WHITE);
display.drawBitmap(80,0,b_16x16,16,16,WHITE);
display.drawBitmap(100,0,y_16x16,16,16,WHITE);
display.display();
Vmax=40;
Vmin=40;
Vmid=40;
b=63;
}
void loop() {
k=0;
a=0;
for(x=0;x<128;x++){
y=63-analogRead(A0)/30;//根据心率传感器输出模拟电压大小来调整倍率,这里输入的是PIN5输出BPM为60的方波.
Buffer[x] = y;
if(Buffer[x]>Vmax)
Vmax=Buffer[x];
if(Buffer[x]<Vmin)
Vmin=Buffer[x];
if(x>1&&k<1&&Buffer[x-2]<Vmid&&Buffer[x-1]>=Vmid)
{ t1=x;
k=1;}
if(k==1&&Buffer[x-1]<Vmid&&Buffer[x]>=Vmid)
{ t2=x;
k=2;}
display.drawLine(a, b, x, y, WHITE);
display.display();
delay(7);
a=x;
b=y;
for(y=20;y<64;y++)
display.drawPixel(0,y,BLACK);
for(y=20;y<64;y++)
display.drawPixel(x+1,y,BLACK);
}
display.partclearDisplay();//自定义函数清除头16行缓存显示.
Vmid=(Vmax+Vmin)/2;
t=t2-t1;
if(t>0)
BPM=1820/t;
else
BPM=0;
display.drawBitmap(8,0,Heart_16x16,16,16,WHITE);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(36,0);
display.println("BPM:");
display.setCursor(90,0);
display.println(BPM);
}
该心率示波仪A0口可接入输出为模拟量的心率模块.(图片显示的是自带的每分60次的方波信号)其特点是与医用心电图形显示模式一样为逐列更新波形..
|
-
|