- #include "stc15f2k60s2.h"
- #include <intrins.h> // 包含头文件
- #define uint unsigned int
- #define uchar unsigned char
- #define ulong unsigned long //宏定义
- #define high 1
- #define low 0
- sbit SCL=P1^0; //串行时钟
- sbit SDA=P1^1; //串行数据
- sbit Xintiao =P1^5; //脉搏检测输入端定义
- sbit speaker =P0^1; //蜂鸣器引脚定义
- void OLED_WrDat(unsigned char IIC_Data); /********Oled数据写入***********/
- void OLED_WrCmd(unsigned char IIC_Command); /********Oled命令写入***********/
- void OLED_P8x16Str(unsigned char x, y,unsigned char ch[]); /*******************功能描述:显示8*16一组标准ASCII字符串 显示的坐标(x,y),y为页范围0~7****************/
- void OLED_Init(void);//Oled初始化函数
- void OLED_Set_Pos(unsigned char x, unsigned char y) ;/*********************OLED 设置坐标************************************/
- void Tim_Init(); //定时器
- void OLED_CLS(void);/*********************OLED复位************************************/
- void Uart_Init(void)//串口初始化程序;115200bps;晶振11.0592MHz;串口方式1
- {
- SCON = 0x50;//设置为八位可变波特率
- AUXR = 0x40;//定时器1设置为1T模式
- TMOD = 0x20;//定时器1为模式2(八位自动重载)
- TL1 = 0xFD;//设置波特率重装值
- TH1 = 0xFD;
- TR1 = 1;//开启定时器1
- ES = 1;//打开串口中断
- EA = 1;//打开总中断
- }
- void Send_Byte(unsigned char dat)//发送一个字节数据的函数
- {
- SBUF = dat;//将dat的值传给SBUF寄存器,然后寄存器发送数据到串口上
- while (TI == 0);//等待数据发送完毕(数据发送完毕后,TI会自己置1)
- TI=0;//手动清零
- }
- void Send_String(char *buf)//定义一个发送字符串的函数
- {
- while(*buf != '\0')//检测这个字符串是否到结尾
- {
- Send_Byte(*buf++);//发送字符串中的每一位
- }
- }
- void delayus(uchar n) //定义延时为us级的延时函数
- {
- while(n--);
- }
- void delay_ms(uint z)//定义延时为ms级的延时函数
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=614;y>0;y--);
- }
- void delay5ms(void) //5ms延时函数
- {
- unsigned char a,b;
- for(b=185;b>0;b--)
- for(a=12;a>0;a--);
- }
- void Delay1000ms()//定义延时为1s的延时函数
- {
- unsigned char i, j, k;
- _nop_();
- _nop_();
- i = 43;
- j = 6;
- k = 203;
- do
- {
- do
- {
- while (--k);
- } while (--j);
- } while (--i);
- }
- uchar Xintiao_Change=0; //心跳改变
- uint Xintiao_Jishu; //心跳计数
- uchar stop;
- uchar View_Data[3];
- uchar View_L[3];
- uchar View_H[3];
- uchar Xintiao_H=100; //脉搏上限
- uchar Xintiao_L=60; //脉搏下限
-
- uchar View_Con; //设置的位(0正常工作,1设置上限,2设置下限)
- uchar View_Change;
- /**********************************************
- //IIC Start
- **********************************************/
- void IIC_Start()
- {
- SCL = high;
- SDA = high;
- SDA = low;
- SCL = low;
- }
- /**********************************************
- //IIC Stop
- **********************************************/
- void IIC_Stop()
- {
- SCL = low;
- SDA = low;
- SCL = high;
- SDA = high;
- }
- /**********************************************
- // 通过I2C总线写一个字节
- **********************************************/
- void Write_IIC_Byte(unsigned char IIC_Byte)
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- if(IIC_Byte & 0x80)
- SDA=high;
- else
- SDA=low;
- SCL=high;
- SCL=low;
- IIC_Byte<<=1;
- }
- SDA=1;
- SCL=1;
- SCL=0;
- }
- void main() //主函数
- {
- OLED_Init(); //液晶初始化函数
- Tim_Init(); //定时器
- OLED_P8x16Str(0x80,16,"Heart Rate: "); //初始化显示
- TR0=1;
- TR1=1; //打开定时器
- while(1)
- {
- if(View_Change)//开始显示变量
- {
- View_Change=0;//变量清零
- if(stop==0) //心率正常时
- {
- if(View_Data[0]==0x30) //最高位为0时不显示
- View_Data[0]=' ';
- }
- else //心率不正常(计数超过5000,也就是两次信号时间超过5s)不显示数据
- {
- View_Data[0]=' ';
- View_Data[1]=' ';
- View_Data[2]=' ';
- }
- switch(View_Con)
- {
- case 0: //正常显示
- {
- OLED_P8x16Str(0x80,16,"Heart Rate: ");//显示一行数据
- OLED_P8x16Str(0xc0,16," ");//显示第二行数据
- OLED_P8x16Str(0xcd,3,View_Data); //第二行显示心率
- break;
- }
- case 1: //设置下限时显示
- {
- OLED_P8x16Str(0x80,16,"Heart Rate: ");//第一行显示心率
- OLED_P8x16Str(0x8d,3,View_Data);
-
- View_L[0]=Xintiao_L/100+0x30; //将下限数据拆字
- View_L[1]=Xintiao_L%100/10+0x30;
- View_L[2]=Xintiao_L%10+0x30;
- if(View_L[0]==0x30) //最高位为0时,不显示
- View_L[0]=' ';
-
- OLED_P8x16Str(0xC0,16,"Warning L : ");//第二行显示下限数据
- OLED_P8x16Str(0xCd,3,View_L);
- break;
- }
- case 2: //设置上限时显示(同上)
- {
- OLED_P8x16Str(0x80,16,"Heart Rate: ");
- OLED_P8x16Str(0x8d,3,View_Data);
-
- View_H[0]=Xintiao_H/100+0x30;
- View_H[1]=Xintiao_H%100/10+0x30;
- View_H[2]=Xintiao_H%10+0x30;
- if(View_H[0]==0x30)
- View_H[0]=' ';
-
- OLED_P8x16Str(0xC0,16,"Warning H : ");
- OLED_P8x16Str(0xCd,3,View_H);
- break;
- }
- }
- }
- }
- }
- switch(Xintiao_Con)
- {
- case 0: //默认Xintiao_Con是为0的
- {
- if(!Xintiao)//每10ms(上面的定时器)检测一次脉搏是否有信号
- {
- Xintiao_Con++;//如果有信号,变量加一,程序就会往下走了
- }
- break;
- }
- case 1:
- {
- if(!Xintiao) //每过10ms检测一下信号是否还存在
- {
- Xintiao_Con++;//存在就加一
- }
- else
- {
- Xintiao_Con=0;//如果不存在了,检测时间很短,说明检测到的不是脉搏信号,可能是其他干扰,将变量清零,跳出此次检测
- }
- break;
- }
- case 2:
- {
- if(!Xintiao)
- {
- Xintiao_Con++;//存在就加一
- }
- else
- {
- Xintiao_Con=0;//如果不存在了,检测时间很短,说明检测到的不是脉搏信号,可能是其他干扰,将变量清零,跳出此次检测
- }
- break;
- }
- case 3:
- {
- if(!Xintiao)
- {
- Xintiao_Con++;//存在就加一
- }
- else
- {
- Xintiao_Con=0;//如果不存在了,检测时间很短,说明检测到的不是脉搏信号,可能是其他干扰,将变量清零,跳出此次检测
- }
- break;
- }
- case 4:
- {
- if(Xintiao)//超过30ms一直有信号,判定此次是脉搏信号,执行以下程序
- {
- if(Xintiao_Change==1)//心率计原理为检测两次脉冲间隔时间计算心率,变量Xintiao_Change第一次脉冲时为0的,所有走下面的else,第二次走这里
- {
- View_Data[0]=(60000/Xintiao_Jishu)/100+0x30;
- View_Data[1]=(60000/Xintiao_Jishu)%100/10+0x30;
- View_Data[2]=(60000/Xintiao_Jishu)%10+0x30;
- if(((60000/Xintiao_Jishu)>=Xintiao_H)||((60000/Xintiao_Jishu)<=Xintiao_L))//心率不在范围内报警
- speaker=1; //蜂鸣器响
- else
- speaker=0; //不响
- View_Change=1; //计算出心率后启动显示
- Xintiao_Jishu=0; //心跳计数清零
- Xintiao_Change=0; //计算出心率后该变量清零,准备下次检测心率
- stop=0; //计算出心率后stop清零
- }
- else//第一次脉冲时Xintiao_Change为0
- {
- Xintiao_Jishu=0; //脉冲计时变量清零,开始计时
- Xintiao_Change=1;//Xintiao_Change置1,准备第二次检测到脉冲时计算心率
- }
- Xintiao_Con=0; //清零,准备检测下一次脉冲
- break;
- }
- }
- }
- }
- /**定时器T0工作函数**/
- void Time0() interrupt 1
- {
- TH0=0xfc; //1ms
- TL0=0x18; //重新赋初值
- Xintiao_Jishu++; //心跳计数加
- if(Xintiao_Jishu==5000)//心跳计数大于5000
- {
- Xintiao_Jishu=0; //数据清零
- View_Change=1; //显示位置1
- Xintiao_Change=0; //置零,准备再次检测
- stop=1; //心跳计数超过5000后说明心率不正常或者没有测出,stop置1
- speaker=0; //关闭蜂鸣器
- }
- }
- /**定时器初始化函数**/
- void Tim_Init()
- {
- EA=1; //打开中断总开关
- ET0=1; //打开T0中断允许开关
- ET1=1; //打开T1中断允许开关
- TMOD=0x11; //设定定时器状态
- TH0=0xfc; //1ms
- TL0=0x18; //赋初值
- TH1=0xd8; //10ms
- TL1=0xf0; //赋初值
- }
- /**在指定地址显示指定数量的指定字符**/
- void OLED_P8x16Str(unsigned char x, y,unsigned char ch[])
- {
- unsigned char c=0,i=0,j=0;
- while (ch[j]!='\0')
- {
- c =ch[j]-32;
- if(x>120){x=0;y++;}
- OLED_Set_Pos(x,y);
- for(i=0;i<8;i++)
- OLED_WrDat(F8X16[c*16+i]);
- OLED_Set_Pos(x,y+1);
- for(i=0;i<8;i++)
- OLED_WrDat(F8X16[c*16+i+8]);
- x+=8;
- j++;
- }
- }
- /*********************OLED写数据************************************/
- void OLED_WrDat(unsigned char IIC_Data)
- {
- IIC_Start();
- Write_IIC_Byte(0x78);
- Write_IIC_Byte(0x40); //write data
- Write_IIC_Byte(IIC_Data);
- IIC_Stop();
- }
- /*********************OLED写命令************************************/
- void OLED_WrCmd(unsigned char IIC_Command)
- {
- IIC_Start();
- Write_IIC_Byte(0x78); //Slave address,SA0=0
- Write_IIC_Byte(0x00); //write command
- Write_IIC_Byte(IIC_Command);
- IIC_Stop();
- }
- /*********************OLED初始化************************************/
- void OLED_Init(void)
- {
- delay(500);//初始化之前的延时很重要!
- OLED_WrCmd(0xae);//--turn off oled panel
- OLED_WrCmd(0x00);//---set low column address
- OLED_WrCmd(0x10);//---set high column address
- OLED_WrCmd(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
- OLED_WrCmd(0x81);//--set contrast control register
- OLED_WrCmd(Brightness); // Set SEG Output Current Brightness
- OLED_WrCmd(0xa1);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
- OLED_WrCmd(0xc8);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
- OLED_WrCmd(0xa6);//--set normal display
- OLED_WrCmd(0xa8);//--set multiplex ratio(1 to 64)
- OLED_WrCmd(0x3f);//--1/64 duty
- OLED_WrCmd(0xd3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
- OLED_WrCmd(0x00);//-not offset
- OLED_WrCmd(0xd5);//--set display clock divide ratio/oscillator frequency
- OLED_WrCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
- OLED_WrCmd(0xd9);//--set pre-charge period
- OLED_WrCmd(0xf1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
- OLED_WrCmd(0xda);//--set com pins hardware configuration
- OLED_WrCmd(0x12);
- OLED_WrCmd(0xdb);//--set vcomh
- OLED_WrCmd(0x40);//Set VCOM Deselect Level
- OLED_WrCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
- OLED_WrCmd(0x02);//
- OLED_WrCmd(0x8d);//--set Charge Pump enable/disable
- OLED_WrCmd(0x14);//--set(0x10) disable
- OLED_WrCmd(0xa4);// Disable Entire Display On (0xa4/0xa5)
- OLED_WrCmd(0xa6);// Disable Inverse Display On (0xa6/a7)
- OLED_WrCmd(0xaf);//--turn on oled panel
- OLED_Set_Pos(0,0);
- }
- /*********************OLED 设置坐标************************************/
- void OLED_Set_Pos(unsigned char x, unsigned char y)
- {
- OLED_WrCmd(0xb0+y);
- OLED_WrCmd(((x&0xf0)>>4)|0x10);
- OLED_WrCmd((x&0x0f)|0x01);
- }
- /*********************OLED复位************************************/
- void OLED_CLS(void)
- {
- unsigned char y,x;
- for(y=0;y<8;y++)
- {
- OLED_WrCmd(0xb0+y);
- OLED_WrCmd(0x01);
- OLED_WrCmd(0x10);
- for(x=0;x<X_WIDTH;x++)
- OLED_WrDat(0);
- }
- }
复制代码
|