新手,急,C语言,用ATMega48怎么控制OCMJ4X8C啊?
串行的,高手可不可以写个简单的程序详细说明一下啊,头文件、程序里面需要注意的,等等,说的越详细越好,我几乎不懂!!谢谢大家了! 大家帮帮忙啊,着急啊!! http://pic16.com/bbs/dispbbs.asp?boardID=8&ID=44640&page=1这里有一个LCD型号是一相的,也是用SPI串口不过是用PIC18F2550单片机控制的。,,你只要修改SPI的写函数就可以使用了。。。 #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
} #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 非常感谢ivws和hali两位朋友!!! 做过,但不是串口的,帮不上
页:
[1]