lunedì 13 giugno 2016

LCD1602 su PIC DevBoard

Codice, ripreso da un produttore cinese, su come usare LCD1602 con PIC. Compilatore MapLab IDE





Schema elettrico


main.c
---------------------------------------------
#include <pic.h>          
#include "LCD1602.h"         
//---------------------------------------
unsigned char str0[]={"Luca            "};
unsigned char str1[]={"Innocenti       "};
unsigned char str2[]={"debiaonoldcomput"};
unsigned char str3[]={"ers.blogspot.com"};
//---------------------------------------

void main(void)           
{

    LCD1602_GPIO_Init();
    LCD1602_init();      
  

    while(1)              
    {
    DisplayListChar(0,0,str0);   
    DisplayListChar(0,1,str1);
  
    Delay1602_MS(500);
 
    DisplayListChar(0,0,str2);   
    DisplayListChar(0,1,str3);
    Delay1602_MS(500);
    }
}

---------------------------------------------


 LCD1602.H
---------------------------------------------
#ifndef  __LCD1602_H
#define  __LCD1602_H


#define E   RA3           
#define RW  RA2        
#define RS  RA5         
#define busy RD7         
#define busy_init TRISD7  
#define Lcd_Date PORTD    


void Delay1602_US(unsigned int t);
void Delay1602_MS(unsigned int t);
void LCD1602_BusyTest(void);
void LCD1602_Write_Instruction(unsigned char combuf);
void LCD1602_Write_data_busy(unsigned char databuf);
void LCD1602_init(void);
void DisplayOneChar(unsigned char X,unsigned char Y,unsigned char DData);
void DisplayListChar(unsigned char X,unsigned char Y,unsigned char *DData);
void LCD1602_GPIO_Init(void);


#endif

---------------------------------------------


 1602.c
---------------------------------------------
#include <pic.h>        
#include "LCD1602.h"   
__CONFIG(HS&WDTDIS&LVPDIS);




void Delay1602_US(unsigned int t)
{
     unsigned int k;     
     for(k=0;k<t;k++);   
}

void Delay1602_MS(unsigned int t)
{
     while(t--)
     Delay1602_US(200);
}
void LCD1602_BusyTest(void)
{
     busy_init=1;          
     RS=0;               
     RW=1;              
     E=1;                
     asm("NOP");         
     asm("NOP");         
     while(busy==1);      
     E=0;                
     busy_init=0;         
}


void LCD1602_Write_Instruction(unsigned char combuf)
{
     LCD1602_BusyTest();         
     RS=0;              
     RW=0;               
     E=0; 
     asm("NOP");         
     Lcd_Date=combuf;      
     asm("NOP");         
     asm("NOP");         
     E=1;              
     asm("NOP");         
     E=0;                
}


void LCD1602_Write_data_busy(unsigned char databuf)
{
     LCD1602_BusyTest();  
     RS=1;              
     RW=0;               
      E=0; 
     asm("NOP");         
     Lcd_Date=databuf;       
     asm("NOP");         
     asm("NOP");         
     E=1;                
     asm("NOP");        
     E=0;                
}



void LCD1602_init(void)
{
     Delay1602_US(1500);         
     LCD1602_Write_Instruction(0x38);
     Delay1602_US(500);         
     LCD1602_Write_Instruction(0x38);
     Delay1602_US(500);          
     LCD1602_Write_Instruction(0x38); 
     LCD1602_Write_Instruction(0x38); 
     LCD1602_Write_Instruction(0x08); 
     LCD1602_Write_Instruction(0x01); 
     LCD1602_Write_Instruction(0x06); 
     LCD1602_Write_Instruction(0x0C);
}



void DisplayOneChar(unsigned char X,unsigned char Y,unsigned char DData)
{
    Y&=1;
    X&=15;
    if(Y)X|=0x40;             
    X|=0x80;                  
    LCD1602_Write_Instruction(X);
    LCD1602_Write_data_busy(DData);
}

void DisplayListChar(unsigned char X,unsigned char Y,unsigned char *DData)
{
    unsigned char ListLength=0;
    Y&=0x01;
    X&=0x0f;
    while(X<16)
    {
        DisplayOneChar(X,Y,DData[ListLength]);
        ListLength++;
        X++;
    }
}


void LCD1602_GPIO_Init(void)
{
    ADCON1=0X07;
    TRISA=0B11010011;    
    TRISD=0B00000000;    
    PORTA=0B00000000;    
    PORTD=0B00000000;    
}

---------------------------------------------