我是把一个8051的c语言程序翻译成 python程序的:
python程序如下:
- #!/usr/bin/env python3
- # encoding: utf-8
- import RPi.GPIO as GPIO
- import time
- #psb接高电平
- RS = 17
- #E(或EN)端为使能(enable)端,高电平(1)时读取信息,负跳变时执行指令
- E = 27
- RES = 18
- #WRD is RW
- WRD = 21
- DPIN = (22,23,24,25,12,16,5,6)
- HANZI_STR="hello you hello you hello you hello you hello you hello you hello you hello you "
- def main():
- init0()
- initial()
- byte_array = bytes(HANZI_STR,'gb18030')
- lcd_mesg(byte_array)
- time.sleep(3)
- def init0():
- GPIO.setwarnings(False)
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(RS, GPIO.OUT)
- GPIO.setup(E, GPIO.OUT)
-
- GPIO.setup(RES, GPIO.OUT)
- GPIO.setup(WRD, GPIO.OUT)
-
- for pin in DPIN:
- print(pin)
- GPIO.setup(pin, GPIO.OUT)
- def initial():
-
- GPIO.output(RES,0)
- time.sleep(0.01)
-
- GPIO.output(RES,1)
- time.sleep(0.01)
- TransferData(0x30,0)
- time.sleep(0.01)
- TransferData(0x30,0)
- time.sleep(0.01)
- TransferData(0x08,0)
- time.sleep(0.01)
- TransferData(0x10,0)
- time.sleep(0.01)
- TransferData(0x0C,0)
- time.sleep(0.01)
- TransferData(0x01,0)
- time.sleep(0.01)
- TransferData(0x06,0)
- time.sleep(0.01)
-
- def lcd_mesg(str):
- TransferData(0x80,0)
- time.sleep(0.01)
- for i in range(32):
- TransferData(str[i],1)
- TransferData(0x90,0)
- time.sleep(0.01)
- for i in range(32,64):
- TransferData(str[i],1)
- def TransferData(byte,di):
- GPIO.output(WRD,0)
- GPIO.output(RS,di)
- time.sleep(0.001)
- dataout(byte)
- GPIO.output(E,1)
- time.sleep(0.001)
- GPIO.output(E,0)
-
- #output to dpin
- def dataout(byte):
- x = 0x01
- for n in range(8):
- if byte&x == x:
- GPIO.output(DPIN[n], 1)
- else:
- GPIO.output(DPIN[n], 0)
- x<<1
-
- if __name__ == '__main__':
- try:
- main()
- except KeyboardInterrupt:
- pass
- finally:
- print('cleaning')
-
-
复制代码原来的c语言程序如下:
- #include <stdio.h>
- #include <math.h>
- #include <reg52.h>
- #include <string.h>
- unsigned char code IC_DAT[];
- unsigned char code pic1[];
- unsigned char code pic2[];
- unsigned char code pic3[];
- sbit RS =P2^0;
- sbit WRD=P2^1;
- sbit E= P2^2;
- sbit PSB=P2^4;
- sbit RES=P2^5;
- void TransferData(char data1,bit DI);
- void display(void);
- void display_grapic(void);
- void delayms(unsigned int n);
- void DisplayLine(unsigned char line1,unsigned char line2);
- void DisplayGraphic(unsigned char code *adder);
- void delay(unsigned int m);
- void lcd_mesg(unsigned char code *adder1);
- unsigned char code IC_DAT[]={
- "泉州海创电子科技"
- "单片机开发板系列"
- "中文字库测试程序"
- "恭喜发财身体健康"
- };
- unsigned char code IC_DAT2[]={
- "海纳百川宽容为先"
- "欲成大业诚信为先"
- "游弋商海济世为先"
- "人立于世守法为先"
- };
- void initinal(void) //LCD字库初始化程序
- {
- delay(40); //大于40MS的延时程序
- PSB=1; //设置为8BIT并口工作模式
- delay(1); //延时
- RES=0; //复位
- delay(1); //延时
- RES=1; //复位置高
- delay(10);
- TransferData(0x30,0); //Extended Function Set :8BIT设置,RE=0: basic instruction set, G=0 :graphic display OFF
- delay(100); //大于100uS的延时程序
- TransferData(0x30,0); //Function Set
- delay(37); ////大于37uS的延时程序
- TransferData(0x08,0); //Display on Control
- delay(100); //大于100uS的延时程序
- TransferData(0x10,0); //Cursor Display Control光标设置
- delay(100); //大于100uS的延时程序
- TransferData(0x0C,0); //Display Control,D=1,显示开
- delay(100); //大于100uS的延时程序
- TransferData(0x01,0); //Display Clear
- delay(10); //大于10mS的延时程序
- TransferData(0x06,0); //Enry Mode Set,光标从右向左加1位移动
- delay(100); //大于100uS的延时程序
- }
- void initina2(void) //LCD显示图片(扩展)初始化程序
- {
- delay(40); //大于40MS的延时程序
- PSB=1; //设置为8BIT并口工作模式
- delay(1); //延时
- RES=0; //复位
- delay(1); //延时
- RES=1; //复位置高
- delay(10);
-
- TransferData(0x36,0); //Extended Function Set RE=1: extended instruction
- delay(100); //大于100uS的延时程序
- TransferData(0x36,0); //Extended Function Set:RE=1: extended instruction set
- delay(37); ////大于37uS的延时程序
- TransferData(0x3E,0); //EXFUNCTION(DL=8BITS,RE=1,G=1)
- delay(100); //大于100uS的延时程序
- TransferData(0x01,0); //CLEAR SCREEN
- delay(100); //大于100uS的延时程序
- }
- void main(void)
- {
- while(1)
- {
- initina2(); //调用LCD显示图片(扩展)初始化程序
-
- DisplayGraphic(pic2); //显示图片2
- delayms(200);
-
- DisplayGraphic(pic3); //显示图片3
- delayms(200);
-
- initinal(); //调用LCD字库初始化程序
- delay(100); //大于100uS的延时程序
- lcd_mesg(IC_DAT); //显示中文汉字1
- delayms(240);
- delayms(240);
-
- initinal(); //调用LCD字库初始化程序
- delay(100); //大于100uS的延时程序
- lcd_mesg(IC_DAT2); //显示中文汉字2
- delayms(240);
- delayms(240);
- }
- }
- void lcd_mesg(unsigned char code *adder1)
- {
- unsigned char i;
- TransferData(0x80,0); //Set Graphic Display RAM Address
- delay(100);
- for(i=0;i<32;i++)
- {
- TransferData(*adder1,1);
- adder1++;
- }
-
- TransferData(0x90,0); //Set Graphic Display RAM Address
- delay(100);
- for(i=32;i<64;i++)
- {
- TransferData(*adder1,1);
- adder1++;
- }
- }
- void TransferData(char data1,bit DI) //传送数据或者命令,当DI=0是,传送命令,当DI=1,传送数据.
- {
- WRD=0;
- RS=DI;
- delay(1);
- P0=data1;
- E=1;
- delay(1);
- E=0;
- }
- void delayms(unsigned int n) //延时10×n毫秒程序
- {
- unsigned int i,j;
- for(i=0;i<n;i++)
- for(j=0;j<2000;j++);
- }
- void delay(unsigned int m) //延时程序
- {
- unsigned int i,j;
- for(i=0;i<m;i++)
- for(j=0;j<10;j++);
- }
- void DisplayGraphic(unsigned char code *adder)
- {
- int i,j;
- //*******显示上半屏内容设置
- for(i=0;i<32;i++) //
- {
- TransferData((0x80 + i),0); //SET 垂直地址 VERTICAL ADD
- TransferData(0x80,0); //SET 水平地址 HORIZONTAL ADD
- for(j=0;j<16;j++)
- {
- TransferData(*adder,1);
- adder++;
- }
- }
- //*******显示下半屏内容设置
- for(i=0;i<32;i++) //
- {
- TransferData((0x80 + i),0); //SET 垂直地址 VERTICAL ADD
- TransferData(0x88,0); //SET 水平地址 HORIZONTAL ADD
- for(j=0;j<16;j++)
- {
- TransferData(*adder,1);
- adder++;
- }
- }
- }
复制代码