├── 1-led ├── D1-D16_LED.c ├── D9-D16_LED_switch.c ├── main.c ├── template.hex └── template.uvproj ├── 2-beep_music ├── SoundPlay.h ├── main.c ├── music.h └── template.uvproj ├── 3-led_segment_displays ├── 7-segment_display.c ├── 7段数码管-26字母表.xls ├── README.md └── dynamic_digital_tube_display.c ├── 4-matrix_key └── matrix_key.c ├── 5-matrix_8x8LED ├── README.md ├── kc51.cbp ├── kc51.hex └── main.c ├── 6-timer ├── inter_timer.c └── query_timer.c ├── 7-external_interrupt ├── external_interrupt.c └── simple_example.c ├── 8-lcd1602_display ├── README.md ├── lcd1602.cbp └── main.c ├── 9-infrared_sensor ├── hwx.cbp └── main.c ├── 999-KeilProject ├── 10-1 DS1302时钟 │ ├── DS1302.c │ ├── DS1302.h │ ├── Delay.c │ ├── Delay.h │ ├── LCD1602.c │ ├── LCD1602.h │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 10-2 DS1302可调时钟 │ ├── DS1302.c │ ├── DS1302.h │ ├── Delay.c │ ├── Delay.h │ ├── Key.c │ ├── Key.h │ ├── LCD1602.c │ ├── LCD1602.h │ ├── Project.uvopt │ ├── Project.uvproj │ ├── Timer0.c │ ├── Timer0.h │ └── main.c ├── 11-1 蜂鸣器播放提示音 │ ├── Buzzer.c │ ├── Buzzer.h │ ├── Delay.c │ ├── Delay.h │ ├── Key.c │ ├── Key.h │ ├── Nixie.c │ ├── Nixie.h │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 11-2 蜂鸣器播放音乐 │ ├── Delay.c │ ├── Delay.h │ ├── Project.uvopt │ ├── Project.uvproj │ ├── Timer0.c │ ├── Timer0.h │ └── main.c ├── 12-1 AT24C02数据存储 │ ├── AT24C02.c │ ├── AT24C02.h │ ├── Delay.c │ ├── Delay.h │ ├── I2C.c │ ├── I2C.h │ ├── Key.c │ ├── Key.h │ ├── LCD1602.c │ ├── LCD1602.h │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 12-2 秒表(定时器扫描按键数码管) │ ├── AT24C02.c │ ├── AT24C02.h │ ├── Delay.c │ ├── Delay.h │ ├── I2C.c │ ├── I2C.h │ ├── Key.c │ ├── Key.h │ ├── Nixie.c │ ├── Nixie.h │ ├── Project.uvopt │ ├── Project.uvproj │ ├── Timer0.c │ ├── Timer0.h │ └── main.c ├── 13-1 DS18B20温度读取 │ ├── DS18B20.c │ ├── DS18B20.h │ ├── Delay.c │ ├── Delay.h │ ├── LCD1602.c │ ├── LCD1602.h │ ├── OneWire.c │ ├── OneWire.h │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 13-2 DS18B20温度报警器 │ ├── AT24C02.c │ ├── AT24C02.h │ ├── DS18B20.c │ ├── DS18B20.h │ ├── Delay.c │ ├── Delay.h │ ├── I2C.c │ ├── I2C.h │ ├── Key.c │ ├── Key.h │ ├── LCD1602.c │ ├── LCD1602.h │ ├── OneWire.c │ ├── OneWire.h │ ├── Project.uvopt │ ├── Project.uvproj │ ├── Timer0.c │ ├── Timer0.h │ └── main.c ├── 14-1 LCD1602液晶显示屏 │ ├── Delay.c │ ├── Delay.h │ ├── LCD1602.c │ ├── LCD1602.h │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 15-1 LED呼吸灯 │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 15-2 直流电机调速 │ ├── Delay.c │ ├── Delay.h │ ├── Key.c │ ├── Key.h │ ├── Nixie.c │ ├── Nixie.h │ ├── Project.uvopt │ ├── Project.uvproj │ ├── Timer0.c │ ├── Timer0.h │ ├── main.c │ └── main.c.bak ├── 16-1 AD模数转换 │ ├── Delay.c │ ├── Delay.h │ ├── LCD1602.c │ ├── LCD1602.h │ ├── Project.uvopt │ ├── Project.uvproj │ ├── XPT2046.c │ ├── XPT2046.h │ └── main.c ├── 16-2 DA数模转换 │ ├── Delay.c │ ├── Delay.h │ ├── Project.uvopt │ ├── Project.uvproj │ ├── Timer0.c │ ├── Timer0.h │ └── main.c ├── 17-1 红外遥控(11.0592MHz晶振) │ ├── Delay.c │ ├── Delay.h │ ├── IR.c │ ├── IR.h │ ├── Int0.c │ ├── Int0.h │ ├── LCD1602.c │ ├── LCD1602.h │ ├── Project.uvopt │ ├── Project.uvproj │ ├── Timer0.c │ ├── Timer0.h │ └── main.c ├── 17-1 红外遥控 │ ├── Delay.c │ ├── Delay.h │ ├── IR.c │ ├── IR.h │ ├── Int0.c │ ├── Int0.h │ ├── LCD1602.c │ ├── LCD1602.h │ ├── Project.uvopt │ ├── Project.uvproj │ ├── Timer0.c │ ├── Timer0.h │ └── main.c ├── 17-2 红外遥控电机调速(11.0592MHz晶振) │ ├── Delay.c │ ├── Delay.h │ ├── IR.c │ ├── IR.h │ ├── Int0.c │ ├── Int0.h │ ├── Key.c │ ├── Key.h │ ├── Motor.c │ ├── Motor.h │ ├── Nixie.c │ ├── Nixie.h │ ├── Project.uvopt │ ├── Project.uvproj │ ├── Timer0.c │ ├── Timer0.h │ ├── Timer1.c │ ├── Timer1.h │ └── main.c ├── 17-2 红外遥控电机调速 │ ├── Delay.c │ ├── Delay.h │ ├── IR.c │ ├── IR.h │ ├── Int0.c │ ├── Int0.h │ ├── Key.c │ ├── Key.h │ ├── Motor.c │ ├── Motor.h │ ├── Nixie.c │ ├── Nixie.h │ ├── Project.uvopt │ ├── Project.uvproj │ ├── Timer0.c │ ├── Timer0.h │ ├── Timer1.c │ ├── Timer1.h │ └── main.c ├── 2-1 点亮一个LED │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 2-2 LED闪烁 │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 2-3 LED流水灯 │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 2-4 LED流水灯Plus │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 3-1 独立按键控制LED亮灭 │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 3-2 独立按键控制LED状态 │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 3-3 独立按键控制LED显示二进制 │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 3-4 独立按键控制LED移位 │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 4-1 静态数码管显示 │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 4-2 动态数码管显示 │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 5-1 模块化编程 │ ├── Delay.c │ ├── Delay.h │ ├── Nixie.c │ ├── Nixie.h │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 5-2 LCD1602调试工具 │ ├── Delay.c │ ├── Delay.h │ ├── LCD1602.c │ ├── LCD1602.h │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 6-1 矩阵键盘 │ ├── Delay.c │ ├── Delay.h │ ├── LCD1602.c │ ├── LCD1602.h │ ├── MatrixKey.c │ ├── MatrixKey.h │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 6-2 矩阵键盘密码锁 │ ├── Delay.c │ ├── Delay.h │ ├── LCD1602.c │ ├── LCD1602.h │ ├── MatrixKey.c │ ├── MatrixKey.h │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 7-1 按键控制LED流水灯模式 │ ├── Delay.c │ ├── Delay.h │ ├── Key.c │ ├── Key.h │ ├── Project.uvopt │ ├── Project.uvproj │ ├── Timer0.c │ ├── Timer0.h │ └── main.c ├── 7-2 定时器时钟 │ ├── Delay.c │ ├── Delay.h │ ├── LCD1602.c │ ├── LCD1602.h │ ├── Project.uvopt │ ├── Project.uvproj │ ├── Timer0.c │ ├── Timer0.h │ └── main.c ├── 8-1 串口向电脑发送数据 │ ├── Delay.c │ ├── Delay.h │ ├── Project.uvopt │ ├── Project.uvproj │ ├── UART.c │ ├── UART.h │ └── main.c ├── 8-2 电脑通过串口控制LED │ ├── Delay.c │ ├── Delay.h │ ├── Project.uvopt │ ├── Project.uvproj │ ├── UART.c │ ├── UART.h │ └── main.c ├── 9-1 LED点阵屏显示图形 │ ├── Delay.c │ ├── Delay.h │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── 9-2 LED点阵屏显示动画 │ ├── Delay.c │ ├── Delay.h │ ├── MatrixLED.c │ ├── MatrixLED.h │ ├── Project.uvopt │ ├── Project.uvproj │ └── main.c ├── LCD1602驱动代码 │ ├── LCD1602.c │ └── LCD1602.h └── 程序说明.pdf ├── Document ├── 51单片机电路原理图_HC6800-ES_V2.0新版.pdf └── README.md ├── README.md ├── img ├── 1-led.webp ├── 8x8led_hong.jpg ├── digital_watch.webp ├── hello_world.webp ├── lcd1602.jpg └── str-7segment.png ├── sdcc ├── README.md ├── code │ ├── MCS51.cbp │ ├── SoundPlay.h │ ├── hc6800v2.h │ ├── main.c │ └── music.h └── lib_src │ ├── __assert.c │ ├── __memcpy.c │ ├── _atof.c │ ├── _autobaud.c │ ├── _bp.c │ ├── _decdptr.c │ ├── _divschar.c │ ├── _divsint.c │ ├── _divslong.c │ ├── _divslonglong.c │ ├── _divuchar.c │ ├── _divuint.c │ ├── _divulong.c │ ├── _divulonglong.c │ ├── _fs2schar.c │ ├── _fs2sint.c │ ├── _fs2slong.c │ ├── _fs2uchar.c │ ├── _fs2uint.c │ ├── _fs2ulong.c │ ├── _fsadd.c │ ├── _fscmp.c │ ├── _fsdiv.c │ ├── _fseq.c │ ├── _fsget1arg.c │ ├── _fsget2args.c │ ├── _fslt.c │ ├── _fsmul.c │ ├── _fsneq.c │ ├── _fsnormalize.c │ ├── _fsreturnval.c │ ├── _fsrshift.c │ ├── _fssub.c │ ├── _fsswapargs.c │ ├── _gptrget.c │ ├── _gptrgetc.c │ ├── _gptrput.c │ ├── _heap.c │ ├── _itoa.c │ ├── _logexpf.c │ ├── _ltoa.c │ ├── _memchr.c │ ├── _memcmp.c │ ├── _memmove.c │ ├── _memset.c │ ├── _modschar.c │ ├── _modsint.c │ ├── _modslong.c │ ├── _modslonglong.c │ ├── _moduchar.c │ ├── _moduint.c │ ├── _modulong.c │ ├── _modulonglong.c │ ├── _mulint.c │ ├── _mullong.c │ ├── _mullonglong.c │ ├── _mulschar.c │ ├── _muluchar.c │ ├── _rlslonglong.c │ ├── _rlulonglong.c │ ├── _rrslonglong.c │ ├── _rrulonglong.c │ ├── _schar2fs.c │ ├── _ser.c │ ├── _setjmp.c │ ├── _sint2fs.c │ ├── _slong2fs.c │ ├── _spx.c │ ├── _startup.c │ ├── _strcat.c │ ├── _strchr.c │ ├── _strcmp.c │ ├── _strcpy.c │ ├── _strcspn.c │ ├── _strlen.c │ ├── _strncat.c │ ├── _strncmp.c │ ├── _strncpy.c │ ├── _strpbrk.c │ ├── _strrchr.c │ ├── _strspn.c │ ├── _strstr.c │ ├── _strtok.c │ ├── _uchar2fs.c │ ├── _uint2fs.c │ ├── _ulong2fs.c │ ├── abs.c │ ├── acosf.c │ ├── aligned_alloc.c │ ├── asincosf.c │ ├── asinf.c │ ├── atan2f.c │ ├── atanf.c │ ├── atoi.c │ ├── atol.c │ ├── atoll.c │ ├── bpx.c │ ├── bsearch.c │ ├── btowc.c │ ├── c16rtomb.c │ ├── c16stombs.c │ ├── c32rtomb.c │ ├── calloc.c │ ├── ceilf.c │ ├── cosf.c │ ├── coshf.c │ ├── cotf.c │ ├── errno.c │ ├── expf.c │ ├── fabsf.c │ ├── floorf.c │ ├── free.c │ ├── frexpf.c │ ├── gets.c │ ├── isalnum.c │ ├── isalpha.c │ ├── isblank.c │ ├── iscntrl.c │ ├── isdigit.c │ ├── isgraph.c │ ├── isinf.c │ ├── islower.c │ ├── isnan.c │ ├── isprint.c │ ├── ispunct.c │ ├── isspace.c │ ├── isupper.c │ ├── isxdigit.c │ ├── labs.c │ ├── ldexpf.c │ ├── log10f.c │ ├── logf.c │ ├── malloc.c │ ├── mblen.c │ ├── mbrlen.c │ ├── mbrtoc16.c │ ├── mbrtoc32.c │ ├── mbrtowc.c │ ├── mbsinit.c │ ├── mbstoc16s.c │ ├── mbstowcs.c │ ├── mbtowc.c │ ├── mcs51 │ ├── crtbank.asm │ ├── crtcall.asm │ ├── crtclear.asm │ ├── crtpagesfr.asm │ ├── crtstart.asm │ ├── crtxclear.asm │ ├── crtxinit.asm │ ├── crtxpop.asm │ ├── crtxpopr0.asm │ ├── crtxpush.asm │ ├── crtxpushr0.asm │ ├── crtxstack.asm │ └── gptr_cmp.asm │ ├── memcpy.c │ ├── modff.c │ ├── powf.c │ ├── printf_fast.c │ ├── printf_fast_f.c │ ├── printf_large.c │ ├── printf_tiny.c │ ├── printfl.c │ ├── puts.c │ ├── qsort.c │ ├── rand.c │ ├── realloc.c │ ├── ser_ir.c │ ├── ser_ir_cts_rts.c │ ├── serial.c │ ├── sincosf.c │ ├── sincoshf.c │ ├── sinf.c │ ├── sinhf.c │ ├── sprintf.c │ ├── sqrtf.c │ ├── strtol.c │ ├── strtoul.c │ ├── strxfrm.c │ ├── tancotf.c │ ├── tanf.c │ ├── tanhf.c │ ├── time.c │ ├── tolower.c │ ├── toupper.c │ ├── vprintf.c │ ├── wcrtomb.c │ ├── wcscmp.c │ ├── wcslen.c │ ├── wcstombs.c │ ├── wctob.c │ └── wctomb.c └── src ├── SoundPlay.h ├── ds1302.c ├── ds1302.h ├── hc6800v2.h ├── hello.cbp ├── lcd1602a.c ├── lcd1602a.h ├── main.c ├── music.h ├── str_7segment ├── 26abc.cbp ├── README.md └── main.c └── test_ds1302.c /1-led/main.c: -------------------------------------------------------------------------------- 1 | // LED闪烁实验 和 跑马灯 2 | 3 | #include //此文件中定义了单片机的一些特殊功能寄存器 4 | #include //因为要用到左右移函数,所以加入这个头文件 5 | 6 | #define leds P2 7 | 8 | typedef unsigned int u16; //对数据类型进行声明定义 9 | typedef unsigned char u8; 10 | 11 | sbit led = P2 ^ 0; //将单片机的P2.0端口定义为led 12 | sbit led3 = P2 ^ 2; 13 | sbit led5 = P2 ^ 4; 14 | sbit led7 = P2 ^ 6; 15 | 16 | 17 | void delay(u16 i); 18 | void wait(u8 i); 19 | void led_work(u8 i); 20 | 21 | 22 | int main() 23 | { 24 | u8 i = 0; 25 | 26 | while (1) { 27 | 28 | for (i = 0; i != 5; i++) { 29 | led_work(i % 2); 30 | wait(1); //大约 s 31 | } 32 | 33 | 34 | leds = 0xfe; // led: 1111 1110 35 | wait(1); //大约 1s 36 | 37 | for (i = 0; i < 7; i++) { //将led左移一位 38 | leds = _crol_(leds, 1); 39 | delay(50000); 40 | } 41 | for (i = 0; i < 7; i++) { //将led右移一位 42 | leds = _cror_(leds, 1); 43 | delay(50000); 44 | } 45 | 46 | } 47 | 48 | return 0; 49 | } 50 | 51 | 52 | void delay(u16 i) 53 | { 54 | while (i--); 55 | } 56 | 57 | void wait(u8 i) 58 | { 59 | while (i--) { 60 | delay(50000); //大约延时450ms 61 | delay(5000); //大约延时45ms 62 | delay(50000); //大约延时450ms 63 | delay(5000); //大约延时45ms 64 | } 65 | } 66 | 67 | void led_work(u8 i) 68 | { 69 | led = i; 70 | led3 = i; 71 | led5 = i; 72 | led7 = i; 73 | } 74 | -------------------------------------------------------------------------------- /1-led/template.hex: -------------------------------------------------------------------------------- 1 | :10000300E4F508E4F508E5085401FF12008B7F01CD 2 | :1000130012005B0508E508B405EC75A0FE7F01122C 3 | :10002300005BE4F508AFA07801EF08800123D8FD59 4 | :1000330012007A0508E508C3940740E9E4F508E5EA 5 | :1000430008C3940750BDAFA07801EF08800103D81F 6 | :08005300FD12007A050880E7A8 7 | :06007A00F5A07F507EC3DB 8 | :0B008000EF1FAC0670011E4C70F62252 9 | :10005B00AD07AF051DEF601612007C7F887E131273 10 | :0F006B00008012007C7F887E1312008080E422C8 11 | :0C008B00EF24FF92A092A292A492A62261 12 | :0300000002009764 13 | :0C009700787FE4F6D8FD758108020003B4 14 | :00000001FF 15 | -------------------------------------------------------------------------------- /3-led_segment_displays/7-segment_display.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************** 2 | * 静态数码管显示实验 * 3 | 实现现象:下载程序后数码管静态显示0。 4 | 注意事项:如果不想让点阵模块显示,可以将74HC595模块上的JP595短接片拔掉。 5 | ***************************************************************************************/ 6 | 7 | #include //此文件中定义了单片机的一些特殊功能寄存器 8 | 9 | typedef unsigned int u16; //对数据类型进行声明定义 10 | typedef unsigned char u8; 11 | 12 | sbit LSA = P2 ^ 2; 13 | sbit LSB = P2 ^ 3; 14 | sbit LSC = P2 ^ 4; 15 | 16 | u8 code smgduan[] = { 17 | 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, // 0 1 2 3 4 5 6 7 8 9 18 | 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x3D, 0x76, 0x8F, 0x0E, // A b c d E F G H I J 19 | 0x75, 0x38, 0xB7, 0x54, 0x5C, 0x73, 0x67, 0x31, 0xC9, 0x78, // K L M n o P q r S t 20 | 0x3E, 0x1C, 0xFE, 0xE4, 0x6E, 0xDA, 0x40, 0x48, 0x80, 0x00 // U v W X Y Z - = . Null 21 | }; 22 | 23 | void delay(u16 i); 24 | 25 | void main() 26 | { 27 | u8 i = 0; 28 | 29 | LSA = 0; 30 | LSB = 0; 31 | LSC = 0; //控制38译码器的Y0输出低电平 32 | 33 | while (1) { 34 | for (i = 0 ; i != 40 ; i++) { 35 | P0 = smgduan[i]; 36 | delay(55000); 37 | delay(55000); 38 | P0 = smgduan[38]; 39 | delay(55000); 40 | } 41 | } 42 | } 43 | 44 | void delay(u16 i) 45 | { 46 | while (i--); 47 | } 48 | -------------------------------------------------------------------------------- /3-led_segment_displays/7段数码管-26字母表.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongwenjun/stc89c52/016336ebc6c8f2d1d86335b476f4bd2f21209857/3-led_segment_displays/7段数码管-26字母表.xls -------------------------------------------------------------------------------- /3-led_segment_displays/README.md: -------------------------------------------------------------------------------- 1 | - 开源项目网址 http://git.io/CB51 2 | 3 | ### 3. 动态数码管显示 [源码](https://github.com/hongwenjun/stc89c52/tree/master/3-led_segment_displays) :gift: [7段26字母 显示 Helloworld](https://github.com/hongwenjun/stc89c52/tree/master/src/str_7segment/) 4 | 5 | ![](https://raw.githubusercontent.com/hongwenjun/stc89c52/master/img/hello_world.webp) 6 | 7 | ```c 8 | sbit LSA = P2 ^ 2; 9 | sbit LSB = P2 ^ 3; 10 | sbit LSC = P2 ^ 4; //控制38译码器的Y0输出低电平 11 | 12 | u8 code smgduan[] = { 13 | 0x3f, 0x06, 0x5b, 0x4f, 0x66, // 0 1 2 3 4 14 | 0x6d, 0x7d, 0x07, 0x7f, 0x6f, // 5 6 7 8 9 15 | 0x77, 0x7c, 0x39, 0x5e, 0x79, // A B C D E 16 | 0x71, 0x00 // F NULL 17 | }; 18 | ``` 19 | 20 | - 文件: * 7段数码管-26字母表.xls * 用来扩展自定义字母和符号 21 | 22 | ![](https://raw.githubusercontent.com/hongwenjun/stc89c52/master/img/str-7segment.png) 23 | 24 | 25 | -------------------------------------------------------------------------------- /5-matrix_8x8LED/README.md: -------------------------------------------------------------------------------- 1 | - 开源项目网址 http://git.io/CB51 2 | 3 | ### 5. 8*8LED点阵-显示文字实验 [源码](https://github.com/hongwenjun/stc89c52/tree/master/5-matrix_8x8LED) 4 | 5 | ![](https://raw.githubusercontent.com/hongwenjun/stc89c52/master/img/8x8led_hong.jpg) 6 | 7 | ```c 8 | void Hc595SendByte(u8 dat) // 函数向74HC595发送一个字节的数据 9 | { 10 | u8 a; 11 | SRCLK = 0; 12 | RCLK = 0; 13 | for (a = 0; a < 8; a++) { 14 | SER = dat >> 7; // 获取最高位 0xA3 : 1010 0011 等于 1 15 | dat <<= 1; // 数据移位,移除最高位1补0 0100 0110 16 | 17 | SRCLK = 1; // 升沿时数据寄存器的数据移位 18 | _nop_(); 19 | _nop_(); // 延时 20 | SRCLK = 0; // 下降沿移位寄存器数据不变 21 | } 22 | 23 | RCLK = 1; // 上升沿时移位寄存器的数据进入数据存储寄存器 24 | _nop_(); 25 | _nop_(); // 延时 26 | RCLK = 0; // 下降沿时存储寄存器数据不变 27 | } 28 | 29 | ``` 30 | 31 | --- 32 | 33 | ```c 34 | // 汉字 "洪" 字模 35 | // 1 0 0 1 0 1 0 0 36 | // 0 1 1 1 1 1 1 1 37 | // 1 0 0 1 0 1 0 0 38 | // 0 1 0 1 0 1 0 0 39 | // 0 0 1 1 1 1 1 1 40 | // 0 1 0 1 0 1 0 0 41 | // 1 0 1 0 0 0 1 0 42 | // 1 0 1 0 0 0 1 0 43 | // 从左-->右,每一列取二进制换算16进制 44 | 45 | u8 ledduan[] = {0xA3, 0x54, 0x4B, 0xFC, 0x48, 0xFC, 0x4B, 0x48}; // 汉字 洪 字模 46 | u8 ledwei[] = {0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe}; // 逐列点亮不改 47 | ``` 48 | -------------------------------------------------------------------------------- /5-matrix_8x8LED/kc51.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 35 | 36 | -------------------------------------------------------------------------------- /5-matrix_8x8LED/kc51.hex: -------------------------------------------------------------------------------- 1 | :1000DC000808A3544BFC48FC4B4808107FBFDFEFCB 2 | :0400EC00F7FBFDFE23 3 | :0B00F100EF1FAC0670011E4C70F622E1 4 | :1000B600C2B6C2B5E4FEEFC4131313540124FF9273 5 | :1000C600B4EF25E0FFD2B60000C2B60EBE08E7D2F6 6 | :0600D600B50000C2B522D6 7 | :10008F0075807FE4FD74102DF8E6F58074082DF867 8 | :10009F00E6FF1200B67F647E001200F1E4FF12004B 9 | :0700AF00B60DBD08E080D989 10 | :03000000020003F8 11 | :0C000300787FE4F6D8FD75811702004AF2 12 | :10000F0002008FE493A3F8E493A34003F68001F278 13 | :10001F0008DFF48029E493A3F85407240CC8C333F2 14 | :10002F00C4540F4420C8834004F456800146F6DFC1 15 | :10003F00E4800B01020408102040809000DCE47E75 16 | :10004F00019360BCA3FF543F30E509541FFEE493B6 17 | :10005F00A360010ECF54C025E060A840B8E493A37D 18 | :10006F00FAE493A3F8E493A3C8C582C8CAC583CAA8 19 | :10007F00F0A3C8C582C8CAC583CADFE9DEE780BE60 20 | :0100F000000F 21 | :00000001FF 22 | -------------------------------------------------------------------------------- /6-timer/inter_timer.c: -------------------------------------------------------------------------------- 1 | // 中断法使用定时器 2 | 3 | #include 4 | 5 | void Timer0Init(void) //2毫秒@12.000MHz 6 | { 7 | EA = 1 ; // 使能总中断 8 | TMOD &= 0xFC; //设置定时器模式 9 | TMOD |= 0x01; 10 | TH0 = (655536 - 2000) / 256; //定时 2ms 11 | TL0 = (655536 - 2000) % 256; 12 | ET0 = 1; // 打开定时器0 13 | TR0 = 1; //定时器0开始计时 14 | } 15 | 16 | 17 | void inter_timer0() interrupt 1 18 | { 19 | static unsigned int cnt; 20 | TH0 = (655536 - 2000) / 256; 21 | TL0 = (655536 - 2000) % 256; 22 | cnt++; 23 | if (cnt >= 1000) { // 2ms × 1000 = 2秒触发一次 24 | cnt = 0; 25 | P2 = ~P2; 26 | 27 | } 28 | 29 | } 30 | 31 | void main(void) 32 | { 33 | 34 | Timer0Init(); 35 | 36 | while (1) 37 | ; 38 | } 39 | -------------------------------------------------------------------------------- /6-timer/query_timer.c: -------------------------------------------------------------------------------- 1 | // 查询法使用定时器 2 | #include 3 | 4 | typedef unsigned int u16; //对数据类型进行声明定义 5 | typedef unsigned char u8; 6 | 7 | void Timer0Init(void) //20毫秒@12.000MHz 8 | { 9 | TMOD &= 0xFC; //设置定时器模式 10 | TMOD |= 0x01; 11 | TH0 = 0xB1; //设置定时 20ms 12 | TL0 = 0xF4; 13 | TF0 = 0; //清除TF0标志 14 | TR0 = 1; //定时器0开始计时 15 | } 16 | 17 | 18 | void wait20ms(u8 n) 19 | { 20 | while (n) { 21 | if (1 == TF0) { 22 | n--; 23 | TH0 = 0xB1; //设置定时 20ms 24 | TL0 = 0xF4; 25 | TF0 = 0; //清除TF0标志 26 | } 27 | } 28 | } 29 | 30 | void main(void) 31 | { 32 | u8 i; 33 | u8 led = 0x1; 34 | 35 | Timer0Init(); 36 | 37 | while (1) { 38 | // D1 - D8 LED流水灯 39 | for (i = 0; i < 8; i++) { 40 | P2 = ~led; 41 | wait20ms(50); // 等待 20ms × 50 = 1000ms 42 | led = led << 1; 43 | } 44 | led = 0x1; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /7-external_interrupt/external_interrupt.c: -------------------------------------------------------------------------------- 1 | /******* 外部中断0 和 中断1 实验 ******************* 2 | 3 | 实现现象:下载程序后按下K3或k4 按键可以对D1小灯状态取反。 4 | ********************************************************/ 5 | 6 | #include "reg52.h" //此文件中定义了单片机的一些特殊功能寄存器 7 | 8 | typedef unsigned int u16; //对数据类型进行声明定义 9 | typedef unsigned char u8; 10 | 11 | sbit k3 = P3 ^ 2; //定义按键K3 12 | sbit k4 = P3 ^ 3; //定义按键K4 13 | sbit led = P2 ^ 0; //定义P20口是led 14 | #define LEDS P2 // P2 led灯组 15 | 16 | void delay(u16 i) 17 | { 18 | while (i--); 19 | } 20 | 21 | void Int0Init() 22 | { 23 | //设置INT0 24 | IT0 = 1; //跳变沿出发方式(下降沿) 25 | EX0 = 1; //打开INT0的中断允许。 26 | 27 | //设置INT1 28 | IT1 = 1; //跳变沿出发方式(下降沿) 29 | EX1 = 1; //打开INT1的中断允许。 30 | 31 | EA = 1; //打开总中断 32 | } 33 | 34 | void main() 35 | { 36 | Int0Init(); // 设置外部中断0和1 37 | while (1); 38 | } 39 | 40 | //外部中断0的中断函数 41 | void Int0() interrupt 0 42 | { 43 | delay(1000); //延时消抖 44 | if (k3 == 0) { 45 | led = ~led; 46 | } 47 | } 48 | 49 | //外部中断1的中断函数 50 | void Int1() interrupt 2 51 | { 52 | delay(1000); //延时消抖 53 | if (k4 == 0) { 54 | LEDS = ~(~LEDS << 1); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /7-external_interrupt/simple_example.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // K3 独立按键 触发外部中断 INT0 4 | //External interrupt0 service routine //外部中断0服务程序 5 | void exint0() interrupt 0 //(location at 0003H) 6 | { 7 | P2++; // LED组灯在 P2 I/O 端口 8 | } 9 | 10 | void main() 11 | { 12 | IT0 = 1; //set INT0 int type //设置INT0 int类型(1:下降 0:低级别) 13 | EX0 = 1; //enable INT0 interrupt //启用INT0中断 14 | EA = 1; //open global interrupt switch //打开全局中断开关 15 | 16 | while (1); 17 | } -------------------------------------------------------------------------------- /8-lcd1602_display/README.md: -------------------------------------------------------------------------------- 1 | - 开源项目网址 http://git.io/CB51 2 | 3 | ### 8. 初识LCD1602液晶模块 [源码](https://github.com/hongwenjun/stc89c52/tree/master/8-lcd1602_display) 4 | 5 | ![](https://raw.githubusercontent.com/hongwenjun/stc89c52/master/img/lcd1602.jpg) 6 | 7 | ```c 8 | #define LcdDB P0 // D0-D7 为8位双向数据线,传输数据或命令 9 | sbit LcdRS = P2 ^ 6; // RS 为寄存器选择,高电平时选择数据寄存器,低电平时选择指令寄存 10 | sbit LcdRW = P2 ^ 5; // R/W 为读写信号线, 高电平时进行读操作,低电平时进行写操作 11 | sbit LcdEN = P2 ^ 7; // E 为使能端,当高电平跳变成低电平时,液晶模块执行命令 12 | 13 | void Lcd_Init(); // LCD1602初始化函数 14 | void LcdWrDat(uchar dat); // LCD1602写8bit数据函数 15 | void LcdWrCmd(uchar cmd); // LCD1602写入8bit命令函数 16 | void LcdBusy(); // 检测忙信号,等待 17 | void Lcd_Show(uchar x, uchar y); // 显示坐标: x为横坐标; y为竖坐标 y=0; 第一行 y=1; 第二行 18 | 19 | void Lcd_Init() 20 | { 21 | LcdWrCmd(0x38); // 写指令38H: 显示模式设置 22 | LcdWrCmd(0x0C); // 写指令0CH: 开显示,不显示光标 23 | LcdWrCmd(0x06); // 写指令06H: 光标移动设置 24 | LcdWrCmd(0x01); // 写指令01H: 显示清屏 25 | // 写指令08H 显示关闭 26 | } 27 | 28 | void LcdWrDat(uchar dat) 29 | { 30 | LcdBusy(); // 检测忙信号 31 | LcdRS = 1; // 高电平时选择数据寄存器 32 | LcdRW = 0; // 低电平时进行写操作 33 | LcdDB = dat; // 写数据 34 | LcdEN = 1; // 电平下降沿,使能 35 | LcdEN = 0; 36 | } 37 | 38 | ``` -------------------------------------------------------------------------------- /8-lcd1602_display/lcd1602.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 35 | 36 | -------------------------------------------------------------------------------- /9-infrared_sensor/hwx.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 35 | 36 | -------------------------------------------------------------------------------- /999-KeilProject/10-1 DS1302时钟/DS1302.h: -------------------------------------------------------------------------------- 1 | #ifndef __DS1302_H__ 2 | #define __DS1302_H__ 3 | 4 | //外部可调用时间数组,索引0~6分别为年、月、日、时、分、秒、星期 5 | extern unsigned char DS1302_Time[]; 6 | 7 | void DS1302_Init(void); 8 | void DS1302_WriteByte(unsigned char Command,Data); 9 | unsigned char DS1302_ReadByte(unsigned char Command); 10 | void DS1302_SetTime(void); 11 | void DS1302_ReadTime(void); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /999-KeilProject/10-1 DS1302时钟/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/10-1 DS1302时钟/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/10-1 DS1302时钟/LCD1602.h: -------------------------------------------------------------------------------- 1 | #ifndef __LCD1602_H__ 2 | #define __LCD1602_H__ 3 | 4 | //用户调用函数: 5 | void LCD_Init(); 6 | void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char); 7 | void LCD_ShowString(unsigned char Line,unsigned char Column,char *String); 8 | void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 9 | void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length); 10 | void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 11 | void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /999-KeilProject/10-1 DS1302时钟/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "LCD1602.h" 3 | #include "DS1302.h" 4 | 5 | void main() 6 | { 7 | LCD_Init(); 8 | DS1302_Init(); 9 | LCD_ShowString(1,1," - - ");//静态字符初始化显示 10 | LCD_ShowString(2,1," : : "); 11 | 12 | DS1302_SetTime();//设置时间 13 | 14 | while(1) 15 | { 16 | DS1302_ReadTime();//读取时间 17 | LCD_ShowNum(1,1,DS1302_Time[0],2);//显示年 18 | LCD_ShowNum(1,4,DS1302_Time[1],2);//显示月 19 | LCD_ShowNum(1,7,DS1302_Time[2],2);//显示日 20 | LCD_ShowNum(2,1,DS1302_Time[3],2);//显示时 21 | LCD_ShowNum(2,4,DS1302_Time[4],2);//显示分 22 | LCD_ShowNum(2,7,DS1302_Time[5],2);//显示秒 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /999-KeilProject/10-2 DS1302可调时钟/DS1302.h: -------------------------------------------------------------------------------- 1 | #ifndef __DS1302_H__ 2 | #define __DS1302_H__ 3 | 4 | //外部可调用时间数组,索引0~6分别为年、月、日、时、分、秒、星期,设置为有符号的便于<0的判断 5 | extern char DS1302_Time[]; 6 | 7 | void DS1302_Init(void); 8 | void DS1302_WriteByte(unsigned char Command,Data); 9 | unsigned char DS1302_ReadByte(unsigned char Command); 10 | void DS1302_SetTime(void); 11 | void DS1302_ReadTime(void); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /999-KeilProject/10-2 DS1302可调时钟/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/10-2 DS1302可调时钟/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/10-2 DS1302可调时钟/Key.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | 4 | /** 5 | * @brief 获取独立按键键码 6 | * @param 无 7 | * @retval 按下按键的键码,范围:0~4,无按键按下时返回值为0 8 | */ 9 | unsigned char Key() 10 | { 11 | unsigned char KeyNumber=0; 12 | 13 | if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=1;} 14 | if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=2;} 15 | if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=3;} 16 | if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=4;} 17 | 18 | return KeyNumber; 19 | } 20 | -------------------------------------------------------------------------------- /999-KeilProject/10-2 DS1302可调时钟/Key.h: -------------------------------------------------------------------------------- 1 | #ifndef __KEY_H__ 2 | #define __KEY_H__ 3 | 4 | unsigned char Key(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/10-2 DS1302可调时钟/LCD1602.h: -------------------------------------------------------------------------------- 1 | #ifndef __LCD1602_H__ 2 | #define __LCD1602_H__ 3 | 4 | //用户调用函数: 5 | void LCD_Init(); 6 | void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char); 7 | void LCD_ShowString(unsigned char Line,unsigned char Column,char *String); 8 | void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 9 | void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length); 10 | void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 11 | void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /999-KeilProject/10-2 DS1302可调时钟/Timer0.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 定时器0初始化,1毫秒@12.000MHz 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Timer0Init(void) 9 | { 10 | TMOD &= 0xF0; //设置定时器模式 11 | TMOD |= 0x01; //设置定时器模式 12 | TL0 = 0x18; //设置定时初值 13 | TH0 = 0xFC; //设置定时初值 14 | TF0 = 0; //清除TF0标志 15 | TR0 = 1; //定时器0开始计时 16 | ET0=1; 17 | EA=1; 18 | PT0=0; 19 | } 20 | 21 | /*定时器中断函数模板 22 | void Timer0_Routine() interrupt 1 23 | { 24 | static unsigned int T0Count; 25 | TL0 = 0x18; //设置定时初值 26 | TH0 = 0xFC; //设置定时初值 27 | T0Count++; 28 | if(T0Count>=1000) 29 | { 30 | T0Count=0; 31 | 32 | } 33 | } 34 | */ 35 | -------------------------------------------------------------------------------- /999-KeilProject/10-2 DS1302可调时钟/Timer0.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIMER0_H__ 2 | #define __TIMER0_H__ 3 | 4 | void Timer0Init(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/11-1 蜂鸣器播放提示音/Buzzer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | //蜂鸣器端口: 5 | sbit Buzzer=P1^5; 6 | 7 | /** 8 | * @brief 蜂鸣器私有延时函数,延时500us 9 | * @param 无 10 | * @retval 无 11 | */ 12 | void Buzzer_Delay500us() //@12.000MHz 13 | { 14 | unsigned char i; 15 | 16 | _nop_(); 17 | i = 247; 18 | while (--i); 19 | } 20 | 21 | /** 22 | * @brief 蜂鸣器发声 23 | * @param ms 发声的时长,范围:0~32767 24 | * @retval 无 25 | */ 26 | void Buzzer_Time(unsigned int ms) 27 | { 28 | unsigned int i; 29 | for(i=0;i 2 | #include "Delay.h" 3 | 4 | /** 5 | * @brief 获取独立按键键码 6 | * @param 无 7 | * @retval 按下按键的键码,范围:0~4,无按键按下时返回值为0 8 | */ 9 | unsigned char Key() 10 | { 11 | unsigned char KeyNumber=0; 12 | 13 | if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=1;} 14 | if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=2;} 15 | if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=3;} 16 | if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=4;} 17 | 18 | return KeyNumber; 19 | } 20 | -------------------------------------------------------------------------------- /999-KeilProject/11-1 蜂鸣器播放提示音/Key.h: -------------------------------------------------------------------------------- 1 | #ifndef __KEY_H__ 2 | #define __KEY_H__ 3 | 4 | unsigned char Key(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/11-1 蜂鸣器播放提示音/Nixie.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | 4 | //数码管段码表 5 | unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; 6 | 7 | /** 8 | * @brief 数码管显示 9 | * @param Location 要显示的位置,范围:1~8 10 | * @param Number 要显示的数字,范围:段码表索引范围 11 | * @retval 无 12 | */ 13 | void Nixie(unsigned char Location,Number) 14 | { 15 | switch(Location) //位码输出 16 | { 17 | case 1:P2_4=1;P2_3=1;P2_2=1;break; 18 | case 2:P2_4=1;P2_3=1;P2_2=0;break; 19 | case 3:P2_4=1;P2_3=0;P2_2=1;break; 20 | case 4:P2_4=1;P2_3=0;P2_2=0;break; 21 | case 5:P2_4=0;P2_3=1;P2_2=1;break; 22 | case 6:P2_4=0;P2_3=1;P2_2=0;break; 23 | case 7:P2_4=0;P2_3=0;P2_2=1;break; 24 | case 8:P2_4=0;P2_3=0;P2_2=0;break; 25 | } 26 | P0=NixieTable[Number]; //段码输出 27 | // Delay(1); //显示一段时间 28 | // P0=0x00; //段码清0,消影 29 | } 30 | -------------------------------------------------------------------------------- /999-KeilProject/11-1 蜂鸣器播放提示音/Nixie.h: -------------------------------------------------------------------------------- 1 | #ifndef __NIXIE_H__ 2 | #define __NIXIE_H__ 3 | 4 | void Nixie(unsigned char Location,Number); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/11-1 蜂鸣器播放提示音/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | #include "Key.h" 4 | #include "Nixie.h" 5 | #include "Buzzer.h" 6 | 7 | unsigned char KeyNum; 8 | 9 | void main() 10 | { 11 | Nixie(1,0); 12 | while(1) 13 | { 14 | KeyNum=Key(); 15 | if(KeyNum) 16 | { 17 | Buzzer_Time(100); 18 | Nixie(1,KeyNum); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /999-KeilProject/11-2 蜂鸣器播放音乐/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/11-2 蜂鸣器播放音乐/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/11-2 蜂鸣器播放音乐/Timer0.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 定时器0初始化,1毫秒@12.000MHz 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Timer0Init(void) 9 | { 10 | TMOD &= 0xF0; //设置定时器模式 11 | TMOD |= 0x01; //设置定时器模式 12 | TL0 = 0x18; //设置定时初值 13 | TH0 = 0xFC; //设置定时初值 14 | TF0 = 0; //清除TF0标志 15 | TR0 = 1; //定时器0开始计时 16 | ET0=1; 17 | EA=1; 18 | PT0=0; 19 | } 20 | 21 | /*定时器中断函数模板 22 | void Timer0_Routine() interrupt 1 23 | { 24 | static unsigned int T0Count; 25 | TL0 = 0x18; //设置定时初值 26 | TH0 = 0xFC; //设置定时初值 27 | T0Count++; 28 | if(T0Count>=1000) 29 | { 30 | T0Count=0; 31 | 32 | } 33 | } 34 | */ 35 | -------------------------------------------------------------------------------- /999-KeilProject/11-2 蜂鸣器播放音乐/Timer0.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIMER0_H__ 2 | #define __TIMER0_H__ 3 | 4 | void Timer0Init(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/12-1 AT24C02数据存储/AT24C02.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "I2C.h" 3 | 4 | #define AT24C02_ADDRESS 0xA0 5 | 6 | /** 7 | * @brief AT24C02写入一个字节 8 | * @param WordAddress 要写入字节的地址 9 | * @param Data 要写入的数据 10 | * @retval 无 11 | */ 12 | void AT24C02_WriteByte(unsigned char WordAddress,Data) 13 | { 14 | I2C_Start(); 15 | I2C_SendByte(AT24C02_ADDRESS); 16 | I2C_ReceiveAck(); 17 | I2C_SendByte(WordAddress); 18 | I2C_ReceiveAck(); 19 | I2C_SendByte(Data); 20 | I2C_ReceiveAck(); 21 | I2C_Stop(); 22 | } 23 | 24 | /** 25 | * @brief AT24C02读取一个字节 26 | * @param WordAddress 要读出字节的地址 27 | * @retval 读出的数据 28 | */ 29 | unsigned char AT24C02_ReadByte(unsigned char WordAddress) 30 | { 31 | unsigned char Data; 32 | I2C_Start(); 33 | I2C_SendByte(AT24C02_ADDRESS); 34 | I2C_ReceiveAck(); 35 | I2C_SendByte(WordAddress); 36 | I2C_ReceiveAck(); 37 | I2C_Start(); 38 | I2C_SendByte(AT24C02_ADDRESS|0x01); 39 | I2C_ReceiveAck(); 40 | Data=I2C_ReceiveByte(); 41 | I2C_SendAck(1); 42 | I2C_Stop(); 43 | return Data; 44 | } 45 | -------------------------------------------------------------------------------- /999-KeilProject/12-1 AT24C02数据存储/AT24C02.h: -------------------------------------------------------------------------------- 1 | #ifndef __AT24C02_H__ 2 | #define __AT24C02_H__ 3 | 4 | void AT24C02_WriteByte(unsigned char WordAddress,Data); 5 | unsigned char AT24C02_ReadByte(unsigned char WordAddress); 6 | 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /999-KeilProject/12-1 AT24C02数据存储/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/12-1 AT24C02数据存储/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/12-1 AT24C02数据存储/I2C.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | sbit I2C_SCL=P2^1; 4 | sbit I2C_SDA=P2^0; 5 | 6 | /** 7 | * @brief I2C开始 8 | * @param 无 9 | * @retval 无 10 | */ 11 | void I2C_Start(void) 12 | { 13 | I2C_SDA=1; 14 | I2C_SCL=1; 15 | I2C_SDA=0; 16 | I2C_SCL=0; 17 | } 18 | 19 | /** 20 | * @brief I2C停止 21 | * @param 无 22 | * @retval 无 23 | */ 24 | void I2C_Stop(void) 25 | { 26 | I2C_SDA=0; 27 | I2C_SCL=1; 28 | I2C_SDA=1; 29 | } 30 | 31 | /** 32 | * @brief I2C发送一个字节 33 | * @param Byte 要发送的字节 34 | * @retval 无 35 | */ 36 | void I2C_SendByte(unsigned char Byte) 37 | { 38 | unsigned char i; 39 | for(i=0;i<8;i++) 40 | { 41 | I2C_SDA=Byte&(0x80>>i); 42 | I2C_SCL=1; 43 | I2C_SCL=0; 44 | } 45 | } 46 | 47 | /** 48 | * @brief I2C接收一个字节 49 | * @param 无 50 | * @retval 接收到的一个字节数据 51 | */ 52 | unsigned char I2C_ReceiveByte(void) 53 | { 54 | unsigned char i,Byte=0x00; 55 | I2C_SDA=1; 56 | for(i=0;i<8;i++) 57 | { 58 | I2C_SCL=1; 59 | if(I2C_SDA){Byte|=(0x80>>i);} 60 | I2C_SCL=0; 61 | } 62 | return Byte; 63 | } 64 | 65 | /** 66 | * @brief I2C发送应答 67 | * @param AckBit 应答位,0为应答,1为非应答 68 | * @retval 无 69 | */ 70 | void I2C_SendAck(unsigned char AckBit) 71 | { 72 | I2C_SDA=AckBit; 73 | I2C_SCL=1; 74 | I2C_SCL=0; 75 | } 76 | 77 | /** 78 | * @brief I2C接收应答位 79 | * @param 无 80 | * @retval 接收到的应答位,0为应答,1为非应答 81 | */ 82 | unsigned char I2C_ReceiveAck(void) 83 | { 84 | unsigned char AckBit; 85 | I2C_SDA=1; 86 | I2C_SCL=1; 87 | AckBit=I2C_SDA; 88 | I2C_SCL=0; 89 | return AckBit; 90 | } 91 | -------------------------------------------------------------------------------- /999-KeilProject/12-1 AT24C02数据存储/I2C.h: -------------------------------------------------------------------------------- 1 | #ifndef __I2C_H__ 2 | #define __I2C_H__ 3 | 4 | void I2C_Start(void); 5 | void I2C_Stop(void); 6 | void I2C_SendByte(unsigned char Byte); 7 | unsigned char I2C_ReceiveByte(void); 8 | void I2C_SendAck(unsigned char AckBit); 9 | unsigned char I2C_ReceiveAck(void); 10 | 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /999-KeilProject/12-1 AT24C02数据存储/Key.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | 4 | /** 5 | * @brief 获取独立按键键码 6 | * @param 无 7 | * @retval 按下按键的键码,范围:0~4,无按键按下时返回值为0 8 | */ 9 | unsigned char Key() 10 | { 11 | unsigned char KeyNumber=0; 12 | 13 | if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=1;} 14 | if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=2;} 15 | if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=3;} 16 | if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=4;} 17 | 18 | return KeyNumber; 19 | } 20 | -------------------------------------------------------------------------------- /999-KeilProject/12-1 AT24C02数据存储/Key.h: -------------------------------------------------------------------------------- 1 | #ifndef __KEY_H__ 2 | #define __KEY_H__ 3 | 4 | unsigned char Key(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/12-1 AT24C02数据存储/LCD1602.h: -------------------------------------------------------------------------------- 1 | #ifndef __LCD1602_H__ 2 | #define __LCD1602_H__ 3 | 4 | //用户调用函数: 5 | void LCD_Init(); 6 | void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char); 7 | void LCD_ShowString(unsigned char Line,unsigned char Column,char *String); 8 | void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 9 | void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length); 10 | void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 11 | void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /999-KeilProject/12-1 AT24C02数据存储/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "LCD1602.h" 3 | #include "Key.h" 4 | #include "AT24C02.h" 5 | #include "Delay.h" 6 | 7 | unsigned char KeyNum; 8 | unsigned int Num; 9 | 10 | void main() 11 | { 12 | LCD_Init(); 13 | LCD_ShowNum(1,1,Num,5); 14 | while(1) 15 | { 16 | KeyNum=Key(); 17 | if(KeyNum==1) //K1按键,Num自增 18 | { 19 | Num++; 20 | LCD_ShowNum(1,1,Num,5); 21 | } 22 | if(KeyNum==2) //K2按键,Num自减 23 | { 24 | Num--; 25 | LCD_ShowNum(1,1,Num,5); 26 | } 27 | if(KeyNum==3) //K3按键,向AT24C02写入数据 28 | { 29 | AT24C02_WriteByte(0,Num%256); 30 | Delay(5); 31 | AT24C02_WriteByte(1,Num/256); 32 | Delay(5); 33 | LCD_ShowString(2,1,"Write OK"); 34 | Delay(1000); 35 | LCD_ShowString(2,1," "); 36 | } 37 | if(KeyNum==4) //K4按键,从AT24C02读取数据 38 | { 39 | Num=AT24C02_ReadByte(0); 40 | Num|=AT24C02_ReadByte(1)<<8; 41 | LCD_ShowNum(1,1,Num,5); 42 | LCD_ShowString(2,1,"Read OK "); 43 | Delay(1000); 44 | LCD_ShowString(2,1," "); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /999-KeilProject/12-2 秒表(定时器扫描按键数码管)/AT24C02.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "I2C.h" 3 | 4 | #define AT24C02_ADDRESS 0xA0 5 | 6 | /** 7 | * @brief AT24C02写入一个字节 8 | * @param WordAddress 要写入字节的地址 9 | * @param Data 要写入的数据 10 | * @retval 无 11 | */ 12 | void AT24C02_WriteByte(unsigned char WordAddress,Data) 13 | { 14 | I2C_Start(); 15 | I2C_SendByte(AT24C02_ADDRESS); 16 | I2C_ReceiveAck(); 17 | I2C_SendByte(WordAddress); 18 | I2C_ReceiveAck(); 19 | I2C_SendByte(Data); 20 | I2C_ReceiveAck(); 21 | I2C_Stop(); 22 | } 23 | 24 | /** 25 | * @brief AT24C02读取一个字节 26 | * @param WordAddress 要读出字节的地址 27 | * @retval 读出的数据 28 | */ 29 | unsigned char AT24C02_ReadByte(unsigned char WordAddress) 30 | { 31 | unsigned char Data; 32 | I2C_Start(); 33 | I2C_SendByte(AT24C02_ADDRESS); 34 | I2C_ReceiveAck(); 35 | I2C_SendByte(WordAddress); 36 | I2C_ReceiveAck(); 37 | I2C_Start(); 38 | I2C_SendByte(AT24C02_ADDRESS|0x01); 39 | I2C_ReceiveAck(); 40 | Data=I2C_ReceiveByte(); 41 | I2C_SendAck(1); 42 | I2C_Stop(); 43 | return Data; 44 | } 45 | -------------------------------------------------------------------------------- /999-KeilProject/12-2 秒表(定时器扫描按键数码管)/AT24C02.h: -------------------------------------------------------------------------------- 1 | #ifndef __AT24C02_H__ 2 | #define __AT24C02_H__ 3 | 4 | void AT24C02_WriteByte(unsigned char WordAddress,Data); 5 | unsigned char AT24C02_ReadByte(unsigned char WordAddress); 6 | 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /999-KeilProject/12-2 秒表(定时器扫描按键数码管)/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/12-2 秒表(定时器扫描按键数码管)/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/12-2 秒表(定时器扫描按键数码管)/I2C.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | sbit I2C_SCL=P2^1; 4 | sbit I2C_SDA=P2^0; 5 | 6 | /** 7 | * @brief I2C开始 8 | * @param 无 9 | * @retval 无 10 | */ 11 | void I2C_Start(void) 12 | { 13 | I2C_SDA=1; 14 | I2C_SCL=1; 15 | I2C_SDA=0; 16 | I2C_SCL=0; 17 | } 18 | 19 | /** 20 | * @brief I2C停止 21 | * @param 无 22 | * @retval 无 23 | */ 24 | void I2C_Stop(void) 25 | { 26 | I2C_SDA=0; 27 | I2C_SCL=1; 28 | I2C_SDA=1; 29 | } 30 | 31 | /** 32 | * @brief I2C发送一个字节 33 | * @param Byte 要发送的字节 34 | * @retval 无 35 | */ 36 | void I2C_SendByte(unsigned char Byte) 37 | { 38 | unsigned char i; 39 | for(i=0;i<8;i++) 40 | { 41 | I2C_SDA=Byte&(0x80>>i); 42 | I2C_SCL=1; 43 | I2C_SCL=0; 44 | } 45 | } 46 | 47 | /** 48 | * @brief I2C接收一个字节 49 | * @param 无 50 | * @retval 接收到的一个字节数据 51 | */ 52 | unsigned char I2C_ReceiveByte(void) 53 | { 54 | unsigned char i,Byte=0x00; 55 | I2C_SDA=1; 56 | for(i=0;i<8;i++) 57 | { 58 | I2C_SCL=1; 59 | if(I2C_SDA){Byte|=(0x80>>i);} 60 | I2C_SCL=0; 61 | } 62 | return Byte; 63 | } 64 | 65 | /** 66 | * @brief I2C发送应答 67 | * @param AckBit 应答位,0为应答,1为非应答 68 | * @retval 无 69 | */ 70 | void I2C_SendAck(unsigned char AckBit) 71 | { 72 | I2C_SDA=AckBit; 73 | I2C_SCL=1; 74 | I2C_SCL=0; 75 | } 76 | 77 | /** 78 | * @brief I2C接收应答位 79 | * @param 无 80 | * @retval 接收到的应答位,0为应答,1为非应答 81 | */ 82 | unsigned char I2C_ReceiveAck(void) 83 | { 84 | unsigned char AckBit; 85 | I2C_SDA=1; 86 | I2C_SCL=1; 87 | AckBit=I2C_SDA; 88 | I2C_SCL=0; 89 | return AckBit; 90 | } 91 | -------------------------------------------------------------------------------- /999-KeilProject/12-2 秒表(定时器扫描按键数码管)/I2C.h: -------------------------------------------------------------------------------- 1 | #ifndef __I2C_H__ 2 | #define __I2C_H__ 3 | 4 | void I2C_Start(void); 5 | void I2C_Stop(void); 6 | void I2C_SendByte(unsigned char Byte); 7 | unsigned char I2C_ReceiveByte(void); 8 | void I2C_SendAck(unsigned char AckBit); 9 | unsigned char I2C_ReceiveAck(void); 10 | 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /999-KeilProject/12-2 秒表(定时器扫描按键数码管)/Key.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | 4 | unsigned char Key_KeyNumber; 5 | 6 | /** 7 | * @brief 获取按键键码 8 | * @param 无 9 | * @retval 按下按键的键码,范围:0,1~4,0表示无按键按下 10 | */ 11 | unsigned char Key(void) 12 | { 13 | unsigned char Temp=0; 14 | Temp=Key_KeyNumber; 15 | Key_KeyNumber=0; 16 | return Temp; 17 | } 18 | 19 | /** 20 | * @brief 获取当前按键的状态,无消抖及松手检测 21 | * @param 无 22 | * @retval 按下按键的键码,范围:0,1~4,0表示无按键按下 23 | */ 24 | unsigned char Key_GetState() 25 | { 26 | unsigned char KeyNumber=0; 27 | 28 | if(P3_1==0){KeyNumber=1;} 29 | if(P3_0==0){KeyNumber=2;} 30 | if(P3_2==0){KeyNumber=3;} 31 | if(P3_3==0){KeyNumber=4;} 32 | 33 | return KeyNumber; 34 | } 35 | 36 | /** 37 | * @brief 按键驱动函数,在中断中调用 38 | * @param 无 39 | * @retval 无 40 | */ 41 | void Key_Loop(void) 42 | { 43 | static unsigned char NowState,LastState; 44 | LastState=NowState; //按键状态更新 45 | NowState=Key_GetState(); //获取当前按键状态 46 | //如果上个时间点按键按下,这个时间点未按下,则是松手瞬间,以此避免消抖和松手检测 47 | if(LastState==1 && NowState==0) 48 | { 49 | Key_KeyNumber=1; 50 | } 51 | if(LastState==2 && NowState==0) 52 | { 53 | Key_KeyNumber=2; 54 | } 55 | if(LastState==3 && NowState==0) 56 | { 57 | Key_KeyNumber=3; 58 | } 59 | if(LastState==4 && NowState==0) 60 | { 61 | Key_KeyNumber=4; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /999-KeilProject/12-2 秒表(定时器扫描按键数码管)/Key.h: -------------------------------------------------------------------------------- 1 | #ifndef __KEY_H__ 2 | #define __KEY_H__ 3 | 4 | unsigned char Key(void); 5 | void Key_Loop(void); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /999-KeilProject/12-2 秒表(定时器扫描按键数码管)/Nixie.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | 4 | //数码管显示缓存区 5 | unsigned char Nixie_Buf[9]={0,10,10,10,10,10,10,10,10}; 6 | 7 | //数码管段码表 8 | unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x40}; 9 | 10 | /** 11 | * @brief 设置显示缓存区 12 | * @param Location 要设置的位置,范围:1~8 13 | * @param Number 要设置的数字,范围:段码表索引范围 14 | * @retval 无 15 | */ 16 | void Nixie_SetBuf(unsigned char Location,Number) 17 | { 18 | Nixie_Buf[Location]=Number; 19 | } 20 | 21 | /** 22 | * @brief 数码管扫描显示 23 | * @param Location 要显示的位置,范围:1~8 24 | * @param Number 要显示的数字,范围:段码表索引范围 25 | * @retval 无 26 | */ 27 | void Nixie_Scan(unsigned char Location,Number) 28 | { 29 | P0=0x00; //段码清0,消影 30 | switch(Location) //位码输出 31 | { 32 | case 1:P2_4=1;P2_3=1;P2_2=1;break; 33 | case 2:P2_4=1;P2_3=1;P2_2=0;break; 34 | case 3:P2_4=1;P2_3=0;P2_2=1;break; 35 | case 4:P2_4=1;P2_3=0;P2_2=0;break; 36 | case 5:P2_4=0;P2_3=1;P2_2=1;break; 37 | case 6:P2_4=0;P2_3=1;P2_2=0;break; 38 | case 7:P2_4=0;P2_3=0;P2_2=1;break; 39 | case 8:P2_4=0;P2_3=0;P2_2=0;break; 40 | } 41 | P0=NixieTable[Number]; //段码输出 42 | } 43 | 44 | /** 45 | * @brief 数码管驱动函数,在中断中调用 46 | * @param 无 47 | * @retval 无 48 | */ 49 | void Nixie_Loop(void) 50 | { 51 | static unsigned char i=1; 52 | Nixie_Scan(i,Nixie_Buf[i]); 53 | i++; 54 | if(i>=9){i=1;} 55 | } 56 | -------------------------------------------------------------------------------- /999-KeilProject/12-2 秒表(定时器扫描按键数码管)/Nixie.h: -------------------------------------------------------------------------------- 1 | #ifndef __NIXIE_H__ 2 | #define __NIXIE_H__ 3 | 4 | void Nixie_SetBuf(unsigned char Location,Number); 5 | void Nixie_Scan(unsigned char Location,Number); 6 | void Nixie_Loop(void); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /999-KeilProject/12-2 秒表(定时器扫描按键数码管)/Timer0.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 定时器0初始化,1毫秒@12.000MHz 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Timer0_Init(void) 9 | { 10 | TMOD &= 0xF0; //设置定时器模式 11 | TMOD |= 0x01; //设置定时器模式 12 | TL0 = 0x18; //设置定时初值 13 | TH0 = 0xFC; //设置定时初值 14 | TF0 = 0; //清除TF0标志 15 | TR0 = 1; //定时器0开始计时 16 | ET0=1; 17 | EA=1; 18 | PT0=0; 19 | } 20 | 21 | /*定时器中断函数模板 22 | void Timer0_Routine() interrupt 1 23 | { 24 | static unsigned int T0Count; 25 | TL0 = 0x18; //设置定时初值 26 | TH0 = 0xFC; //设置定时初值 27 | T0Count++; 28 | if(T0Count>=1000) 29 | { 30 | T0Count=0; 31 | 32 | } 33 | } 34 | */ 35 | -------------------------------------------------------------------------------- /999-KeilProject/12-2 秒表(定时器扫描按键数码管)/Timer0.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIMER0_H__ 2 | #define __TIMER0_H__ 3 | 4 | void Timer0_Init(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/13-1 DS18B20温度读取/DS18B20.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "OneWire.h" 3 | 4 | //DS18B20指令 5 | #define DS18B20_SKIP_ROM 0xCC 6 | #define DS18B20_CONVERT_T 0x44 7 | #define DS18B20_READ_SCRATCHPAD 0xBE 8 | 9 | /** 10 | * @brief DS18B20开始温度变换 11 | * @param 无 12 | * @retval 无 13 | */ 14 | void DS18B20_ConvertT(void) 15 | { 16 | OneWire_Init(); 17 | OneWire_SendByte(DS18B20_SKIP_ROM); 18 | OneWire_SendByte(DS18B20_CONVERT_T); 19 | } 20 | 21 | /** 22 | * @brief DS18B20读取温度 23 | * @param 无 24 | * @retval 温度数值 25 | */ 26 | float DS18B20_ReadT(void) 27 | { 28 | unsigned char TLSB,TMSB; 29 | int Temp; 30 | float T; 31 | OneWire_Init(); 32 | OneWire_SendByte(DS18B20_SKIP_ROM); 33 | OneWire_SendByte(DS18B20_READ_SCRATCHPAD); 34 | TLSB=OneWire_ReceiveByte(); 35 | TMSB=OneWire_ReceiveByte(); 36 | Temp=(TMSB<<8)|TLSB; 37 | T=Temp/16.0; 38 | return T; 39 | } 40 | -------------------------------------------------------------------------------- /999-KeilProject/13-1 DS18B20温度读取/DS18B20.h: -------------------------------------------------------------------------------- 1 | #ifndef __DS18B20_H__ 2 | #define __DS18B20_H__ 3 | 4 | void DS18B20_ConvertT(void); 5 | float DS18B20_ReadT(void); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /999-KeilProject/13-1 DS18B20温度读取/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/13-1 DS18B20温度读取/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/13-1 DS18B20温度读取/LCD1602.h: -------------------------------------------------------------------------------- 1 | #ifndef __LCD1602_H__ 2 | #define __LCD1602_H__ 3 | 4 | //用户调用函数: 5 | void LCD_Init(); 6 | void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char); 7 | void LCD_ShowString(unsigned char Line,unsigned char Column,char *String); 8 | void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 9 | void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length); 10 | void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 11 | void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /999-KeilProject/13-1 DS18B20温度读取/OneWire.h: -------------------------------------------------------------------------------- 1 | #ifndef __ONEWIRE_H__ 2 | #define __ONEWIRE_H__ 3 | 4 | unsigned char OneWire_Init(void); 5 | void OneWire_SendBit(unsigned char Bit); 6 | unsigned char OneWire_ReceiveBit(void); 7 | void OneWire_SendByte(unsigned char Byte); 8 | unsigned char OneWire_ReceiveByte(void); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /999-KeilProject/13-1 DS18B20温度读取/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "LCD1602.h" 3 | #include "DS18B20.h" 4 | #include "Delay.h" 5 | 6 | float T; 7 | 8 | void main() 9 | { 10 | DS18B20_ConvertT(); //上电先转换一次温度,防止第一次读数据错误 11 | Delay(1000); //等待转换完成 12 | LCD_Init(); 13 | LCD_ShowString(1,1,"Temperature:"); 14 | while(1) 15 | { 16 | DS18B20_ConvertT(); //转换温度 17 | T=DS18B20_ReadT(); //读取温度 18 | if(T<0) //如果温度小于0 19 | { 20 | LCD_ShowChar(2,1,'-'); //显示负号 21 | T=-T; //将温度变为正数 22 | } 23 | else //如果温度大于等于0 24 | { 25 | LCD_ShowChar(2,1,'+'); //显示正号 26 | } 27 | LCD_ShowNum(2,2,T,3); //显示温度整数部分 28 | LCD_ShowChar(2,5,'.'); //显示小数点 29 | LCD_ShowNum(2,6,(unsigned long)(T*10000)%10000,4);//显示温度小数部分 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /999-KeilProject/13-2 DS18B20温度报警器/AT24C02.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "I2C.h" 3 | 4 | #define AT24C02_ADDRESS 0xA0 5 | 6 | /** 7 | * @brief AT24C02写入一个字节 8 | * @param WordAddress 要写入字节的地址 9 | * @param Data 要写入的数据 10 | * @retval 无 11 | */ 12 | void AT24C02_WriteByte(unsigned char WordAddress,Data) 13 | { 14 | I2C_Start(); 15 | I2C_SendByte(AT24C02_ADDRESS); 16 | I2C_ReceiveAck(); 17 | I2C_SendByte(WordAddress); 18 | I2C_ReceiveAck(); 19 | I2C_SendByte(Data); 20 | I2C_ReceiveAck(); 21 | I2C_Stop(); 22 | } 23 | 24 | /** 25 | * @brief AT24C02读取一个字节 26 | * @param WordAddress 要读出字节的地址 27 | * @retval 读出的数据 28 | */ 29 | unsigned char AT24C02_ReadByte(unsigned char WordAddress) 30 | { 31 | unsigned char Data; 32 | I2C_Start(); 33 | I2C_SendByte(AT24C02_ADDRESS); 34 | I2C_ReceiveAck(); 35 | I2C_SendByte(WordAddress); 36 | I2C_ReceiveAck(); 37 | I2C_Start(); 38 | I2C_SendByte(AT24C02_ADDRESS|0x01); 39 | I2C_ReceiveAck(); 40 | Data=I2C_ReceiveByte(); 41 | I2C_SendAck(1); 42 | I2C_Stop(); 43 | return Data; 44 | } 45 | -------------------------------------------------------------------------------- /999-KeilProject/13-2 DS18B20温度报警器/AT24C02.h: -------------------------------------------------------------------------------- 1 | #ifndef __AT24C02_H__ 2 | #define __AT24C02_H__ 3 | 4 | void AT24C02_WriteByte(unsigned char WordAddress,Data); 5 | unsigned char AT24C02_ReadByte(unsigned char WordAddress); 6 | 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /999-KeilProject/13-2 DS18B20温度报警器/DS18B20.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "OneWire.h" 3 | 4 | //DS18B20指令 5 | #define DS18B20_SKIP_ROM 0xCC 6 | #define DS18B20_CONVERT_T 0x44 7 | #define DS18B20_READ_SCRATCHPAD 0xBE 8 | 9 | /** 10 | * @brief DS18B20开始温度变换 11 | * @param 无 12 | * @retval 无 13 | */ 14 | void DS18B20_ConvertT(void) 15 | { 16 | OneWire_Init(); 17 | OneWire_SendByte(DS18B20_SKIP_ROM); 18 | OneWire_SendByte(DS18B20_CONVERT_T); 19 | } 20 | 21 | /** 22 | * @brief DS18B20读取温度 23 | * @param 无 24 | * @retval 温度数值 25 | */ 26 | float DS18B20_ReadT(void) 27 | { 28 | unsigned char TLSB,TMSB; 29 | int Temp; 30 | float T; 31 | OneWire_Init(); 32 | OneWire_SendByte(DS18B20_SKIP_ROM); 33 | OneWire_SendByte(DS18B20_READ_SCRATCHPAD); 34 | TLSB=OneWire_ReceiveByte(); 35 | TMSB=OneWire_ReceiveByte(); 36 | Temp=(TMSB<<8)|TLSB; 37 | T=Temp/16.0; 38 | return T; 39 | } 40 | -------------------------------------------------------------------------------- /999-KeilProject/13-2 DS18B20温度报警器/DS18B20.h: -------------------------------------------------------------------------------- 1 | #ifndef __DS18B20_H__ 2 | #define __DS18B20_H__ 3 | 4 | void DS18B20_ConvertT(void); 5 | float DS18B20_ReadT(void); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /999-KeilProject/13-2 DS18B20温度报警器/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/13-2 DS18B20温度报警器/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/13-2 DS18B20温度报警器/I2C.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //引脚定义 4 | sbit I2C_SCL=P2^1; 5 | sbit I2C_SDA=P2^0; 6 | 7 | /** 8 | * @brief I2C开始 9 | * @param 无 10 | * @retval 无 11 | */ 12 | void I2C_Start(void) 13 | { 14 | I2C_SDA=1; 15 | I2C_SCL=1; 16 | I2C_SDA=0; 17 | I2C_SCL=0; 18 | } 19 | 20 | /** 21 | * @brief I2C停止 22 | * @param 无 23 | * @retval 无 24 | */ 25 | void I2C_Stop(void) 26 | { 27 | I2C_SDA=0; 28 | I2C_SCL=1; 29 | I2C_SDA=1; 30 | } 31 | 32 | /** 33 | * @brief I2C发送一个字节 34 | * @param Byte 要发送的字节 35 | * @retval 无 36 | */ 37 | void I2C_SendByte(unsigned char Byte) 38 | { 39 | unsigned char i; 40 | for(i=0;i<8;i++) 41 | { 42 | I2C_SDA=Byte&(0x80>>i); 43 | I2C_SCL=1; 44 | I2C_SCL=0; 45 | } 46 | } 47 | 48 | /** 49 | * @brief I2C接收一个字节 50 | * @param 无 51 | * @retval 接收到的一个字节数据 52 | */ 53 | unsigned char I2C_ReceiveByte(void) 54 | { 55 | unsigned char i,Byte=0x00; 56 | I2C_SDA=1; 57 | for(i=0;i<8;i++) 58 | { 59 | I2C_SCL=1; 60 | if(I2C_SDA){Byte|=(0x80>>i);} 61 | I2C_SCL=0; 62 | } 63 | return Byte; 64 | } 65 | 66 | /** 67 | * @brief I2C发送应答 68 | * @param AckBit 应答位,0为应答,1为非应答 69 | * @retval 无 70 | */ 71 | void I2C_SendAck(unsigned char AckBit) 72 | { 73 | I2C_SDA=AckBit; 74 | I2C_SCL=1; 75 | I2C_SCL=0; 76 | } 77 | 78 | /** 79 | * @brief I2C接收应答位 80 | * @param 无 81 | * @retval 接收到的应答位,0为应答,1为非应答 82 | */ 83 | unsigned char I2C_ReceiveAck(void) 84 | { 85 | unsigned char AckBit; 86 | I2C_SDA=1; 87 | I2C_SCL=1; 88 | AckBit=I2C_SDA; 89 | I2C_SCL=0; 90 | return AckBit; 91 | } 92 | -------------------------------------------------------------------------------- /999-KeilProject/13-2 DS18B20温度报警器/I2C.h: -------------------------------------------------------------------------------- 1 | #ifndef __I2C_H__ 2 | #define __I2C_H__ 3 | 4 | void I2C_Start(void); 5 | void I2C_Stop(void); 6 | void I2C_SendByte(unsigned char Byte); 7 | unsigned char I2C_ReceiveByte(void); 8 | void I2C_SendAck(unsigned char AckBit); 9 | unsigned char I2C_ReceiveAck(void); 10 | 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /999-KeilProject/13-2 DS18B20温度报警器/Key.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | 4 | unsigned char Key_KeyNumber; 5 | 6 | /** 7 | * @brief 获取按键键码 8 | * @param 无 9 | * @retval 按下按键的键码,范围:0,1~4,0表示无按键按下 10 | */ 11 | unsigned char Key(void) 12 | { 13 | unsigned char Temp=0; 14 | Temp=Key_KeyNumber; 15 | Key_KeyNumber=0; 16 | return Temp; 17 | } 18 | 19 | /** 20 | * @brief 获取当前按键的状态,无消抖及松手检测 21 | * @param 无 22 | * @retval 按下按键的键码,范围:0,1~4,0表示无按键按下 23 | */ 24 | unsigned char Key_GetState() 25 | { 26 | unsigned char KeyNumber=0; 27 | 28 | if(P3_1==0){KeyNumber=1;} 29 | if(P3_0==0){KeyNumber=2;} 30 | if(P3_2==0){KeyNumber=3;} 31 | if(P3_3==0){KeyNumber=4;} 32 | 33 | return KeyNumber; 34 | } 35 | 36 | /** 37 | * @brief 按键驱动函数,在中断中调用 38 | * @param 无 39 | * @retval 无 40 | */ 41 | void Key_Loop(void) 42 | { 43 | static unsigned char NowState,LastState; 44 | LastState=NowState; //按键状态更新 45 | NowState=Key_GetState(); //获取当前按键状态 46 | //如果上个时间点按键按下,这个时间点未按下,则是松手瞬间,以此避免消抖和松手检测 47 | if(LastState==1 && NowState==0) 48 | { 49 | Key_KeyNumber=1; 50 | } 51 | if(LastState==2 && NowState==0) 52 | { 53 | Key_KeyNumber=2; 54 | } 55 | if(LastState==3 && NowState==0) 56 | { 57 | Key_KeyNumber=3; 58 | } 59 | if(LastState==4 && NowState==0) 60 | { 61 | Key_KeyNumber=4; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /999-KeilProject/13-2 DS18B20温度报警器/Key.h: -------------------------------------------------------------------------------- 1 | #ifndef __KEY_H__ 2 | #define __KEY_H__ 3 | 4 | unsigned char Key(void); 5 | void Key_Loop(void); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /999-KeilProject/13-2 DS18B20温度报警器/LCD1602.h: -------------------------------------------------------------------------------- 1 | #ifndef __LCD1602_H__ 2 | #define __LCD1602_H__ 3 | 4 | //用户调用函数: 5 | void LCD_Init(); 6 | void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char); 7 | void LCD_ShowString(unsigned char Line,unsigned char Column,char *String); 8 | void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 9 | void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length); 10 | void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 11 | void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /999-KeilProject/13-2 DS18B20温度报警器/OneWire.h: -------------------------------------------------------------------------------- 1 | #ifndef __ONEWIRE_H__ 2 | #define __ONEWIRE_H__ 3 | 4 | unsigned char OneWire_Init(void); 5 | void OneWire_SendBit(unsigned char Bit); 6 | unsigned char OneWire_ReceiveBit(void); 7 | void OneWire_SendByte(unsigned char Byte); 8 | unsigned char OneWire_ReceiveByte(void); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /999-KeilProject/13-2 DS18B20温度报警器/Timer0.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 定时器0初始化,1毫秒@12.000MHz 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Timer0_Init(void) 9 | { 10 | TMOD &= 0xF0; //设置定时器模式 11 | TMOD |= 0x01; //设置定时器模式 12 | TL0 = 0x18; //设置定时初值 13 | TH0 = 0xFC; //设置定时初值 14 | TF0 = 0; //清除TF0标志 15 | TR0 = 1; //定时器0开始计时 16 | ET0=1; 17 | EA=1; 18 | PT0=0; 19 | } 20 | 21 | /*定时器中断函数模板 22 | void Timer0_Routine() interrupt 1 23 | { 24 | static unsigned int T0Count; 25 | TL0 = 0x18; //设置定时初值 26 | TH0 = 0xFC; //设置定时初值 27 | T0Count++; 28 | if(T0Count>=1000) 29 | { 30 | T0Count=0; 31 | 32 | } 33 | } 34 | */ 35 | -------------------------------------------------------------------------------- /999-KeilProject/13-2 DS18B20温度报警器/Timer0.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIMER0_H__ 2 | #define __TIMER0_H__ 3 | 4 | void Timer0_Init(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/14-1 LCD1602液晶显示屏/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/14-1 LCD1602液晶显示屏/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/14-1 LCD1602液晶显示屏/LCD1602.h: -------------------------------------------------------------------------------- 1 | #ifndef __LCD1602_H__ 2 | #define __LCD1602_H__ 3 | 4 | void LCD_Init(void); 5 | void LCD_ShowChar(unsigned char Line,unsigned char Column,unsigned char Char); 6 | void LCD_ShowString(unsigned char Line,unsigned char Column,unsigned char *String); 7 | void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 8 | void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length); 9 | void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 10 | void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /999-KeilProject/14-1 LCD1602液晶显示屏/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "LCD1602.h" 3 | #include "Delay.h" 4 | 5 | void main() 6 | { 7 | LCD_Init(); //LCD初始化 8 | LCD_ShowChar(1,1,'A'); //在1行1列显示字符A 9 | LCD_ShowString(1,3,"Hello"); //在1行3列显示字符串Hello 10 | LCD_ShowNum(1,9,66,2); //在1行9列显示数字66,长度为2 11 | LCD_ShowSignedNum(1,12,-88,2); //在1行12列显示有符号数字-88,长度为2 12 | LCD_ShowHexNum(2,1,0xA5,2); //在2行1列显示十六进制数字0xA5,长度为2 13 | LCD_ShowBinNum(2,4,0xA5,8); //在2行4列显示二进制数字0xA5,长度为8 14 | LCD_ShowChar(2,13,0xDF); //在2行13列显示编码为0xDF的字符 15 | LCD_ShowChar(2,14,'C'); //在2行14列显示字符C 16 | while(1) 17 | { 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /999-KeilProject/15-1 LED呼吸灯/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | sbit LED=P2^0; 4 | 5 | void Delay(unsigned int t) 6 | { 7 | while(t--); 8 | } 9 | 10 | void main() 11 | { 12 | unsigned char Time,i; 13 | while(1) 14 | { 15 | for(Time=0;Time<100;Time++) //改变亮灭时间,由暗到亮 16 | { 17 | for(i=0;i<20;i++) //计次延时 18 | { 19 | LED=0; //LED亮 20 | Delay(Time); //延时Time 21 | LED=1; //LED灭 22 | Delay(100-Time); //延时100-Time 23 | } 24 | } 25 | for(Time=100;Time>0;Time--) //改变亮灭时间,由亮到暗 26 | { 27 | for(i=0;i<20;i++) //计次延时 28 | { 29 | LED=0; //LED亮 30 | Delay(Time); //延时Time 31 | LED=1; //LED灭 32 | Delay(100-Time); //延时100-Time 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /999-KeilProject/15-2 直流电机调速/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/15-2 直流电机调速/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/15-2 直流电机调速/Key.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | 4 | /** 5 | * @brief 获取独立按键键码 6 | * @param 无 7 | * @retval 按下按键的键码,范围:0~4,无按键按下时返回值为0 8 | */ 9 | unsigned char Key() 10 | { 11 | unsigned char KeyNumber=0; 12 | 13 | if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=1;} 14 | if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=2;} 15 | if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=3;} 16 | if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=4;} 17 | 18 | return KeyNumber; 19 | } 20 | -------------------------------------------------------------------------------- /999-KeilProject/15-2 直流电机调速/Key.h: -------------------------------------------------------------------------------- 1 | #ifndef __KEY_H__ 2 | #define __KEY_H__ 3 | 4 | unsigned char Key(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/15-2 直流电机调速/Nixie.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | 4 | //数码管段码表 5 | unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; 6 | 7 | /** 8 | * @brief 数码管显示 9 | * @param Location 要显示的位置,范围:1~8 10 | * @param Number 要显示的数字,范围:段码表索引范围 11 | * @retval 无 12 | */ 13 | void Nixie(unsigned char Location,Number) 14 | { 15 | switch(Location) //位码输出 16 | { 17 | case 1:P2_4=1;P2_3=1;P2_2=1;break; 18 | case 2:P2_4=1;P2_3=1;P2_2=0;break; 19 | case 3:P2_4=1;P2_3=0;P2_2=1;break; 20 | case 4:P2_4=1;P2_3=0;P2_2=0;break; 21 | case 5:P2_4=0;P2_3=1;P2_2=1;break; 22 | case 6:P2_4=0;P2_3=1;P2_2=0;break; 23 | case 7:P2_4=0;P2_3=0;P2_2=1;break; 24 | case 8:P2_4=0;P2_3=0;P2_2=0;break; 25 | } 26 | P0=NixieTable[Number]; //段码输出 27 | Delay(1); //显示一段时间 28 | P0=0x00; //段码清0,消影 29 | } 30 | -------------------------------------------------------------------------------- /999-KeilProject/15-2 直流电机调速/Nixie.h: -------------------------------------------------------------------------------- 1 | #ifndef __NIXIE_H__ 2 | #define __NIXIE_H__ 3 | 4 | void Nixie(unsigned char Location,Number); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/15-2 直流电机调速/Timer0.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 定时器0初始化,100us@12.000MHz 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Timer0_Init(void) 9 | { 10 | TMOD &= 0xF0; //设置定时器模式 11 | TMOD |= 0x01; //设置定时器模式 12 | TL0 = 0x9C; //设置定时初值 13 | TH0 = 0xFF; //设置定时初值 14 | TF0 = 0; //清除TF0标志 15 | TR0 = 1; //定时器0开始计时 16 | ET0=1; 17 | EA=1; 18 | PT0=0; 19 | } 20 | 21 | /*定时器中断函数模板 22 | void Timer0_Routine() interrupt 1 23 | { 24 | static unsigned int T0Count; 25 | TL0 = 0x9C; //设置定时初值 26 | TH0 = 0xFF; //设置定时初值 27 | T0Count++; 28 | if(T0Count>=1000) 29 | { 30 | T0Count=0; 31 | 32 | } 33 | } 34 | */ 35 | -------------------------------------------------------------------------------- /999-KeilProject/15-2 直流电机调速/Timer0.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIMER0_H__ 2 | #define __TIMER0_H__ 3 | 4 | void Timer0_Init(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/15-2 直流电机调速/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | #include "Key.h" 4 | #include "Nixie.h" 5 | #include "Timer0.h" 6 | 7 | sbit Motor=P1^0; 8 | 9 | unsigned char Counter,Compare; //计数值和比较值,用于输出PWM 10 | unsigned char KeyNum,Speed; 11 | 12 | void main() 13 | { 14 | Timer0_Init(); 15 | while(1) 16 | { 17 | KeyNum=Key(); 18 | if(KeyNum==1) 19 | { 20 | Speed++; 21 | Speed%=4; 22 | if(Speed==0){Compare=0;} //设置比较值,改变PWM占空比 23 | if(Speed==1){Compare=50;} 24 | if(Speed==2){Compare=75;} 25 | if(Speed==3){Compare=100;} 26 | } 27 | Nixie(1,Speed); 28 | } 29 | } 30 | 31 | void Timer0_Routine() interrupt 1 32 | { 33 | TL0 = 0x9C; //设置定时初值 34 | TH0 = 0xFF; //设置定时初值 35 | Counter++; 36 | Counter%=100; //计数值变化范围限制在0~99 37 | if(Counter 2 | #include "Delay.h" 3 | #include "Key.h" 4 | #include "Nixie.h" 5 | #include "Timer0.h" 6 | 7 | sbit Motor=P1^0; 8 | 9 | unsigned char Counter,Compare; //计数值和比较值,用于输出PWM 10 | unsigned char KeyNum,Speed; 11 | 12 | void main() 13 | { 14 | Timer0_Init(); 15 | while(1) 16 | { 17 | KeyNum=Key(); 18 | if(KeyNum==1) 19 | { 20 | Speed++; 21 | Speed%=4; 22 | if(Speed==0){Compare=0;} //设置比较值,改变PWM占空比 23 | if(Speed==1){Compare=50;} 24 | if(Speed==2){Compare=75;} 25 | if(Speed==3){Compare=100;} 26 | } 27 | Nixie(1,Speed); 28 | } 29 | } 30 | 31 | void Timer0_Routine() interrupt 1 32 | { 33 | TL0 = 0x9C; //设置定时初值 34 | TH0 = 0xFF; //设置定时初值 35 | Counter++; 36 | Counter%=100; //计数器变化范围限制在0~99 37 | if(Counter 2 | #include 3 | 4 | //引脚定义 5 | sbit XPY2046_DIN=P3^4; 6 | sbit XPY2046_CS=P3^5; 7 | sbit XPY2046_DCLK=P3^6; 8 | sbit XPY2046_DOUT=P3^7; 9 | 10 | /** 11 | * @brief ZPT2046读取AD值 12 | * @param Command 命令字,范围:头文件内定义的宏,结尾的数字表示转换的位数 13 | * @retval AD转换后的数字量,范围:8位为0~255,12位为0~4095 14 | */ 15 | unsigned int XPT2046_ReadAD(unsigned char Command) 16 | { 17 | unsigned char i; 18 | unsigned int Data=0; 19 | XPY2046_DCLK=0; 20 | XPY2046_CS=0; 21 | for(i=0;i<8;i++) 22 | { 23 | XPY2046_DIN=Command&(0x80>>i); 24 | XPY2046_DCLK=1; 25 | XPY2046_DCLK=0; 26 | } 27 | for(i=0;i<16;i++) 28 | { 29 | XPY2046_DCLK=1; 30 | XPY2046_DCLK=0; 31 | if(XPY2046_DOUT){Data|=(0x8000>>i);} 32 | } 33 | XPY2046_CS=1; 34 | return Data>>8; 35 | } 36 | -------------------------------------------------------------------------------- /999-KeilProject/16-1 AD模数转换/XPT2046.h: -------------------------------------------------------------------------------- 1 | #ifndef __XPT2046_H__ 2 | #define __XPT2046_H__ 3 | 4 | #define XPT2046_VBAT 0xAC 5 | #define XPT2046_AUX 0xEC 6 | #define XPT2046_XP 0x9C //0xBC 7 | #define XPT2046_YP 0xDC 8 | 9 | unsigned int XPT2046_ReadAD(unsigned char Command); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /999-KeilProject/16-1 AD模数转换/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | #include "LCD1602.h" 4 | #include "XPT2046.h" 5 | 6 | unsigned int ADValue; 7 | 8 | void main(void) 9 | { 10 | LCD_Init(); 11 | LCD_ShowString(1,1,"ADJ NTC GR"); 12 | while(1) 13 | { 14 | ADValue=XPT2046_ReadAD(XPT2046_XP); //读取AIN0,可调电阻 15 | LCD_ShowNum(2,1,ADValue,3); //显示AIN0 16 | ADValue=XPT2046_ReadAD(XPT2046_YP); //读取AIN1,热敏电阻 17 | LCD_ShowNum(2,6,ADValue,3); //显示AIN1 18 | ADValue=XPT2046_ReadAD(XPT2046_VBAT); //读取AIN2,光敏电阻 19 | LCD_ShowNum(2,11,ADValue,3); //显示AIN2 20 | Delay(100); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /999-KeilProject/16-2 DA数模转换/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/16-2 DA数模转换/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/16-2 DA数模转换/Timer0.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 定时器0初始化,100us@12.000MHz 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Timer0_Init(void) 9 | { 10 | TMOD &= 0xF0; //设置定时器模式 11 | TMOD |= 0x01; //设置定时器模式 12 | TL0 = 0x9C; //设置定时初值 13 | TH0 = 0xFF; //设置定时初值 14 | TF0 = 0; //清除TF0标志 15 | TR0 = 1; //定时器0开始计时 16 | ET0=1; 17 | EA=1; 18 | PT0=0; 19 | } 20 | 21 | /*定时器中断函数模板 22 | void Timer0_Routine() interrupt 1 23 | { 24 | static unsigned int T0Count; 25 | TL0 = 0x9C; //设置定时初值 26 | TH0 = 0xFF; //设置定时初值 27 | T0Count++; 28 | if(T0Count>=1000) 29 | { 30 | T0Count=0; 31 | 32 | } 33 | } 34 | */ 35 | -------------------------------------------------------------------------------- /999-KeilProject/16-2 DA数模转换/Timer0.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIMER0_H__ 2 | #define __TIMER0_H__ 3 | 4 | void Timer0_Init(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/16-2 DA数模转换/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | #include "Timer0.h" 4 | 5 | sbit DA=P2^1; 6 | 7 | unsigned char Counter,Compare; //计数值和比较值,用于输出PWM 8 | unsigned char i; 9 | 10 | void main() 11 | { 12 | Timer0_Init(); 13 | while(1) 14 | { 15 | for(i=0;i<100;i++) 16 | { 17 | Compare=i; //设置比较值,改变PWM占空比 18 | Delay(10); 19 | } 20 | for(i=100;i>0;i--) 21 | { 22 | Compare=i; //设置比较值,改变PWM占空比 23 | Delay(10); 24 | } 25 | } 26 | } 27 | 28 | void Timer0_Routine() interrupt 1 29 | { 30 | TL0 = 0x9C; //设置定时初值 31 | TH0 = 0xFF; //设置定时初值 32 | Counter++; 33 | Counter%=100; //计数值变化范围限制在0~99 34 | if(Counter 2 | 3 | /** 4 | * @brief 外部中断0初始化 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Int0_Init(void) 9 | { 10 | IT0=1; 11 | IE0=0; 12 | EX0=1; 13 | EA=1; 14 | PX0=1; 15 | } 16 | 17 | /*外部中断0中断函数模板 18 | void Int0_Routine(void) interrupt 0 19 | { 20 | 21 | } 22 | */ 23 | -------------------------------------------------------------------------------- /999-KeilProject/17-1 红外遥控(11.0592MHz晶振)/Int0.h: -------------------------------------------------------------------------------- 1 | #ifndef __INT0_H__ 2 | #define __INT0_H__ 3 | 4 | void Int0_Init(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/17-1 红外遥控(11.0592MHz晶振)/LCD1602.h: -------------------------------------------------------------------------------- 1 | #ifndef __LCD1602_H__ 2 | #define __LCD1602_H__ 3 | 4 | //用户调用函数: 5 | void LCD_Init(); 6 | void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char); 7 | void LCD_ShowString(unsigned char Line,unsigned char Column,char *String); 8 | void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 9 | void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length); 10 | void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 11 | void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /999-KeilProject/17-1 红外遥控(11.0592MHz晶振)/Timer0.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 定时器0初始化 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Timer0_Init(void) 9 | { 10 | TMOD &= 0xF0; //设置定时器模式 11 | TMOD |= 0x01; //设置定时器模式 12 | TL0 = 0; //设置定时初值 13 | TH0 = 0; //设置定时初值 14 | TF0 = 0; //清除TF0标志 15 | TR0 = 0; //定时器0不计时 16 | } 17 | 18 | /** 19 | * @brief 定时器0设置计数器值 20 | * @param Value,要设置的计数器值,范围:0~65535 21 | * @retval 无 22 | */ 23 | void Timer0_SetCounter(unsigned int Value) 24 | { 25 | TH0=Value/256; 26 | TL0=Value%256; 27 | } 28 | 29 | /** 30 | * @brief 定时器0获取计数器值 31 | * @param 无 32 | * @retval 计数器值,范围:0~65535 33 | */ 34 | unsigned int Timer0_GetCounter(void) 35 | { 36 | return (TH0<<8)|TL0; 37 | } 38 | 39 | /** 40 | * @brief 定时器0启动停止控制 41 | * @param Flag 启动停止标志,1为启动,0为停止 42 | * @retval 无 43 | */ 44 | void Timer0_Run(unsigned char Flag) 45 | { 46 | TR0=Flag; 47 | } 48 | -------------------------------------------------------------------------------- /999-KeilProject/17-1 红外遥控(11.0592MHz晶振)/Timer0.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIMER0_H__ 2 | #define __TIMER0_H__ 3 | 4 | void Timer0_Init(void); 5 | void Timer0_SetCounter(unsigned int Value); 6 | unsigned int Timer0_GetCounter(void); 7 | void Timer0_Run(unsigned char Flag); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /999-KeilProject/17-1 红外遥控(11.0592MHz晶振)/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | #include "LCD1602.h" 4 | #include "IR.h" 5 | 6 | unsigned char Num; 7 | unsigned char Address; 8 | unsigned char Command; 9 | 10 | void main() 11 | { 12 | LCD_Init(); 13 | LCD_ShowString(1,1,"ADDR CMD NUM"); 14 | LCD_ShowString(2,1,"00 00 000"); 15 | 16 | IR_Init(); 17 | 18 | while(1) 19 | { 20 | if(IR_GetDataFlag() || IR_GetRepeatFlag()) //如果收到数据帧或者收到连发帧 21 | { 22 | Address=IR_GetAddress(); //获取遥控器地址码 23 | Command=IR_GetCommand(); //获取遥控器命令码 24 | 25 | LCD_ShowHexNum(2,1,Address,2); //显示遥控器地址码 26 | LCD_ShowHexNum(2,7,Command,2); //显示遥控器命令码 27 | 28 | if(Command==IR_VOL_MINUS) //如果遥控器VOL-按键按下 29 | { 30 | Num--; //Num自减 31 | } 32 | if(Command==IR_VOL_ADD) //如果遥控器VOL+按键按下 33 | { 34 | Num++; //Num自增 35 | } 36 | 37 | LCD_ShowNum(2,12,Num,3); //显示Num 38 | } 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /999-KeilProject/17-1 红外遥控/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/17-1 红外遥控/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/17-1 红外遥控/IR.h: -------------------------------------------------------------------------------- 1 | #ifndef __IR_H__ 2 | #define __IR_H__ 3 | 4 | #define IR_POWER 0x45 5 | #define IR_MODE 0x46 6 | #define IR_MUTE 0x47 7 | #define IR_START_STOP 0x44 8 | #define IR_PREVIOUS 0x40 9 | #define IR_NEXT 0x43 10 | #define IR_EQ 0x07 11 | #define IR_VOL_MINUS 0x15 12 | #define IR_VOL_ADD 0x09 13 | #define IR_0 0x16 14 | #define IR_RPT 0x19 15 | #define IR_USD 0x0D 16 | #define IR_1 0x0C 17 | #define IR_2 0x18 18 | #define IR_3 0x5E 19 | #define IR_4 0x08 20 | #define IR_5 0x1C 21 | #define IR_6 0x5A 22 | #define IR_7 0x42 23 | #define IR_8 0x52 24 | #define IR_9 0x4A 25 | 26 | void IR_Init(void); 27 | unsigned char IR_GetDataFlag(void); 28 | unsigned char IR_GetRepeatFlag(void); 29 | unsigned char IR_GetAddress(void); 30 | unsigned char IR_GetCommand(void); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /999-KeilProject/17-1 红外遥控/Int0.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 外部中断0初始化 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Int0_Init(void) 9 | { 10 | IT0=1; 11 | IE0=0; 12 | EX0=1; 13 | EA=1; 14 | PX0=1; 15 | } 16 | 17 | /*外部中断0中断函数模板 18 | void Int0_Routine(void) interrupt 0 19 | { 20 | 21 | } 22 | */ 23 | -------------------------------------------------------------------------------- /999-KeilProject/17-1 红外遥控/Int0.h: -------------------------------------------------------------------------------- 1 | #ifndef __INT0_H__ 2 | #define __INT0_H__ 3 | 4 | void Int0_Init(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/17-1 红外遥控/LCD1602.h: -------------------------------------------------------------------------------- 1 | #ifndef __LCD1602_H__ 2 | #define __LCD1602_H__ 3 | 4 | //用户调用函数: 5 | void LCD_Init(); 6 | void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char); 7 | void LCD_ShowString(unsigned char Line,unsigned char Column,char *String); 8 | void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 9 | void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length); 10 | void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 11 | void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /999-KeilProject/17-1 红外遥控/Timer0.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 定时器0初始化 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Timer0_Init(void) 9 | { 10 | TMOD &= 0xF0; //设置定时器模式 11 | TMOD |= 0x01; //设置定时器模式 12 | TL0 = 0; //设置定时初值 13 | TH0 = 0; //设置定时初值 14 | TF0 = 0; //清除TF0标志 15 | TR0 = 0; //定时器0不计时 16 | } 17 | 18 | /** 19 | * @brief 定时器0设置计数器值 20 | * @param Value,要设置的计数器值,范围:0~65535 21 | * @retval 无 22 | */ 23 | void Timer0_SetCounter(unsigned int Value) 24 | { 25 | TH0=Value/256; 26 | TL0=Value%256; 27 | } 28 | 29 | /** 30 | * @brief 定时器0获取计数器值 31 | * @param 无 32 | * @retval 计数器值,范围:0~65535 33 | */ 34 | unsigned int Timer0_GetCounter(void) 35 | { 36 | return (TH0<<8)|TL0; 37 | } 38 | 39 | /** 40 | * @brief 定时器0启动停止控制 41 | * @param Flag 启动停止标志,1为启动,0为停止 42 | * @retval 无 43 | */ 44 | void Timer0_Run(unsigned char Flag) 45 | { 46 | TR0=Flag; 47 | } 48 | -------------------------------------------------------------------------------- /999-KeilProject/17-1 红外遥控/Timer0.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIMER0_H__ 2 | #define __TIMER0_H__ 3 | 4 | void Timer0_Init(void); 5 | void Timer0_SetCounter(unsigned int Value); 6 | unsigned int Timer0_GetCounter(void); 7 | void Timer0_Run(unsigned char Flag); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /999-KeilProject/17-1 红外遥控/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | #include "LCD1602.h" 4 | #include "IR.h" 5 | 6 | unsigned char Num; 7 | unsigned char Address; 8 | unsigned char Command; 9 | 10 | void main() 11 | { 12 | LCD_Init(); 13 | LCD_ShowString(1,1,"ADDR CMD NUM"); 14 | LCD_ShowString(2,1,"00 00 000"); 15 | 16 | IR_Init(); 17 | 18 | while(1) 19 | { 20 | if(IR_GetDataFlag() || IR_GetRepeatFlag()) //如果收到数据帧或者收到连发帧 21 | { 22 | Address=IR_GetAddress(); //获取遥控器地址码 23 | Command=IR_GetCommand(); //获取遥控器命令码 24 | 25 | LCD_ShowHexNum(2,1,Address,2); //显示遥控器地址码 26 | LCD_ShowHexNum(2,7,Command,2); //显示遥控器命令码 27 | 28 | if(Command==IR_VOL_MINUS) //如果遥控器VOL-按键按下 29 | { 30 | Num--; //Num自减 31 | } 32 | if(Command==IR_VOL_ADD) //如果遥控器VOL+按键按下 33 | { 34 | Num++; //Num自增 35 | } 36 | 37 | LCD_ShowNum(2,12,Num,3); //显示Num 38 | } 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速(11.0592MHz晶振)/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速(11.0592MHz晶振)/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速(11.0592MHz晶振)/IR.h: -------------------------------------------------------------------------------- 1 | #ifndef __IR_H__ 2 | #define __IR_H__ 3 | 4 | #define IR_POWER 0x45 5 | #define IR_MODE 0x46 6 | #define IR_MUTE 0x47 7 | #define IR_START_STOP 0x44 8 | #define IR_PREVIOUS 0x40 9 | #define IR_NEXT 0x43 10 | #define IR_EQ 0x07 11 | #define IR_VOL_MINUS 0x15 12 | #define IR_VOL_ADD 0x09 13 | #define IR_0 0x16 14 | #define IR_RPT 0x19 15 | #define IR_USD 0x0D 16 | #define IR_1 0x0C 17 | #define IR_2 0x18 18 | #define IR_3 0x5E 19 | #define IR_4 0x08 20 | #define IR_5 0x1C 21 | #define IR_6 0x5A 22 | #define IR_7 0x42 23 | #define IR_8 0x52 24 | #define IR_9 0x4A 25 | 26 | void IR_Init(void); 27 | unsigned char IR_GetDataFlag(void); 28 | unsigned char IR_GetRepeatFlag(void); 29 | unsigned char IR_GetAddress(void); 30 | unsigned char IR_GetCommand(void); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速(11.0592MHz晶振)/Int0.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 外部中断0初始化 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Int0_Init(void) 9 | { 10 | IT0=1; 11 | IE0=0; 12 | EX0=1; 13 | EA=1; 14 | PX0=1; 15 | } 16 | 17 | /*外部中断0中断函数模板 18 | void Int0_Routine(void) interrupt 0 19 | { 20 | 21 | } 22 | */ 23 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速(11.0592MHz晶振)/Int0.h: -------------------------------------------------------------------------------- 1 | #ifndef __INT0_H__ 2 | #define __INT0_H__ 3 | 4 | void Int0_Init(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速(11.0592MHz晶振)/Key.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | 4 | /** 5 | * @brief 获取独立按键键码 6 | * @param 无 7 | * @retval 按下按键的键码,范围:0~4,无按键按下时返回值为0 8 | */ 9 | unsigned char Key() 10 | { 11 | unsigned char KeyNumber=0; 12 | 13 | if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=1;} 14 | if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=2;} 15 | if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=3;} 16 | if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=4;} 17 | 18 | return KeyNumber; 19 | } 20 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速(11.0592MHz晶振)/Key.h: -------------------------------------------------------------------------------- 1 | #ifndef __KEY_H__ 2 | #define __KEY_H__ 3 | 4 | unsigned char Key(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速(11.0592MHz晶振)/Motor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Timer1.h" 3 | 4 | //引脚定义 5 | sbit Motor=P1^0; 6 | 7 | unsigned char Counter,Compare; 8 | 9 | /** 10 | * @brief 电机初始化 11 | * @param 无 12 | * @retval 无 13 | */ 14 | void Motor_Init(void) 15 | { 16 | Timer1_Init(); 17 | } 18 | 19 | /** 20 | * @brief 电机设置速度 21 | * @param Speed 要设置的速度,范围0~100 22 | * @retval 无 23 | */ 24 | void Motor_SetSpeed(unsigned char Speed) 25 | { 26 | Compare=Speed; 27 | } 28 | 29 | //定时器1中断函数 30 | void Timer1_Routine() interrupt 3 31 | { 32 | TL1 = 0x9C; //设置定时初值 33 | TH1 = 0xFF; //设置定时初值 34 | Counter++; 35 | Counter%=100; //计数值变化范围限制在0~99 36 | if(Counter 2 | #include "Delay.h" 3 | 4 | unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; 5 | 6 | void Nixie(unsigned char Location,Number) 7 | { 8 | switch(Location) 9 | { 10 | case 1:P2_4=1;P2_3=1;P2_2=1;break; 11 | case 2:P2_4=1;P2_3=1;P2_2=0;break; 12 | case 3:P2_4=1;P2_3=0;P2_2=1;break; 13 | case 4:P2_4=1;P2_3=0;P2_2=0;break; 14 | case 5:P2_4=0;P2_3=1;P2_2=1;break; 15 | case 6:P2_4=0;P2_3=1;P2_2=0;break; 16 | case 7:P2_4=0;P2_3=0;P2_2=1;break; 17 | case 8:P2_4=0;P2_3=0;P2_2=0;break; 18 | } 19 | P0=NixieTable[Number]; 20 | Delay(1); 21 | P0=0x00; 22 | } 23 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速(11.0592MHz晶振)/Nixie.h: -------------------------------------------------------------------------------- 1 | #ifndef __NIXIE_H__ 2 | #define __NIXIE_H__ 3 | 4 | void Nixie(unsigned char Location,Number); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速(11.0592MHz晶振)/Timer0.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 定时器0初始化 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Timer0_Init(void) 9 | { 10 | TMOD &= 0xF0; //设置定时器模式 11 | TMOD |= 0x01; //设置定时器模式 12 | TL0 = 0; //设置定时初值 13 | TH0 = 0; //设置定时初值 14 | TF0 = 0; //清除TF0标志 15 | TR0 = 0; //定时器0不计时 16 | } 17 | 18 | /** 19 | * @brief 定时器0设置计数器值 20 | * @param Value,要设置的计数器值,范围:0~65535 21 | * @retval 无 22 | */ 23 | void Timer0_SetCounter(unsigned int Value) 24 | { 25 | TH0=Value/256; 26 | TL0=Value%256; 27 | } 28 | 29 | /** 30 | * @brief 定时器0获取计数器值 31 | * @param 无 32 | * @retval 计数器值,范围:0~65535 33 | */ 34 | unsigned int Timer0_GetCounter(void) 35 | { 36 | return (TH0<<8)|TL0; 37 | } 38 | 39 | /** 40 | * @brief 定时器0启动停止控制 41 | * @param Flag 启动停止标志,1为启动,0为停止 42 | * @retval 无 43 | */ 44 | void Timer0_Run(unsigned char Flag) 45 | { 46 | TR0=Flag; 47 | } 48 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速(11.0592MHz晶振)/Timer0.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIMER0_H__ 2 | #define __TIMER0_H__ 3 | 4 | void Timer0_Init(void); 5 | void Timer0_SetCounter(unsigned int Value); 6 | unsigned int Timer0_GetCounter(void); 7 | void Timer0_Run(unsigned char Flag); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速(11.0592MHz晶振)/Timer1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 定时器1初始化,100us@12.000MHz 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Timer1_Init(void) 9 | { 10 | TMOD &= 0x0F; //设置定时器模式 11 | TMOD |= 0x10; //设置定时器模式 12 | TL1 = 0x9C; //设置定时初值 13 | TH1 = 0xFF; //设置定时初值 14 | TF1 = 0; //清除TF1标志 15 | TR1 = 1; //定时器1开始计时 16 | ET1=1; 17 | EA=1; 18 | PT1=0; 19 | } 20 | 21 | /*定时器中断函数模板 22 | void Timer1_Routine() interrupt 3 23 | { 24 | static unsigned int T1Count; 25 | TL1 = 0x9C; //设置定时初值 26 | TH1 = 0xFF; //设置定时初值 27 | T1Count++; 28 | if(T1Count>=1000) 29 | { 30 | T1Count=0; 31 | 32 | } 33 | } 34 | */ 35 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速(11.0592MHz晶振)/Timer1.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIMER1_H__ 2 | #define __TIMER1_H__ 3 | 4 | void Timer1_Init(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速(11.0592MHz晶振)/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | #include "Key.h" 4 | #include "Nixie.h" 5 | #include "Motor.h" 6 | #include "IR.h" 7 | 8 | unsigned char Command,Speed; 9 | 10 | void main() 11 | { 12 | Motor_Init(); 13 | IR_Init(); 14 | while(1) 15 | { 16 | if(IR_GetDataFlag()) //如果收到数据帧 17 | { 18 | Command=IR_GetCommand(); //获取遥控器命令码 19 | 20 | if(Command==IR_0){Speed=0;} //根据遥控器命令码设置速度 21 | if(Command==IR_1){Speed=1;} 22 | if(Command==IR_2){Speed=2;} 23 | if(Command==IR_3){Speed=3;} 24 | 25 | if(Speed==0){Motor_SetSpeed(0);} //速度输出 26 | if(Speed==1){Motor_SetSpeed(50);} 27 | if(Speed==2){Motor_SetSpeed(75);} 28 | if(Speed==3){Motor_SetSpeed(100);} 29 | } 30 | Nixie(1,Speed); //数码管显示速度 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速/IR.h: -------------------------------------------------------------------------------- 1 | #ifndef __IR_H__ 2 | #define __IR_H__ 3 | 4 | #define IR_POWER 0x45 5 | #define IR_MODE 0x46 6 | #define IR_MUTE 0x47 7 | #define IR_START_STOP 0x44 8 | #define IR_PREVIOUS 0x40 9 | #define IR_NEXT 0x43 10 | #define IR_EQ 0x07 11 | #define IR_VOL_MINUS 0x15 12 | #define IR_VOL_ADD 0x09 13 | #define IR_0 0x16 14 | #define IR_RPT 0x19 15 | #define IR_USD 0x0D 16 | #define IR_1 0x0C 17 | #define IR_2 0x18 18 | #define IR_3 0x5E 19 | #define IR_4 0x08 20 | #define IR_5 0x1C 21 | #define IR_6 0x5A 22 | #define IR_7 0x42 23 | #define IR_8 0x52 24 | #define IR_9 0x4A 25 | 26 | void IR_Init(void); 27 | unsigned char IR_GetDataFlag(void); 28 | unsigned char IR_GetRepeatFlag(void); 29 | unsigned char IR_GetAddress(void); 30 | unsigned char IR_GetCommand(void); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速/Int0.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 外部中断0初始化 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Int0_Init(void) 9 | { 10 | IT0=1; 11 | IE0=0; 12 | EX0=1; 13 | EA=1; 14 | PX0=1; 15 | } 16 | 17 | /*外部中断0中断函数模板 18 | void Int0_Routine(void) interrupt 0 19 | { 20 | 21 | } 22 | */ 23 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速/Int0.h: -------------------------------------------------------------------------------- 1 | #ifndef __INT0_H__ 2 | #define __INT0_H__ 3 | 4 | void Int0_Init(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速/Key.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | 4 | /** 5 | * @brief 获取独立按键键码 6 | * @param 无 7 | * @retval 按下按键的键码,范围:0~4,无按键按下时返回值为0 8 | */ 9 | unsigned char Key() 10 | { 11 | unsigned char KeyNumber=0; 12 | 13 | if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=1;} 14 | if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=2;} 15 | if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=3;} 16 | if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=4;} 17 | 18 | return KeyNumber; 19 | } 20 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速/Key.h: -------------------------------------------------------------------------------- 1 | #ifndef __KEY_H__ 2 | #define __KEY_H__ 3 | 4 | unsigned char Key(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速/Motor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Timer1.h" 3 | 4 | //引脚定义 5 | sbit Motor=P1^0; 6 | 7 | unsigned char Counter,Compare; 8 | 9 | /** 10 | * @brief 电机初始化 11 | * @param 无 12 | * @retval 无 13 | */ 14 | void Motor_Init(void) 15 | { 16 | Timer1_Init(); 17 | } 18 | 19 | /** 20 | * @brief 电机设置速度 21 | * @param Speed 要设置的速度,范围0~100 22 | * @retval 无 23 | */ 24 | void Motor_SetSpeed(unsigned char Speed) 25 | { 26 | Compare=Speed; 27 | } 28 | 29 | //定时器1中断函数 30 | void Timer1_Routine() interrupt 3 31 | { 32 | TL1 = 0x9C; //设置定时初值 33 | TH1 = 0xFF; //设置定时初值 34 | Counter++; 35 | Counter%=100; //计数值变化范围限制在0~99 36 | if(Counter 2 | #include "Delay.h" 3 | 4 | unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; 5 | 6 | void Nixie(unsigned char Location,Number) 7 | { 8 | switch(Location) 9 | { 10 | case 1:P2_4=1;P2_3=1;P2_2=1;break; 11 | case 2:P2_4=1;P2_3=1;P2_2=0;break; 12 | case 3:P2_4=1;P2_3=0;P2_2=1;break; 13 | case 4:P2_4=1;P2_3=0;P2_2=0;break; 14 | case 5:P2_4=0;P2_3=1;P2_2=1;break; 15 | case 6:P2_4=0;P2_3=1;P2_2=0;break; 16 | case 7:P2_4=0;P2_3=0;P2_2=1;break; 17 | case 8:P2_4=0;P2_3=0;P2_2=0;break; 18 | } 19 | P0=NixieTable[Number]; 20 | Delay(1); 21 | P0=0x00; 22 | } 23 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速/Nixie.h: -------------------------------------------------------------------------------- 1 | #ifndef __NIXIE_H__ 2 | #define __NIXIE_H__ 3 | 4 | void Nixie(unsigned char Location,Number); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速/Timer0.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 定时器0初始化 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Timer0_Init(void) 9 | { 10 | TMOD &= 0xF0; //设置定时器模式 11 | TMOD |= 0x01; //设置定时器模式 12 | TL0 = 0; //设置定时初值 13 | TH0 = 0; //设置定时初值 14 | TF0 = 0; //清除TF0标志 15 | TR0 = 0; //定时器0不计时 16 | } 17 | 18 | /** 19 | * @brief 定时器0设置计数器值 20 | * @param Value,要设置的计数器值,范围:0~65535 21 | * @retval 无 22 | */ 23 | void Timer0_SetCounter(unsigned int Value) 24 | { 25 | TH0=Value/256; 26 | TL0=Value%256; 27 | } 28 | 29 | /** 30 | * @brief 定时器0获取计数器值 31 | * @param 无 32 | * @retval 计数器值,范围:0~65535 33 | */ 34 | unsigned int Timer0_GetCounter(void) 35 | { 36 | return (TH0<<8)|TL0; 37 | } 38 | 39 | /** 40 | * @brief 定时器0启动停止控制 41 | * @param Flag 启动停止标志,1为启动,0为停止 42 | * @retval 无 43 | */ 44 | void Timer0_Run(unsigned char Flag) 45 | { 46 | TR0=Flag; 47 | } 48 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速/Timer0.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIMER0_H__ 2 | #define __TIMER0_H__ 3 | 4 | void Timer0_Init(void); 5 | void Timer0_SetCounter(unsigned int Value); 6 | unsigned int Timer0_GetCounter(void); 7 | void Timer0_Run(unsigned char Flag); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速/Timer1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 定时器1初始化,100us@12.000MHz 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Timer1_Init(void) 9 | { 10 | TMOD &= 0x0F; //设置定时器模式 11 | TMOD |= 0x10; //设置定时器模式 12 | TL1 = 0x9C; //设置定时初值 13 | TH1 = 0xFF; //设置定时初值 14 | TF1 = 0; //清除TF1标志 15 | TR1 = 1; //定时器1开始计时 16 | ET1=1; 17 | EA=1; 18 | PT1=0; 19 | } 20 | 21 | /*定时器中断函数模板 22 | void Timer1_Routine() interrupt 3 23 | { 24 | static unsigned int T1Count; 25 | TL1 = 0x9C; //设置定时初值 26 | TH1 = 0xFF; //设置定时初值 27 | T1Count++; 28 | if(T1Count>=1000) 29 | { 30 | T1Count=0; 31 | 32 | } 33 | } 34 | */ 35 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速/Timer1.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIMER1_H__ 2 | #define __TIMER1_H__ 3 | 4 | void Timer1_Init(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/17-2 红外遥控电机调速/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | #include "Key.h" 4 | #include "Nixie.h" 5 | #include "Motor.h" 6 | #include "IR.h" 7 | 8 | unsigned char Command,Speed; 9 | 10 | void main() 11 | { 12 | Motor_Init(); 13 | IR_Init(); 14 | while(1) 15 | { 16 | if(IR_GetDataFlag()) //如果收到数据帧 17 | { 18 | Command=IR_GetCommand(); //获取遥控器命令码 19 | 20 | if(Command==IR_0){Speed=0;} //根据遥控器命令码设置速度 21 | if(Command==IR_1){Speed=1;} 22 | if(Command==IR_2){Speed=2;} 23 | if(Command==IR_3){Speed=3;} 24 | 25 | if(Speed==0){Motor_SetSpeed(0);} //速度输出 26 | if(Speed==1){Motor_SetSpeed(50);} 27 | if(Speed==2){Motor_SetSpeed(75);} 28 | if(Speed==3){Motor_SetSpeed(100);} 29 | } 30 | Nixie(1,Speed); //数码管显示速度 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /999-KeilProject/2-1 点亮一个LED/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | P2=0xFE; //1111 1110 6 | while(1) 7 | { 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /999-KeilProject/2-2 LED闪烁/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void Delay500ms() //@12.000MHz 5 | { 6 | unsigned char i, j, k; 7 | 8 | _nop_(); 9 | i = 4; 10 | j = 205; 11 | k = 187; 12 | do 13 | { 14 | do 15 | { 16 | while (--k); 17 | } while (--j); 18 | } while (--i); 19 | } 20 | 21 | 22 | void main() 23 | { 24 | while(1) 25 | { 26 | P2=0xFE; //1111 1110 27 | Delay500ms(); 28 | P2=0xFF; //1111 1111 29 | Delay500ms(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /999-KeilProject/2-3 LED流水灯/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void Delay500ms() //@12.000MHz 5 | { 6 | unsigned char i, j, k; 7 | 8 | _nop_(); 9 | i = 4; 10 | j = 205; 11 | k = 187; 12 | do 13 | { 14 | do 15 | { 16 | while (--k); 17 | } while (--j); 18 | } while (--i); 19 | } 20 | 21 | 22 | void main() 23 | { 24 | while(1) 25 | { 26 | P2=0xFE;//1111 1110 27 | Delay500ms(); 28 | P2=0xFD;//1111 1101 29 | Delay500ms(); 30 | P2=0xFB;//1111 1011 31 | Delay500ms(); 32 | P2=0xF7;//1111 0111 33 | Delay500ms(); 34 | P2=0xEF;//1110 1111 35 | Delay500ms(); 36 | P2=0xDF;//1101 1111 37 | Delay500ms(); 38 | P2=0xBF;//1011 1111 39 | Delay500ms(); 40 | P2=0x7F;//0111 1111 41 | Delay500ms(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /999-KeilProject/2-4 LED流水灯Plus/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Delay1ms(unsigned int xms); //@12.000MHz 4 | 5 | void main() 6 | { 7 | while(1) 8 | { 9 | P2=0xFE;//1111 1110 10 | Delay1ms(1000); 11 | P2=0xFD;//1111 1101 12 | Delay1ms(1000); 13 | P2=0xFB;//1111 1011 14 | Delay1ms(100); 15 | P2=0xF7;//1111 0111 16 | Delay1ms(100); 17 | P2=0xEF;//1110 1111 18 | Delay1ms(100); 19 | P2=0xDF;//1101 1111 20 | Delay1ms(100); 21 | P2=0xBF;//1011 1111 22 | Delay1ms(100); 23 | P2=0x7F;//0111 1111 24 | Delay1ms(100); 25 | } 26 | } 27 | 28 | void Delay1ms(unsigned int xms) //@12.000MHz 29 | { 30 | unsigned char i, j; 31 | while(xms) 32 | { 33 | i = 2; 34 | j = 239; 35 | do 36 | { 37 | while (--j); 38 | } while (--i); 39 | xms--; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /999-KeilProject/3-1 独立按键控制LED亮灭/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | while(1) 6 | { 7 | if(P3_1==0 || P3_0==0) //如果K1按键或K2按键按下 8 | { 9 | P2_0=0; //LED1输出0,点亮 10 | } 11 | else 12 | { 13 | P2_0=1; //LED1输出1,熄灭 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /999-KeilProject/3-2 独立按键控制LED状态/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Delay(unsigned int xms) 4 | { 5 | unsigned char i, j; 6 | while(xms) 7 | { 8 | i = 2; 9 | j = 239; 10 | do 11 | { 12 | while (--j); 13 | } while (--i); 14 | xms--; 15 | } 16 | } 17 | 18 | void main() 19 | { 20 | while(1) 21 | { 22 | if(P3_1==0) //如果K1按键按下 23 | { 24 | Delay(20); //延时消抖 25 | while(P3_1==0); //松手检测 26 | Delay(20); //延时消抖 27 | 28 | P2_0=~P2_0; //LED1取反 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /999-KeilProject/3-3 独立按键控制LED显示二进制/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Delay(unsigned int xms) 4 | { 5 | unsigned char i, j; 6 | while(xms--) 7 | { 8 | i = 2; 9 | j = 239; 10 | do 11 | { 12 | while (--j); 13 | } while (--i); 14 | } 15 | } 16 | 17 | void main() 18 | { 19 | unsigned char LEDNum=0; 20 | while(1) 21 | { 22 | if(P3_1==0) //如果K1按键按下 23 | { 24 | Delay(20); //延时消抖 25 | while(P3_1==0); //松手检测 26 | Delay(20); //延时消抖 27 | 28 | LEDNum++; //变量自增 29 | P2=~LEDNum; //变量取反输出给LED 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /999-KeilProject/3-4 独立按键控制LED移位/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | void Delay(unsigned int xms); 3 | 4 | unsigned char LEDNum; 5 | 6 | void main() 7 | { 8 | P2=~0x01; //上电默认LED1点亮 9 | while(1) 10 | { 11 | if(P3_1==0) //如果K1按键按下 12 | { 13 | Delay(20); 14 | while(P3_1==0); 15 | Delay(20); 16 | 17 | LEDNum++; //LEDNum自增 18 | if(LEDNum>=8) //限制LEDNum自增范围 19 | LEDNum=0; 20 | P2=~(0x01< 2 | 3 | //数码管段码表 4 | unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; 5 | 6 | //数码管显示子函数 7 | void Nixie(unsigned char Location,Number) 8 | { 9 | switch(Location) //位码输出 10 | { 11 | case 1:P2_4=1;P2_3=1;P2_2=1;break; 12 | case 2:P2_4=1;P2_3=1;P2_2=0;break; 13 | case 3:P2_4=1;P2_3=0;P2_2=1;break; 14 | case 4:P2_4=1;P2_3=0;P2_2=0;break; 15 | case 5:P2_4=0;P2_3=1;P2_2=1;break; 16 | case 6:P2_4=0;P2_3=1;P2_2=0;break; 17 | case 7:P2_4=0;P2_3=0;P2_2=1;break; 18 | case 8:P2_4=0;P2_3=0;P2_2=0;break; 19 | } 20 | P0=NixieTable[Number]; //段码输出 21 | } 22 | 23 | void main() 24 | { 25 | Nixie(2,3); //在数码管的第2位置显示3 26 | while(1) 27 | { 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /999-KeilProject/4-2 动态数码管显示/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //数码管段码表 4 | unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; 5 | 6 | //延时子函数 7 | void Delay(unsigned int xms) 8 | { 9 | unsigned char i, j; 10 | while(xms--) 11 | { 12 | i = 2; 13 | j = 239; 14 | do 15 | { 16 | while (--j); 17 | } while (--i); 18 | } 19 | } 20 | 21 | //数码管显示子函数 22 | void Nixie(unsigned char Location,Number) 23 | { 24 | switch(Location) //位码输出 25 | { 26 | case 1:P2_4=1;P2_3=1;P2_2=1;break; 27 | case 2:P2_4=1;P2_3=1;P2_2=0;break; 28 | case 3:P2_4=1;P2_3=0;P2_2=1;break; 29 | case 4:P2_4=1;P2_3=0;P2_2=0;break; 30 | case 5:P2_4=0;P2_3=1;P2_2=1;break; 31 | case 6:P2_4=0;P2_3=1;P2_2=0;break; 32 | case 7:P2_4=0;P2_3=0;P2_2=1;break; 33 | case 8:P2_4=0;P2_3=0;P2_2=0;break; 34 | } 35 | P0=NixieTable[Number]; //段码输出 36 | Delay(1); //显示一段时间 37 | P0=0x00; //段码清0,消影 38 | } 39 | 40 | void main() 41 | { 42 | while(1) 43 | { 44 | Nixie(1,1); //在数码管的第1位置显示1 45 | // Delay(20); 46 | Nixie(2,2); //在数码管的第2位置显示2 47 | // Delay(20); 48 | Nixie(3,3); //在数码管的第3位置显示3 49 | // Delay(20); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /999-KeilProject/5-1 模块化编程/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | //延时子函数 3 | void Delay(unsigned int xms) 4 | { 5 | unsigned char i, j; 6 | while(xms--) 7 | { 8 | i = 2; 9 | j = 239; 10 | do 11 | { 12 | while (--j); 13 | } while (--i); 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /999-KeilProject/5-1 模块化编程/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/5-1 模块化编程/Nixie.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" //包含Delay头文件 3 | 4 | //数码管段码表 5 | unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; 6 | 7 | //数码管显示子函数 8 | void Nixie(unsigned char Location,Number) 9 | { 10 | switch(Location) //位码输出 11 | { 12 | case 1:P2_4=1;P2_3=1;P2_2=1;break; 13 | case 2:P2_4=1;P2_3=1;P2_2=0;break; 14 | case 3:P2_4=1;P2_3=0;P2_2=1;break; 15 | case 4:P2_4=1;P2_3=0;P2_2=0;break; 16 | case 5:P2_4=0;P2_3=1;P2_2=1;break; 17 | case 6:P2_4=0;P2_3=1;P2_2=0;break; 18 | case 7:P2_4=0;P2_3=0;P2_2=1;break; 19 | case 8:P2_4=0;P2_3=0;P2_2=0;break; 20 | } 21 | P0=NixieTable[Number]; //段码输出 22 | Delay(1); //显示一段时间 23 | P0=0x00; //段码清0,消影 24 | } 25 | -------------------------------------------------------------------------------- /999-KeilProject/5-1 模块化编程/Nixie.h: -------------------------------------------------------------------------------- 1 | #ifndef __NIXIE_H__ 2 | #define __NIXIE_H__ 3 | 4 | void Nixie(unsigned char Location,Number); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/5-1 模块化编程/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" //包含Delay头文件 3 | #include "Nixie.h" //包含数码管头文件 4 | 5 | void main() 6 | { 7 | while(1) 8 | { 9 | Nixie(1,1); //在数码管的第1位置显示1 10 | Nixie(2,2); //在数码管的第2位置显示2 11 | Nixie(3,3); //在数码管的第3位置显示3 12 | Nixie(4,4); //在数码管的第4位置显示4 13 | Nixie(5,5); //在数码管的第5位置显示5 14 | Nixie(6,6); //在数码管的第6位置显示6 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /999-KeilProject/5-2 LCD1602调试工具/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/5-2 LCD1602调试工具/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/5-2 LCD1602调试工具/LCD1602.h: -------------------------------------------------------------------------------- 1 | #ifndef __LCD1602_H__ 2 | #define __LCD1602_H__ 3 | 4 | //用户调用函数: 5 | void LCD_Init(); 6 | void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char); 7 | void LCD_ShowString(unsigned char Line,unsigned char Column,char *String); 8 | void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 9 | void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length); 10 | void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 11 | void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /999-KeilProject/5-2 LCD1602调试工具/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "LCD1602.h" //包含LCD1602头文件 3 | #include "Delay.h" //包含Delay头文件 4 | 5 | int Result=0; 6 | 7 | void main() 8 | { 9 | LCD_Init(); 10 | while(1) 11 | { 12 | Result++; //Result自增 13 | Delay(1000); //延时1秒 14 | LCD_ShowNum(1,1,Result,3); //在LCD的1行1列显示Result,长度为3位 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /999-KeilProject/6-1 矩阵键盘/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/6-1 矩阵键盘/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/6-1 矩阵键盘/LCD1602.h: -------------------------------------------------------------------------------- 1 | #ifndef __LCD1602_H__ 2 | #define __LCD1602_H__ 3 | 4 | //用户调用函数: 5 | void LCD_Init(); 6 | void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char); 7 | void LCD_ShowString(unsigned char Line,unsigned char Column,char *String); 8 | void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 9 | void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length); 10 | void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 11 | void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /999-KeilProject/6-1 矩阵键盘/MatrixKey.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | 4 | /** 5 | * @brief 矩阵键盘读取按键键码 6 | * @param 无 7 | * @retval KeyNumber 按下按键的键码值 8 | 如果按键按下不放,程序会停留在此函数,松手的一瞬间,返回按键键码,没有按键按下时,返回0 9 | */ 10 | unsigned char MatrixKey() 11 | { 12 | unsigned char KeyNumber=0; 13 | 14 | P1=0xFF; 15 | P1_3=0; 16 | if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=1;} 17 | if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=5;} 18 | if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=9;} 19 | if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=13;} 20 | 21 | P1=0xFF; 22 | P1_2=0; 23 | if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=2;} 24 | if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=6;} 25 | if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=10;} 26 | if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=14;} 27 | 28 | P1=0xFF; 29 | P1_1=0; 30 | if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=3;} 31 | if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=7;} 32 | if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=11;} 33 | if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=15;} 34 | 35 | P1=0xFF; 36 | P1_0=0; 37 | if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=4;} 38 | if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=8;} 39 | if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=12;} 40 | if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=16;} 41 | 42 | return KeyNumber; 43 | } 44 | -------------------------------------------------------------------------------- /999-KeilProject/6-1 矩阵键盘/MatrixKey.h: -------------------------------------------------------------------------------- 1 | #ifndef __MATRIXKEY_H__ 2 | #define __MATRIXKEY_H__ 3 | 4 | unsigned char MatrixKey(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/6-1 矩阵键盘/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" //包含Delay头文件 3 | #include "LCD1602.h" //包含LCD1602头文件 4 | #include "MatrixKey.h" //包含矩阵键盘头文件 5 | 6 | unsigned char KeyNum; 7 | 8 | void main() 9 | { 10 | LCD_Init(); //LCD初始化 11 | LCD_ShowString(1,1,"MatrixKey:"); //LCD显示字符串 12 | while(1) 13 | { 14 | KeyNum=MatrixKey(); //获取矩阵键盘键码 15 | if(KeyNum) //如果有按键按下 16 | { 17 | LCD_ShowNum(2,1,KeyNum,2); //LCD显示键码 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /999-KeilProject/6-2 矩阵键盘密码锁/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/6-2 矩阵键盘密码锁/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/6-2 矩阵键盘密码锁/LCD1602.h: -------------------------------------------------------------------------------- 1 | #ifndef __LCD1602_H__ 2 | #define __LCD1602_H__ 3 | 4 | //用户调用函数: 5 | void LCD_Init(); 6 | void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char); 7 | void LCD_ShowString(unsigned char Line,unsigned char Column,char *String); 8 | void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 9 | void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length); 10 | void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 11 | void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /999-KeilProject/6-2 矩阵键盘密码锁/MatrixKey.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | 4 | /** 5 | * @brief 矩阵键盘读取按键键码 6 | * @param 无 7 | * @retval KeyNumber 按下按键的键码值 8 | 如果按键按下不放,程序会停留在此函数,松手的一瞬间,返回按键键码,没有按键按下时,返回0 9 | */ 10 | unsigned char MatrixKey() 11 | { 12 | unsigned char KeyNumber=0; 13 | 14 | P1=0xFF; 15 | P1_3=0; 16 | if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=1;} 17 | if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=5;} 18 | if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=9;} 19 | if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=13;} 20 | 21 | P1=0xFF; 22 | P1_2=0; 23 | if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=2;} 24 | if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=6;} 25 | if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=10;} 26 | if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=14;} 27 | 28 | P1=0xFF; 29 | P1_1=0; 30 | if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=3;} 31 | if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=7;} 32 | if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=11;} 33 | if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=15;} 34 | 35 | P1=0xFF; 36 | P1_0=0; 37 | if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=4;} 38 | if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=8;} 39 | if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=12;} 40 | if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=16;} 41 | 42 | return KeyNumber; 43 | } 44 | -------------------------------------------------------------------------------- /999-KeilProject/6-2 矩阵键盘密码锁/MatrixKey.h: -------------------------------------------------------------------------------- 1 | #ifndef __MATRIXKEY_H__ 2 | #define __MATRIXKEY_H__ 3 | 4 | unsigned char MatrixKey(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/6-2 矩阵键盘密码锁/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | #include "LCD1602.h" 4 | #include "MatrixKey.h" 5 | 6 | unsigned char KeyNum; 7 | unsigned int Password,Count; 8 | 9 | void main() 10 | { 11 | LCD_Init(); 12 | LCD_ShowString(1,1,"Password:"); 13 | while(1) 14 | { 15 | KeyNum=MatrixKey(); 16 | if(KeyNum) 17 | { 18 | if(KeyNum<=10) //如果S1~S10按键按下,输入密码 19 | { 20 | if(Count<4) //如果输入次数小于4 21 | { 22 | Password*=10; //密码左移一位 23 | Password+=KeyNum%10; //获取一位密码 24 | Count++; //计次加一 25 | } 26 | LCD_ShowNum(2,1,Password,4); //更新显示 27 | } 28 | if(KeyNum==11) //如果S11按键按下,确认 29 | { 30 | if(Password==2345) //如果密码等于正确密码 31 | { 32 | LCD_ShowString(1,14,"OK "); //显示OK 33 | Password=0; //密码清零 34 | Count=0; //计次清零 35 | LCD_ShowNum(2,1,Password,4); //更新显示 36 | } 37 | else //否则 38 | { 39 | LCD_ShowString(1,14,"ERR"); //显示ERR 40 | Password=0; //密码清零 41 | Count=0; //计次清零 42 | LCD_ShowNum(2,1,Password,4); //更新显示 43 | } 44 | } 45 | if(KeyNum==12) //如果S12按键按下,取消 46 | { 47 | Password=0; //密码清零 48 | Count=0; //计次清零 49 | LCD_ShowNum(2,1,Password,4); //更新显示 50 | } 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /999-KeilProject/7-1 按键控制LED流水灯模式/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/7-1 按键控制LED流水灯模式/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/7-1 按键控制LED流水灯模式/Key.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | 4 | /** 5 | * @brief 获取独立按键键码 6 | * @param 无 7 | * @retval 按下按键的键码,范围:0~4,无按键按下时返回值为0 8 | */ 9 | unsigned char Key() 10 | { 11 | unsigned char KeyNumber=0; 12 | 13 | if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=1;} 14 | if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=2;} 15 | if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=3;} 16 | if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=4;} 17 | 18 | return KeyNumber; 19 | } 20 | -------------------------------------------------------------------------------- /999-KeilProject/7-1 按键控制LED流水灯模式/Key.h: -------------------------------------------------------------------------------- 1 | #ifndef __KEY_H__ 2 | #define __KEY_H__ 3 | 4 | unsigned char Key(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/7-1 按键控制LED流水灯模式/Timer0.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 定时器0初始化,1毫秒@12.000MHz 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Timer0Init(void) 9 | { 10 | TMOD &= 0xF0; //设置定时器模式 11 | TMOD |= 0x01; //设置定时器模式 12 | TL0 = 0x18; //设置定时初值 13 | TH0 = 0xFC; //设置定时初值 14 | TF0 = 0; //清除TF0标志 15 | TR0 = 1; //定时器0开始计时 16 | ET0=1; 17 | EA=1; 18 | PT0=0; 19 | } 20 | 21 | /*定时器中断函数模板 22 | void Timer0_Routine() interrupt 1 23 | { 24 | static unsigned int T0Count; 25 | TL0 = 0x18; //设置定时初值 26 | TH0 = 0xFC; //设置定时初值 27 | T0Count++; 28 | if(T0Count>=1000) 29 | { 30 | T0Count=0; 31 | 32 | } 33 | } 34 | */ 35 | -------------------------------------------------------------------------------- /999-KeilProject/7-1 按键控制LED流水灯模式/Timer0.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIMER0_H__ 2 | #define __TIMER0_H__ 3 | 4 | void Timer0Init(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/7-1 按键控制LED流水灯模式/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Timer0.h" 3 | #include "Key.h" 4 | #include 5 | 6 | unsigned char KeyNum,LEDMode; 7 | 8 | void main() 9 | { 10 | P2=0xFE; 11 | Timer0Init(); 12 | while(1) 13 | { 14 | KeyNum=Key(); //获取独立按键键码 15 | if(KeyNum) //如果按键按下 16 | { 17 | if(KeyNum==1) //如果K1按键按下 18 | { 19 | LEDMode++; //模式切换 20 | if(LEDMode>=2)LEDMode=0; 21 | } 22 | } 23 | } 24 | } 25 | 26 | void Timer0_Routine() interrupt 1 27 | { 28 | static unsigned int T0Count; 29 | TL0 = 0x18; //设置定时初值 30 | TH0 = 0xFC; //设置定时初值 31 | T0Count++; //T0Count计次,对中断频率进行分频 32 | if(T0Count>=500)//分频500次,500ms 33 | { 34 | T0Count=0; 35 | if(LEDMode==0) //模式判断 36 | P2=_crol_(P2,1); //LED输出 37 | if(LEDMode==1) 38 | P2=_cror_(P2,1); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /999-KeilProject/7-2 定时器时钟/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/7-2 定时器时钟/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/7-2 定时器时钟/LCD1602.h: -------------------------------------------------------------------------------- 1 | #ifndef __LCD1602_H__ 2 | #define __LCD1602_H__ 3 | 4 | //用户调用函数: 5 | void LCD_Init(); 6 | void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char); 7 | void LCD_ShowString(unsigned char Line,unsigned char Column,char *String); 8 | void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 9 | void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length); 10 | void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 11 | void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /999-KeilProject/7-2 定时器时钟/Timer0.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 定时器0初始化,1毫秒@12.000MHz 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void Timer0Init(void) 9 | { 10 | TMOD &= 0xF0; //设置定时器模式 11 | TMOD |= 0x01; //设置定时器模式 12 | TL0 = 0x18; //设置定时初值 13 | TH0 = 0xFC; //设置定时初值 14 | TF0 = 0; //清除TF0标志 15 | TR0 = 1; //定时器0开始计时 16 | ET0=1; 17 | EA=1; 18 | PT0=0; 19 | } 20 | 21 | /*定时器中断函数模板 22 | void Timer0_Routine() interrupt 1 23 | { 24 | static unsigned int T0Count; 25 | TL0 = 0x18; //设置定时初值 26 | TH0 = 0xFC; //设置定时初值 27 | T0Count++; 28 | if(T0Count>=1000) 29 | { 30 | T0Count=0; 31 | 32 | } 33 | } 34 | */ 35 | -------------------------------------------------------------------------------- /999-KeilProject/7-2 定时器时钟/Timer0.h: -------------------------------------------------------------------------------- 1 | #ifndef __TIMER0_H__ 2 | #define __TIMER0_H__ 3 | 4 | void Timer0Init(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/7-2 定时器时钟/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | #include "LCD1602.h" 4 | #include "Timer0.h" 5 | 6 | unsigned char Sec=55,Min=59,Hour=23; 7 | 8 | void main() 9 | { 10 | LCD_Init(); 11 | Timer0Init(); 12 | 13 | LCD_ShowString(1,1,"Clock:"); //上电显示静态字符串 14 | LCD_ShowString(2,1," : :"); 15 | 16 | while(1) 17 | { 18 | LCD_ShowNum(2,1,Hour,2); //显示时分秒 19 | LCD_ShowNum(2,4,Min,2); 20 | LCD_ShowNum(2,7,Sec,2); 21 | } 22 | } 23 | 24 | void Timer0_Routine() interrupt 1 25 | { 26 | static unsigned int T0Count; 27 | TL0 = 0x18; //设置定时初值 28 | TH0 = 0xFC; //设置定时初值 29 | T0Count++; 30 | if(T0Count>=1000) //定时器分频,1s 31 | { 32 | T0Count=0; 33 | Sec++; //1秒到,Sec自增 34 | if(Sec>=60) 35 | { 36 | Sec=0; //60秒到,Sec清0,Min自增 37 | Min++; 38 | if(Min>=60) 39 | { 40 | Min=0; //60分钟到,Min清0,Hour自增 41 | Hour++; 42 | if(Hour>=24) 43 | { 44 | Hour=0; //24小时到,Hour清0 45 | } 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /999-KeilProject/8-1 串口向电脑发送数据/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/8-1 串口向电脑发送数据/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/8-1 串口向电脑发送数据/UART.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 串口初始化,4800bps@12.000MHz 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void UART_Init() 9 | { 10 | SCON=0x40; 11 | PCON |= 0x80; 12 | TMOD &= 0x0F; //设置定时器模式 13 | TMOD |= 0x20; //设置定时器模式 14 | TL1 = 0xF3; //设定定时初值 15 | TH1 = 0xF3; //设定定时器重装值 16 | ET1 = 0; //禁止定时器1中断 17 | TR1 = 1; //启动定时器1 18 | } 19 | 20 | /** 21 | * @brief 串口发送一个字节数据 22 | * @param Byte 要发送的一个字节数据 23 | * @retval 无 24 | */ 25 | void UART_SendByte(unsigned char Byte) 26 | { 27 | SBUF=Byte; 28 | while(TI==0); 29 | TI=0; 30 | } 31 | -------------------------------------------------------------------------------- /999-KeilProject/8-1 串口向电脑发送数据/UART.h: -------------------------------------------------------------------------------- 1 | #ifndef __UART_H__ 2 | #define __UART_H__ 3 | 4 | void UART_Init(); 5 | void UART_SendByte(unsigned char Byte); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /999-KeilProject/8-1 串口向电脑发送数据/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | #include "UART.h" 4 | 5 | unsigned char Sec; 6 | 7 | void main() 8 | { 9 | UART_Init(); //串口初始化 10 | while(1) 11 | { 12 | UART_SendByte(Sec); //串口发送一个字节 13 | Sec++; //Sec自增 14 | Delay(1000); //延时1秒 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /999-KeilProject/8-2 电脑通过串口控制LED/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/8-2 电脑通过串口控制LED/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/8-2 电脑通过串口控制LED/UART.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * @brief 串口初始化,4800bps@12.000MHz 5 | * @param 无 6 | * @retval 无 7 | */ 8 | void UART_Init() 9 | { 10 | SCON=0x50; 11 | PCON |= 0x80; 12 | TMOD &= 0x0F; //设置定时器模式 13 | TMOD |= 0x20; //设置定时器模式 14 | TL1 = 0xF3; //设定定时初值 15 | TH1 = 0xF3; //设定定时器重装值 16 | ET1 = 0; //禁止定时器1中断 17 | TR1 = 1; //启动定时器1 18 | EA=1; 19 | ES=1; 20 | } 21 | 22 | /** 23 | * @brief 串口发送一个字节数据 24 | * @param Byte 要发送的一个字节数据 25 | * @retval 无 26 | */ 27 | void UART_SendByte(unsigned char Byte) 28 | { 29 | SBUF=Byte; 30 | while(TI==0); 31 | TI=0; 32 | } 33 | 34 | /*串口中断函数模板 35 | void UART_Routine() interrupt 4 36 | { 37 | if(RI==1) 38 | { 39 | 40 | RI=0; 41 | } 42 | } 43 | */ 44 | -------------------------------------------------------------------------------- /999-KeilProject/8-2 电脑通过串口控制LED/UART.h: -------------------------------------------------------------------------------- 1 | #ifndef __UART_H__ 2 | #define __UART_H__ 3 | 4 | void UART_Init(); 5 | void UART_SendByte(unsigned char Byte); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /999-KeilProject/8-2 电脑通过串口控制LED/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | #include "UART.h" 4 | 5 | void main() 6 | { 7 | UART_Init(); //串口初始化 8 | while(1) 9 | { 10 | 11 | } 12 | } 13 | 14 | void UART_Routine() interrupt 4 15 | { 16 | if(RI==1) //如果接收标志位为1,接收到了数据 17 | { 18 | P2=~SBUF; //读取数据,取反后输出到LED 19 | UART_SendByte(SBUF); //将受到的数据发回串口 20 | RI=0; //接收标志位清0 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /999-KeilProject/9-1 LED点阵屏显示图形/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/9-1 LED点阵屏显示图形/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/9-1 LED点阵屏显示图形/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | 4 | sbit RCK=P3^5; //RCLK 5 | sbit SCK=P3^6; //SRCLK 6 | sbit SER=P3^4; //SER 7 | 8 | #define MATRIX_LED_PORT P0 9 | 10 | /** 11 | * @brief 74HC595写入一个字节 12 | * @param Byte 要写入的字节 13 | * @retval 无 14 | */ 15 | void _74HC595_WriteByte(unsigned char Byte) 16 | { 17 | unsigned char i; 18 | for(i=0;i<8;i++) 19 | { 20 | SER=Byte&(0x80>>i); 21 | SCK=1; 22 | SCK=0; 23 | } 24 | RCK=1; 25 | RCK=0; 26 | } 27 | 28 | /** 29 | * @brief LED点阵屏显示一列数据 30 | * @param Column 要选择的列,范围:0~7,0在最左边 31 | * @param Data 选择列显示的数据,高位在上,1为亮,0为灭 32 | * @retval 无 33 | */ 34 | void MatrixLED_ShowColumn(unsigned char Column,Data) 35 | { 36 | _74HC595_WriteByte(Data); 37 | MATRIX_LED_PORT=~(0x80>>Column); 38 | Delay(1); 39 | MATRIX_LED_PORT=0xFF; 40 | } 41 | 42 | void main() 43 | { 44 | SCK=0; 45 | RCK=0; 46 | while(1) 47 | { 48 | MatrixLED_ShowColumn(0,0x3C); 49 | MatrixLED_ShowColumn(1,0x42); 50 | MatrixLED_ShowColumn(2,0xA9); 51 | MatrixLED_ShowColumn(3,0x85); 52 | MatrixLED_ShowColumn(4,0x85); 53 | MatrixLED_ShowColumn(5,0xA9); 54 | MatrixLED_ShowColumn(6,0x42); 55 | MatrixLED_ShowColumn(7,0x3C); 56 | } 57 | } 58 | 59 | -------------------------------------------------------------------------------- /999-KeilProject/9-2 LED点阵屏显示动画/Delay.c: -------------------------------------------------------------------------------- 1 | 2 | void Delay(unsigned int xms) 3 | { 4 | unsigned char i, j; 5 | while(xms--) 6 | { 7 | i = 2; 8 | j = 239; 9 | do 10 | { 11 | while (--j); 12 | } while (--i); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /999-KeilProject/9-2 LED点阵屏显示动画/Delay.h: -------------------------------------------------------------------------------- 1 | #ifndef __DELAY_H__ 2 | #define __DELAY_H__ 3 | 4 | void Delay(unsigned int xms); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /999-KeilProject/9-2 LED点阵屏显示动画/MatrixLED.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | 4 | sbit RCK=P3^5; //RCLK 5 | sbit SCK=P3^6; //SRCLK 6 | sbit SER=P3^4; //SER 7 | 8 | #define MATRIX_LED_PORT P0 9 | 10 | /** 11 | * @brief 74HC595写入一个字节 12 | * @param Byte 要写入的字节 13 | * @retval 无 14 | */ 15 | void _74HC595_WriteByte(unsigned char Byte) 16 | { 17 | unsigned char i; 18 | for(i=0;i<8;i++) 19 | { 20 | SER=Byte&(0x80>>i); 21 | SCK=1; 22 | SCK=0; 23 | } 24 | RCK=1; 25 | RCK=0; 26 | } 27 | 28 | /** 29 | * @brief 点阵屏初始化 30 | * @param 无 31 | * @retval 无 32 | */ 33 | void MatrixLED_Init() 34 | { 35 | SCK=0; 36 | RCK=0; 37 | } 38 | 39 | /** 40 | * @brief LED点阵屏显示一列数据 41 | * @param Column 要选择的列,范围:0~7,0在最左边 42 | * @param Data 选择列显示的数据,高位在上,1为亮,0为灭 43 | * @retval 无 44 | */ 45 | void MatrixLED_ShowColumn(unsigned char Column,Data) 46 | { 47 | _74HC595_WriteByte(Data); 48 | MATRIX_LED_PORT=~(0x80>>Column); 49 | Delay(1); 50 | MATRIX_LED_PORT=0xFF; 51 | } 52 | -------------------------------------------------------------------------------- /999-KeilProject/9-2 LED点阵屏显示动画/MatrixLED.h: -------------------------------------------------------------------------------- 1 | #ifndef __MATRIX_LED_H__ 2 | #define __MATRIX_LED_H__ 3 | 4 | void MatrixLED_Init(); 5 | void MatrixLED_ShowColumn(unsigned char Column,Data); 6 | 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /999-KeilProject/9-2 LED点阵屏显示动画/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Delay.h" 3 | #include "MatrixLED.h" 4 | 5 | //动画数据 6 | unsigned char code Animation[]={ 7 | 0x3C,0x42,0xA9,0x85,0x85,0xA9,0x42,0x3C, 8 | 0x3C,0x42,0xA1,0x85,0x85,0xA1,0x42,0x3C, 9 | 0x3C,0x42,0xA5,0x89,0x89,0xA5,0x42,0x3C, 10 | }; 11 | 12 | void main() 13 | { 14 | unsigned char i,Offset=0,Count=0; 15 | MatrixLED_Init(); 16 | while(1) 17 | { 18 | for(i=0;i<8;i++) //循环8次,显示8列数据 19 | { 20 | MatrixLED_ShowColumn(i,Animation[i+Offset]); 21 | } 22 | Count++; //计次延时 23 | if(Count>15) 24 | { 25 | Count=0; 26 | Offset+=8; //偏移+8,切换下一帧画面 27 | if(Offset>16) 28 | { 29 | Offset=0; 30 | } 31 | } 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /999-KeilProject/LCD1602驱动代码/LCD1602.h: -------------------------------------------------------------------------------- 1 | #ifndef __LCD1602_H__ 2 | #define __LCD1602_H__ 3 | 4 | //用户调用函数: 5 | void LCD_Init(); 6 | void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char); 7 | void LCD_ShowString(unsigned char Line,unsigned char Column,char *String); 8 | void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 9 | void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length); 10 | void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 11 | void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /999-KeilProject/程序说明.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongwenjun/stc89c52/016336ebc6c8f2d1d86335b476f4bd2f21209857/999-KeilProject/程序说明.pdf -------------------------------------------------------------------------------- /Document/51单片机电路原理图_HC6800-ES_V2.0新版.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongwenjun/stc89c52/016336ebc6c8f2d1d86335b476f4bd2f21209857/Document/51单片机电路原理图_HC6800-ES_V2.0新版.pdf -------------------------------------------------------------------------------- /Document/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongwenjun/stc89c52/016336ebc6c8f2d1d86335b476f4bd2f21209857/Document/README.md -------------------------------------------------------------------------------- /img/1-led.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongwenjun/stc89c52/016336ebc6c8f2d1d86335b476f4bd2f21209857/img/1-led.webp -------------------------------------------------------------------------------- /img/8x8led_hong.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongwenjun/stc89c52/016336ebc6c8f2d1d86335b476f4bd2f21209857/img/8x8led_hong.jpg -------------------------------------------------------------------------------- /img/digital_watch.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongwenjun/stc89c52/016336ebc6c8f2d1d86335b476f4bd2f21209857/img/digital_watch.webp -------------------------------------------------------------------------------- /img/hello_world.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongwenjun/stc89c52/016336ebc6c8f2d1d86335b476f4bd2f21209857/img/hello_world.webp -------------------------------------------------------------------------------- /img/lcd1602.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongwenjun/stc89c52/016336ebc6c8f2d1d86335b476f4bd2f21209857/img/lcd1602.jpg -------------------------------------------------------------------------------- /img/str-7segment.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongwenjun/stc89c52/016336ebc6c8f2d1d86335b476f4bd2f21209857/img/str-7segment.png -------------------------------------------------------------------------------- /sdcc/code/MCS51.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 46 | 47 | -------------------------------------------------------------------------------- /sdcc/lib_src/__assert.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | assert.c 3 | 4 | Copyright (C) 2000, Michael Hope 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #include 30 | #include 31 | 32 | void __assert(const char *expression, const char *functionname, const char *filename, unsigned int linenumber) 33 | { 34 | printf("Assert(%s) failed in function %s at line %u in file %s.\n", 35 | expression, functionname, linenumber, filename); 36 | for(;;); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /sdcc/lib_src/__memcpy.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | _memcpy.c - part of string library functions 3 | 4 | Copyright (C) 1999, Sandeep Dutta . sandeep.dutta@usa.net 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #include 30 | 31 | void *__memcpy (void *dst, const void *src, size_t n) 32 | { 33 | void *ret = dst; 34 | char *d = dst; 35 | const char *s = src; 36 | 37 | while (n--) 38 | *d++ = *s++; 39 | 40 | return(ret); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /sdcc/lib_src/_bp.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | _bp.c - just declares bp as a variable 3 | 4 | Copyright (C) 1999, Sandeep Dutta . sandeep.dutta@usa.net 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | __data unsigned char bp ; 30 | -------------------------------------------------------------------------------- /sdcc/lib_src/_divuchar.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | _divuchar.c :- routine for unsigned char (8 bit) division. just calls 3 | routine for unsigned int division 4 | 5 | Copyright (C) 2017, Philipp Klaus Krause . pkk@spth.de 6 | 7 | This library is free software; you can redistribute it and/or modify it 8 | under the terms of the GNU General Public License as published by the 9 | Free Software Foundation; either version 2, or (at your option) any 10 | later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this library; see the file COPYING. If not, write to the 19 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 20 | MA 02110-1301, USA. 21 | 22 | As a special exception, if you link this library with other files, 23 | some of which are compiled with SDCC, to produce an executable, 24 | this library does not by itself cause the resulting executable to 25 | be covered by the GNU General Public License. This exception does 26 | not however invalidate any other reasons why the executable file 27 | might be covered by the GNU General Public License. 28 | -------------------------------------------------------------------------*/ 29 | 30 | unsigned int 31 | _divuchar (unsigned char x, unsigned char y) 32 | { 33 | return ((unsigned int)x / (unsigned int)y); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /sdcc/lib_src/_memchr.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | _memchr.c - part of string library functions 3 | 4 | Copyright (C) 2009-2019, Philipp Klaus Krause . pkk@spth.de 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #include 30 | 31 | void *memchr(const void *s, int c, size_t n) 32 | { 33 | unsigned char *p = (unsigned char *)s; 34 | unsigned char *end = p + n; 35 | for(; p != end; p++) 36 | if(*p == (unsigned char)c) 37 | return((void *)p); 38 | return(0); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /sdcc/lib_src/_moduchar.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | _moduchar.c :- routine for signed char (8 bit) division. 3 | 4 | Copyright (C) 2013, Philipp Klaus Krause . pkk@spth.de 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | unsigned int 30 | _moduchar (unsigned char x, unsigned char y) 31 | { 32 | return ((unsigned int)x % (unsigned int)y); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /sdcc/lib_src/_rlslonglong.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | _rlslonglong.c - routine for left shift of 64 bit long long 3 | 4 | Copyright (C) 2012, Philipp Klaus Krause . philipp@informatik.uni-frankfurt.de 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #pragma std_c99 30 | 31 | #include 32 | 33 | #ifdef __SDCC_LONGLONG 34 | 35 | long long _rlslonglong(long long l, char s) 36 | { 37 | return((unsigned long long)(l) << s); 38 | } 39 | 40 | #endif 41 | 42 | -------------------------------------------------------------------------------- /sdcc/lib_src/_spx.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | _spx.c - just declares bp as a variable 3 | 4 | Copyright (C) 1999, Sandeep Dutta . sandeep.dutta@usa.net 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | __data unsigned char spx ; 30 | __data unsigned char _page_no__; 31 | -------------------------------------------------------------------------------- /sdcc/lib_src/_strncmp.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | _strncmp.c - part of string library functions 3 | 4 | Copyright (C) 1999, Sandeep Dutta . sandeep.dutta@usa.net 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #include 30 | 31 | int strncmp ( const char * first, const char * last, size_t count ) 32 | { 33 | if (!count) 34 | return(0); 35 | 36 | while (--count && *first && *first == *last) { 37 | first++; 38 | last++; 39 | } 40 | 41 | return( *first - *last ); 42 | } 43 | -------------------------------------------------------------------------------- /sdcc/lib_src/bpx.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | bpx.c - just declares bpx as a variable 3 | 4 | Copyright (C) 1999, Sandeep Dutta . sandeep.dutta@usa.net 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #if defined(__SDCC_mcs51) 30 | __data unsigned char bpx ; 31 | #else 32 | __data unsigned int bpx ; 33 | #endif 34 | -------------------------------------------------------------------------------- /sdcc/lib_src/btowc.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | btowc.c - convert a single-byte character to a wide character 3 | 4 | Copyright (C) 2016, Philipp Klaus Krause, pkk@spth.de 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #include 30 | 31 | #include 32 | 33 | _Static_assert(EOF & 0x80, "Invalid EOF"); 34 | 35 | wint_t btowc(int c) 36 | { 37 | if(c & 0x80) 38 | return WEOF; 39 | return c; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /sdcc/lib_src/c32rtomb.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | c32rtomb.c - convert a wide character to a multibyte sequence 3 | 4 | Copyright (C) 2016, Philipp Klaus Krause, pkk@spth.de 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #include 30 | 31 | #include 32 | 33 | size_t c32rtomb(char *restrict s, char32_t c32, mbstate_t *restrict ps) 34 | { 35 | ps; 36 | return(wctomb(s, c32)); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /sdcc/lib_src/ceilf.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | ceilf.c - Returns the integer larger or equal than x 3 | 4 | Copyright (C) 2001, 2002, Jesus Calvino-Fraga, jesusc@ieee.org 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | /* Version 1.0 - Initial release */ 30 | 31 | #include 32 | 33 | float ceilf(float x) _FLOAT_FUNC_REENTRANT 34 | { 35 | long r; 36 | r=x; 37 | if (r<0) 38 | return r; 39 | else 40 | return (r+((r 32 | #include 33 | 34 | float sincosf(float x, bool iscos); 35 | 36 | float cosf(float x) _FLOAT_FUNC_REENTRANT 37 | { 38 | if (x==0.0) return 1.0; 39 | return sincosf(x, 1); 40 | } 41 | -------------------------------------------------------------------------------- /sdcc/lib_src/coshf.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | coshf.c - Computes cosh(x) where x is a 32-bit float. 3 | 4 | Copyright (C) 2001, 2002, Jesus Calvino-Fraga, jesusc@ieee.org 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | /* Version 1.0 - Initial release */ 30 | 31 | #include 32 | #include 33 | 34 | float sincoshf(float x, bool iscosh); 35 | 36 | float coshf(float x) _FLOAT_FUNC_REENTRANT 37 | { 38 | return sincoshf(x, 1); 39 | } 40 | -------------------------------------------------------------------------------- /sdcc/lib_src/errno.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | errno.c - just declares errno as a variable 3 | 4 | Copyright (C) 2003, Jesus Calvino-Fraga, jesusc@ieee.org 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | int errno; 30 | -------------------------------------------------------------------------------- /sdcc/lib_src/fabsf.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | fabsf.c - Returns the absolute value of a 32-bit float. 3 | 4 | Copyright (C) 2001, 2002, Jesus Calvino-Fraga, jesusc@ieee.org 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | /* Version 1.0 - Initial release */ 30 | 31 | #include 32 | #include 33 | 34 | float fabsf(float x) _FLOAT_FUNC_REENTRANT 35 | { 36 | union float_long fl; 37 | 38 | fl.f = x; 39 | fl.l &= 0x7fffffff; 40 | return fl.f; 41 | } 42 | -------------------------------------------------------------------------------- /sdcc/lib_src/floorf.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | floorf.c - Returns the integer smaller or equal than x 3 | 4 | Copyright (C) 2001, 2002, Jesus Calvino-Fraga, jesusc@ieee.org 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | /* Version 1.0 - Initial release */ 30 | 31 | #include 32 | 33 | float floorf (float x) _FLOAT_FUNC_REENTRANT 34 | { 35 | long r; 36 | r=x; 37 | if (r<=0) 38 | return (r+((r>x)?-1:0)); 39 | else 40 | return r; 41 | } 42 | -------------------------------------------------------------------------------- /sdcc/lib_src/isalnum.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | isalnum.c 3 | 4 | Philipp Klaus Krause, philipp@informatik.uni-frankfurt.de 2013 5 | 6 | (c) 2013 Goethe-Universität Frankfurt 7 | 8 | This library is free software; you can redistribute it and/or modify it 9 | under the terms of the GNU General Public License as published by the 10 | Free Software Foundation; either version 2, or (at your option) any 11 | later version. 12 | 13 | This library is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this library; see the file COPYING. If not, write to the 20 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 21 | MA 02110-1301, USA. 22 | 23 | As a special exception, if you link this library with other files, 24 | some of which are compiled with SDCC, to produce an executable, 25 | this library does not by itself cause the resulting executable to 26 | be covered by the GNU General Public License. This exception does 27 | not however invalidate any other reasons why the executable file 28 | might be covered by the GNU General Public License. 29 | -------------------------------------------------------------------------*/ 30 | 31 | #include 32 | 33 | #ifdef isalnum 34 | #undef isalnum 35 | #endif 36 | 37 | int isalnum (int c) 38 | { 39 | return (isalpha (c) || isdigit (c)); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /sdcc/lib_src/isalpha.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | isalpha.c 3 | 4 | Philipp Klaus Krause, philipp@informatik.uni-frankfurt.de 2013 5 | 6 | (c) 2013 Goethe-Universität Frankfurt 7 | 8 | This library is free software; you can redistribute it and/or modify it 9 | under the terms of the GNU General Public License as published by the 10 | Free Software Foundation; either version 2, or (at your option) any 11 | later version. 12 | 13 | This library is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this library; see the file COPYING. If not, write to the 20 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 21 | MA 02110-1301, USA. 22 | 23 | As a special exception, if you link this library with other files, 24 | some of which are compiled with SDCC, to produce an executable, 25 | this library does not by itself cause the resulting executable to 26 | be covered by the GNU General Public License. This exception does 27 | not however invalidate any other reasons why the executable file 28 | might be covered by the GNU General Public License. 29 | -------------------------------------------------------------------------*/ 30 | 31 | #include 32 | 33 | #ifdef isalpha 34 | #undef isalpha 35 | #endif 36 | 37 | int isalpha (int c) 38 | { 39 | return (isupper (c) || islower (c)); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /sdcc/lib_src/iscntrl.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | iscntrl.c 3 | 4 | Philipp Klaus Krause, philipp@informatik.uni-frankfurt.de 2013 5 | 6 | (c) 2013 Goethe-Universität Frankfurt 7 | 8 | This library is free software; you can redistribute it and/or modify it 9 | under the terms of the GNU General Public License as published by the 10 | Free Software Foundation; either version 2, or (at your option) any 11 | later version. 12 | 13 | This library is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this library; see the file COPYING. If not, write to the 20 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 21 | MA 02110-1301, USA. 22 | 23 | As a special exception, if you link this library with other files, 24 | some of which are compiled with SDCC, to produce an executable, 25 | this library does not by itself cause the resulting executable to 26 | be covered by the GNU General Public License. This exception does 27 | not however invalidate any other reasons why the executable file 28 | might be covered by the GNU General Public License. 29 | -------------------------------------------------------------------------*/ 30 | 31 | #include 32 | 33 | int iscntrl (int c) 34 | { 35 | return (c < ' ' || c == 0x7f); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /sdcc/lib_src/isgraph.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | isgraph.c 3 | 4 | Philipp Klaus Krause, philipp@informatik.uni-frankfurt.de 2013 5 | 6 | (c) 2013 Goethe-Universität Frankfurt 7 | 8 | This library is free software; you can redistribute it and/or modify it 9 | under the terms of the GNU General Public License as published by the 10 | Free Software Foundation; either version 2, or (at your option) any 11 | later version. 12 | 13 | This library is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this library; see the file COPYING. If not, write to the 20 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 21 | MA 02110-1301, USA. 22 | 23 | As a special exception, if you link this library with other files, 24 | some of which are compiled with SDCC, to produce an executable, 25 | this library does not by itself cause the resulting executable to 26 | be covered by the GNU General Public License. This exception does 27 | not however invalidate any other reasons why the executable file 28 | might be covered by the GNU General Public License. 29 | -------------------------------------------------------------------------*/ 30 | 31 | #include 32 | 33 | int isgraph (int c) 34 | { 35 | return (c > ' ' && c <= '~'); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /sdcc/lib_src/isinf.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | isinf.c - Check if a float value is an infinite (positive or negative) 3 | 4 | Copyright (C) 2016, Ben Shi, powerstudio1st@163.com 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #define __SDCC_FLOAT_LIB 30 | #include 31 | #include 32 | 33 | int isinf (float f) 34 | { 35 | unsigned long *pl = (unsigned long *) &f; 36 | return *pl == 0x7f800000 || *pl == 0xff800000; 37 | } 38 | -------------------------------------------------------------------------------- /sdcc/lib_src/isprint.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | isprint.c 3 | 4 | Philipp Klaus Krause, philipp@informatik.uni-frankfurt.de 2013 5 | 6 | (c) 2013 Goethe-Universität Frankfurt 7 | 8 | This library is free software; you can redistribute it and/or modify it 9 | under the terms of the GNU General Public License as published by the 10 | Free Software Foundation; either version 2, or (at your option) any 11 | later version. 12 | 13 | This library is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this library; see the file COPYING. If not, write to the 20 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 21 | MA 02110-1301, USA. 22 | 23 | As a special exception, if you link this library with other files, 24 | some of which are compiled with SDCC, to produce an executable, 25 | this library does not by itself cause the resulting executable to 26 | be covered by the GNU General Public License. This exception does 27 | not however invalidate any other reasons why the executable file 28 | might be covered by the GNU General Public License. 29 | -------------------------------------------------------------------------*/ 30 | 31 | #include 32 | 33 | int isprint (int c) 34 | { 35 | return (c >= ' ' && c <= '~'); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /sdcc/lib_src/ispunct.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | ispunct.c 3 | 4 | Philipp Klaus Krause, philipp@informatik.uni-frankfurt.de 2013 5 | 6 | (c) 2013 Goethe-Universität Frankfurt 7 | 8 | This library is free software; you can redistribute it and/or modify it 9 | under the terms of the GNU General Public License as published by the 10 | Free Software Foundation; either version 2, or (at your option) any 11 | later version. 12 | 13 | This library is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this library; see the file COPYING. If not, write to the 20 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 21 | MA 02110-1301, USA. 22 | 23 | As a special exception, if you link this library with other files, 24 | some of which are compiled with SDCC, to produce an executable, 25 | this library does not by itself cause the resulting executable to 26 | be covered by the GNU General Public License. This exception does 27 | not however invalidate any other reasons why the executable file 28 | might be covered by the GNU General Public License. 29 | -------------------------------------------------------------------------*/ 30 | 31 | #include 32 | 33 | int ispunct (int c) 34 | { 35 | return (isprint (c) && !isspace (c) && !isalnum (c)); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /sdcc/lib_src/isspace.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | isspace.c 3 | 4 | Philipp Klaus Krause, philipp@informatik.uni-frankfurt.de 2013 5 | 6 | (c) 2013 Goethe-Universität Frankfurt 7 | 8 | This library is free software; you can redistribute it and/or modify it 9 | under the terms of the GNU General Public License as published by the 10 | Free Software Foundation; either version 2, or (at your option) any 11 | later version. 12 | 13 | This library is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this library; see the file COPYING. If not, write to the 20 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 21 | MA 02110-1301, USA. 22 | 23 | As a special exception, if you link this library with other files, 24 | some of which are compiled with SDCC, to produce an executable, 25 | this library does not by itself cause the resulting executable to 26 | be covered by the GNU General Public License. This exception does 27 | not however invalidate any other reasons why the executable file 28 | might be covered by the GNU General Public License. 29 | -------------------------------------------------------------------------*/ 30 | 31 | #include 32 | 33 | #ifdef isspace 34 | #undef isspace 35 | #endif 36 | 37 | int isspace (int c) 38 | { 39 | return (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /sdcc/lib_src/isxdigit.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | isxdigit.c 3 | 4 | Philipp Klaus Krause, philipp@informatik.uni-frankfurt.de 2013 5 | 6 | (c) 2013 Goethe-Universität Frankfurt 7 | 8 | This library is free software; you can redistribute it and/or modify it 9 | under the terms of the GNU General Public License as published by the 10 | Free Software Foundation; either version 2, or (at your option) any 11 | later version. 12 | 13 | This library is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this library; see the file COPYING. If not, write to the 20 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 21 | MA 02110-1301, USA. 22 | 23 | As a special exception, if you link this library with other files, 24 | some of which are compiled with SDCC, to produce an executable, 25 | this library does not by itself cause the resulting executable to 26 | be covered by the GNU General Public License. This exception does 27 | not however invalidate any other reasons why the executable file 28 | might be covered by the GNU General Public License. 29 | -------------------------------------------------------------------------*/ 30 | 31 | #include 32 | 33 | int isxdigit (int c) 34 | { 35 | return (c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F'); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /sdcc/lib_src/log10f.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | log10f.c - Computes the base 10 log of a 32 bit float. 3 | 4 | Copyright (C) 2001, 2002, Jesus Calvino-Fraga, jesusc@ieee.org 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | /* Version 1.0 - Initial release */ 30 | 31 | #include 32 | #include 33 | 34 | float log10f(float x) _FLOAT_FUNC_REENTRANT 35 | { 36 | return logf(x)*0.4342944819; 37 | } 38 | -------------------------------------------------------------------------------- /sdcc/lib_src/mbrlen.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | mbrlen.c - determine number of bytes in next multibyte character 3 | 4 | Copyright (C) 2016, Philipp Klaus Krause, pkk@spth.de 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #include 30 | 31 | size_t mbrlen(const char *restrict s, size_t n, mbstate_t *restrict ps) 32 | { 33 | static mbstate_t sps; 34 | 35 | return(mbrtowc(0, s, n, ps ? ps : &sps)); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /sdcc/lib_src/mbsinit.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | mbsinit.c - determine conversion state 3 | 4 | Copyright (C) 2016, Philipp Klaus Krause, pkk@spth.de 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #include 30 | 31 | int mbsinit(const mbstate_t *ps) 32 | { 33 | return(!ps || !ps->c[0] && !ps->c[1] && !ps->c[2]); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /sdcc/lib_src/mcs51/crtpagesfr.asm: -------------------------------------------------------------------------------- 1 | ;-------------------------------------------------------------------------- 2 | ; crtpagesfr.asm - C run-time: define page sfr for movx @r0/r1 instructions 3 | ; 4 | ; Copyright (C) 2004, Erik Petrich 5 | ; 6 | ; This library is free software; you can redistribute it and/or modify it 7 | ; under the terms of the GNU General Public License as published by the 8 | ; Free Software Foundation; either version 2, or (at your option) any 9 | ; later version. 10 | ; 11 | ; This library is distributed in the hope that it will be useful, 12 | ; but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | ; GNU General Public License for more details. 15 | ; 16 | ; You should have received a copy of the GNU General Public License 17 | ; along with this library; see the file COPYING. If not, write to the 18 | ; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | ; MA 02110-1301, USA. 20 | ; 21 | ; As a special exception, if you link this library with other files, 22 | ; some of which are compiled with SDCC, to produce an executable, 23 | ; this library does not by itself cause the resulting executable to 24 | ; be covered by the GNU General Public License. This exception does 25 | ; not however invalidate any other reasons why the executable file 26 | ; might be covered by the GNU General Public License. 27 | ;-------------------------------------------------------------------------- 28 | 29 | __XPAGE == 0xa0 ; 0xa0 is P2 on the original 8051 30 | -------------------------------------------------------------------------------- /sdcc/lib_src/modff.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | modff.c - Returns both the integer and fraction of a float. 3 | 4 | Copyright (C) 2001, 2002, Jesus Calvino-Fraga, jesusc@ieee.org 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | /* Version 1.0 - Initial release */ 30 | 31 | #include 32 | 33 | float modff(float x, float * y) 34 | { 35 | *y=(long)x; 36 | return (x-*y); 37 | } 38 | -------------------------------------------------------------------------------- /sdcc/lib_src/printf_fast_f.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | printf_fast_f.c - Same as printf_fast, but with floating point enabled 3 | 4 | Copyright (C) 2004, Paul Stoffregen, paul@pjrc.com 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #define __SDCC_FLOAT_LIB 30 | #include 31 | 32 | #ifdef FLOAT_ASM_MCS51 33 | 34 | #define FLOAT 35 | #define PRINTF_FAST printf_fast_f 36 | 37 | #include "printf_fast.c" 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /sdcc/lib_src/puts.c: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------- 2 | puts.c - source file for C function puts() 3 | 4 | Copyright (C) 2016, Philipp Klaus Krause . pkk@spth.de 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #include 30 | 31 | int puts (const char *s) 32 | { 33 | while (*s) 34 | if (putchar(*s++) == EOF) 35 | return EOF; 36 | return putchar('\n'); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /sdcc/lib_src/sinf.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | sinf.c - Computes sin(x) where x is a 32-bit float. 3 | 4 | Copyright (C) 2001, 2002, Jesus Calvino-Fraga, jesusc@ieee.org 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | /* Version 1.0 - Initial release */ 30 | 31 | #include 32 | #include 33 | 34 | float sincosf(float x, bool iscos); 35 | 36 | float sinf(float x) _FLOAT_FUNC_REENTRANT 37 | { 38 | if (x==0.0) return 0.0; 39 | return sincosf(x, 0); 40 | } 41 | -------------------------------------------------------------------------------- /sdcc/lib_src/sinhf.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | sinhf.c - Computes sinh(x) where x is a 32-bit float. 3 | 4 | Copyright (C) 2001, 2002, Jesus Calvino-Fraga, jesusc@ieee.org 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | /* Version 1.0 - Initial release */ 30 | 31 | #include 32 | #include 33 | 34 | float sincoshf(float x, bool iscosh); 35 | 36 | float sinhf(float x) _FLOAT_FUNC_REENTRANT 37 | { 38 | return sincoshf(x, 0); 39 | } 40 | -------------------------------------------------------------------------------- /sdcc/lib_src/strxfrm.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | _strcmp.c - part of string library functions 3 | 4 | Copyright (C) 2011, Philipp Klaus Krause . pkk@spth.de 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #include "string.h" 30 | 31 | size_t strxfrm(char *dest, const char *src, size_t n) 32 | { 33 | strncpy(dest, src, n); 34 | return(strlen(src) + 1); 35 | } 36 | 37 | -------------------------------------------------------------------------------- /sdcc/lib_src/tanf.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | tanf.c - Computes tan(x) where x is a 32-bit float. 3 | 4 | Copyright (C) 2001, 2002, Jesus Calvino-Fraga, jesusc@ieee.org 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | /* Version 1.0 - Initial release */ 30 | 31 | #include 32 | #include 33 | 34 | float tancotf(float x, bool iscotan); 35 | 36 | float tanf(float x) _FLOAT_FUNC_REENTRANT 37 | { 38 | return tancotf(x, 0); 39 | } 40 | -------------------------------------------------------------------------------- /sdcc/lib_src/tolower.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | tolower.c 3 | 4 | Philipp Klaus Krause, philipp@informatik.uni-frankfurt.de 2013 5 | 6 | (c) 2013 Goethe-Universität Frankfurt 7 | 8 | This library is free software; you can redistribute it and/or modify it 9 | under the terms of the GNU General Public License as published by the 10 | Free Software Foundation; either version 2, or (at your option) any 11 | later version. 12 | 13 | This library is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this library; see the file COPYING. If not, write to the 20 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 21 | MA 02110-1301, USA. 22 | 23 | As a special exception, if you link this library with other files, 24 | some of which are compiled with SDCC, to produce an executable, 25 | this library does not by itself cause the resulting executable to 26 | be covered by the GNU General Public License. This exception does 27 | not however invalidate any other reasons why the executable file 28 | might be covered by the GNU General Public License. 29 | -------------------------------------------------------------------------*/ 30 | 31 | #include 32 | 33 | int tolower (int c) 34 | { 35 | return (isupper (c) ? c + ('a' - 'A') : c); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /sdcc/lib_src/toupper.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | toupper.c 3 | 4 | Philipp Klaus Krause, philipp@informatik.uni-frankfurt.de 2013 5 | 6 | (c) 2013 Goethe-Universität Frankfurt 7 | 8 | This library is free software; you can redistribute it and/or modify it 9 | under the terms of the GNU General Public License as published by the 10 | Free Software Foundation; either version 2, or (at your option) any 11 | later version. 12 | 13 | This library is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this library; see the file COPYING. If not, write to the 20 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 21 | MA 02110-1301, USA. 22 | 23 | As a special exception, if you link this library with other files, 24 | some of which are compiled with SDCC, to produce an executable, 25 | this library does not by itself cause the resulting executable to 26 | be covered by the GNU General Public License. This exception does 27 | not however invalidate any other reasons why the executable file 28 | might be covered by the GNU General Public License. 29 | -------------------------------------------------------------------------*/ 30 | 31 | #include 32 | 33 | int toupper (int c) 34 | { 35 | return (islower (c) ? c + ('A' - 'a') : c); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /sdcc/lib_src/wcscmp.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | wcscmp.c - compare strings 3 | 4 | Copyright (C) 2016, Philipp Klaus Krause, pkk@spth.de 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #include 30 | 31 | int wcscmp(const wchar_t *s1, const wchar_t *s2) 32 | { 33 | while(*s1 == *s2 && *s1) 34 | s1++, s2++; 35 | 36 | if(*s1 < *s2) 37 | return(-1); 38 | if(*s1 > *s2) 39 | return(1); 40 | return(0); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /sdcc/lib_src/wcslen.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | wcslen.c - compute the length of a string 3 | 4 | Copyright (C) 2016, Philipp Klaus Krause, pkk@spth.de 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #include 30 | 31 | size_t wcslen(const wchar_t *s) 32 | { 33 | register size_t n = 0; 34 | while (*s) 35 | { 36 | n++; 37 | s++; 38 | } 39 | 40 | return n; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /sdcc/lib_src/wctob.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | wctob.c - convert a wide character to a single-byte character 3 | 4 | Copyright (C) 2016, Philipp Klaus Krause, pkk@spth.de 5 | 6 | This library is free software; you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by the 8 | Free Software Foundation; either version 2, or (at your option) any 9 | later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this library; see the file COPYING. If not, write to the 18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19 | MA 02110-1301, USA. 20 | 21 | As a special exception, if you link this library with other files, 22 | some of which are compiled with SDCC, to produce an executable, 23 | this library does not by itself cause the resulting executable to 24 | be covered by the GNU General Public License. This exception does 25 | not however invalidate any other reasons why the executable file 26 | might be covered by the GNU General Public License. 27 | -------------------------------------------------------------------------*/ 28 | 29 | #include 30 | 31 | #include 32 | 33 | _Static_assert(WEOF & 0xffffff80, "Invalid WEOF"); 34 | 35 | int wctob(wint_t c) 36 | { 37 | if(c & 0xffffff80) 38 | return WEOF; 39 | return c; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /src/ds1302.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongwenjun/stc89c52/016336ebc6c8f2d1d86335b476f4bd2f21209857/src/ds1302.c -------------------------------------------------------------------------------- /src/ds1302.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongwenjun/stc89c52/016336ebc6c8f2d1d86335b476f4bd2f21209857/src/ds1302.h -------------------------------------------------------------------------------- /src/hello.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 49 | 50 | -------------------------------------------------------------------------------- /src/lcd1602a.h: -------------------------------------------------------------------------------- 1 | #ifndef LCD1602A_H_INCLUDED 2 | #define LCD1602A_H_INCLUDED 3 | 4 | /* # 初识LCD1602液晶模块 5 | LCD1602什么意思,LCD表示LiquidCrystal Display 6 | 1602表示一行可以显示16个字符,一共有两行 7 | */ 8 | 9 | #include 10 | 11 | #define uchar unsigned char 12 | #define uint unsigned int 13 | 14 | #define LcdDB P0 // D0-D7 为8位双向数据线,传输数据或命令 15 | sbit LcdRS = P2 ^ 6; // RS 为寄存器选择,高电平时选择数据寄存器,低电平时选择指令寄存 16 | sbit LcdRW = P2 ^ 5; // R/W 为读写信号线, 高电平时进行读操作,低电平时进行写操作 17 | sbit LcdEN = P2 ^ 7; // E 为使能端,当高电平跳变成低电平时,液晶模块执行命令 18 | 19 | void Lcd_Init(); // LCD1602初始化函数 20 | void LcdWrDat(uchar dat); // LCD1602写8bit数据函数 21 | void LcdWrCmd(uchar cmd); // LCD1602写入8bit命令函数 22 | void LcdBusy(); // 检测忙信号,等待 23 | void Lcd_Show(uchar x, uchar y); // 显示坐标 0 行首,1 第2行 24 | 25 | 26 | #endif // LCD1602A_H_INCLUDED 27 | -------------------------------------------------------------------------------- /src/str_7segment/26abc.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 35 | 36 | -------------------------------------------------------------------------------- /src/test_ds1302.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongwenjun/stc89c52/016336ebc6c8f2d1d86335b476f4bd2f21209857/src/test_ds1302.c --------------------------------------------------------------------------------