STM32采用串口驅動lcd12864顯示屏的程序,請大家鑒賞下,后續會更新程序!!
單片機源程序如下:
- /*****************************************************************************************/
- /* */
- /* LCD串行驅動程序 2008年12月20日晚 by cihu */
- /* */
- /* 實測寫8條液晶要接近40ms,故不能頻繁寫液晶,寫時在操作系統里最好要大于200ms刷新頻率 */
- /* */
- /*****************************************************************************************/
- #include "stm32f10x.h"
- #include "12864.h"
-
- #define DLY 25
- #define COMM 0
- #define CDAT 1
- u8 lcd_temp;
- u8 h_x[4]={0x80,0x90,0x88,0x98}; //行地址
- u8 flag_LCD; //為0時關閉液晶顯示,1時打開
- RCC_ClocksTypeDef RCC_ClockFreq;
- void RCC_Configuration(void)
- {
- SystemInit();//源自system_stm32f10x.c文件,只需要調用此函數,則可完成RCC的配置.具體請看2_RCC
- /**************************************************
- 獲取RCC的信息,調試用
- 請參考RCC_ClocksTypeDef結構體的內容,當時鐘配置完成后,
- 里面變量的值就直接反映了器件各個部分的運行頻率
- ***************************************************/
- RCC_GetClocksFreq(&RCC_ClockFreq);
-
- /* 這個配置可使外部晶振停振的時候,產生一個NMI中斷,不需要用的可屏蔽掉*/
- //RCC_ClockSecuritySystemCmd(ENABLE);
- }
- /*******************************************************************************
- * Function Name : GPIO_Configuration
- * 設置PB5,7,9;
- *******************************************************************************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /*允許總線CLOCK,在使用GPIO之前必須允許相應端的時鐘.
- 從STM32的設計角度上說,沒被允許的端將不接入時鐘,也就不會耗能,
- 這是STM32節能的一種技巧,*/
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
- /* PB5,7,9輸出 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M時鐘速度
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- }
- /*----------------延時函數-------------------*/
- void ldelay (u32 us) //delay time
- {
- u8 i,temp;
- for(i=0;i<10;i++)
- for(temp=us;temp>0;temp--);
- }
- /*-----------write string------------*/
- /*------------------初始化-----------------*/
- void LCD_Init (void) //使用前須調用此函數初始化
- {
- flag_LCD=1;
- // IO2DIR=IO2DIR|CS_IO|STD_IO|SCLK_IO; //PINSEL2已經在startup.s中配置完,現只需配置方向
-
- wr_lcd (COMM,0x30); /*30---基本指令動作*/
- wr_lcd (COMM,0x01); /*清屏,地址指針指向00H*/
- ldelay (100);
- wr_lcd (COMM,0x06); /*光標的移動方向*/
- wr_lcd (COMM,0x0c); /*開顯示,關游標*/
- }
- /*******************************
- **函數名:ChipHalInit()
- **功能:片內硬件初始化
- *******************************/
- void ChipHalInit(void)
- {
- RCC_Configuration(); //初始化時鐘源
- GPIO_Configuration(); //初始化GPIO
- }
- /*--------------清DDRAM------------------*/
- void clrram (void)
- {
- wr_lcd (COMM,0x30);
- wr_lcd (COMM,0x01);
- ldelay (180);
- }
- /*---------------------------------------*/
- void wr_lcd(u8 dat_COMM,u8 content)
- {
- u8 a,i,j;
- ldelay (30);
- lcd_temp=(1<<7);
- a=content;
- CS_H;
- CLK_L;
- SID_H;
- for(i=0;i<5;i++)
- {
- CLK_H;
- ldelay(DLY);
- CLK_L;
- }
- SID_L;
- CLK_H;
- ldelay(DLY);
- CLK_L;
- if(dat_COMM)
- SID_H; //data
- else
- SID_L; //COMMand
-
- CLK_H;
- ldelay(DLY);
- CLK_L;
- SID_L;
- CLK_H;
- ldelay(DLY);
- CLK_L;
- for(j=0;j<2;j++)
- {
- for(i=0;i<4;i++)
- {
- if(lcd_temp&a) SID_H;
- else SID_L;
- lcd_temp=lcd_temp>>1;
- CLK_H;
- ldelay(DLY);
- CLK_L;
- }
- SID_L;
- for(i=0;i<4;i++)
- {
- CLK_H;
- ldelay(DLY);
- CLK_L;
- }
- }
- }
- void PutString(char *str,u8 line) //在第line行輸出一個字符串
- {
- u8 i=0;
-
- if(!flag_LCD)
- {
- return;
- }
- wr_lcd (COMM,0x30);
- wr_lcd (COMM,h_x[line-1]);
- while(str[i] != '\0' )
- {
- if(str[i]=='\n')
- {
- wr_lcd (COMM,h_x[line]);
- i++;
- continue;
- }
- if(str[i]=='\t')
- {
- wr_lcd (COMM,h_x[line-1]+8);
- i++;
- continue;
- }
- wr_lcd (CDAT,str[i++]);
- }
- }
- void PutChar(char *str) //在默認位置輸出一個字符串
- {
- u8 i=0;
- if(!flag_LCD)
- {
- return;
- }
- while(str[i] != '\0' )
- {
- wr_lcd (CDAT,str[i++]);
- }
- }
- /*-----------write number------------*/
- void PutIntNum(int value,u8 num,u8 choose)
- {
- char string[20],*str,i=0;
-
- if(!flag_LCD)
- {
- return;
- }
- str=string;
- if(value<0)
- {
- wr_lcd(CDAT,'-');
- value=0-value;
- }
- if (choose==10) //顯示10進制
- {
-
- while(str[i]!= '\0' )
- {
- wr_lcd (CDAT,str[i++]);
- }
- }
-
- else if (choose==16) //顯示16進制
- {
- while(str[i]!='\0' )
- {
- wr_lcd (CDAT,str[i++]);
- }
- }
- }
- void PutFloatNum(float value,u8 num1,u8 num2)
- {
- char string[20],*str,i=0;
- str=string;
-
- if(!flag_LCD)
- {
- return;
- }
- if(value<0)
- {
- wr_lcd(CDAT,'-');
- value=0-value;
- }
-
- while(str[i]!='\0' )
- {
- wr_lcd (CDAT,str[i++]);
- }
- }
復制代碼
代碼下載:
12864串行顯示程序.zip
(5.86 MB, 下載次數: 49)
2020-8-13 10:45 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|