Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *****************************************************************************
- **
- ** File : main.c
- **
- ** Abstract : main function.
- **
- ** Functions : main
- **
- ** Environment : Atollic TrueSTUDIO/STM32
- ** STMicroelectronics STM32F10x Standard Peripherals Library
- **
- ** Distribution: The file is distributed �as is,� without any warranty
- ** of any kind.
- **
- ** (c)Copyright Atollic AB.
- ** You may use this file as-is or modify it according to the needs of your
- ** project. Distribution of this file (unmodified or modified) is not
- ** permitted. Atollic AB permit registered Atollic TrueSTUDIO(R) users the
- ** rights to distribute the assembled, compiled & linked contents of this
- ** file as part of an application binary file, provided that it is built
- ** using the Atollic TrueSTUDIO(R) toolchain.
- **
- **
- *****************************************************************************
- */
- /* Includes */
- #include <stddef.h>
- #include "stm32f10x.h"
- #include "stm32f10x_gpio.h"
- #include "main.h"
- #include "SysTickHandler.h"
- #include "abstraction.h"
- //#include "STM32vldiscovery.h"
- /* Private typedef */
- typedef enum{
- toggle,
- togglevar,
- blink,
- blinkvar
- }ledstate_e;
- /* Private define */
- /* Private macro */
- /* Private variables */
- void waitconst ( int del );
- void waitvar ( void );
- void initled(void);
- void initbutton ( void );
- /* Private function prototypes */
- /* Private functions */
- void SwitchMode ( ledstate_e * state ){
- static unsigned int pushedButton = 0;
- unsigned int pressed = 0;
- pressed = GPIO_ReadInputDataBit(B1_BUTTON_PORT, B1_BUTTON_PIN);
- if( pressed != pushedButton && pressed>0){
- switch(*state){
- case togglevar : *state = toggle; break;
- case toggle : *state = blink; break;
- case blink : *state = blinkvar; break;
- case blinkvar: *state = togglevar; break;
- default: *state = blink; break;
- }
- }
- pushedButton=pressed;
- return;
- }
- /**
- **===========================================================================
- **
- ** Abstract: main program
- **
- **===========================================================================
- */
- int main(void){
- initled();
- initSysTickHandler(1);
- ledstate_e mode=blinkvar;
- ledstate_e modeold;
- while (1){
- SwitchMode( & mode );
- //set initial states for toggle
- if(mode != modeold){
- if( mode == blink || mode == blinkvar){
- GPIO_SetBits(G_LED_PORT,G_LED_PIN);
- GPIO_SetBits(B_LED_PORT,B_LED_PIN);
- }
- else{
- GPIO_ResetBits(B_LED_PORT,B_LED_PIN);
- GPIO_ResetBits(G_LED_PORT,G_LED_PIN);
- }
- }
- modeold = mode;
- //get wait states...
- switch(mode){
- case togglevar : waitvar(); break;
- case toggle : waitconst(1000); break;
- case blink : waitconst(1000); break;
- case blinkvar: waitvar(); break;
- default: mode = blink; break;
- }
- //toggle LEDs, because the initial states configures the mode of this leds,
- //the ports only have to be toggled
- G_LED_PORT->ODR ^= G_LED_PIN;
- B_LED_PORT->ODR ^= B_LED_PIN;
- }
- return -1;
- }
- void initbutton ( void ){
- GPIO_InitTypeDef button_struct;
- EXTI_InitTypeDef exti_struct;
- NVIC_InitTypeDef nvic_struct;
- RCC_APB2PeriphClockCmd(B1_BUTTON_RCC,ENABLE); //enable clock for the GPIO
- //configure Button-Pin
- button_struct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- button_struct.GPIO_Pin=B1_BUTTON_PIN;
- button_struct.GPIO_Speed=GPIO_Speed_2MHz;
- //Save Settings
- GPIO_Init(B1_BUTTON_PORT,&button_struct);//enable GPIO_Port
- //Configure Specialfunction for pin!
- RCC_APB2PeriphClockCmd(B1_AFIO_RCC,ENABLE); // enable Specialfunction for Interrupt-Handling
- //Pin is located at PA0 -> therfore EXTI0 is used for the Interrupt.
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
- exti_struct.EXTI_Line=EXTI_Line0;
- exti_struct.EXTI_Mode=EXTI_Mode_Interrupt;
- exti_struct.EXTI_Trigger=EXTI_Trigger_Rising;
- exti_struct.EXTI_LineCmd=ENABLE;
- nvic_struct.NVIC_IRQChannel=EXTI0_IRQn;
- nvic_struct.NVIC_IRQChannelCmd=ENABLE;
- nvic_struct.NVIC_IRQChannelPreemptionPriority=0x0F;
- nvic_struct.NVIC_IRQChannelSubPriority=0x0F;
- EXTI_Init(&exti_struct);
- NVIC_Init(&nvic_struct);
- return;
- }
- void initled ( void ){
- GPIO_InitTypeDef led_struct;
- RCC_APB2PeriphClockCmd(G_LED_RCC,ENABLE);
- led_struct.GPIO_Mode=GPIO_Mode_Out_PP;
- led_struct.GPIO_Pin=G_LED_PIN;
- led_struct.GPIO_Speed=GPIO_Speed_2MHz;
- GPIO_Init(G_LED_PORT, &led_struct);
- RCC_APB2PeriphClockCmd(B_LED_RCC,ENABLE);
- led_struct.GPIO_Mode=GPIO_Mode_Out_PP;
- led_struct.GPIO_Pin=B_LED_PIN;
- led_struct.GPIO_Speed=GPIO_Speed_2MHz;
- GPIO_Init(B_LED_PORT, &led_struct);
- return;
- }
- void waitconst ( int del){
- delay(del);
- return;
- }
- void waitvar ( void ){
- static int wait = 100;
- static int mode=0;
- delay(wait);
- if(mode==0){
- wait-=50;
- if(wait<100)
- mode=1;
- }
- else{
- wait+=50;
- if(wait>1500)
- mode=0;
- }
- return;
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /*
- * Minimal __assert_func used by the assert() macro
- * */
- void __assert_func(const char *file, int line, const char *func, const char *failedexpr){
- while(1)
- {}
- }
- /*
- * Minimal __assert() uses __assert__func()
- * */
- void __assert(const char *file, int line, const char *failedexpr){
- __assert_func (file, line, NULL, failedexpr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement