Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //INCLUDE & DEFINES
- #include "stm32f30x.h"
- #define MODE_OUT 1
- #define MODE_AFN 2
- #define MODE_ANL 3
- #define SPEED_2 2
- #define SPEED_10 1
- #define SPEED_50 3
- //************************************************
- // USER DEFINES
- //************************************************
- #define LED_GPIO GPIOE
- #define BLUE 8
- #define RED 9
- #define ORANGE 10
- #define GREEN 11
- #define BLUE2 12
- #define RED2 13
- #define ORANGE2 14
- #define GREEN2 15
- #define BTN_GPIO GPIOA
- #define BTN 0
- //************************************************
- // TFT DEFINES
- //************************************************
- #define RD 14
- #define RD_PORT GPIOC
- #define RS 6
- #define RS_PORT GPIOE
- #define WR 15
- #define WR_PORT GPIOC
- #define CS 13
- #define CS_PORT GPIOC
- #define LCD 0
- #define LCD_PORT GPIOD
- #define CS_LOW GPIO_CLR(CS_PORT, CS);
- #define RS_LOW GPIO_CLR(RS_PORT, RS);
- #define RD_LOW GPIO_CLR(RD_PORT, RD);
- #define WR_LOW GPIO_CLR(WR_PORT, WR);
- #define CS_HIGH GPIO_SET(CS_PORT, CS);
- #define RS_HIGH GPIO_SET(RS_PORT, RS);
- #define RD_HIGH GPIO_SET(RD_PORT, RD);
- #define WR_HIGH GPIO_SET(WR_PORT, WR);
- //Basic Colors
- #define C_RED 0xf800
- #define C_GREEN 0x7e00
- #define C_BLUE 0x001f
- #define C_BLACK 0x0000
- #define C_YELLOW 0xffe0
- #define C_WHITE 0xffff
- //Other Colors
- #define C_CYAN 0x07ff
- #define C_BRIGHT_RED 0xf810
- #define C_GRAY1 0x8410
- #define C_GRAY2 0x4208
- //TFT resolution 240*320
- #define MIN_X 0
- #define MIN_Y 0
- #define MAX_X 239
- #define MAX_Y 319
- void OUTPUT (GPIO_TypeDef *GPIOX, uint32_t PIN);
- void init (void);
- void sendCommand(unsigned int index);
- void sendData(unsigned int data);
- void pushData(unsigned char data);
- unsigned char getData(void);
- unsigned int readRegister(unsigned int index);
- void all_pin_output(void);
- void FILL_SCREEN(uint32_t COLOR);
- void BAR_SCREEN(void);
- //************************************************
- //FUNCTIONS
- void CONFIG_F3(void);
- void GPIO_OUTPUT ( GPIO_TypeDef *GPIOx, uint32_t PIN, uint32_t SPEED);
- void GPIO_INPUT (GPIO_TypeDef *GPIOX, uint32_t PIN, uint32_t SPEED);
- void GPIO_SET(GPIO_TypeDef *GPIOX, uint32_t PIN);
- void GPIO_CLR(GPIO_TypeDef *GPIOX, uint32_t PIN);
- char IS_BTN_TGL(void);
- void WAIT_BTN_TGL(void);
- //CODE
- int myState = 0;
- void delay(void)
- {
- long x;
- for(x = 0; x<200000; x++)
- {
- x = x + 1;
- x = x - 1;
- }
- }
- void main(void)
- {
- CONFIG_F3();
- LED_GPIO->OTYPER = 0; // ALL PUSH PULL on LED_GPIO
- LED_GPIO->PUPDR = 0; // ALL NO PU.PD on LED_GPIO
- LCD_PORT->OTYPER = 0;
- LCD_PORT->PUPDR = 0;
- RS_PORT->OTYPER = 0;
- RS_PORT->PUPDR = 0;
- RD_PORT->OTYPER = 0;
- RD_PORT->PUPDR = 0;
- WR_PORT->OTYPER = 0;
- WR_PORT->PUPDR = 0;
- CS_PORT->OTYPER = 0;
- CS_PORT->PUPDR = 0;
- GPIO_OUTPUT ( LED_GPIO, RED, SPEED_50 );
- GPIO_OUTPUT ( LED_GPIO, RED2, SPEED_50 );
- GPIO_OUTPUT ( LED_GPIO, GREEN, SPEED_50 );
- GPIO_OUTPUT ( LED_GPIO, GREEN2, SPEED_50 );
- GPIO_OUTPUT ( LED_GPIO, ORANGE, SPEED_50 );
- GPIO_OUTPUT ( LED_GPIO, ORANGE2, SPEED_50 );
- GPIO_OUTPUT ( LED_GPIO, BLUE, SPEED_50 );
- GPIO_OUTPUT ( LED_GPIO, BLUE2, SPEED_50 );
- all_pin_output(); //TFT
- GPIO_INPUT ( BTN_GPIO, BTN, SPEED_50 ); // BTN is INPUT
- init();
- FILL_SCREEN(C_WHITE);
- //ALL LED OFF
- LED_GPIO->BSRR = (((1<<ORANGE)|(1<<ORANGE2)|(1<<BLUE)|(1<<BLUE2)|(1<<RED)|(1<<RED2)|(1<<GREEN)|(1<<GREEN2))<<16);
- GPIO_SET (LED_GPIO, RED);
- while(1)
- {
- BAR_SCREEN();
- WAIT_BTN_TGL();
- FILL_SCREEN(C_RED);
- WAIT_BTN_TGL();
- FILL_SCREEN(C_GREEN);
- WAIT_BTN_TGL();
- FILL_SCREEN(C_BLUE);
- WAIT_BTN_TGL();
- FILL_SCREEN(C_BLACK);
- WAIT_BTN_TGL();
- FILL_SCREEN(C_YELLOW);
- WAIT_BTN_TGL();
- FILL_SCREEN(C_WHITE);
- WAIT_BTN_TGL();
- FILL_SCREEN(C_CYAN);
- WAIT_BTN_TGL();
- FILL_SCREEN(C_BRIGHT_RED);
- WAIT_BTN_TGL();
- FILL_SCREEN(C_GRAY1);
- WAIT_BTN_TGL();
- FILL_SCREEN(C_GRAY2);
- WAIT_BTN_TGL();
- }
- }
- char IS_BTN_TGL(void)
- {
- if(BTN_GPIO->IDR & 0x01)
- {
- if(myState)
- myState=0;
- else
- myState=1;
- delay();
- delay();
- return 1;
- }
- return 0;
- }
- void WAIT_BTN_TGL(void)
- {
- while((BTN_GPIO->IDR & 0x01) == 0);
- delay();delay();
- }
- void CONFIG_F3(void)
- {
- //16MHZ / 2 = 8MHZ
- //8MHZ * 9 = 72MHZ
- RCC->CFGR = 0x61F0002; //HSE, 9x PLL, HSE/2, HSE/PREDIV selected as PLL input clock, PLL CLOCK
- RCC->CR = 0x1010000; //PLL ENABLED, HSE ON
- while((RCC->CR & 0x2000000) == 0); //WAIT UNTIL PLL LOCKED
- //72MHZ from now on
- RCC->AHBENR = 0x317E0057; //Enable all AHB Peripheral Clocks
- }
- void GPIO_OUTPUT (GPIO_TypeDef *GPIOX, uint32_t PIN, uint32_t SPEED)
- {
- GPIOX->MODER &= ~(3<<(PIN*2));
- GPIOX->MODER |= (MODE_OUT<<(PIN*2));
- GPIOX->OSPEEDR &= ~(3<<(PIN*2));
- GPIOX->OSPEEDR |= (SPEED<<(PIN*2));
- }
- void GPIO_INPUT (GPIO_TypeDef *GPIOX, uint32_t PIN, uint32_t SPEED)
- {
- GPIOX->MODER &= ~(3<<(PIN*2));
- GPIOX->OSPEEDR &= ~(3<<(PIN*2));
- GPIOX->OSPEEDR |= (SPEED<<(PIN*2));
- }
- void GPIO_SET(GPIO_TypeDef *GPIOX, uint32_t PIN)
- {
- GPIOX->BSRR = (1<<PIN);
- }
- void GPIO_CLR(GPIO_TypeDef *GPIOX, uint32_t PIN)
- {
- GPIOX->BSRR = ((1<<PIN)<<16);
- }
- //**********************************************************************
- // CODE FOR TFT
- //**********************************************************************
- void FILL_SCREEN(uint32_t COLOR)
- {
- for(unsigned char i=0;i<2;i++)
- {
- for(unsigned int f=0;f<38400;f++)
- {
- sendData(COLOR);
- }
- }
- }
- void BAR_SCREEN(void)
- {
- for(unsigned int i=0;i<320;i++)
- {
- for(unsigned int f=0;f<24;f++)
- sendData(C_RED);
- for(unsigned int f=0;f<24;f++)
- sendData(C_GREEN);
- for(unsigned int f=0;f<24;f++)
- sendData(C_BLUE);
- for(unsigned int f=0;f<24;f++)
- sendData(C_YELLOW);
- for(unsigned int f=0;f<24;f++)
- sendData(C_BLACK);
- for(unsigned int f=0;f<24;f++)
- sendData(C_WHITE);
- for(unsigned int f=0;f<24;f++)
- sendData(C_CYAN);
- for(unsigned int f=0;f<24;f++)
- sendData(C_BRIGHT_RED);
- for(unsigned int f=0;f<24;f++)
- sendData(C_GRAY1);
- for(unsigned int f=0;f<24;f++)
- sendData(C_GRAY2);
- }
- }
- void OUTPUT (GPIO_TypeDef *GPIOX, uint32_t PIN)
- {
- GPIOX->MODER &= (0xFFFF<<(PIN*2));
- GPIOX->MODER |= (0x5555<<(PIN*2));
- GPIOX->OSPEEDR |= (0xFFFF<<(PIN*2));
- }
- void all_pin_output(void)
- {
- GPIO_OUTPUT ( RD_PORT, RD, SPEED_50 );
- GPIO_OUTPUT ( CS_PORT, CS, SPEED_50 );
- GPIO_OUTPUT ( WR_PORT, WR, SPEED_50 );
- GPIO_OUTPUT ( RS_PORT, RS, SPEED_50 );
- OUTPUT ( LCD_PORT, LCD );
- }
- void all_pin_input(void)
- {
- LCD_PORT->MODER &= ~(0xFFFF<<(LCD*2));
- LCD_PORT->OSPEEDR |= (0xFFFF<<(LCD*2));
- }
- #define all_pin_low() (LCD_PORT->BSRR = ((0xFF<<LCD)<<16))
- void pushData(unsigned char data)
- {
- //all_pin_low();
- LCD_PORT->ODR = (int)((int)data<<LCD);
- }
- unsigned char getData(void)
- {
- unsigned char data=0;
- delay();
- data = ((LCD_PORT->IDR>>LCD) & 0xFF);
- return data;
- }
- void sendCommand(unsigned int index)
- {
- CS_LOW;
- RS_LOW;
- RD_HIGH;
- WR_HIGH;
- WR_LOW;
- pushData(index>>8);
- WR_HIGH;
- WR_LOW;
- pushData(index);
- WR_HIGH;
- CS_HIGH;
- }
- void sendData(unsigned int data)
- {
- CS_LOW;
- RS_HIGH;
- RD_HIGH;
- WR_LOW;
- pushData(data>>8);
- WR_HIGH;
- WR_LOW;
- pushData(data);
- WR_HIGH;
- CS_HIGH;
- }
- unsigned int readRegister(unsigned int index)
- {
- unsigned int data=0;
- CS_LOW;
- RS_LOW;
- RD_HIGH;
- all_pin_output();
- WR_LOW;
- pushData(index>>8);
- WR_HIGH;
- WR_LOW;
- pushData(index);
- WR_HIGH;
- all_pin_input();
- all_pin_low();
- RS_HIGH;
- RD_LOW;
- data = (int)getData()<<8;
- RD_HIGH;
- RD_LOW;
- data |= getData();
- RD_HIGH;
- CS_HIGH;
- all_pin_output();
- return data;
- }
- void init (void)
- {
- int IC_CODE = 0;
- CS_HIGH;
- all_pin_output();
- all_pin_low();
- delay();
- IC_CODE = readRegister(0x0);
- if(IC_CODE == 0x5408)
- {
- sendCommand(0x0000);
- sendData(0x0001);
- delay();
- sendCommand(0x0001);
- sendData(0x0000);
- sendCommand(0x0002);
- sendData(0x0700);
- sendCommand(0x0003);
- sendData(0x1030);
- sendCommand(0x0004);
- sendData(0x0000);
- sendCommand(0x0008);
- sendData(0x0207);
- sendCommand(0x0009);
- sendData(0x0000);
- sendCommand(0x000A);
- sendData(0x0000);
- sendCommand(0x000C);
- sendData(0x0000);
- sendCommand(0x000D);
- sendData(0x0000);
- sendCommand(0x000F);
- sendData(0x0000);
- //power on sequence VGHVGL
- sendCommand(0x0010);
- sendData(0x0000);
- sendCommand(0x0011);
- sendData(0x0007);
- sendCommand(0x0012);
- sendData(0x0000);
- sendCommand(0x0013);
- sendData(0x0000);
- //vgh
- sendCommand(0x0010);
- sendData(0x1290);
- sendCommand(0x0011);
- sendData(0x0227);
- delay();
- //vregiout
- sendCommand(0x0012);
- sendData(0x001d); //0x001b
- delay();
- //vom amplitude
- sendCommand(0x0013);
- sendData(0x1500);
- delay();
- //vom H
- sendCommand(0x0029);
- sendData(0x0018);
- sendCommand(0x002B);
- sendData(0x000D);
- //gamma
- sendCommand(0x0030);
- sendData(0x0004);
- sendCommand(0x0031);
- sendData(0x0307);
- sendCommand(0x0032);
- sendData(0x0002);// 0006
- sendCommand(0x0035);
- sendData(0x0206);
- sendCommand(0x0036);
- sendData(0x0408);
- sendCommand(0x0037);
- sendData(0x0507);
- sendCommand(0x0038);
- sendData(0x0204);//0200
- sendCommand(0x0039);
- sendData(0x0707);
- sendCommand(0x003C);
- sendData(0x0405);// 0504
- sendCommand(0x003D);
- sendData(0x0F02);
- //ram
- sendCommand(0x0050);
- sendData(0x0000);
- sendCommand(0x0051);
- sendData(0x00EF);
- sendCommand(0x0052);
- sendData(0x0000);
- sendCommand(0x0053);
- sendData(0x013F);
- sendCommand(0x0060);
- sendData(0xA700);
- sendCommand(0x0061);
- sendData(0x0001);
- sendCommand(0x006A);
- sendData(0x0000);
- //
- sendCommand(0x0080);
- sendData(0x0000);
- sendCommand(0x0081);
- sendData(0x0000);
- sendCommand(0x0082);
- sendData(0x0000);
- sendCommand(0x0083);
- sendData(0x0000);
- sendCommand(0x0084);
- sendData(0x0000);
- sendCommand(0x0085);
- sendData(0x0000);
- //
- sendCommand(0x0090);
- sendData(0x0010);
- sendCommand(0x0092);
- sendData(0x0600);
- sendCommand(0x0093);
- sendData(0x0003);
- sendCommand(0x0095);
- sendData(0x0110);
- sendCommand(0x0097);
- sendData(0x0000);
- sendCommand(0x0098);
- sendData(0x0000);
- sendCommand(0x0007);
- sendData(0x0133);
- sendCommand(0x0022);//Start to write to display RAM
- }
- else
- {
- sendCommand(0x0001);
- sendData(0x0100);
- sendCommand(0x0002);
- sendData(0x0700);
- sendCommand(0x0003);
- sendData(0x1030);
- sendCommand(0x0004);
- sendData(0x0000);
- sendCommand(0x0008);
- sendData(0x0302);
- sendCommand(0x000A);
- sendData(0x0000);
- sendCommand(0x000C);
- sendData(0x0000);
- sendCommand(0x000D);
- sendData(0x0000);
- sendCommand(0x000F);
- sendData(0x0000);
- delay();
- sendCommand(0x0030);
- sendData(0x0000);
- sendCommand(0x0031);
- sendData(0x0405);
- sendCommand(0x0032);
- sendData(0x0203);
- sendCommand(0x0035);
- sendData(0x0004);
- sendCommand(0x0036);
- sendData(0x0B07);
- sendCommand(0x0037);
- sendData(0x0000);
- sendCommand(0x0038);
- sendData(0x0405);
- sendCommand(0x0039);
- sendData(0x0203);
- sendCommand(0x003c);
- sendData(0x0004);
- sendCommand(0x003d);
- sendData(0x0B07);
- sendCommand(0x0020);
- sendData(0x0000);
- sendCommand(0x0021);
- sendData(0x0000);
- sendCommand(0x0050);
- sendData(0x0000);
- sendCommand(0x0051);
- sendData(0x00ef);
- sendCommand(0x0052);
- sendData(0x0000);
- sendCommand(0x0053);
- sendData(0x013f);
- delay();
- sendCommand(0x0060);
- sendData(0xa700);
- sendCommand(0x0061);
- sendData(0x0001);
- sendCommand(0x0090);
- sendData(0x003A);
- sendCommand(0x0095);
- sendData(0x021E);
- sendCommand(0x0080);
- sendData(0x0000);
- sendCommand(0x0081);
- sendData(0x0000);
- sendCommand(0x0082);
- sendData(0x0000);
- sendCommand(0x0083);
- sendData(0x0000);
- sendCommand(0x0084);
- sendData(0x0000);
- sendCommand(0x0085);
- sendData(0x0000);
- sendCommand(0x00FF);
- sendData(0x0001);
- sendCommand(0x00B0);
- sendData(0x140D);
- sendCommand(0x00FF);
- sendData(0x0000);
- delay();
- sendCommand(0x0007);
- sendData(0x0133);
- delay();
- //exit standby
- sendCommand(0x0010);
- sendData(0x14E0);
- delay();
- sendCommand(0x0007);
- sendData(0x0133);
- sendCommand(0x0022);
- }
- //paint screen black
- for(unsigned char i=0;i<2;i++)
- {
- for(unsigned int f=0;f<38400;f++)
- {
- sendData(C_RED);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement