液晶显示波形

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/*******************************************************************************
* File Name : main.c
* Author : Wuhan R&D Center, Embest
* Date First Issued : 08/08/2008
* Description : Main program body
********************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
#include"stdio.h"
//#include "monitor.h"

/************ 用于定义ITM Viewer相关的ITM激励寄存器端口 ************************/
/*#define ITM_Port8(n) (*((volatile unsigned char *)(0xE0000000+4*n)))
#define ITM_Port16(n) (*((volatile unsigned short*)(0xE0000000+4*n)))
#define ITM_Port32(n) (*((volatile unsigned long *)(0xE0000000+4*n)))

#define DEMCR (*((volatile unsigned long *)(0xE000EDFC)))
#define TRCENA 0x01000000*/

/*用于定义是否使用ITM Viewer*/
//#define DBG_ITM

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_Address ((u32)0x4001244C)
int AD_value;
//static unsigned long ticks;
//unsigned char Clock1s;

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
vu16 ADC_ConvertedValue;
ErrorStatus HSEStartUpStatus;

/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void SysTick_Configuration(void);
void USART_Configuration1(void);
void SetupClock (void);
int fputc(int ch, FILE *f);
/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name : main
* Description : Main program
* Input : None
* Output : None
* Return : None
*******************************************************************************/
#define LED1ON GPIO_SetBits(GPIOF,GPIO_Pin_0)
#define LED1OFF GPIO_ResetBits(GPIOF,GPIO_Pin_0) //片选cs

#define LED2ON GPIO_SetBits(GPIOF,GPIO_Pin_1)
#define LED2OFF GPIO_ResetBits(GPIOF,GPIO_Pin_1)//复位rest
#define LED3ON GPIO_SetBits(GPIOF,GPIO_Pin_2)
#define LED3OFF GPIO_ResetBits(GPIOF,GPIO_Pin_2)//数据sdata
#define LED4ON GPIO_SetBits(GPIOF,GPIO_Pin_3)
#define LED4OFF GPIO_ResetBits(GPIOF,GPIO_Pin_3)//时钟sclk


unsigned char Ver='A'; //驱动版本,默认为A版本,一共4个版本
void delayms(unsigned int ii)//1ms延时函数
{

unsigned int i,x;
for (x=0;x{
for (i=0;i<100;i++);
}
}

void send(unsigned char value, unsigned char cd) //写8位数据
{
unsigned char i,dt;
LED

1OFF;
LED4OFF;
if(cd == 0)
LED3OFF;
else
LED3ON;
LED4ON;

dt=value;
for(i=0;i<8;i++)
{
LED4OFF;
if(dt&0x80)
LED3ON;
else
LED3OFF;
dt=dt<<1;
LED4ON;
}
LED1ON;//cs=1;
}
void readdata() //读数据
{
unsigned char i,j;
unsigned char ch[6];
send(0x09, 0); // 读数据
LED1OFF;//cs=0;
LED4OFF;//sclk=0;
for (i=0;i<6;i++)
{
ch[i]=0;
for (j=0;j<8;j++)
{

LED4ON;//sclk=1;
if (!(GPIO_ReadOutputDataBit(GPIOF,GPIO_Pin_2)))//sdata==0)
{
ch[i]=ch[i]|(1<<(7-j));
}
LED4OFF;//sclk=0;
}
}

switch(ch[0])
{
case 255:
Ver='A';
break;
case 127:
Ver='B';
break;
case 0:
if(ch[1]+ch[2]+ch[3]==0)
{
Ver='D';
}else
{
Ver='C';
}
break;
}
LED1ON;//cs=1;
}

void LCD_Initialize() //LCD初始化
{
unsigned char i;
LED2ON;//rest=1;
LED1ON;//cs=1;
LED3OFF;//sdata=0;
LED4ON;//sclk=1;
LED2OFF;//rest=0;
delayms(50);
LED2ON;//rest=1;
delayms(50);

send(0x00, 0); // NOP
delayms(5);

send(0x01, 0); // LCD Software Reset
delayms(5);

send(0xC6, 0); // Initial Escape

send(0xB9, 0); // Refresh set
send(0x00, 1);

readdata(); //读出数据

send(0xB5, 0); // Gamma Curve Set
send(0x01, 1);

send(0xbd, 0); //common driver output select//很重要的
if(Ver=='D')
{
send(0x04, 1);
}else
{
send(0x00, 1);
}

send(0xBE, 0); // Power Control
send(0x03, 1);


send(0x11, 0); // Sleep out

send(0xBA, 0); // Power Control
send(0x7F, 1);
send(0x03, 1);

send(0x36, 0); // 扫描方式
if (Ver=='A')
{
send(0x00|0x08,1); //RGB
}
else
{
send(0x00,1); //RGB
}

send(0xB7, 0); // Temperature gradient set
for(i=0; i<14; i++)
{
send(0, 1);
}


send(0x29, 0); //display ON

send(0x03, 0); // Booster Voltage ON

delayms(20); // Booster Voltage stabilisieren lassen

send(0x20, 0); //display inversion OFF

send(0x25, 0); // Write contrast
switch(Ver) //对比度设置
{
case 'A':
send(70, 1); //对比度设置
break;
case 'B':
send(67, 1); //对比度设置
break;
case 'C':
//send(74, 1);
send(66, 1);
// send(64, 1); //对比度设置
break;
case 'D': //对比度设置
send(39, 1);
break;

}

}

//************************
//颜色模式设置:color=1为4096色模式
// :color=0为256色模式
//************************
void LCD_ColorSet(unsigned char Color)
{

if (Color==1) {
send(0x3a, 0); //interface pixel format
send(0x03, 1); //0x03 为4096色,0x02为256色

}
else


{
send(0x3a, 0); //interface pixel format
send(0x02, 1); //0x03 为4096色,0x02为256色



send(0x2d, 0); //调色板设置
if (Ver=='B'||Ver=='C')
{
//red
send(~0x00, 1);
send(~0x02, 1);
send(~0x03, 1);
send(~0x04, 1);
send(~0x05, 1);
send(~0x06, 1);
send(~0x08, 1);
send(~0x0f, 1);

//green
send(~0x00, 1);
send(~0x02, 1);
send(~0x03, 1);
send(~0x04, 1);
send(~0x05, 1);
send(~0x06, 1);
send(~0x08, 1);
send(~0x0f, 1);
//blue
send(~0x00, 1);
send(~0x03, 1);
send(~0x06, 1);
send(~0x0f, 1);
}else
{
//red
send(0x00, 1);
send(0x02, 1);
send(0x03, 1);
send(0x04, 1);
send(0x05, 1);
send(0x06, 1);
send(0x08, 1);
send(0x0f, 1);

//green
send(0x00, 1);
send(0x02, 1);
send(0x03, 1);
send(0x04, 1);
send(0x05, 1);
send(0x06, 1);
send(0x08, 1);
send(0x0f, 1);
//blue
send(0x00, 1);
send(0x03, 1);
send(0x06, 1);
send(0x0f, 1);
}


}
}
void addset(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2) //坐标设置 4个参数依次为开始X,开始Y,结束x,结束Y
{
send(0x2a,0 );//column address set
send(x1,1 );
send(x2,1 );
send(0x2B,0 );//page address set
send(y1,1 );
send(y2,1 );
send(0x2C,0 );//memory write
}
//************************
//清屏:color=1为4096色模式清屏
// :color=0为256色模式清屏
//************************
void LCD_Clear(unsigned int value,unsigned char Color)
{
unsigned char x, y;
addset(0,0,97,66);
if (Color==1)
{
for(y = 0; y < 67; y ++)
{
for(x = 0; x < 49;x ++)
{ send(value>>4,1);
send(((value&0x0f)<<4)|(value>>8),1);
send(value,1);
}
}
}
else
{
for(y = 0; y < 67; y ++)
{
for(x = 0; x < 98; x ++)
{
send(value,1);
}
}
}
}
//在指定位置显示一个字符(8*12大小)
//dcolor为内容颜色,gbcolor为背静颜色
void showdian(unsigned char x,unsigned char y,unsigned char dcolor,unsigned char bgcolor)
{


addset(x,y,x+1,y+1); //设置区域





send(dcolor,1);


send(bgcolor,1);


}
int main(void)
{int i,j;
#ifdef DEBUG
debug();
#endif

/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
SysTick_Configuration();
/* NVIC configuration ------------------------------------------------------*/
NVIC_Configuration();

/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();

/* Configure the USART1 */
USART_Configuration1();

//printf("\r\n USART1 print AD_value -------------------------- \r\n");

/* DMA channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseA

ddr = (u32)&ADC_ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);

/* Enable DMA channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);

/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);

/* ADC1 regular channel13 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 1, ADC_SampleTime_55Cycles5);

/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);

/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);

/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));

/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));

/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);


LCD_Initialize() ;
LCD_ColorSet(0); //0为256色模式,1为4096色模式
LCD_Clear(255,0); //清屏
addset(0,0,103,81); //设置起止坐标,4个参数依次为开始X,开始Y,结束x,结束Y
while(1)
{
for(i=0;i<100;i++)
{for(j=0;j<10;j++)
{AD_value=ADC_GetConversionValue(ADC1);
/* Printf message with AD value to serial port every 1 second */
showdian(i,65-AD_value/63 ,0x03,255);


}

delayms(100);
}
LCD_Clear(255,0); //清屏
addset(0,0,103,81);
}
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)

{ /* RCC system reset(for debug purpose) */
RCC_DeInit();

/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);

/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();

if(HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK */
RCC_HCL

KConfig(RCC_SYSCLK_Div1);

/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);

/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);

/* ADCCLK = PCLK2/4 */
RCC_ADCCLKConfig(RCC_PCLK2_Div4);

/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

/* PLLCLK = 8MHz * 9 = 56 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

/* Enable PLL */
RCC_PLLCmd(ENABLE);

/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}

/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}

/* Enable peripheral clocks --------------------------------------------------*/
/* Enable DMA clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

/* Enable ADC1 and GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);

/* Enable USART1 and GPIOA clock */
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOA| RCC_APB2Periph_GPIOC|
RCC_APB2Periph_AFIO, ENABLE);


}
void SysTick_Configuration(void)
{
/* Select AHB clock(HCLK) as SysTick clock source */
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);

/* Set SysTick Priority to 3 */
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);

/* SysTick interrupt each 1ms with HCLK equal to 72MHz */
SysTick_SetReload(72000);

/* Enable the SysTick Interrupt */
SysTick_ITConfig(ENABLE);
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

/* Configure PC.03 (ADC Channel13) as analog input -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure PF. as Output push-pull */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
}

/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}

#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert error has occurred.
* Input : - file: pointer to the source file name
* - line: assert error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 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\r\n", file, line) */

/* Infinite loop */
while (1)
{
}
}
#endif
void USART_Configuration1(void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
/* USART1 configuration ------------------------------------------------------*/
/* USART1 configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
- USART Clock disabled
- USART CPOL: Clock is active low
- USART CPHA: Data is captured on the middle
- USART LastBit: The clock pulse of the last data bit is not output to
the SCLK pin
*/
USART_ART_Clock = USART_Clock_Disable;
USART_ART_CPOL = USART_CPOL_Low;
USART_ART_CPHA = USART_CPHA_2Edge;
USART_ART_LastBit = USART_LastBit_Disable;
/* Configure the USART1 synchronous paramters */
USART_ClockInit(USART1, &USART_ClockInitStructure);

USART_ART_BaudRate = 115200;
USART_ART_WordLength = USART_WordLength_8b;
USART_ART_StopBits = USART_StopBits_1;
USART_ART_Parity = USART_Parity_No ;
USART_ART_HardwareFlowCo

ntrol = USART_HardwareFlowControl_None;

USART_ART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* Configure USART1 basic and asynchronous paramters */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 */
USART_Cmd(USART1, ENABLE);
}

/*******************************************************************************
* Function Name : fputc
* Description : Retargets the C library printf function to the USART or ITM Viewer.
* Input : None
* Output : None
* Return : None
*******************************************************************************/

int fputc(int ch, FILE *f)
{
#ifdef DBG_ITM
/* 将Printf内容发往ITM激励寄存器端口 */
if (DEMCR & TRCENA) {
while (ITM_Port32(0) == 0);
ITM_Port8(0) = ch;
}
#else
/* 将Printf内容发往串口 */
USART_SendData(USART1, (unsigned char) ch);
while (!(USART1->SR & USART_FLAG_TXE));
#endif
return (ch);
}


/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/

相关文档
最新文档