//1.在oled屏上面显示出年月日,小时,分钟,秒,可以通过按键来调整时间
//
//2.一个闹钟功能,通过按键设置闹钟的时间,到点了需要蜂鸣器响和LED闪烁
//
//3.一个倒计时,跟闹钟差不多,也是通过按键设置时间,到时间了LED闪烁
//OLED----MSP430
//VCC-----3.3V
//GND-----GND
//D0------P3.2
//D1------P3.0
//RES-----P2.0
//DC------P2.2
//CS------P8.1
cpp memset(buf, 0, sizeof(buf));
sprintf(buf, "m%d ", mode);
OLED_ShowString(100, 16, buf, 16);
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02d:%02d:%02d", alarm_h, alarm_m, alarm_s);
OLED_ShowString(0, 32, buf, 16);
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02d:%02d:%02d", DJS_h, DJS_m, DJS_s);
OLED_ShowString(0, 48, buf, 16);
OLED_Refresh_Gram(); //Update display to OLED
while (1)
{
if ((P2IN & BIT1) == 0)
{
if ((P2IN & BIT1) == 0)
{
mode++;
if (mode == 14)
mode = 0;
memset(buf, 0, sizeof(buf));
sprintf(buf, "m%d ", mode);
OLED_ShowString(100, 16, buf, 16);
// while ((P2IN & BIT1) == 0)
// ;
}
}
else if ((P1IN & BIT1) == 0)
{
if ((P1IN & BIT1) == 0)
{
if (mode == 1)
{
RTCCTL01 = RTCHOLD + RTCMODE + RTCTEV_0; //关闭RTC
RTCYEAR++;
if (RTCYEAR == 2030)
RTCYEAR = 2010;
RTCCTL0 |= RTCRDYIE; /* RTCRDY中断使能 */
RTCCTL01 &= ~RTCHOLD; //打开RTC
}
else if (mode == 2)
{
RTCCTL01 = RTCHOLD + RTCMODE + RTCTEV_0; //关闭RTC
RTCMON++;
if (RTCMON == 13)
RTCMON = 1;
RTCCTL0 |= RTCRDYIE; /* RTCRDY中断使能 */
RTCCTL01 &= ~RTCHOLD; //打开RTC
}
else if (mode == 3)
{
RTCCTL01 = RTCHOLD + RTCMODE + RTCTEV_0; //关闭RTC
RTCDAY++;
if (RTCDAY == 31)
RTCDAY = 1;
RTCCTL0 |= RTCRDYIE; /* RTCRDY中断使能 */
RTCCTL01 &= ~RTCHOLD; //打开RTC
}
else if (mode == 4)
{
RTCCTL01 = RTCHOLD + RTCMODE + RTCTEV_0; //关闭RTC
RTCHOUR++;
if (RTCHOUR == 24)
RTCHOUR = 0;
RTCCTL0 |= RTCRDYIE; /* RTCRDY中断使能 */
RTCCTL01 &= ~RTCHOLD; //打开RTC
}
else if (mode == 5)
{
RTCCTL01 = RTCHOLD + RTCMODE + RTCTEV_0; //关闭RTC
RTCMIN++;
if (RTCMIN == 60)
RTCMIN = 0;
RTCCTL0 |= RTCRDYIE; /* RTCRDY中断使能 */
RTCCTL01 &= ~RTCHOLD; //打开RTC
}
else if (mode == 6)
{
RTCCTL01 = RTCHOLD + RTCMODE + RTCTEV_0; //关闭RTC
RTCSEC++;
if (RTCSEC == 60)
RTCSEC = 0;
RTCCTL0 |= RTCRDYIE; /* RTCRDY中断使能 */
RTCCTL01 &= ~RTCHOLD; //打开RTC
}
else if (mode == 7)
{
alarm_h++;
if (alarm_h == 24)
alarm_h = 0;
}
else if (mode == 8)
{
alarm_m++;
if (alarm_m == 60)
alarm_m = 0;
}
else if (mode == 9)
{
alarm_s++;
if (alarm_s == 60)
alarm_s = 0;
}
else if (mode == 10)
{
DJS_h++;
if (DJS_h == 24)
DJS_h = 0;
}
else if (mode == 11)
{
DJS_m++;
if (DJS_m == 60)
DJS_m = 0;
}
else if (mode == 12)
{
DJS_s++;
if (DJS_s == 60)
DJS_s = 0;
}
else if (mode == 13)
{
//开始倒计时
DJS_IFG = 0;
DJS_start = 1;
}
// while ((P1IN & BIT1) == 0)
// ;
}
}
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02d:%02d:%02d", alarm_h, alarm_m, alarm_s);
OLED_ShowString(0, 32, buf, 16);
memset(buf, 0, sizeof(buf));
sprintf(buf, "%d-%02d-%02d", nian, yue, ri);
OLED_ShowString(0, 0, buf, 16);
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02d:%02d:%02d", shi, fen, miao);
OLED_ShowString(0, 16, buf, 16);
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02d:%02d:%02d", DJS_h, DJS_m, DJS_s);
OLED_ShowString(0, 48, buf, 16);
OLED_Refresh_Gram(); //Update display to OLED
//闹钟闪烁灯
if (alarm_ifg == 1)
{
P1OUT ^= BIT0;
delay_ms(50);
}
//倒计时
if (DJS_IFG == 1)
{
P4OUT ^= BIT7;
delay_ms(50);
}
}
}
// Timer0 A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR(void)
{
int DJS = 0;
if (DJS_start == 1)
{
DJS = DJS_h * 3600 + DJS_m * 60 + DJS_s; //倒计时多少秒
if (DJS > 0)
{
DJS--;
DJS_h = DJS / 3600;
DJS_m = DJS % 3600 / 60;
DJS_s = DJS % 3600 % 60;
}
else
{
DJS_start = 0; //不开始了
DJS_IFG = 1; //倒计时结束标志
DJS_time = 10; //闹腾10s
BEEPOPEN; //打开PWM
}
}
if (DJS_IFG == 1)
{
if (DJS_time > 0)
{
DJS_time--; //闹钟时间10s
}
else if (DJS_time == 0)
{
DJS_IFG = 0; //关闭定时器结束标志
BEEPCLOSE; //关PWM
P4OUT &= ~(BIT7); //关灯0
}
}
if (alarm_ifg == 1)
{
if (alarm_time > 0)
{
alarm_time--; //闹钟时间10s
}
else if (alarm_time == 0)
{
alarm_ifg = 0; //关闭闹钟
BEEPCLOSE; //关PWM
P1OUT &= ~(BIT0); //关灯0
}
}
}
cpphttps://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2
本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!