【发布时间】:2017-02-23 17:32:23
【问题描述】:
我已经使用足够的启动文件在 keil 中为 stm32f103c8t6 板编写了代码。我使用数据表中的信息直接写入内存地址。但 st-link 上传似乎将十六进制文件上传到磨损地址,例如: GPIOA = 0X4001 0800 这是根据 stm 数据表。 但 stlink 显示设备数据范围从 0x0800 0000 到 0x0800 03d4。 我的代码是一个简单的程序,可以让 portA1 的 LED 闪烁。我将 LED 连接到一个 10k 电阻上。当我使用指针指定内存位置时,为什么 stm 分配了错误的地址,或者可能有任何其他错误。代码如下。
void delay(int a);
int main(void)
{
unsigned int* GPIO_A;
GPIO_A = (unsigned int*)0x40010800 ; // Assigning GPIOA to the correct memory location
unsigned int* GPIO_A_CRL;
GPIO_A_CRL = GPIO_A + 0x00 ; // Assigning GPIO_A_CRL to the correct memory location
/*unsigned int* GPIO_A_IDR;
GPIO_A_IDR = GPIO_A + 0X08 ; // Assigning GPIO_A_IDR to the correct memory location */
unsigned int* GPIO_A_BSRR;
GPIO_A_BSRR = GPIO_A + 0X10 ; // Assigning GPIO_A_BSRR to the correct memory location
unsigned int* GPIO_A_BRR;
GPIO_A_BRR = GPIO_A + 0X14 ; // Assigning GPIO_A_BSRR to the correct memory location
unsigned int* RCC_APB2ENR;
RCC_APB2ENR = (unsigned int*)(0x40021000 + 0X18) ; // Assigning RCC_APB2ENR to the correct memory location
*RCC_APB2ENR = 0X04; // Set clock for GPIOA
*GPIO_A_CRL = 0X00008888 ; // Defining pin modes for GPIO_A_CRL
while(1) // infinite loop
{
*GPIO_A_BSRR = 0X00000002; // Set bit 1 to 1
delay(2); // delay
*GPIO_A_BRR = 0x00000002; // reset bit 1 to reset value(0)
delay(2); // delay
}
}
void delay(int a)
{
long b = a*1000000;
for(int i=0;i<b;i++)
{
int c=1;
}
}
【问题讨论】:
-
我建议你把你的问题放在electronics.stackexchange.com
-
我有很多不使用库github.com/dwelch67/stm32_samples/tree/master/STM32F103C8T6 直接访问寄存器的芯片示例,包括如何使用串行引导加载程序或stlink 加载。
-
您是否能够使用您拥有的任何其他示例的工具对这部分进行编程?我想当我第一次得到这些部件时,我必须解锁闪光灯,然后一旦完成,它们就更容易编程了。
-
请注意,如果您进行优化,您的代码中需要大量的 volatile 才能使其正常工作,不要认为这是您的问题。
-
我想以艰苦的方式学习。而且我发现直接访问内存位置更容易