⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 st7920.h

📁 C51编程
💻 H
字号:

//#include <c8051f020.h>                    // SFR declarations



sbit cs_st7920=P1^1;
sbit io_st7920=P1^3;
sbit clk_st7920=P1^5;


void PORT_Init (void);
void to_st7920(unsigned char,unsigned char,unsigned char);
void initialize_st7920();
void show_string_st7920(unsigned char ,unsigned char *);
void calc_volt(int,unsigned char,int,unsigned char *);
void show_volt(unsigned char*);

/*void main (void) {
   unsigned char sh[7];

   // disable watchdog timer
   WDTCN = 0xde;
   WDTCN = 0xad;

   PORT_Init();
	
   initialize_st7920();
//   show_string_st7920(0x80,"asd");

   calc_volt(0x0ffe,24,0x0FFF,sh);
   show_volt(sh);



   while (1) 
   {                            // spin forever

   }
}*/
/*void PORT_Init (void)
{
   XBR2    = 0x40;                     // Enable crossbar and weak pull-ups
   P1MDOUT |= 0xFF;                    // enable P1 as push-pull output
}*/




void initialize_st7920()
{
    to_st7920(00,00,0x30);      //功能设定:基本指令集动作;
    to_st7920(00,00,0x0f);      //显示状态:整体显示开、游标开、游标位置开;
  //  to_st7920(00,00,0x01);      //清除显示;
   // time2ms;
    to_st7920(00,00,0x06);      //指定在数据的读取与写入时,设定光标移动方向 以及指定显示的移位;
	to_st7920(0,0,0x0c);
}


void to_st7920(unsigned char rw,unsigned char rs,unsigned char DT)
{
    unsigned char i = 0 ;
	unsigned char temp=0;
//////////////////
    cs_st7920=1;
//////////////////
    io_st7920=1;
    for(i=0;i<5;i++)
    {
        clk_st7920=0;
        clk_st7920=1;
    }
///////////////////
	clk_st7920=0;
    if(rw==1)
        io_st7920=1;
    else
        io_st7920=0;
    clk_st7920=1;
///////////////////
    clk_st7920=0;
    if(rs==1)
        io_st7920=1;
    else
        io_st7920=0;
    clk_st7920=1;
//////////////////
	clk_st7920=0;
    io_st7920=0;
	clk_st7920=1;
///////////////////
    temp=DT;
    for(i=0;i<4;i++)
    {
	    clk_st7920=0;
        if((temp&0x80)==0x80)
            io_st7920=1;
        else
            io_st7920=0;
        clk_st7920=1;
        temp = temp<<1;
    }
////////////////////
    io_st7920=0;
    for(i=0;i<4;i++)
    {
        clk_st7920=0;
        clk_st7920=1;
    }
///////////////////
    for(i=0;i<4;i++)
    {
	    clk_st7920=0;
        if((temp&0x80)==0x80)
            io_st7920=1;
        else
            io_st7920=0;
        clk_st7920=1;
        temp = temp<<1;
    }
////////////////////
    io_st7920=0;
    for(i=0;i<4;i++)
    {
        clk_st7920=0;
        clk_st7920=1;
    }
///////////////////
    clk_st7920=0;
	cs_st7920=0;
}


void show_string_st7920(unsigned char pos,unsigned char *s)   
{
    to_st7920(0,0,pos);
    while(*s)
    {
        to_st7920(0,1,*s);
        s++;
    }
}

void calc_volt(int ad_value,unsigned char ref_v,int max_value,unsigned char *s)
{ 
    char i;
    long int temp;
	temp=ad_value;
	temp=temp*ref_v*10210/max_value;
    for(i=5;i>=0;i--)
   {	    
       s[i]=temp%10+0x30;
	   temp=temp/10;
   }
	s[4]=0;   
}

void show_volt(unsigned char *s)
{
    show_string_st7920(0x80,"通道");
    to_st7920(0,1,AMX0SL+0x30);
    to_st7920(0,1,':');
    to_st7920(0,1,*s);
    to_st7920(0,1,'.');
	show_string_st7920(0x84,s+1);
	show_string_st7920(0x86,"伏");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -