您的位置:控制工程论坛网论坛 » 嵌入式系统 » STC12C5412AD 单片机 OCMJ48C液晶 电子钟设计源代码(下)

junhong07

junhong07   |   当前状态:在线

总积分:7915  2024年可用积分:1

注册时间: 2008-01-15

最后登录时间: 2019-06-23

空间 发短消息加为好友

STC12C5412AD 单片机 OCMJ48C液晶 电子钟设计源代码(下)

junhong07  发表于 2009/10/21 20:16:56      1000 查看 0 回复  [上一主题]  [下一主题]

手机阅读

/*****************************************************************************
    函数:unsigned char GetKeyNum(unsigned char keyval)
    出口参数:键值
    入口参数:chanal1 转换经处理后的值
    日期:2007.12.29
    编者:zhangfazhong
    状态:已测通过
*****************************************************************************/
unsigned char GetKeyNum(void)
{
 unsigned char keynum = 0;
 unsigned char j = 0;
 unsigned char i = 0;
  unsigned char hold = 0;
 float prodata = 0;
 PortBitSet(1,1,0x10);  //P1_1设置为输入
 ADCInitial(1,0x11);  //1通道最快速度
 delay(400);   //等待电压稳定
 for (;j<10;j++)
 {
  StartADC();
  while (!(ADC_CONTR&0x10));
  StopADC();
  for (i=0;i<10;i++)
   {
   ch1data[i] = ch1data[i+1];  //将第一位给第0位
   }
  ch1data[9] = ADC_DATA;    //当次采样给第9
//  s_send(ADC_DATA);
 }
//排序
  for(i=0;i<9;i++)   
 {
  for(j=i+1;j<9;j++)
  if (ch1data[i]>ch1data[j])
  {
   hold = ch1data[i];
   ch1data[i] = ch1data[j];
   ch1data[j] = hold;
  }
 }
 for (i=3;i<7;i++)    //求和
 {
  prodata = prodata + ch1data[i];
 }
 prodata = prodata/4;   //去平均值 
// s_send(prodata);
 switch ((unsigned char)prodata)
 {
  case 0x80: keynum = 1; break;
  case 0xab: keynum = 2; break;
  case 0xc0: keynum = 3; break;
  case 0xcd: keynum = 4; break;
  default : keynum = 0;
 }
 return (keynum);
}

/*****************************************************************************
    函数:StartADC()
    出口参数:无
    入口参数:无
    日期:2007.11.06
    编者:zhangfazhong
    状态:已测通过
*****************************************************************************/
void StartADC(void)
{
// IE = IE | 0x40;   //开ADC总中断
// AUXR = AUXR | 0x10;   //ADC中断允许
 ADC_CONTR = ADC_CONTR | 0x80;  //ADC_POWER = 1 ADC上电
// Delay();
 ADC_DATA = 0x00;
 ADC_CONTR = ADC_CONTR | 0x08;  //ADC_START = 1 启动adc
}

/******************************************************************************
    函数:StopADC()
    出口参数:无
    入口参数:无
    日期:2007.11.06
    编者:zhangfazhong
    状态:已测通过
******************************************************************************/
void StopADC(void)
{
 ADC_CONTR = ADC_CONTR & 0xe7;  //ADC_FLG = 0 清中断 ADC_START = 0 停止adc 1110 0111 
}

/*****************************************************************************
    函数:ADCInitial(unsigned char chanal,unsigned char speed)
    出口参数:无
    入口参数:chanal(转换通道),speed(转换速度)
    日期:200711.06
    编者:zhangfazhong
    状态:已测通过
*****************************************************************************/
void ADCInitial(unsigned char chanal,unsigned char speed)
{
 switch (chanal)
 {
  case 0:
   ADC_CONTR = ADC_CONTR & 0xf8;
   break;
  case 1:
   ADC_CONTR = ADC_CONTR | 0x01;
   ADC_CONTR = ADC_CONTR & 0xf9;
   break;
  case 2:
   ADC_CONTR = ADC_CONTR | 0x02;
   ADC_CONTR = ADC_CONTR & 0xfa;
   break;
  case 3:
   ADC_CONTR = ADC_CONTR | 0x03;
   ADC_CONTR = ADC_CONTR & 0xfb;
   break;
  case 4:
   ADC_CONTR = ADC_CONTR | 0x04;
   ADC_CONTR = ADC_CONTR & 0xfc;
   break;
  case 5:
   ADC_CONTR = ADC_CONTR | 0x05;
   ADC_CONTR = ADC_CONTR & 0xfd;
   break;
  case 6:
   ADC_CONTR = ADC_CONTR | 0x06;
   ADC_CONTR = ADC_CONTR & 0xfe;
   break;
  case 7:
   ADC_CONTR = ADC_CONTR | 0x07;
   break;
  default : break;
 }
 switch (speed)
 {
  case 0x11:
   ADC_CONTR = ADC_CONTR | 0x60;
   break;
  case 0x10:
   ADC_CONTR = ADC_CONTR | 0x40;
   ADC_CONTR = ADC_CONTR | 0xdf;
   break;
  case 0x01:
   ADC_CONTR = ADC_CONTR | 0x20;
   ADC_CONTR = ADC_CONTR & 0xbf;
   break;
  case 0x00:
   ADC_CONTR = ADC_CONTR & 0x9f;
   break;
  default : break;
 }
}

/******************************************************************************
*               函数:void ChangTime(void)
*               出口参数:无
*               入口参数:无
*               日期:  2008.01.08
*               编者:zhangfazhong
*               状态:已测 通过
******************************************************************************/
void ChangTime(void)
{
  unsigned int week[8] = {0xd2bb,0xd2bb,0xb6fe,0xc8fd,0xcbc4,0xcee5,0xc1f9,0xc8d5};
                     //一   一  二     三       四   五  六    日
 wr_lcd (comm,0x82); 
 wr_lcd (dat,0xca);//时
 wr_lcd (dat,0xb1);
 
 wr_lcd (comm,0x83); 
 wr_lcd (dat,0xbc);//间
 wr_lcd (dat,0xe4);

 wr_lcd (comm,0x84);
 wr_lcd (dat,0xd0);//校
 wr_lcd (dat,0xa3); 

 wr_lcd (comm,0x85); 
 wr_lcd (dat,0xd7);//准
 wr_lcd (dat,0xbc);

 wr_lcd (comm,0x90); 
 wr_lcd (dat,(unsigned)(caldar.hour/10)+48);//小时
 wr_lcd (dat,caldar.hour%10+48);

 wr_lcd (comm,0x91); 
 wr_lcd (dat,0x3a);//:
 wr_lcd (dat,(unsigned)(caldar.minute/10)+48);//分钟

 wr_lcd (comm,0x92); 
 wr_lcd (dat,caldar.minute%10+48);
 wr_lcd (dat,0x3a);//:

 wr_lcd (comm,0x93); 
 wr_lcd (dat,(unsigned)(caldar.second/10)+48);//秒钟
 wr_lcd (dat,caldar.second%10+48);

 wr_lcd (comm,0x95); 
 wr_lcd (dat,0xd0);//星期
 wr_lcd (dat,0xc7);

 wr_lcd (comm,0x96); 
 wr_lcd (dat,0xc6);//期
 wr_lcd (dat,0xda);

 wr_lcd (comm,0x97); 
 wr_lcd (dat,(unsigned char)(week[caldar.week]>>8));//一 二 三 ....日
 wr_lcd (dat,(unsigned char)week[caldar.week]);

 wr_lcd (comm,0x99); 
 wr_lcd (dat,0xcd);//退出
 wr_lcd (dat,0xcb);
 
 wr_lcd (comm,0x9a); 
 wr_lcd (dat,0xb3);
 wr_lcd (dat,0xf6);

 wr_lcd (comm,0x9d); 
 wr_lcd (dat,0xc8);//确定
 wr_lcd (dat,0xb7);
 
 wr_lcd (comm,0x9e); 
 wr_lcd (dat,0xb6);
 wr_lcd (dat,0xa8);
 
 switch (keycounter2)
 {
  case 1:  //小时调整
   wr_lcd (comm,0x88); 
   wr_lcd (dat,0x1e);
   wr_lcd (dat,0x1e);
   if (keycounter3 == 1)  //加一调整
   {
    if (caldar.hour<23)
    caldar.hour++;
    else
    caldar.hour = 0;
    keycounter3 = 0;
    }
    if (keycounter4 == 1)  //减1调整 
    {
     if (caldar.hour == 0)
    caldar.hour = 23;
    else
    caldar.hour --;
    keycounter4 = 0;
    }
  break;
  case 2:   //分钟调整
   wr_lcd (comm,0x89); 
   wr_lcd (dat,0x20);
   wr_lcd (dat,0x1e);
   wr_lcd (comm,0x8a); 
   wr_lcd (dat,0x1e);
   wr_lcd (dat,0x20);
   if (keycounter3 == 1)  //加一调整
   {
    if (caldar.minute<59)
    caldar.minute++;
    else
    caldar.minute = 0;
    keycounter3 = 0;
    }
    if (keycounter4 == 1)  //减1调整
    {
     if (caldar.minute == 0)
    caldar.minute = 59;
    else
    caldar.minute --;
    keycounter4 = 0;
    }
  break;
  case 3:  //秒钟调整
   wr_lcd (comm,0x8b); 
   wr_lcd (dat,0x1e);
   wr_lcd (dat,0x1e);
   if (keycounter3 == 1)  //清零调整
   {
    caldar.second = 0;
    keycounter3 = 0;
    }
    if (keycounter4 == 1)  //清零调整
    {
    caldar.second = 0;
    keycounter4 = 0;
    }
  break;
  case 4:  //星期调整
   wr_lcd (comm,0x8f); 
   wr_lcd (dat,0x1e);
   wr_lcd (dat,0x1e);
   if (keycounter3 == 1)  //加一调整
   {
    if (caldar.week<7)
    caldar.week++;
    else
    caldar.week = 0;
    keycounter3 = 0;
    }
    if (keycounter4 == 1)  //减1调整
    {
     if (caldar.week <= 1)
    caldar.week = 7;
    else
    caldar.week --;
    keycounter4 = 0;
    }
  break;
  case 5:
   wr_lcd (comm,0x88); //操作成功 请按退出 //
   wr_lcd (dat,0xb2);
   wr_lcd (dat,0xd9);
   wr_lcd (comm,0x89); //
   wr_lcd (dat,0xd7);
   wr_lcd (dat,0xf7);
   wr_lcd (comm,0x8a); //
   wr_lcd (dat,0xb3);
   wr_lcd (dat,0xc9);  
   wr_lcd (comm,0x8b); //
   wr_lcd (dat,0xb9);
   wr_lcd (dat,0xa6);
   wr_lcd (comm,0x8c); //
   wr_lcd (dat,0xa2);
   wr_lcd (dat,0xb0);
   wr_lcd (comm,0x8d); //
   wr_lcd (dat,0xc7);
   wr_lcd (dat,0xeb);
   wr_lcd (comm,0x8e); //
   wr_lcd (dat,0xcd);
   wr_lcd (dat,0xcb);
   wr_lcd (comm,0x8f); //
   wr_lcd (dat,0xb3);
   wr_lcd (dat,0xf6);
  // chn_disp (tab3);

  // keycounter2 = 0;  //若去了以上语句则需加此句
  break;
  case 6:keycounter2 = 0;break;
  default :keycounter2 = 0;
 }
}

/******************************************************************************
*               函数:void ChangCalendar(void)
*               出口参数:无
*               入口参数:无
*               日期:  2008.01.08
*               编者:zhangfazhong
*               状态:待测
******************************************************************************/
void ChangCalendar(void)
{
  unsigned int week[8] = {0xd2bb,0xd2bb,0xb6fe,0xc8fd,0xcbc4,0xcee5,0xc1f9,0xc8d5};
                     //一   一  二     三       四   五  六    日
 wr_lcd (comm,0x82); 
 wr_lcd (dat,0xc8);//日
 wr_lcd (dat,0xd5);
 
 wr_lcd (comm,0x83); 
 wr_lcd (dat,0xc0);//历
 wr_lcd (dat,0xfa);

 wr_lcd (comm,0x84);
 wr_lcd (dat,0xd0);//校
 wr_lcd (dat,0xa3); 

 wr_lcd (comm,0x85); 
 wr_lcd (dat,0xd7);//准
 wr_lcd (dat,0xbc);

 wr_lcd (comm,0x90); 
 wr_lcd (dat,caldar.year/1000 + 48);//
 wr_lcd (dat,(caldar.year/100)%10 + 48);

 wr_lcd (comm,0x91); 
 wr_lcd (dat,(caldar.year/10)%10 + 48);//
 wr_lcd (dat,caldar.year%10 + 48);

 wr_lcd (comm,0x92); //年
 wr_lcd (dat,0xc4);//:
  wr_lcd (dat,0xea);

 wr_lcd (comm,0x93); 
 wr_lcd (dat,caldar.month/10 + 48);
 wr_lcd (dat,caldar.month%10 + 48);

 wr_lcd (comm,0x94); 
 wr_lcd (dat,0xd4);//月
 wr_lcd (dat,0xc2);

 wr_lcd (comm,0x95); 
 wr_lcd (dat,caldar.day/10 + 48);
 wr_lcd (dat,(caldar.day%10) + 48);

 wr_lcd (comm,0x96);  //日
 wr_lcd (dat,0xc8);
 wr_lcd (dat,0xd5);

  switch (keycounter2)
 {
  case 1:  //年份调整
   wr_lcd (comm,0x88); 
   wr_lcd (dat,0x1e);
   wr_lcd (dat,0x1e);
      wr_lcd (comm,0x89); 
   wr_lcd (dat,0x1e);
   wr_lcd (dat,0x1e);
   if (keycounter3 == 1)  //加一调整
   {
    if (caldar.year<2050)
    caldar.year++;
    else
    caldar.year = 2000;
    keycounter3 = 0;
    }
    if (keycounter4 == 1)  //减1调整 
    {
     if (caldar.year == 2000)
    caldar.year = 2050;
    else
    caldar.year --;
    keycounter4 = 0;
    }
  break;
  case 2:   //月份调整
   wr_lcd (comm,0x8b); 
   wr_lcd (dat,0x1e);
   wr_lcd (dat,0x1e);
   if (keycounter3 == 1)  //加一调整
   {
    if (caldar.month<12)
    caldar.month++;
    else
    caldar.month = 1;
    keycounter3 = 0;
    }
    if (keycounter4 == 1)  //减1调整
    {
     if (caldar.month == 1)
    caldar.month = 12;
    else
    caldar.month --;
    keycounter4 = 0;
    }
  break;
  case 3:  //日调整
   wr_lcd (comm,0x8d); 
   wr_lcd (dat,0x1e);
   wr_lcd (dat,0x1e);
   if (keycounter3 == 1)  //加一调整
   {
    if (caldar.day < GetMonthDay(caldar.year,caldar.month))
    caldar.day++;
    else
    caldar.day = 1;
    keycounter3 = 0;
    }
    if (keycounter4 == 1)  //减1调整
    {
     if (caldar.day == 1)
    caldar.day = GetMonthDay(caldar.year,caldar.month);
    else
    caldar.day --;
    keycounter4 = 0;
    }
  break;
  case 4:
   wr_lcd (comm,0x88); //操作成功 请按退出 //
   wr_lcd (dat,0xb2);
   wr_lcd (dat,0xd9);
   wr_lcd (comm,0x89); //
   wr_lcd (dat,0xd7);
   wr_lcd (dat,0xf7);
   wr_lcd (comm,0x8a); //
   wr_lcd (dat,0xb3);
   wr_lcd (dat,0xc9);  
   wr_lcd (comm,0x8b); //
   wr_lcd (dat,0xb9);
   wr_lcd (dat,0xa6);
   wr_lcd (comm,0x8c); //
   wr_lcd (dat,0xa2);
   wr_lcd (dat,0xb0);
   wr_lcd (comm,0x8d); //
   wr_lcd (dat,0xc7);
   wr_lcd (dat,0xeb);
   wr_lcd (comm,0x8e); //
   wr_lcd (dat,0xcd);
   wr_lcd (dat,0xcb);
   wr_lcd (comm,0x8f); //
   wr_lcd (dat,0xb3);
   wr_lcd (dat,0xf6);
  // chn_disp (tab3);

  // keycounter2 = 0;  //若去了以上语句则需加此句
  break;
  case 5:keycounter2 = 0;break;
  default :keycounter2 = 0;
 }

 wr_lcd (comm,0x99); 
 wr_lcd (dat,0xcd);//退出
 wr_lcd (dat,0xcb);
 
 wr_lcd (comm,0x9a); 
 wr_lcd (dat,0xb3);
 wr_lcd (dat,0xf6);

 wr_lcd (comm,0x9d); 
 wr_lcd (dat,0xc8);//确定
 wr_lcd (dat,0xb7);
 
 wr_lcd (comm,0x9e); 
 wr_lcd (dat,0xb6);
 wr_lcd (dat,0xa8);
}

/******************************************************************************
*               函数:void ChangClock(void)
*               出口参数:无
*               入口参数:无
*               日期:  2008.01.08
*               编者:zhangfazhong
*               状态:已测 通过
******************************************************************************/
void ChangClock(void)
{
  wr_lcd (comm,0x82); 
 wr_lcd (dat,0xc4);//闹
 wr_lcd (dat,0xd6);
 
 wr_lcd (comm,0x83); 
 wr_lcd (dat,0xc1);//铃
 wr_lcd (dat,0xe5);

 wr_lcd (comm,0x84);
 wr_lcd (dat,0xc9);//设
 wr_lcd (dat,0xe8); 

 wr_lcd (comm,0x85); 
 wr_lcd (dat,0xb6);//定
 wr_lcd (dat,0xa8);

 wr_lcd (comm,0x92); 
 wr_lcd (dat,(unsigned)(caldar.alarmhour/10)+48);//小时
 wr_lcd (dat,caldar.alarmhour%10+48);

 wr_lcd (comm,0x93); 
 wr_lcd (dat,0x3a);//:
 wr_lcd (dat,(unsigned)(caldar.alarmminute/10)+48);//分钟

 wr_lcd (comm,0x94); 
 wr_lcd (dat,caldar.alarmminute%10+48);
// wr_lcd (dat,0x3a);//:


 wr_lcd (comm,0x99); 
 wr_lcd (dat,0xcd);//退出
 wr_lcd (dat,0xcb);
 
 wr_lcd (comm,0x9a); 
 wr_lcd (dat,0xb3);
 wr_lcd (dat,0xf6);

 wr_lcd (comm,0x9d); 
 wr_lcd (dat,0xc8);//确定
 wr_lcd (dat,0xb7);
 
 wr_lcd (comm,0x9e); 
 wr_lcd (dat,0xb6);
 wr_lcd (dat,0xa8);

  if (clockflag)
    { //开启
      wr_lcd (comm,0x96); 
     wr_lcd (dat,0xbf);
    wr_lcd (dat,0xaa);
    }
  else
    { //关闭
      wr_lcd (comm,0x96); 
    wr_lcd (dat,0xb9);
    wr_lcd (dat,0xd8);
    }
 
 switch (keycounter2)
 {
  case 1:  //小时调整
   wr_lcd (comm,0x8a); 
   wr_lcd (dat,0x1e);
   wr_lcd (dat,0x1e);
   if (keycounter3 == 1)  //加一调整
   {
    if (caldar.alarmhour<23)
    caldar.alarmhour++;
    else
    caldar.alarmhour = 0;
    keycounter3 = 0;
    }
    if (keycounter4 == 1)  //减1调整 
    {
     if (caldar.alarmhour == 0)
    caldar.alarmhour = 23;
    else
    caldar.alarmhour --;
    keycounter4 = 0;
    }
  break;
  case 2:   //分钟调整
   wr_lcd (comm,0x8b); 
   wr_lcd (dat,0x20);
   wr_lcd (dat,0x1e);
   wr_lcd (comm,0x8c); 
   wr_lcd (dat,0x1e);
   wr_lcd (dat,0x20);
   if (keycounter3 == 1)  //加一调整
   {
    if (caldar.alarmminute<59)
    caldar.alarmminute++;
    else
    caldar.alarmminute = 0;
    keycounter3 = 0;
    }
    if (keycounter4 == 1)  //减1调整
    {
     if (caldar.alarmminute == 0)
    caldar.alarmminute = 59;
    else
    caldar.alarmminute --;
    keycounter4 = 0;
    }
  break;
    case 3:  //闹铃开启设置
      wr_lcd (comm,0x8e); 
   wr_lcd (dat,0x1e);
   wr_lcd (dat,0x1e);
      if (keycounter3 == 1)  //加一调整
   {
    clockflag = ~clockflag;
    keycounter3 = 0;
    }
    if (keycounter4 == 1)  //减1调整
    {
     clockflag = ~clockflag;
    keycounter4 = 0;
    }
    break;
  case 4:
   wr_lcd (comm,0x88); //操作成功 请按退出 //
   wr_lcd (dat,0xb2);
   wr_lcd (dat,0xd9);
   wr_lcd (comm,0x89); //
   wr_lcd (dat,0xd7);
   wr_lcd (dat,0xf7);
   wr_lcd (comm,0x8a); //
   wr_lcd (dat,0xb3);
   wr_lcd (dat,0xc9);  
   wr_lcd (comm,0x8b); //
   wr_lcd (dat,0xb9);
   wr_lcd (dat,0xa6);
   wr_lcd (comm,0x8c); //
   wr_lcd (dat,0xa2);
   wr_lcd (dat,0xb0);
   wr_lcd (comm,0x8d); //
   wr_lcd (dat,0xc7);
   wr_lcd (dat,0xeb);
   wr_lcd (comm,0x8e); //
   wr_lcd (dat,0xcd);
   wr_lcd (dat,0xcb);
   wr_lcd (comm,0x8f); //
   wr_lcd (dat,0xb3);
   wr_lcd (dat,0xf6);
  // chn_disp (tab3);

  // keycounter2 = 0;  //若去了以上语句则需加此句
  break;
  case 5:keycounter2 = 0;break;
  default :keycounter2 = 0;
 } 
}

/************************************************************************
  功能:void Alarm(void)
  出口参数:无
  入口参数:无
  时间:2008.01.03
  编者:zhangfazhong
  状态:已测 通过
************************************************************************/
void Alarm(void)
{
 if (caldar.hour == caldar.alarmhour)
  if(caldar.minute == caldar.alarmminute)
   {
        ISD1420_Play(1);
    switch (caldar.second)
          {
            case 0:PLAY = 0;break;
            case 23:PLAY = 1;
            break;
            case 25:PLAY = 0;
            break;
            default:break;
          }
   }
    else
      PLAY = 1;
}

/******************************************************************************
    函数名:Play()
    功能:播放录音
    出口参数:无
    入口参数:播放录音段
    日期:2007.10.23
    编者:zhangfazhong  已测功能实现
******************************************************************************/
void ISD1420_Play(unsigned char playrecord)
{
 unsigned char temp = 0;
   switch(playrecord)
   {
  case 1: ADDRESS0 = 0;  //录音第一段
    ADDRESS1 = 0;
    ADDRESS2 = 0;
    ADDRESS3 = 0;
    break;
  case 2:ADDRESS0 = 1;  //录音第二段
            ADDRESS1 = 0;
      ADDRESS2 = 1;
      ADDRESS3 = 0;
      break;
  case 3:ADDRESS0 = 0;  //录音第三段
            ADDRESS1 = 1;
      ADDRESS2 = 0;
      ADDRESS3 = 1;
      break;
  case 4:ADDRESS0 = 1;  //录音第四段
            ADDRESS1 = 1;
      ADDRESS2 = 1;
      ADDRESS3 = 1;
      break;
  default:break;
   }
}

1楼 0 0 回复