⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 449test.c

📁 430所有的演示程序示范
💻 C
字号:
//
//
//	www.mcu-china.com
//      www.mcu430.com  
//
//    4 键 时钟程序
//    左边第一键 调整确定/ADC转换键
//    左边第二键 调整时钟 调整时分秒选择  蜂鸣器测试
//    右边第一键 加
//    右边第二键 减 发光二极管测试
//
//     在没有按调整时间键时, 左边第一键 为 时间显示与ADC转换的切换
//
#include <msp430x44x.h>
#include <math.h>
# define  uint unsigned int
# define  uchar unsigned char


uchar time_buffer[3]={17,35,0};
uchar key_do=0;
uchar key_buffer=0;
uchar input_buffer[6]={0};
uchar input_con=0;
uchar time_sec01=0;
uchar lcdflash_temp=0;
uchar disp_buffer[6]={0};
uchar alr1[3]={06,30,00};
uchar alr2[3]={12,30,00};
uchar alr_flg=0;
uchar fun_con=2;

uchar adc_f = 0 ;

# define  a 0x10
# define  b 0x20
# define  c 0x40
# define  d 0x80
# define  e 0x1
# define  f 0x2
# define  g 0x4
# define  h 0x8

const char distab_asc[]=
      {
      a+e+f+d+c, f,     e+c+f+h+d,  0,    c+f+h,      0,      c+f+h+d,  f,    //abcd
      a+e+c+f+h,0,      a+e+c+f,    0,    a+e+h+c+d,  f,      e+c+f+d,  f,    //efgh
      d,        f,      d+h,        f,    d,          f+e+h,  e+f+h,0,        //ijkl
      e+f+a+d,  f+a+b+c, e+f+b,    b+c+h, c+f+h+d,    0,      a+e+f+c,  f,    //mnop
      a+e+d+c,  f+d,    d,      a+f+b+g+h,a+e+c+d+h,  0,      a+d,      a+f,  //qrst
      e+f+h+d,  f,      e+f+g,  e,        e+f+h+d,    f+d+b+c, b+g,     e+h,  //uvwx
      b+d,      e,      c+g+h,  0                                             //yz
      };


const char distab_mi[]=
        {
        0x1b,0xf0,
        0x80,0x2,
        0x5a,0xb4,
        0x58,0xf4,
        0xc1,0x06,
        0x59,0xd4,
        0x5b,0xd4,
        0x10,0x70,
        0x5b,0xf4,
        0x59,0xf4
	};

////////////////////////////
//  160 lcd
////////////////////////////

const char distab[]=
        {
        0xaf,0x06,0x6d,0x4f,
	0xc6,0xcb,0xeb,0x0e,
	0xef,0xcf, 0xee,0xe3,
	0x61,0x67,0xe9,0xe8
	};

/*
/////////////////////////////////
//  91 lcd
/////////////////////////////////
const char distab[]={
        0x7d,0x60,0x3e,0x7a,
        0x63,0x5b,0x5f,0x70,
        0x7f,0x7b,0x77,0x4f,
        0x0e,0x6e,0x1f,0x17,
        
        0x7d+0x80,0x60+0x80,0x3e+0x80,0x7a+0x80,
        0x63+0x80,0x5b+0x80,0x5f+0x80,0x70+0x80,
        0x7f+0x80,0x7b+0x80,0x77+0x80,0x4f+0x80,
        0x0e+0x80,0x6e+0x80,0x1f+0x80,0x17+0x80
        };

*/


void delay(int x)   // 5*x+12  nop
      {             //128- 23    256-49
      while(x--);   
      }             //192- 36    280- 54   448- 87
      
void time_dis(void)
    {
      LCDMEM[7]=0xa;
      LCDMEM[8]=0x90;
      LCDMEM[9]=0x12;
      LCDMEM[10]=0x80;
      LCDMEM[11]=0x2;
      LCDMEM[12]=0x93;
      LCDMEM[13]=0x72;
      LCDMEM[14]=0x5b;
      LCDMEM[15]=0x94;
    }

void time_change(void)
    {
      LCDMEM[10]=0x1;
      LCDMEM[11]=0x1;
      LCDMEM[12]=0x1;
      LCDMEM[13]=0x1;
    }
    
    
void clrlcd(void)
     {
     uchar i=0;
     for(i=0;i<20;i++)
        LCDMEM[i]=0;
     }


void main(void)
{ 
  WDTCTL = WDTPW + WDTHOLD;             // Stop WDT
  LCDCTL = 0XFD;
  BTCTL =  BTFRFQ1;                      // STK LCD freq 
  P5SEL =  0xFC;                         // Common and Rxx all selected
  FLL_CTL0 |= XCAP14PF;                 // Configure load caps

  TACTL = TASSEL0 + TACLR;              // ACLK, clear TAR
  CCTL0 = CCIE;                         // CCR0 interrupt enabled
  CCR0 = 66;
  P5DIR |= 0x02;                        // P5.1 output
  TACTL |= MC1;                         // Start Timer_A in continuous mode

  P1DIR = 0XFF ;
  P6DIR = 0XFF ;
  P6SEL = 0X01 ;
  P6OUT = 0XFF ;
  
    P2DIR=0xf0;
    P2OUT=0X00;
    P2IE=0X0f;
    P2IES=0x0f;
    P2IFG=0;
    clrlcd();
    _EINT();
      
  for(;;)                              
  {
    _BIS_SR(LPM3_bits);                    // CPU off
    _NOP();                             // Required only for C-spy
  }
}




void dc_adc(void)
    {
        int i ;
        long dc_adc1=0;

        P6OUT &= ~BIT7 ;
        P6SEL = 0x01;                               // Enable A/D channel inputs
          ADC12CTL0 = ADC12ON+MSC+SHT0_2+ REFON  ;  //+ REFON ;           // Turn on ADC12, set sampling time
          delay(20000);
          ADC12CTL1 = SHP  ;                // Use sampling timer, single sequence
          ADC12MCTL0 = INCH_0 + SREF_1;     // ref+=1.5v, channel = A0
          ADC12CTL0 |= ENC;                 // Enable conversions
        delay(16000);
        for(i=0;i<256;i++)
            {
                ADC12CTL0 |= ADC12SC;       // Start conversion
                while ((ADC12IFG & BIT0)==0);
                dc_adc1 = dc_adc1 + ADC12MEM0;
            }
          dc_adc1 = dc_adc1 >> 8 ;
          ADC12CTL0  &=  ~ENC;

          ADC12CTL0     =   0   ;
          ADC12CTL1     =   0   ;
          ADC12MCTL0    =   0   ;  
          
        for(i=0;i<4;i++)
            {
                LCDMEM[i]= distab[dc_adc1%10];
                dc_adc1 = dc_adc1 / 10 ;
            }

    }



// Timer A0 interrupt service routine
//interrupt[TIMERA0_VECTOR] void Timer_A (void)
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A(void)
  {
  uchar i=0;
  CCR0 += 8192; 
  LCDMEM[16]=distab_asc[time_buffer[2]*2];
  LCDMEM[17]=distab_asc[time_buffer[2]*2+1];
  time_sec01++;
  if(time_sec01==8)
  {
    P6OUT |= BIT4 ;
    P1OUT = 0 ;
    time_sec01=0;
    time_buffer[2]++;
    if(time_buffer[2]==60)
       {
       time_buffer[2]=0;
       time_buffer[1]++;
       if(time_buffer[1]==60)
            {
            time_buffer[1]=0;
            time_buffer[0]++;
            if(time_buffer[0]==24)
                 time_buffer[0]=0;            
            }       
       }
      disp_buffer[0]=distab[time_buffer[2]%10];
      disp_buffer[1]=distab[time_buffer[2]/10];
      disp_buffer[2]=distab[time_buffer[1]%10];
      disp_buffer[3]=distab[time_buffer[1]/10];
      disp_buffer[4]=distab[time_buffer[0]%10];
      disp_buffer[5]=distab[time_buffer[0]/10];
  //    for(i=0;i<6;i++)
  //      LCDMEM[i]=disp_buffer[i];
     }

  if(adc_f == 0xff)
      {
          dc_adc();
      }
  
  else if(input_con==0)
       {
       for(i=0;i<6;i++)
          LCDMEM[i]=disp_buffer[i];
       time_dis();
       }
   else         // 是数据输入,则将要输入的数据位置闪烁显示
       {
       time_change();
       if((time_sec01&1)==0)
          {
          LCDMEM[4-fun_con*2]  =  0x0;
          LCDMEM[5-fun_con*2]  =  0x0;
          }
       else
          {
          LCDMEM[4-fun_con*2]  =  disp_buffer[4-fun_con*2];
          LCDMEM[5-fun_con*2]  =  disp_buffer[5-fun_con*2];
          }
       }
  }



uchar key_just(void)    //00:down  0xff:no down
    {
    P2DIR=0xf0;
    char in;
    in = ~P2IN ;
    in = in&0xf;
    in ++ ;
    if( in ==0)
          return(0xff);
    return(0);
    }
    
uchar key_code(void)    
    {
     uchar in=0;
//     P2DIR=0XF;
     in=P2IN&0x0f;
     in = ~in ;
     in = in & 0xf;
     if(in==0x4) return(1);
     if(in==0x1) return(2);
     if(in==0x2) return(3);
     if(in==0x8) return(4);
     }
    
uchar key(void)
    {
    uchar temp=0xff    ;
    P2IFG=0;
    if(key_just()==0)
        delay(1000);
    if(key_just()!=0)
        return(temp);
    temp=key_code();
    key_buffer=temp;
key_loop:    
    if(key_just()==0xff)
       goto  key_loop;
    else
       {
       P1IFG=0;
       return(temp);
       }
    }

void fun(void)
    {
    fun_con++;
    if(fun_con==3)
       fun_con=0;
    input_con=1;
    }
    
void up(void)
    {
      uchar i=0;
      if(fun_con==0)
          {
          time_buffer[0]++;
          if(time_buffer[0]==24)  time_buffer[0]=0;
          }
      if(fun_con==1)
          {
          time_buffer[1]++;
          if(time_buffer[1]==60)  time_buffer[1]=0;
          }
      if(fun_con==2)
          {
          time_buffer[2]++;
          if(time_buffer[2]==60)  time_buffer[2]=0;
          }
      disp_buffer[0]=distab[time_buffer[2]%10];
      disp_buffer[1]=distab[time_buffer[2]/10];
      disp_buffer[2]=distab[time_buffer[1]%10];
      disp_buffer[3]=distab[time_buffer[1]/10];
      disp_buffer[4]=distab[time_buffer[0]%10];
      disp_buffer[5]=distab[time_buffer[0]/10];
      for(i=0;i<6;i++)
         LCDMEM[i]=disp_buffer[i];
    }
    
void down(void)
    {
      uchar i=0;
      if(fun_con==0)
          {
          time_buffer[0]--;
          if(time_buffer[0]==0xff)  time_buffer[0]=23;
          }
      if(fun_con==1)
          {
          time_buffer[1]--;
          if(time_buffer[1]==0xff)  time_buffer[1]=59;
          }
      if(fun_con==2)
          {
          time_buffer[2]--;
          if(time_buffer[2]==0xff)  time_buffer[2]=59;
          }
      disp_buffer[0]=distab[time_buffer[2]%10];
      disp_buffer[1]=distab[time_buffer[2]/10];
      disp_buffer[2]=distab[time_buffer[1]%10];
      disp_buffer[3]=distab[time_buffer[1]/10];
      disp_buffer[4]=distab[time_buffer[0]%10];
      disp_buffer[5]=distab[time_buffer[0]/10];
      for(i=0;i<6;i++)
         LCDMEM[i]=disp_buffer[i];
    }
    

void enter(void)
    {
    char i;
    if(fun_con == 2 )   //认为没有调整时间
        {
            adc_f = ~adc_f ;
            for(i=0;i<20;i++)
                LCDMEM[i] = 0 ;
        }
    else
        {
        input_con=0;
        fun_con=2;
        }
    }

      
//interrupt[PORT1_VECTOR] void key_p1(void)
#pragma vector=PORT2_VECTOR
__interrupt void KKK(void)
      {
      uchar key_;
      
      key_=key();
      P2IFG=0;
      switch(key_)
            {
            case 1:
                {
                P6OUT &= ~BIT4 ;
                fun();
                break;
                }
            case 2:
                {
                P1OUT = 0XFF ;
                up();
                break;
                }
            case 3:
                {
                down();
                break;
                }
            case 4:
                {
                enter();
                break;
                }
            }      
      }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -