3599|1

360

帖子

0

TA的资源

纯净的硅(初级)

楼主
 

msp430f147+MAX7219+ENCODER 實作 [复制链接]

原先是用LaunchPad 來做這個項目,因為程序編譯後,超過G2231的Flash ROM的SIZE,於是翻箱倒櫃找出很久很久以前,我自己画的147PCB,上頭的MSP430F147是從淘寶購入的2手元件,說真的也是為了湊足3帖,才如此的賣力哈哈,這次用的ENCODER,在一般的洗衣機可以看到他的蹤跡,可順時針及逆時針旋轉,讓設定的數值增加及減少,ENCODER照片可到我的相簿裡看,實作示範影片如下


程序如下 ,因為時間有些緊奏,也許還有些BUG

#include  <msp430x14x.h>
//  MSP430G2xx1 Demo - Timer_A, Toggle P1.0, CCR0 Cont. Mode ISR, DCO SMCLK
//
//  Description: Toggle P1.0 using software and TA_0 ISR. Toggles every
//  50000 SMCLK cycles. SMCLK provides clock source for TACLK.
//  During the TA_0 ISR, P1.0 is toggled and 50000 clock cycles are added to
//  CCR0. TA_0 ISR is triggered every 50000 cycles. CPU is normally off and
//  used only during TA_ISR.
//  ACLK = n/a, MCLK = SMCLK = TACLK = default DCO

 
unsigned char k1,k2,k3,k4;
unsigned int cnt=0;
unsigned char up=0,down=0,K_BUF,K_BUF_1,count0=0,count1=0,key_lock=0;
unsigned char B_TIME=0x03,bibi_1;
#define set_Max7129_DIN  P1OUT |= 0x08
#define set_Max7129_LOAD P1OUT |= 0x10
#define set_Max7129_CLK  P1OUT |= 0x20

#define clr_Max7129_DIN  P1OUT &= 0xf7
#define clr_Max7129_LOAD P1OUT &= 0xef
#define clr_Max7129_CLK  P1OUT &= 0xdf

#define set_BIBI  P5OUT |= 0x10
#define clr_BIBI  P5OUT &= 0xef
// max7129 暫存器說明
#define DECODE_MODE     0x09    // 解碼方式 
#define INTENSITY       0x0A    // 顯示亮度 
#define SCAN_LIMIT      0x0B    // 掃描位數
#define SHUT_DOWN       0x0C    // 掉電模式
#define DISPLAY_TEST    0x0F    // 顯示測試 

// x是後 y是前const
const unsigned char  buf_x[10][8]={ {0x00,0x83,0x03,0x03,0x03,0x03,0x03,0x83},//0
                                    {0x00,0x83,0x80,0x80,0x80,0x80,0x82,0x80},//1
                                    {0x00,0x83,0x02,0x02,0x83,0x01,0x01,0x83},//2
                                    {0x00,0x83,0x01,0x01,0x83,0x01,0x01,0x83},//3
                                    {0x00,0x01,0x01,0x01,0x83,0x03,0x03,0x03},//4
                                    {0x00,0x83,0x01,0x01,0x83,0x02,0x02,0x83},//5
                                    {0x00,0x83,0x03,0x03,0x83,0x02,0x02,0x83},//6
                                    {0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x83},//7
                                    {0x00,0x83,0x03,0x03,0x83,0x03,0x03,0x83},//8
                                    {0x00,0x83,0x01,0x01,0x83,0x03,0x03,0x83} //9
                                 };
                               
const unsigned char   buf_y[10][8]={ {0x00,0x2c,0x0c,0x0c,0x0c,0x0c,0x0c,0x2c},//0- 
                                    {0x00,0x2c,0x20,0x20,0x20,0x20,0x24,0x20},//1-
                                    {0x00,0x2c,0x04,0x04,0x2c,0x08,0x08,0x2c},//2-
                                    {0x00,0x2c,0x08,0x08,0x2c,0x08,0x08,0x2c},//3-
                                    {0x00,0x08,0x08,0x08,0x2c,0x0c,0x0c,0x0c},//4-
                                    {0x00,0x2c,0x08,0x08,0x2c,0x04,0x04,0x2c},//5-
                                    {0x00,0x2c,0x0c,0x0c,0x2c,0x04,0x04,0x2c},//6-
                                    {0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x2c},//7-
                                    {0x00,0x2c,0x0c,0x0c,0x2c,0x0c,0x0c,0x2c},//8-
                                    {0x00,0x2c,0x08,0x08,0x2c,0x0c,0x0c,0x2c} //9-
                                 };
 
//
void Max7219_WriteByte(unsigned char byte);
void Max7219_WriteData_01(unsigned char addr, unsigned char dat0,unsigned char dat1);
void Max7219_Init(void);
     
void Max7219_WriteByte(unsigned char byte)
{

unsigned char i;

          for (i=0; i<8; i++)     
       { 

           clr_Max7129_CLK;
          // Max7129_DIN = (bit)(byte & MSB);//運算結果=0即輸出0,不是0即輸出1
            if ( (byte&0x80) == 0x80 ) set_Max7129_DIN ;
             else clr_Max7129_DIN ;
       byte <<= 1;  
           set_Max7129_CLK ;  
           }

}     
     
     // write a byte to register
     // 先address 後data
     
void Max7219_WriteData_01(unsigned char addr, unsigned char dat0,unsigned char dat1)

     
        set_Max7129_LOAD ;  
     
            clr_Max7129_LOAD ;
                                          
        Max7219_WriteByte(addr);
        Max7219_WriteByte(dat0);

        Max7219_WriteByte(addr);
        Max7219_WriteByte(dat1);

            set_Max7129_LOAD ;
                                                      
}

void OUT_0(void)
          
{   
            Max7219_WriteData_01(1, buf_x[k1][0]|buf_y[k2][0],buf_x[k3][0]|buf_y[k4][0]);
            Max7219_WriteData_01(2, buf_x[k1][1]|buf_y[k2][1],buf_x[k3][1]|buf_y[k4][1]);
            Max7219_WriteData_01(3, buf_x[k1][2]|buf_y[k2][2],buf_x[k3][2]|buf_y[k4][2]);
            Max7219_WriteData_01(4, buf_x[k1][3]|buf_y[k2][3],buf_x[k3][3]|buf_y[k4][3]);

            Max7219_WriteData_01(5, buf_x[k1][4]|buf_y[k2][4],buf_x[k3][4]|buf_y[k4][4]);
            Max7219_WriteData_01(6, buf_x[k1][5]|buf_y[k2][5],buf_x[k3][5]|buf_y[k4][5]);
            Max7219_WriteData_01(7, buf_x[k1][6]|buf_y[k2][6],buf_x[k3][6]|buf_y[k4][6]);
            Max7219_WriteData_01(8, buf_x[k1][7]|buf_y[k2][7],buf_x[k3][7]|buf_y[k4][7]);
}    

      
void Max7219_Init(void)      
{             
       Max7219_WriteData_01(SHUT_DOWN,    0x01,0X01);   // 掉電模式 : 1, 正常操作; 0, 睡眠模式

           Max7219_WriteData_01(DISPLAY_TEST, 0x00,0X00);   // 顯示測試 : 0, 正常模式; 1, 測試模式

           Max7219_WriteData_01(DECODE_MODE,  0x00,0X00);   // 解碼方式:0, 不解碼; 1, BCD解碼

           Max7219_WriteData_01(SCAN_LIMIT,   0x07,0X07);   // 掃描位數 : 0 ~ 7 最多支援8位數字

           Max7219_WriteData_01(INTENSITY,    0x01,0X01);   // 顯示亮度: 0 ~ F  F最亮
}

// Timer A0 interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
 K_BUF=P1IN; //read encoder
 K_BUF= K_BUF& 0x03;
 
 if(key_lock==0)
 {
  if (K_BUF_1==0)
      {
        if(K_BUF==1){up=1;down=0;key_lock=1;}
        if(K_BUF==2){up=0;down=1;key_lock=1;}  
      }
  if (K_BUF_1==1)
      {
        if(K_BUF==3){up=1;down=0;key_lock=1;}
        if(K_BUF==0){up=0;down=1;key_lock=1;}
      }
  if (K_BUF_1==2)
       {
        if(K_BUF==0){up=1;down=0;key_lock=1;}
        if(K_BUF==1){up=0;down=1;key_lock=1;}    
       }

  if (K_BUF_1==3)
       {
        if(K_BUF==2){up=1;down=0;key_lock=1;}
        if(K_BUF==1){up=0;down=1;key_lock=1;}    
       }
 }

 K_BUF_1=K_BUF;

   if (bibi_1==1)
    { B_TIME--;
      if (B_TIME >= 0X01)
        { set_BIBI ; //BUZZER ON
        }
      else
        { clr_BIBI ; //BUZZER OFF
          bibi_1 = 0;
          B_TIME = 0X03;
        }
     }
 
  CCR0 += 5000;                            // Add Offset to CCR0
}

void key_function(void)                             
{
 if (key_lock==0x01)
  { if(up==0x01)
    {
     count0++;
      if (count0>=5) //16
            {
             bibi_1=1;   
             cnt++;           
             count0=0;
             }   
    key_lock=0;
    up=0;
    count1=0;
    }
   
 if(down==0x01)
 
    {
     count1++;
      if (count1>=5) //16
            {
             bibi_1=1;   
             cnt--;           
             count1=0;
             }   
    key_lock=0x00;
    down=0;
    count0=0;
    } 
   
  }
}
  
int main(void)

       WDTCTL = WDTPW + WDTHOLD;            // Stop watchdog timer  
       CCTL0 = CCIE;                        // CCR0 interrupt enabled
       CCR0 = 5000;
       TACTL = TASSEL_2 + MC_2;             // SMCLK, contmode

       _BIS_SR(GIE);     //  interrupt   
       P5DIR |= 0x10;    //  buzzer
        clr_BIBI;     
       P1DIR |= 0x38;    //P1.3 P1.4 P1.5 all are set output mode
       P3DIR |= 0xf0;  
       P3OUT  = 0xa0;
       set_Max7129_LOAD ;
              
       set_Max7129_CLK ;        
       Max7219_Init(); 

 
            
          while(1)
              {
               key_function();
               k1 =  cnt % 10 ;
               k2 = (cnt/10)%10 ;  
               k3 = (cnt/100)%10 ;
               k4 = (cnt/1000)%10 ;         
            //   k5 = (cnt/10000)%10 ;
            //   k6 = (cnt/100000)%10 ;                                       
               OUT_0();
              }

}

最新回复

那学习了,thanks!  详情 回复 发表于 2012-5-3 15:02
 
点赞 关注

回复
举报

22

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
那学习了,thanks!
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表