2016年3月29日 星期二

微處理器實作: AT89S52 四合一七段顯示器demo程式

        這個程式只要在於利用測試版上的四合一七段顯示器,撰寫類似動畫或廣告看板的程式,並藉此熟悉AT89S52輸出PORT和七段顯示器的操作。

程式執行情況:




完整程式碼: led_show.c

#include <REGX52.h>
#include "led_seq.h"

#define D0 P3_3
#define D1 P3_2
#define D2 P3_1
#define D3 P3_0

#define LED_NUM_0 0xC0
#define LED_NUM_1 0xF9
#define LED_NUM_2 0xA4
#define LED_NUM_3 0xB0
#define LED_NUM_4 0x99
#define LED_NUM_5 0x92
#define LED_NUM_6 0x82
#define LED_NUM_7 0xF8
#define LED_NUM_8 0x80
#define LED_NUM_9 0x98

#define HIGH 1
#define LOW 0

#define LED_OUT P1

unsigned char led_num[10] =
{
LED_NUM_0, LED_NUM_1, LED_NUM_2, LED_NUM_3, LED_NUM_4, 
LED_NUM_5, LED_NUM_6, LED_NUM_7, LED_NUM_8, LED_NUM_9
};

void my_delay(unsigned int n)
{
unsigned int i, j;

for(j=0; j<n; j++)
for(i=0; i<120; i++) ; 

}

void led_write_1c(unsigned char r, unsigned char x)
{
P3 = ~(1 << (3-r)); //enable control bit: active LOW
LED_OUT = x;
my_delay(3);
LED_OUT = 0xFF;


void led_write_4c_num(unsigned short x)
{
led_write_1c(0, led_num[x%10    ]);
led_write_1c(1, led_num[x/10%10 ]);
led_write_1c(2, led_num[x/100%10]);
led_write_1c(3, led_num[x/1000  ]);


main()
{
unsigned char i;
unsigned short counter=0;
unsigned short show_num=0;
unsigned char LED_7_SEG[4] = {0xFF, 0xFF, 0XFF, 0XFF};
unsigned int seq_pointer = 0;
while(1)
{
// play test_seq, and update LED_7_SEG[]
if(counter == 0)
{
for(i=0; i<4; i++)
LED_7_SEG[3-i] = test_seq[seq_pointer][i];
}

if(test_seq[seq_pointer][4] == counter+5)
{
counter = 0;
seq_pointer++;

if(seq_pointer == TEST_SEQ_LEN) seq_pointer = 0;
}
else 
counter++;

// display LED_7_SEG[]
for(i=0; i<4; i++)
led_write_1c(i, LED_7_SEG[i]);
}
}

led_seq.h(動畫內容)的內容:

#define TEST_SEQ_LEN 591

static unsigned char code test_seq[TEST_SEQ_LEN][5] = { 

/* 1 bar circle around: 36 */
{0xfe, 0xff, 0xff, 0xff, 10},
{0xff, 0xfe, 0xff, 0xff, 10},
{0xff, 0xff, 0xfe, 0xff, 10},
{0xff, 0xff, 0xff, 0xfe, 10},
{0xff, 0xff, 0xff, 0xfd, 10},
{0xff, 0xff, 0xff, 0xfb, 10},
{0xff, 0xff, 0xff, 0xf7, 10},
{0xff, 0xff, 0xf7, 0xff, 10},
{0xff, 0xf7, 0xff, 0xff, 10},
{0xf7, 0xff, 0xff, 0xff, 10},
{0xef, 0xff, 0xff, 0xff, 10},
{0xdf, 0xff, 0xff, 0xff, 10},
{0xfe, 0xff, 0xff, 0xff, 10},
{0xff, 0xfe, 0xff, 0xff, 10},
{0xff, 0xff, 0xfe, 0xff, 10},
{0xff, 0xff, 0xff, 0xfe, 10},
{0xff, 0xff, 0xff, 0xfd, 10},
{0xff, 0xff, 0xff, 0xfb, 10},
{0xff, 0xff, 0xff, 0xf7, 10},
{0xff, 0xff, 0xf7, 0xff, 10},
{0xff, 0xf7, 0xff, 0xff, 10},
{0xf7, 0xff, 0xff, 0xff, 10},
{0xef, 0xff, 0xff, 0xff, 10},
{0xdf, 0xff, 0xff, 0xff, 10},
{0xfe, 0xff, 0xff, 0xff, 10},
{0xff, 0xfe, 0xff, 0xff, 10},
{0xff, 0xff, 0xfe, 0xff, 10},
{0xff, 0xff, 0xff, 0xfe, 10},
{0xff, 0xff, 0xff, 0xfd, 10},
{0xff, 0xff, 0xff, 0xfb, 10},
{0xff, 0xff, 0xff, 0xf7, 10},
{0xff, 0xff, 0xf7, 0xff, 10},
{0xff, 0xf7, 0xff, 0xff, 10},
{0xf7, 0xff, 0xff, 0xff, 10},
{0xef, 0xff, 0xff, 0xff, 10},
{0xdf, 0xff, 0xff, 0xff, 10},


/* circle all 4 seg.: 36 */ 
{0xfe, 0xff, 0xff, 0xff, 10},
{0xfe, 0xfe, 0xff, 0xff, 10},
{0xfe, 0xfe, 0xfe, 0xff, 10},
{0xfe, 0xfe, 0xfe, 0xfe, 10},
{0xfe, 0xfe, 0xfe, 0xfc, 10},
{0xfe, 0xfe, 0xfe, 0xf8, 10},
{0xfe, 0xfe, 0xfe, 0xf0, 10},
{0xfe, 0xfe, 0xf6, 0xf0, 10},
{0xfe, 0xf6, 0xf6, 0xf0, 10},
{0xf6, 0xf6, 0xf6, 0xf0, 10},
{0xe6, 0xf6, 0xf6, 0xf0, 10},
{0xc6, 0xf6, 0xf6, 0xf0, 10},
{0xfe, 0xff, 0xff, 0xff, 10},
{0xfe, 0xfe, 0xff, 0xff, 10},
{0xfe, 0xfe, 0xfe, 0xff, 10},
{0xfe, 0xfe, 0xfe, 0xfe, 10},
{0xfe, 0xfe, 0xfe, 0xfc, 10},
{0xfe, 0xfe, 0xfe, 0xf8, 10},
{0xfe, 0xfe, 0xfe, 0xf0, 10},
{0xfe, 0xfe, 0xf6, 0xf0, 10},
{0xfe, 0xf6, 0xf6, 0xf0, 10},
{0xf6, 0xf6, 0xf6, 0xf0, 10},
{0xe6, 0xf6, 0xf6, 0xf0, 10},
{0xc6, 0xf6, 0xf6, 0xf0, 10},
{0xfe, 0xff, 0xff, 0xff, 10},
{0xfe, 0xfe, 0xff, 0xff, 10},
{0xfe, 0xfe, 0xfe, 0xff, 10},
{0xfe, 0xfe, 0xfe, 0xfe, 10},
{0xfe, 0xfe, 0xfe, 0xfc, 10},
{0xfe, 0xfe, 0xfe, 0xf8, 10},
{0xfe, 0xfe, 0xfe, 0xf0, 10},
{0xfe, 0xfe, 0xf6, 0xf0, 10},
{0xfe, 0xf6, 0xf6, 0xf0, 10},
{0xf6, 0xf6, 0xf6, 0xf0, 10},
{0xe6, 0xf6, 0xf6, 0xf0, 10},
{0xc6, 0xf6, 0xf6, 0xf0, 10},

/* h snake: 48 */ 
{0xdf, 0xff, 0xff, 0xff, 10},
{0xde, 0xff, 0xff, 0xff, 10},
{0xde, 0xfe, 0xff, 0xff, 10},
{0xde, 0xfe, 0xfe, 0xff, 10},
{0xde, 0xfe, 0xfe, 0xfe, 10},
{0xde, 0xfe, 0xfe, 0xfc, 10},
{0xde, 0xfe, 0xfe, 0xbc, 10},
{0xde, 0xfe, 0xbe, 0xbc, 10},
{0xde, 0xbe, 0xbe, 0xbc, 10},
{0x9e, 0xbe, 0xbe, 0xbc, 10},
{0x8e, 0xbe, 0xbe, 0xbc, 10},
{0x86, 0xbe, 0xbe, 0xbc, 10},
{0x86, 0xb6, 0xbe, 0xbc, 10},
{0x86, 0xb6, 0xb6, 0xbc, 10},
{0x86, 0xb6, 0xb6, 0xb4, 10},
{0x86, 0xb6, 0xb6, 0xb0, 10},
{0xdf, 0xff, 0xff, 0xff, 10},
{0xde, 0xff, 0xff, 0xff, 10},
{0xde, 0xfe, 0xff, 0xff, 10},
{0xde, 0xfe, 0xfe, 0xff, 10},
{0xde, 0xfe, 0xfe, 0xfe, 10},
{0xde, 0xfe, 0xfe, 0xfc, 10},
{0xde, 0xfe, 0xfe, 0xbc, 10},
{0xde, 0xfe, 0xbe, 0xbc, 10},
{0xde, 0xbe, 0xbe, 0xbc, 10},
{0x9e, 0xbe, 0xbe, 0xbc, 10},
{0x8e, 0xbe, 0xbe, 0xbc, 10},
{0x86, 0xbe, 0xbe, 0xbc, 10},
{0x86, 0xb6, 0xbe, 0xbc, 10},
{0x86, 0xb6, 0xb6, 0xbc, 10},
{0x86, 0xb6, 0xb6, 0xb4, 10},
{0x86, 0xb6, 0xb6, 0xb0, 10},
{0xdf, 0xff, 0xff, 0xff, 10},
{0xde, 0xff, 0xff, 0xff, 10},
{0xde, 0xfe, 0xff, 0xff, 10},
{0xde, 0xfe, 0xfe, 0xff, 10},
{0xde, 0xfe, 0xfe, 0xfe, 10},
{0xde, 0xfe, 0xfe, 0xfc, 10},
{0xde, 0xfe, 0xfe, 0xbc, 10},
{0xde, 0xfe, 0xbe, 0xbc, 10},
{0xde, 0xbe, 0xbe, 0xbc, 10},
{0x9e, 0xbe, 0xbe, 0xbc, 10},
{0x8e, 0xbe, 0xbe, 0xbc, 10},
{0x86, 0xbe, 0xbe, 0xbc, 10},
{0x86, 0xb6, 0xbe, 0xbc, 10},
{0x86, 0xb6, 0xb6, 0xbc, 10},
{0x86, 0xb6, 0xb6, 0xb4, 10},
{0x86, 0xb6, 0xb6, 0xb0, 10},

/* 1 bar snake : 72 */
{0xef, 0xff, 0xff, 0xff, 10},
{0xdf, 0xff, 0xff, 0xff, 10},
{0xfe, 0xff, 0xff, 0xff, 10},
{0xfd, 0xff, 0xff, 0xff, 10},
{0xfb, 0xff, 0xff, 0xff, 10},
{0x7f, 0xff, 0xff, 0xff, 10},
{0xff, 0xef, 0xff, 0xff, 10},
{0xff, 0xdf, 0xff, 0xff, 10},
{0xff, 0xfe, 0xff, 0xff, 10},
{0xff, 0xfd, 0xff, 0xff, 10},
{0xff, 0xfb, 0xff, 0xff, 10},
{0xff, 0x7f, 0xff, 0xff, 10},
{0xff, 0xff, 0xef, 0xff, 10},
{0xff, 0xff, 0xdf, 0xff, 10},
{0xff, 0xff, 0xfe, 0xff, 10},
{0xff, 0xff, 0xfd, 0xff, 10},
{0xff, 0xff, 0xfb, 0xff, 10},
{0xff, 0xff, 0x7f, 0xff, 10},
{0xff, 0xff, 0xff, 0xef, 10},
{0xff, 0xff, 0xff, 0xdf, 10},
{0xff, 0xff, 0xff, 0xfe, 10},
{0xff, 0xff, 0xff, 0xfd, 10},
{0xff, 0xff, 0xff, 0xfb, 10},
{0xff, 0xff, 0xff, 0x7f, 10},
{0xef, 0xff, 0xff, 0xff, 10},
{0xdf, 0xff, 0xff, 0xff, 10},
{0xfe, 0xff, 0xff, 0xff, 10},
{0xfd, 0xff, 0xff, 0xff, 10},
{0xfb, 0xff, 0xff, 0xff, 10},
{0x7f, 0xff, 0xff, 0xff, 10},
{0xff, 0xef, 0xff, 0xff, 10},
{0xff, 0xdf, 0xff, 0xff, 10},
{0xff, 0xfe, 0xff, 0xff, 10},
{0xff, 0xfd, 0xff, 0xff, 10},
{0xff, 0xfb, 0xff, 0xff, 10},
{0xff, 0x7f, 0xff, 0xff, 10},
{0xff, 0xff, 0xef, 0xff, 10},
{0xff, 0xff, 0xdf, 0xff, 10},
{0xff, 0xff, 0xfe, 0xff, 10},
{0xff, 0xff, 0xfd, 0xff, 10},
{0xff, 0xff, 0xfb, 0xff, 10},
{0xff, 0xff, 0x7f, 0xff, 10},
{0xff, 0xff, 0xff, 0xef, 10},
{0xff, 0xff, 0xff, 0xdf, 10},
{0xff, 0xff, 0xff, 0xfe, 10},
{0xff, 0xff, 0xff, 0xfd, 10},
{0xff, 0xff, 0xff, 0xfb, 10},
{0xff, 0xff, 0xff, 0x7f, 10},

{0xef, 0xff, 0xff, 0xff, 10},
{0xdf, 0xff, 0xff, 0xff, 10},
{0xfe, 0xff, 0xff, 0xff, 10},
{0xfd, 0xff, 0xff, 0xff, 10},
{0xfb, 0xff, 0xff, 0xff, 10},
{0x7f, 0xff, 0xff, 0xff, 10},
{0xff, 0xef, 0xff, 0xff, 10},
{0xff, 0xdf, 0xff, 0xff, 10},
{0xff, 0xfe, 0xff, 0xff, 10},
{0xff, 0xfd, 0xff, 0xff, 10},
{0xff, 0xfb, 0xff, 0xff, 10},
{0xff, 0x7f, 0xff, 0xff, 10},
{0xff, 0xff, 0xef, 0xff, 10},
{0xff, 0xff, 0xdf, 0xff, 10},
{0xff, 0xff, 0xfe, 0xff, 10},
{0xff, 0xff, 0xfd, 0xff, 10},
{0xff, 0xff, 0xfb, 0xff, 10},
{0xff, 0xff, 0x7f, 0xff, 10},
{0xff, 0xff, 0xff, 0xef, 10},
{0xff, 0xff, 0xff, 0xdf, 10},
{0xff, 0xff, 0xff, 0xfe, 10},
{0xff, 0xff, 0xff, 0xfd, 10},
{0xff, 0xff, 0xff, 0xfb, 10},
{0xff, 0xff, 0xff, 0x7f, 10},


/*v snake: 60*/ 
{0xff, 0xff, 0xff, 0xfd, 10},
{0xff, 0xff, 0xff, 0xf9, 10},
{0xff, 0xff, 0xff, 0xf1, 10},
{0xff, 0xff, 0xff, 0xe1, 10},
{0xff, 0xff, 0xff, 0xc1, 10},
{0xff, 0xff, 0xfd, 0xc1, 10},
{0xff, 0xff, 0xf9, 0xc1, 10},
{0xff, 0xff, 0xf1, 0xc1, 10},
{0xff, 0xff, 0xe1, 0xc1, 10},
{0xff, 0xff, 0xc1, 0xc1, 10},
{0xff, 0xfd, 0xc1, 0xc1, 10},
{0xff, 0xf9, 0xc1, 0xc1, 10},
{0xff, 0xf1, 0xc1, 0xc1, 10},
{0xff, 0xe1, 0xc1, 0xc1, 10},
{0xff, 0xc1, 0xc1, 0xc1, 10},
{0xfd, 0xc1, 0xc1, 0xc1, 10},
{0xf9, 0xc1, 0xc1, 0xc1, 10},
{0xf1, 0xc1, 0xc1, 0xc1, 10},
{0xe1, 0xc1, 0xc1, 0xc1, 10},
{0xc1, 0xc1, 0xc1, 0xc1, 10},
{0xff, 0xff, 0xff, 0xfd, 10},
{0xff, 0xff, 0xff, 0xf9, 10},
{0xff, 0xff, 0xff, 0xf1, 10},
{0xff, 0xff, 0xff, 0xe1, 10},
{0xff, 0xff, 0xff, 0xc1, 10},
{0xff, 0xff, 0xfd, 0xc1, 10},
{0xff, 0xff, 0xf9, 0xc1, 10},
{0xff, 0xff, 0xf1, 0xc1, 10},
{0xff, 0xff, 0xe1, 0xc1, 10},
{0xff, 0xff, 0xc1, 0xc1, 10},
{0xff, 0xfd, 0xc1, 0xc1, 10},
{0xff, 0xf9, 0xc1, 0xc1, 10},
{0xff, 0xf1, 0xc1, 0xc1, 10},
{0xff, 0xe1, 0xc1, 0xc1, 10},
{0xff, 0xc1, 0xc1, 0xc1, 10},
{0xfd, 0xc1, 0xc1, 0xc1, 10},
{0xf9, 0xc1, 0xc1, 0xc1, 10},
{0xf1, 0xc1, 0xc1, 0xc1, 10},
{0xe1, 0xc1, 0xc1, 0xc1, 10},
{0xc1, 0xc1, 0xc1, 0xc1, 10},
{0xff, 0xff, 0xff, 0xfd, 10},
{0xff, 0xff, 0xff, 0xf9, 10},
{0xff, 0xff, 0xff, 0xf1, 10},
{0xff, 0xff, 0xff, 0xe1, 10},
{0xff, 0xff, 0xff, 0xc1, 10},
{0xff, 0xff, 0xfd, 0xc1, 10},
{0xff, 0xff, 0xf9, 0xc1, 10},
{0xff, 0xff, 0xf1, 0xc1, 10},
{0xff, 0xff, 0xe1, 0xc1, 10},
{0xff, 0xff, 0xc1, 0xc1, 10},
{0xff, 0xfd, 0xc1, 0xc1, 10},
{0xff, 0xf9, 0xc1, 0xc1, 10},
{0xff, 0xf1, 0xc1, 0xc1, 10},
{0xff, 0xe1, 0xc1, 0xc1, 10},
{0xff, 0xc1, 0xc1, 0xc1, 10},
{0xfd, 0xc1, 0xc1, 0xc1, 10},
{0xf9, 0xc1, 0xc1, 0xc1, 10},
{0xf1, 0xc1, 0xc1, 0xc1, 10},
{0xe1, 0xc1, 0xc1, 0xc1, 10},
{0xc1, 0xc1, 0xc1, 0xc1, 10},

/* 4 bar snake : 75*/
{0xff, 0xff, 0xff, 0xff, 10},
{0xef, 0xff, 0xff, 0xff, 10},
{0xcf, 0xff, 0xff, 0xff, 10},
{0xce, 0xff, 0xff, 0xff, 10},
{0xcc, 0xff, 0xff, 0xff, 10},
{0xd8, 0xff, 0xff, 0xff, 10},
{0xf8, 0xef, 0xff, 0xff, 10},
{0xf9, 0xcf, 0xff, 0xff, 10},
{0xfb, 0xce, 0xff, 0xff, 10},
{0xff, 0xcc, 0xff, 0xff, 10},
{0xff, 0xd8, 0xff, 0xff, 10},
{0xff, 0xf8, 0xef, 0xff, 10},
{0xff, 0xf9, 0xcf, 0xff, 10},
{0xff, 0xfb, 0xce, 0xff, 10},
{0xff, 0xff, 0xcc, 0xff, 10},
{0xff, 0xff, 0xd8, 0xff, 10},
{0xff, 0xff, 0xf8, 0xef, 10},
{0xff, 0xff, 0xf9, 0xcf, 10},
{0xff, 0xff, 0xfb, 0xce, 10},
{0xff, 0xff, 0xff, 0xcc, 10},
{0xff, 0xff, 0xff, 0xd8, 10},
{0xff, 0xff, 0xff, 0xf8, 10},
{0xff, 0xff, 0xff, 0xf9, 10},
{0xff, 0xff, 0xff, 0xfb, 10},
{0xff, 0xff, 0xff, 0xff, 10},
{0xff, 0xff, 0xff, 0xff, 10},
{0xef, 0xff, 0xff, 0xff, 10},
{0xcf, 0xff, 0xff, 0xff, 10},
{0xce, 0xff, 0xff, 0xff, 10},
{0xcc, 0xff, 0xff, 0xff, 10},
{0xd8, 0xff, 0xff, 0xff, 10},
{0xf8, 0xef, 0xff, 0xff, 10},
{0xf9, 0xcf, 0xff, 0xff, 10},
{0xfb, 0xce, 0xff, 0xff, 10},
{0xff, 0xcc, 0xff, 0xff, 10},
{0xff, 0xd8, 0xff, 0xff, 10},
{0xff, 0xf8, 0xef, 0xff, 10},
{0xff, 0xf9, 0xcf, 0xff, 10},
{0xff, 0xfb, 0xce, 0xff, 10},
{0xff, 0xff, 0xcc, 0xff, 10},
{0xff, 0xff, 0xd8, 0xff, 10},
{0xff, 0xff, 0xf8, 0xef, 10},
{0xff, 0xff, 0xf9, 0xcf, 10},
{0xff, 0xff, 0xfb, 0xce, 10},
{0xff, 0xff, 0xff, 0xcc, 10},
{0xff, 0xff, 0xff, 0xd8, 10},
{0xff, 0xff, 0xff, 0xf8, 10},
{0xff, 0xff, 0xff, 0xf9, 10},
{0xff, 0xff, 0xff, 0xfb, 10},
{0xff, 0xff, 0xff, 0xff, 10},
{0xff, 0xff, 0xff, 0xff, 10},
{0xef, 0xff, 0xff, 0xff, 10},
{0xcf, 0xff, 0xff, 0xff, 10},
{0xce, 0xff, 0xff, 0xff, 10},
{0xcc, 0xff, 0xff, 0xff, 10},
{0xd8, 0xff, 0xff, 0xff, 10},
{0xf8, 0xef, 0xff, 0xff, 10},
{0xf9, 0xcf, 0xff, 0xff, 10},
{0xfb, 0xce, 0xff, 0xff, 10},
{0xff, 0xcc, 0xff, 0xff, 10},
{0xff, 0xd8, 0xff, 0xff, 10},
{0xff, 0xf8, 0xef, 0xff, 10},
{0xff, 0xf9, 0xcf, 0xff, 10},
{0xff, 0xfb, 0xce, 0xff, 10},
{0xff, 0xff, 0xcc, 0xff, 10},
{0xff, 0xff, 0xd8, 0xff, 10},
{0xff, 0xff, 0xf8, 0xef, 10},
{0xff, 0xff, 0xf9, 0xcf, 10},
{0xff, 0xff, 0xfb, 0xce, 10},
{0xff, 0xff, 0xff, 0xcc, 10},
{0xff, 0xff, 0xff, 0xd8, 10},
{0xff, 0xff, 0xff, 0xf8, 10},
{0xff, 0xff, 0xff, 0xf9, 10},
{0xff, 0xff, 0xff, 0xfb, 10},
{0xff, 0xff, 0xff, 0xff, 10},

/* h spand :54 */
{0xff, 0xff, 0xff, 0xff, 10},
{0xff, 0xff, 0xff, 0xf9, 10},
{0xff, 0xff, 0xff, 0xc9, 10},
{0xff, 0xff, 0xf9, 0xc9, 10},
{0xff, 0xff, 0xc9, 0xc9, 10},
{0xff, 0xf9, 0xc9, 0xc9, 10},
{0xff, 0xc9, 0xc9, 0xc9, 10},
{0xf9, 0xc9, 0xc9, 0xc9, 10},
{0xc9, 0xc9, 0xc9, 0xc9, 10},
{0xc9, 0xc9, 0xc9, 0xc9, 10},
{0xf9, 0xc9, 0xc9, 0xc9, 10},
{0xff, 0xc9, 0xc9, 0xc9, 10},
{0xff, 0xf9, 0xc9, 0xc9, 10},
{0xff, 0xff, 0xc9, 0xc9, 10},
{0xff, 0xff, 0xf9, 0xc9, 10},
{0xff, 0xff, 0xff, 0xc9, 10},
{0xff, 0xff, 0xff, 0xf9, 10},
{0xff, 0xff, 0xff, 0xff, 10},
{0xff, 0xff, 0xff, 0xff, 10},
{0xff, 0xff, 0xff, 0xf9, 10},
{0xff, 0xff, 0xff, 0xc9, 10},
{0xff, 0xff, 0xf9, 0xc9, 10},
{0xff, 0xff, 0xc9, 0xc9, 10},
{0xff, 0xf9, 0xc9, 0xc9, 10},
{0xff, 0xc9, 0xc9, 0xc9, 10},
{0xf9, 0xc9, 0xc9, 0xc9, 10},
{0xc9, 0xc9, 0xc9, 0xc9, 10},
{0xc9, 0xc9, 0xc9, 0xc9, 10},
{0xf9, 0xc9, 0xc9, 0xc9, 10},
{0xff, 0xc9, 0xc9, 0xc9, 10},
{0xff, 0xf9, 0xc9, 0xc9, 10},
{0xff, 0xff, 0xc9, 0xc9, 10},
{0xff, 0xff, 0xf9, 0xc9, 10},
{0xff, 0xff, 0xff, 0xc9, 10},
{0xff, 0xff, 0xff, 0xf9, 10},
{0xff, 0xff, 0xff, 0xff, 10},
{0xff, 0xff, 0xff, 0xff, 10},
{0xff, 0xff, 0xff, 0xf9, 10},
{0xff, 0xff, 0xff, 0xc9, 10},
{0xff, 0xff, 0xf9, 0xc9, 10},
{0xff, 0xff, 0xc9, 0xc9, 10},
{0xff, 0xf9, 0xc9, 0xc9, 10},
{0xff, 0xc9, 0xc9, 0xc9, 10},
{0xf9, 0xc9, 0xc9, 0xc9, 10},
{0xc9, 0xc9, 0xc9, 0xc9, 10},
{0xc9, 0xc9, 0xc9, 0xc9, 10},
{0xf9, 0xc9, 0xc9, 0xc9, 10},
{0xff, 0xc9, 0xc9, 0xc9, 10},
{0xff, 0xf9, 0xc9, 0xc9, 10},
{0xff, 0xff, 0xc9, 0xc9, 10},
{0xff, 0xff, 0xf9, 0xc9, 10},
{0xff, 0xff, 0xff, 0xc9, 10},
{0xff, 0xff, 0xff, 0xf9, 10},
{0xff, 0xff, 0xff, 0xff, 10},

/* 2 way h spand: 54*/ 
{0xff, 0xff, 0xff, 0xff, 10},
{0xdf, 0xff, 0xff, 0xfb, 10},
{0xdd, 0xff, 0xff, 0xeb, 10},
{0xdd, 0xdf, 0xfb, 0xeb, 10},
{0xdd, 0xdd, 0xeb, 0xeb, 10},
{0xdd, 0xd9, 0xcb, 0xeb, 10},
{0xdd, 0xc9, 0xc9, 0xeb, 10},
{0xd9, 0xc9, 0xc9, 0xcb, 10},
{0xc9, 0xc9, 0xc9, 0xc9, 10},
{0xc9, 0xc9, 0xc9, 0xc9, 10},
{0xd9, 0xc9, 0xc9, 0xcb, 10},
{0xdd, 0xc9, 0xc9, 0xeb, 10},
{0xdd, 0xd9, 0xcb, 0xeb, 10},
{0xdd, 0xdd, 0xeb, 0xeb, 10},
{0xdd, 0xdf, 0xfb, 0xeb, 10},
{0xdd, 0xff, 0xff, 0xeb, 10},
{0xdf, 0xff, 0xff, 0xfb, 10},
{0xff, 0xff, 0xff, 0xff, 10},
{0xff, 0xff, 0xff, 0xff, 10},
{0xdf, 0xff, 0xff, 0xfb, 10},
{0xdd, 0xff, 0xff, 0xeb, 10},
{0xdd, 0xdf, 0xfb, 0xeb, 10},
{0xdd, 0xdd, 0xeb, 0xeb, 10},
{0xdd, 0xd9, 0xcb, 0xeb, 10},
{0xdd, 0xc9, 0xc9, 0xeb, 10},
{0xd9, 0xc9, 0xc9, 0xcb, 10},
{0xc9, 0xc9, 0xc9, 0xc9, 10},
{0xc9, 0xc9, 0xc9, 0xc9, 10},
{0xd9, 0xc9, 0xc9, 0xcb, 10},
{0xdd, 0xc9, 0xc9, 0xeb, 10},
{0xdd, 0xd9, 0xcb, 0xeb, 10},
{0xdd, 0xdd, 0xeb, 0xeb, 10},
{0xdd, 0xdf, 0xfb, 0xeb, 10},
{0xdd, 0xff, 0xff, 0xeb, 10},
{0xdf, 0xff, 0xff, 0xfb, 10},
{0xff, 0xff, 0xff, 0xff, 10},
{0xff, 0xff, 0xff, 0xff, 10},
{0xdf, 0xff, 0xff, 0xfb, 10},
{0xdd, 0xff, 0xff, 0xeb, 10},
{0xdd, 0xdf, 0xfb, 0xeb, 10},
{0xdd, 0xdd, 0xeb, 0xeb, 10},
{0xdd, 0xd9, 0xcb, 0xeb, 10},
{0xdd, 0xc9, 0xc9, 0xeb, 10},
{0xd9, 0xc9, 0xc9, 0xcb, 10},
{0xc9, 0xc9, 0xc9, 0xc9, 10},
{0xc9, 0xc9, 0xc9, 0xc9, 10},
{0xd9, 0xc9, 0xc9, 0xcb, 10},
{0xdd, 0xc9, 0xc9, 0xeb, 10},
{0xdd, 0xd9, 0xcb, 0xeb, 10},
{0xdd, 0xdd, 0xeb, 0xeb, 10},
{0xdd, 0xdf, 0xfb, 0xeb, 10},
{0xdd, 0xff, 0xff, 0xeb, 10},
{0xdf, 0xff, 0xff, 0xfb, 10},
{0xff, 0xff, 0xff, 0xff, 10},
/* small squares h flip : 18*/
{0x9c, 0x9c, 0x9c, 0x9c, 30},
{0xa3, 0xa3, 0xa3, 0xa3, 30},
{0x9c, 0x9c, 0xa3, 0xa3, 30},
{0xa3, 0xa3, 0x9c, 0x9c, 30},
{0xa3, 0xa3, 0xa3, 0xa3, 30},
{0x9c, 0x9c, 0x9c, 0x9c, 30},
{0x9c, 0x9c, 0x9c, 0x9c, 30},
{0xa3, 0xa3, 0xa3, 0xa3, 30},
{0x9c, 0x9c, 0xa3, 0xa3, 30},
{0xa3, 0xa3, 0x9c, 0x9c, 30},
{0xa3, 0xa3, 0xa3, 0xa3, 30},
{0x9c, 0x9c, 0x9c, 0x9c, 30},
{0x9c, 0x9c, 0x9c, 0x9c, 30},
{0xa3, 0xa3, 0xa3, 0xa3, 30},
{0x9c, 0x9c, 0xa3, 0xa3, 30},
{0xa3, 0xa3, 0x9c, 0x9c, 30},
{0xa3, 0xa3, 0xa3, 0xa3, 30},
{0x9c, 0x9c, 0x9c, 0x9c, 30},

/* small square run around: 24 */
{0x9c, 0xff, 0xff, 0xff, 15},
{0xff, 0x9c, 0xff, 0xff, 15},
{0xff, 0xff, 0x9c, 0xff, 15},
{0xff, 0xff, 0xff, 0x9c, 15},
{0xff, 0xff, 0xff, 0xa3, 15},
{0xff, 0xff, 0xa3, 0xff, 15},
{0xff, 0xa3, 0xff, 0xff, 15},
{0xa3, 0xff, 0xff, 0xff, 15},
{0x9c, 0xff, 0xff, 0xff, 15},
{0xff, 0x9c, 0xff, 0xff, 15},
{0xff, 0xff, 0x9c, 0xff, 15},
{0xff, 0xff, 0xff, 0x9c, 15},
{0xff, 0xff, 0xff, 0xa3, 15},
{0xff, 0xff, 0xa3, 0xff, 15},
{0xff, 0xa3, 0xff, 0xff, 15},
{0xa3, 0xff, 0xff, 0xff, 15},
{0x9c, 0xff, 0xff, 0xff, 15},
{0xff, 0x9c, 0xff, 0xff, 15},
{0xff, 0xff, 0x9c, 0xff, 15},
{0xff, 0xff, 0xff, 0x9c, 15},
{0xff, 0xff, 0xff, 0xa3, 15},
{0xff, 0xff, 0xa3, 0xff, 15},
{0xff, 0xa3, 0xff, 0xff, 15},
{0xa3, 0xff, 0xff, 0xff, 15},

/* small square snake run: 42*/
{0x9c, 0xff, 0xff, 0xff, 15},
{0xa3, 0xff, 0xff, 0xff, 15},
{0xff, 0xa3, 0xff, 0xff, 15},
{0xff, 0x9c, 0xff, 0xff, 15},
{0xff, 0xff, 0x9c, 0xff, 15},
{0xff, 0xff, 0xa3, 0xff, 15},
{0xff, 0xff, 0xff, 0xa3, 15},
{0xff, 0xff, 0xff, 0x9c, 15},
{0xff, 0xff, 0x9c, 0xff, 15},
{0xff, 0xff, 0xa3, 0xff, 15},
{0xff, 0xa3, 0xff, 0xff, 15},
{0xff, 0x9c, 0xff, 0xff, 15},
{0x9c, 0xff, 0xff, 0xff, 15},
{0xa3, 0xff, 0xff, 0xff, 15},
{0x9c, 0xff, 0xff, 0xff, 15},
{0xa3, 0xff, 0xff, 0xff, 15},
{0xff, 0xa3, 0xff, 0xff, 15},
{0xff, 0x9c, 0xff, 0xff, 15},
{0xff, 0xff, 0x9c, 0xff, 15},
{0xff, 0xff, 0xa3, 0xff, 15},
{0xff, 0xff, 0xff, 0xa3, 15},
{0xff, 0xff, 0xff, 0x9c, 15},
{0xff, 0xff, 0x9c, 0xff, 15},
{0xff, 0xff, 0xa3, 0xff, 15},
{0xff, 0xa3, 0xff, 0xff, 15},
{0xff, 0x9c, 0xff, 0xff, 15},
{0x9c, 0xff, 0xff, 0xff, 15},
{0xa3, 0xff, 0xff, 0xff, 15},
{0x9c, 0xff, 0xff, 0xff, 15},
{0xa3, 0xff, 0xff, 0xff, 15},
{0xff, 0xa3, 0xff, 0xff, 15},
{0xff, 0x9c, 0xff, 0xff, 15},
{0xff, 0xff, 0x9c, 0xff, 15},
{0xff, 0xff, 0xa3, 0xff, 15},
{0xff, 0xff, 0xff, 0xa3, 15},
{0xff, 0xff, 0xff, 0x9c, 15},
{0xff, 0xff, 0x9c, 0xff, 15},
{0xff, 0xff, 0xa3, 0xff, 15},
{0xff, 0xa3, 0xff, 0xff, 15},
{0xff, 0x9c, 0xff, 0xff, 15},
{0x9c, 0xff, 0xff, 0xff, 15},
{0xa3, 0xff, 0xff, 0xff, 15},

/* fuel mode : 72*/
{0xf7, 0xff, 0xff, 0xff, 10},
{0xb7, 0xff, 0xff, 0xff, 10},
{0xb6, 0xff, 0xff, 0xff, 10},
{0xb6, 0xf7, 0xff, 0xff, 10},
{0xb6, 0xb7, 0xff, 0xff, 10},
{0xb6, 0xb6, 0xff, 0xff, 10},
{0xb6, 0xb6, 0xf7, 0xff, 10},
{0xb6, 0xb6, 0xb7, 0xff, 10},
{0xb6, 0xb6, 0xb6, 0xff, 10},
{0xb6, 0xb6, 0xb6, 0xf7, 10},
{0xb6, 0xb6, 0xb6, 0xb7, 10},
{0xb6, 0xb6, 0xb6, 0xb6, 10},
{0xb6, 0xb6, 0xb6, 0xb0, 10},
{0xb6, 0xb6, 0xb6, 0x80, 10},
{0xb6, 0xb6, 0xb0, 0x80, 10},
{0xb6, 0xb6, 0x80, 0x80, 10},
{0xb6, 0xb0, 0x80, 0x80, 10},
{0xb6, 0x80, 0x80, 0x80, 10},
{0xb0, 0x80, 0x80, 0x80, 10},
{0x80, 0x80, 0x80, 0x80, 10},
{0x80, 0x80, 0x80, 0x00, 10},
{0x80, 0x80, 0x00, 0x00, 10},
{0x80, 0x00, 0x00, 0x00, 10},
{0x00, 0x00, 0x00, 0x00, 10},
{0xf7, 0xff, 0xff, 0xff, 10},
{0xb7, 0xff, 0xff, 0xff, 10},
{0xb6, 0xff, 0xff, 0xff, 10},
{0xb6, 0xf7, 0xff, 0xff, 10},
{0xb6, 0xb7, 0xff, 0xff, 10},
{0xb6, 0xb6, 0xff, 0xff, 10},
{0xb6, 0xb6, 0xf7, 0xff, 10},
{0xb6, 0xb6, 0xb7, 0xff, 10},
{0xb6, 0xb6, 0xb6, 0xff, 10},
{0xb6, 0xb6, 0xb6, 0xf7, 10},
{0xb6, 0xb6, 0xb6, 0xb7, 10},
{0xb6, 0xb6, 0xb6, 0xb6, 10},
{0xb6, 0xb6, 0xb6, 0xb0, 10},
{0xb6, 0xb6, 0xb6, 0x80, 10},
{0xb6, 0xb6, 0xb0, 0x80, 10},
{0xb6, 0xb6, 0x80, 0x80, 10},
{0xb6, 0xb0, 0x80, 0x80, 10},
{0xb6, 0x80, 0x80, 0x80, 10},
{0xb0, 0x80, 0x80, 0x80, 10},
{0x80, 0x80, 0x80, 0x80, 10},
{0x80, 0x80, 0x80, 0x00, 10},
{0x80, 0x80, 0x00, 0x00, 10},
{0x80, 0x00, 0x00, 0x00, 10},
{0x00, 0x00, 0x00, 0x00, 10},
{0xf7, 0xff, 0xff, 0xff, 10},
{0xb7, 0xff, 0xff, 0xff, 10},
{0xb6, 0xff, 0xff, 0xff, 10},
{0xb6, 0xf7, 0xff, 0xff, 10},
{0xb6, 0xb7, 0xff, 0xff, 10},
{0xb6, 0xb6, 0xff, 0xff, 10},
{0xb6, 0xb6, 0xf7, 0xff, 10},
{0xb6, 0xb6, 0xb7, 0xff, 10},
{0xb6, 0xb6, 0xb6, 0xff, 10},
{0xb6, 0xb6, 0xb6, 0xf7, 10},
{0xb6, 0xb6, 0xb6, 0xb7, 10},
{0xb6, 0xb6, 0xb6, 0xb6, 10},
{0xb6, 0xb6, 0xb6, 0xb0, 10},
{0xb6, 0xb6, 0xb6, 0x80, 10},
{0xb6, 0xb6, 0xb0, 0x80, 10},
{0xb6, 0xb6, 0x80, 0x80, 10},
{0xb6, 0xb0, 0x80, 0x80, 10},
{0xb6, 0x80, 0x80, 0x80, 10},
{0xb0, 0x80, 0x80, 0x80, 10},
{0x80, 0x80, 0x80, 0x80, 10},
{0x80, 0x80, 0x80, 0x00, 10},
{0x80, 0x80, 0x00, 0x00, 10},
{0x80, 0x00, 0x00, 0x00, 10},
{0x00, 0x00, 0x00, 0x00, 10},
};




VHDL: 四合一七段顯示器解碼器

         這個練習用VHDL設計一個四合一的七段顯示器解碼器,然後燒錄電路於CPLD中,再用Arduino與其對接,用程式驗證此解碼器的正確性。


          用VHDL語法撰寫的四合一的七段顯示器解碼器原始碼:

ibrary ieee;
use ieee.std_logic_1164.all;

entity seven_seg_4in1 is 
port(    com_in:     in  std_logic_vector(1 downto 0);
            num_in:    in  std_logic_vector(3 downto 0);
          com_out: out  std_logic_vector(3 downto 0);
           num_out: out  std_logic_vector(7 downto 0)
           );
 end seven_seg_4in1;

 architecture x of  seven_seg_4in1 is
 begin
 with com_in select 
com_out <= "0001" when "00",
                    "0010" when "01",
                    "0100" when "10",
                    "1000" when "11",
                    "0000" when others;
 
with num_in select
num_out<="11000000" when "0000",
"11111001" when "0001",
"10100100" when "0010",
"10110000" when "0011",
"10011001" when "0100",
"10010010" when "0101",
"10000010" when "0110",
"11111000" when "0111",
"10000000" when "1000",
"10011000" when "1001",
"10001000" when "1010",
"10000011" when "1011",
"11000110" when "1100",
"10100001" when "1101",
"10000110" when "1110",
"10001110" when "1111";
 end x;



腳位配置圖(Pin Asignment)


Arduino 端的程式碼:

#define com_in_0 4
#define com_in_1 5
#define num_in_0 8 
#define num_in_1 9
#define num_in_2 10
#define num_in_3 11

void setup() {
  pinMode(com_in_0, OUTPUT);
  pinMode(com_in_1, OUTPUT);
  pinMode(num_in_0, OUTPUT);
  pinMode(num_in_1, OUTPUT);
  pinMode(num_in_2, OUTPUT);
  pinMode(num_in_3, OUTPUT);
  
}

void loop() {
 int i, j;
 for(j=0; j<4; j++)
 {
    digitalWrite(com_in_1, (j>>1)&1); 
    digitalWrite(com_in_0, j&1); 
    
    for(i=0; i<16; i++)
    {
      digitalWrite(num_in_3, (i>>3)&1); 
      digitalWrite(num_in_2, (i>>2)&1); 
      digitalWrite(num_in_1, (i>>1)&1); 
      digitalWrite(num_in_0, i&1);
      delay(300);
    }
 }
}

執行結果:



LabVIEW: 用4個七段顯示器顯示00.00~99.99的數值

         這個練習主要是利用cluster組織七段顯示器,並用index array取出預存的編碼值(其中還會用到Quotient & Remainder的元件,進行位數的處理),最後進行顯示。

Block diagram 


Front panel


執行結果


2016年3月28日 星期一

C# : 文字編輯器

本練習重點在於開啟檔案,編修,並做存檔。

完整程式碼:

form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace P20160328_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Menu_File_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Menu_File_Open_Click(object sender, EventArgs e)
        {
            if (OFDialog.ShowDialog() == DialogResult.OK)
            {
                Form2 frm = new  Form2() ;
                frm.MdiParent = this;
                frm.Show() ;
                frm.WindowState = FormWindowState.Maximized;
                if(OFDialog.FilterIndex == 1)
                    frm.RTB.LoadFile(OFDialog.FileName, RichTextBoxStreamType.PlainText);
                else
                    frm.RTB.LoadFile(OFDialog.FileName, RichTextBoxStreamType.RichText);

                frm.Text = OFDialog.FileName;
            }
        }

        private void Menu_File_Save_Click(object sender, EventArgs e)
        {
            //get avtive mdi child window
            Form2 activefrm = (Form2)this.ActiveMdiChild;
            if (activefrm != null)
            {
                //check file format
                if (activefrm.Text.Substring(activefrm.Text.Length - 3, 3) == "txt")
                    activefrm.RTB.SaveFile(activefrm.Text.Substring(2, activefrm.Text.Length - 2),
                        RichTextBoxStreamType.PlainText);
                else
                    activefrm.RTB.SaveFile(activefrm.Text.Substring(2, activefrm.Text.Length - 2),
                        RichTextBoxStreamType.RichText);

                //modify text of the active window
                activefrm.Text = activefrm.Text.Substring(2, activefrm.Text.Length - 2);
            }            
        }

        private void Menu_File_SaveAs_Click(object sender, EventArgs e)
        {
            //ok button of save file dialog  is pressed
            if (SFDialog.ShowDialog() == DialogResult.OK)
            {
                Form2 activefrm = (Form2)this.ActiveMdiChild;

                //check file format 
                if (SFDialog.FilterIndex == 1)
                {
                    activefrm.RTB.SaveFile(SFDialog.FileName,
                        RichTextBoxStreamType.PlainText);
                }
                else
                {
                    activefrm.RTB.SaveFile(SFDialog.FileName,
                        RichTextBoxStreamType.RichText);
                }

                //modify text of the active window
                activefrm.Text = SFDialog.FileName;
            }

        }

        private void Menu_Edit_Font_Click(object sender, EventArgs e)
        {
            Form2 activefrm = (Form2)this.ActiveMdiChild;
            if (activefrm != null)
                if (FDialog.ShowDialog() == DialogResult.OK)
                    activefrm.RTB.SelectionFont = FDialog.Font;
        }
        private void Menu_Edit_Color_Click(object sender, EventArgs e)
        {
            Form2 activefrm = (Form2)this.ActiveMdiChild;
            if (activefrm != null)
                if (CDialog.ShowDialog() == DialogResult.OK)
                    activefrm.RTB.SelectionColor = CDialog.Color;
        }

        private void Form1_MdiChildActivate(object sender, EventArgs e)
        {
            Form2 activefrm = (Form2)this.ActiveMdiChild;
            if (activefrm != null)
            {
                Menu_File_Save.Enabled = true;
                Menu_File_SaveAs.Enabled = true;
                Menu_Edit_Font.Enabled = true;
                Menu_Edit_Color.Enabled = true;
            }
            else
            {
                Menu_File_Save.Enabled = false;
                Menu_File_SaveAs.Enabled = false;
                Menu_Edit_Font.Enabled = false;
                Menu_Edit_Color.Enabled = false;   
            }
        }
    }
}

form2.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace P20160328_1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void RTB_TextChanged(object sender, EventArgs e)
        {
            if (this.Text.Substring(0, 1) != "*")
                this.Text = "* " + this.Text;
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.Text.Substring(0, 1) == "*")
            {
                if (MessageBox.Show("Save?", "File modified !", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (this.Text.Substring(this.Text.Length - 3, 3) == "txt")
                        RTB.SaveFile(this.Text.Substring(2, this.Text.Length - 2), RichTextBoxStreamType.PlainText);
                    else
                        RTB.SaveFile(this.Text.Substring(2, this.Text.Length - 2), RichTextBoxStreamType.RichText);
                }
            }
        }
    }
}

form1 視窗設計


form2 視窗設計


LabVIEW: 紅綠燈設計(用Array改寫)

延續上一個練習,將紅綠燈改成用兩個一為陣列控制。(注意array的引線穿進for迴圈時,會做auto indexing,如要取消須選取迴圈邊框的[]點,按右鍵選取Disable Indexing)

Block diagram

Front panel 

LabVIEW: 紅綠燈設計

        這個練習主要練習cluster, for迴圈,while迴圈還有flat sequence結構:綠燈先持續亮3秒鐘,然後閃爍五秒鐘,換黃燈持續亮5秒,最後紅燈亮5秒。








程式執行結果


2016年3月26日 星期六

VHDL: 除頻電路設計

        這個練習是要設計一個除頻電路,我的外部clock是由一個4MH的石英震盪器所產生,我想用一個除頻電路將其輸出頻率更改為1HZ: 其實做法很簡單,只要做一個計數器計算到第2M cycle和第4M cycle時輸出訊號做反向的動作就可以完成。

完整程式如下: 

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;

entity my_div is
    generic(divisor:integer:=4000000);         -- 因為要產生1Hz的Cclock, 所以定義除數為4000000
port(
    clk_in: in std_logic;
    clk_out: out std_logic
);
end my_div;

architecture arch of my_div is
    signal cnt2 : std_logic;
begin
    process(clk_in)
        variable cnt1 : integer range 0 to divisor:=1;
        variable divisor2 : integer range 0 to divisor;
    begin
        divisor2:=divisor/2;
        if(clk_in'event and clk_in='1') then         -- cnt1為計數器,累加至4000000時歸0
            if cnt1 = divisor then 
                cnt1 := 1;
            else
            cnt1 := cnt1 + 1;
            end if;
        end if;

        if(clk_in'event and clk_in='1') then
            if((cnt1 = divisor2) or (cnt1 = divisor)) then  --  cn1 為2000000或4000000時,輸出做反向
                cnt2<= not cnt2;
            end if;
        end if;
        clk_out <= cnt2;
    end process;
    end arch;


結果如下:




VHDL: 半加器練習

        VHDL 超高速積體電路硬體描述語言(VHSIC hardware description language)在數位系統設計和IC設計上應用極廣,孰悉她的語法有助於我們設計自己的數位系統。
     
VHDL 的語法和BASIC,Pascal,C很相似,學過以上語言的人應該很容易上手,但是硬體的延遲處理,和平行處理等特性,不像傳統程式語言一行一行敘述執行,這是學VHDL要特別注意的。

        這個練習主要是先了解VHDL的程式架構,在編譯成功之後,放到實際硬體應證結果。

程式:

----------------------- 這是註解: 用VHDL描述半加器 --------------------------------

library ieee;                                    -- 使用ieee標準元件庫
use ieee.std_logic_1164.all;            -- 選擇元件包裝

entity half_adder is                         -- 描述此元件規格: 兩個輸入port和兩個輸出port
port (x, y: in std_logic;
s, c: out std_logic);
end half_adder;

architecture ha of half_adder is       -- 描述此元件的行為: 
begin                                             
s<= x  xor y;                          -- x 和 y 做 xor 運算之後,輸出到 s 
c<= x and y ;                         -- x 和 y 做 and 運算之後,輸出到 c 
end ha;
-----------------------------------------------------------------------------------------------

在Quartus裡Compile成功之後,做成Symbol file,再給定input和output



腳位配置(pin assigment)


模擬(Simulation)結果正確無誤



最後就是驗證的工作了: 
輸入部分,我採用arduino以輸出腳位給值(寫程式控制)的方式,這樣可以減少麵包板上的插件動作,加快驗證工作的進行。輸出部分則是用兩顆LED連接s和c。

2016年3月24日 星期四

3×3魔術方塊解法

3×3魔術方塊解法

公元1974年布達佩斯(匈牙利首都)的一位年輕的建築學教授- 愛爾諾魯畢克(Rubik) 出於教學上的需要,設計了一個工程結構26個陵長為1.9公分的小立方體,能自由地繞轉著一個同樣大小的中心塊轉動,為了區別這些小立方體,魯畢克教授,在這些小方塊的表面貼了不同顏色的塑膠圖片,這就是世上第一個魔術方塊。魔術方塊於1975年申請專利,1977年開始出現於市場,1981年的統計,世上魔術方塊的愛好者已 超過千萬之眾。

零、基本介紹
一、魔術方塊的構造:
3×3魔術方塊由構造來分,可分為中心、邊、角。


二、魔術方塊公式怎麼看:
我們記錄公式的方式都是把其中一面對著自己,然後各別去定位每一面的方向,再以目標面對準自己,旋轉順時針方向和逆時針方向來記錄轉的方向 ,以下做個符號簡易說明

中間層方向

三、簡易流程:
基本的方塊解法是叫「LBL(Layer by Layer)」,它的步驟如下圖

壹.第一層First ayer
很多人沒學過魔方的人都說:「我只會解一『面』。」其實解3×3魔術方塊第一步驟是第一「層」,最後目的為上圖左二第一層(FL),解第一面和第一層的解法沒有差異,只要注意每一個角塊和邊塊應該放到的位置,通常我們從白色那面開始,第一層側面邊角要和中心顏色相同要解完不是難事。以下是幾種基本的狀況: 

若有角或邊在已在正確的「位置」,但是方向不正確,那就先隨意放上一個錯誤的角或邊,再放入正確的。

貳.第二層Sceond ayer
解完第一層後,將方塊180度翻過來,使底層朝下,再把第二層的中心對到第一層的顏色。再來就是稍微較難理解的公式了,有兩個公式。首先你會看到頂層和頂面是一堆雜亂的顏色,先任意鎖定一個該放入第二層的邊塊,將該邊塊在第三「層」的顏色對到中心上,會出現以下兩種情況


    接著將剩下的邊塊依照上面兩個公式完成。若遇到邊塊在錯誤槽的情況,就先用公式放入其他錯誤的邊塊,再將正確的放入即可。若遇到已在正確槽,卻方向錯誤的情況時,也是先用公式放入任意錯誤邊塊,再用公式放入正確的邊


參.頂面之邊Orientation of ast ayer-Edges

在這個步驟裡,目的是要將頂面轉出「十字」,特別注意:不要受到角的影響,公式只有一條,就是大家俗稱的「六步法」。

六步法:

 公式(六步法):「R' U' F' U F R

   我們只要一直重覆同一個公式,從case1經過一個完整的流程到Done。可能出現的情況有4種:case1轉一次六步法會變成case2case2轉一次六步法會變成case3L形換到如圖示方向,轉一次六步法就完成了 Done


肆.頂面之角Orientation of ast ayer-Corners

    接著是較複雜的步驟,有兩個公式,可是有七種情況,目的是完成頂面成同一顏色。公式:
 
    還有以下五種狀況,只要按照下面的方向擺好轉一次以上左邊步驟,就會出現上面兩種圖形之一,然後再依情況選擇方法完成。

伍.第三層之角Permutauion of ast ayer-Corners  (換角)
    這個步驟裡的公式只有一個,有點長,但不會很難記。首先,看到第三層的角塊,找到有同色的面,並將其朝左如case1


*新手注意事項:
  1. 這裡的公式嚴格說起來只有兩個,其餘為「鏡像公式」,所以要熟悉鏡向公式的推法。
  2. 角的對齊要仔細看,別被邊搞混了,當然邊也是一樣別被角搞混了。
對齊方式說明:


陸.第三層之邊Permutauion of ast ayer-Edges (換邊)
       若遇到以下兩種狀況,不用管方向,只要做一次P3P4的公式就會出現P3P4Case,然後比照辦理。

恭喜你成功解出3×3魔術方塊

:參考 .方塊達人鋪http://www.unicube.tw/
參考 Ian 2008/5/11完成之「3*3魔術方塊基礎解法.