C51 COMPILER V9.59.0.0 MAIN 06/10/2021 10:07:38 PAGE 1 C51 COMPILER V9.59.0.0, COMPILATION OF MODULE MAIN OBJECT MODULE PLACED IN .\list\main.obj COMPILER INVOKED BY: C:\Keil_v5\C51\BIN\C51.EXE main.c OMF2 OPTIMIZE(8,SPEED) BROWSE DEBUG PRINT(.\list\main.lst) TABS(2 -) OBJECT(.\list\main.obj) line level source 1 /*---------------------------------------------------------------------*/ 2 /* --- STC MCU Limited ------------------------------------------------*/ 3 /* --- STC 1T Series MCU Demo Programme -------------------------------*/ 4 /* --- Mobile: (86)13922805190 ----------------------------------------*/ 5 /* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/ 6 /* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/ 7 /* --- Web: www.STCMCU.com --------------------------------------------*/ 8 /* --- Web: www.STCMCUDATA.com ---------------------------------------*/ 9 /* --- QQ: 800003751 -------------------------------------------------*/ 10 /* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */ 11 /*---------------------------------------------------------------------*/ 12 13 #include "config.h" 14 #include "STC8A_I2C.h" 15 #include "STC8A_GPIO.h" 16 #include "STC8A_NVIC.h" 17 #include "STC8A_Timer.h" 18 #include "STC8A_Switch.h" 19 #include "STC8A_Soft_I2C.h" 20 21 /************* 功能说明 ************** 22 23 内部集成I2C串行总线控制器做从机模式,SCL->P3.2, SDA->P3.3; 24 IO口模拟I2C做主机模式,SCL->P0.0, SDA->P0.1; 25 通过外部飞线连接 P0.0->P3.2, P0.1->P3.3,实现I2C自发自收功能。 26 27 用STC的MCU的IO方式驱动8位数码管。 28 使用Timer0的16位自动重装来产生1ms节拍,程序运行于这个节拍下,用户修改MCU主时钟频率时,自动定时于1ms. 29 计数器每秒钟加1, 计数范围为0~9999. 30 31 显示效果为: 上电后主机每秒钟发送一次计数数据,并在左边4个数码管上显示发送内容;从机接收到数据后在右边4个数 -码管显示。 32 33 下载时, 选择时钟 24MHz (可以在配置文件"config.h"中修改). 34 35 ******************************************/ 36 37 /************* 本地常量声明 **************/ 38 #define DIS_DOT 0x20 39 #define DIS_BLACK 0x10 40 #define DIS_ 0x11 41 42 u8 code t_display[]={ //标准字库 43 // 0 1 2 3 4 5 6 7 8 9 A B C D E F 44 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71, 45 //black - H J K L N o P U t G Q r M y 46 0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e, 47 0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46}; //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1 48 49 u8 code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //位码 50 51 /************* 本地变量声明 **************/ 52 u8 LED8[8]; //显示缓冲 53 u8 display_index; //显示位索引 C51 COMPILER V9.59.0.0 MAIN 06/10/2021 10:07:38 PAGE 2 54 bit DisplayFlag; 55 bit B_1ms; //1ms标志 56 57 u16 msecond; 58 u16 second; //测试用的秒计数变量 59 u8 tmp[4]; //通用数组 60 61 /************* 本地函数声明 **************/ 62 63 void DisplayScan(void); 64 65 /************* 外部函数和变量声明 *****************/ 66 67 68 69 /******************** IO口配置 ********************/ 70 void GPIO_config(void) 71 { 72 1 P0_MODE_IO_PU(GPIO_Pin_0 | GPIO_Pin_1); //P0.0,P0.1 设置为准双向口 73 1 P3_MODE_IO_PU(GPIO_Pin_3); //P3.3 设置为准双向口 74 1 P6_MODE_IO_PU(GPIO_Pin_All); //P6 设置为准双向口 75 1 P7_MODE_IO_PU(GPIO_Pin_All); //P7 设置为准双向口 76 1 } 77 78 /************************ 定时器配置 ****************************/ 79 void Timer_config(void) 80 { 81 1 TIM_InitTypeDef TIM_InitStructure; //结构定义 82 1 TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload; //指定工作模式, TIM_16BitAutoReload,TIM_16Bit,TI -M_8BitAutoReload,TIM_16BitAutoReloadNoMask 83 1 TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_E -xt 84 1 TIM_InitStructure.TIM_ClkOut = DISABLE; //是否输出高速脉冲, ENABLE或DISABLE 85 1 TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 1000UL); //中断频率, 1000次/秒 86 1 TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLE 87 1 Timer_Inilize(Timer0,&TIM_InitStructure); //初始化Timer0 Timer0,Timer1,Timer2,Timer3,Timer4 88 1 NVIC_Timer0_Init(ENABLE,Priority_0); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Pr -iority_2,Priority_3 89 1 } 90 91 /**************** I2C初始化函数 *****************/ 92 void I2C_config(void) 93 { 94 1 I2C_InitTypeDef I2C_InitStructure; 95 1 I2C_InitStructure.I2C_Mode = I2C_Mode_Slave; //主从选择 I2C_Mode_Master, I2C_Mode_Slave 96 1 I2C_InitStructure.I2C_Enable = ENABLE; //I2C功能使能, ENABLE, DISABLE 97 1 I2C_InitStructure.I2C_SL_MA = ENABLE; //使能从机地址比较功能, ENABLE, DISABLE 98 1 I2C_InitStructure.I2C_SL_ADR = 0x2d; //从机设备地址, 0~127 (0x2d<<1 = 0x5a) 99 1 I2C_Init(&I2C_InitStructure); 100 1 NVIC_I2C_Init(I2C_Mode_Slave,I2C_ESTAI|I2C_ERXI|I2C_ETXI|I2C_ESTOI,Priority_0); //主从模式, I2C_Mode_Mast -er, I2C_Mode_Slave; 中断使能, I2C_ESTAI/I2C_ERXI/I2C_ETXI/I2C_ESTOI/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priori -ty_2,Priority_3 101 1 102 1 I2C_SW(I2C_P33_P32); //I2C_P14_P15,I2C_P24_P25,I2C_P33_P32 103 1 } 104 105 106 /******************** task A **************************/ 107 void main(void) 108 { 109 1 u8 i; 110 1 C51 COMPILER V9.59.0.0 MAIN 06/10/2021 10:07:38 PAGE 3 111 1 GPIO_config(); 112 1 Timer_config(); 113 1 I2C_config(); 114 1 EA = 1; 115 1 116 1 display_index = 0; 117 1 DisplayFlag = 0; 118 1 119 1 for(i=0; i<8; i++) LED8[i] = 0x10; //上电消隐 120 1 121 1 while (1) 122 1 { 123 2 if(DisplayFlag) 124 2 { 125 3 DisplayFlag = 0; 126 3 LED8[4] = I2C_Buffer[0]; 127 3 LED8[5] = I2C_Buffer[1]; 128 3 LED8[6] = I2C_Buffer[2]; 129 3 LED8[7] = I2C_Buffer[3]; 130 3 } 131 2 132 2 if(B_1ms) 133 2 { 134 3 B_1ms = 0; 135 3 DisplayScan(); 136 3 137 3 if(++msecond >= 1000) //1秒到 138 3 { 139 4 msecond = 0; //清1000ms计数 140 4 second++; //秒计数+1 141 4 if(second >= 10000) second = 0; //秒计数范围为0~9999 142 4 143 4 tmp[0] = second / 1000; 144 4 tmp[1] = (second % 1000) / 100; 145 4 tmp[2] = (second % 100) / 10; 146 4 tmp[3] = second % 10; 147 4 LED8[0] = tmp[0]; 148 4 LED8[1] = tmp[1]; 149 4 LED8[2] = tmp[2]; 150 4 LED8[3] = tmp[3]; 151 4 152 4 SI2C_WriteNbyte(0, tmp, 4); 153 4 } 154 3 } 155 2 } 156 1 } 157 158 /********************** 显示扫描函数 ************************/ 159 void DisplayScan(void) 160 { 161 1 P7 = ~T_COM[7-display_index]; 162 1 P6 = ~t_display[LED8[display_index]]; 163 1 if(++display_index >= 8) display_index = 0; //8位结束回0 164 1 } 165 166 167 MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 367 ---- CONSTANT SIZE = 51 ---- C51 COMPILER V9.59.0.0 MAIN 06/10/2021 10:07:38 PAGE 4 XDATA SIZE = ---- ---- PDATA SIZE = ---- ---- DATA SIZE = 17 12 IDATA SIZE = ---- ---- BIT SIZE = 2 ---- EDATA SIZE = ---- ---- HDATA SIZE = ---- ---- XDATA CONST SIZE = ---- ---- FAR CONST SIZE = ---- ---- END OF MODULE INFORMATION. C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)