首 页文档资料下载资料维修视频包年699元
请登录  |  免费注册
当前位置:精通维修下载 > 文档资料 > 家电技术 > 单元电路介绍 > 其它电路
c8051f005键盘测试程序
来源:本站整理  作者:佚名  2011-08-04 10:14:02



c8051f005键盘测试程序

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f000.h>
#include <stdio.h>
#include <intrins.h>
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
#define  uchar unsigned char
#define  uint  unsigned int
#define BAUDRATE             115200              // Baud rate of UART in bps
#define SYSCLK               11059200           // SYSCLK Frequency in Hz
void SYSCLK_Init (void);
void delaynus(unsigned int q) ;                 //N us延时函数
void PORT_Init (void);
void SPI0_Init (void);
void LCD_Init(void);
void SenDSPIByte(unsigned char ch);
void delaynms (unsigned int j);
void writecom(unsigned char com);
void writedata(unsigned char d);
void writechar(unsigned char ua);
void Write_COM(uchar ins);
void lcden(datad);
void LCD_set_xy( unsigned char x, unsigned char y );
void LCD_write_string(unsigned char n);
void lcd_key1(void);
void lcd_key2(void);
void lcd_key3(void);
void lcd_key4(void);
void UART0_Init (void);
void presskey(void);
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
sbit S3=P1^0;
sbit S4=P1^1;
sbit S5=P1^2;
sbit S6=P1^3;
sbit lcdcs=P3^0;
unsigned char datad ,comd,kk,sdf,ppca;
unsigned char virt_port,v,b,m;
unsigned char lcd_data_count;
unsigned char *lcdpoint;
unsigned char   qqq; 
unsigned char  data8;
unsigned int   i;
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main(void){

        WDTCN = 0xde;                       // dISAble watchdog timer
        WDTCN = 0xad;
        SYSCLK_Init ();
        PORT_Init ();
        UART0_Init() ;                      // initialize crossbar and GPIO
        SPI0_Init ();
        LCD_Init() ;

        delaynms (20);
        delaynus (100);
        LCD_set_xy(0X00,0);
        delaynus (200);
        presskey();
        delaynus (200);
        LCD_write_string(16);
        delaynus (200);
        while(1)
        {
        if(S3==0)
        {
        Write_COM(0X01);
        delaynms (200);
        LCD_set_xy(0X00,0);
        delaynus (200);
        lcd_key1();
        delaynus (200);
        LCD_write_string(6);
        delaynus (200);
        }
        else if(S4==0)
        {
        Write_COM(0X01);
        delaynms (200);
        LCD_set_xy(0X00,0);
        delaynus (200);
        lcd_key2();
        delaynus (200);
        LCD_write_string(6);
        delaynus (200);
        }
        else if(S5==0)
        {
        Write_COM(0X01);
        delaynms (200);
        LCD_set_xy(0X00,0);
        delaynus (200);
        lcd_key3();
        delaynus (200);
        LCD_write_string(6);
        delaynus (200);
        }
        else if(S6==0)
        {
        Write_COM(0X01);
        delaynms (200);
        LCD_set_xy(0X00,0);
        delaynus (200);
        lcd_key4();
        delaynus (200);
        LCD_write_string(6);
        delaynus (200);
        }
        else
        {
        }
        }

}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
void PORT_Init (void)
{

        XBR0 = 0x27;                        // XBAR0: Initial Reset Value
        XBR1 = 0x00;                        // XBAR1: Initial Reset Value
        XBR2 = 0x5c;                        // XBAR2: Initial Reset Value
        PRT0CF = 0x14;                      // Output configuration for P0 
          PRT1CF = 0x10; // Output configuration for P3 
           PRT3CF = 0x01; // Output configuration for P3 

}
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
void SYSCLK_Init (void)
{
                                  
      OSCXCN = 0x67;                      // start external oscillator with
      for (i=0; i < 256; i++) ;           // XTLVLD blanking interval (>1ms)
      while (!(OSCXCN & 0x80)) ;          // Wait for Crystal osc. to settle
      OSCICN = 0x88;                      // select external oscillator as SYSCLK                              
}
//-----------------------------------------------------------------------------
// SPI0_Init
//-----------------------------------------------------------------------------
void SPI0_Init (void)
{
      SPI0CFG = 0x07;                        // data sampLED on 1st SCK rising edge
      SPI0CFG|=0xC0;                         //CKPOL =1;
      SPI0CN = 0x03;                         // Master mode; SPI enabled; flags
      SPI0CKR = SYSCLK/2/2000000-1;           // SPI cLOCk <= 8MHz (limited by 
                                              // EEPROM spec.)
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
void UART0_Init (void)
{
   SCON    = 0x50;                     // SCON: mode 1, 8-bit UART, enable RX
   TMOD    = 0x20;                     // TMOD: timer 1, mode 2, 8-bit reload
   TH1    = -(SYSCLK/BAUDRATE/16);     // set Timer1 reload value for baudrate
   TR1    = 1;                         // start Timer1
   CKCON |= 0x10;                      // Timer1 uses SYSCLK as time base
   PCON  |= 0x80;                      // SMOD = 1
   TI     = 1;                         // InDICate TX ready
}
//-----------------------------------------------------------------------------
// LCD_Init
//-----------------------------------------------------------------------------
void LCD_Init(void) //向LCD送命令

{

//      unsigned int xdata   x;
        delaynms(100);
        datad=0x00;
        SendSPIByte(datad);
        delaynms(10);
        Write_COM(0x30);               
        delaynms(10);     
        Write_COM(0x30);               
        delaynms(10);
        Write_COM(0x30);               
        delaynms(10) ; 
        Write_COM(0x28);           
        delaynms(100);
        virt_port=0;
        SendSPIByte(virt_port);
        lcden(virt_port);
        Write_COM(0x01); 
        delaynms(100);          
        Write_COM(0x06); 
        delaynms(10) ;           
        Write_COM(0x0C); 
        delaynms(500) ;           



}
//-----------------------------------------------------------------------------
// SendSPIByte
//-----------------------------------------------------------------------------
void SendSPIByte(unsigned char ch)
{       lcdcs=1;
        delaynus(100);
         SPIF = 0;
        SPI0DAT = ch;
        while (SPIF == 0);
        delaynus(100);
        lcdcs=0;
        delaynus(100);    
        _nop_();                 // 等待写结束                       
}
//-----------------------------------------------------------------------------
// lcden
//-----------------------------------------------------------------------------
void lcden(datad)
{
          datad|=0x08;            
          SendSPIByte(datad);
          datad&=0xf7;
          SendSPIByte(datad);
}
//-----------------------------------------------------------------------------
// delaynms
//-----------------------------------------------------------------------------
void delaynms (unsigned int uu)
{
   unsigned int oo,ll;
   for (oo=0;oo<uu;oo++)
   {
    for(ll=0;ll<1140;ll++);
    
   }
}
//-----------------------------------------------------------------------------
// writechar
//-----------------------------------------------------------------------------
void writechar(unsigned char ua)
{       
        
        uint  j;
        uchar t,x;
        for(j=0;j<500;j++);
        datad|=0x02;
        SendSPIByte(datad);
        datad|=ua&0xf0;
        SendSPIByte(datad);
        datad|=0x08;            
        SendSPIByte(datad);
        for(x=0;x<3;x++);
        datad&=0xf7;
        SendSPIByte(datad);
        for(x=0;x<3;x++);
        datad&=0x07;
        delaynus(100);                
        SendSPIByte(virt_port);

        t|=ua&0x0f;
        datad|=t<<4;
        SendSPIByte(datad);
        for(x=0;x<3;x++);
        datad|=0x08;            
        SendSPIByte(datad);
        for(x=0;x<3;x++);
        datad&=0xf7;
        SendSPIByte(datad);
        for(x=0;x<3;x++);
       datad=0x00;
        t=0x00;
       SendSPIByte(datad);
}
//-----------------------------------------------------------------------------
// Write_COM
//-----------------------------------------------------------------------------
void Write_COM(uchar ins)
     {
      uchar  t;
      uint j;
     
      for(j=0;j<5000;j++);                    //用延时代替查询
      virt_port|=ins&0xf0;
      SendSPIByte(virt_port);
      //LCDE=1;
      virt_port|=0x08;
      SendSPIByte(virt_port);
      for(i=3;i>0;i--);
      //LCDE=0;
      virt_port&=~0x08;
      SendSPIByte(virt_port);

      virt_port&=0x07;
      SendSPIByte(virt_port);
      t=ins<<4;             
      virt_port|=t&0xf0;           
      SendSPIByte(virt_port);
      //LCDE=1;
      virt_port|=0x08;
      SendSPIByte(virt_port);
      for(i=3;i>0;i--);
      //LCDE=0;
      virt_port&=~0x08;
      SendSPIByte(virt_port);
      virt_port=0;
      SendSPIByte(virt_port);
}
//-----------------------------------------------------------------------------
// LCD_set_xy
//-----------------------------------------------------------------------------
void LCD_set_xy( unsigned char x, unsigned char y )
{
    unsigned char address;
    if (y == 0) address = 0x80 + x;
    else 
    address = 0xc0 + x;
    Write_COM(address);
}
//-----------------------------------------------------------------------------
// LCD_write_string
//-----------------------------------------------------------------------------
void LCD_write_string(unsigned char n)
{  

    unsigned char data1;
    for(n;n>0;n--)
    {

    data1=*lcdpoint;
    writechar(data1);
      delaynms(100);

    lcdpoint++;
    delaynus(10);
    } 

}
//-----------------------------------------------------------------------------
// presskey
//-----------------------------------------------------------------------------
void presskey(void)
{
     unsigned char xdata DDCdata[16]={0x50,0x4c,0x45,0x41,0x53,0x45,0x20,0x50,0x52,0x45,0x53,0x53,0x20,0x4b,0x45,0x59};
     lcdpoint=&DDCdata;
}
//-----------------------------------------------------------------------------
// lcd_key1
//-----------------------------------------------------------------------------
void lcd_key1(void)
{   
    unsigned char xdata key1ok[6]={0x53,0x33,0x20,0x4f,0x4b,0x21};
    lcdpoint=&key1ok;
}
//-----------------------------------------------------------------------------
// lcd_key2
//-----------------------------------------------------------------------------
void lcd_key2(void)
{   
    unsigned char xdata key2ok[6]={0x53,0x34,0x20,0x4f,0x4b,0x21};
    lcdpoint=&key2ok;
}
//-----------------------------------------------------------------------------
// lcd_key3
//-----------------------------------------------------------------------------
void lcd_key3(void)
{   
    unsigned char xdata key3ok[6]={0x53,0x35,0x20,0x4f,0x4b,0x21};
    lcdpoint=&key3ok;
}
//-----------------------------------------------------------------------------
// lcd_key4
//-----------------------------------------------------------------------------
void lcd_key4(void)
{
    unsigned char xdata key4ok[6]={0x53,0x36,0x20,0x4f,0x4b,0x21};
    lcdpoint=&key4ok;
}
//-----------------------------------------------------------------------------
// delaynus
//-----------------------------------------------------------------------------

void delaynus(unsigned int q)       //N us延时函数
  {
   for (i=0;i<q;i++)
      {
      _nop_();
      }
  } 
  

关键词:

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分)
Copyright © 2007-2017 down.gzweix.Com. All Rights Reserved .
页面执行时间:16,875.00000 毫秒