File "_LCD.c.bak"

Full Path: /home/analogde/www/private/Projet/_LCD.c.bak
File size: 5.27 KB
MIME-type: text/x-c
Charset: 8 bit



#include "68HC12_registres.h"


// Definitions des constantes
#define WR          0x20     // lcd WR bit
#define RS          0x40     // lcd RS bit
#define EN          0x80     // lcd EN bit


// Prototypes
void initialise_LCD();
void envoi_chaine(char *sptr);
void envoi_caractere(unsigned char datval);

// RAM Variables
unsigned char Buffer_LCD;	
unsigned char Etat_LCD;		

#define LCD_DELAYTIME   0x200   // ajuster  cette valeur en fonction du temps de rponse et des performances de l'afficheur mise en oeuvre

/*********************************************************************************/
void delai_secondaire(unsigned int ucount)
{
    while(ucount > 0)
         { --ucount;
         }
}
/*********************************************************************************/
void attendre(unsigned int mcount)
{
    while(mcount > 0)
         { --mcount;
           delai_secondaire(LCD_DELAYTIME);
         }
}
/*********************************************************************************/
void transfert_LCD()
{
    attendre(1);
    Etat_LCD = SPI0SR;   	// lecture du registre d'tat = reset
    SP0DR = Buffer_LCD; 	// transmettre l'octet

    do{ // attendre que le flag du registre d'tat passe  1
        Etat_LCD = SPI0SR;
    }while(Etat_LCD < 0x80);

    Etat_LCD = SP0DR;    // receive value back
}
/*********************************************************************************/
void ecrire_4bits_LCD(char LCDdata){
    
    // merge les 4 bits de poids faible de LCDdata avec les 4 bits de poids fort contenu dans le Buffer_LCD (ce sont les bits de controles)
    
    
    Buffer_LCD &= 0xF0;
    Buffer_LCD |= LCDdata;

    Buffer_LCD &= ~EN;  	// active les faibles
    transfert_LCD();  		// envoie les donnes
    Buffer_LCD |= EN;  		// active les forts
    transfert_LCD();  		// envoie les donnes
    Buffer_LCD &= ~EN;  	// active les faibles
    transfert_LCD();		// envoie les donnes
}
/*********************************************************************************/
void ecrire_octet_LCD(unsigned char lcdval)
{
    ecrire_4bits_LCD(lcdval >> 4);      // envoi les 4 bits de poids forts
    ecrire_4bits_LCD(lcdval & 0x0F);    // puis  les 4 bits de poids faible
}
/*********************************************************************************/
void commande_LCD(unsigned char cmdval)
{
    Buffer_LCD &= ~RS; 			// efface RS (mise  0) afin de selectionner le mode  LCD Command 
    ecrire_octet_LCD(cmdval);
    attendre(10);
}
/*********************************************************************************/

void envoi_caractere(unsigned char datval)
{
    Buffer_LCD |= RS; 		// mise  1 de RS pour selectionner le mode LCD Data 
    ecrire_octet_LCD(datval); 		// crire un octet de donne 
}
/*********************************************************************************/
void envoi_chaine(char *sptr)
{
	while(*sptr){
        envoi_caractere(*sptr);
		++sptr;
	}
}
/*********************************************************************************/
void initialise_SPI()
{
SPI0CR1 = 0x52; // parametre  la liaison SPI	
    
   		    /* 0 1 0 1 0 0 1 0                          
                       | | | | | | | |
                       | | | | | | | \____ MSB first
                       | | | | | | \______ multiple bytes with SS asserted
                       | | | | | \________ 0 phase
                       | | | | \__________ 0 polarity
                       | | | \____________ Master mode
                       | | \______________ not open drain
                       | \________________ Enable SPI
                       \__________________ No interrupts      
                   */
    
    
    SPI0CR2 = 0x10; // permettre le mode /SS  - selectionne l'esclave 
    SPI0BR = 0x00;  // vitesse de transmission (baud clock rate) = 4 MHz 


}

/*********************************************************************************/
void initialise_LCD()
{    
    Buffer_LCD = (WR + EN);	// set WR and EN bits
    transfert_LCD();  		// tranmettre

    // initialise l'afficheur 
    Buffer_LCD &= ~(RS + EN);	// clear RS and EN bits (select lcd Command)
    transfert_LCD();		// transmettre

   
    attendre(50);

    // set to 4 bit wide mode
    ecrire_4bits_LCD(3);   // send 3
    attendre(50);
    ecrire_4bits_LCD(3);   // send 3
    attendre(50);
    ecrire_4bits_LCD(3);   // send 3
    attendre(50);
    ecrire_4bits_LCD(2);   // send 2
    attendre(50);

    commande_LCD(0x2c);  // 2x40 display
    commande_LCD(0x06);  // display and cursor on
    commande_LCD(0x0e);  // shift cursor right
    commande_LCD(0x01);  // clear display and home cursor
    commande_LCD(0x80);

    Buffer_LCD = 0; // Reset Lcd states to rest
    transfert_LCD();  // send status to LCD
}
/**************************************************************************************/
void main()
{
    char keyval;

	initialise_LCD();
	initialise_SPI()	

	envoi_chaine("Hello  1234567890_AB");    // ligne 1
	envoi_chaine("Line 3 1234567890_AB");    // ligne 3
	envoi_chaine("Line 2 1234567890_AB");    // ligne 2
	envoi_chaine("Line 4 1234567890_AB");    // ligne 4
}
/**************************************************************************************/