搜索
bottom↓
回复: 6

新手,急,C语言,用ATMega48怎么控制OCMJ4X8C啊?

[复制链接]

出0入0汤圆

发表于 2009-5-22 10:17:03 | 显示全部楼层 |阅读模式
串行的,高手可不可以写个简单的程序详细说明一下啊,头文件、程序里面需要注意的,等等,说的越详细越好,我几乎不懂!!谢谢大家了!

出0入0汤圆

 楼主| 发表于 2009-5-22 10:51:14 | 显示全部楼层
大家帮帮忙啊,着急啊!!

出0入0汤圆

发表于 2009-5-22 11:06:33 | 显示全部楼层
http://pic16.com/bbs/dispbbs.asp?boardID=8&ID=44640&page=1

这里有一个LCD型号是一相的,也是用SPI串口不过是用PIC18F2550单片机控制的。,,你只要修改SPI的写函数就可以使用了。。。

出0入0汤圆

发表于 2009-5-22 11:12:01 | 显示全部楼层
#include "includes.h"

#if ST7920 > 0

#include "font_st.h"
#include "st7920.h"

/*---------------显示汉字或字符----------------*/
void chinese_disp (const unsigned char *chinese)
{
        unsigned char i;
  WriteLCD (Com,0x30);
  WriteLCD (Com,0x80);

        for(i=16*4;i!=0;i--)
                WriteLCD (Data,pgm_read_byte(chinese++));
}

/*-----------上半屏显示汉字或字符------------*/
void chinese_disp1 (const unsigned char *chinese)
{
  unsigned char i;
  WriteLCD (Com,0x30);
  WriteLCD (Com,0x80);
       
        for(i=16;i!=0;i--)
                WriteLCD (Data,pgm_read_byte(chinese++));
               
  WriteLCD (Com,0x90);
        for(i=16;i!=0;i--)
                WriteLCD (Data,pgm_read_byte(chinese++));
}

/*----------------显示图形-----------------*/
void img_disp (const unsigned char *img)
{
  unsigned char i,j;
  for(j=0;j<32;j++)
  {
    for(i=0;i<8;i++)
    {
      WriteLCD (Com,0x34);
      WriteLCD (Com,y+j);
      WriteLCD (Com,x1+i);
      WriteLCD (Com,0x30);
                        WriteLCD (Data,pgm_read_byte(img++));
      WriteLCD (Data,pgm_read_byte(img++));
    }
  }
  for(j=32;j<64;j++)
  {
    for(i=0;i<8;i++)
    {
      WriteLCD (Com,0x34);
      WriteLCD (Com,y+j-32);
      WriteLCD (Com,x2+i);
      WriteLCD (Com,0x30);
                        WriteLCD (Data,pgm_read_byte(img++));
      WriteLCD (Data,pgm_read_byte(img++));
    }
  }
  WriteLCD (Com,0x36);
}

/*-------------下半屏显示图形--------------*/
void img_disp1 (const unsigned char *img)
{
  unsigned char i,j;
  for(j=0;j<32;j++)
  {
    for(i=0;i<8;i++)
    {
      WriteLCD (Com,0x34);
      WriteLCD (Com,y+j);
      WriteLCD (Com,x2+i);
      WriteLCD (Com,0x30);
                        WriteLCD (Data,pgm_read_byte(img++));
      WriteLCD (Data,pgm_read_byte(img++));
    }
  }
  WriteLCD (Com,0x36);
}

/*--------------显示点阵----------------*/
void lat_disp (unsigned char Dataa1,unsigned char Dataa2)
{
  unsigned char i,j,k,x;
  x=x1;
  for(k=0;k<2;k++)
  {
    for(j=0;j<16;j++)
    {
      for(i=0;i<8;i++)
      {
        WriteLCD (Com,0x34);
        WriteLCD (Com,y+j*2);
        WriteLCD (Com,x+i);
        WriteLCD (Com,0x30);
        WriteLCD (Data,Dataa1);
        WriteLCD (Data,Dataa1);
      }
      for(i=0;i<8;i++)
      {
        WriteLCD (Com,0x34);
        WriteLCD (Com,y+j*2+1);
        WriteLCD (Com,x+i);
        WriteLCD (Com,0x30);
        WriteLCD (Data,Dataa2);
        WriteLCD (Data,Dataa2);
      }
    }
    x=x2;
  }
  WriteLCD (Com,0x36);
}

/*-----------------------------------------------*/
//当Dataa1=0xff,Dataa2=0xff时,在x0,y0处反白显示16xl*yl.
void con_disp (unsigned char Dataa1,unsigned char Dataa2,unsigned char x0,unsigned char y0,unsigned char xl,unsigned char yl)
{
  unsigned char i,j;
  for(j=0;j<yl;j++)
  {
    for(i=0;i<xl;i++)
    {
      WriteLCD (Com,0x34);
      WriteLCD (Com,y0+j);
      WriteLCD (Com,x0+i);
      WriteLCD (Com,0x30);
      WriteLCD (Data,Dataa1);
      WriteLCD (Data,Dataa2);
    }
  }
  WriteLCD (Com,0x36);
}

/*--------------清DDRAM------------------*/
void clrram (void)
{
  WriteLCD (Com,0x30);
  WriteLCD (Com,0x01);
        _delay_us(1600);
}

/*---------------------------------------*/
void WriteByteLCD(unsigned char data)
{
        unsigned char i;
        for(i=8;i!=0;i--)
        {
                if(data & 0x80)
                        sda_1;
                else
                        sda_0;
                sclk_fall;
                data<<=1;
        }
}

/*---------------------------------------
* 时钟clk,上升沿把数据锁入
* 1 1 1 1 1 r/w rs 0   d7 d6 d5 d4 0 0 0 0   d3 d2 d1 d0 0 0 0 0*/
void WriteLCD (unsigned char Data_Com,unsigned char data)
{
        _delay_us(50);
        sclk_0;
        WriteByteLCD(Data_Com);
        WriteByteLCD(data & 0xf0);
        WriteByteLCD(data<<4 );
}

/*------------------初始化-----------------*/
void st7920_init (void)
{
  //rst=1;
  //psb=0;
        st_init;
  WriteLCD (Com,0x30);  /*30---基本指令动作*/   
  WriteLCD (Com,0x01);  /*清屏,地址指针指向00H*/
        _delay_us(1600);
  WriteLCD (Com,0x06);  /*光标的移动方向*/
  WriteLCD (Com,0x0c);  /*开显示,关游标*/
}

extern unsigned char Second;
#endif

/*------------------主程序--------------------*/
void st7920_test(void)
{
        #if ST7920 > 0
        static unsigned char time;
        static unsigned char j;
       
        if((unsigned char)(Second - time) >= 2)
        {
                st_init;
                time = Second;
                switch(j)
                {
                        case 0:
                                lat_disp (0x00,0x00);
                                chinese_disp (tab1);
                                con_disp (0xff,0xff,0x8c,0x80,2,16);
                                j = 1;
                                break;
                        case 1:
                                clrram();
                                lat_disp (0xcc,0xcc);
                                j = 2;
                                break;
                        case 2:
                                lat_disp (0x00,0x00);
                                chinese_disp1 (tab31);
                                img_disp1 (tab32);
                                j = 3;
                                break;
                        case 3:
                                clrram();
                                lat_disp (0xff,0x00);
                                j = 4;
                                break;
                        case 4:
                                img_disp (tab5);
                                j = 0;
                                break;
                        default:
                                j = 0;                               
                                break;
                }
        }
        #endif
}

出0入0汤圆

发表于 2009-5-22 11:13:17 | 显示全部楼层
#ifndef __st7920_h
#define __st7920_h 1

#define x1    0x80
#define x2    0x88
#define y     0x80

#define Com            0xf8  //写指令
#define Data           0xfa  //写数据

#define sda                         PD3
#define sclk                        PD4
#define sda_1                 PORTD |= (1<<sda)
#define sda_0                 PORTD &=~(1<<sda)
#define sclk_1                 PORTD |= (1<<sclk)
#define sclk_0                 PORTD &=~(1<<sclk)
#define st_init         DDRD |= (1<<sclk)|(1<<sda)
#define sclk_fall PORTD |= (1<<sclk);PORTD &=~(1<<sclk)

/*---------------显示汉字或字符----------------*/
void chinese_disp (const unsigned char *chinese);

/*-----------上半屏显示汉字或字符------------*/
void chinese_disp1 (const unsigned char *chinese);

/*----------------显示图形-----------------*/
void img_disp (const unsigned char *img);

/*-------------下半屏显示图形--------------*/
void img_disp1 (const unsigned char *img);

/*--------------显示点阵----------------*/
void lat_disp (unsigned char DATAa1,unsigned char DATAa2);

/*-----------------------------------------------*/
//当DATAa1=0xff,DATAa2=0xff时,在x0,y0处反白显示16xl*yl.
void con_disp (unsigned char DATAa1,unsigned char DATAa2,
               unsigned char x0,unsigned char y0,
                                                         unsigned char xl,unsigned char yl);
                                                         
/*--------------清DDRAM------------------*/
void clrram (void);                       

/*---------------------------------------*/
void WriteLCD (unsigned char DATA_COM,unsigned char data);                       

/*------------------初始化-----------------*/
void st7920_init (void);         

void st7920_test (void);
                                                         
#endif

出0入0汤圆

 楼主| 发表于 2009-5-27 17:12:45 | 显示全部楼层
非常感谢ivws和hali两位朋友!!!

出0入0汤圆

发表于 2009-5-27 17:28:32 | 显示全部楼层
做过,但不是串口的,帮不上
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-5 00:08

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表