Advertisement
AtomSoft

2.8" TFT RS

Mar 30th, 2013
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.20 KB | None | 0 0
  1. //INCLUDE & DEFINES
  2. #include "stm32f30x.h"
  3.  
  4. #define MODE_OUT 1
  5. #define MODE_AFN 2
  6. #define MODE_ANL 3
  7.  
  8. #define SPEED_2  2
  9. #define SPEED_10 1
  10. #define SPEED_50 3
  11.  
  12. //************************************************
  13. //                USER DEFINES
  14. //************************************************
  15. #define LED_GPIO GPIOE
  16.  
  17. #define BLUE 8
  18. #define RED 9
  19. #define ORANGE 10
  20. #define GREEN 11
  21.  
  22. #define BLUE2 12
  23. #define RED2 13
  24. #define ORANGE2 14
  25. #define GREEN2 15
  26.  
  27. #define BTN_GPIO GPIOA
  28. #define BTN 0
  29. //************************************************
  30. //                TFT DEFINES
  31. //************************************************
  32.  
  33.   #define RD 14
  34.   #define RD_PORT GPIOC
  35.  
  36.   #define RS 6
  37.   #define RS_PORT GPIOE
  38.  
  39.   #define WR 15
  40.   #define WR_PORT GPIOC
  41.  
  42.   #define CS 13
  43.   #define CS_PORT GPIOC
  44.  
  45.   #define LCD 0
  46.   #define LCD_PORT GPIOD
  47.  
  48.   #define CS_LOW GPIO_CLR(CS_PORT, CS);
  49.   #define RS_LOW GPIO_CLR(RS_PORT, RS);
  50.   #define RD_LOW GPIO_CLR(RD_PORT, RD);
  51.   #define WR_LOW GPIO_CLR(WR_PORT, WR);
  52.  
  53.   #define CS_HIGH GPIO_SET(CS_PORT, CS);
  54.   #define RS_HIGH GPIO_SET(RS_PORT, RS);
  55.   #define RD_HIGH GPIO_SET(RD_PORT, RD);
  56.   #define WR_HIGH GPIO_SET(WR_PORT, WR);
  57.  
  58.   //Basic Colors
  59.   #define C_RED     0xf800
  60.   #define C_GREEN   0x7e00
  61.   #define C_BLUE    0x001f
  62.   #define C_BLACK   0x0000
  63.   #define C_YELLOW      0xffe0
  64.   #define C_WHITE   0xffff
  65.  
  66.   //Other Colors
  67.   #define C_CYAN    0x07ff 
  68.   #define C_BRIGHT_RED  0xf810 
  69.   #define C_GRAY1   0x8410  
  70.   #define C_GRAY2   0x4208  
  71.  
  72.   //TFT resolution 240*320
  73.   #define MIN_X 0
  74.   #define MIN_Y 0
  75.   #define MAX_X 239
  76.   #define MAX_Y 319
  77.  
  78.   void  OUTPUT (GPIO_TypeDef *GPIOX, uint32_t PIN);
  79.   void  init (void);
  80.   void  sendCommand(unsigned int index);
  81.   void  sendData(unsigned int data);
  82.   void  pushData(unsigned char data);
  83.   unsigned char  getData(void);
  84.   unsigned int  readRegister(unsigned int index);
  85.   void all_pin_output(void);
  86.   void FILL_SCREEN(uint32_t COLOR);
  87.   void BAR_SCREEN(void);
  88. //************************************************
  89.  
  90.  
  91. //FUNCTIONS
  92. void CONFIG_F3(void);
  93. void GPIO_OUTPUT ( GPIO_TypeDef *GPIOx, uint32_t PIN, uint32_t SPEED);
  94. void GPIO_INPUT (GPIO_TypeDef *GPIOX, uint32_t PIN, uint32_t SPEED);
  95. void GPIO_SET(GPIO_TypeDef *GPIOX, uint32_t PIN);
  96. void GPIO_CLR(GPIO_TypeDef *GPIOX, uint32_t PIN);
  97. char IS_BTN_TGL(void);
  98. void WAIT_BTN_TGL(void);
  99. //CODE
  100. int myState = 0;
  101.  
  102. void delay(void)
  103. {
  104.     long x;
  105.     for(x = 0; x<200000; x++)
  106.     {
  107.       x = x + 1;
  108.       x = x - 1;
  109.     }
  110. }
  111. void main(void)
  112. {
  113.   CONFIG_F3();
  114.  
  115.   LED_GPIO->OTYPER = 0;            // ALL PUSH PULL on LED_GPIO
  116.   LED_GPIO->PUPDR = 0;             // ALL NO PU.PD on LED_GPIO
  117.  
  118.   LCD_PORT->OTYPER = 0;
  119.   LCD_PORT->PUPDR = 0;
  120.  
  121.   RS_PORT->OTYPER = 0;
  122.   RS_PORT->PUPDR = 0;
  123.  
  124.   RD_PORT->OTYPER = 0;
  125.   RD_PORT->PUPDR = 0;
  126.  
  127.   WR_PORT->OTYPER = 0;
  128.   WR_PORT->PUPDR = 0;
  129.  
  130.   CS_PORT->OTYPER = 0;
  131.   CS_PORT->PUPDR = 0;
  132.  
  133.   GPIO_OUTPUT ( LED_GPIO, RED, SPEED_50 );
  134.   GPIO_OUTPUT ( LED_GPIO, RED2, SPEED_50 );
  135.   GPIO_OUTPUT ( LED_GPIO, GREEN, SPEED_50 );
  136.   GPIO_OUTPUT ( LED_GPIO, GREEN2, SPEED_50 );
  137.   GPIO_OUTPUT ( LED_GPIO, ORANGE, SPEED_50 );
  138.   GPIO_OUTPUT ( LED_GPIO, ORANGE2, SPEED_50 );
  139.   GPIO_OUTPUT ( LED_GPIO, BLUE, SPEED_50 );
  140.   GPIO_OUTPUT ( LED_GPIO, BLUE2, SPEED_50 );
  141.  
  142.   all_pin_output(); //TFT
  143.  
  144.   GPIO_INPUT ( BTN_GPIO, BTN, SPEED_50 );  // BTN is INPUT
  145.  
  146.   init();
  147.   FILL_SCREEN(C_WHITE);
  148.  
  149.   //ALL LED OFF
  150.   LED_GPIO->BSRR = (((1<<ORANGE)|(1<<ORANGE2)|(1<<BLUE)|(1<<BLUE2)|(1<<RED)|(1<<RED2)|(1<<GREEN)|(1<<GREEN2))<<16);
  151.  
  152.   GPIO_SET (LED_GPIO, RED);
  153.   while(1)
  154.   {
  155.     BAR_SCREEN();
  156.     WAIT_BTN_TGL();
  157.     FILL_SCREEN(C_RED);
  158.     WAIT_BTN_TGL();
  159.     FILL_SCREEN(C_GREEN);
  160.     WAIT_BTN_TGL();
  161.     FILL_SCREEN(C_BLUE);
  162.     WAIT_BTN_TGL();
  163.     FILL_SCREEN(C_BLACK);
  164.     WAIT_BTN_TGL();
  165.     FILL_SCREEN(C_YELLOW);
  166.     WAIT_BTN_TGL();
  167.     FILL_SCREEN(C_WHITE);
  168.     WAIT_BTN_TGL();
  169.     FILL_SCREEN(C_CYAN);
  170.     WAIT_BTN_TGL();
  171.     FILL_SCREEN(C_BRIGHT_RED);
  172.     WAIT_BTN_TGL();
  173.     FILL_SCREEN(C_GRAY1);
  174.     WAIT_BTN_TGL();
  175.     FILL_SCREEN(C_GRAY2);
  176.     WAIT_BTN_TGL();
  177.   }
  178. }
  179. char IS_BTN_TGL(void)
  180. {
  181.     if(BTN_GPIO->IDR & 0x01)
  182.     {
  183.         if(myState)
  184.           myState=0;
  185.         else
  186.           myState=1;
  187.  
  188.         delay();
  189.         delay();
  190.         return 1;
  191.     }
  192.     return 0;
  193. }
  194.  
  195. void WAIT_BTN_TGL(void)
  196. {
  197.     while((BTN_GPIO->IDR & 0x01) == 0);
  198.     delay();delay();
  199.    
  200. }
  201. void CONFIG_F3(void)
  202. {
  203.   //16MHZ / 2 = 8MHZ
  204.   //8MHZ * 9 = 72MHZ
  205.  
  206.   RCC->CFGR = 0x61F0002; //HSE, 9x PLL, HSE/2, HSE/PREDIV selected as PLL input clock, PLL CLOCK
  207.   RCC->CR = 0x1010000;   //PLL ENABLED, HSE ON
  208.  
  209.   while((RCC->CR & 0x2000000) == 0);  //WAIT UNTIL PLL LOCKED
  210.   //72MHZ from now on
  211.   RCC->AHBENR = 0x317E0057; //Enable all AHB Peripheral Clocks
  212. }
  213. void GPIO_OUTPUT (GPIO_TypeDef *GPIOX, uint32_t PIN, uint32_t SPEED)
  214. {
  215.   GPIOX->MODER &= ~(3<<(PIN*2));
  216.   GPIOX->MODER |=  (MODE_OUT<<(PIN*2));
  217.  
  218.   GPIOX->OSPEEDR &= ~(3<<(PIN*2));
  219.   GPIOX->OSPEEDR |= (SPEED<<(PIN*2));
  220.  
  221. }
  222. void GPIO_INPUT (GPIO_TypeDef *GPIOX, uint32_t PIN, uint32_t SPEED)
  223. {
  224.   GPIOX->MODER &= ~(3<<(PIN*2));
  225.  
  226.   GPIOX->OSPEEDR &= ~(3<<(PIN*2));
  227.   GPIOX->OSPEEDR |= (SPEED<<(PIN*2));
  228. }
  229. void GPIO_SET(GPIO_TypeDef *GPIOX, uint32_t PIN)
  230. {
  231.   GPIOX->BSRR = (1<<PIN);
  232. }
  233. void GPIO_CLR(GPIO_TypeDef *GPIOX, uint32_t PIN)
  234. {
  235.   GPIOX->BSRR = ((1<<PIN)<<16);
  236. }
  237.  
  238. //**********************************************************************
  239. //                  CODE FOR TFT
  240. //**********************************************************************
  241. void FILL_SCREEN(uint32_t COLOR)
  242. {
  243.     for(unsigned char i=0;i<2;i++)
  244.     {
  245.         for(unsigned int f=0;f<38400;f++)
  246.         {
  247.             sendData(COLOR);
  248.         }
  249.     }
  250. }
  251. void BAR_SCREEN(void)
  252. {
  253.     for(unsigned int i=0;i<320;i++)
  254.     {
  255.         for(unsigned int f=0;f<24;f++)
  256.             sendData(C_RED);
  257.  
  258.         for(unsigned int f=0;f<24;f++)
  259.             sendData(C_GREEN);
  260.  
  261.         for(unsigned int f=0;f<24;f++)
  262.             sendData(C_BLUE);
  263.        
  264.         for(unsigned int f=0;f<24;f++)
  265.             sendData(C_YELLOW);
  266.  
  267.         for(unsigned int f=0;f<24;f++)
  268.             sendData(C_BLACK);
  269.  
  270.         for(unsigned int f=0;f<24;f++)
  271.             sendData(C_WHITE);
  272.  
  273.         for(unsigned int f=0;f<24;f++)
  274.             sendData(C_CYAN);
  275.        
  276.         for(unsigned int f=0;f<24;f++)
  277.             sendData(C_BRIGHT_RED);
  278.  
  279.         for(unsigned int f=0;f<24;f++)
  280.             sendData(C_GRAY1);
  281.  
  282.         for(unsigned int f=0;f<24;f++)
  283.             sendData(C_GRAY2);
  284.  
  285.     }
  286. }
  287. void OUTPUT (GPIO_TypeDef *GPIOX, uint32_t PIN)
  288. {
  289.   GPIOX->MODER &= (0xFFFF<<(PIN*2));
  290.   GPIOX->MODER |=  (0x5555<<(PIN*2));
  291.   GPIOX->OSPEEDR |= (0xFFFF<<(PIN*2));
  292. }
  293. void all_pin_output(void)
  294. {
  295.   GPIO_OUTPUT ( RD_PORT, RD, SPEED_50 );
  296.   GPIO_OUTPUT ( CS_PORT, CS, SPEED_50 );
  297.   GPIO_OUTPUT ( WR_PORT, WR, SPEED_50 );
  298.   GPIO_OUTPUT ( RS_PORT, RS, SPEED_50 );
  299.   OUTPUT ( LCD_PORT, LCD );
  300. }
  301.  
  302. void all_pin_input(void)
  303. {
  304.   LCD_PORT->MODER &=  ~(0xFFFF<<(LCD*2));
  305.   LCD_PORT->OSPEEDR |= (0xFFFF<<(LCD*2));
  306. }
  307.  
  308. #define all_pin_low() (LCD_PORT->BSRR = ((0xFF<<LCD)<<16))
  309.  
  310. void  pushData(unsigned char data)
  311. {
  312.     //all_pin_low();
  313.     LCD_PORT->ODR = (int)((int)data<<LCD);
  314. }
  315. unsigned char  getData(void)
  316. {
  317.     unsigned char data=0;
  318.     delay();
  319.  
  320.     data = ((LCD_PORT->IDR>>LCD) & 0xFF);
  321.  
  322.     return data;
  323.  
  324. }
  325. void sendCommand(unsigned int index)
  326. {
  327.     CS_LOW;
  328.     RS_LOW;
  329.     RD_HIGH;
  330.     WR_HIGH;
  331.  
  332.     WR_LOW;
  333.     pushData(index>>8);
  334.     WR_HIGH;
  335.  
  336.     WR_LOW;
  337.     pushData(index);
  338.     WR_HIGH;
  339.  
  340.     CS_HIGH;
  341. }
  342.  
  343. void sendData(unsigned int data)
  344. {
  345.     CS_LOW;
  346.     RS_HIGH;
  347.     RD_HIGH;
  348.  
  349.     WR_LOW;
  350.     pushData(data>>8);
  351.     WR_HIGH;
  352.  
  353.     WR_LOW;
  354.     pushData(data);
  355.     WR_HIGH;
  356.  
  357.     CS_HIGH;
  358. }
  359.  
  360. unsigned int readRegister(unsigned int index)
  361. {
  362.     unsigned int data=0;
  363.  
  364.     CS_LOW;
  365.     RS_LOW;
  366.     RD_HIGH;
  367.     all_pin_output();
  368.  
  369.     WR_LOW;
  370.     pushData(index>>8);
  371.     WR_HIGH;
  372.  
  373.     WR_LOW;
  374.     pushData(index);
  375.     WR_HIGH;
  376.  
  377.     all_pin_input();
  378.     all_pin_low();
  379.     RS_HIGH;
  380.  
  381.     RD_LOW;
  382.     data = (int)getData()<<8;
  383.     RD_HIGH;
  384.  
  385.     RD_LOW;
  386.     data |= getData();
  387.     RD_HIGH;
  388.  
  389.     CS_HIGH;
  390.     all_pin_output();
  391.     return data;
  392. }
  393.  
  394.  
  395. void   init (void)
  396. {
  397.     int IC_CODE = 0;
  398.     CS_HIGH;
  399.  
  400.     all_pin_output();
  401.     all_pin_low();
  402.  
  403.     delay();
  404.     IC_CODE = readRegister(0x0);
  405.  
  406.     if(IC_CODE == 0x5408)
  407.     {
  408.         sendCommand(0x0000);
  409.         sendData(0x0001);
  410.         delay();
  411.  
  412.         sendCommand(0x0001);
  413.         sendData(0x0000);
  414.         sendCommand(0x0002);
  415.         sendData(0x0700);
  416.         sendCommand(0x0003);
  417.         sendData(0x1030);
  418.         sendCommand(0x0004);
  419.         sendData(0x0000);
  420.         sendCommand(0x0008);
  421.         sendData(0x0207);
  422.         sendCommand(0x0009);
  423.         sendData(0x0000);
  424.         sendCommand(0x000A);
  425.         sendData(0x0000);
  426.         sendCommand(0x000C);
  427.         sendData(0x0000);
  428.         sendCommand(0x000D);
  429.         sendData(0x0000);
  430.         sendCommand(0x000F);
  431.         sendData(0x0000);
  432.         //power on sequence VGHVGL
  433.         sendCommand(0x0010);
  434.         sendData(0x0000);
  435.         sendCommand(0x0011);
  436.         sendData(0x0007);
  437.         sendCommand(0x0012);
  438.         sendData(0x0000);
  439.         sendCommand(0x0013);
  440.         sendData(0x0000);
  441.         //vgh
  442.         sendCommand(0x0010);
  443.         sendData(0x1290);
  444.         sendCommand(0x0011);
  445.         sendData(0x0227);
  446.         delay();
  447.         //vregiout
  448.         sendCommand(0x0012);
  449.         sendData(0x001d); //0x001b
  450.         delay();
  451.         //vom amplitude
  452.         sendCommand(0x0013);
  453.         sendData(0x1500);
  454.         delay();
  455.         //vom H
  456.         sendCommand(0x0029);
  457.         sendData(0x0018);
  458.         sendCommand(0x002B);
  459.         sendData(0x000D);
  460.  
  461.         //gamma
  462.         sendCommand(0x0030);
  463.         sendData(0x0004);
  464.         sendCommand(0x0031);
  465.         sendData(0x0307);
  466.         sendCommand(0x0032);
  467.         sendData(0x0002);// 0006
  468.         sendCommand(0x0035);
  469.         sendData(0x0206);
  470.         sendCommand(0x0036);
  471.         sendData(0x0408);
  472.         sendCommand(0x0037);
  473.         sendData(0x0507);
  474.         sendCommand(0x0038);
  475.         sendData(0x0204);//0200
  476.         sendCommand(0x0039);
  477.         sendData(0x0707);
  478.         sendCommand(0x003C);
  479.         sendData(0x0405);// 0504
  480.         sendCommand(0x003D);
  481.         sendData(0x0F02);
  482.         //ram
  483.         sendCommand(0x0050);
  484.         sendData(0x0000);
  485.         sendCommand(0x0051);
  486.         sendData(0x00EF);
  487.         sendCommand(0x0052);
  488.         sendData(0x0000);
  489.         sendCommand(0x0053);
  490.         sendData(0x013F);
  491.         sendCommand(0x0060);
  492.         sendData(0xA700);
  493.         sendCommand(0x0061);
  494.         sendData(0x0001);
  495.         sendCommand(0x006A);
  496.         sendData(0x0000);
  497.         //
  498.         sendCommand(0x0080);
  499.         sendData(0x0000);
  500.         sendCommand(0x0081);
  501.         sendData(0x0000);
  502.         sendCommand(0x0082);
  503.         sendData(0x0000);
  504.         sendCommand(0x0083);
  505.         sendData(0x0000);
  506.         sendCommand(0x0084);
  507.         sendData(0x0000);
  508.         sendCommand(0x0085);
  509.         sendData(0x0000);
  510.         //
  511.         sendCommand(0x0090);
  512.         sendData(0x0010);
  513.         sendCommand(0x0092);
  514.         sendData(0x0600);
  515.         sendCommand(0x0093);
  516.         sendData(0x0003);
  517.         sendCommand(0x0095);
  518.         sendData(0x0110);
  519.         sendCommand(0x0097);
  520.         sendData(0x0000);
  521.         sendCommand(0x0098);
  522.         sendData(0x0000);
  523.         sendCommand(0x0007);
  524.         sendData(0x0133);
  525.  
  526.         sendCommand(0x0022);//Start to write to display RAM
  527.  
  528.     }
  529.     else
  530.     {
  531.  
  532.         sendCommand(0x0001);
  533.         sendData(0x0100);
  534.         sendCommand(0x0002);
  535.         sendData(0x0700);
  536.         sendCommand(0x0003);
  537.         sendData(0x1030);
  538.         sendCommand(0x0004);
  539.         sendData(0x0000);
  540.         sendCommand(0x0008);
  541.         sendData(0x0302);
  542.  
  543.         sendCommand(0x000A);
  544.         sendData(0x0000);
  545.         sendCommand(0x000C);
  546.         sendData(0x0000);
  547.         sendCommand(0x000D);
  548.         sendData(0x0000);
  549.         sendCommand(0x000F);
  550.         sendData(0x0000);
  551.  
  552.         delay();
  553.  
  554.         sendCommand(0x0030);
  555.         sendData(0x0000);
  556.         sendCommand(0x0031);
  557.         sendData(0x0405);
  558.         sendCommand(0x0032);
  559.         sendData(0x0203);
  560.         sendCommand(0x0035);
  561.         sendData(0x0004);
  562.         sendCommand(0x0036);
  563.         sendData(0x0B07);
  564.         sendCommand(0x0037);
  565.         sendData(0x0000);
  566.         sendCommand(0x0038);
  567.         sendData(0x0405);
  568.         sendCommand(0x0039);
  569.         sendData(0x0203);
  570.         sendCommand(0x003c);
  571.         sendData(0x0004);
  572.         sendCommand(0x003d);
  573.         sendData(0x0B07);
  574.         sendCommand(0x0020);
  575.         sendData(0x0000);
  576.         sendCommand(0x0021);
  577.         sendData(0x0000);
  578.         sendCommand(0x0050);
  579.         sendData(0x0000);
  580.         sendCommand(0x0051);
  581.         sendData(0x00ef);
  582.         sendCommand(0x0052);
  583.         sendData(0x0000);
  584.         sendCommand(0x0053);
  585.         sendData(0x013f);
  586.  
  587.         delay();
  588.  
  589.         sendCommand(0x0060);
  590.         sendData(0xa700);
  591.         sendCommand(0x0061);
  592.         sendData(0x0001);
  593.         sendCommand(0x0090);
  594.         sendData(0x003A);
  595.         sendCommand(0x0095);
  596.         sendData(0x021E);
  597.         sendCommand(0x0080);
  598.         sendData(0x0000);
  599.         sendCommand(0x0081);
  600.         sendData(0x0000);
  601.         sendCommand(0x0082);
  602.         sendData(0x0000);
  603.         sendCommand(0x0083);
  604.         sendData(0x0000);
  605.         sendCommand(0x0084);
  606.         sendData(0x0000);
  607.         sendCommand(0x0085);
  608.         sendData(0x0000);
  609.         sendCommand(0x00FF);
  610.         sendData(0x0001);
  611.         sendCommand(0x00B0);
  612.         sendData(0x140D);
  613.         sendCommand(0x00FF);
  614.         sendData(0x0000);
  615.         delay();
  616.         sendCommand(0x0007);
  617.         sendData(0x0133);
  618.         delay();
  619.         //exit standby
  620.         sendCommand(0x0010);
  621.         sendData(0x14E0);
  622.         delay();
  623.         sendCommand(0x0007);
  624.         sendData(0x0133);
  625.         sendCommand(0x0022);
  626.     }
  627.  
  628.     //paint screen black
  629.     for(unsigned char i=0;i<2;i++)
  630.     {
  631.         for(unsigned int f=0;f<38400;f++)
  632.         {
  633.             sendData(C_RED);
  634.         }
  635.     }
  636. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement