void Global_Init(void) { // Initialize system SystemInit(); // Initialize button and LEDs init_GPIO(); // Initialize delay TM_DELAY_Init(); // Init sounds InitSounds(); // Initialize accelerometer AccInit(); // Initialize USB, IO, SysTick, and all those other things you do in the morning init(); //timerINit Timer_Init(); }
int main(void){ // initialize the GPIO pins we need init_GPIO(); /* This flashes the LEDs on the board once * Two registers are used to set the pins (pin level is VCC) * or to reset the pins (pin level is GND) * * BSRR stands for bit set/reset register * it is seperated into a high and a low word (each of 16 bit size) * * A logical 1 in BSRR will set the pin and a logical 1 in BRR will * reset the pin. A logical 0 in either register has no effect */ GPIOC->ODR = 0x0300; // set PD8 and PC9 while (1){ GPIOC->ODR |= 0x0100; // set PC8 without affecting other bits if (GPIOA->IDR & 0x0001)// check if PA0 button is pressed { GPIOC->ODR |= 0x0200;// turn on PC9 } Delay(1000000L); // wait a little GPIOC->ODR &= ~0x0300; // clear PC8 and PC9 Delay(1000000L); // wait a little } }
void soc_family_init(void) { init_sysclk(); init_exception_prio(); /* Enable peripheral clocks. */ init_peripheral_clocks(); /* Set up IO pin direction and mode registers. */ init_io(); #ifdef SYSTEM_SOC_CORTEXM3_STM32_GPIO_USED /* Set up GPIO. */ init_GPIO(); #endif #ifdef SYSTEM_SOC_CORTEXM3_STM32_USART_USED /* Set up USART. */ init_USART(); #endif #ifdef SYSTEM_SOC_CORTEXM3_STM32_SPI_USED /* Set up SPI. */ init_SPI(); #endif }
int main(void) { ///////// Initial Portion ///////////////////////// init_USART1(9600); // initialize USART1 @ 9600 baud SystemCoreClockUpdate(); /* Get Core Clock Frequency */ if (SysTick_Config(SystemCoreClock / 1000)) { /* SysTick 1 msec interrupts */ while (1); /* Capture error */ } USART_puts(USART1, "Init 1 complete! Hello World!rn"); // just send a message to indicate that it works //Init GPIO for Leds on board and button input init_GPIO(); /////////////////- Loop - //////////////////////////////////// while (1){ // You can do whatever you want in here USART_puts(USART1, "Init 2 complete! Hello World! \r \n"); // just send a message to indicate that it works //GPIO_ToggleBits(GPIOD, GPIO_Pin_15 | GPIO_Pin_14 | GPIO_Pin_13| GPIO_Pin_12 ); GPIO_ToggleBits(GPIOD, GPIO_Pin_12 ); //Test Leds on board Delay(200); GPIO_ToggleBits(GPIOD, GPIO_Pin_13 ); //Test Leds on board Delay(200); GPIO_ToggleBits(GPIOD, GPIO_Pin_14 ); //Test Leds on board Delay(1000); } ////////////////////////////////////////////////////////// }
int main(void) { // initialize the GPIO pins we need initClock(); init_GPIO(); init_ADC(); init_DAC(); init_GYACC(); usb::init(); Herkulex hercules; GPIOE->ODR |= 0xC000; Tools::Delay(Tools::DELAY_AROUND_1S / 2); GPIOE->ODR &= ~0xC000; enable_ADC_watchdog(2760, 3870); // ~6.9V -- ~9.5V (V33 = 3.41V) hercules.setTorque(DEFAULT_ID, TORQUE_ON); unsigned debounce = 10000, oldb=0; unsigned g = GYACC_txrx(USE_GYRO, 0x8F00); unsigned a = GYACC_txrx(USE_ACC, 0xA000); for(;;) { char p[4]; uint32_t r = usb::read(p); for (int i=0; i < r; ++i) p[i] += 2; usb::write(p, r); if (r) GPIOE->ODR += 0x4000; if (!debounce--) { const unsigned b = ~GPIOC->IDR; const unsigned p = (b ^ oldb) & b; if (p & 2) { GPIOE->ODR ^= 0x8000; hercules.positionControl(DEFAULT_ID, 512 + 300, 40, 0); } else if (p & 4) { GPIOE->ODR ^= 0x4000; hercules.positionControl(DEFAULT_ID, 512, 40, 0); } else if (p & 8) { GPIOE->ODR -= 0x4000; hercules.positionControl(DEFAULT_ID, 512 - 300, 40, 0); } oldb = b; debounce = 10000; } } }
void serieport_init(void) { RCC_PeriphClockCmd(RCC_USART6, ENABLE); RCC_PeriphClockCmd(RCC_GPIOC, ENABLE); init_GPIO(GPIOC, GPIO_Pin_6 | GPIO_Pin_7, GPIO_Mode_AF, GPIO_Fast_Speed, GPIO_OType_PP, GPIO_PuPd_UP); GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6); GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6); init_SCI(USART6, 9600, USART_WordLength_8b, USART_StopBits_1, USART_Parity_No, USART_Mode_Rx | USART_Mode_Tx, USART_HardwareFlowControl_None); }
/*============================================================================== * Function: All_Init * * Description: This function initializes all the peripherals. * ==============================================================================*/ void All_Init(void){ initModesAndClock(); Init_PIT(); Init_ISR(); init_GPIO(); Init_PIT_CH0(VAL_TMR_0); }
/*********************************************************************** * PURPOSE: Initialize all required modules for camera measurement * * INPUTS: * RETURNS: ***********************************************************************/ void InitCamera(void) { line = &buffer0[0]; init_GPIO(); // For CLK and SI output on GPIO init_FTM2(); // To generate CLK, SI, and trigger ADC init_ADC0(); init_PIT(); // To trigger camera read based on integration time }
void ad_init() { RCC_PeriphClockCmd(RCC_ADC1, ENABLE); RCC_PeriphClockCmd(RCC_GPIOC, ENABLE); init_GPIO(GPIOC, GPIO_Pin_0, GPIO_Mode_AN, GPIO_Fast_Speed, GPIO_OType_PP, GPIO_PuPd_NOPULL); initCommon_ADC(ADC_Mode_Independent, ADC_Prescaler_Div2, ADC_DMAAccessMode_Disabled, ADC_TwoSamplingDelay_5Cycles); init_ADC(ADC_Resolution_12b, DISABLE, ENABLE, ADC_ExternalTrigConvEdge_None, ADC_ExternalTrigConv_T1_CC1, ADC_DataAlign_Right, 1); ADC_Cmd(ADC1,ENABLE); ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_144Cycles); }
void init(void) { //init_RCC(); /* RCC_Configuration */ RCC_Configuration(); init_GPIO(); init_SPI(); NVIC_Configuration(); USART_Configuration(); init_printf(0,putc); }
int main(void) { SysTick_Config(SystemCoreClock/1000); // Every 1 ms init_GPIO(); init_USART(); while(1) { GPIO_ToggleBits(GPIOA, GPIO_Pin_5); USART_SendData(USART2, 'a'); delay_ms(500); } }
int main(void) { SystemInit(); init_usart(9600); init_GPIO(); init_timer(); NEC_Init(); while (1) { } return 0; }
int main(void) { setSysTick(); init_GPIO(); while(1){ //if(GPIOA->IDR & 0x01) if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) GPIO_ToggleBits(GPIOD, GPIO_Pin_14); Delay(200); } return 0; }
//==================================================================== // MAIN FUNCTION //==================================================================== void main (void){ init_LCD(); init_GPIO(); //Get operating CLK freq RCC_ClocksTypeDef CLK; RCC_GetClocksFreq(&CLK); sysclock= (CLK.SYSCLK_Frequency)/((float)(pow(10,6))); //Sys clocks in MHz sprintf(lcdstring,"CLK %d MHz",sysclock); lcd_putstring(lcdstring); lcd_command(LINE_TWO); lcd_putstring("Servo Step Test, SW0"); //Init procedure waits for SW0 while(GPIO_ReadInputData(GPIOA)&GPIO_IDR_0){} lcd_command(CLEAR); init_ADC(); init_EXTI(); init_USART1(); init_TIM2(); init_TIM3(); init_TIM14(); init_TIM17(); for(;;){ while(TIM3OC2<=3800){ TIM3OC2++; TIM_SetCompare2(TIM3,TIM3OC2); for(int x=0;x<=255;x++){ for(int y=0;y<=255;y++){} } } lcd_command(CURSOR_HOME); sprintf(lcdstring,"ADC:%d ",(int) ADCval); lcd_putstring(lcdstring); lcd_command(LINE_TWO); sprintf(lcdstring,"TIM:%d ",(int) TIM2->CCR3);//TIMcmp); //TIM_SetCompare2(TIM3,(int)(64000*((0.9+1.1*ADCval/2047.0)/20.0))); lcd_putstring(lcdstring); } } // End of main
int main(void) { RCC_Configuration(); init_GPIO(); delay_init(); NVIC_cfg();//配置中断 MBRTU_ini(0x01); //Tim3初始化 init_TIM3(); wd_init(); TM7711_init(); init_serial();//串口初始化 // TIM3_Start(0); MeasurePoll_init(); while(1) { MBRTUPoll(); MeasurePoll(); } }
/****************************************************************************** ** Function name: main ** ** Description: Program entry point. Contains initializations and menu loop ** ** Parameters: None ** Returned value: Program exit value ** ******************************************************************************/ int main(void) { SystemInit(); SystemCoreClockUpdate(); setCANBUS1(); load_nonpersistent(); SysTick_Config(SystemCoreClock / 10); // 100mS Systicker. I2C1Init(); ADCInit(ADC_CLK); init_GPIO(); init_watchdog(); while(1){shunt_read();} return 0; }
// Setup Peripherals: DMA GPIO ADC DAC void init_DMA_GPIO_ADC(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; DAC_InitTypeDef DAC_InitStructure; /* Enable ADC3, DMA2, DMA1, DAC and GPIOC clocks ****************************************/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE); /* DMA1 clock and GPIOB clock enable (to be used with DAC) */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_GPIOB, ENABLE); /* DAC Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); /* This enables the peripheral clock to * the GPIOA IO module */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* GPIOD Clock enable*/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); /* GPIO Init **********************************************************/ init_GPIO(); //see above definition //Tim6 Configuration: TIM6_Config(); //ADC3 /* DMA2 Stream0 channel2 configuration **************************************/ DMA_InitStructure.DMA_Channel = DMA_Channel_2; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream0, &DMA_InitStructure); DMA_Cmd(DMA2_Stream0, ENABLE); //DAC2 /* DAC channel2 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_2, &DAC_InitStructure); /* DMA1_Stream6 channel7 configuration **************************************/ DMA_DeInit(DMA1_Stream6); DMA_InitStructure.DMA_Channel = DMA_Channel_7; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)DAC_DHR12R2_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&DAC1ConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream6, &DMA_InitStructure); /* Enable DMA1_Stream6 */ DMA_Cmd(DMA1_Stream6, ENABLE); /* Enable DAC Channel2 */ DAC_Cmd(DAC_Channel_2, ENABLE); /* Enable DMA for DAC Channel2 */ DAC_DMACmd(DAC_Channel_2, ENABLE); /* ADC Common Init **********************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC3 Init ****************************************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC3, &ADC_InitStructure); /* ADC3 regular channel12 configuration *************************************/ ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 1, ADC_SampleTime_3Cycles); /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE); /* Enable ADC3 DMA */ ADC_DMACmd(ADC3, ENABLE); /* Enable ADC3 */ ADC_Cmd(ADC3, ENABLE); }
int init () { enum LEDs led1,led2,led3,led4; led1 = GREEN; led2 = GREEN; led3 = GREEN; led4 = GREEN; uint8_t led[4] = {led1,led2,led3,led4}; /* uint16_t motor1 = Motor1; uint16_t motor2 = Motor2; uint16_t motor3 = Motor3; uint16_t motor4 = Motor4; uint16_t flipflop = FlipFlop; uint16_t input = Input;*/ struct Motor motor; motor.Motor1=171; motor.Motor2=172; motor.Motor3=173; motor.Motor4=174; motor.FlipFlop=175; motor.Input=176; if (isInitialized) { return 1 ; } if (!init_GPIO ()) { return 0 ; } fd_act = open ("/dev/ttyO0", O_RDWR | O_NOCTTY | O_NDELAY) ; if (fd_act < 0) { return 0 ; } int flags = fcntl(fd_act, F_GETFL, 0) ; fcntl(fd_act, F_SETFL, flags | O_NONBLOCK); //read calls are non blocking //set port options struct termios options; //Get the current options for the port tcgetattr (fd_act, &options); //Set the baud rates to 115200 cfsetispeed (&options, B115200); cfsetospeed (&options, B115200); options.c_cflag |= (CLOCAL | CREAD); //Enable the receiver and set local mode options.c_iflag = 0; //clear input options options.c_lflag = 0; //clear local options options.c_oflag &= ~OPOST; //clear output options (raw output) //Set the new options for the port tcsetattr (fd_act, TCSANOW, &options); setupInput (motor.Input) ; setupOutput (motor.FlipFlop) ; clear_GPIO (motor.FlipFlop) ; usleep (1000) ; set_GPIO (motor.FlipFlop) ; setupOutput (motor.Motor1); setupOutput (motor.Motor2); setupOutput (motor.Motor3); setupOutput (motor.Motor4); set_GPIO (motor.Motor1) ; set_GPIO (motor.Motor2) ; set_GPIO (motor.Motor3) ; set_GPIO (motor.Motor4) ; //configure motors uint8_t reply[256]; int m; for (m = 0; m < 4; m++) { clear_GPIO (motor.Motor1 + m); sendCmd (0xe0, reply, 2); if (reply[0] != 0xe0 || reply[1] != 0x00) { printf ("motor%d cmd=0x%02x reply=0x%02x\n", m + 1, (int) reply[0], (int) reply[1]); } usleep(1000); sendCmd (m + 1, reply, 1); set_GPIO (motor.Motor1 + m); } //all select lines active clear_GPIO (motor.Motor1) ; clear_GPIO (motor.Motor2) ; clear_GPIO (motor.Motor3) ; clear_GPIO (motor.Motor4) ; //start multicast sendCmd (0xa0, reply, 1); sendCmd (0xa0, reply, 1); sendCmd (0xa0, reply, 1); sendCmd (0xa0, reply, 1); sendCmd (0xa0, reply, 1); //reset IRQ flipflop - on error 176 reads 1, this code resets 176 to 0 clear_GPIO (motor.FlipFlop) ; set_GPIO (motor.FlipFlop); set(led) ; signal(SIGINT,terminate_interrupt); //Interrupts for Ctrl+C. isInitialized = 1 ; return 1 ; }
int main(void) { ///////// Initial Portion ///////////////////////// init_USART1(460800); // initialize USART1 @ 9600 baud SystemCoreClockUpdate(); /* Get Core Clock Frequency */ if (SysTick_Config(SystemCoreClock / 1000000)) { /* SysTick 1 micro sec interrupts */ while (1); /* Capture error */ } USART_puts(USART1, "START !"); // just send a message to indicate that it works USART_puts(USART1, " \r \n"); // new line //Init GPIO for Leds on board and button input init_GPIO(); //Init SPI1 mySPI_Init(); //Delay for all periferals to be ready Delay(1000000); //Delay 1s for cap to be charged // mySPI_SendByte(0x11); //SDATAC //mySPI_Send3Byte(0x20,0x00,0x00); //Issue Reset Pulse for ADS1299 GPIO_ResetBits(GPIOD, GPIO_Pin_2); Delay(5); //Delay couples of clock cycles for ADS to read the signal GPIO_SetBits(GPIOD, GPIO_Pin_2); // Complete reset Pulse Delay(18); //Delay at least 18 clock cycles //Send stop command for ADS1299 /*Put CS line down send out command (stop command) SDATAC = 0x11, delay 1 micro seccond; put CS line up again*/ mySPI_SendByte(0x11); //SDATAC Delay(10000); //¾WREG CONFIG3 E0h // mySPI_SendData(0x43); //Configure register mySPI_Send3Byte(0x43,0x00,0xE1); //config3 ENABLE internal reference mySPI_Send3Byte(0x41,0x00,0xD6); // Config1 noi daisy chain; output data rate 250sps(F/4096) mySPI_Send3Byte(0x42,0x00,0xC0); //Config 2: test source externa, keep all as default //Write to 8 channel open the shorted mySPI_Send3Byte(0x45,0x00,0x00); //collecting data for channel 1-4 mySPI_Send3Byte(0x46,0x00,0x00); mySPI_Send3Byte(0x47,0x00,0x00); mySPI_Send3Byte(0x48,0x00,0x00); mySPI_Send3Byte(0x20,0x00,0x00); switch (getIDval & 0x1F ) { //least significant bits reports channels case 0x10: //16 gMaxChan = 4; //ads1294 break; case 0x11: //17 gMaxChan = 6; //ads1296 break; case 0x12: //18 gMaxChan = 8; //ads1298 break; case 0x1E: //30 gMaxChan = 8; //ads1299 USART_puts(USART1, "ADS1299 Hooray!"); break; default: gMaxChan = 0; } readRegister(18,0); // Read all register from address 0 //Start conversation. Set start pin to 1 GPIO_SetBits(GPIOD, GPIO_Pin_0); // Asking for data to send continueously mySPI_SendByte(0x10); //SDATAC begin to read data /////////////////- Loop - //////////////////////////////////// while (1){ uint8_t i =0; // You can do whatever you want in here /* USART_puts(USART1, "Init 2 complete! Hello World! \r \n"); // just send a message to indicate that it works //GPIO_ToggleBits(GPIOD, GPIO_Pin_15 | GPIO_Pin_14 | GPIO_Pin_13| GPIO_Pin_12 ); GPIO_ToggleBits(GPIOD, GPIO_Pin_12 ); //Test Leds on board Delay(200); GPIO_ToggleBits(GPIOD, GPIO_Pin_13 ); //Test Leds on board Delay(200); GPIO_ToggleBits(GPIOD, GPIO_Pin_14 ); //Test Leds on board Delay(1000); */ // GPIO_ToggleBits(GPIOD, GPIO_Pin_12 ); //Test Leds on board if(!(GPIO_ReadInputData(GPIOD) & GPIO_Pin_1)){ //Read state of D1 (DRDY signal). Start new process if it is 0 for( i=0; i<9; i++){ char strADS[50]; getdata = mySPI_GetData(0x00); // regAddress = (0x20 | i); // getDataSPI = mySPI_Send3Byte((0x20 | i),0x00,0x00); //Increase to required address from the base address; if( i==2){ //Read 1 register at a time if (getdata > 0x007FFFFF) //Convert minus data { getdata = 0xFFFFFF - getdata; getdata = getdata/19373; //snprintf(strADS, 50, "Channel %d Val: %X \r \n", i, getdata); //convert int to string snprintf(strADS, 50, "E0,%d\n",getdata); //convert int to string USART_puts(USART1, strADS); //pirnt the address in string format } else{ getdata = getdata/19373; //snprintf(strADS, 50, "Channel %d Val: %X \r \n", i, getdata); //convert int to string snprintf(strADS, 50, "E0,%d\n",getdata); //convert int to string USART_puts(USART1, strADS); //pirnt the address in string format } } } //Delay(50000); } //if }// while (1) ////////////////////////////////////////////////////////// } //main
int main(void){ // initialize the GPIO pins we need init_GPIO(); /* This flashed the LEDs on the board once * Two registers are used to set the pins (pin level is VCC) * or to reset the pins (pin level is GND) * * BSRR stands for bit set/reset register * it is seperated into a high and a low word (each of 16 bit size) * * A logical 1 in BSRRL will set the pin and a logical 1 in BSRRH will * reset the pin. A logical 0 in either register has no effect */ GPIOD->BSRRL = 0xF000; // set PD12 thru PD15 Delay(1000000L); // wait a short period of time GPIOD->BSRRH = 0xF000; // reset PD12 thru PD15 // this counter is used to count the number of button presses uint8_t i = 0; while (1){ /* Every GPIO port has an input and * output data register, ODR and IDR * respectively, which hold the status of the pin * * Here the IDR of GPIOA is checked whether bit 0 is * set or not. If it's set the button is pressed */ if(GPIOA->IDR & 0x0001){ // if the number of button presses is greater than 4, reset the counter (we start counting from 0!) if(i > 3){ i = 0; } else{ // if it's smaller than 4, switch the LEDs switch(i){ case 0: GPIOD->BSRRL = GPIO_Pin_12; // this sets LED1 (green) GPIOD->BSRRH = GPIO_Pin_15; // this resets LED4 (blue) break; case 1: GPIOD->BSRRL = GPIO_Pin_13; // this sets LED2 (orange) GPIOD->BSRRH = GPIO_Pin_12; // this resets LED1 break; case 2: GPIOD->BSRRL = GPIO_Pin_14; // this sets LED3 (red) GPIOD->BSRRH = GPIO_Pin_13; // this resets LED2 break; case 3: GPIOD->BSRRL = GPIO_Pin_15; // this sets LED4 GPIOD->BSRRH = GPIO_Pin_14; // this resets LED3 break; } i++; // increase the counter every time the switch is pressed } Delay(3000000L); // add a small delay to debounce the switch } } }