c8051F040 MCU driver 74HC595 program

Publisher:SparklingMoonLatest update time:2018-01-06 Source: eefocusKeywords:c8051F040 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include "C8051F040.h" 
#define uchar unsigned char 
#define uint unsigned int 

uchar xdata virt_port; 
bit SPI_BUSY; 
unsigned char xdata tab1[16]={0x20,0x20,0x42,0x54,0x20,0x46,0x30,0x34,0x30,0x20,0x56,0x31,0x2e,0x30 


                            ,0x20,0x20}; 
unsigned char xdata Netdata[16]={0x77,0x77,0x77,0x2e,0x78,0x68,0x6c,0x2e,0x63,0x6f, 
                                0x6d,0x2e,0x63,0x6e,0x20,0x20};  
sbit LCDCs=P2^5; 
void os_init(); 
void port_init(); 
void spi_init(); 
void writecode(uchar ins); 
void writEDAta(uchar d); 
void lcdchar (void); 
void lcdinit(); 
void writelcd(uchar spi_data); 
void main() 
{   
    WDTCN = 0xDE;                                           // 关闭看门狗 
    WDTCN = 0xAD; 
    os_init(); 
    port_init(); 
    spi_init(); 
      SFRPAGE   = CONFIG_PAGE; 
     EIE1      = 0x01; 
     EA=1; 
   while(1) 
   { 
  
    
   lcdinit(); 
   lcdchar(); 
   } 


void os_init() 

    int i = 0; 
    SFRPAGE   = CONFIG_PAGE; 
    OSCXCN    = 0x67;                                    //外部22.1184M晶振 
    for (i = 0; i < 3000; i++);                         //等待外部晶振稳定  
    while ((OSCXCN & 0x80) == 0); 
    CLKSEL    = 0x01;                                    //系统时钟不分频 

void port_init() 

    SFRPAGE   = CONFIG_PAGE; 
       P0MDOUT=0xff; 
    XBR0      = 0x06; 
    XBR2      = 0x40; 


void spi_init() 

      SFRPAGE   = SPI0_PAGE; 
      SPI0CFG   = 0x40; 
      SPI0CN    = 0x01; 
      SPI0CKR   = 0x6D; 






 void delay1us(void) 
    { 
    uchar xdata i; 
    for(i=10;i>0;i--); 
    }  

void writelcd(uchar spi_data)       //向LV595写数据以SPI方式 

  uint xdata i,j,k; 
     for(i=0;i<9;i++); 
    LcdCs=0; 
    delay1us();     
    SPI_BUSY=1; 
    SFRPAGE   = SPI0_PAGE; 
    SPI0DAT=spi_data; 
    for(j=0;j<500;j++) 
        { 
       if(!SPI_BUSY)break; 
        }  
    delay1us(); 
     
    LcdCs=1; 
    delay1us(); 
    delay1us(); 
    delay1us(); 
    delay1us(); 
    delay1us(); 
    delay1us(); 
    for(k=0;k<5000;k++); 
     
/* ************************************************************************** 
 // 以下这段代码,是用IO口模拟SPI传输的程序 
   LcdCs=0; 
  for(i=0;i<8;i++) 
  { SPICLK=0; 
    if(spi_data&0x80) 
       spi_mosi=1; 
    else  
       spi_mosi=0; 
    spi_data<<=1; 
    SPICLK=1; 
     for(j=0;j<10;j++) 
     {_nop_();} 
    SPICLK=0; 
   } 
   LcdCs=1; 
/***************************************************************************/ 

void writecode(uchar ins) //Write command to LCD control module 
     { 
      uchar xdata t; 
      uint xdata j; 
     
      for(j=0;j<500;j++);     
      virt_port|=ins&0xf0; //Get high byte and write data to 1602  
      writelcd(virt_port); //Write high 4 bits first and then low 4 bits. This is when selecting four data lines 
      
      virt_port|=0x08;                           
      writelcd(virt_port); //Enable E pin as high level 
      virt_port&=~0x08;                     
      writelcd(virt_port); //Enable E pin as low level 

      t=ins<<4;              
      virt_port|=t&0xf0; //Get low byte and write data to 1602 
      writelcd(virt_port); 
      virt_port|=0x08; //Enable E pin as high level 
      writelcd(virt_port); 
      virt_port&=~0x08; //Enable E pin as low level 
      writelcd(virt_port); 
      virt_port=0; 
      writelcd(virt_port); 
     } 

void writedata(uchar d) //Write data to 1602 
     { 
      uchar xdata i,t; 
      uint xdata j; 
       
      for(j=0;j<500;j++);         
      virt_port|=0x02; //Cursor return instruction 
      writelcd(virt_port); 
      virt_port|=d&0xf0; //Get high byte data to data register 
      writelcd(virt_port); 
      virt_port|=0x08; //Enable E pin as high level 
      writelcd(virt_port); 
      virt_port&=0xf7; //Enable E pin as low level 
      writelcd(virt_port); 
      virt_port&=0x07; //Set cursor to right, text also moves right 
      writelcd(virt_port); 
            
      t=d&0x0f; 
      virt_port|=t<<4; //Get low byte data to data register 
      writelcd(virt_port); 

      virt_port|=0x08; //Enable E pin as high level 
      writelcd(virt_port); 
      for(i=3;i>0;i--);  
      virt_port&=0xf7; //Enable E pin as low level 
      writelcd(virt_port); 
      virt_port=0; //Data clear 
      writelcd(virt_port); 
    } 

 void lcdchar (void) 
     { 
     uint xdata i,j,k; 
     for(j=0;j<5000;j++); //data read and write operations, the screen does not move 
     for(k=0;k<5000;k++); 
   
     for(i=0;i<16;i++) 
     { 
       writedata(tab1[i]); 
       for(j=0;j<1000;j++); 
       for(k=0;k<1000;k++); 
       for(k=0;k<5000;k++); 
       for(k=0;k<1000;k++); 
       for(k=0;k<5000;k++); 
       for(k=0;k<5000;k++); 
       for(k=0;k<5000;k++); 
      
      } 
                                         
   
      writecode(0xb0);                       // 换行                    
      for(j=0;j<5000;j++);                                         
      for(k=0;k<5000;k++); 
  
     for(i=0;i<16;i++) 
     { 
       writedata(Netdata[i]); 
       for(j=0;j<5000;j++)    delay1us(); 
       for(k=0;k<5000;k++); 
       for(k=0;k<5000;k++); 
       for(k=0;k<5000;k++); 
       for(k=0;k<5000;k++); 
        
                
      }; 
    
     } 


void lcdinit() 

     uint xdata i; 
     
      virt_port=0x00; 
      writelcd(virt_port);         //RS脚为零写指令寄存器     RW为低电平 
      virt_port|=0x08;              //使能脚E=1 
      writelcd(virt_port); 
       virt_port&=0xf7;             //使E=0使能脚发生跳变高--低 
      writelcd(virt_port); 
      for(i=1000;i>0;i--);         //3次延时,重要! 
      for(i=8000;i>0;i--);         //用延时代替查询       

     
      virt_port=0;                 //写入20码,设置为4位总线方式,非常重要!!!!! 
      writelcd(virt_port); 
      virt_port|=0x20;             //只写高字节 
      writelcd(virt_port); 
      virt_port|=0x08;           //E=1 
      writelcd(virt_port); 
      for(i=20;i>0;i--);         //使命令执行 
      virt_port&=0xf7;           //E=0 
      writelcd(virt_port); 
      for(i=10;i>0;i--); 
      virt_port=0; 
      writelcd(virt_port); 
      
      

      writecode(0x38);            //设两行显示,4线方式5*10字符显示 
      for(i=50;i>0;i--); 
        writecode(0x06);            //光标右移 
      for(i=50;i>0;i--); 
      
       
      writecode(0x0c);            //开显示指令 
      for(i=50;i>0;i--); 
       
      
      writecode(0x02);            //光标返回指令 
      for(i=50;i>0;i--); 
        
     
      
      writecode(0x01);            //清屏指令 
      for(i=50;i>0;i--); 


void spi_srs  () interrupt 6 

    if(SPIF)         
        { 
      SFRPAGE   = SPI0_PAGE; 
      SPIF=0; 

        SPI_BUSY=0; 
        } 
         

    if(WCOL) 
        { 
       SFRPAGE   = SPI0_PAGE; 
        WCOL=0; 
        } 

}


Keywords:c8051F040 Reference address:c8051F040 MCU driver 74HC595 program

Previous article:89C2051 driving stepper motor circuit and source code
Next article:C8051F005 MCU PWM test program

Recommended ReadingLatest update time:2024-11-23 15:32

STM32 controls 74HC595 to drive dot matrix text to move
I encountered a very strange problem. The program was obviously correct but it just didn't display. Finally, I moved the variable count defined in the main function to outside the main function and it worked. After simulation, I found that the initial value of the local variable defined in the main function was not 0,
[Microcontroller]
Design of intelligent node of vehicle CAN bus based on C8051F040
The electrical and electronic system is an important part of the vehicle, and its working state directly affects the performance of the vehicle. When designing the vehicle electrical system according to the traditional design concept, various subsystems are often piled up to improve the performance of the system. Th
[Microcontroller]
Design of intelligent node of vehicle CAN bus based on C8051F040
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号