#include<reg52.h> //包含單片機寄存器的頭文件
#include "oled.h"
#include "bmp.h"
#include "adc0832.h"
sbit BEEP =P3^7;//蜂鳴器宏定義
sbit S4 =P3^6;//按鍵宏定義
sbit S5 =P3^5;
sbit S6 =P3^4;
int n,ci,dd[11],jj=0;
u32 rat[3],a=0;
bit w=0;
unsigned long time111=0;
u8 coutne10=0;
u32 counte=0;
void Time_Init(void);
int main(void)
{
u8 x_couter=0;
u8 t=0,waveform_last,waveform,alarm_l=60,alarm_h=100;
u8 key=0;
Time_Init();//定時器初始化
OLED_Init(); //初始化OLED
OLED_Clear(); //清屏
OLED_ShowCHinese(0,6,0);//顯示“心"
OLED_ShowCHinese(18,6,1);//顯示"率"
OLED_ShowString(32,6, ":");
OLED_ShowString(0,4, "L");
OLED_ShowString(8,4, ":");
OLED_ShowString(64,4, "H");
OLED_ShowString(72,4, ":");
while(1)
{
DrawLine_Y(0,40,64,1);//畫一條y軸的線
if(S4==0)//s4按下選擇修改低/高報警值
{
delay_ms(5);//按鍵消抖
if(S4==0)
{
while(!S4);
key++;
}
}
if(key%2==0)
OLED_ShowString(120,4, "L"); //顯示當前選擇修改的對象
else
OLED_ShowString(120,4, "H");
if(S6==0)//S6按下增加報警值
{
delay_ms(5);
if(S6==0)
{
while(!S6);//按鍵消抖
if(key%2==0)
{
alarm_l++;
}
else
{
alarm_h++;
}
}
}
if(S5==0)//s5按下減小報警值
{
delay_ms(5);//按鍵消抖
if(S5==0)
{
while(!S5);
if(key%2==0)
{
alarm_l--;
}
else
{
alarm_h--;
}
}
} ////////////按鍵掃描
OLED_ShowNum(15,4,alarm_l,3,16);//顯示ASCII字符的碼值 alarm_l
OLED_ShowNum(80,4,alarm_h,3,16);//顯示ASCII字符的碼值 alarm_h
OLED_ShowNum(42,6,ci,3,16); //顯示心率
t=(adc0832(0))*25/255;//讀取adc0832的ad值
x_couter=x_couter+2;
if(x_couter>=127)//判斷顯示是否超出屏幕,
x_couter=0; //從起點開始顯示
waveform_last= waveform;//上次ad數值
waveform= t;//本次ad數值
DrawLine_Y(x_couter+3,40,64,0);//清除上次顯示的波形
DrawLine(x_couter,waveform_last+40,x_couter+2,waveform+40,1 ); //脈沖波形顯示
if(((ci<alarm_l)||(ci>alarm_h))&&(ci!=0))
{
BEEP=0;//異常報警
}
else
{
BEEP=1;
}
}
}
/********************************************************************
函數名稱: void Time_Init(void)
功能簡介: 定時器中斷初始化
入口參數: 無
返回值 :無
*********************************************************************/
void Time_Init(void)
{
IT0=1; //INT0下降沿中斷
EX0=1; //允許INT1中斷
TMOD=0x11; //定時器0/1,16位定時器,方式1
TH0=0x0;//初值全部為0,
TL0=0x0;
TH1=(65536-5000)/256;//初值全部為5ms
TL1=(65536-5000)%256;//初
ET0=1;//使能定時器0
ET1=1;
EA=1;//使能總中斷
TR0=1;//啟動定時器0
TR1=1;//啟動定時器0
}
/********************************************************************
函數名稱: external0() interrupt 0
功能簡介: 外部0中斷函數
入口參數: 無
返回值 :無
*********************************************************************/
external0() interrupt 0//外部中斷服務程序,優先級為0
{
int k,j,tmp;
w=~w;
if(w==0)
{
EX0=0;
ET0=1;
TR0=1;//
TH0=0x0;
TL0=0x0;
n=0;
}
else
{
time111=n*65536+TH0*256+TL0;//us,讀取一次脈搏的時間
counte=60000000/time111; //計算一分鐘的脈搏次數
if(counte>50&&counte<140)//排除異常的脈搏,取合理值
{
dd[jj]=counte;
jj++;
}
if(jj>11)
{
jj=0;
for(j=0;j<10;j++)//采用冒泡排序進行排序
{ for(k=0;k<10-j;k++)
{
if(dd[k]>dd[k+1])
{
tmp=dd[k+1];
dd[k+1]=dd[k];
dd[k]=tmp;
}
}
}
if(dd[6]>50&&dd[6]<140&&dd[3]>50&&dd[3]<140&&dd[7]>50&&dd[7]<140)
{
ci=(dd[5]+dd[6]+dd[7])/3; //采用冒泡排序以后進行數字中值濾波
}
}
TH0=0x0;//定時器重新清零,為讀取下次脈搏做準備
TL0=0x0;
n=0;
}
}
/********************************************************************
函數名稱: void Time0()interrupt 1
功能簡介: 定時器0中斷函數
入口參數: 無
返回值 :無
*********************************************************************/
void Time0()interrupt 1//優先級為1
{
n++;//
}
void Time1()interrupt 3//優先級為3
{
TH1=(65536-5000)/256;//定時器內部進行重新設值
TL1=(65536-5000)%256;//
if(n>20)//當時間大于n*65536us后脈沖沒有變化。說明手指沒有放在傳感器上,所有數據重新清零,等待測量
{
n=0;
jj=0;
ci=0;
}
else
{
if(w==0&&EX0==0)
{
EX0=1;
}
}
}
#include "oled.h"
//#include "stdlib.h"
#include "oledfont.h"
//#include "delay.h"
//OLED的顯存
//存放格式如下.
//[0]0 1 2 3 ... 127
//[1]0 1 2 3 ... 127
//[2]0 1 2 3 ... 127
//[3]0 1 2 3 ... 127
//[4]0 1 2 3 ... 127
//[5]0 1 2 3 ... 127
//[6]0 1 2 3 ... 127
//[7]0 1 2 3 ... 127
//float adcx; //采集到的心率的原始ADC值,每次手放到傳感器上的時候會輸出一段時間的0
//u8 display_flag = 0; //顯示界面標志 0--顯示主界面 1--顯示心率界面
//u8 alarm = 0; //警報變量: 0--不報警 1--報警
//u8 alarm_en = 0; //報警使能標志位,在手放上之后才使能
//u8 waveform[128] = {0}; //波形采集數組,采集128個點,OLED的寬度為128個像素
//u8 waveform_copy[128] = {0}; //波形采集數組,采集128個點,OLED的寬度為128個像素
//u8 waveform_flag = 0; //波形采樣時間計數,采樣128次之后才一次性顯示出來
void delay_ms(unsigned int ms)
{
unsigned int a;
while(ms)
{
a=1800;
while(a--);
ms--;
}
return;
}
#if OLED_MODE==1
//向SSD1106寫入一個字節。
//dat:要寫入的數據/命令
//cmd:數據/命令標志 0,表示命令;1,表示數據;
void OLED_WR_Byte(u8 dat,u8 cmd)
{
DATAOUT(dat);
if(cmd)
OLED_DC_Set();
else
OLED_DC_Clr();
OLED_CS_Clr();
OLED_WR_Clr();
OLED_WR_Set();
OLED_CS_Set();
OLED_DC_Set();
}
#else
//向SSD1306寫入一個字節。
//dat:要寫入的數據/命令
//cmd:數據/命令標志 0,表示命令;1,表示數據;
void OLED_WR_Byte(u8 dat,u8 cmd)
{
u8 i;
if(cmd)
OLED_DC_Set();
else
OLED_DC_Clr();
OLED_CS_Clr();
for(i=0;i<8;i++)
{
OLED_SCLK_Clr();
if(dat&0x80)
{
OLED_SDIN_Set();
}
else
OLED_SDIN_Clr();
OLED_SCLK_Set();
dat<<=1;
}
OLED_CS_Set();
OLED_DC_Set();
}
#endif
void OLED_Set_Pos(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)|0x01,OLED_CMD);
}
//開啟OLED顯示
void OLED_Display_On(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令
OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON
OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON
}
//關閉OLED顯示
void OLED_Display_Off(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令
OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF
OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF
}
//清屏函數,清完屏,整個屏幕是黑色的!和沒點亮一樣!!!
void OLED_Clear(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte (0xb0+i,OLED_CMD); //設置頁地址(0~7)
OLED_WR_Byte (0x00,OLED_CMD); //設置顯示位置—列低地址
OLED_WR_Byte (0x10,OLED_CMD); //設置顯示位置—列高地址
for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
} //更新顯示
}
//在指定位置顯示一個字符,包括部分字符
//x:0~127
//y:0~63
//mode:0,反白顯示;1,正常顯示
//size:選擇字體 16/12
void OLED_ShowChar(u8 x,u8 y,u8 chr)
{
unsigned char c=0,i=0;
c=chr-' ';//得到偏移后的值
if(x>Max_Column-1){x=0;y=y+2;}
if(SIZE ==16)
{
OLED_Set_Pos(x,y);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
OLED_Set_Pos(x,y+1);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
}
else {
OLED_Set_Pos(x,y+1);
for(i=0;i<6;i++)
OLED_WR_Byte(F6x8[c][ i],OLED_DATA);[ i]
}
}
//m^n函數
u32 oled_pow(u8 m,u8 n)
{
u32 result=1;
while(n--)result*=m;
return result;
}
//顯示2個數字
//x,y :起點坐標
//len :數字的位數
//size:字體大小
//mode:模式 0,填充模式;1,疊加模式
//num:數值(0~4294967295);
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size2)
{
u8 t,temp;
u8 enshow=0;
for(t=0;t<len;t++)
{
temp=(num/oled_pow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
OLED_ShowChar(x+(size2/2)*t,y,' ');
continue;
}else enshow=1;
}
OLED_ShowChar(x+(size2/2)*t,y,temp+'0');
}
}
//顯示一個字符號串
void OLED_ShowString(u8 x,u8 y,u8 *chr)
{
unsigned char j=0;
while (chr[j]!='\0')
{ OLED_ShowChar(x,y,chr[j]);
x+=8;
if(x>120){x=0;y+=2;}
j++;
}
}
//顯示漢字
void OLED_ShowCHinese(u8 x,u8 y,u8 no)
{
u8 t,adder=0;
OLED_Set_Pos(x,y);
for(t=0;t<16;t++)
{
OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
adder+=1;
}
OLED_Set_Pos(x,y+1);
for(t=0;t<16;t++)
{
OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
adder+=1;
}
}
/***********功能描述:顯示顯示BMP圖片128×64起始點坐標(x,y),x的范圍0~127,y為頁的范圍0~7*****************/
void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[])
{
unsigned int j=0;
unsigned char x,y;
if(y1%8==0) y=y1/8;
else y=y1/8+1;
for(y=y0;y<y1;y++)
{
OLED_Set_Pos(x0,y);
for(x=x0;x<x1;x++)
{
OLED_WR_Byte(BMP[j++],OLED_DATA);
}
}
}
u8 OLED_GRAM[16];
//u8 OLEDGRAM1[128];
u8 last_y,last_pos;
//?????LCD
void OLED_Refresh_Gram(void)
{
// u8 i,n;
// for(i=0;i<8;i++)
// {
// OLED_WR_Byte (0xb0+i,OLED_CMD); //?????(0~7)
// OLED_WR_Byte (0x00,OLED_CMD); //??????????
// OLED_WR_Byte (0x10,OLED_CMD); //??????????
// OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
// OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD);
//
// OLED_WR_Byte(OLED_GRAM[ i],OLED_DATA); [ i]
// }
}
//??
//x:0~127
//y:0~63
//t:1 ?? 0,??
void OLED_DrawPoint(u8 x,u8 y,u8 t)
{
u8 i;
u8 pos,bx,temp=0;
if(x>127||y>63)return;
pos=7-y/8;
bx=y%8;
temp=1<<(7-bx);
if(last_y!=pos)
{
for(i=0;i<32;i++)
OLED_GRAM[ i]=0;[ i]
}
last_y=pos;
i=x/16;
switch(i)//屏幕分割處理,屏幕位8*128,c51 ram不夠屏幕刷新
{
case 0:if(t)OLED_GRAM[x]|=temp;
else OLED_GRAM[x]&=~temp;break;
case 1:if(t)OLED_GRAM[x-16]|=temp;
else OLED_GRAM[x-16]&=~temp;break;
case 2:if(t)OLED_GRAM[x-32]|=temp;
else OLED_GRAM[x-32]&=~temp;break;
case 3:if(t)OLED_GRAM[x-48]|=temp;
else OLED_GRAM[x-48]&=~temp;break;
case 4:if(t)OLED_GRAM[x-64]|=temp;
else OLED_GRAM[x-64]&=~temp;break;
case 5:if(t)OLED_GRAM[x-80]|=temp;
else OLED_GRAM[x-80]&=~temp;break;
case 6:if(t)OLED_GRAM[x-96]|=temp;
else OLED_GRAM[x-96]&=~temp;break;
case 7:if(t)OLED_GRAM[x-112]|=temp;
else OLED_GRAM[x-112]&=~temp;break;
default :break ;
}
OLED_WR_Byte (0xb0+pos,OLED_CMD); //?????(0~7)
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
OLED_WR_Byte((x&0x0f)|0x00,OLED_CMD);
switch(i)
{
case 0:OLED_WR_Byte(OLED_GRAM[x],OLED_DATA);
break;
case 1:OLED_WR_Byte(OLED_GRAM[x-16],OLED_DATA);
break;
case 2:OLED_WR_Byte(OLED_GRAM[x-32],OLED_DATA);
break;
case 3:OLED_WR_Byte(OLED_GRAM[x-48],OLED_DATA);
break;
case 4:OLED_WR_Byte(OLED_GRAM[x-64],OLED_DATA);
;break;
case 5:OLED_WR_Byte(OLED_GRAM[x-80],OLED_DATA);
break;
case 6:OLED_WR_Byte(OLED_GRAM[x-96],OLED_DATA);
break;
case 7:OLED_WR_Byte(OLED_GRAM[x-112],OLED_DATA);
break;
default :break ;
}
OLED_WR_Byte(0x00,OLED_DATA);
OLED_WR_Byte(0x00,OLED_DATA);
}
void DrawLine_X(unsigned char X0,unsigned char X1,unsigned char Y,unsigned char Color)
{
unsigned char Temp;
if(X0>X1)
{
Temp=X1;
X1=X0;
X0=Temp;
}
for(;X0<=X1;X0++)
{
OLED_DrawPoint(X0,Y,Color);
}
}
void DrawLine_Y(unsigned char X,unsigned char Y0,unsigned char Y1,unsigned char Color)
{
unsigned char Temp;
if(Y0>Y1)
{
Temp=Y1;
Y1=Y0;
Y0=Temp;
}
for(;Y0<=Y1;Y0++)
{
OLED_DrawPoint(X,Y0,Color);
}
}
void DrawLine( unsigned char StartX,unsigned char StartY,unsigned char EndX,unsigned char EndY,unsigned char Color )
{
int t ,distance;
int x = 0 , y = 0, delta_x,delta_y ;
char incx, incy ;
delta_x =EndX-StartX ;
delta_y =EndY-StartY ;
if(delta_x>0)
{
incx = 1;
}
else if(delta_x == 0)
{
DrawLine_Y( StartX,StartY,EndY,Color );
return ;
}
else
incx = -1;
if( delta_y > 0 )
{
incy = 1 ;
}
else if(delta_y== 0)
{
DrawLine_X( StartX,EndX, StartY,Color );
return ;
}
else
{
incy = -1;
}
if(EndY>=45)
{
L=1;
}
else
L=0;
delta_x = myabs( delta_x );
delta_y = myabs( delta_y );
if( delta_x > delta_y)
{
distance = delta_x ;
}
else
{
distance = delta_y ;
}
OLED_DrawPoint( StartX ,StartY, Color );
/* Draw Line*/
for( t = 0 ; t <= distance+1 ; t++ )
{
OLED_DrawPoint( StartX,StartY,Color );
x += delta_x ;
y += delta_y ;
if( x > distance )
{
x -=distance;
StartX += incx ;
}
if(y > distance)
{
y -= distance;
StartY += incy;
}
}
}
int myabs(int a)
{
int temp;
if(a<0) temp=-a;
else temp=a;
return temp;
}
//初始化
void OLED_Init(void)
{
OLED_RST_Set();
delay_ms(100);
OLED_RST_Clr();
delay_ms(100);
OLED_RST_Set();
OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
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 Set Mapping RAM Display Start Line (0x00~0x3F)
OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
OLED_WR_Byte(0x00,OLED_CMD);//-not offset
OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
OLED_WR_Byte(0x12,OLED_CMD);
OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
OLED_WR_Byte(0x02,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
OLED_WR_Byte(0xa0,OLED_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
OLED_WR_Byte(0xc0,OLED_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
OLED_Clear();
OLED_Set_Pos(0,0);
}
|