切换到宽版
  • 1971阅读
  • 10回复

[PIC]坛友处买的YXD-255128 灰度显示 [复制链接]

上一主题 下一主题
离线d2xs
 

发帖
117
M币
2761
专家
2
粉丝
16
只看楼主 倒序阅读 我要置顶 楼主  发表于: 2017-03-25
从坛友处购得lcd两块,吃灰多日!近日花了点时间,借鉴网友的程序,成功点亮!
后来看了下ST7529的规格书,发现支持32级灰度显示!马上动手实验,填了不少的坑!终于成功了!









附上代码,关键地方已有注释。应为rom空间不够,采用的是单片机usart接收电脑传过来的图像数据!

  1. #include <xc.h>
  2. #include <pic16f887.h>
  3. #define _XTAL_FREQ 8000000
  4. #define rst RD2
  5. #define rd  RD7
  6. #define wr  RD6
  7. #define a0  RD5
  8. #define cs  RD4
  9. #define P1  PORTB
  10. #define uchar unsigned char
  11. #define uint unsigned int
  12. uchar RX_dat, w;
  13. bit RX_ok;
  14. // CONFIG1
  15. #pragma config FOSC = INTRC_CLKOUT// Oscillator Selection bits (INTOSC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
  16. #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
  17. #pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
  18. #pragma config MCLRE = OFF      // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
  19. #pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
  20. #pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
  21. #pragma config BOREN = ON       // Brown Out Reset Selection bits (BOR enabled)
  22. #pragma config IESO = OFF       // Internal External Switchover bit (Internal/External Switchover mode is disabled)
  23. #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
  24. #pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
  25. // CONFIG2
  26. #pragma config BOR4V = BOR21V   // Brown-out Reset Selection bit (Brown-out Reset set to 2.1V)
  27. #pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)
  28. void CMD(uchar command) {
  29.     a0 = 0;
  30.     cs = 0;
  31.     rd = 1;
  32.     wr = 0;
  33.     P1 = command;
  34.     //__nop();
  35.     wr = 1;
  36.     cs = 1;
  37. }
  38. void DAT(uchar date) {
  39.     a0 = 1;
  40.     cs = 0;
  41.     rd = 1;
  42.     wr = 0;
  43.     P1 = date;
  44.     //__nop();
  45.     wr = 1;
  46.     cs = 1;
  47. }
  48. void LCD_init(void) {
  49.     rst = 0;
  50.     __delay_ms(200);
  51.     rst = 1;
  52.     __delay_ms(1000);
  53.     CMD(0x30); //Ext in
  54.     __nop();
  55.     CMD(0x94); //Sleep out
  56.     __nop();
  57.     CMD(0xD1); //Internal OSC on
  58.     __nop();
  59.     CMD(0x20); //Pwr Control
  60.     DAT(0x08); //Turn On Booster
  61.     __delay_ms(1);
  62.     CMD(0x20); //Pwr Control
  63.     DAT(0x0B); //Turn on REF&VFLR
  64.     __nop();
  65.     CMD(0x81); //EC Control 对比度调节(微调)
  66.     DAT(0x04); // 低5位
  67.     DAT(0x03); //高3位
  68.     __nop();
  69.     CMD(0xca); //Display Control
  70.     DAT(0x04); //1/2x OSC 内部震荡2分频
  71.     DAT(0x27); //Drive Duty=160 扫描周期
  72.     DAT(0x00); //Change it To Find a Certain Value For Better Display Status.
  73.     __nop();
  74.     CMD(0xa7); //0XA6:正显(黑底白字); 0XA7:反显(白底黑字)
  75.     __nop();
  76.     CMD(0xbb); //行扫描顺序设置
  77.     DAT(0x01); //0~79&80~159
  78.     __nop();
  79.     CMD(0xbc); //列扫描顺序设置
  80.     DAT(0x00); //Normal
  81.     DAT(0x00); //Normal
  82.     DAT(0x02); //3B3P Data Mode
  83.     __nop();
  84.     CMD(0x75); //Line Addr Set 行地址范围
  85.     DAT(0x00); //Start=0
  86.     DAT(0x9f); //End=160 0x9f 0~159  128 0x7f 0~127
  87.     __nop();
  88.     CMD(0x15); //Column Addr Set 列地址范围
  89.     DAT(0x00); //Start=0
  90.     DAT(0x54); //End=255/3-1=84
  91.     __nop();
  92.     CMD(0x31); //Ext out
  93.     __nop();
  94.     CMD(0x32); //Analog Circuit Set
  95.     DAT(0x00); //OSC Freq=000   Default 震荡频率
  96.     DAT(0x01); //Booster Eff=01 Default 升压效率
  97.     DAT(0x06); //Bias=1/7 LCD分压比 显示对比度调节关键参数(粗调)
  98.     __nop();
  99.     CMD(0x34); //关闭抖动电路
  100.     __nop();
  101.     CMD(0x20);//Set Gray 1 value 奇数帧灰度电平设置
  102.     DAT(0x01);
  103.     DAT(0x03);
  104.     DAT(0x05);
  105.     DAT(0x07);
  106.     DAT(0x09);
  107.     DAT(0x0B);
  108.     DAT(0x0D);
  109.     DAT(0x0F);
  110.     DAT(0x11);
  111.     DAT(0x13);
  112.     DAT(0x15);
  113.     DAT(0x17);
  114.     DAT(0x19);
  115.     DAT(0x1B);
  116.     DAT(0x1D);
  117.     DAT(0x1F);
  118.     __nop();
  119.     CMD(0x21);//Set Gray 2 value  偶数帧灰度电平设置
  120.     DAT(0x00);
  121.     DAT(0x02);
  122.     DAT(0x04);
  123.     DAT(0x06);
  124.     DAT(0x08);
  125.     DAT(0x0A);
  126.     DAT(0x0C);
  127.     DAT(0x0E);
  128.     DAT(0x10);
  129.     DAT(0x12);
  130.     DAT(0x14);
  131.     DAT(0x16);
  132.     DAT(0x18);
  133.     DAT(0x1A);
  134.     DAT(0x1C);
  135.     DAT(0x1E);
  136.     __nop();
  137.     CMD(0x30); //Ext in
  138.     __nop();
  139.     CMD(0xaf); //display on
  140. }
  141. void Displaychar(uchar Data, uchar x) {
  142.     uchar k, OutData;
  143.     for (k = 0; k < 2; k++) {
  144.         OutData = (Data & 0xF0);
  145.         DAT(OutData);
  146.         w++;
  147.         if (w == 255) {
  148.             w = 0;
  149.             break;
  150.         }
  151.         Data <<= 4;
  152.     }
  153. }
  154. void Set_xy(uchar x, uchar y) {
  155.     x += 0x00;
  156.     y += 0x10;
  157.     CMD(0x30);
  158.     CMD(0x15);
  159.     DAT(x);
  160.     DAT(0x54); // column address set start=0;end=(240/3)-1=0x4f  
  161.     CMD(0x75);
  162.     DAT(y);
  163.     DAT(0X8F); // line address set start=0;end=159  
  164. }
  165. void ClrScreen(void) {
  166.     uchar i, j;
  167.     CMD(0x30); // ext0 set of instruction  
  168.     CMD(0x15);
  169.     DAT(0x00);
  170.     DAT(0x54); // column address set start=0;end=(240/3)-1=0x4f  
  171.     CMD(0x75);
  172.     DAT(0x00);
  173.     DAT(0X9F); // line address set start=0;end=159  
  174.     CMD(0x5c); // RAMWR, wrriting to memory  
  175.     for (i = 0; i < 160; i++) {
  176.         for (j = 0; j < 255; j++) // each time process 8 dot  
  177.         {
  178.             DAT(0x00);
  179.         }
  180.     }
  181. }
  182. void MCU_init(void) {
  183.     OSCCON = 0B01111000;
  184.     OSCTUNE = 0B00000000;
  185.     PORTA = 0;
  186.     PORTB = 0;
  187.     PORTC = 0;
  188.     PORTD = 0;
  189.     PORTE = 0;
  190.     CM1CON0 = 0X07;
  191.     CM2CON0 = 0X07;
  192.     ANSEL = 0B00000000;
  193.     ANSELH = 0B00000000;
  194.     TRISA = 0B00000111;
  195.     TRISB = 0B00000000;
  196.     TRISC = 0B00000000;
  197.     TRISD = 0B00000000;
  198.     TRISE = 0B00001000;
  199.     OPTION_REG = 0B11000000;
  200.     INTCON = 0B11000000;
  201.     PIE1 = 0B00000010;
  202.     PIR1 = 0B00000000;
  203.     PIE2 = 0B00000000;
  204.     PIR2 = 0B00000000;
  205.     T1CON = 0B00000100;
  206.     T2CON = 0B00100010; //5*16*250*0.5us=10MS
  207.     PR2 = 0XF9; //250
  208.     TMR1ON = 1;
  209.     TMR2ON = 1;
  210. }
  211. void Usart_Init(void) {
  212.     GIE = 0;
  213.     TRISC7 = 1; //RX置输入
  214.     TRISC6 = 0; //TX清成输出
  215.     SYNC = 0; //0异步模式 1同步模式
  216.     BRG16 = 1; //使用16位波特率发生器
  217.     BRGH = 1; //高波特率选择位 0低速  1高速
  218.     SPBRGH = 0x00;
  219.     SPBRG = 0x22; //57600
  220.     GIE = 1; //开GIE
  221.     RCIE = 1; //开接收中断
  222. }
  223. void Usart_TX_char(char temp) {
  224.     TXREG = temp;
  225.     while (TRMT == 0);
  226. }
  227. void Usart_TX_string(const char *str) {
  228.     SPEN = 1; //使能串行端口
  229.     TXEN = 1;
  230.     while ((*str) != '\0') {
  231.         Usart_TX_char(*str++);
  232.     }
  233. }
  234. void huidu(void) {
  235.     uchar i, j, k;
  236.     CMD(0x30); // ext0 set of instruction  
  237.     CMD(0x15);
  238.     DAT(0x00);
  239.     DAT(0x54); // column address set start=0;end=(240/3)-1=0x4f  
  240.     CMD(0x75);
  241.     DAT(0x00);
  242.     DAT(0X9F); // line address set start=0;end=159  
  243.     CMD(0x5c); // RAMWR, wrriting to memory  
  244.     for (i = 0; i < 160; i++) {
  245.         for (j = 255; j > 0; j--) // each time process 8 dot  
  246.         {
  247.             k = (j & 0xF8);
  248.             DAT(k);
  249.         }
  250.     }
  251. }
  252. void main(void) {
  253.     rst = 0;
  254.     MCU_init();
  255.     RD3 = 1;
  256.     Usart_Init();
  257.     LCD_init();
  258.     //ClrScreen();
  259.     huidu();
  260.     Set_xy(0, 0);
  261.     CMD(0x5c);
  262.     SPEN = 1; //使能串行端口
  263.     CREN = 1; //连续接受多位数据
  264.     Usart_TX_string("OK");
  265.     while (1) {
  266.         if (RX_ok == 1) {
  267.            Displaychar(RX_dat, 1);
  268.            RX_ok = 0;
  269.         }
  270.     }
  271. }
  272. void interrupt ISR(void) {
  273.     if (TMR2IE && TMR2IF) {
  274.         TMR2IF = 0;
  275.     }
  276.     if (RCIE && RCIF) {
  277.         RCIF = 0;
  278.         RX_dat = RCREG;
  279.         RX_ok = 1;
  280.         RD3 = ~RD3;
  281.     }
  282. }





本文内容包含图片或附件,获取更多资讯,请 登录 后查看;或者 注册 成为会员获得更多权限
本帖最近打赏记录:共3条打赏M币+21
离线yang123xh

发帖
463
M币
585
专家
-10
粉丝
80
只看该作者 1楼 发表于: 2017-03-25
厉害,这么长的代码是咋记下来的
离线solosky

发帖
154
M币
807
专家
1
粉丝
16
只看该作者 2楼 发表于: 2017-03-25
显示屏不错啊 ,可惜买不到了。
离线d2xs

发帖
117
M币
2761
专家
2
粉丝
16
只看该作者 3楼 发表于: 2017-03-26
回 yang123xh 的帖子
yang123xh:厉害,这么长的代码是咋记下来的 (2017-03-25 15:52) 回 yang123xh 的帖子

ctrl+v and ctrl+p 就好!并不用去记!
离线hongo

发帖
51345
M币
144490
专家
271
粉丝
15149
只看该作者 4楼 发表于: 2017-03-27
很牛,这个屏可以做电视了吧?
离线cknck
发帖
31
M币
21
专家
0
粉丝
4
只看该作者 5楼 发表于: 2017-04-05
厉害,学习了
离线ayumi7

发帖
12299
M币
3965
专家
25
粉丝
5718
只看该作者 6楼 发表于: 2017-04-06
    弄个黑白液晶电视 耍耍
离线lbq691477940

发帖
764
M币
1792
专家
12
粉丝
75
只看该作者 7楼 发表于: 2017-04-08
回 solosky 的帖子
solosky:显示屏不错啊 ,可惜买不到了。  (2017-03-25 16:49) 回 solosky 的帖子

我留有二个没地方用
离线mnpecho

发帖
14
M币
-448
专家
0
粉丝
1
只看该作者 8楼 发表于: 2017-04-08
看着像移动座机的屏幕 啊
离线1065307738

发帖
319
M币
2863
专家
1
粉丝
23
只看该作者 9楼 发表于: 01-13
这么牛,能不能用stm32f103驱动呢?直接显示jpg图片。
快速回复
限80 字节
如果您提交过一次失败了,可以用”恢复数据”来恢复帖子内容
 
上一个 下一个