Hi,
I'm using the STM32F103ZE uC on the MCBSTM32E development board. I tried the example programs, they all compile and download and run fine, but they are NOT SIMPLE. They use a lot of Library files etc, etc, etc. I just try to make some ports go high or low.
The example GPIO program states: - Enable the clock signal for the GPIO. - Configure the Alternate Function to use a GPIO (usually standard after reset). - Configure the GPIO pins as input or output. - Set remaining parameters like speed, pull-up/down. - Enable the GPIO. - Read from or write to the GPIO.
Can someone give me a simple (and I mean REALLY SIMPLE) program to control just one single GPIO port?
My questions to activate some port: - do I need something to know about internal clock signals? - do I need some startup file?
Thanks,
Henk
Me again,
I forgot to tell:
the only additional file needed is: STM32F10x.s
Well, now that I'm on 'steam':
I've modified my program so that is runs using the official added STMicroelectronics header files. Then this program gets even smaller.
I hope other newbies will appreciate this.
All you need (except love) is: - the GPIO header file - the startup file - init the GPIO clock - set the GPIO port to Output - write data to the port
So this is the real answer I was looking for. . .
//------------------------------------------------------------ // // Very simpe program to control the 8 PB08-PB15 Leds on the // Keil MCBSTM32E board using a STM32F103ZE micro controller. // // This program will show a toggling 0xAAAA / 0x5555 pattern // on the onboard Leds appr. each 300ms. // // Copyright! Non of this shall be reproduced in any way! ( ;-) ) // // 2010.08.06 // Henk van Winkoop // The Netherlands // // The only additional files needed is: STM32F10x.s // // This version uses the STMicroelectronics manufacturer // setup header files. // //------------------------------------------------------------ //include the file that defines all kind of GPIO stuff #include <stm32f10x_gpio.h> //============================================================ // MAIN //============================================================ int main(void){ //------------------------------ // declare variables //------------------------------ //delay counter variable int i; //------------------------------ // init the micro controller //------------------------------ //enable the GPIO clock for port GPIOB RCC->APB2ENR|=0x0008; //set all PB08-PB15 ports as output GPIOB->CRH=0x33333333; //------------------------------ // main loop //------------------------------ //forever do... for(;;){ //roughly wait 300ms for(i=0;i<0x40000;i++){ }//for //switch only all even-numbered Leds on GPIOB->ODR=0x0000aaaa; //roughly wait 300ms for(i=0;i<0x40000;i++){ }//for //switch only all odd-numbered Leds on GPIOB->ODR=0x00005555; }//for }//main
Thanks for listening...
How long will your delay take if you try to change the optimization level?
Might there be a problem?
Is there any good way to overcome this problem?
as far as I know you should add some assembler nop() instruction inside a 'for' loop to be sure this code is not optimized into the recycle bin. This is the way to do it with 8051 or Atmel AVR uC's so I assume Cortex will have some identical code.
www.8052.com/.../162556
A nop() only adds a delay if the compiler knows that the instruction time of the nop() is an important side effect. If the compiler doesn't, then the nop is "nothing" and can be removed.
Besides - even if you do have a nop instruction in the loop, the optimization level will change the number of machine cycles neede for the loop primitives which means that the total time will change depending on what code that is generated for the loop.
A nop() can be good when you need sub-microsecond delays. Maybe get 20ns extra delay between activating two GPIO signals (under the assumption that the compiler knows and honors the "side effect" of the nop).
Whenever you want longer delays, you should look into the timer system. Either busy-looping while waiting for a timer register to reach a certain value, or do something else while waiting for the timer to generate an interrupt.
The only time you may consider software delay loops, is if you write the delay loop in assembler. And then you better avoid trying to inline that assembler inside a C function - write the delay() function fully in assembler.
Hi Both, Can above program runs well on STM3210E-EVAl board? If there are any changes are to be done then what are those?
Thanks and Regards, Avinash S B