Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f10x.h"
- #define LCD_GPIO GPIOB
- #define LCD_D4 GPIO_Pin_0
- #define LCD_D5 GPIO_Pin_1
- #define LCD_D6 GPIO_Pin_2
- #define LCD_D7 GPIO_Pin_3
- #define LCD_E GPIO_Pin_4
- #define LCD_RS GPIO_Pin_5
- #define LCD_CLEAR 0x01
- #define LCD_HOME 0x02
- #define LCD_ENTRY_MODE 0x04
- #define LCD_EM_SHIFT_CURSOR 0
- #define LCD_EM_INCREMENT 2
- #define LCD_DISPLAY_ONOFF 0x08
- #define LCD_DISPLAY_OFF 0
- #define LCD_DISPLAY_ON 4
- #define LCD_CURSOR_ON 2
- #define LCD_CURSOR_BLINK 1
- #define LCD_DISPLAY_CURSOR_SHIFT 0x10
- #define LCD_FUNCTION_SET 0x20
- #define LCD_FONT8 0
- #define LCD_TWO_LINE 8
- #define LCD_4_BIT 0
- #define LCD_CGRAM_SET 0x40
- #define LCD_DDRAM_SET 0x80
- #define SysTick_Frequency 9000000 // 9MHz
- GPIO_InitTypeDef GPIO_InitStructure;
- uint32_t licznik = 0;
- void LCD_WriteNibble(unsigned char nibble);
- void LCD_WriteData(unsigned char dataToWrite);
- void LCD_WriteCommand(unsigned char commandToWrite);
- void LCD_Initialize(void);
- void GPIO_Config(void);
- void RCC_Config(void);
- void Delay(uint32_t ms);
- int main(void)
- {
- unsigned char tekst[12] = {"Hello World!"};
- int i = 0;
- RCC_Config();
- GPIO_Config();
- if (SysTick_Config(SysTick_Frequency / 1000)) // przerwanie co 1/2 sek.
- {
- //W razie bl$du petla nieskonczona
- while (1);
- }
- //SysTick bedzie taktowany z f = 72MHz/8 = 9MHz
- SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
- LCD_Initialize(); // Inicjalizuj LCD
- LCD_WriteCommand(LCD_CLEAR); // Wyczysc LCD
- LCD_WriteCommand(LCD_HOME);
- LCD_WriteCommand(LCD_DISPLAY_ONOFF | LCD_DISPLAY_ON);
- for (i=0; i<12; i++) // Wypisz tekst
- LCD_WriteData(tekst[i]);
- while (1);
- return 0;
- }
- void LCD_WriteNibble(unsigned char nibble){
- volatile unsigned int delayCnt = 0;
- GPIO_WriteBit(LCD_GPIO, LCD_D4, (nibble & 0x01)); // ustaw bity na liniach
- GPIO_WriteBit(LCD_GPIO, LCD_D5, (nibble & 0x02)); // D4 – D7
- GPIO_WriteBit(LCD_GPIO, LCD_D6, (nibble & 0x04));
- GPIO_WriteBit(LCD_GPIO, LCD_D7, (nibble & 0x08));
- GPIO_WriteBit(LCD_GPIO, LCD_E, Bit_SET); // ustaw wysoki poziom E
- for(delayCnt = 0; delayCnt < 16; delayCnt++); // poczekaj troche…
- GPIO_WriteBit(LCD_GPIO, LCD_E, Bit_RESET); // ustaw niski poziom E
- }
- // wyslij komende do wyswietlacza
- void LCD_WriteCommand(unsigned char commandToWrite){
- volatile unsigned int delayCnt = 0;
- GPIO_WriteBit(LCD_GPIO, LCD_RS, Bit_RESET); // RS = 0 - komenda
- LCD_WriteNibble(commandToWrite >> 4); // wyslij starszy pólbajt
- LCD_WriteNibble(commandToWrite & 0x0F); // wyslij mlodszy pólbajt
- if (commandToWrite > 3) // w zaleznosci od komendy dobierz opóznienie
- for(delayCnt = 0; delayCnt < 3000; delayCnt++);
- else
- for(delayCnt = 0; delayCnt < 150000; delayCnt++);
- }
- // wyslij znak do wyswietlenia
- void LCD_WriteData(unsigned char dataToWrite){
- volatile unsigned int delayCnt = 0;
- GPIO_WriteBit(LCD_GPIO, LCD_RS, Bit_SET); // RS = 1 - komenda
- LCD_WriteNibble(dataToWrite >> 4); // wyslij starszy pólbajt
- LCD_WriteNibble(dataToWrite & 0x0F); // wyslij mlodszy pólbajt
- Delay(1);
- }
- void LCD_Initialize(){
- volatile unsigned int delayCnt = 0;
- GPIO_InitStructure.GPIO_Pin = LCD_D4|LCD_D5|LCD_D6|LCD_D7|LCD_RS|LCD_E;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(LCD_GPIO, &GPIO_InitStructure);
- Delay(15);
- LCD_WriteNibble(0x03);
- Delay(15);
- LCD_WriteNibble(0x03);
- Delay(15);
- LCD_WriteNibble(0x03);
- Delay(15);
- LCD_WriteNibble(0x03);
- Delay(15);
- LCD_WriteCommand(0x28);
- Delay(15);
- LCD_WriteCommand(0x08);
- Delay(15);
- LCD_WriteCommand(0x01);
- Delay(15);
- LCD_WriteCommand(0x06);
- Delay(15);
- }
- void RCC_Config(void)
- //konfigurowanie sygnalow taktujacych
- {
- ErrorStatus HSEStartUpStatus; //zmienna opisujaca rezultat uruchomienia HSE
- RCC_DeInit(); //Reset ustawien RCC
- 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
- //inne przykladowe konfiguracje:
- //RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_9); //ustaw PLLCLK = HSI/2*9 czyli 8MHz / 2 * 9 = 36 MHz
- //RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_9); //ustaw PLLCLK = HSE/2*9 czyli 8MHz / 2 * 9 = 36 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_GPIOB | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);//wlacz taktowanie portu GPIO B
- } else {
- }
- }
- void GPIO_Config(){
- GPIO_InitTypeDef GPIO_InitStructure;
- // disable JTAG
- 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_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- void Delay(uint32_t ms){
- uint32_t start=licznik;
- while(licznik<(start+ms));
- }
- void SysTick_Handler(void){
- licznik++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement