`之前使用AVR 单片机做了音乐频谱的显示,可是发现其速度太慢,而且内存太小,所以现在使用了 STM32,换了一块大的液晶屏,并采用UCOS ii以方便以后功能的添加。FFT使用了官方的DSP库,256点变换。效果图:
视频演示地址:http://www.tudou.com/programs/view/G1Wd8GeM9Sw/
下面是部分源代码:
TFT.c
- #include "TFT.h"
- U16 TFT_ChipID;
- static void __FSMCConfig(void)
- {
- FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
- FSMC_NORSRAMTimingInitTypeDef p;
- /*-- FSMC Configuration ------------------------------------------------------*/
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
- /* FSMC_Bank1_NORSRAM4 timing configuration */
- p.FSMC_AddressSetupTime = 1;
- p.FSMC_AddressHoldTime = 0;
- p.FSMC_DataSetupTime = 5;
- p.FSMC_BusTurnAroundDuration = 0;
- p.FSMC_CLKDivision = 0;
- p.FSMC_DataLatency = 0;
- p.FSMC_AccessMode = FSMC_AccessMode_B;
- /* FSMC_Bank1_NORSRAM4 configured as follows:
- - Data/Address MUX = Disable
- - Memory Type = SRAM
- - Data Width = 16bit
- - Write Operation = Enable
- - Extended Mode = Disable
- - Asynchronous Wait = Disable */
- FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM4;
- FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
- FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR;
- FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
- FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
- FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
- FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
- FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
- FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
- FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
- FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
- FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
- FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
- FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
- FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
- /* Enable FSMC_Bank1_NORSRAM4 */
- FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM4, ENABLE);
- }
- static void __GpioConfig(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- #ifndef FSMC_TFT
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOD, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(TFT_DATA_PIN, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = TFT_CS|TFT_RS|TFT_WR|TFT_RD|TFT_RESET;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(TFT_COTROL_PIN, &GPIO_InitStructure);
-
- GPIO_ResetBits(TFT_COTROL_PIN, TFT_RESET);
- GPIO_SetBits(TFT_COTROL_PIN, TFT_CS);
- GPIO_SetBits(TFT_COTROL_PIN, TFT_RS);
- GPIO_SetBits(TFT_COTROL_PIN, TFT_WR);
- GPIO_SetBits(TFT_COTROL_PIN, TFT_RD);
- GPIO_SetBits(TFT_COTROL_PIN, TFT_RESET);
- #else
- /*FSMC*/
- /* Enable GPIOD, GPIOE, GPIOF, GPIOG and AFIO clocks */
- RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
- RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG |
- RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE);
- /* Set PG.15(REST), Out put*/
- GPIO_InitStructure.GPIO_Pin = TFT_RESET;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(TFT_COTROL_PIN, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = TFT_LED;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- /* Set PD.00(D2), PD.01(D3), PD.04(NOE), PD.05(NWE), PD.08(D13), PD.09(D14),
- PD.10(D15), PD.14(D0), PD.15(D1) as alternate
- function push pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 |
- GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 |
- GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- /* Set PE.07(D4), PE.08(D5), PE.09(D6), PE.10(D7), PE.11(D8), PE.12(D9), PE.13(D10),
- PE.14(D11), PE.15(D12) as alternate function push pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
- GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |
- GPIO_Pin_15;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
- /* Set PF.00(A0 (RS)) as alternate function push pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_Init(GPIOF, &GPIO_InitStructure);
- /* Set PG.12(NE4 (LCD/CS)) as alternate function push pull - CE3(LCD /CS) */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_Init(GPIOG, &GPIO_InitStructure);
- GPIO_SetBits(TFT_COTROL_PIN, TFT_RESET);
- #endif /*FSMC_TFT*/
- }
- #ifndef FSMC_TFT
- static void __DB_AS_InPut(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- // DB15--0
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(TFT_DATA_PIN, &GPIO_InitStructure);
- }
- static void __DB_AS_OutPut(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- // DB15--0
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(TFT_DATA_PIN, &GPIO_InitStructure);
- }
- #endif /*FSMC_TFT*/
- static void __WriteReg(U32 tftReg,U32 tftRegValue)
- {
- #ifndef FSMC_TFT
- /* Write 16-bit Index, then Write Reg */
- GPIO_ResetBits (TFT_COTROL_PIN, TFT_CS);//ClrCs
- GPIO_ResetBits (TFT_COTROL_PIN, TFT_RS);//ClrRs
- GPIO_ResetBits (TFT_COTROL_PIN, TFT_WR);//ClrWr
- GPIO_Write (TFT_DATA_PIN, tftReg);//LCD_Write(TFT_Reg);
- GPIO_SetBits (TFT_COTROL_PIN, TFT_WR);//SetWr
- /* Write 16-bit Reg */
- GPIO_SetBits (TFT_COTROL_PIN, TFT_RS);//SetRs
- GPIO_ResetBits (TFT_COTROL_PIN, TFT_WR);//ClrWr
- GPIO_Write (TFT_DATA_PIN, tftRegValue);//LCD_Write(TFT_RegValue);
- GPIO_SetBits (TFT_COTROL_PIN, TFT_WR);//SetWr
- GPIO_SetBits (TFT_COTROL_PIN, TFT_CS);//SetCs
- #else
- /*FSMC*/
- /* Write 16-bit Index, then Write Reg */
- TFT->TFT_REG = tftReg;
- /* Write 16-bit Reg */
- TFT->TFT_RAM = tftRegValue;
- #endif /*FSMC_TFT*/
- }
- static U32 __ReadReg(U32 tftReg)
- {
-
- #ifndef FSMC_TFT
- U32 data;
- /* Write 16-bit Index (then Read Reg) */
- GPIO_ResetBits (TFT_COTROL_PIN, TFT_CS);//ClrCs
- GPIO_ResetBits (TFT_COTROL_PIN, TFT_RS);//ClrRs
- GPIO_ResetBits (TFT_COTROL_PIN, TFT_WR);//ClrWr
- GPIO_Write (TFT_DATA_PIN, TFT_Reg);//LCD_Write(LCD_Reg);
- GPIO_SetBits (TFT_COTROL_PIN, TFT_WR);//SetWr
- /* Read 16-bit Reg */
- GPIO_SetBits (TFT_COTROL_PIN, TFT_RS);//SetRs
- __DB_AS_InPut();
- GPIO_ResetBits (TFT_COTROL_PIN, TFT_RD);//ClrRd
- GPIO_SetBits (TFT_COTROL_PIN, TFT_RD);//SetRd
- data = GPIO_ReadInputData(TFT_DATA_PIN);
- GPIO_SetBits (TFT_COTROL_PIN, TFT_CS);//SetCs
- __DB_AS_OutPut();
- return data;
- #else
- /*FSMC*/
- /* Write 16-bit Index (then Read Reg) */
- TFT->TFT_REG = tftReg;
- /* Read 16-bit Reg */
- //data = TFT->TFT_RAM;
- return (TFT->TFT_RAM);
- #endif /*FSMC_TFT*/
-
- }
- #if 0
- static void __WriteGRAM(U16 *pRGB_Code,U32 pixelNum)
- {
- #ifndef FSMC_TFT
- //u32 i=0;
- GPIO_ResetBits(TFT_COTROL_PIN, TFT_CS);//ClrCs
- GPIO_ResetBits(TFT_COTROL_PIN, TFT_RS);//ClrRs
- GPIO_ResetBits(TFT_COTROL_PIN, TFT_WR);//ClrWr
- GPIO_Write(TFT_DATA_PIN, 0x0022);
- GPIO_SetBits(TFT_COTROL_PIN, TFT_WR);//SetWr
- GPIO_SetBits(TFT_COTROL_PIN, TFT_RS);//SetRs
- while(pixelNum>0)
- {
- GPIO_ResetBits(TFT_COTROL_PIN, TFT_WR);//ClrWr
- GPIO_Write(TFT_DATA_PIN, *pRGB_Code);
- GPIO_SetBits(TFT_COTROL_PIN, TFT_WR);//SetWr
- pixelNum--;
- pRGB_Code++;
- }
- GPIO_SetBits(TFT_COTROL_PIN, TFT_CS);//SetCs
- #else
- /*FSMC*/
- /* Write 16-bit Index, then Write Reg */
- TFT->TFT_REG = 0x0022;
- /* Write 16-bit RGB */
- while(pixelNum>0)
- {
- TFT->TFT_RAM = *pRGB_Code;
- pixelNum--;
- pRGB_Code++;
- }
- #endif /*FSMC_TFT*/
- }
- #endif
- void TFT_ClearAll(U32 color)
- {
- U32 i=0;
-
- __WriteReg(0x50, 0x0000); // Horizontal GRAM Start Address
- __WriteReg(0x51, 0x00EF); // Horizontal GRAM End Address
- __WriteReg(0x52, 0x0000); // Vertical GRAM Start Address
- __WriteReg(0x53, 0x013F); // Vertical GRAM Start Address
-
- __WriteReg(0x20, 0); // GRAM horizontal Address
- __WriteReg(0x21, 0);
-
- /* Write 16-bit Index, then Write Reg */
- TFT->TFT_REG = 0x0022;
-
- while(i
- {
- /* Write 16-bit RGB */
- TFT->TFT_RAM = color;
- i++;
- }
- }
- void TFT_SetBackLED(U32 data)
- {
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
- TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
- TIM_OCInitStructure.TIM_Pulse=data;
- TIM_OC3Init(TIM3, &TIM_OCInitStructure);
- }
- U8 TFT_Init(void)
- {
- U8 statues=0;
- U32 i;
- __GpioConfig();
- __FSMCConfig();
- TFT_ChipID=__ReadReg(0);
- __WriteReg(0xE5, 0x8000); // Set the internal vcore voltage
- __WriteReg(0x00,0x0001);
- for(i=50000;i>0;i--);
- __WriteReg(0x01,0x0100); //Driver Output Contral. 0x0100
- __WriteReg(0x02,0x0700); //LCD Driver Waveform Contral.
- __WriteReg(0x03,0x0030); //Entry Mode Set. 0x1018 0x1030
-
- __WriteReg(0x04,0x0000); //Scalling Contral.
- __WriteReg(0x08,0x0202); //Display Contral 2.(0x0207)
- __WriteReg(0x09,0x0000); //Display Contral 3.(0x0000)
- __WriteReg(0x0a,0x0000); //Frame Cycle Contal.(0x0000)
- __WriteReg(0x0c,0x0000); //Extern Display Interface Contral 1.(0x0000)
- __WriteReg(0x0d,0x0000); //Frame Maker Position.
- __WriteReg(0x0f,0x0000); //Extern Display Interface Contral 2.
- __WriteReg(0x2b,0x0020); //frame rate and color control(0x0000)
-
-
- __WriteReg(0x10,0x0000); //Power Control 1.(0x16b0)
- __WriteReg(0x11,0x0004); //Power Control 2.(0x0001)
- __WriteReg(0x12,0x0000); //Power Control 3.(0x0138)
- __WriteReg(0x13,0x0000); //Power Control 4.
- for(i=60000;i>0;i--);
-
- __WriteReg(0x10, 0x17B0); // SAP, BT[3:0], AP, DSTB, SLP, STB
- __WriteReg(0x11, 0x0001); // DC1[2:0], DC0[2:0], VC[2:0]
- for(i=60000;i>0;i--); // Delay 50ms
- __WriteReg(0x12, 0x013e); // VREG1OUT voltage
- for(i=60000;i>0;i--); // Delay 50ms
- __WriteReg(0x13, 0x1c00); // VDV[4:0] for VCOM amplitude
- __WriteReg(0x29, 0x001e); // VCM[4:0] for VCOMH
- for(i=60000;i>0;i--);
-
- __WriteReg(0x20, 0x0000); // GRAM horizontal Address
- __WriteReg(0x21, 0x0000); // GRAM Vertical Address
-
- // ----------- Adjust the Gamma Curve ----------//
- __WriteReg(0x30, 0x0002);
- __WriteReg(0x31, 0x0606);
- __WriteReg(0x32, 0x0501);
- __WriteReg(0x35, 0x0206);
- __WriteReg(0x36, 0x0504);
- __WriteReg(0x37, 0x0707);
- __WriteReg(0x38, 0x0306);
- __WriteReg(0x39, 0x0007);
- __WriteReg(0x3C, 0x0700);
- __WriteReg(0x3D, 0x0700);
- //------------------ Set GRAM area ---------------//
- __WriteReg(0x50, 0x0000); // Horizontal GRAM Start Address
- __WriteReg(0x51, 0x00EF); // Horizontal GRAM End Address
- __WriteReg(0x52, 0x0000); // Vertical GRAM Start Address
- __WriteReg(0x53, 0x013F); // Vertical GRAM End Address
- __WriteReg(0x60, 0x2700); // Gate Scan Line
- __WriteReg(0x61, 0x0001); // NDL,VLE, REV
- __WriteReg(0x6A, 0x0000); // set scrolling line
- //-------------- Partial Display Control ---------//
- __WriteReg(0x80, 0x0000);
- __WriteReg(0x81, 0x0000);
- __WriteReg(0x82, 0x0000);
- __WriteReg(0x83, 0x0000);
- __WriteReg(0x84, 0x0000);
- __WriteReg(0x85, 0x0000);
- //-------------- Panel Control -------------------//
- __WriteReg(0x90, 0x0010);
- __WriteReg(0x92, 0x0000);
- __WriteReg(0x93, 0x0003);
- __WriteReg(0x95, 0x0110);
- __WriteReg(0x97, 0x0000);
- __WriteReg(0x98, 0x0000);
- __WriteReg(0x07, 0x0173); // 262K color and display ON
- TFT_ClearAll(BACKGROUND_COLOR);//黑屏
- return statues;
- }
- void TFT_SetPixelIndex(S32 x, S32 y, U32 pixelIndex)
- {
- if((x<0)||(y<0)||(x>=TFT_H_PIXEL)||(y>=TFT_V_PIXEL))
- return;
- __WriteReg(0x20, x); // GRAM horizontal Address
- __WriteReg(0x21, y); // GRAM Vertical Address
- /* Write 16-bit Index, then Write Reg */
- TFT->TFT_REG = 0x0022;
- /* Write 16-bit RGB */
- TFT->TFT_RAM = pixelIndex;
- }
- U16 TFT_GetPixelIndex(S32 x, S32 y)
- {
- U32 tempData=0;
- if((x<0)||(y<0)||(x>=TFT_H_PIXEL)||(y>=TFT_V_PIXEL))
- return 0;
- __WriteReg(0x20, x); // GRAM horizontal Address
- __WriteReg(0x21, y); // GRAM Vertical Address
- /* Write 16-bit Index, then Write Reg */
- TFT->TFT_REG = 0x0022;
- /* Write 16-bit RGB */
- tempData=TFT->TFT_RAM;
- tempData=TFT->TFT_RAM;
- return (tempData);
- }
- void TFT_XorPoint(S32 x,S32 y)
- {
- U32 temp=0;
- if((x<0)||(y<0)||(x>=TFT_H_PIXEL)||(y>=TFT_V_PIXEL))
- return;
- __WriteReg(0x20, x); // GRAM horizontal Address
- __WriteReg(0x21, y); // GRAM Vertical Address
- /* Write 16-bit Index, then Write Reg */
- TFT->TFT_REG = 0x0022;
- /* Write 16-bit RGB */
- temp = TFT->TFT_RAM;
- temp = TFT->TFT_RAM;
- TFT->TFT_RAM = 0xFFFF-temp;
- }
- void TFT_DrawHLine(S32 x0, S32 y, S32 x1, U32 color)
- {
- U32 pixelNum =0;
- if((x0>=TFT_H_PIXEL)||(y>=TFT_V_PIXEL)||(y<0))
- return ;
- if(x0<0)
- x0=0;
- if(x1>=TFT_H_PIXEL)
- x1=TFT_H_PIXEL-1;
- pixelNum = x1-x0+1;
- __WriteReg(0x50, x0); // Horizontal GRAM Start Address
- __WriteReg(0x51, x1); // Horizontal GRAM End Address
- __WriteReg(0x52, y); // Vertical GRAM Start Address
- __WriteReg(0x53, y); // Vertical GRAM End Address
-
- __WriteReg(0x20, x0); // GRAM horizontal Address
- __WriteReg(0x21, y); // GRAM Vertical Address
-
- /* Write 16-bit Index, then Write Reg */
- TFT->TFT_REG = 0x0022;
- /* Write 16-bit RGB */
- while(pixelNum>0)
- {
- TFT->TFT_RAM = color;
- pixelNum--;
- }
- __WriteReg(0x50, 0x0000); // Horizontal GRAM Start Address
- __WriteReg(0x51, 0x00EF); // Horizontal GRAM End Address
- __WriteReg(0x52, 0x0000); // Vertical GRAM Start Address
- __WriteReg(0x53, 0x013F); // Vertical GRAM End Address
- }
- void TFT_DrawVLine(S32 x, S32 y0, S32 y1, U32 color)
- {
- U32 pixelNum =0;
- if((x>=TFT_H_PIXEL)||(y0>=TFT_V_PIXEL)||(x<0))
- return ;
- if(y1>=TFT_V_PIXEL)
- y1=TFT_V_PIXEL-1;
- pixelNum = y1-y0+1;
- __WriteReg(0x50, x); // Horizontal GRAM Start Address
- __WriteReg(0x51, x); // Horizontal GRAM End Address
- __WriteReg(0x52, y0); // Vertical GRAM Start Address
- __WriteReg(0x53, y1); // Vertical GRAM End Address
-
- __WriteReg(0x20, x); // GRAM horizontal Address
- __WriteReg(0x21, y0); // GRAM Vertical Address
-
- /* Write 16-bit Index, then Write Reg */
- TFT->TFT_REG = 0x0022;
- /* Write 16-bit RGB */
- while(pixelNum>0)
- {
- TFT->TFT_RAM = color;
- pixelNum--;
- }
- __WriteReg(0x50, 0x0000); // Horizontal GRAM Start Address
- __WriteReg(0x51, 0x00EF); // Horizontal GRAM End Address
- __WriteReg(0x52, 0x0000); // Vertical GRAM Start Address
- __WriteReg(0x53, 0x013F); // Vertical GRAM End Address
- }
- void TFT_FillRect(S32 x0, S32 y0, S32 x1, S32 y1,U32 color)
- {
- U32 pixelNum =0;
- if((x0>=TFT_H_PIXEL)||(y0>=TFT_V_PIXEL))
- return ;
- if(x0<0)
- x0=0;
- if(y0<0)
- y0=0;
- if(x1>=TFT_H_PIXEL)
- x1=TFT_H_PIXEL-1;
- if(y1>=TFT_V_PIXEL)
- y1=TFT_V_PIXEL-1;
- pixelNum = (x1-x0+1)*(y1-y0+1);
- __WriteReg(0x50, x0); // Horizontal GRAM Start Address
- __WriteReg(0x51, x1); // Horizontal GRAM End Address
- __WriteReg(0x52, y0); // Vertical GRAM Start Address
- __WriteReg(0x53, y1); // Vertical GRAM End Address
-
- __WriteReg(0x20, x0); // GRAM horizontal Address
- __WriteReg(0x21, y0); // GRAM Vertical Address
-
- /* Write 16-bit Index, then Write Reg */
- TFT->TFT_REG = 0x0022;
- /* Write 16-bit RGB */
- while(pixelNum>0)
- {
- TFT->TFT_RAM = color;
- pixelNum--;
- }
- __WriteReg(0x50, 0x0000); // Horizontal GRAM Start Address
- __WriteReg(0x51, 0x00EF); // Horizontal GRAM End Address
- __WriteReg(0x52, 0x0000); // Vertical GRAM Start Address
- __WriteReg(0x53, 0x013F); // Vertical GRAM End Address
- }
- void TFT_InverseRect(S32 x0, S32 y0, S32 x1, S32 y1)
- {
- U32 pixelNum =0;
- U16 tempData=0;
- if((x0>=TFT_H_PIXEL)||(y0>=TFT_V_PIXEL))
- return ;
- if(x0<0)
- x0=0;
- if(y0<0)
- y0=0;
- if(x1>=TFT_H_PIXEL)
- x1=TFT_H_PIXEL-1;
- if(y1>=TFT_V_PIXEL)
- y1=TFT_V_PIXEL-1;
- pixelNum = (x1-x0+1)*(y1-y0+1);
- __WriteReg(0x50, x0); // Horizontal GRAM Start Address
- __WriteReg(0x51, x1); // Horizontal GRAM End Address
- __WriteReg(0x52, y0); // Vertical GRAM Start Address
- __WriteReg(0x53, y1); // Vertical GRAM End Address
-
- __WriteReg(0x20, x0); // GRAM horizontal Address
- __WriteReg(0x21, y0); // GRAM Vertical Address
-
- /* Write 16-bit Index, then Write Reg */
- TFT->TFT_REG = 0x0022;
- /* Write 16-bit RGB */
- while(pixelNum>0)
- {
- tempData=TFT->TFT_RAM;
- tempData=TFT->TFT_RAM;
- tempData=0xffff-tempData;
- TFT->TFT_RAM = tempData;
- pixelNum--;
- }
- __WriteReg(0x50, 0x0000); // Horizontal GRAM Start Address
- __WriteReg(0x51, 0x00EF); // Horizontal GRAM End Address
- __WriteReg(0x52, 0x0000); // Vertical GRAM Start Address
- __WriteReg(0x53, 0x013F); // Vertical GRAM End Address
- }
- void TFT_PutChar(FontStyle_Typedef *pFontStyle, S32 x,S32 y,S8 data)
- {
- U8 i,j,tempData;
- OU8 *p;
- data -= 0x20;
- p = &pFontStyle->pFontSize->pFontBuffer[data * pFontStyle->pFontSize->FontVNum * pFontStyle->pFontSize->FontHNum];
- for (i = 0; i < pFontStyle->pFontSize->FontHigh; i++)
- {
- tempData = p[i];
- for (j=0;jpFontSize->FontWide;j++)
- {
- if ((tempData&0x01)==0x01)
- TFT_SetPixelIndex(x+j,y+i,pFontStyle->FontColor);
- else
- {
- if(pFontStyle->FontBackgroundColor<=0xffff)
- TFT_SetPixelIndex(x+j,y+i,pFontStyle->FontBackgroundColor);
- }
- tempData>>=1;
- }
- }
- }
- void TFT_PutString(FontStyle_Typedef *pFontStyle, S32 x, S32 y, S8 *pData)
- {
- S32 xStart=x;
- if(x<0)
- xStart=0;
- while(*pData)
- {
- switch(*pData)
- {
- case '
- ':
- {
- y+=pFontStyle->pFontSize->FontHigh;
- }break;
- case ' ':
- {
- x+=((4-((x - xStart) / pFontStyle->pFontSize->FontWide % 4)) * pFontStyle->pFontSize->FontWide);
- if(x>(TFT_H_PIXEL-pFontStyle->pFontSize->FontWide))
- {
- x=xStart;
- y+=pFontStyle->pFontSize->FontHigh;
- }
- }break;
- case '
':
- {
- x=xStart;
- }break;
- default:
- {
- if((*pData>=0x20)&&(*pData<=0x7e))
- {
- TFT_PutChar(pFontStyle,x,y,*pData);
- x+=pFontStyle->pFontSize->FontWide;
- if(x>(TFT_H_PIXEL-pFontStyle->pFontSize->FontWide))
- {
- x=xStart;
- y+=pFontStyle->pFontSize->FontHigh;
- }
- }
- /*
- else if((*pChar>=0xa1)&&(*pChar<=0xf7))
- {
- LCD_PutDZK16(x,y,pBuffer,&ZDK_RAM_Data[GetFontAddr(pChar)],FontColor,BackgroundColor);
- x+=16;
- if(x>(LCD_LevelPixels-16))
- {
- x=0;
- y+=16;
- }
- pChar++;
- }
- */
- else
- {
- TFT_PutChar(pFontStyle,x,y,'');
- x+=pFontStyle->pFontSize->FontWide;
- if(x>(TFT_H_PIXEL-pFontStyle->pFontSize->FontWide))
- {
- x=xStart;
- y+=pFontStyle->pFontSize->FontHigh;
- }
- }
- }break;
- }
- pData++;
- }
- }
复制代码
Task.c
- #include "Task.h"
- #include
- #define FFT_VALUE_START_NUM 3
- #define MAG_VALUE_MAX 700
- #define MAG_NUM 80
- S32 OutFFT[NPT],InFFT[NPT],MagFFT[MAG_NUM],DisplayFFT[2][MAG_NUM];
- OS_STK Task_MusicSpectrumStk[TASK_MUSIC_SPECTRUM_STK_SIZE];
- OS_STK Task_StatusBarStk[TASK_STATUS_BAR_STK_SIZE];
- OS_EVENT *UseTFTSem;
- static void __DrawVBar(U32 x,U32 y,U32 len,U32 data0,U32 data1)
- {
- U32 y1;
- if(data0>len-2)
- data0=len-2;
- if(data1>len)
- data1=len;
- y1=y+len;
- TFT_FillRect(x, y, x+1, y1-data0,0x0);
- TFT_FillRect(x, y1-data0, x+1, y1,0xe7e0);
- TFT_FillRect(x, y1-data1, x+1, y1-data1+1,0xffff);
- }
- void Task_MusicSpectrum(void *p_arg)
- {
- INT8U err;
- U32 i;
- float x,y;
- (void) p_arg;
- OS_CPU_SysTickInit();
- while(1)
- {
- //输入需要FFT变换的数据
- for(i=0;i
- {
- InFFT[i]=ADC_ConvertedValue[i]&0xffffffc;
- InFFT[i]<<=20;
- }
- //再次启动ADC的DMA
- ADC_RestartDMA(NPT);
- //FFT变换
- cr4_fft_256_stm32(OutFFT, InFFT, NPT);
- //功率计算
- for(i=0;i
- {
- x=NPT*((float)((OutFFT[i+FFT_VALUE_START_NUM]<<16)>>16))/32768; /* sine_cosine --> cos */
- y=NPT*((float)(OutFFT[i+FFT_VALUE_START_NUM] >> 16))/32768; /* sine_cosine --> sin */
- MagFFT[i]=(U32)(sqrt(x*x+ y*y)/NPT*MAG_VALUE_MAX);
- if(MagFFT[i]>DisplayFFT[0][i])
- DisplayFFT[0][i]=MagFFT[i];
- else if(DisplayFFT[0][i]>1)
- DisplayFFT[0][i]-=2;
- else
- DisplayFFT[0][i]=0;
- if(MagFFT[i]>DisplayFFT[1][i])
- {
- DisplayFFT[1][i]=MagFFT[i];
- }
- else if(DisplayFFT[1][i]>2)
- {
- DisplayFFT[1][i]-=1;
- }
- else
- {
- DisplayFFT[1][i]=2;
- }
- //显示
- //TFT操作,等待其他任务释放资源
- OSSemPend(UseTFTSem,0,&err);
- __DrawVBar(i*3,17,301,DisplayFFT[0][i],DisplayFFT[1][i]);
- //TFT操作结束,释放TFT资源的占用
- OSSemPost(UseTFTSem);
- }
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- void Task_StatusBar(void *p_arg)
- {
- INT8U err;
- /*
- U32 i;
- U32 x,y;
- */
- U8 h=0,m=0,s=0;
- FontStyle_Typedef StatusBarFontStyle;
- StatusBarFontStyle.FontBackgroundColor=BACKGROUND_COLOR;
- (void) p_arg;
- OSStatInit();
- while(1)
- {
- s++;
- if(s==60)
- {
- s=0;
- m++;
- if(m==60)
- {
- m=0;
- h++;
- if(h==24)
- h=0;
- }
- }
- StatusBarFontStyle.FontColor=FONT_CYAN;
- StatusBarFontStyle.pFontSize=&Font6x8;
- //TFT操作,等待其他任务释放资源
- ///*
- OSSemPend(UseTFTSem,0,&err);
- //-------------------------------------------------------------//
- TFT_PutString(&StatusBarFontStyle, 0, 0, " : : ");
- TFT_PutChar(&StatusBarFontStyle, 0,0,0x30+h/10);
- TFT_PutChar(&StatusBarFontStyle, 6,0,0x30+h%10);
- TFT_PutChar(&StatusBarFontStyle, 18,0,0x30+m/10);
- TFT_PutChar(&StatusBarFontStyle, 24,0,0x30+m%10);
- TFT_PutChar(&StatusBarFontStyle, 36,0,0x30+s/10);
- TFT_PutChar(&StatusBarFontStyle, 42,0,0x30+s%10);
- TFT_PutString(&StatusBarFontStyle, 0, 8, "2014-05-08");
-
- StatusBarFontStyle.pFontSize=&Font8x16;
- TFT_PutString(&StatusBarFontStyle, 180, 0, "CPU: %");
- TFT_PutChar(&StatusBarFontStyle, 212,0,0x30+OSCPUUsage/10);
- TFT_PutChar(&StatusBarFontStyle, 220,0,0x30+OSCPUUsage%10);
- StatusBarFontStyle.FontColor=FONT_BLUE;
- TFT_PutString(&StatusBarFontStyle, 68, 0, "MusicSpectrum");
- TFT_DrawHLine(0, 16, 239, TFT_YELLOW);
- //------------------------------------------------------------//
-
- //TFT操作结束,释放TFT资源的占用
- OSSemPost(UseTFTSem);
- //*/
- OSTimeDlyHMSM(0, 0, 1, 0);
- }
- }
- void Task_Init(void)
- {
- UseTFTSem=OSSemCreate(1);
- OSTaskCreate(Task_MusicSpectrum,(void *)0,(OS_STK *)&Task_MusicSpectrumStk[TASK_MUSIC_SPECTRUM_STK_SIZE - 1],TASK_MUSIC_SPECTRUM_PRIO);
- OSTaskCreate(Task_StatusBar,(void *)0,(OS_STK *)&Task_StatusBarStk[TASK_STATUS_BAR_STK_SIZE - 1],TASK_STATUS_BAR_PRIO);
- }
复制代码
main.c
- /**********************************************************
- * | | / / |----| /--------------------
- * | /| / / |---| / @沐雨迎风工作室
- * | / | | | | / Data:2014-5-5
- * | | | | | / Project:STM32
- * ----------------------/ File:main.c
- **********************************************************
- Modification History:
- versions Date Name Comment
- --- -------- -------- -----------
- 1 2014/5/5 Zhan Lei New
- */
- /*------------Include file---------------*/
- #include "Global.h"
- #include
- /*------------Typedef---------------*/
- /*------------Macro definition---------------*/
- #define PI2 6.28318530717959
- /*------------Define variables---------------*/
- U16 dutyRatio=300;
- /*------------Define functions---------------*/
- void TIM3_Configuration(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- //TIM_OCInitTypeDef TIM_OCInitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- TIM_DeInit(TIM3);
- TIM_TimeBaseStructure.TIM_Prescaler=0;
- TIM_TimeBaseStructure.TIM_Period=600;
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
- TFT_SetBackLED(dutyRatio);
- TIM_Cmd(TIM3,ENABLE);
- }
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
-
- /* Configure one bit for preemption priority */
- #if defined (VECT_TAB_RAM)
- /* Set the Vector Table base location at 0x20000000 */
- NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
- #elif defined(VECT_TAB_FLASH_IAP)
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x2000);
- #else /* VECT_TAB_FLASH */
- /* Set the Vector Table base location at 0x08000000 */
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
- #endif
- /* Configure the NVIC Preemption Priority Bits */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
- NVIC_Init(&NVIC_InitStructure);
-
- }
- int main(void)
- {
- CPU_IntDis();
- NVIC_Configuration();
- TIM3_Configuration();
- ADC_UserInit();
- Font_Init();
- TFT_Init();
- RTC_Init();
- OSInit();
- Task_Init();
- OSTimeSet(0);
- OSStart();
- while(1);
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d
- ", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
复制代码
`
11
评分
-
参与人数 1 | +5 |
积分 +10 |
收起
理由
|
vvg
| + 5 |
+ 10 |
您的帖子很精彩,期待您分享的下一个帖子!. |
查看全部评分
|
|
|
|