#include <reg51.h>
#include <intrins.h>
sbit LE1 = P3^6;
sbit LE2 = P3^7;
unsigned char temp;
unsigned int d ;
const unsigned char code wang_norm[32] = {
0x00,0x00,0xFF,0x7F,0x00,0x01,0x00,0x01,
0x00,0x01,0x00,0x01,0x00,0x01,0xF8,0x3F,
0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,
0x00,0x01,0x00,0x01,0xFE,0x7F,0x00,0x00
};
const unsigned char code jing_norm[32] = {
0x20,0x20,0x20,0x10,0xFE,0x13,0x20,0xFC,
0xFC,0x01,0x20,0x10,0xFE,0x8B,0x00,0x88,
0xFC,0x49,0x04,0x49,0xFC,0x51,0x04,0x51,
0xFC,0x1D,0x04,0xE1,0x14,0x41,0x08,0x01
};
const unsigned char code han_norm[32] = {
0x00,0x00,0xF8,0x23,0x10,0x10,0x20,0x08,
0x44,0x84,0x54,0x45,0xE4,0x44,0x44,0x14,
0xE4,0x14,0x54,0x25,0x4C,0xE6,0x44,0x25,
0x84,0x24,0x04,0x24,0xFC,0x27,0x04,0x00
};
unsigned char idata display_buf[16][2];
unsigned char data scroll_offset = 0;
unsigned char data current_row = 0;
unsigned char data refresh_counter = 0;
void timer0_init(void);
void update_display_buffer(void);
void delay_ms(unsigned int ms);
void display_test_pattern(void);
void timer0_init(void) {
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x67;
ET0 = 1;
EA = 1;
TR0 = 1;
}
unsigned char reverse_bits(unsigned char b) {
unsigned char result = 0;
if(b & 0x01) result |= 0x80;
if(b & 0x02) result |= 0x40;
if(b & 0x04) result |= 0x20;
if(b & 0x08) result |= 0x10;
if(b & 0x10) result |= 0x08;
if(b & 0x20) result |= 0x04;
if(b & 0x40) result |= 0x02;
if(b & 0x80) result |= 0x01;
return result;
}
void update_display_buffer(void) {
unsigned char i;
unsigned char col_byte;
unsigned char col_bit;
unsigned char effective_offset = scroll_offset;
if(effective_offset >= 64) effective_offset = 0;
col_byte = effective_offset / 8;
col_bit = effective_offset % 8;
for(i = 0; i < 16; i++) {
unsigned char wang_left;
unsigned char wang_right;
unsigned char jing_left;
unsigned char jing_right;
unsigned char han_left;
unsigned char han_right;
unsigned char byte0;
unsigned char byte1;
unsigned char byte2;
wang_left = wang_norm[i*2];
wang_right = wang_norm[i*2+1];
jing_left = jing_norm[i*2];
jing_right = jing_norm[i*2+1];
han_left = han_norm[i*2];
han_right = han_norm[i*2+1];
if(col_byte < 2) {
byte0 = wang_left;
byte1 = wang_right;
byte2 = 0x00;
}
else if(col_byte < 4) {
if(col_byte == 2) {
byte0 = wang_right;
byte1 = 0x00;
byte2 = jing_left;
} else {
byte0 = 0x00;
byte1 = jing_left;
byte2 = jing_right;
}
}
else if(col_byte < 6) {
byte0 = jing_left;
byte1 = jing_right;
byte2 = 0x00;
}
else if(col_byte < 8) {
if(col_byte == 6) {
byte0 = jing_right;
byte1 = 0x00;
byte2 = han_left;
} else {
byte0 = 0x00;
byte1 = han_left;
byte2 = han_right;
}
}
else {
byte0 = 0x00;
byte1 = 0x00;
byte2 = 0x00;
}
byte0 = reverse_bits(byte0);
byte1 = reverse_bits(byte1);
byte2 = reverse_bits(byte2);
display_buf[i][0] = (byte0 >> col_bit) | (byte1 << (8 - col_bit));
temp = (byte1 >> col_bit) | (byte2 << (8 - col_bit));
display_buf[i][1] = temp;
}
}
void timer0_isr(void) interrupt 1 {
TH0 = 0xFC;
TL0 = 0x67;
refresh_counter++;
if(refresh_counter < 2) return;
refresh_counter = 0;
P2 = 0x00;
LE1 = 1;
_nop_(); _nop_(); _nop_();
LE1 = 0;
LE2 = 1;
_nop_(); _nop_(); _nop_();
LE2 = 0;
P3 = (P3 & 0xF0) | current_row;
_nop_(); _nop_(); _nop_();
P2 = display_buf[current_row][1];
LE1 = 1;
_nop_(); _nop_(); _nop_();
LE1 = 0;
P2 = display_buf[current_row][0];
LE2 = 1;
_nop_(); _nop_(); _nop_();
LE2 = 0;
current_row++;
if(current_row >= 16) {
current_row = 0;
}
}
void delay_ms(unsigned int ms) {
unsigned int i, j;
for(i = 0; i < ms; i++) {
for(j = 0; j < 120; j++) {
_nop_(); _nop_(); _nop_();
}
}
}
void display_test_pattern(void) {
unsigned char i, j;
for(j = 0; j < 5; j++) {
for(i = 0; i < 16; i++) {
P3 = i;
P2 = (j % 2) ? 0xFF : 0x00;
LE1 = 1; LE1 = 0;
P2 = (j % 2) ? 0xFF : 0x00;
LE2 = 1; LE2 = 0;
delay_ms(5);
}
delay_ms(200);
}
for(i = 0; i < 16; i++) {
P3 = i;
P2 = reverse_bits(wang_norm[i*2]);
LE2 = 1; LE2 = 0;
P2 = reverse_bits(wang_norm[i*2+1]);
LE1 = 1; LE1 = 0;
delay_ms(2);
}
delay_ms(1000);
for(i = 0; i < 16; i++) {
P3 = i;
P2 = 0x00;
LE1 = 1; LE1 = 0;
LE2 = 1; LE2 = 0;
}
}
void main(void) {
unsigned char i;
P0 = 0xFF;
P1 = 0xFF;
P2 = 0x00;
P3 = 0x00;
for(i = 0; i < 16; i++) {
display_buf[i][0] = 0x00;
display_buf[i][1] = 0x00;
}
display_test_pattern();
timer0_init();
while(1) {
update_display_buffer();
scroll_offset++;
if(scroll_offset >= 64) {
scroll_offset = 0;
}
for(d = 0; d < 80; d++) {
delay_ms(1);
}
}
}
修改使得适配字模 陆喆,且相应的变量名改变
最新发布