您当前的位置:五五电子网电子知识单片机-工控设备AVR单片机12864(ST7902)串口C51程序 正文
12864(ST7902)串口C51程序

12864(ST7902)串口C51程序

点击数:7466 次   录入时间:03-04 11:54:41   整理:http://www.55dianzi.com   AVR单片机

/********************************************************************************
*program name:st7920.c
*function: TEST TO st7920(drive)
*Design: qinhao
*Date:2009-01-07
*********************************************************************************/
#include <reg52.h>
#include <stdlib.h>
#include <intrins.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int

sbit sclk=P3^2;
sbit sid=P3^1;
sbit cs=P3^0;
unsigned char code tab[]={"深圳勤正达液晶显示器"};
void delaym(unsigned int n)
{unsigned int i,j;
for(j=n;j>0;j--);
for(i=112;i>0;i--);
}

void sendbit(uchar dat1, bitcnt)
{
unsigned char i;
for(i=0;i<bitcnt;i++)
{
sclk = 0;
dat1=dat1<<1;
sid = CY;
sclk = 1;
sclk = 0;
}
}

unsigned char receivebyte(void)
{
unsigned char i,temp1,temp2;
temp1=temp2=0;
for(i=0;i<8;i++)
{
temp1=temp1<<1;
sclk = 0;
sclk = 1;
sclk = 0;
if(sid) temp1++;
}
for(i=0;i<8;i++)
{
temp2=temp2<<1;
sclk = 0;
sclk = 1;
sclk = 0;
if(sid) temp2++;
}
return ((0xf0&temp1)+(0x0f&temp2));
}

void checkbusy( void )
{
do sendbit(0xfc,8); //11111,RW(1),RS(0),0
while(0x80&receivebyte());
}

void senddat(uchar dat1)
{
cs=1;
checkbusy();
sendbit(0xfa,8);
sendbit(dat1&0xf0,8);
sendbit((dat1<<4)&0xf0,8);
cs=0;

}

void sendcom(uchar com)
{
cs=1;
checkbusy();
sendbit(0xf8,8);
sendbit(com&0xf0,8);
sendbit((com<<4)&0xf0,8);
cs=0;

}

void inti()
{
sendcom(0x30); ///////8位MPU控制界面,基本指令集

sendcom(0x03);

sendcom(0x0c); ////// 开显示,关游标,

sendcom(0x01);

sendcom(0x06);

}
void display(uchar dat1)
{
unsigned char i,j;
sendcom(0x34);
sendcom(0x36);
for(i=0;i<32;i++)
{
sendcom(0x80|i);
sendcom(0x80);
for(j=0;j<32;j++)
senddat(dat1);
}
}

void main()
{
uchar i;
delaym(100);
inti();
for(i=0;i<20;i++)
{

senddat(tab[i]);
};
display(0xff);
while(1);
}

并口:
/********************************************************************************
*program name:并st7920.c
*function: TEST TO FM240128m-12l(drive)
*Design: qinhao
*Date:2009-3-30
*********************************************************************************/
#include <reg52.h>
#include <stdlib.h>
#include <intrins.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int

sbit e=P3^2;
sbit rs=P3^0;
sbit rw=P3^1;
sbit rst=P3^6;
/********************************************************************************
延时N ms
*********************************************************************************/
void delaym(unsigned int n)
{
 unsigned int i,j;
 for(j=n;j>0;j--)
 for(i=112;i>0;i--);
}                         /////// 精确MS,
/********************************************************************************
检查状态,D7为忙状态位,D6~D0为AC地址
*********************************************************************************/
uchar  BusyFlag()
{
 uchar  BF;
 rs=0;
 rw=1;
 e=1;
 _nop_();
 _nop_();
 BF=P1;
 _nop_();
 e=0;
 _nop_();
 return(BF);
}
/********************************************************************************
发送命令
*********************************************************************************/
void SendCommand(uchar command)
{
  // uchar BF;
 while(BusyFlag()&0x80);
 rs=0;
 rw=0;
 e=1;
 _nop_();
 P1=command;
 _nop_();
 _nop_();
 e=0;
 _nop_();
 _nop_();
}
/********************************************************************************
发送数据
*********************************************************************************/
void SendData(uchar dat)
{
   //uchar BF;
 while(BusyFlag()&0x80);
 rs=1;
 rw=0;
 e=1;
 _nop_();
 P1=dat;
 _nop_();
 _nop_();
 e=0;
 _nop_();
 _nop_();
}
/********************************************************************************
读AC地址
*********************************************************************************/
uchar ReaDAC()
{
 uchar AC;
 AC=BusyFlag()&0x07;
 return(AC);
}
/********************************************************************************
读数据
*********************************************************************************/
uchar ReadData()
{
 uchar dat;
 while(BusyFlag()&0x80);
 rs=1;
 rw=1;
 e=1;
 _nop_();
 dat=P1;
 _nop_();
 _nop_();
 e=0;
 _nop_();
 _nop_();
 return(dat);
}

/********************************************************************************
初始化LCD
*********************************************************************************/
void LcdInit()
{
 rst=0;
 delaym(10);
 rst=1;
 SendCommand(0x30);//基本指令集
 delaym(10);
 SendCommand(0x03);//AC回00H
 SendCommand(0x0c);//开显示,关游标
 SendCommand(0x01);//清屏,AC返回00H
 delaym(10);
 SendCommand(0x06);//设定游标显示移位
 delaym(10);
}
/********************************************************************************

*********************************************************************************/

void main()
{
 uchar i;
 LcdInit();
  SendCommand(0x34); 扩充指令
 SendCommand(0x04); 反白00行
 while(1)
 {
  i++;
  SendData(i);
  delaym(700);
 };
}




本文关键字:程序  AVR单片机单片机-工控设备 - AVR单片机