打印
[Cortex-M0技术交流]

【第四批】sh007笔记-SPI驱动1.8寸TFT

[复制链接]
4407|12
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
sh007|  楼主 | 2012-1-6 22:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 sh007 于 2012-1-6 22:36 编辑

最近公司上新项目,新平台很忙 都没写 真是惭愧!现在赶紧补上。
1.8寸TFT 是ST7735的驱动芯片,是SPI方式驱动,数据口只有五个。
赶紧上代码吧!
  

  



补几张照片 ,哈哈!

主文件
 
#include <stdio.h>
#include "NUC1xx.h"
#include "DrvUART.h"
#include "DrvSYS.h"
#include "DrvSPI.h"
#include "DrvPDMA.h"
#include "SPI_FLASH_PDMA.h"
#include "ST7735R_SPI.H"
#include "Driver\DrvGPIO.h"
#define    RW_LEN       80
#define    DelayCnt      500

extern void Test_picture(uint8_t *map);
extern unsigned char gImage_hotpower[];
extern unsigned char gImage_hotpage[];
uint8_t SPI_PDMA_RXBuf[RW_LEN];
uint8_t SPI_PDMA_TXBuf[RW_LEN];
void  RCC_Config(void);
void  UART_Config(void);

int main()
{
    RCC_Config();
    UART_Config();
    SPI_Init();
   printf("\n");
printf("+----------------------------------------------------------------------+\n");  
printf("|                 NUC120助学板之SPI ---TFT_ST7735                      |\n");
printf("+----------------------------------------------------------------------+\n");
printf("***NUC120***配置主频为: %2d MHz.\n",DrvSYS_GetHCLKFreq()/1000000);
printf("---SPI0主机模式,频率为: %2d MHz.\n", DrvSPI_GetClock1Freq(eDRVSPI_PORT0)/1000000);
    //设置PA5为输出,PA5为RESET
    DrvGPIO_Open(E_GPB,10, E_IO_OUTPUT);
GPIOB->DOUT &= ~(1 << 10);
TFT_INIT();

while(1)
{
RamAdressSet();
TFT_Single_Colour(0xf800); //红
delay(DelayCnt);
RamAdressSet();
TFT_Single_Colour(0x07e0); //绿
delay(DelayCnt);
RamAdressSet();
TFT_Single_Colour(0x001f); //蓝
delay(DelayCnt);
RamAdressSet();
TFT_Single_Colour(0xf81f); //黄
delay(DelayCnt);
RamAdressSet();
TFT_Single_Colour(0xffe0); //青
delay(DelayCnt);
RamAdressSet();
TFT_Single_Colour(0x07ff); //紫
delay(DelayCnt);
RamAdressSet();
TFT_Single_Colour(0xffff); //白
delay(DelayCnt);
   Test_picture(gImage_hotpower) ; //大叔
   delay(DelayCnt*5);
   Test_picture(gImage_hotpage) ;  //大叔小孩
   delay(DelayCnt*5);
      
}

}
void  RCC_Config(void)
{
UNLOCKREG();
    SYSCLK->WRCON.XTL12M_EN = 1;
    // Waiting for 12M Xtal stalble
    DrvSYS_Delay(5000);
//HCLK 为外部12M 通过PLL倍频到 50MHz.
DrvSYS_Open(50000000);
DrvSYS_Delay(5000);
LOCKREG();
}
void UART_Config()
{
  STR_UART_T sParam;
// Set UART I/O
DrvGPIO_InitFunction(E_FUNC_UART0);
//Select UART Clock Source From 12MHz
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC, 0);
// UART Setting
    sParam.u32BaudRate   = 9600;
    sParam.u8cDataBits   = DRVUART_DATABITS_8;
    sParam.u8cStopBits   = DRVUART_STOPBITS_1;
    sParam.u8cParity   = DRVUART_PARITY_NONE;
    sParam.u8cRxTriggerLevel= DRVUART_FIFO_1BYTES;
// Set UART Configuration
DrvUART_Open(UART_PORT0, &sParam);
}

SPI驱动TFT
void LCD_Write_CMD(uint8_t CMDTmp)
{
uint32_t au32SourceData;
//configure transaction length as 8 bits
DrvSPI_SetBitLength(eDRVSPI_PORT0, 8);

//CS =0
RS_LOW;

au32SourceData = CMDTmp;
DrvSPI_SingleWrite(eDRVSPI_PORT0, &au32SourceData);
// wait
while (DrvSPI_IsBusy(eDRVSPI_PORT0)) {}

}
void LCD_Write_DATA8B(uint8_t DataTmp)
{
uint32_t au32SourceData;

// configure transaction length as 8 bits
DrvSPI_SetBitLength(eDRVSPI_PORT0, 8);

//CS =1
RS_HIGH;
au32SourceData = DataTmp;
DrvSPI_SingleWrite(eDRVSPI_PORT0, &au32SourceData);

// wait
while (DrvSPI_IsBusy(eDRVSPI_PORT0)) {}

}

void LCD_Write_DATA16B(uint16_t LCD_Data)
{
uint32_t au32SourceData;

// configure transaction length as 8 bits
DrvSPI_SetBitLength(eDRVSPI_PORT0, 16);

//CS =1
RS_HIGH;

au32SourceData = LCD_Data;
//while(!DrvSPI_SingleWrite(eDRVSPI_PORT1, &au32SourceData)){}
DrvSPI_SingleWrite(eDRVSPI_PORT0, &au32SourceData);
// wait
while (DrvSPI_IsBusy(eDRVSPI_PORT0)) {}

}

void TFT_RESET(void)
{
RESET_LOW;
delay(100);
RESET_HIGH;
delay(100);
}
void TFT_INIT(void)
{
RESET_LOW;
delay(10);
RESET_HIGH;
delay(10);

//------------------------------------------------------------------//
//-------------------Software Reset-------------------------------//
//------------------------------------------------------------------//

LCD_Write_CMD(0x11);//Sleep out
delay(5);
//ST7735R Frame Rate
LCD_Write_CMD(0xB1);
LCD_Write_DATA8B(0x01);
LCD_Write_DATA8B(0x2C);
LCD_Write_DATA8B(0x2D);
LCD_Write_CMD(0xB2);
LCD_Write_DATA8B(0x01);
LCD_Write_DATA8B(0x2C);
LCD_Write_DATA8B(0x2D);
LCD_Write_CMD(0xB3);
LCD_Write_DATA8B(0x01);
LCD_Write_DATA8B(0x2C);
LCD_Write_DATA8B(0x2D);
LCD_Write_DATA8B(0x01);
LCD_Write_DATA8B(0x2C);
LCD_Write_DATA8B(0x2D);
//------------------------------------End ST7735R Frame Rate-----------------------------------------//
LCD_Write_CMD(0xB4);//Column inversion
LCD_Write_DATA8B(0x07);
//------------------------------------ST7735R Power Sequence-----------------------------------------//
LCD_Write_CMD(0xC0);
LCD_Write_DATA8B(0xA2);
LCD_Write_DATA8B(0x02);
LCD_Write_DATA8B(0x84);
LCD_Write_CMD(0xC1);
LCD_Write_DATA8B(0xC5);
LCD_Write_CMD(0xC2);
LCD_Write_DATA8B(0x0A);
LCD_Write_DATA8B(0x00);
LCD_Write_CMD(0xC3);
LCD_Write_DATA8B(0x8A);
LCD_Write_DATA8B(0x2A);
LCD_Write_CMD(0xC4);
LCD_Write_DATA8B(0x8A);
LCD_Write_DATA8B(0xEE);
//---------------------------------End ST7735R Power Sequence-------------------------------------//
LCD_Write_CMD(0xC5);//VCOM
LCD_Write_DATA8B(0x0E);
LCD_Write_CMD(0x36);//MX, MY, RGB mode
LCD_Write_DATA8B(0xC8);//C8 //自上而下,自左向右,BGR扫描显示。
//------------------------------------ST7735R Gamma Sequence-----------------------------------------//
LCD_Write_CMD(0xe0);
LCD_Write_DATA8B(0x02);
LCD_Write_DATA8B(0x1c);
LCD_Write_DATA8B(0x07);
LCD_Write_DATA8B(0x12);
LCD_Write_DATA8B(0x37);
LCD_Write_DATA8B(0x32);
LCD_Write_DATA8B(0x29);
LCD_Write_DATA8B(0x2d);
LCD_Write_DATA8B(0x29);
LCD_Write_DATA8B(0x25);
LCD_Write_DATA8B(0x2b);
LCD_Write_DATA8B(0x39);
LCD_Write_DATA8B(0x00);
LCD_Write_DATA8B(0x01);
LCD_Write_DATA8B(0x03);
LCD_Write_DATA8B(0x10);
LCD_Write_CMD(0xe1);
LCD_Write_DATA8B(0x03);
LCD_Write_DATA8B(0x1d);
LCD_Write_DATA8B(0x07);
LCD_Write_DATA8B(0x06);
LCD_Write_DATA8B(0x2e);
LCD_Write_DATA8B(0x2c);
LCD_Write_DATA8B(0x29);
LCD_Write_DATA8B(0x2d);
LCD_Write_DATA8B(0x2e);
LCD_Write_DATA8B(0x2e);
LCD_Write_DATA8B(0x37);
LCD_Write_DATA8B(0x3f);
LCD_Write_DATA8B(0x00);
LCD_Write_DATA8B(0x00);
LCD_Write_DATA8B(0x02);
LCD_Write_DATA8B(0x10);

LCD_Write_CMD(0x2A); //Column Address Set
LCD_Write_DATA8B(0x00);
LCD_Write_DATA8B(0x00);
LCD_Write_DATA8B(0x00);
LCD_Write_DATA8B(0x7F);

LCD_Write_CMD(0x2B); //Row Address Set
LCD_Write_DATA8B(0x00);
LCD_Write_DATA8B(0x00); //01
LCD_Write_DATA8B(0x00);
LCD_Write_DATA8B(0x9F); //A0
//------------------------------------End ST7735R Gamma Sequence-----------------------------------------//
//设置显示色彩 12位(4k,03h),16位(65k,05h),18位(262k,06h)。
LCD_Write_CMD(0x3A);
LCD_Write_DATA8B(0x05); //262k mode
LCD_Write_CMD(0x29);//Display on
delay(10);
}

void RamAdressSet(void)
{
LCD_Write_CMD(0x2A);
LCD_Write_DATA8B(0x00);
LCD_Write_DATA8B(0x02);
LCD_Write_DATA8B(0x00);
LCD_Write_DATA8B(0x81);

LCD_Write_CMD(0x2B);
LCD_Write_DATA8B(0x00);
LCD_Write_DATA8B(0x01);
LCD_Write_DATA8B(0x00);
LCD_Write_DATA8B(0xA0);

LCD_Write_CMD(0x2C);//Write DATA TO RAM
}


void LCD_DataWrite(uint8_t LCD_DataH,uint8_t LCD_DataL)
{
LCD_Write_DATA8B(LCD_DataH);
LCD_Write_DATA8B(LCD_DataL);
}


void TFT_Single_Colour(uint16_t SCDATA)
{
uint8_t i,j;
for (i=0;i<128;i++)
for (j=0;j<160;j++)
LCD_Write_DATA16B(SCDATA);
}

void Test_picture(uint8_t *map)
{
int j;
RamAdressSet();
for(j=0;j<(128*160);j++)
LCD_DataWrite(map[2*j+1],map[2*j]);
}

相关帖子

沙发
sh007|  楼主 | 2012-1-6 22:40 | 只看该作者
加上代码

NUC120_SPI0_TFT1.8.rar (231.54 KB)

使用特权

评论回复
板凳
lwslws201| | 2012-1-6 22:41 | 只看该作者
:)  帮顶把!

使用特权

评论回复
地板
hotpower| | 2012-1-7 00:06 | 只看该作者
很好!

使用特权

评论回复
5
feiyinglala| | 2012-1-7 00:25 | 只看该作者
哎呀,后辈对您的佩服如滔滔江水

使用特权

评论回复
6
ljhtj| | 2012-1-7 09:26 | 只看该作者
好,支持

使用特权

评论回复
7
封剑藏刀| | 2012-1-7 20:24 | 只看该作者
不错,那个屏的价格怎么样。

使用特权

评论回复
8
Swallow_0322| | 2012-1-8 19:33 | 只看该作者
顶!很不错!

使用特权

评论回复
9
ddllxxrr| | 2012-1-14 18:03 | 只看该作者
真不错,楼主辛苦啦

使用特权

评论回复
10
jasontu| | 2012-1-30 16:07 | 只看该作者
不知你這樣的frame rate 如何?

使用特权

评论回复
11
lixiaoxu2meng| | 2012-1-30 16:50 | 只看该作者

使用特权

评论回复
12
sh007|  楼主 | 2012-2-5 19:25 | 只看该作者
这个没认真算过,这种只能说学习用,做产品 是不太现实的。除非要求不高! 10# jasontu

使用特权

评论回复
13
cnjxlxp| | 2012-2-9 21:11 | 只看该作者
请问能不能上传屏的手册和如何连接屏和MCU的电路图,或者说明那个引脚接哪个引脚啊

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

174

帖子

1

粉丝