单片机unsigned char code/unsigned char idata变量

本文解释了在C51编程语言中使用data关键词的意义,它用于定义变量存储在可直接寻址的内部数据存储区(128B)。文章详细介绍了不同存储类型的用途,包括code、data、idata、bdata、xdata和pdata。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

From:  http://hi.baidu.com/%CC%EC%CC%ECiloveyou/blog/item/89605380c8a4ffd49123d9db.html

 

在读别人的程序中,有这样的语句

unsigned char data 变量名,怎么多了一个data关键词?

查了帮助文档C51。pdf,在Page91页找到了答案:

这里的关键词:data, code,定义了变量存储的内存空间。

附录:

附图

c51中的存储类型

code :程序存储区(64KB),

data :可直接寻址的内部数据存储区(128B)

idata:不可直接寻址的内部数据存储区(256B)

bdata:可位寻址内部数据存储区(16B)

xdata:外部数据存储区(64KB)

pdata:分页的外部数据存储区

#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); } } } 修改使得适配字模 陆喆,且相应的变量名改变
最新发布
07-06
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值