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(); }
}
|