PIC24F16KL402开发板使用笔记六: SPI驱动2.2TFT LCD

使用内总RC振荡,4倍频,提供给SPI模块,速度还是不太理想。
SPI四分频
 

  1. /*
  2. * File: main_tftlcd.c
  3. * Author: Administrator
  4. *
  5. * Created on 2015?4?4?, ??3:41
  6. */
  7. #include "xc.h"
  8. #include "lcd.h"
  9. _FBS
  10. (
  11. BWRP_OFF & // Boot Segment Write Protect (Disabled)
  12. BSS_OFF // Boot segment Protect (No boot flash segment)
  13. )
  14. //_FGS
  15. //(
  16. // GWRP_OFF & // General Segment Flash Write Protect (General segment may be written)
  17. // GSS0_OFF // General Segment Code Protect (No Protection)
  18. //)
  19. _FOSCSEL
  20. (
  21. FNOSC_FRCPLL & // Oscillator Select (8MHz FRC with Postscaler (FRCDIV))
  22. // SOSCSRC_ANA & // SOSC Source Type (Analog Mode for use with crystal)
  23. // LPRCSEL_HP & // LPRC Power and Accuracy (High Power/High Accuracy)
  24. IESO_ON // Internal External Switch Over bit (Internal External Switchover mode enabled (Two-speed Start-up enabled))
  25. )
  26. _FOSC
  27. (
  28. // POSCMD_HS & // Primary Oscillator Mode (Primary oscillator disabled)
  29. OSCIOFNC_ON & // CLKO Enable Configuration bit (CLKO output signal enabled)
  30. POSCFREQ_MS & // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock frequency between 100kHz to 8MHz)
  31. SOSCSEL_SOSCHP &// SOSC Power Selection Configuration bits (Secondary Oscillator configured for high-power operation)
  32. FCKSM_CSECME // Clock Switching and Monitor Selection (Clock Switching and Fail-safe Clock Monitor Enabled)
  33. )
  34. _FWDT
  35. (
  36. // WDTPS_PS32768 & // Watchdog Timer Postscale Select bits (1:32768)
  37. FWPSA_PR128 & // WDT Prescaler bit (WDT prescaler ratio of 1:128)
  38. FWDTEN_OFF & // Watchdog Timer Enable bits (WDT disabled in hardware; SWDTEN bit disabled)
  39. WINDIS_OFF // Windowed Watchdog Timer Disable bit (Standard WDT selected (windowed WDT disabled))
  40. )
  41. // Warning:
  42. // Always enable MCLRE_ON config bit setting so that the MCLR pin function will
  43. // work for low-voltage In-Circuit Serial Programming (ICSP). The Microstick
  44. // programming circuitry only supports low-voltage ICSP. If you disable MCLR pin
  45. // functionality, a high-voltage ICSP tool will be required to re-program the
  46. // part in the future.
  47. _FPOR
  48. (
  49. BOREN_BOR3 & // Brown-out Reset Enable bits (Enabled in hardware; SBOREN bit disabled)
  50. PWRTEN_ON & // Power-up Timer Enable (PWRT enabled)
  51. I2C1SEL_PRI & // Alternate I2C1 Pin Mapping bit (Default SCL1/SDA1 Pins for I2C1)
  52. BORV_V18 & // Brown-out Reset Voltage bits (Brown-out Reset at 1.8V)
  53. MCLRE_ON // MCLR Pin Enable bit (RA5 input disabled; MCLR enabled)
  54. )
  55. _FICD
  56. (
  57. ICS_PGx3 // ICD Pin Placement Select (EMUC/EMUD share PGC3/PGD3)
  58. )
  59. unsigned char data_temp;
  60. void init_channel()
  61. {
  62. //Init SPI pins.
  63. //As master SDI1 is input, SDO1 is ouput, SCK1 is output, SS is output.
  64. ;
  65. }
  66. void init_SPI1_IO()
  67. {
  68. ANSAbits.ANSA0 =0;
  69. ANSAbits.ANSA1 = 0;
  70. ANSBbits.ANSB14 = 0;
  71. ANSBbits.ANSB13 = 0;
  72. TRISBbits.TRISB10 = 1; //Set SDI1 as an input.
  73. TRISBbits.TRISB13 = 0; //Set SDO1 as an output.
  74. TRISBbits.TRISB11 = 0; //Set SCK1 as an output.
  75. TRISBbits.TRISB15 = 0; //Set SS1 as an output.
  76. TRISAbits.TRISA0 = 0; //LCD DC pin.
  77. TRISAbits.TRISA1 = 0; //LCD RST pin.
  78. TRISBbits.TRISB14 = 0; //LCD CS pin.
  79. TRISBbits.TRISB3 = 0;
  80. PORTAbits.RA0 = PORTAbits.RA1=PORTBbits.RB14 = 1;
  81. }
  82. void init_SPI1(void)
  83. {
  84. // SPI1CON1 setting 
  85. SSP1STAT =0b0000000010000000; //0b 0000 0000 1000 0000 0x0000; 
  86. SSP1CON1 = 0b0000000010110000; //0x0021;//
  87. //SSP1CON3bits.BOEN = 0;
  88. SSP1ADD = 0x00fe;
  89. PADCFG1 = 0x0c00; //0b0000 1100 0000 0000
  90. // IFS1bits.SSP1IF = 0;
  91. // IEC1bits.SSP1IE = 1;
  92. // IPC4bits.SSP1IP = 4;
  93. }
  94. void tx_spi_data(unsigned char dat)
  95. {
  96. SSP1BUF = dat;
  97. //while(!IFS1bits.SSP1IF);
  98. while(SSP1STATbits.BF)
  99. // IFS1bits.SSP1IF = 0;
  100. data_temp = SSP1BUF;
  101. }
  102. void delay(unsigned int uiCnt)
  103. {
  104. int i,j;
  105. for(i=0;i<uiCnt;i++)
  106. for(j=0;j<10;j++);
  107. }
  108. int main(void) 
  109. init_SPI1_IO();
  110. init_SPI1();
  111. Lcd_Init(); //tft初始化
  112. LCD_Clear(BLACK); //清屏
  113. BACK_COLOR=BLACK;;POINT_COLOR=WHITE; 
  114. while(1)
  115. // Lcd_Init(); //tft初始化
  116. LCD_Clear(RED);
  117. delayms(300);
  118. LCD_Clear(GREEN);
  119. delayms(300);
  120. LCD_Clear(BLUE);
  121. delayms(300);
  122. }
  123. return 0;
  124. }
  125. void __attribute__((interrupt,no_auto_psv)) _SPI1Interrupt(void)
  126. {
  127. //IFS1bits.SSP1IF = 0; //Clear Interrupt status of SPI1
  128. // IEC1bits.SSP1IE = 1;
  129. }
复制代码
  1. #include "font.h"
  2. #include "sys.h"
  3. #include "lcd.h"
  4. //unsigned char bitdata;
  5. extern void tx_spi_data(unsigned char dat);
  6. u16 BACK_COLOR, POINT_COLOR; 
  7. void LCD_Writ_Bus(char da) 
  8. tx_spi_data(da);
  9. void LCD_WR_DATA8(char da) //·¢ËÍÊý¾Ý-8λ²ÎÊý
  10. {
  11. LCD_DC=1;
  12. LCD_Writ_Bus(da);
  13. void LCD_WR_DATA(int da)
  14. {
  15. LCD_DC=1;
  16. LCD_Writ_Bus(da>>8);
  17. LCD_Writ_Bus(da);
  18. void LCD_WR_REG(char da) 
  19. {
  20. LCD_DC=0;
  21. LCD_Writ_Bus(da);
  22. }
  23. void LCD_WR_REG_DATA(int reg,int da)
  24. {
  25. LCD_WR_REG(reg);
  26. LCD_WR_DATA(da);
  27. }
  28. void Address_set(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2)
  29. LCD_WR_REG(0x2a);
  30. LCD_WR_DATA8(x1>>8);
  31. LCD_WR_DATA8(x1);
  32. LCD_WR_DATA8(x2>>8);
  33. LCD_WR_DATA8(x2);
  34. LCD_WR_REG(0x2b);
  35. LCD_WR_DATA8(y1>>8);
  36. LCD_WR_DATA8(y1);
  37. LCD_WR_DATA8(y2>>8);
  38. LCD_WR_DATA8(y2);
  39. LCD_WR_REG(0x2C);
  40. }
  41. void Lcd_Init(void)
  42. {
  43. LCD_CS =1;
  44. LCD_REST=1;
  45. delayms(5); 
  46. LCD_REST=0;
  47. delayms(5);
  48. LCD_REST=1;
  49. LCD_CS=1; 
  50. delayms(5);
  51. LCD_CS =0;
  52. LCD_WR_REG(0xCB); 
  53. LCD_WR_DATA8(0x39); 
  54. LCD_WR_DATA8(0x2C); 
  55. LCD_WR_DATA8(0x00); 
  56. LCD_WR_DATA8(0x34); 
  57. LCD_WR_DATA8(0x02); 
  58. LCD_WR_REG(0xCF); 
  59. LCD_WR_DATA8(0x00); 
  60. LCD_WR_DATA8(0XC1); 
  61. LCD_WR_DATA8(0X30); 
  62. LCD_WR_REG(0xE8); 
  63. LCD_WR_DATA8(0x85); 
  64. LCD_WR_DATA8(0x00); 
  65. LCD_WR_DATA8(0x78); 
  66. LCD_WR_REG(0xEA); 
  67. LCD_WR_DATA8(0x00); 
  68. LCD_WR_DATA8(0x00); 
  69. LCD_WR_REG(0xED); 
  70. LCD_WR_DATA8(0x64); 
  71. LCD_WR_DATA8(0x03); 
  72. LCD_WR_DATA8(0X12); 
  73. LCD_WR_DATA8(0X81); 
  74. LCD_WR_REG(0xF7); 
  75. LCD_WR_DATA8(0x20); 
  76. LCD_WR_REG(0xC0); //Power control 
  77. LCD_WR_DATA8(0x23); //VRH[5:0] 
  78. LCD_WR_REG(0xC1); //Power control 
  79. LCD_WR_DATA8(0x10); //SAP[2:0];BT[3:0] 
  80. LCD_WR_REG(0xC5); //VCM control 
  81. LCD_WR_DATA8(0x3e); //¶Ô±È¶Èµ÷½Ú
  82. LCD_WR_DATA8(0x28); 
  83. LCD_WR_REG(0xC7); //VCM control2 
  84. LCD_WR_DATA8(0x86); //--
  85. LCD_WR_REG(0x36); // Memory Access Control 
  86. LCD_WR_DATA8(0x48); //C8 //48 68ÊúÆÁ//28 E8 ºáÆÁ
  87. LCD_WR_REG(0x3A); 
  88. LCD_WR_DATA8(0x55); 
  89. LCD_WR_REG(0xB1); 
  90. LCD_WR_DATA8(0x00); 
  91. LCD_WR_DATA8(0x18); 
  92. LCD_WR_REG(0xB6); // Display Function Control 
  93. LCD_WR_DATA8(0x08); 
  94. LCD_WR_DATA8(0x82);
  95. LCD_WR_DATA8(0x27); 
  96. LCD_WR_REG(0xF2); // 3Gamma Function Disable 
  97. LCD_WR_DATA8(0x00); 
  98. LCD_WR_REG(0x26); //Gamma curve selected 
  99. LCD_WR_DATA8(0x01); 
  100. LCD_WR_REG(0xE0); //Set Gamma 
  101. LCD_WR_DATA8(0x0F); 
  102. LCD_WR_DATA8(0x31); 
  103. LCD_WR_DATA8(0x2B); 
  104. LCD_WR_DATA8(0x0C); 
  105. LCD_WR_DATA8(0x0E); 
  106. LCD_WR_DATA8(0x08); 
  107. LCD_WR_DATA8(0x4E); 
  108. LCD_WR_DATA8(0xF1); 
  109. LCD_WR_DATA8(0x37); 
  110. LCD_WR_DATA8(0x07); 
  111. LCD_WR_DATA8(0x10); 
  112. LCD_WR_DATA8(0x03); 
  113. LCD_WR_DATA8(0x0E); 
  114. LCD_WR_DATA8(0x09); 
  115. LCD_WR_DATA8(0x00); 
  116. LCD_WR_REG(0XE1); //Set Gamma 
  117. LCD_WR_DATA8(0x00); 
  118. LCD_WR_DATA8(0x0E); 
  119. LCD_WR_DATA8(0x14); 
  120. LCD_WR_DATA8(0x03); 
  121. LCD_WR_DATA8(0x11); 
  122. LCD_WR_DATA8(0x07); 
  123. LCD_WR_DATA8(0x31); 
  124. LCD_WR_DATA8(0xC1); 
  125. LCD_WR_DATA8(0x48); 
  126. LCD_WR_DATA8(0x08); 
  127. LCD_WR_DATA8(0x0F); 
  128. LCD_WR_DATA8(0x0C); 
  129. LCD_WR_DATA8(0x31); 
  130. LCD_WR_DATA8(0x36); 
  131. LCD_WR_DATA8(0x0F); 
  132. LCD_WR_REG(0x11); //Exit Sleep 
  133. delayms(120); 
  134. LCD_WR_REG(0x29); //Display on 
  135. LCD_WR_REG(0x2c); 
  136. }
  137. //ÇåÆÁº¯Êý
  138. //Color:ÒªÇåÆÁµÄÌî³äÉ«
  139. void LCD_Clear(u16 Color)
  140. {
  141. u8 VH,VL;
  142. u16 i,j;
  143. VH=Color>>8;
  144. VL=Color; 
  145. Address_set(0,0,LCD_W-1,LCD_H-1);
  146. for(i=0;i<LCD_W;i++)
  147. {
  148. for (j=0;j<LCD_H;j++)
  149. {
  150. LCD_WR_DATA8(VH);
  151. LCD_WR_DATA8(VL); 
  152. }
  153. }
  154. }
  155. //ÔÚÖ¸¶¨Î»ÖÃÏÔʾһ¸öºº×Ö(32*33´óС)
  156. //dcolorΪÄÚÈÝÑÕÉ«£¬gbcolorΪ±³¾²ÑÕÉ«
  157. void showhanzi(unsigned int x,unsigned int y,unsigned char index) 
  158. unsigned char i,j;
  159. unsigned char *temp=hanzi; 
  160. Address_set(x,y,x+31,y+31); //ÉèÖÃÇøÓò 
  161. temp+=index*128; 
  162. for(j=0;j<128;j++)
  163. {
  164. for(i=0;i<8;i++)
  165. if((*temp&(1<<i))!=0)
  166. {
  167. LCD_WR_DATA(POINT_COLOR);
  168. else
  169. {
  170. LCD_WR_DATA(BACK_COLOR);
  171. }
  172. temp++;
  173. }
  174. }
  175. //»&shy;µã
  176. //POINT_COLOR:´ËµãµÄÑÕÉ«
  177. void LCD_DrawPoint(u16 x,u16 y)
  178. {
  179. Address_set(x,y,x,y);//ÉèÖùâ±êλÖà
  180. LCD_WR_DATA(POINT_COLOR); 
  181. //»&shy;Ò»¸ö´óµã
  182. //POINT_COLOR:´ËµãµÄÑÕÉ«
  183. void LCD_DrawPoint_big(u16 x,u16 y)
  184. {
  185. LCD_Fill(x-1,y-1,x+1,y+1,POINT_COLOR);
  186. //ÔÚÖ¸¶¨ÇøÓòÄÚÌî³äÖ¸¶¨ÑÕÉ«
  187. //ÇøÓò´óС:
  188. // (xend-xsta)*(yend-ysta)
  189. void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color)
  190. u16 i,j; 
  191. Address_set(xsta,ysta,xend,yend); //ÉèÖùâ±êλÖà
  192. for(i=ysta;i<=yend;i++)
  193. for(j=xsta;j<=xend;j++)LCD_WR_DATA(color);//ÉèÖùâ±êλÖà
  194. //»&shy;Ïß
  195. //x1,y1:Æðµã×ø±ê
  196. //x2,y2:ÖÕµã×ø±ê 
  197. void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2)
  198. {
  199. u16 t; 
  200. int xerr=0,yerr=0,delta_x,delta_y,distance; 
  201. int incx,incy,uRow,uCol; 
  202. delta_x=x2-x1; //¼ÆËã×ø±êÔöÁ¿ 
  203. delta_y=y2-y1; 
  204. uRow=x1; 
  205. uCol=y1; 
  206. if(delta_x>0)incx=1; //ÉèÖõ¥²½·½Ïò 
  207. else if(delta_x==0)incx=0;//´¹Ö±Ïß 
  208. else {incx=-1;delta_x=-delta_x;} 
  209. if(delta_y>0)incy=1; 
  210. else if(delta_y==0)incy=0;//ˮƽÏß 
  211. else{incy=-1;delta_y=-delta_y;} 
  212. if( delta_x>delta_y)distance=delta_x; //Ñ¡È¡»ù±¾ÔöÁ¿×ø±êÖá 
  213. else distance=delta_y; 
  214. for(t=0;t<=distance+1;t++ )//»&shy;ÏßÊä³ö 
  215. LCD_DrawPoint(uRow,uCol);//»&shy;µã 
  216. xerr+=delta_x ; 
  217. yerr+=delta_y ; 
  218. if(xerr>distance) 
  219. xerr-=distance; 
  220. uRow+=incx; 
  221. if(yerr>distance) 
  222. yerr-=distance; 
  223. uCol+=incy; 
  224. //»&shy;¾ØÐÎ
  225. void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2)
  226. {
  227. LCD_DrawLine(x1,y1,x2,y1);
  228. LCD_DrawLine(x1,y1,x1,y2);
  229. LCD_DrawLine(x1,y2,x2,y2);
  230. LCD_DrawLine(x2,y1,x2,y2);
  231. }
  232. //ÔÚÖ¸¶¨Î»Öû&shy;Ò»¸öÖ¸¶¨´óСµÄÔ²
  233. //(x,y):ÖÐÐĵã
  234. //r :°ë¾¶
  235. void Draw_Circle(u16 x0,u16 y0,u8 r)
  236. {
  237. int a,b;
  238. int di;
  239. a=0;b=r; 
  240. di=3-(r<<1); //ÅжÏϸöµãλÖõıêÖ¾
  241. while(a<=b)
  242. {
  243. LCD_DrawPoint(x0-b,y0-a); //3 
  244. LCD_DrawPoint(x0+b,y0-a); //0 
  245. LCD_DrawPoint(x0-a,y0+b); //1 
  246. LCD_DrawPoint(x0-b,y0-a); //7 
  247. LCD_DrawPoint(x0-a,y0-b); //2 
  248. LCD_DrawPoint(x0+b,y0+a); //4 
  249. LCD_DrawPoint(x0+a,y0-b); //5
  250. LCD_DrawPoint(x0+a,y0+b); //6 
  251. LCD_DrawPoint(x0-b,y0+a); 
  252. a++;
  253. //ʹÓÃBresenhamËã·¨»&shy;Ô² 
  254. if(di<0)di +=4*a+6; 
  255. else
  256. {
  257. di+=10+4*(a-b); 
  258. b--;
  259. LCD_DrawPoint(x0+a,y0+b);
  260. }
  261. //ÔÚÖ¸¶¨Î»ÖÃÏÔʾһ¸ö×Ö·û
  262. //num:ÒªÏÔʾµÄ×Ö·û:" "--->"~"
  263. //mode:µþ¼Ó·½Ê½(1)»¹ÊǷǵþ¼Ó·½Ê½(0)
  264. //ÔÚÖ¸¶¨Î»ÖÃÏÔʾһ¸ö×Ö·û
  265. //num:ÒªÏÔʾµÄ×Ö·û:" "--->"~"
  266. //mode:µþ¼Ó·½Ê½(1)»¹ÊǷǵþ¼Ó·½Ê½(0)
  267. void LCD_ShowChar(u16 x,u16 y,u8 num,u8 mode)
  268. {
  269. u8 temp;
  270. u8 pos,t;
  271. u16 x0=x;
  272. u16 colortemp=POINT_COLOR; 
  273. if(x>LCD_W-16||y>LCD_H-16)return; 
  274. //ÉèÖô°¿Ú 
  275. num=num-' ';//µÃµ½Æ«ÒƺóµÄÖµ
  276. Address_set(x,y,x+8-1,y+16-1); //ÉèÖùâ±êλÖà
  277. if(!mode) //·Çµþ¼Ó·½Ê½
  278. {
  279. for(pos=0;pos<16;pos++)
  280. temp=asc2_1608[(u16)num*16+pos]; //µ÷ÓÃ1608×ÖÌå
  281. for(t=0;t<8;t++)
  282. if(temp&0x01)POINT_COLOR=colortemp;
  283. else POINT_COLOR=BACK_COLOR;
  284. LCD_WR_DATA(POINT_COLOR); 
  285. temp>>=1; 
  286. x++;
  287. }
  288. x=x0;
  289. y++;
  290. }else//µþ¼Ó·½Ê½
  291. {
  292. for(pos=0;pos<16;pos++)
  293. {
  294. temp=asc2_1608[(u16)num*16+pos]; //µ÷ÓÃ1608×ÖÌå
  295. for(t=0;t<8;t++)
  296. if(temp&0x01)LCD_DrawPoint(x+t,y+pos);//»&shy;Ò»¸öµã 
  297. temp>>=1; 
  298. }
  299. }
  300. }
  301. POINT_COLOR=colortemp; 
  302. //m^nº¯Êý
  303. u32 mypow(u8 m,u8 n)
  304. {
  305. u32 result=1; 
  306. while(n--)result*=m; 
  307. return result;
  308. //ÏÔʾ2¸öÊý×Ö
  309. //x,y :Æðµã×ø±ê 
  310. //len :Êý×ÖµÄλÊý
  311. //color:ÑÕÉ«
  312. //num:ÊýÖµ(0~4294967295); 
  313. void LCD_ShowNum(u16 x,u16 y,u32 num,u8 len)
  314. u8 t,temp;
  315. u8 enshow=0;
  316. num=(u16)num;
  317. for(t=0;t<len;t++)
  318. {
  319. temp=(num/mypow(10,len-t-1))%10;
  320. if(enshow==0&&t<(len-1))
  321. {
  322. if(temp==0)
  323. {
  324. LCD_ShowChar(x+8*t,y,' ',0);
  325. continue;
  326. }else enshow=1; 
  327. }
  328. LCD_ShowChar(x+8*t,y,temp+48,0); 
  329. }
  330. //ÏÔʾ2¸öÊý×Ö
  331. //x,y:Æðµã×ø±ê
  332. //num:ÊýÖµ(0~99); 
  333. void LCD_Show2Num(u16 x,u16 y,u16 num,u8 len)
  334. u8 t,temp; 
  335. for(t=0;t<len;t++)
  336. {
  337. temp=(num/mypow(10,len-t-1))%10;
  338. LCD_ShowChar(x+8*t,y,temp+'0',0); 
  339. }
  340. //ÏÔʾ×Ö·û´®
  341. //x,y:Æðµã×ø±ê 
  342. //*p:×Ö·û´®ÆðʼµØÖ·
  343. //ÓÃ16×ÖÌå
  344. void LCD_ShowString(u16 x,u16 y,const u8 *p)
  345. while(*p!='\0')
  346. if(x>LCD_W-16){x=0;y+=16;}
  347. if(y>LCD_H-16){y=x=0;}
  348. LCD_ShowChar(x,y,*p,0);
  349. x+=8;
  350. p++;
  351. }
粽子糖果 发表于10-26 10:56 浏览65535次
分享到:

已有0条评论

暂时还没有回复哟,快来抢沙发吧

添加一条新评论

只有登录用户才能评论,请先登录注册哦!

话题作者

粽子糖果
粽子糖果(总统)
金币:41623个|学分:51975个
立即注册
畅学电子网,带你进入电子开发学习世界
专业电子工程技术学习交流社区,加入畅学一起充电加油吧!

x

畅学电子网订阅号