程序里面的void Transmit_Display( u8 x, u8 y, float tiggtal, int N)函數只需把參數傳進去,x,y是控制顯示坐標,最后一個是控制保留小數點后面幾位小數,
自帶數組顯示函數和字符串顯示函數
單片機源程序如下:
- #include "lcd12864.h"
- #include "delay.h"
- #include "GPIO_Int.h"
- #include "math.h"
- #include "usart.h"
- /*********************************************
- 數據讀寫端口
- *********************************************/
- void Lcd_Send_Date(u8 date)
- {
- u8 i;
- LCD_RS=1;
- for(i=0;i<8;i++)
- {
- if((date<<i)&0x80)
- {
- LCD_RW=1;
- }
- else
- {
- LCD_RW=0;
- }
- LCD_EN=0;
- LCD_EN=1;
- }
- }
- /***********************************************
- 寫入命令
- ***********************************************/
- void Write_Cmd(u8 com)
- {
- Lcd_Send_Date(0xf8);
- Lcd_Send_Date(0xf0&com);
- Lcd_Send_Date(com<<4);
- delay_ms(1);
- }
- /***********************************************
- 寫入數據
- ***********************************************/
- void Write_Data(u8 dat)
- {
- Lcd_Send_Date(0xfa);
- Lcd_Send_Date(dat&0xf0);
- Lcd_Send_Date(dat<<4);
- delay_ms(1);
- }
- /***********************************************
- 液晶屏初始化
- ***********************************************/
- void Init_ST7920()
- {
- delay_ms(40); //大于40MS的延時程序
-
- Write_Cmd(0x30); //選擇基本指令集
- Write_Cmd(0x40); //對CGRAM第一個自定義字符操作,
- delay_us(100); //延時大于100us
-
- Write_Cmd(0x30); //選擇8bit數據流
-
- delay_us(40); //延時大于37us
-
- Write_Cmd(0x0c); //開顯示(無游標、不反白)
-
- delay_us(100); //延時大于100us
-
- Write_Cmd(0x01); //清除顯示,并且設定地址指針為00H
-
- delay_ms(15); //延時大于10ms
-
- Write_Cmd(0x06); //指定在資料的讀取及寫入時,設定游標的移動方向及指定顯示的移位,光標從右向左加1位移動
-
- delay_us(100); //延時大于100us
-
- }
-
- void Display_Count(u8 x,u8 y,u8 data)
- {
- switch(data)
- {
- case 0:LCD_DisplayInit(x,y,"零");
- break;
- case 1:LCD_DisplayInit(x,y,"一");
- break;
- case 2:LCD_DisplayInit(x,y,"二");
- break;
- case 3:LCD_DisplayInit(x,y,"三");
- break;
- case 4:LCD_DisplayInit(x,y,"四");
- break;
- default: break;
-
- }
- }
- /***********************************************
- 顯示字符串
- x:橫坐標值,范圍0~8
- y:縱坐標值,范圍1~4
- ***********************************************/
- void LCD_Struct_Display( LCD_DisplayTypedef *Display_Structure )
- {
- switch(Display_Structure->ordinate)
- {
- case 1: Write_Cmd(0x80+( (Display_Structure->abscissa) -1) );break;
- case 2: Write_Cmd(0x90+( (Display_Structure->abscissa) -1) );break;
- case 3: Write_Cmd(0x88+( (Display_Structure->abscissa) -1) );break;
- case 4: Write_Cmd(0x98+( (Display_Structure->abscissa) -1) );break;
- default: break;
- }
- while(*( Display_Structure->character_string ) != '\0')
- {
- if((*( Display_Structure->character_string )) == 'n')
- {
- USART_RX_STA = 0;
- break;
- }
- Write_Data( (*( Display_Structure->character_string )) );
- ( Display_Structure->character_string )++;
- delay_us(50);
- }
- }
- /********************************************************
- 普通在LCD顯示字符
- *******************************************************/
- void LCD_DisplayInit(u8 x, u8 y,u8 *spr)
- {
- LCD_DisplayTypedef LCD_DisplayStructure;
-
- LCD_DisplayStructure.abscissa = x;
- LCD_DisplayStructure.ordinate = y;
- LCD_DisplayStructure.character_string = spr;
-
- LCD_Struct_Display(&LCD_DisplayStructure);
- }
- /***********************************************
- 顯示串口接收的字符串
- 向串口發送x,y,"字符串"
- ***********************************************/
- void USART1_ReceiveInit(u8 *USART1_Buffer)
- {
-
- LCD_DisplayTypedef LCD_DisplayStructure;
-
- LCD_DisplayStructure.abscissa = (*USART1_Buffer - 48);
- LCD_DisplayStructure.ordinate = (*(USART1_Buffer+2) - 48);
- LCD_DisplayStructure.character_string = USART1_Buffer+4;
-
- LCD_Struct_Display(&LCD_DisplayStructure);
-
- }
- /****************************************************************
-
- ********進行數字轉換輸出到LCD顯*********
- ******************************************************************/
- /***************************************************************
- 計算并返回小數點的位置
- ***************************************************************/
- int Radix_point(float value)
- {
- int medium_int = 0, count_RP = 0;
- medium_int = (int)value;
-
- if(value == 0)
- {
- return count_RP;
- }
-
- if(value < 0)
- {
- value = (-value);
- }
-
- if(medium_int > 0)
- {
- while(medium_int)
- {
- count_RP++;
- medium_int /= 10;
- }
- return count_RP;
- }
- else
- {
- while(!medium_int)
- {
- value *= 10;
- medium_int = (int)(value);
- count_RP++;
- }
- return count_RP;
- }
- }
- /*********************************************************
- 把FLOAT型書轉換成INT型返回出去
- ***********************************************************/
- int Transform_val(float val, int N)
- {
- int value = 0;
-
- if(val < 0)
- {
- val = (-val);
- }
-
- if(val >= 1 )
- {
- value = (int)(val)*pow(10,N) + (int)((val - (int)(val))*pow(10,N));
- }
- else
- if(1>val > 0)
- {
- value = (int)((val)*pow(10,N));
- }
- return value;
- }
- /******************************************************************
- 數字顯示函數
- *******************************************************************/
- void Display_number(int channel_first, float channel_scond)
- {
- int current=1, val, count = 1, i = 0, j = 0;
- float medium_float;
- j = (Radix_point(channel_scond)) - 1;
-
- val = channel_first;
- if(channel_scond < 0)
- {
- medium_float = (-channel_scond);
- }
- else
- {
- medium_float = channel_scond;
- }
-
- while( (val>=10) )
- {
- val /= 10;
- count *= 10;
- }
- if(channel_scond < 0)
- {
- Write_Data('-');
- }
- if(medium_float >= 1)
- {
- while(count)
- {
- if(Radix_point(medium_float) == i)
- {
- Write_Data('.');
- current = 0;
- i = 0;
- }
- else
- {
- Write_Data('0' + (channel_first/count%10));
- count /=10;
- if(current)
- i++;
- }
- }
- }
- else
- if(medium_float > 0)
- {
- Write_Data('0');
- Write_Data('.');
- while(j > 0)
- {
- Write_Data('0');
- j--;
- }
- while(count)
- {
- Write_Data('0' + (channel_first/count%10));
- count /=10;
- }
- }
- else
- if(medium_float == 0)
- {
- Write_Data('0');
- }
- }
- /***************************************************************
- 數字傳送顯示
- **************************************************************/
- void Transmit_Display( u8 x, u8 y, float tiggtal, int N)
- {
- switch(y)
- {
- case 1: Write_Cmd(0x80+ (x -1)) ;break;
- case 2: Write_Cmd(0x90+ (x -1)) ;break;
- case 3: Write_Cmd(0x88+ (x -1)) ;break;
- case 4: Write_Cmd(0x98+ (x -1)) ;break;
- default: break;
- }
- Display_number(Transform_val(tiggtal,N),tiggtal);
-
- }
復制代碼
LCD12864.h
- #ifndef _LCD12864_H_
- #define _LCD12864_H_
- #include "sys.h"
- /******定義一個結構體******/
- typedef struct
- {
- u8 USART1_DiaplayEnable;
- u8 abscissa; //x
- u8 ordinate; //y
- u8 *character_string;
- }LCD_DisplayTypedef;
- /**************************/
- void Write_Cmd(u8 com);
- void Write_Data(u8 dat);
- void Init_ST7920(void);
- void Display_Count(u8 x,u8 y,u8 data);
- /* 結構體顯示 */
- void LCD_DisplayInit(u8 x, u8 y,u8 *spr);
- void LCD_Struct_Display( LCD_DisplayTypedef *Display_Structure );
- /* 串口接收顯示 */
- void USART1_ReceiveInit(u8 *USART1_Buffer);
- /* 數字傳送顯示 */
-
- void Transmit_Display( u8 x, u8 y, float tiggtal, int N);
-
- #endif
復制代碼
|