Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f10x.h"
- void GPIO_Config(void);
- void RCC_Config(void);
- int main(void)
- {
- volatile unsigned long int i;
- volatile unsigned long int p = 0x40000ul;
- uint8_t button_state=0xFF, temp=0, port_data ;
- //konfiguracja systemu
- RCC_Config();
- GPIO_Config();
- GPIO_ResetBits(GPIOA, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 );
- GPIO_ResetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
- while (1)
- {
- port_data = GPIO_ReadInputData(GPIOA); //czytaj port GPIOA
- temp = port_data ^ button_state; // czy stan przycisków sie zmienil?
- temp &= button_state; // czy to byla zmiana z 1 na 0?
- button_state = port_data; // zapamietaj nowy stan
- if (temp & 0x01) // czy to przycisk 1?
- {
- for(i = 0; i < p; i++);
- GPIO_WriteBit(GPIOB, GPIO_Pin_0, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_0)));
- }
- if (temp & 0x02) // czy to przycisk 2?
- {
- for(i = 0; i < p; i++);
- GPIO_WriteBit(GPIOB, GPIO_Pin_1, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_1)));
- }
- if (temp & 0x04) // czy to przycisk 3?
- {
- for(i = 0; i < p; i++);
- GPIO_WriteBit(GPIOB, GPIO_Pin_2, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_2)));
- }
- if (temp & 0x08) // czy to przycisk 4?
- {
- for(i = 0; i < p; i++);
- GPIO_WriteBit(GPIOB, GPIO_Pin_3, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_3)));
- }
- };
- return 0;
- }
- void RCC_Config(void)
- //konfigurowanie sygnalow taktujacych
- {
- ErrorStatus HSEStartUpStatus; //zmienna opisujaca rezultat uruchomienia HSE
- RCC_DeInit(); //Reset ustawien RCC
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
- RCC_HSEConfig(RCC_HSE_ON); //Wlaczenie HSE
- HSEStartUpStatus = RCC_WaitForHSEStartUp(); //Odczekaj az HSE bedzie gotowy
- if(HSEStartUpStatus == SUCCESS)
- {
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//
- FLASH_SetLatency(FLASH_Latency_2); //ustaw zwloke dla pamieci Flash; zaleznie od taktowania rdzenia
- //0:<24MHz; 1:24~48MHz; 2:>48MHz
- RCC_HCLKConfig(RCC_SYSCLK_Div1); //ustaw HCLK=SYSCLK
- RCC_PCLK2Config(RCC_HCLK_Div1); //ustaw PCLK2=HCLK
- RCC_PCLK1Config(RCC_HCLK_Div2); //ustaw PCLK1=HCLK/2
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //ustaw PLLCLK = HSE*9 czyli 8MHz * 9 = 72 MHz
- RCC_PLLCmd(ENABLE); //wlacz PLL
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //odczekaj na poprawne uruchomienie PLL
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //ustaw PLL jako zrodlo sygnalu zegarowego
- while(RCC_GetSYSCLKSource() != 0x08); //odczekaj az PLL bedzie sygnalem zegarowym systemu
- /*Tu nalezy umiescic kod zwiazany z konfiguracja sygnalow zegarowych potrzebnych w programie peryferiow*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);//wlacz taktowanie portu GPIO A
- } else {
- }
- }
- void GPIO_Config(void)
- {
- //konfigurowanie portow GPIO
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
- /*Tu nalezy umiescic kod zwiazany z konfiguracja poszczegolnych portow GPIO potrzebnych w programie*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //wyjscie push-pull
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //wejscie bez podciagania
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement