4687|0

34

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

LPCXpress编译器内存分配bug? (有图有真相!) [复制链接]

用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;这里必死.





串口结果! 如图:



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标准..............

此帖出自NXP MCU论坛
点赞 关注
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/7 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表