pic操作lcd 模拟spi 9非标准spi 9位数据位

#include"lcd.h"
#include
#include
#include
#include




//#include"config.h"


#define Delay_1us() asm("nop")
#define Delay_10us() asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop")
void Delay_100us()   {  
    Delay_10us(); //1 


Delay_10us(); //2  
Delay_10us(); //3 
Delay_10us(); //4  
Delay_10us(); //5   
Delay_10us(); //6   
Delay_10us(); //7  
Delay_10us(); //8  
Delay_10us(); //9  
Delay_1us(); //6us  
Delay_1us();  
Delay_1us();  
Delay_1us();  
Delay_1us(); 
Delay_1us(); 
  } 


void Delay_ms(unsigned int ms)
{  unsigned int j;
for(j=ms;j>0;j--)  

  Delay_100us(); //1  
  Delay_100us(); //2  
  Delay_100us(); //3  
  Delay_100us(); //4  
  Delay_100us(); //5  
  Delay_100us(); //6  
  Delay_100us(); //7  
  Delay_100us(); //8  
  
  Delay_100us(); //9  
  Delay_10us(); //86us 
  Delay_10us();  
  Delay_10us();  
  Delay_10us();  
  Delay_10us();  


  Delay_10us();   
  Delay_10us();  
  Delay_10us();  
  Delay_1us();   
  Delay_1us();  
  Delay_1us();  
  Delay_1us();  
  Delay_1us();  
  Delay_1us();  
} }


void TimeDelay(unsigned int Timers)
{
unsigned int i;
while(Timers)
{
Timers--;
for(i=0;i<10;i++);
}
}


void SPI_SSSet(unsigned char Status)
{
if(Status)
        {SPI_CS = 1;
      Delay_ms(10);
        // Delay_100us(1);
        }
else  
        {SPI_CS = 0;
   Delay_ms(10);
          //Delay_100us(1);
}
}
void LCD_Init(void)
{
init_spi();
SPI_SCK = 1;


SPI_CS = 1;

Delay_ms(10);
SPI_SCK = 0;
Delay_ms(10);
SPI_SCK = 1;
Delay_ms(10);

}
void init_spi()
{
    
     TRISBbits.TRISB0=0;
     TRISBbits.TRISB1=0;
     TRISBbits.TRISB2=0;  //模拟spi端口
     TRISDbits.TRISD3=0;
   
      
//     TRISDbits.TRISD0=1;//按键
//     TRISEbits.TRISE7=1;
//     TRISEbits.TRISE6=1;
//     TRISEbits.TRISE5=1;
//     TRISEbits.TRISE4=1;//该5口无模拟输入功能
     
     
}


void PutChar(unsigned int x,unsigned int y,unsigned char a,unsigned char type) 
{

SPI_SSSet(0);
SPI_Send(ASC_DISPLAY&CMD);
SPI_Send((x>>8)|DAT);
SPI_Send(x|DAT);
SPI_Send((y>>8)|DAT);
SPI_Send(y|DAT);
SPI_Send(type|DAT);
SPI_Send(a|DAT);
       
SPI_SSSet(1);
}


//void PutChars(unsigned int x,unsigned int y,unsigned int a,unsigned char type) 
//{
//
// SPI_SSSet(0);
// SPI_Send(ASC_DISPLAY&CMD);
// SPI_Send((x>>8)|DAT);
// SPI_Send(x|DAT);
// SPI_Send((y>>8)|DAT);
// SPI_Send(y|DAT);
// SPI_Send(type|DAT);
//  SPI_Send((a>>8)|DAT);
// SPI_Send(a|DAT);
// SPI_SSSet(1);
//}
void ShowString(unsigned int x,unsigned int y,unsigned char *p,unsigned char type)
{
    
    unsigned char length=0;
    unsigned char i=0;
    length =strlen(p);
    for (length;length>0;length--)
    {
       // i++;
        SPI_SSSet(0);
SPI_Send(ASC_DISPLAY&CMD);
        switch(type)
        {   case LCD_ASC8_FONT:SPI_Send(((x+(8*i))>>8)|DAT);
                SPI_Send((x+(8*i))|DAT);   break;
            case LCD_ASC12_FONT:SPI_Send(((x+(12*i))>>8)|DAT);
                SPI_Send((x+(12*i))|DAT);   break;
            case LCD_ASC16_FONT:SPI_Send(((x+(16*i))>>8)|DAT);
                SPI_Send((x+(16*i))|DAT);   break;
        
        }
SPI_Send((y>>8)|DAT);
SPI_Send(y|DAT);
SPI_Send(type|DAT);
SPI_Send((p[i])|DAT);
SPI_SSSet(1);
        i++;
    
    
    }


    
    


}


//void PutString(unsigned int x,unsigned int y,unsigned char *p,unsigned char type)
//{
//
// SPI_SSSet(0);
// SPI_Send(ASC_STR_DISPLAY&CMD);
// SPI_Send((x>>8)|DAT);
// SPI_Send(x|DAT);
// SPI_Send((y>>8)|DAT);
// SPI_Send(y|DAT);
// SPI_Send(type|DAT);
//
//        while(*p!=0)
// {
// SPI_Send((*p)|DAT);
// p++;
// }
//        
//        
//
// SPI_SSSet(1);
//}




void PutChar_cn(unsigned int x,unsigned int y,unsigned char * GB,unsigned char type) 
{
//??????×?×???
SPI_SSSet(0);
SPI_Send(HZ_DISPLAY&CMD);
SPI_Send((x>>8)|DAT);
SPI_Send(x|DAT);
SPI_Send((y>>8)|DAT);
SPI_Send(y|DAT);
SPI_Send(type|DAT);
SPI_Send((*(GB++))|DAT);
SPI_Send((*GB)|DAT);
SPI_SSSet(1);
}


//void PutPixel(unsigned int x,unsigned int y)
//{
// //??????×÷
//// SPI_SSSet(0); //SS????????
//// SPI_Send(DRAW_POINT&CMD); //??????DRAW_POINT
//// SPI_Send((x>>8)|DAT); //????????????,???è??????X?á????
//// SPI_Send(x|DAT);
//// SPI_Send((y>>8)|DAT); //????Y?á????
//// SPI_Send(y|DAT);
//// SPI_SSSet(1); //?ê????×÷??SS??????
//}


//
//void Line(unsigned int s_x,unsigned int  s_y,unsigned int  e_x,unsigned int  e_y)
//{  
// //?????±??
//// SPI_SSSet(0); //SS????????
//// SPI_Send(DRAW_LINE&CMD); //??????DRAW_LINE
//// SPI_Send((s_x>>8)|DAT); //????X?á×?±ê
//// SPI_Send(s_x|DAT);
//// SPI_Send((s_y>>8)|DAT); //????Y?á×?±ê
//// SPI_Send(s_y|DAT);
//// SPI_Send((e_x>>8)|DAT); //????X?á×?±ê
//// SPI_Send(e_x|DAT);
//// SPI_Send((e_y>>8)|DAT); //????Y?á×?±ê
//// SPI_Send(e_y|DAT);
//// SPI_SSSet(1); //?ê????×÷??SS??????
//}
//void Circle(unsigned int x,unsigned int y,unsigned int r,unsigned char mode)
//{
//// SPI_SSSet(0); //SS????????
//// if(mode)
//// SPI_Send(DRAW_SOLID_CIRCLE&CMD); //??????DRAW_SOLID_CIRCLE
//// else
//// SPI_Send(DRAW_CIRCLE&CMD); //??????DRAW_CIRCLE
//// SPI_Send((x>>8)|DAT); //????X?á×?±ê
//// SPI_Send(x|DAT);
//// SPI_Send((y>>8)|DAT); //????Y?á×?±ê
//// SPI_Send(y|DAT);
//// SPI_Send((r>>8)|DAT); //??°???r
//// SPI_Send(r|DAT);
//// SPI_SSSet(1); //?ê????×÷??SS??????
//}


void ClrScreen(void)
{
//??????×÷
SPI_SSSet(0); //SS????????
SPI_Send(CLEAR_SCREEN&CMD); //??????CLEAR_SCREEN
        TimeDelay(2);
SPI_SSSet(1); //?ê????×÷??SS??????
}


//void SetBG_Color(unsigned int BGColor)
//{
//
// SPI_SSSet(0); //SS????????
// SPI_Send(SET_BG_COLOR&CMD); //??????SET_BG_COLOR
// SPI_Send((BGColor>>8)|DAT);  //±??°????16bit RGB565
// SPI_Send(BGColor|DAT);
// SPI_SSSet(1); //?ê????×÷??SS??????
//}




//void SetFG_Color(unsigned int FGColor)
//{
// SPI_SSSet(0); //SS????????
// SPI_Send(SET_FG_COLOR&CMD); //??????SET_FG_COLOR
// SPI_Send((FGColor>>8)|DAT); //?°?°????16bit RGB565
// SPI_Send(FGColor|DAT);
// SPI_SSSet(1); //?ê????×÷??SS??????
//}


//void PutBitmap(unsigned int x,unsigned int y,unsigned long p)
//{
//
// SPI_SSSet(0); //SS????????
// SPI_Send(ICON_DISPLAY&CMD); //??????ICON_DISPLAY
// SPI_Send((x>>8)|DAT); //????X?á×?±ê
// SPI_Send(x|DAT);
// SPI_Send((y>>8)|DAT); //????Y?á×?±ê
// SPI_Send(y|DAT);
// SPI_Send(((unsigned int)(p>>24))|DAT); //bmp ??????flash??????DWORD ?à??
// SPI_Send(((unsigned int)(p>>16))|DAT);
// SPI_Send(((unsigned int)(p>>8))|DAT);
// SPI_Send(((unsigned int)p)|DAT);
// SPI_SSSet(1); //?ê????×÷??SS??????
//}
//void delay( unsigned char z)
//{
//   unsigned int x,y;
//        for(x=z;x>0;x--)
//                for(y=880;y>0;y--);
//    
//
//}




void SetBackLight(unsigned char brightness) 
{

SPI_SSSet(0);
SPI_Send(SET_LCD_BRIGHTNESS&CMD);
Delay_1us();//2us
        SPI_Send(brightness|DAT);
        Delay_1us();
SPI_SSSet(1);
}


//
//void Box(unsigned int s_x,unsigned int  s_y,unsigned int  e_x,unsigned int  e_y,unsigned char mode)
//{  
//// //?????±??
//// SPI_SSSet(0);
//// SPI_Send(DRAW_BOX&CMD);
//// SPI_Send((s_x>>8)|DAT); //????X?á×?±ê
//// SPI_Send(s_x|DAT);
//// SPI_Send((s_y>>8)|DAT); //????Y?á×?±ê
//// SPI_Send(s_y|DAT);
//// SPI_Send((e_x>>8)|DAT); //????X?á×?±ê
//// SPI_Send(e_x|DAT);
//// SPI_Send((e_y>>8)|DAT); //????Y?á×?±ê
//// SPI_Send(e_y|DAT);
//// SPI_Send(mode|DAT);
//// SPI_SSSet(1);
//}




//void Button(unsigned int s_x,unsigned int  s_y,unsigned int  e_x,unsigned int  e_y,unsigned char mode)
//{  
// //?????±??
//// SPI_SSSet(0); //SS????????
//// SPI_Send(DRAW_BUTTON&CMD); //??????DRAW_LINE
//// SPI_Send((s_x>>8)|DAT); //????X?á×?±ê
//// SPI_Send(s_x|DAT);
//// SPI_Send((s_y>>8)|DAT); //????Y?á×?±ê
//// SPI_Send(s_y|DAT);
//// SPI_Send((e_x>>8)|DAT); //????X?á×?±ê
//// SPI_Send(e_x|DAT);
//// SPI_Send((e_y>>8)|DAT); //????Y?á×?±ê
//// SPI_Send(e_y|DAT);
//// SPI_Send(mode|DAT);
//// SPI_SSSet(1);
//}




//void EditASC(unsigned int s_x,unsigned int  s_y,unsigned int  e_x,unsigned int  e_y,unsigned char *p,unsigned char type)
//{  
// unsigned char j;
// SPI_SSSet(0); //SS????????
// SPI_Send(DRAW_EDIT_ASC&CMD); //??????DRAW_LINE
// SPI_Send((s_x>>8)|DAT); //????X?á×?±ê
// SPI_Send(s_x|DAT);
// SPI_Send((s_y>>8)|DAT); //????Y?á×?±ê
// SPI_Send(s_y|DAT);
// SPI_Send((e_x>>8)|DAT); //????X?á×?±ê
// SPI_Send(e_x|DAT);
// SPI_Send((e_y>>8)|DAT); //????Y?á×?±ê
// SPI_Send(e_y|DAT);
// SPI_Send(type|DAT);
// //while(*p!=0)
// for(j=0;j<(type>>4);j++)
// {
// SPI_Send((*p)|DAT); //????????×?????????
// p++;
// }
// SPI_SSSet(1); //?ê????×÷??SS??????
//}


//========================================================================
// ????: void Line(unsigned int s_x,unsigned int  s_y,unsigned int  e_x,unsigned int  e_y)
// ?è??: ??s_x??s_y??????×?±ê??e_x??e_y???á??×?±ê?????????±??
//========================================================================
//void EditCN(unsigned int s_x,unsigned int  s_y,unsigned int  e_x,unsigned int  e_y,unsigned char *p,unsigned char type)
//{  
// unsigned char j;
// //?????±??
// SPI_SSSet(0); //SS????????
// SPI_Send(DRAW_EDIT_HZ&CMD); //??????DRAW_LINE
// SPI_Send((s_x>>8)|DAT); //????X?á×?±ê
// SPI_Send(s_x|DAT);
// SPI_Send((s_y>>8)|DAT); //????Y?á×?±ê
// SPI_Send(s_y|DAT);
// SPI_Send((e_x>>8)|DAT); //????X?á×?±ê
// SPI_Send(e_x|DAT);
// SPI_Send((e_y>>8)|DAT); //????Y?á×?±ê
// SPI_Send(e_y|DAT);
// SPI_Send(type|DAT);
// //while(*p!=0)
// for(j=0;j<(type>>4);j++)
// {
// SPI_Send((*(p++))|DAT);
// SPI_Send((*(p++))|DAT);
// }
// SPI_SSSet(1); //?ê????×÷??SS??????
//}






void SPI_Send(unsigned int dat)                 //数据传输函数(传送带操作)
{


    unsigned char i=0;
for(i=0;i<9;i++)
{
if(dat&0x100)
        {
SPI_SD0 = 1;
                Delay_1us();
            Delay_1us();
            Delay_1us();
        }
else 
{
            SPI_SD0 = 0;
                Delay_1us();
            Delay_1us();
            Delay_1us();
        }
SPI_SCK = 0;
             
                Delay_1us();
                Delay_1us();
                Delay_1us();
                Delay_1us();
                Delay_1us();
                Delay_1us();
                Delay_1us();
                Delay_1us();//----------------------------------多延时是为了50%的占空比
                Delay_1us();
                Delay_1us();
                Delay_1us();
                Delay_1us();
                Delay_1us();
                Delay_1us();
                Delay_1us();
                
                
SPI_SCK = 1;
                
Delay_1us();
dat = dat<<1;
         Delay_1us();
            Delay_1us();
            Delay_1us();


}
    
    
}
    

你可能感兴趣的:(单片机,pic,pic,模拟spi,9位数据位)