- #include "stm32f10x.h"
- #define SET_CS GPIOD->BSRR=0x00001000
- #define CLR_CS GPIOD->BRR=0x00001000
- #define SET_WR GPIOD->BSRR=0x00004000
- #define CLR_WR GPIOD->BRR=0x00004000
- #define SET_RD GPIOD->BSRR=0x00008000
- #define CLR_RD GPIOD->BRR=0x00008000
- #define SET_RS GPIOD->BSRR=0x00002000
- #define CLR_RS GPIOD->BRR=0x00002000
- GPIO_InitTypeDef GPIO_InitStructure;
- void mysysinit(void);//系统时钟初始
- void my_USART_init(void);//初始化
- void my_send_byte(unsigned char send_date); //发送一个字节
- void delay1us(uint32_t);//延时1微秒程序
- void init_STM32(void); //开机初始化
- uint16_t read_LCD_register(uint16_t reg);//读出指定的液晶寄存器,
- void wr_cmd_date(uint16_t reg,uint16_t date); //写入指定的寄存器值
- void LCD9328_init(void);//9328初始化
- void lcm_coordinate(uint16_t x1,uint16_t y1 );//扫描地
- int main(void)
- {
- uint32_t numb;
- uint16_t x,y,buff_sd_tft;
- init_STM32();///开机初始化时钟,初始化串口
- // delay1us(20000);
- LCD9328_init();
- // delay1us(10000);
- buff_sd_tft=0x0000;
- y=0;x=0;//lcm_coordinate(x,y);
- while(1)
- {
-
-
-
- //lcm_coordinate(x,y);
- wr_cmd_date(0x22,buff_sd_tft);
-
- //delay1us(200);
- numb++;
- if(numb==76800){numb=0;buff_sd_tft=(buff_sd_tft+215);}
-
-
- }
- /* d=read_LCD_register(0x20); //读液晶的00寄存器,判断是9525(9328)|9320
- e=d;
- a=e;
- c=(d>>8);
- my_send_byte(c);
- my_send_byte(a);
- while(1);*/
-
- }
- void lcm_coordinate(uint16_t x1,uint16_t y1 )//扫描地
- {
- wr_cmd_date(0x0050,x1);
- wr_cmd_date(0x0051,x1);
- wr_cmd_date(0x0052,y1);
- wr_cmd_date(0x0053,y1);
- wr_cmd_date(0x0020,x1);
- wr_cmd_date(0x0021,y1);
-
- }
- void LCD9328_init()//横屏初始
- {
-
- delay1us(150000);
- wr_cmd_date(0x0001,0x0100); //s1-s720
- wr_cmd_date(0x0002,0x0700);
- wr_cmd_date(0x0003,0x1030);
- wr_cmd_date(0x0004,0x0000);
- wr_cmd_date(0x0008,0x0207);
- wr_cmd_date(0x0009,0x0000);
- wr_cmd_date(0x000A,0x0000);
- wr_cmd_date(0x000C,0x0000);
- wr_cmd_date(0x000D,0x0000);
- wr_cmd_date(0x000F,0x0000);
- //power on sequence VGHVGL
- wr_cmd_date(0x0010,0x0000);
- wr_cmd_date(0x0011,0x0007);
- wr_cmd_date(0x0012,0x0000);
- wr_cmd_date(0x0013,0x0000);
- //vgh
- wr_cmd_date(0x0010,0x1290);
- wr_cmd_date(0x0011,0x0227);
- delay1us(800000);
- //vregiout
- wr_cmd_date(0x0012,0x001d); //0x001b
- delay1us(800000);
- //vom amplitude
- wr_cmd_date(0x0013,0x1500);
- delay1us(800000);
- //vom H
- wr_cmd_date(0x0029,0x0018);
- wr_cmd_date(0x002B,0x000D);
- //gamma
- wr_cmd_date(0x0030,0x0004);
- wr_cmd_date(0x0031,0x0307);
- wr_cmd_date(0x0032,0x0002);// 0006
- wr_cmd_date(0x0035,0x0206);
- wr_cmd_date(0x0036,0x0408);
- wr_cmd_date(0x0037,0x0507);
- wr_cmd_date(0x0038,0x0204);//0200
- wr_cmd_date(0x0039,0x0707);
- wr_cmd_date(0x003C,0x0405);// 0504
- wr_cmd_date(0x003D,0x0F02);
- //ram
- wr_cmd_date(0x0050,0x0000);
- wr_cmd_date(0x0051,0x00EF);
- wr_cmd_date(0x0052,0x0000);
- wr_cmd_date(0x0053,0x013F);
- wr_cmd_date(0x0060,0xA700);
- wr_cmd_date(0x0061,0x0001);
- wr_cmd_date(0x006A,0x0000);
- //
- wr_cmd_date(0x0080,0x0000);
- wr_cmd_date(0x0081,0x0000);
- wr_cmd_date(0x0082,0x0000);
- wr_cmd_date(0x0083,0x0000);
- wr_cmd_date(0x0084,0x0000);
- wr_cmd_date(0x0085,0x0000);
- //
- wr_cmd_date(0x0090,0x0010);
- wr_cmd_date(0x0093,0x0003);
- wr_cmd_date(0x0095,0x0110);
- wr_cmd_date(0x0097,0x0000);
- wr_cmd_date(0x0098,0x0000);
- wr_cmd_date(0x0007,0x0133);
- }
- void wr_cmd_date(uint16_t reg,uint16_t date)
- {
- GPIOE->CRL=0x33333333; //数据口转换成输出
- GPIOE->CRH=0x33333333; //数据口转换成输出
- CLR_CS;
- CLR_RS;
- SET_RD;
- CLR_WR;
- GPIOE->ODR=reg;//GPIO_Write(GPIOE, reg);// //写入命令
- SET_WR;
- SET_RS;
- //delay_1us(1);
- CLR_WR;
- GPIOE->ODR=date;//GPIO_Write(GPIOE, date); // //写入命令
- SET_WR;
- SET_CS;
- }
- uint16_t read_LCD_register(uint16_t reg)
- {
- uint16_t date;
- GPIOE->CRL=0x33333333; //数据口转换成输出
- GPIOE->CRH=0x33333333; //数据口转换成输出
- CLR_CS;
- //delay_1us(1);
- CLR_RS;
- //delay_1us(1);
- SET_RD;
- CLR_WR;
- GPIO_Write(GPIOE, reg); //写入命令
- SET_WR;
- //delay_1us(1);
- CLR_RD;
- GPIOE->CRL=0x88888888; //数据口转换成输入
- GPIOE->CRH=0x88888888; //数据口转换成输入
- // for(n=200;n;n--);
- date=(GPIOE->IDR);//读出数据
- SET_RD;
- SET_CS;
-
- return (date);
- }
- void init_STM32()
- {
- mysysinit();//RCC初始化,系统时钟设置72MHZ
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能APB2的GPIO_A时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);//使能APB2的GPIO_D时钟
- // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能APB2的GPIO_B时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);//使能APB2的GPIO_E时钟
- //RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//使能APB2的GPIO_E时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能APB1的USART2时钟
- /* 设置PD口用于控制LCD的为输出 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15| GPIO_Pin_8|
- GPIO_Pin_9 | GPIO_Pin_10| GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- /* 设置PE口用于控制LCD的为输出 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_2| GPIO_Pin_3| GPIO_Pin_4|
- GPIO_Pin_5 | GPIO_Pin_6| 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_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- my_USART_init();
- my_send_byte(0);
- my_send_byte(1);
- my_send_byte(2);
- my_send_byte(3);
- }
- void delay1us(uint32_t time)
- { uint32_t b,c;
- for(c=time;c;c--) //定时=time*3*333.6=time*1000.8ns
- {
- for(b=8;b;b--); //8*13.9*3= 333.6
- }
-
- }
- /***********************************
- 发送一个字节函数通过串口
- ************************************/
- void my_send_byte(unsigned char send_date )
- {
- while( (USART1->SR&0x00000080)!=0x80);//发送寄存器为空
- USART1->DR=send_date;
- }
- /**********************************
- 初始化串口
- **********************************/
- void my_USART_init()
- {
- /*USART2的优先级设为5*/
- NVIC->IP[37]=5;
- /*开启38号中断即USART2,关闭其他所有外部的中断*/
- NVIC->ISER[1]=0x00000020;
- /*设置复用模式下的引脚模式为全双工:TX输出推挽复用,RX为输入上拉模式,速度50MHZ*/
- GPIOA->CRH=0x000008b0;
- /* 1.开启USART,
- *
- */
- USART1->CR1=0x2000;
- /* 1.关闭局域网模式
- * 2.1个停止位
- * 3.CK引脚禁能
- */
- USART1->CR2=0;
- /* 1.关闭调制解调模式
- * 2.关闭DMA模式
- * 3.关闭智能卡、红外模式
- * 4.关闭错误中断
- */
- USART1->CR3=0;
- /* 波特率设置
- 2011年8月11日
- 王均伟
- 天津第四项目部宿舍
- BRR中的第四位(DIV_Fraction)作为小数,高12位(DIV_MANtissa)作为整数部分,
-
- 1,根据公式:波特率=fck/16*usardiv,其中usardivBRR寄存器的值,所以变形得:USARDIV=fck/16*波特率
- 2.算出来BRR寄存器的值后就要把这个值变成16进制数据写入BRR寄存器中,
- 遵循以下规则:
- 小数部分*16=DIV_Fraction或者取近似的值
- 整数部分直接=DIV_MANtissa
- 3.把这个16进制值写入BRR寄存器
- 例如我要算波特率设成9600bps的BRR寄存器值,
- 1.先求USARDIV=36000000/16*9600=234.375
- 2.换成十六进制:DIV_Fraction=16*0.375=0x6
- DIV_MANtissa=234=0xea
- 3.组合并写入寄存器
- USART2->BRR=0x0ea6;值得注意的是这里是16位半字操作,所以不要以为是32位。
- */
- USART1->BRR=0x0ea6;
- /* 1.开启USART
- * 2.开启接收完毕中断
- * 3.开启发送功能
- * 4.开启接收功能
- */
- USART1->CR1=0x202c;
-
- }
- void mysysinit()//系统初始化程序
- {
- ErrorStatus HSEStartUpStatus;//说明标志位
- RCC_DeInit();//所有外设全部缺省设置
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- /* Wait till HSE is ready and if Time out is reached exit */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)//启动成功
- {
- /*这两条FLASH指令必须加上,不知为啥?不加上就运行几秒后出错,参照系统初始化*/
- /* Enable The Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//FLASH缓存开启
- /* Configure the Latency cycle: Set 2 Latency cycles */
- FLASH_SetLatency(FLASH_Latency_2); //设置FLASH这些位表示SYSCLK(系统时钟)周期与闪存访问时间的比例,为010:两个等待状态,当 48MHz < SYSCLK ≤ 72MHz
- /* Set PLL clock output to 72MHz using HSE (8MHz) as entry clock */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);//外部时钟为8M,PLL的输入时钟=8MHZ,倍频系数9,
- /* Configure HCLK such as HCLK = SYSCLK */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置了啦AHB分频器的分频系数=1,即HCLK=SYSCLK=72MHZ
- /* Configure PCLK1 such as PCLK1 = HCLK/2 */
- RCC_PCLK1Config(RCC_HCLK_Div2);//设置了APB1外设的时钟频率最大是36M这里是APB1的分频器设为2,PCLK1=HCLK/2=72/2=36MHZ正好是最大值
- /* Configure PCLK2 such as PCLK2 = HCLK */
- RCC_PCLK2Config(RCC_HCLK_Div1);//设置PLCK2=HCLK=72MHZ,的APB2分频器=1
- /* Select the PLL as system clock source */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//设置了SYSCLK的提供者为PLL,频率由上面算出=72MHZ
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_PLLRDY, DISABLE);//PLL中断关闭
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_HSERDY,DISABLE);//HSE中断关闭
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_HSIRDY, DISABLE); //HSI中断关闭
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_LSERDY, DISABLE); //LSE中断关闭
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_LSIRDY, DISABLE); //LSI中断关闭
- /* PLL clock divided by 1.5 used as USB clock source */
- RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);//设置USB的时钟为=72、1.5=48mhz
- /* Configure ADCCLK such as ADCCLK = PCLK2/2 */
- RCC_ADCCLKConfig(RCC_PCLK2_Div2);//设置ADC时钟=PCLK2/2= 36MHZ
- /* disable the LSE */
- RCC_LSEConfig(RCC_LSE_OFF);//外部低速晶振关闭
- /*DISable the RTC clock */
- RCC_RTCCLKCmd(DISABLE);
- /* DISable the Clock Security System */
- RCC_ClockSecuritySystemCmd(DISABLE);
- /* Enable the PLL */
- RCC_PLLCmd(ENABLE);//使能PLL
-
-
-
- /* PLL ans system clock config */
- }
- else
- {
- /* Add here some code to deal with this error */
- }
- }
复制代码
|