|
- /* main.c */
- #include <REGX52.H>
- #include <stdio.h>
- #include "lcd1602.h"
- #define MAIN_FOSC_DELAY 12000000UL
- #define T1MS_0 (65536-MAIN_FOSC_DELAY/12/1000) //1ms timer calculation method in 12T mode
- #define T1MS_1 (65536-MAIN_FOSC_DELAY/12/1000) //1ms timer calculation method in 12T mode
- #define TIMER_MODE0 0x00
- #define TIMER_MODE1 0x01
- #define TIMER_MODE2 0x02
- #define TIMER_MODE3 0x03
- static bit _1_ms_flag = 0; //1ms標志位
- static bit _100ms_flag = 0; //100ms標志位
- static bit _500ms_flag = 0; //500ms標志位
- static void Timer0Init( void );
- //主函數
- int main( void )
- {
- idata float disTemp[4] = {0};
- idata unsigned char display_buf[16];//顯示數組
- idata unsigned int display_len;//顯示數據長度
- Timer0Init();
- MngLCD1602_Init();//LCD1602初始化
- EA = 1;
- while( 1 )
- {
- if(1 == _100ms_flag)
- {
- _100ms_flag = 0;
-
- disTemp[0] += 0.1f;
- disTemp[1] += 0.2f;
- disTemp[2] += 0.3f;
- disTemp[3] += 0.4f;
-
- display_len = sprintf( display_buf, "%.2f ", disTemp[0] );
- displayString( 0, 0, display_buf, display_len );
-
- display_len = sprintf( display_buf, "%.2f ", disTemp[1] );
- displayString( 10, 0, display_buf, display_len );
-
- display_len = sprintf( display_buf, "%.2f ", disTemp[2] );
- displayString( 0, 1, display_buf, display_len );
-
- display_len = sprintf( display_buf, "%.2f ", disTemp[3] );
- displayString( 10, 1, display_buf, display_len );
- }
- else
- {
- /* empty */
- }
-
- if(1 == _1_ms_flag)
- {
- _1_ms_flag = 0u;
- MngLCD1602_Handle();
- }
- else
- {
- /* empty */
- }
- }
- }
- //定時器0中斷
- static void Timer0Init( void ) //定時器初始化為1ms一次
- {
- TMOD |= 0X01; //選擇為定時器0模式,工作方式1,僅用TR0打開啟動。
- TL0 = T1MS_0;//給定時器賦初值,定時1ms ,計算方式(65536 -(12/12*1000))%256
- TH0 = T1MS_0 >> 8;//給定時器賦初值,定時1ms ,計算方式(65536 -(12/12*1000))/256
- ET0 = 1; //打開定時器0中斷允許
- TR0 = 1; //打開定時器
- }
- void time0_isr() interrupt 1 using 0
- {
- static unsigned int count = 0;
- static unsigned int count2 = 0;
- TL0 = T1MS_0;//手動重裝載
- TH0 = T1MS_0 >> 8;//手動重裝載
-
- _1_ms_flag = 1;//1ms標志位置1
- if( ++count >= 100 ) //0.1秒到了
- {
- count = 0;
- _100ms_flag = 1;//1秒標志位置1
- }
- if( ++count2 >= 500 )
- {
- count2 = 0;
- _500ms_flag = 1;
- }
- }
- /* LCD1602.h */
- #ifndef __LCD1602_H_
- #define __LCD1602_H_
- /**********************************
- 包含頭文件
- **********************************/
- #include <REGX52.H>
- typedef unsigned long uint32_t;
- typedef unsigned int uint16_t;
- typedef unsigned char uint8_t;
- /**********************************
- PIN口定義
- **********************************/
- #define LCD1602_DATAPINS P0
- sbit LCD1602_RS = P2 ^ 6;
- sbit LCD1602_RW = P2 ^ 5;
- sbit LCD1602_E = P2 ^ 7;
- /**********************************
- 函數聲明
- **********************************/
- extern void MngLCD1602_Init( void );
- extern void MngLCD1602_Handle( void );
- extern void displayString( uint8_t x, uint8_t y, uint8_t* dat, uint8_t len);
- #endif
- /* LCD1602.c */
- #include "lcd1602.h"
- #include <string.h>
- #define LCD1602_ROW_NUM 2u
- #define LCD1602_COLUMN_NUM 16u
- #define LCD1602_READ_DATA() (LCD1602_DATAPINS)
- #define LCD1602_WRITE_DATA(dat) (LCD1602_DATAPINS = (uint8_t)dat)
- #define LCD1602_SET_RS_H() (LCD1602_RS = 1)
- #define LCD1602_SET_RS_L() (LCD1602_RS = 0)
- #define LCD1602_SET_RW_H() (LCD1602_RW = 1)
- #define LCD1602_SET_RW_L() (LCD1602_RW = 0)
- #define LCD1602_SET_E_H() (LCD1602_E = 1)
- #define LCD1602_SET_E_L() (LCD1602_E = 0)
- typedef enum
- {
- false = 0u,
- true,
- }Bool;
- typedef enum
- {
- Ce_Sending = 0u,
- Ce_SendOK,
- }TeLCD1602_SendState;
- typedef enum
- {
- Ce_SendAddr_00 = 0u,
- Ce_SendData_00,
- Ce_SendAddr_10,
- Ce_SendData_10,
- }TeLCD1602_SendStep;
- typedef enum TeLCD1602_e_writeStateTypeTag
- {
- Ce_Stay_0 = 0u,
- Ce_Stay_1 = 1u,
- Ce_Stay_2 = 2u,
- } TeLCD1602_e_writeStateType;
- typedef struct TsLCD1602_h_RowParaTypeTag
- {
- const uint8_t* e_p_DisplayStartAddr;
- const uint8_t* e_p_DisplayEndAddr;
- } TsLCD1602_h_RowParaType;
- typedef struct TsLCD1602_h_displayParaTypeTag
- {
- TeLCD1602_SendStep e_e_SendStep;
- TeLCD1602_e_writeStateType e_e_WriteState;
- TsLCD1602_h_RowParaType e_h_RowPara[LCD1602_ROW_NUM];
- uint8_t* e_p_DisplayNextAddr;
- } TsLCD1602_h_displayParaType;
- static volatile uint8_t SeLCD1602_u_displayBuffer[LCD1602_ROW_NUM][LCD1602_COLUMN_NUM];
- static volatile TsLCD1602_h_displayParaType SsLCD1602_h_displayPara;
- static Bool MngLCD1602_IsBusy( void );
- static void MngLCD1602_Display();
- static TeLCD1602_SendState LcdWriteData( const uint8_t dat );
- static TeLCD1602_SendState LcdWriteCom( const uint8_t com ) ;
- static Bool MngLCD1602_IsBusy( void )
- {
- LCD1602_WRITE_DATA(0xff);
- LCD1602_SET_RS_L();
- LCD1602_SET_RW_H();
- if( 0 != (LCD1602_READ_DATA() & 0x80) )
- {
- LCD1602_SET_E_L();
- LCD1602_SET_E_H();
- return true;
- }
- else
- {
- LCD1602_SET_E_L();
- return false;
- }
- }
- static TeLCD1602_SendState LcdWriteCom( const uint8_t com )
- {
- switch( SsLCD1602_h_displayPara.e_e_WriteState )
- {
- case Ce_Stay_0:
- LCD1602_SET_RS_L();
- LCD1602_SET_RW_L();
- LCD1602_WRITE_DATA( com );
- SsLCD1602_h_displayPara.e_e_WriteState = Ce_Stay_1;
- return Ce_Sending;
- break;
- case Ce_Stay_1:
- LCD1602_SET_E_H();
- SsLCD1602_h_displayPara.e_e_WriteState = Ce_Stay_2;
- return Ce_Sending;
- break;
- case Ce_Stay_2:
- LCD1602_SET_E_L();
- SsLCD1602_h_displayPara.e_e_WriteState = Ce_Stay_0;
- return Ce_SendOK;
- break;
- default:
- SsLCD1602_h_displayPara.e_e_WriteState = Ce_Stay_0;
- return Ce_SendOK;
- break;
- }
- }
- static TeLCD1602_SendState LcdWriteData( const uint8_t dat )
- {
- switch( SsLCD1602_h_displayPara.e_e_WriteState )
- {
- case Ce_Stay_0:
- LCD1602_SET_RS_H();
- LCD1602_SET_RW_L();
- LCD1602_WRITE_DATA( dat );
- SsLCD1602_h_displayPara.e_e_WriteState = Ce_Stay_1;
- return Ce_Sending;
- break;
- case Ce_Stay_1:
- LCD1602_SET_E_H();
- SsLCD1602_h_displayPara.e_e_WriteState = Ce_Stay_2;
- return Ce_Sending;
- break;
- case Ce_Stay_2:
- LCD1602_SET_E_L();
- SsLCD1602_h_displayPara.e_e_WriteState = Ce_Stay_0;
- return Ce_SendOK;
- break;
- default:
- SsLCD1602_h_displayPara.e_e_WriteState = Ce_Stay_0;
- return Ce_SendOK;
- break;
- }
- }
- static void MngLCD1602_Display( void )
- {
- switch(SsLCD1602_h_displayPara.e_e_SendStep)
- {
- case Ce_SendAddr_00:
- if(Ce_Stay_0 == SsLCD1602_h_displayPara.e_e_WriteState)
- {
- if(false == MngLCD1602_IsBusy())
- {
- LcdWriteCom(0x80);
- }
- else
- {
- /* empty */
- }
- }
- else if(Ce_SendOK == LcdWriteCom(0x80)) /* 第0,0位置開始顯示 */
- {
- SsLCD1602_h_displayPara.e_e_SendStep = Ce_SendData_00;
- SsLCD1602_h_displayPara.e_e_WriteState = Ce_Stay_0;
- SsLCD1602_h_displayPara.e_p_DisplayNextAddr = SsLCD1602_h_displayPara.e_h_RowPara[0].e_p_DisplayStartAddr;
- }
- else
- {
- /* empty */
- }
- break;
-
- case Ce_SendData_00:
- if(Ce_Stay_0 == SsLCD1602_h_displayPara.e_e_WriteState)
- {
- if(false == MngLCD1602_IsBusy())
- {
- LcdWriteData(*SsLCD1602_h_displayPara.e_p_DisplayNextAddr);
- }
- else
- {
- /* empty */
- }
- }
- else if(Ce_SendOK == LcdWriteData(*SsLCD1602_h_displayPara.e_p_DisplayNextAddr))
- {
- SsLCD1602_h_displayPara.e_p_DisplayNextAddr++;
- if(SsLCD1602_h_displayPara.e_p_DisplayNextAddr > SsLCD1602_h_displayPara.e_h_RowPara[0].e_p_DisplayEndAddr)
- {
- SsLCD1602_h_displayPara.e_e_SendStep = Ce_SendAddr_10;
- SsLCD1602_h_displayPara.e_e_WriteState = Ce_Stay_0;
- SsLCD1602_h_displayPara.e_p_DisplayNextAddr = SsLCD1602_h_displayPara.e_h_RowPara[1].e_p_DisplayStartAddr;
- }
- else
- {
- /* empty */
- }
- }
- else
- {
- /* empty */
- }
- break;
-
- case Ce_SendAddr_10:
- if(Ce_Stay_0 == SsLCD1602_h_displayPara.e_e_WriteState)
- {
- if(false == MngLCD1602_IsBusy())
- {
- LcdWriteCom(0x80 + 0x40);
- }
- else
- {
- /* empty */
- }
- }
- else if(Ce_SendOK == LcdWriteCom(0x80 + 0x40)) /* 第0,1位置開始顯示 */
- {
- SsLCD1602_h_displayPara.e_e_SendStep = Ce_SendData_10;
- SsLCD1602_h_displayPara.e_e_WriteState = Ce_Stay_0;
- SsLCD1602_h_displayPara.e_p_DisplayNextAddr = SsLCD1602_h_displayPara.e_h_RowPara[1].e_p_DisplayStartAddr;
- }
- else
- {
- /* empty */
- }
- break;
-
- case Ce_SendData_10:
- if(Ce_Stay_0 == SsLCD1602_h_displayPara.e_e_WriteState)
- {
- if(false == MngLCD1602_IsBusy())
- {
- LcdWriteData(*SsLCD1602_h_displayPara.e_p_DisplayNextAddr);
- }
- else
- {
- /* empty */
- }
- }
- else if(Ce_SendOK == LcdWriteData(*SsLCD1602_h_displayPara.e_p_DisplayNextAddr))
- {
- SsLCD1602_h_displayPara.e_p_DisplayNextAddr++;
- if(SsLCD1602_h_displayPara.e_p_DisplayNextAddr > SsLCD1602_h_displayPara.e_h_RowPara[1].e_p_DisplayEndAddr)
- {
- SsLCD1602_h_displayPara.e_e_SendStep = Ce_SendAddr_00;
- SsLCD1602_h_displayPara.e_e_WriteState = Ce_Stay_0;
- SsLCD1602_h_displayPara.e_p_DisplayNextAddr = SsLCD1602_h_displayPara.e_h_RowPara[0].e_p_DisplayStartAddr;
- }
- else
- {
- /* empty */
- }
- }
- else
- {
- /* empty */
- }
- break;
-
- default :
- SsLCD1602_h_displayPara.e_e_SendStep = Ce_SendAddr_00;
- SsLCD1602_h_displayPara.e_p_DisplayNextAddr = SsLCD1602_h_displayPara.e_h_RowPara[0].e_p_DisplayStartAddr;
- break;
- }
- }
- void MngLCD1602_Init( void )
- {
- uint8_t index = 0u;
- uint8_t LeLCD1602InitCfg[4] = {0x38, 0x0c, 0x06, 0x01};
-
- SsLCD1602_h_displayPara.e_e_SendStep = Ce_SendAddr_00;
- SsLCD1602_h_displayPara.e_e_WriteState = Ce_Stay_0;
- SsLCD1602_h_displayPara.e_p_DisplayNextAddr = &SeLCD1602_u_displayBuffer[0][0];
-
- SsLCD1602_h_displayPara.e_h_RowPara[0].e_p_DisplayStartAddr = &SeLCD1602_u_displayBuffer[0][0];
- SsLCD1602_h_displayPara.e_h_RowPara[0].e_p_DisplayEndAddr = &SeLCD1602_u_displayBuffer[0][LCD1602_COLUMN_NUM-1];
-
- SsLCD1602_h_displayPara.e_h_RowPara[1].e_p_DisplayStartAddr = &SeLCD1602_u_displayBuffer[1][0];
- SsLCD1602_h_displayPara.e_h_RowPara[1].e_p_DisplayEndAddr = &SeLCD1602_u_displayBuffer[1][LCD1602_COLUMN_NUM-1];
-
- memset(SeLCD1602_u_displayBuffer, 0x20, sizeof(SeLCD1602_u_displayBuffer));
-
- while(index < (sizeof(LeLCD1602InitCfg)/sizeof(uint8_t)))
- {
- if(Ce_Stay_0 == SsLCD1602_h_displayPara.e_e_WriteState)
- {
- if(false == MngLCD1602_IsBusy())
- {
- LcdWriteCom(LeLCD1602InitCfg[index]);
- }
- else
- {
- /* empty */
- }
- }
- else if(Ce_SendOK == LcdWriteCom(LeLCD1602InitCfg[index]))
- {
- index++;
- }
- else
- {
- /* empty */
- }
- }
- }
- void MngLCD1602_Handle( void )
- {
- MngLCD1602_Display();
- }
- void displayString( uint8_t x, uint8_t y, uint8_t* dat, uint8_t len)
- {
- if(y >= 2)
- {
- return;
- }
- else
- {
- /* empty */
- }
- if(16 > (x + len))
- {
- memcpy(&SeLCD1602_u_displayBuffer[y][x], dat, len);
- }
- else
- {
- memcpy(&SeLCD1602_u_displayBuffer[y][x], dat, 15-x);
- }
- }
復制代碼
你可以參考下我的,寫了一個小時才調好。51還是有點費勁,沒法直接仿真,而且對指針、宏定義的處理也有些問題。你移植到stm32的話就很簡單了。周期調用MngLCD1602_Handle();接口就ok了,1ms、5ms、10ms一次都可以,時間越長刷新的越慢。 |
-
-
keil.zip
2023-5-24 22:55 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
135.3 KB, 下載次數: 4, 下載積分: 黑幣 -5
|