本帖最后由 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驱动TFTvoid 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]);
}
|