C51 COMPILER V9.59.0.0 MAIN 06/10/2021 17:40:00 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 LARGE 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_GPIO.h" 15 #include "STC8A_UART.h" 16 #include "STC8A_SPI.h" 17 #include "STC8A_BMM.h" 18 #include "STC8A_NVIC.h" 19 #include "STC8A_Switch.h" 20 21 /************* 功能说明 *************** 22 23 UART_BMM, M2M_BMM, SPI_BMM 综合使用演示例程. 24 25 通过串口发送数据给MCU1,MCU1将接收到的数据由SPI发送给MCU2,MCU2再通过串口发送出去. 26 27 通过串口发送数据给MCU2,MCU2将接收到的数据由SPI发送给MCU1,MCU1再通过串口发送出去. 28 29 MCU1/MCU2: UART接收 -> UART Rx BMM -> M2M -> SPI Tx BMM -> SPI发送 30 31 MCU2/MCU1: SPI接收 -> SPI Rx BMM -> M2M -> UART Tx BMM -> UART发送 32 33 MCU1 MCU2 34 |-----------------| |-----------------| 35 | MISO |-----------| MISO | 36 --| TX MOSI |-----------| MOSI TX |-- 37 | SCLK |-----------| SCLK | 38 --| RX SS |-----------| SS RX |-- 39 |-----------------| |-----------------| 40 41 42 下载时, 选择时钟 22.1184MHz (可以在配置文件"config.h"中修改). 43 44 ******************************************/ 45 46 /************* 本地常量声明 **************/ 47 48 #define BUF_LENGTH 107 //n+1 49 50 /************* 本地变量声明 **************/ 51 52 bit SpiTxFlag; 53 bit SpiRxFlag; 54 bit BmmTx1Flag; C51 COMPILER V9.59.0.0 MAIN 06/10/2021 17:40:00 PAGE 2 55 bit BmmRx1Flag; 56 57 bit u2sFlag; //UART to SPI 58 bit s2uFlag; //SPI to UART 59 60 bit SpiSendFlag; 61 bit UartSendFlag; 62 63 u8 xdata UartTxBuffer[256]; 64 u8 xdata UartRxBuffer[256]; 65 u8 xdata SpiTxBuffer[256]; 66 u8 xdata SpiRxBuffer[256]; 67 68 69 /************* 本地函数声明 **************/ 70 71 72 /************* 外部函数和变量声明 *****************/ 73 74 75 /******************** IO口配置 ********************/ 76 void GPIO_config(void) 77 { 78 1 P2_MODE_IO_PU(GPIO_Pin_All); //P2 设置为准双向口 79 1 P3_MODE_IO_PU(GPIO_Pin_0 | GPIO_Pin_1); //P3.0,P3.1 设置为准双向口 80 1 81 1 SPI_SW(SPI_P22_P23_P24_P25); //SPI_P12_P13_P14_P15,SPI_P22_P23_P24_P25,SPI_P74_P75_P76_P77,SPI_P35_P34_P3 -3_P32 82 1 SPI_SS_2 = 1; 83 1 } 84 85 /******************** UART配置 ********************/ 86 void UART_config(void) 87 { 88 1 COMx_InitDefine COMx_InitStructure; //结构定义 89 1 90 1 COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART -_9bit_BRTx 91 1 COMx_InitStructure.UART_BRT_Use = BRT_Timer1; //使用波特率, BRT_Timer1, BRT_Timer2 (注意: 串口2固定使 -用BRT_Timer2) 92 1 COMx_InitStructure.UART_BaudRate = 115200ul; //波特率, 一般 110 ~ 115200 93 1 COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE 94 1 COMx_InitStructure.BaudRateDouble = DISABLE; //波特率加倍, ENABLE或DISABLE 95 1 UART_Configuration(UART1, &COMx_InitStructure); //初始化串口1 UART1,UART2,UART3,UART4 96 1 NVIC_UART1_Init(ENABLE,Priority_1); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Pri -ority_2,Priority_3 97 1 } 98 99 /******************** SPI 配置 ********************/ 100 void SPI_config(void) 101 { 102 1 SPI_InitTypeDef SPI_InitStructure; 103 1 104 1 SPI_InitStructure.SPI_Enable = ENABLE; //SPI启动 ENABLE, DISABLE 105 1 SPI_InitStructure.SPI_SSIG = ENABLE; //片选位 ENABLE, DISABLE 106 1 SPI_InitStructure.SPI_FirstBit = SPI_MSB; //移位方向 SPI_MSB, SPI_LSB 107 1 SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; //主从选择 SPI_Mode_Master, SPI_Mode_Slave 108 1 SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //时钟相位 SPI_CPOL_High, SPI_CPOL_Low 109 1 SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //数据边沿 SPI_CPHA_1Edge, SPI_CPHA_2Edge 110 1 SPI_InitStructure.SPI_Speed = SPI_Speed_64; //SPI速度 SPI_Speed_4, SPI_Speed_16, SPI_Speed_64, S -PI_Speed_2 111 1 SPI_Init(&SPI_InitStructure); C51 COMPILER V9.59.0.0 MAIN 06/10/2021 17:40:00 PAGE 3 112 1 NVIC_SPI_Init(DISABLE,Priority_0); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Prio -rity_2,Priority_3 113 1 } 114 115 /******************** BMM 配置 ********************/ 116 void BMM_config(void) 117 { 118 1 BMM_M2M_InitTypeDef BMM_M2M_InitStructure; //结构定义 119 1 BMM_SPI_InitTypeDef BMM_SPI_InitStructure; //结构定义 120 1 BMM_UART_InitTypeDef BMM_UART_InitStructure; //结构定义 121 1 122 1 //---------------------------------------------- 123 1 BMM_UART_InitStructure.BMM_TX_Length = BUF_LENGTH; //BMM传输总字节数 (0~255) + 1 124 1 BMM_UART_InitStructure.BMM_TX_Buffer = UartTxBuffer; //发送数据存储地址 125 1 BMM_UART_InitStructure.BMM_RX_Length = BUF_LENGTH; //BMM传输总字节数 (0~255) + 1 126 1 BMM_UART_InitStructure.BMM_RX_Buffer = UartRxBuffer; //接收数据存储地址 127 1 BMM_UART_InitStructure.BMM_TX_Enable = ENABLE; //BMM使能 ENABLE,DISABLE 128 1 BMM_UART_InitStructure.BMM_RX_Enable = ENABLE; //BMM使能 ENABLE,DISABLE 129 1 BMM_UART_Inilize(UART1, &BMM_UART_InitStructure); //初始化 130 1 131 1 NVIC_BMM_UART1_Tx_Init(ENABLE,Priority_0,Priority_0); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priorit -y_0~Priority_3; 总线优先级(低到高) Priority_0~Priority_3 132 1 NVIC_BMM_UART1_Rx_Init(ENABLE,Priority_0,Priority_0); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priorit -y_0~Priority_3; 总线优先级(低到高) Priority_0~Priority_3 133 1 BMM_UR1R_TRIG(); //触发UART1接收功能 134 1 135 1 //---------------------------------------------- 136 1 BMM_M2M_InitStructure.BMM_Enable = ENABLE; //BMM使能 ENABLE,DISABLE 137 1 BMM_M2M_InitStructure.BMM_Length = BUF_LENGTH; //BMM传输总字节数 (0~255) + 1 138 1 BMM_M2M_InitStructure.BMM_Tx_Buffer = UartRxBuffer; //发送数据存储地址 139 1 BMM_M2M_InitStructure.BMM_Rx_Buffer = SpiTxBuffer; //接收数据存储地址 140 1 BMM_M2M_InitStructure.BMM_SRC_Dir = M2M_ADDR_INC; //数据源地址改变方向 M2M_ADDR_INC,M2M_ADDR_DEC 141 1 BMM_M2M_InitStructure.BMM_DEST_Dir = M2M_ADDR_INC; //数据目标地址改变方向 M2M_ADDR_INC,M2M_ADDR_DEC 142 1 BMM_M2M_Inilize(&BMM_M2M_InitStructure); //初始化 143 1 NVIC_BMM_M2M_Init(ENABLE,Priority_0,Priority_0); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0~P -riority_3; 总线优先级(低到高) Priority_0~Priority_3 144 1 145 1 //---------------------------------------------- 146 1 BMM_SPI_InitStructure.BMM_Enable = DISABLE; //BMM使能 ENABLE,DISABLE 147 1 BMM_SPI_InitStructure.BMM_Tx_Enable = ENABLE; //BMM发送数据使能 ENABLE,DISABLE 148 1 BMM_SPI_InitStructure.BMM_Rx_Enable = ENABLE; //BMM接收数据使能 ENABLE,DISABLE 149 1 BMM_SPI_InitStructure.BMM_Length = BUF_LENGTH; //BMM传输总字节数 (0~255) + 1 150 1 BMM_SPI_InitStructure.BMM_Tx_Buffer = SpiTxBuffer; //发送数据存储地址 151 1 BMM_SPI_InitStructure.BMM_Rx_Buffer = SpiRxBuffer; //接收数据存储地址 152 1 BMM_SPI_InitStructure.BMM_SS_Sel = SPI_SS_P22; //自动控制SS脚选择 SPI_SS_P12,SPI_SS_P22,SPI_SS_P74,SPI -_SS_P35 153 1 BMM_SPI_InitStructure.BMM_AUTO_SS = DISABLE; //自动控制SS脚使能 ENABLE,DISABLE 154 1 BMM_SPI_Inilize(&BMM_SPI_InitStructure); //初始化 155 1 SET_BMM_SPI_CR(BMM_ENABLE | SPI_TRIG_S | CLR_FIFO); //bit7 1:使能 SPI_BMM, bit5 1:开始 SPI_BMM 从机模式, -bit0 1:清除 SPI_BMM FIFO 156 1 NVIC_BMM_SPI_Init(ENABLE,Priority_0,Priority_0); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0~P -riority_3; 总线优先级(低到高) Priority_0~Priority_3 157 1 } 158 159 void M2M_UART_SPI(void) 160 { 161 1 BMM_M2M_CLR_STA(); 162 1 SET_M2M_TX_FIFO(UartRxBuffer); 163 1 SET_M2M_RX_FIFO(SpiTxBuffer); 164 1 BMM_M2M_TRIG(); 165 1 } 166 C51 COMPILER V9.59.0.0 MAIN 06/10/2021 17:40:00 PAGE 4 167 void M2M_SPI_UART(void) 168 { 169 1 BMM_M2M_CLR_STA(); 170 1 SET_M2M_TX_FIFO(SpiRxBuffer); 171 1 SET_M2M_RX_FIFO(UartTxBuffer); 172 1 BMM_M2M_TRIG(); 173 1 } 174 175 void UART_BMM_Tx(void) 176 { 177 1 BMM_UR1T_TRIG(); 178 1 } 179 180 void UART_BMM_Rx(void) 181 { 182 1 BMM_UR1R_TRIG(); 183 1 } 184 185 void SPI_BMM_Master(void) 186 { 187 1 SET_BMM_SPI_CR(0); 188 1 _nop_(); 189 1 _nop_(); 190 1 _nop_(); 191 1 _nop_(); 192 1 _nop_(); 193 1 _nop_(); 194 1 SPI_SS_2 = 0; 195 1 SPCTL = 0xd2; //使能 SPI 主机模式,忽略SS引脚功能 196 1 SET_BMM_SPI_CR(BMM_ENABLE | SPI_TRIG_M); //bit7 1:使能 SPI_BMM, bit6 1:开始 SPI_BMM 主机模式 197 1 } 198 199 void SPI_BMM_Slave(void) 200 { 201 1 SET_BMM_SPI_CR(0); 202 1 _nop_(); 203 1 _nop_(); 204 1 _nop_(); 205 1 _nop_(); 206 1 _nop_(); 207 1 _nop_(); 208 1 SPCTL = 0x42; //重新设置为从机待机 209 1 SET_BMM_SPI_CR(BMM_ENABLE | SPI_TRIG_S); //bit7 1:使能 SPI_BMM, bit5 1:开始 SPI_BMM 从机模式 210 1 } 211 212 /******************** task A **************************/ 213 void main(void) 214 { 215 1 GPIO_config(); 216 1 UART_config(); 217 1 SPI_config(); 218 1 BMM_config(); 219 1 EA = 1; 220 1 221 1 printf("STC8A8K64D4 UART1-BMM-SPI互为主从透传程序.\r\n"); 222 1 223 1 while (1) 224 1 { 225 2 //UART接收 -> UART BMM -> SPI BMM -> SPI发送 226 2 if(BmmRx1Flag) 227 2 { 228 3 BmmRx1Flag = 0; C51 COMPILER V9.59.0.0 MAIN 06/10/2021 17:40:00 PAGE 5 229 3 u2sFlag = 1; 230 3 M2M_UART_SPI(); //UART Memory -> SPI Memory 231 3 } 232 2 233 2 if(SpiSendFlag) 234 2 { 235 3 SpiSendFlag = 0; 236 3 UART_BMM_Rx(); //UART Recive Continue 237 3 SPI_BMM_Master(); //SPI Send Memory 238 3 } 239 2 240 2 if(SpiTxFlag) 241 2 { 242 3 SpiTxFlag = 0; 243 3 SPI_BMM_Slave(); //SPI Slave Mode 244 3 } 245 2 246 2 247 2 //SPI接收 -> SPI BMM -> UART BMM -> UART发送 248 2 if(SpiRxFlag) 249 2 { 250 3 SpiRxFlag = 0; 251 3 s2uFlag = 1; 252 3 M2M_SPI_UART(); //SPI Memory -> UART Memory 253 3 } 254 2 255 2 if(UartSendFlag) 256 2 { 257 3 UartSendFlag = 0; 258 3 SPI_BMM_Slave(); //SPI Slave Mode 259 3 UART_BMM_Tx(); //UART Send Memory 260 3 } 261 2 } 262 1 } 263 264 265 MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 587 ---- CONSTANT SIZE = 45 ---- XDATA SIZE = 1024 42 PDATA SIZE = ---- ---- DATA SIZE = ---- ---- IDATA SIZE = ---- ---- BIT SIZE = 8 ---- EDATA SIZE = ---- ---- HDATA SIZE = ---- ---- XDATA CONST SIZE = ---- ---- FAR CONST SIZE = ---- ---- END OF MODULE INFORMATION. C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)