第四号程序:2.2‘TFTLCD显示,用到PB3,4,5. 其中有一个奇怪问题。PD12需要外接上位电阻,置1时才能将电平拉高,如果不接上位电阻,即使内部使能输出上拉,置1电平也只能达1.5V。PD7作普通IO,置内部上拉高电平正常。只有PD12不行。查了PD12也没有什么特殊功能。换个STM32F303VCT6芯片,内部上拉,只能将电平拉到1V。目前此原因还没找到。
main.c
//CPU:stm32f373vct6
//PB0ָʾµÆÿÃëÉÁ˸һ´Î,IO¿Ú°ëÃ뷴תһ´Î
//TIMER3 TIMEBASE
//2.2 TFT LCD(LTM022A69B)
//Display a string\circle\rectangle on the LCD.
//Designed By Cache.Lee 2012.12.5
#include "stm32f37x.h"
#include "LTM022A69B.h"
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOE, ENABLE);
/* LEDS */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void TIM_INIT(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 10000; //10ms¶¨Ê±Ê±¼ä
TIM_TimeBaseStructure.TIM_Prescaler = 71;//1M ¼ÆÊýƵÂÊ ×¢Òâ´ËֵΪ16λ,²»Äܳ¬¹ý65535 ·ñÔò¼ÆÊý´íÎó
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* TIM Interrupts enable */
TIM_ITConfig(TIM3,TIM_IT_Update, ENABLE);
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);
}
void TIM_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Enable the TIM3 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
main()
{
GPIO_Config();
TIM_Config();//´ËÓï¾äÒªÔÚTIM_INIT();֮ǰ ·ñÔòÎÞ·¨½øÖжÏ
TIM_INIT();
LCD_Init();
LCD_Clear(Red);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
LCD_DisplayStringLine(Line4, "2.2 LCD tft ");
LCD_DrawLine(0,0,10,LCD_DIR_VERTICAL);
LCD_DrawRect(7,20,30,50);
LCD_DrawCircle(100,100,50);
while(1);
}
void TIM3_IRQHandler(void)
{
static unsigned int count=0;
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
count++;
if(count>=50)//°ëÃ뷴תһ´Î
{
GPIOB->ODR ^= GPIO_Pin_0;
count=0;
}
}
}
LTM022A69B.C文件
#include "LTM022A69B.h"
#include "fonts.c"
#include "stm32f37x.h"
const uint color[]={0xf800,0x07e0,0x001f,0xffe0,0x0000,0xffff,0x07ff,0xf81f};
static sFONT *LCD_Currentfonts;
static __IO uint16_t TextColor = 0x0000, BackColor = 0xFFFF;
//=============================================
void Delay(unsigned int t)
{
unsigned int n,m;
for(n=0;n<t;n++)
for(m=0;m<600;m++);
}
void LCD_WR_REG(uchar index)
{
uchar i;
LCD_CS0;
LCD_RS0;
for(i=0;i<8;i++)
{
LCD_SCL0;
LCD_SCI0;
LCD_SCL1;
}
for(i=0;i<8;i++)
{
LCD_SCL0;
if(index&0x80)
LCD_SCI1;
else
LCD_SCI0;
index=index<<1;
LCD_SCL1;
}
LCD_CS1;
}
void LCD_WR_CMD(uchar index,uint val)
{
uchar i;
LCD_WR_REG(index);
LCD_RS1;
LCD_CS0;
for(i=0;i<16;i++)
{
LCD_SCL0;
if(val&0x8000)
LCD_SCI1;
else
LCD_SCI0;
LCD_SCL1;
val=val<<1;
}
LCD_CS1;
}
void LCD_WR_Data(uint val)
{
uchar i;
for(i=0;i<16;i++)
{
LCD_SCL0;
if(val&0x8000) LCD_SCI1;
else LCD_SCI0;
val=val<<1;
LCD_SCL1;
}
}
void LCD_SetFont(sFONT *fonts)
{
LCD_Currentfonts = fonts;
}
void LCD_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD|RCC_AHBPeriph_GPIOB, ENABLE);
// GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_15);
// GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_15);
// GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_15);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_1);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_5|GPIO_Pin_4;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_12;
GPIO_Init(GPIOD,&GPIO_InitStructure);
Delay(100);
LCD_RST0;
Delay(100);
LCD_RST1;
Delay(100);
//register reset
LCD_WR_CMD(0x0003,0x0001);
//oscillator start
LCD_WR_CMD(0x003A,0x0001);
Delay(10);
//y-setting
LCD_WR_CMD(0x0024,0x007B);
Delay(1);
LCD_WR_CMD(0x0025,0x003B);
LCD_WR_CMD(0x0026,0x0034);
Delay(1);
LCD_WR_CMD(0x0027,0x0004);
LCD_WR_CMD(0x0052,0x0025);
Delay(1);
Delay(1);
LCD_WR_CMD(0x0053,0x0033);
LCD_WR_CMD(0x0061,0x001C);
Delay(1);
LCD_WR_CMD(0x0062,0x002C);
LCD_WR_CMD(0x0063,0x0022);
Delay(1);
LCD_WR_CMD(0x0064,0x0027);
Delay(1);
LCD_WR_CMD(0x0065,0x0014);
Delay(1);
LCD_WR_CMD(0x0066,0x0010);
//Basical clock for 1 line (BASECOUNT[7:0]) number specified
LCD_WR_CMD(0x002E,0x002D);
//Power supply setting
LCD_WR_CMD(0x0019,0x0000);
Delay(20);
LCD_WR_CMD(0x001A,0x1000);
LCD_WR_CMD(0x001B,0x0023);
LCD_WR_CMD(0x001C,0x0C01);
LCD_WR_CMD(0x001D,0x0000);
LCD_WR_CMD(0x001E,0x0009);
LCD_WR_CMD(0x001F,0x0035);
LCD_WR_CMD(0x0020,0x0015);
LCD_WR_CMD(0x0018,0x1E7B);
//windows setting
LCD_WR_CMD(0x0008,0x0000);
LCD_WR_CMD(0x0009,0x00EF);
LCD_WR_CMD(0x000a,0x0000);
LCD_WR_CMD(0x000b,0x013F);
//LCD display area setting
LCD_WR_CMD(0x0029,0x0000);
LCD_WR_CMD(0x002A,0x0000);
LCD_WR_CMD(0x002B,0x00EF);
LCD_WR_CMD(0x002C,0x013F);
//Gate scan setting
LCD_WR_CMD(0x0032,0x0002);
//n line inversion line number
LCD_WR_CMD(0x0033,0x0000);
//Line inversion/frame inversion/interlace setting
LCD_WR_CMD(0x0037,0x0000);
//GOE1,GOE2 signal start
LCD_WR_CMD(0x003B,0x0001);
//Color mode
LCD_WR_CMD(0x0004,0x0000);
//windows mode setting
LCD_WR_CMD(0x0005,0x0014);
//Display setting register 2
LCD_WR_CMD(0x0001,0x0040);
//display setting
LCD_WR_CMD(0x0000,0x0000);//display on
LCD_SetFont(&LCD_DEFAULT_FONT);
}
void LCD_test()
{
uint temp,num,i;
uchar n;
LCD_WR_CMD(0x08,0x00);
LCD_WR_CMD(0x0a,0x0000);
LCD_WR_CMD(0x09,0xEF);
LCD_WR_CMD(0x0b,0x013F);
LCD_WR_CMD(0x06,0x0000);
LCD_WR_CMD(0x07,0x0000);
LCD_WR_REG(0x0E);
LCD_CS0;
LCD_RS1;
for(n=0;n<8;n++)
{
temp=color[n];
for(num=40*240;num>0;num--)
LCD_WR_Data(temp);
}
for(n=0;n<8;n++)
{
LCD_WR_CMD(0x08,0x00);
LCD_WR_CMD(0x0a,0x0000);
LCD_WR_CMD(0x09,0xEF);
LCD_WR_CMD(0x0b,0x013F);
LCD_WR_CMD(0x06,0x0000);
LCD_WR_CMD(0x07,0x0000);
LCD_WR_REG(0x0E);
LCD_CS0;
LCD_RS1;
temp=color[n];
for(i=0;i<240;i++)
for(num=0;num<320;num++)
LCD_WR_Data(temp);
}
LCD_CS1;
}
void LCD_Clear(uint p)
{
uint i,j;
LCD_WR_CMD(0x08,0x00);
LCD_WR_CMD(0x0a,0x0000);
LCD_WR_CMD(0x09,0xEF);
LCD_WR_CMD(0x0b,0x013F);
LCD_WR_CMD(0x06,0x0000);
LCD_WR_CMD(0x07,0x0000);
LCD_WR_REG(0x0E);
LCD_CS0;
LCD_RS1;
for(i=0;i<240;i++)
{
for(j=0;j<320;j++)
{
LCD_WR_Data(p);//Delay(1);
}
}
LCD_CS1;
}
void PutPixel(uint dx,uint dy)
{
LCD_WR_CMD(0x08,0x00);
LCD_WR_CMD(0x0a,0x0000);
LCD_WR_CMD(0x09,0xEF);
LCD_WR_CMD(0x0b,0x013F);
LCD_WR_CMD(0x06,dx);
LCD_WR_CMD(0x07,dy);
LCD_WR_CMD(0x0E,TextColor);
}
void LCD_DrawChar(uint8_t Xpos, uint16_t Ypos, const uint16_t *c)
{
uint32_t index = 0, i = 0,k;
LCD_WR_CMD(0x08,Xpos); //x start point
LCD_WR_CMD(0x0a,Ypos); //y start point
LCD_WR_CMD(0x09,Xpos+LCD_Currentfonts->Height-1); //x end point
LCD_WR_CMD(0x0b,Ypos+LCD_Currentfonts->Width-1); //y end point
LCD_WR_CMD(0x06,Xpos);
LCD_WR_CMD(0x07,Ypos);
LCD_WR_REG(0x0E);
LCD_CS0;
LCD_RS1;
for(index = 0; index < LCD_Currentfonts->Height; index++)
{
for(i = 0; i < LCD_Currentfonts->Width; i++)
{
k=i;//LCD_Currentfonts->Width-i;
if((((c[index] & ((0x80 << ((LCD_Currentfonts->Width / 12 ) * 8 ) ) >> k)) == 0x00) &&(LCD_Currentfonts->Width <= 12))||
(((c[index] & (0x1 << k)) == 0x00)&&(LCD_Currentfonts->Width > 12 )))
{
LCD_WR_Data(BackColor);
}
else
{
LCD_WR_Data(TextColor);
}
}
}
LCD_CS1;
}
sFONT *LCD_GetFont(void)
{
return LCD_Currentfonts;
}
void LCD_DisplayChar(uint8_t Line, uint16_t Column, uint8_t Ascii)
{
Ascii -= 32;
LCD_DrawChar(Line, Column, &LCD_Currentfonts->table[Ascii * LCD_Currentfonts->Height]);
}
void LCD_DisplayStringLine(uint8_t Line, uint8_t *ptr)
{
uint16_t refcolumn = 0;//LCD_PIXEL_WIDTH - 1;
/* Send the string character by character on lCD */
while ((*ptr != 0) & (((refcolumn + LCD_Currentfonts->Width) & 0xFFFF) >= LCD_Currentfonts->Width))
{
/* Display one character on LCD */
LCD_DisplayChar(Line, refcolumn, *ptr);
/* Decrement the column position by 16 */
refcolumn += LCD_Currentfonts->Width;
/* Point on the next character */
ptr++;
}
}
void LCD_SetBackColor(__IO uint16_t Color)
{
BackColor = Color;
}
void LCD_SetTextColor(__IO uint16_t Color)
{
TextColor = Color;
}
void LCD_SetColors(__IO uint16_t _TextColor, __IO uint16_t _BackColor)
{
TextColor = _TextColor;
BackColor = _BackColor;
}
void LCD_GetColors(__IO uint16_t *_TextColor, __IO uint16_t *_BackColor)
{
*_TextColor = TextColor; *_BackColor = BackColor;
}
void LCD_ClearLine(uint8_t Line)
{
uint16_t refcolumn = LCD_PIXEL_WIDTH - 1;
/* Send the string character by character on lCD */
while (((refcolumn + 1)& 0xFFFF) >= LCD_Currentfonts->Width)
{
/* Display one character on LCD */
LCD_DisplayChar(Line, refcolumn, ' ');
/* Decrement the column position by 16 */
refcolumn -= LCD_Currentfonts->Width;
}
}
void LCD_SetCursor(uint8_t Xpos, uint16_t Ypos)
{
LCD_WR_CMD(0x06,Xpos);
LCD_WR_CMD(0x07,Ypos);
}
void LCD_SetDisplayWindow(uint8_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width)
{
LCD_WR_CMD(0x08,Xpos); //x start point
LCD_WR_CMD(0x0a,Ypos); //y start point
LCD_WR_CMD(0x09,Xpos+LCD_Currentfonts->Height-1); //x end point
LCD_WR_CMD(0x0b,Ypos+LCD_Currentfonts->Width-1); //y end point
}
void LCD_DrawLine(uint8_t Xpos, uint16_t Ypos, uint16_t Length, uint8_t Direction)
{
uint32_t i = 0;
if(Direction == LCD_DIR_HORIZONTAL)
{
for(i = 0; i < Length; i++)
{
PutPixel(Xpos,Ypos+i);
}
}
else
{
for(i = 0; i < Length; i++)
{
PutPixel(Xpos+i,Ypos);
}
}
}
void LCD_DrawRect(uint8_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width)
{
LCD_DrawLine(Xpos, Ypos, Width, LCD_DIR_HORIZONTAL);
LCD_DrawLine((Xpos + Height), Ypos, Width, LCD_DIR_HORIZONTAL);
LCD_DrawLine(Xpos, Ypos, Height, LCD_DIR_VERTICAL);
LCD_DrawLine(Xpos, (Ypos + Width - 1), Height, LCD_DIR_VERTICAL);
}
void LCD_DrawCircle(uint8_t Xpos, uint16_t Ypos, uint16_t Radius)
{
int32_t D;/* Decision Variable */
uint32_t CurX;/* Current X Value */
uint32_t CurY;/* Current Y Value */
//Ypos=320-Ypos;
D = 3 - (Radius << 1);
CurX = 0;
CurY = Radius;
while (CurX <= CurY)
{
PutPixel(Xpos + CurX, Ypos + CurY);
PutPixel(Xpos + CurX, Ypos - CurY);
PutPixel(Xpos - CurX, Ypos + CurY);
PutPixel(Xpos - CurX, Ypos - CurY);
PutPixel(Xpos + CurY, Ypos + CurX);
PutPixel(Xpos + CurY, Ypos - CurX);
PutPixel(Xpos - CurY, Ypos + CurX);
PutPixel(Xpos - CurY, Ypos - CurX);
if (D < 0)
{
D += (CurX << 2) + 6;
}
else
{
D += ((CurX - CurY) << 2) + 10;
CurY--;
}
CurX++;
}
}
LTM022A69B.H文件
#ifndef _LTM022A69B_H_
#define _LTM022A69B_H_
#include "stm32f37x.h"
#include "fonts.h"
#define uchar unsigned char
#define uint unsigned int
#define LCD_RST0 GPIO_ResetBits(GPIOD,GPIO_Pin_12)
#define LCD_RST1 GPIO_SetBits(GPIOD,GPIO_Pin_12)
#define LCD_CS1 GPIO_SetBits(GPIOD,GPIO_Pin_7)
#define LCD_CS0 GPIO_ResetBits(GPIOD,GPIO_Pin_7)
#define LCD_RS1 GPIO_SetBits(GPIOB,GPIO_Pin_4)
#define LCD_RS0 GPIO_ResetBits(GPIOB,GPIO_Pin_4)
#define LCD_SCL0 GPIO_ResetBits(GPIOB,GPIO_Pin_3)
#define LCD_SCL1 GPIO_SetBits(GPIOB,GPIO_Pin_3)
#define LCD_SCI0 GPIO_ResetBits(GPIOB,GPIO_Pin_5)
#define LCD_SCI1 GPIO_SetBits(GPIOB,GPIO_Pin_5)
#define LCD_COLOR_WHITE 0xFFFF
#define LCD_COLOR_BLACK 0x0000
#define LCD_COLOR_GREY 0xF7DE
#define LCD_COLOR_BLUE 0x001F
#define LCD_COLOR_BLUE2 0x051F
#define LCD_COLOR_RED 0xF800
#define LCD_COLOR_MAGENTA 0xF81F
#define LCD_COLOR_GREEN 0x07E0
#define LCD_COLOR_CYAN 0x7FFF
#define LCD_COLOR_YELLOW 0xFFE0
#define White LCD_COLOR_WHITE
#define Black LCD_COLOR_BLACK
#define Grey LCD_COLOR_GREY
#define Blue LCD_COLOR_BLUE
#define Blue2 LCD_COLOR_BLUE2
#define Red LCD_COLOR_RED
#define Magenta LCD_COLOR_MAGENTA
#define Green LCD_COLOR_GREEN
#define Cyan LCD_COLOR_CYAN
#define Yellow LCD_COLOR_YELLOW
#define LCD_LINE_0 LINE(0)
#define LCD_LINE_1 LINE(1)
#define LCD_LINE_2 LINE(2)
#define LCD_LINE_3 LINE(3)
#define LCD_LINE_4 LINE(4)
#define LCD_LINE_5 LINE(5)
#define LCD_LINE_6 LINE(6)
#define LCD_LINE_7 LINE(7)
#define LCD_LINE_8 LINE(8)
#define LCD_LINE_9 LINE(9)
#define LCD_LINE_10 LINE(10)
#define LCD_LINE_11 LINE(11)
#define LCD_LINE_12 LINE(12)
#define LCD_LINE_13 LINE(13)
#define LCD_LINE_14 LINE(14)
#define LCD_LINE_15 LINE(15)
#define LCD_LINE_16 LINE(16)
#define LCD_LINE_17 LINE(17)
#define LCD_LINE_18 LINE(18)
#define LCD_LINE_19 LINE(19)
#define LCD_LINE_20 LINE(20)
#define LCD_LINE_21 LINE(21)
#define LCD_LINE_22 LINE(22)
#define LCD_LINE_23 LINE(23)
#define LCD_LINE_24 LINE(24)
#define LCD_LINE_25 LINE(25)
#define LCD_LINE_26 LINE(26)
#define LCD_LINE_27 LINE(27)
#define LCD_LINE_28 LINE(28)
#define LCD_LINE_29 LINE(29)
#define Line0 LCD_LINE_0
#define Line1 LCD_LINE_1
#define Line2 LCD_LINE_2
#define Line3 LCD_LINE_3
#define Line4 LCD_LINE_4
#define Line5 LCD_LINE_5
#define Line6 LCD_LINE_6
#define Line7 LCD_LINE_7
#define Line8 LCD_LINE_8
#define Line9 LCD_LINE_9
#define LCD_PIXEL_WIDTH 0x0140
#define LCD_PIXEL_HEIGHT 0x00F0
#define LCD_DIR_HORIZONTAL 0x0000
#define LCD_DIR_VERTICAL 0x0001
#define LCD_DEFAULT_FONT Font16x24
void Delay(unsigned int t);
void LCD_WR_REG(uchar index);
void LCD_WR_CMD(uchar index,uint val);
void LCD_WR_Data(uint val);
void LCD_SetFont(sFONT *fonts);
void LCD_Init(void);
void LCD_test(void);
void LCD_Clear(uint p);
void PutPixel(uint dx,uint dy);
void LCD_DrawChar(uint8_t Xpos, uint16_t Ypos, const uint16_t *c);
sFONT *LCD_GetFont(void);
void LCD_DisplayChar(uint8_t Line, uint16_t Column, uint8_t Ascii);
void LCD_DisplayStringLine(uint8_t Line, uint8_t *ptr);
void LCD_SetBackColor(__IO uint16_t Color);
void LCD_SetTextColor(__IO uint16_t Color);
void LCD_SetColors(__IO uint16_t _TextColor, __IO uint16_t _BackColor);
void LCD_GetColors(__IO uint16_t *_TextColor, __IO uint16_t *_BackColor);
void LCD_ClearLine(uint8_t Line);
void LCD_SetCursor(uint8_t Xpos, uint16_t Ypos);
void LCD_SetDisplayWindow(uint8_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width);
void LCD_DrawLine(uint8_t Xpos, uint16_t Ypos, uint16_t Length, uint8_t Direction);
void LCD_DrawRect(uint8_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width);
void LCD_DrawCircle(uint8_t Xpos, uint16_t Ypos, uint16_t Radius);
#endif
|