MSP430F149 反应速度测试仪 oled 显示
2022-09-15 来源:csdn
/***********************************
利用MSP430设计一款“反应速度测试仪”
在P2.3口接有一高亮度LED(低电平点亮)
P2.3口接有一按键(低电平表示按下)。
要求受试者在看到红灯亮后立即按键。
测量从亮灯到按键之间的时间并显示出来,就是受试者的反应时间
***********************************/
#include 'msp430x14x.h'
#include 'iic.h'
//使用系统自带延时
#define CPU_F ((double)1000000)
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
unsigned int TA_OverflowCnt; //TA溢出次数存放变量
unsigned long int ResponseTime; //执行时间存放变量
void display() //显示
{
//OLED_ShowString(0, 2, 'haha:',16);
// OLED_ShowString(0, 0, '20',16);
//OLED_ShowChar(33, 0, '-',16);
//OLED_ShowChar(57, 0, '-',16);
// OLED_ShowChar(78, 2,':',16);
// OLED_ShowChar(102, 2,':',16);
OLED_ShowCHinese(0,0,0); //显示汉字 反
OLED_ShowCHinese( 16,0,1); //显示汉字 应
OLED_ShowCHinese(32,0,2); //显示汉字 时
OLED_ShowCHinese(48,0,3); //显示汉字 间
OLED_ShowChar(64, 0,':',16);
//OLED_ShowString(0, 6, 'new money:',16);
OLED_ShowNum(0,6,ResponseTime,5,16);
OLED_ShowString(0, 2, 'TAR:',16);
OLED_ShowNum(32,2,TAR,6,16);
OLED_ShowNum(0,4,TA_OverflowCnt,2,16);
}
void main( void )
{
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
//BCSCTL1 = 0; //BTCTL上电不初始化,需要人为清零
Initial_LY096BG30(); //oled 初始化
OLED_Clear();
_EINT();
P2DIR |= BIT3 + BIT7; //红灯所在IO设为输出
_EINT(); //总中断允许
while(1)
{
TA_OverflowCnt = 0; //长执行时间函数TA会溢出,用一变量计数溢出次数
P2OUT &= ~BIT3; //灯亮
TACTL = TASSEL_1 + MC_2 + TAIE +TACLR; //TA清零并开始计时,ACLK,开中断
while(P1IN & BIT0); //等待P1.5按键被按下
TACTL = TASSEL_1 + MC_0; //TA停止计时
P2OUT |= BIT3; //灯灭
ResponseTime = TA_OverflowCnt*65536 + TAR; //读取TA计数值及溢出次数计算总周期
OLED_ShowNum(82,0,ResponseTime,5,16);
ResponseTime = (TA_OverflowCnt*1000000)/32768; //将ACLK周期数换算成毫秒时间值
display(); //显示反应时间测量结果,精确到毫秒
__delay_cycles(3000000); //延迟约3s,重新开始
}
}
#pragma vector = TIMERA1_VECTOR
__interrupt void TA_ISR(void)
{
switch(TAIV)
{
case 2: break;
case 4: break;
case 10:
{
P2OUT ^= BIT7;
TA_OverflowCnt++; //TA每次溢出,溢出次数变量+1
}
break;
}
}
#ifdef iic_c
#define iic_ext
#else
#define iic_ext extern
#endif
#define uchar unsigned char
#define uint unsigned int
#define SET_SCL P2OUT|=BIT0;
#define CLR_SCL P2OUT&=~BIT0;
#define SET_SDA P2OUT|=BIT1;
#define CLR_SDA P2OUT&=~BIT1;
#define SDA_IN P2DIR&=~BIT1;
#define SDA_OUT P2DIR|=BIT1; //SDA P2.1
#define SCL_OUT P2DIR|=BIT0; //SCL P2.0
#define SDA_Read P2IN&BIT1
#define OLED_CMD 0 //写命令
#define OLED_DATA 1 //写数据
iic_ext void delay();
iic_ext void Initial_LY096BG30();
iic_ext void Delay_1ms(unsigned int Del_1ms);
iic_ext void Delay_50ms(unsigned int Del_50ms);
iic_ext void picture_1();
iic_ext void fill_picture(unsigned char fill_Data);
iic_ext uchar IIC_Addresing(uchar addr);
iic_ext void OLED_Clear();
iic_ext void OLED_ShowCHinese(uchar x,uchar y,uchar n);
iic_ext void OLED_ShowString(uchar x,uchar y,uchar *ptr,uchar size);
iic_ext void OLED_ShowChar(uchar x,uchar y,uchar ptr,uchar size);
iic_ext void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[]);
iic_ext void OLED_ShowNum(uchar x,uchar y,uint num,uchar len,uchar size);
iic_ext void OLED_DrawPoint(uint x,uchar y,uchar t);
iic_ext void OLED_Refreash_Gram();
iic_ext void OLED_Fill(uchar x1,uchar y1,uchar x2,uchar y2,uchar dot);
iic_ext void Oled_RunShow(uchar y,uchar num,uchar len,uchar betw);
iic_ext void OLED_ShowOneHZ(uint x,uchar y,uchar chr,uchar size,uchar mode);
iic_ext void OLED_Clear_kuai(uchar x,uchar y,uchar dat);
iic_ext void OLED_ShowHz(uchar x,uchar y,uchar len,uchar size);
iic_ext void OLED_ShowBattery(uchar x,uchar y,uchar no);
iic_ext void OLED_ShowBatCharge(uchar x,uchar y,uchar len,uchar size);
iic_ext void OLED_DS1302_Num(uchar x,uchar y,uint num,uchar size);
//iic_ext void OLED_Show_DS1302(uchar x,uchar y,uchar no);
```c
#define iic_c
#include #include'iic.h' #include'oled.h' //uchar OLED_GRAM0[128][8]; //uchar OLED_GRAM1[128][8]; void delay() { unsigned char q0; for(q0=0;q0<3;q0++) { _NOP(); } } void IIC_Start() { SDA_OUT; SCL_OUT; //设置为输出模式 SET_SDA; SET_SCL; delay(); CLR_SDA; delay(); CLR_SCL; delay(); } void IIC_Stop() { SDA_OUT; SCL_OUT; CLR_SCL; CLR_SDA; delay(); SET_SCL; delay(); SET_SDA; delay(); } uchar Write_IIC_Byte(uchar dat) { uchar ack=0; uchar mask; SDA_OUT; delay(); for(mask=0x80;mask!=0;mask>>=1) { if((mask&dat)==0) { CLR_SDA; } else { SET_SDA; } delay(); SET_SCL; delay(); CLR_SCL; delay(); } SET_SDA; delay(); SDA_IN; SET_SCL; delay(); ack=SDA_Read; //接收应答位 CLR_SCL; delay(); return ack; } /* uchar IIC_Addresing(uchar addr) { uchar ack=0; IIC_Start(); ack= Write_IIC_Byte(addr); IIC_Stop(); return ack; } */ /********************************************** // IIC Write Command **********************************************/ void Write_IIC_Command(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(); } /********************************************** // IIC Write Data **********************************************/ void Write_IIC_Data(unsigned char IIC_Data) //写数据 { IIC_Start(); Write_IIC_Byte(0x78); Write_IIC_Byte(0x40); //write data Write_IIC_Byte(IIC_Data); IIC_Stop(); } void OLED_WR_Byte(uchar dat,uchar cmd) { if(cmd) { Write_IIC_Data(dat); //写数据 } else { Write_IIC_Command(dat); //写指令 } } /******************************************** // fill_Picture ********************************************/ void fill_picture(unsigned char fill_Data) { unsigned char m,n; for(m=0;m<8;m++) { OLED_WR_Byte(0xb0+m,OLED_CMD); OLED_WR_Byte(0x00,OLED_CMD); OLED_WR_Byte(0x10,OLED_CMD); // Write_IIC_Command(0xb0+m); //page0-page1 //Write_IIC_Command(0x00); //low column start address //Write_IIC_Command(0x10); //high column start address for(n=0;n<128;n++) { OLED_WR_Byte(fill_Data,OLED_DATA); } } } #if(0) /****************************************** // picture ******************************************/ void picture_1() //显示图片 { unsigned char x,y; unsigned int i=0; for(y=0;y<8;y++) { Write_IIC_Command(0xb0+y); //页地址 Write_IIC_Command(0x00); //低位 Write_IIC_Command(0x10); //高位 for(x=0;x<128;x++) { Write_IIC_Data(show1[i++]); } } } #endif /***********************Delay****************************************/ void Delay_50ms(unsigned int Del_50ms) // { unsigned int m; for(;Del_50ms>0;Del_50ms--) for(m=6245;m>0;m--); } void Delay_1ms(unsigned int Del_1ms) // { unsigned char j; while(Del_1ms--) { for(j=0;j<123;j++); } } 坐标位置/ void OLED_Set_Pos(uchar x,uchar y) { OLED_WR_Byte(0xb0+y,OLED_CMD); OLED_WR_Byte((x&0x0f),OLED_CMD); OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD); } void OLED_Clear_kuai(uchar x,uchar y,uchar dat) { // uchar pos,bx,temp=0; // pos=7-y/8; OLED_Set_Pos(x,y); OLED_WR_Byte(dat,OLED_DATA); } void OLED_Fill(uchar x1,uchar y1,uchar x2,uchar y2,uchar dot) { uchar x,y; for(x=x1;x for(y=y1;y } // OLED_Refresh_Gram();//更新显示 } /* //更新显存 void OLED_Refresh_Gram() { uint i,n; for(i=0;i<8;i++) { OLED_WR_Byte(0xb0+i,OLED_CMD); //OLED_WR_Byte(0xb0,OLED_CMD); OLED_WR_Byte(0x00,OLED_CMD); OLED_WR_Byte(0x10,OLED_CMD); for(n=0;n<128;n++) { OLED_WR_Byte(OLED_GRAM0[n][i],OLED_DATA); } } } 滚屏清零 void OLEDRun_Clear() { uint i,n; for(i=0;i<8;i++) { for(n=0;n<128;n++) { OLED_GRAM0[n][i]=0; } } OLED_Refresh_Gram(); //更新显示 } //点亮点 void OLED_DrawPoint(uint x,uchar y,uchar t) { uchar pos,bx,temp=0; if(x>255||y>63) return; pos=7-y/8; //第几页 bx=y%8; //页的第几行 temp=1<<(7-bx); //y方向上对应的点 0-7 if(t)OLED_GRAM0[x][pos]|=temp; else OLED_GRAM0[x][pos]&=~temp; OLED_Refresh_Gram(); } 区域填充 0 灭 1亮/ void OLED_Fill(uchar x1,uchar y1,uchar x2,uchar y2,uchar dot) { uchar x,y; for(x=x1;x for(y=y1;y } OLED_Refresh_Gram();//更新显示 } */ void OLED_Clear() //清零 { uchar i,n; for(i=0;i<8;i++) { OLED_WR_Byte(0xb0+i,OLED_CMD); OLED_WR_Byte(0x00,OLED_CMD); OLED_WR_Byte(0x10,OLED_CMD); for(n=0;n<128;n++) OLED_WR_Byte(0,OLED_DATA); } } /指定位置显示ASCLL字符 void OLED_ShowChar(uchar x,uchar y,uchar ptr,uchar size) { uchar c=0,i=0; c=ptr-' '; // if(x>127) {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); } } } ///显示字符串 void OLED_ShowString(uchar x,uchar y,uchar *ptr,uchar size) { uchar j=0; while(ptr[j]!='