文本LCD模塊的控制FPGA
文本LCD模塊便宜且易于使用微控制器或FPGA進(jìn)行接口。
這是一個(gè)1行x 16個(gè)字符的模塊:
要控制LCD模塊,您需要11個(gè)IO引腳來(lái)驅動(dòng)8位數據總線(xiàn)和3個(gè)控制信號。3個(gè)控制信號是:
大多數LCD模塊都基于HD44780芯片或是兼容的。查閱Wikipedia以獲取更多信息。
7位設計
讓我們用FPGA板驅動(dòng)LCD模塊。
這是我們設計的框圖:
Pluto從PC串行端口接收數據,對其進(jìn)行反序列化,然后將其發(fā)送到LCD模塊。解串器與串行接口項目中的模塊相同,因此此處僅對其進(jìn)行實(shí)例化。
module LCDmodule(clk, RxD, LCD_RS, LCD_RW, LCD_E, LCD_DataBus); input clk, RxD; output LCD_RS, LCD_RW, LCD_E; output [7:0] LCD_DataBus; wire RxD_data_ready; wire [7:0] RxD_data; async_receiver deserializer(.clk(clk), .RxD(RxD), .RxD_data_ready(RxD_data_ready), .RxD_data(RxD_data));
每當串行端口提供一個(gè)字節時(shí),“ RxDdataready”將在一個(gè)時(shí)鐘周期內處于活動(dòng)狀態(tài)。
PC通過(guò)串口以8位模式向我們發(fā)送數據。理想情況下,我們需要從PC接收9位,以便我們可以驅動(dòng)8位數據總線(xiàn)和LCD模塊的“ RS”線(xiàn)?,F在,讓我們使用接收到的數據的MSB(第7位)來(lái)驅動(dòng)“ RS”,并將僅7位發(fā)送到數據總線(xiàn)。
assign LCD_RS = RxD_data[7]; assign LCD_DataBus = {1'b0, RxD_data[6:0]}; // sends only 7 bits to the module, padded with a '0' in front to make 8 bits assign LCD_RW = 0;
我們從不讀取LCD模塊,所以R / W線(xiàn)是接地的。
最后一個(gè)麻煩是“ E”信號需要長(cháng)時(shí)間激活,即220ns。從FPGA的角度來(lái)看,這很長(cháng),因為我使用的是25MHz時(shí)鐘(周期為40ns)。因此,“ E”至少需要驅動(dòng)5.5個(gè)時(shí)鐘。在這里,我們使用一個(gè)計數器對時(shí)鐘進(jìn)行計數,將其驅動(dòng)7個(gè)時(shí)鐘。
reg [2:0] count; always @(posedge clk) if(RxD_data_ready | (count!=0)) count <= count + 1;
“ E”信號是通過(guò)寄存器創(chuàng )建的,因此可以保證無(wú)干擾。
reg LCD_E; always @(posedge clk) LCD_E <= (count!=0);
波形如下所示:
HDL設計在這里。
軟件方面
我們對LCD進(jìn)行初始化并發(fā)送一些要顯示的數據。
以下是初始化LCD模塊并顯示“ hello”的C代碼。
void main(){ OpenComm(); // initialize the LCD module WriteCommByte(0x38); // "Function Set" in 8 bits mode WriteCommByte(0x0F); // "Display ON" with cursors ON WriteCommByte(0x01); // "Clear Display", can take up to 1.64ms, so the delay Sleep(2); // display "hello" WriteCommByte('h' + 0x80); WriteCommByte('e' + 0x80); WriteCommByte('l' + 0x80); WriteCommByte('l' + 0x80); WriteCommByte('o' + 0x80); CloseComm(); }
完整的代碼在這里。
要獲取有關(guān)HD44780指令集的更多信息,請在此處檢查。
8位設計
主要缺點(diǎn)是較早的設計是我們僅向LCD數據總線(xiàn)發(fā)送7位。這是一個(gè)問(wèn)題,因為無(wú)法再使用LCD模塊的設置DD RAM地址命令。
一種簡(jiǎn)單的解決方法是使用轉義符。我們選擇了字符0x00。
新協(xié)議如下:
新的C代碼是:
void main(){ OpenComm(); // initialize the LCD module WriteCommByte(0x00); WriteCommByte(0x38); // "Function Set" in 8 bits mode WriteCommByte(0x00); WriteCommByte(0x0F); // "Display ON" with cursors ON WriteCommByte(0x00); WriteCommByte(0x01); // "Clear Display", can take up to 1.64ms, so the delay Sleep(2); WriteCommByte('h'); WriteCommByte('e'); WriteCommByte('l'); WriteCommByte('l'); WriteCommByte('o'); WriteCommByte(0x00); WriteCommByte(0xC0); // go on second half of LCD WriteCommByte('e'); WriteCommByte('v'); WriteCommByte('e'); WriteCommByte('r'); WriteCommByte('y'); WriteCommByte('o'); WriteCommByte('n'); WriteCommByte('e'); CloseComm(); }
新的HDL代碼如下所示:
module LCDmodule(clk, RxD, LCD_RS, LCD_RW, LCD_E, LCD_DataBus); input clk, RxD;output LCD_RS, LCD_RW, LCD_E; output [7:0] LCD_DataBus; wire RxD_data_ready; wire [7:0] RxD_data; async_receiver deserialer(.clk(clk), .RxD(RxD), .RxD_data_ready(RxD_data_ready), .RxD_data(RxD_data)); assign LCD_RW = 0;assign LCD_DataBus = RxD_data; wire Received_Escape = RxD_data_ready & (RxD_data==0); wire Received_Data = RxD_data_ready & (RxD_data!=0); reg [2:0] count; always @(posedge clk) if(Received_Data | (count!=0)) count <= count + 1; // activate LCD_E for 6 clocks, so at 25MHz, that's 6x40ns=240ns reg LCD_E; always @(posedge clk)if(LCD_E==0) LCD_E <= Received_Data; else LCD_E <= (count!=6); reg LCD_instruction; always @(posedge clk)if(LCD_instruction==0) LCD_instruction <= Received_Escape; else LCD_instruction <= (count!=7); assign LCD_RS = ~LCD_instruction; endmodule
HD44780規范顯示,“ E”變低后,“ RS”必須在10ns內有效。因此,您會(huì )注意到這里“ E”僅被驅動(dòng)6個(gè)時(shí)鐘,并且“ LCDinstruction”標志僅在時(shí)鐘7之后被復位,以提供25ns的空間。
That's all folks!輪到您嘗試了。
評論