LPCXpress编译器内存分配bug? (有图有真相!)

2019-03-25 20:43发布

用lpcxpress编译程序如下.
#include "LPC11xx.h"
int main(void)
{
        SystemInit();
        unsigned char aaa[1000];
        unsigned long p;
#define BAUDRATE 115200         NVIC_DisableIRQ(UART_IRQn);
        LPC_IOCON->IO1_6 = 0xc1;        // UART RXD
        LPC_IOCON->IO1_7 = 0xc1;        // UART TXD
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);        // Enable UART clock
        LPC_SYSCON->UARTCLKDIV = 0x1;                // divided by 1
        LPC_UART->LCR = 0x83;        // 8 bits, no Parity, 1 Stop bit
        uint32_t Fdiv;
        Fdiv = (((SystemCoreClock/LPC_SYSCON->SYSAHBCLKDIV)/LPC_SYSCON->UARTCLKDIV)/16)/BAUDRATE;
        LPC_UART->DLM = Fdiv/256;
        LPC_UART->DLL = Fdiv%256;
        LPC_UART->LCR = 0x03;                // DLAB = 0
        LPC_UART->FCR = 0x07;                // Enable and reset TX and RX FIFO.
        Fdiv = LPC_UART->LSR;                // Read to clear the line status.         p=&p;
        LPC_UART->THR = p;LPC_UART->THR = p>>8;LPC_UART->THR = p>>16;LPC_UART->THR = p>>24;
        unsigned char i;
        p=&i;
        LPC_UART->THR = p;LPC_UART->THR = p>>8;LPC_UART->THR = p>>16;LPC_UART->THR = p>>24;
        unsigned char i1;
        p=&i1;
        LPC_UART->THR = p;LPC_UART->THR = p>>8;LPC_UART->THR = p>>16;LPC_UART->THR = p>>24;
        unsigned char i2;
        p=&i2;
        LPC_UART->THR = p;LPC_UART->THR = p>>8;LPC_UART->THR = p>>16;LPC_UART->THR = p>>24;
        LPC_UART->THR = i;LPC_UART->THR = i1;LPC_UART->THR = i2;         LPC_IOCON->IO2_6 = 0x000000c0;        // Hi-Z
        LPC_GPIO3->DIR = 0x00000030;        // GPIO3_4, GPIO3_5 output. now 1
        LPC_GPIO3->MASKED_ACCESS[0x030] = 0x00000000;        // GPIO3_5, GPIO3_4 output 0
        for (;;) {
                if (LPC_GPIO2->MASKED_ACCESS[0x040] == 0x00000000) LPC_GPIO3->MASKED_ACCESS[0x010] = 0xffffffff;        // GPIO3_4 output 1
                else LPC_GPIO3->MASKED_ACCESS[0x010] = 0x00000000;        // GPIO3_4 output 0
        }
}


重点在这里:

  •      p=&p;
  •         LPC_UART->THR = p;LPC_UART->THR = p>>8;LPC_UART->THR = p>>16;LPC_UART->THR = p>>24;
  •         unsigned char i;
  •         p=&i;
  •         LPC_UART->THR = p;LPC_UART->THR = p>>8;LPC_UART->THR = p>>16;LPC_UART->THR = p>>24;
  •         unsigned char i1;
  •         p=&i1;
  •         LPC_UART->THR = p;LPC_UART->THR = p>>8;LPC_UART->THR = p>>16;LPC_UART->THR = p>>24;
  •         unsigned char i2;
  •         p=&i2;
  •         LPC_UART->THR = p;LPC_UART->THR = p>>8;LPC_UART->THR = p>>16;LPC_UART->THR = p>>24;
  •         LPC_UART->THR = i;LPC_UART->THR = i1;LPC_UART->THR = i2;

检测变量的地址. 通过串口打印




运行, 跑到 LPC_UART->THR = i;LPC_UART->THR = i1;LPC_UART->THR = i2;这里必死.





串口结果! 如图:

1009190919e1ebd1e4accd563b.JPG

pc1111的ram地址是0x1000 0000开始, 到0x1000 0800结束!

关键的问题是: 变量的地址变化: D0-D7-D6-D5 后边分配变量跑死.....

用keil编译直接提示:
: error:  #268: declaration may not appear after executable statement in block


IAR也是要求变量放在开头的.....

难道MCU的C都是要求这么诡异的????

查到

Prior to the 1999version of the C language standard, declarations are not allowed afterexecutable statements in a block.  But since 1999 they are, so thecode is quite legal if you are compiling with the current C standard.


难道MCU的编译器还是C89标准..............

此帖出自小平头技术问答
0条回答

一周热门 更多>