- #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 */
- }
- }
復制代碼
|