Projet

Général

Profil

Feature #2809 » lcd_4b.c

Anonyme, 26/06/2013 14:08

 


/** INCLUDES *******************************************************/
#include <p18cxxx.h>
#include "bootloader.h"


#define RS LATDbits.LATD0
#define R_W LATDbits.LATD1
#define E LATDbits.LATD2
#define DATA LATD


/** V A R I A B L E S ********************************************************/
#pragma udata


#pragma interrupt YourHighPriorityISRCode
void YourHighPriorityISRCode(){}
#pragma interruptlow YourLowPriorityISRCode
void YourLowPriorityISRCode(){}

// Prototypes des fonctions de gestion du LCD en mode 4 bits

void init_port_lcd (void);
void lcd_car (unsigned char car);
void lcd_com (unsigned char commande);
void lcd_com_init (unsigned char commande);
void lcd_init (void);
void lcd_str (unsigned char *str);
unsigned int readAN0(void);
void init_can(void);


// Prototypes des fonctions de gestion du timer

void tpo_ms (unsigned short tmr_val);
void tpo_us (unsigned short tmr_val);

void main (void)
{
unsigned long int i;
char tm=0,tc=0,td=0,tu=0;
char message[15]="temperature";
unsigned int num,temp;
TRISA=255; //Port A en entr?es
// TRISD=0; //Port C en sortie

init_can(); //Initialisation du can

// Port C is an ouput port
TRISC = 0;

init_port_lcd();
lcd_init();

lcd_str(message);
/* lcd_com(0xC4);
lcd_car('=');
lcd_car(' ');
lcd_car(' ');
lcd_car(0xdf);
lcd_car('c');*/
// TRISA=255; //Port A en entr?es
// TRISD=0; //Port C en sortie



lcd_com(0xC4);

temp = readAN0(); // resultat ds num
// temp = num; // relation pour la temperature

if(temp<10)
{
tu=temp;
}
else if(temp<100)
{
td=temp/10;
tu=temp-(td*10);
}
else if(temp<1000)
{
tc=temp/100;
td=(temp-(tc*100))/10;
tu=temp-(tc*100)-(td*10);
}
else
{ tm=temp/1000;
tc=(temp-(tm*1000))/100;
td=(temp-(tm*1000)-(tc*100))/10;
tu=(temp-(tm*1000)-(tc*100)-(td*10));
}

tu+=48;
td+=48;
tc+=48;
tm+=48;
lcd_car(tm);
lcd_car(tc);
lcd_car(td);
lcd_car(tu);
lcd_car(0xDF);
lcd_car('c');

LATCbits.LATC0=1; // Set C0 pin to 1 (5V)
for(i=0;i<10000;i++); // Dummy loop spending 48ms
LATCbits.LATC0=0; // Set pin C0 to 0 (0V)
for(i=0;i<10000;i++); // Dummy loop spending 48ms
}


// Fonctions de gestion du LCD en mode 4 bits

void init_port_lcd (void)
{
// LCD connecte au port 3
TRISD = 0;
LATD = 0x00;
}

// Fonction permettant l affichage d un caractere
void lcd_car (unsigned char car)
{
tpo_us (200);
RS = 1;
R_W = 0;
E = 0;
DATA = ((car & 0xf0) | (DATA & 0x0f)); // Stockage des bits de poids fort de car dans DATA
tpo_us (10);
E = 1;
tpo_us (10);
E = 0;
car <<= 4;

DATA = ((car & 0xf0) | (DATA & 0x0f)); // Stockage des bits de poids faible de car dans DATA
tpo_us (10);
E = 1;
tpo_us (10);
E = 0;
}

void lcd_com (unsigned char commande)
{
tpo_us (100);
RS = 0;
R_W = 0;
E = 0;
DATA = ((commande & 0xf0) | (DATA & 0x0f));

tpo_us (10);
E = 1;
tpo_us (10);
E = 0;
commande <<= 4;
DATA = ((commande & 0xf0) | (DATA & 0x0f));
tpo_us (10);
E = 1;
tpo_us (10);
E = 0;
}

void lcd_com_init (unsigned char commande)
{
RS = 0;
R_W = 0;
E = 0;
DATA = ((commande & 0xf0) | (DATA & 0x0f));
tpo_us (10);
//for(i=0; i<50; i++);
E = 1;
tpo_us (10);
//for(i=0; i<50; i++);
E = 0;
}

void lcd_init (void)
{
tpo_ms (50);
lcd_com_init (0x30);
tpo_ms (5);
lcd_com_init (0x30);
tpo_us (150);
lcd_com_init (0x30);
tpo_us (150);
lcd_com_init (0x20);
lcd_com (0x28); // Mode 4 bits, affichage de caracteres 5x8 sur 4 lignes
lcd_com (0x0E); // Cursor and display on
lcd_com (0x01); // Display clear
lcd_com (0x06);
tpo_ms (5);
}

// Fonction permettant l affichage d une chaine de caracteres
void lcd_str (unsigned char *str)
{
unsigned char i=0;
while(str[i]!= '\0')
{
lcd_car (str[i]);
i++;
}
}



// Fonctions de gestion du timer 0

void tpo_ms (unsigned short tmr_val)
{
unsigned char data;

T0CON = 0b00000100; // run in 16-bit timer, 1:32 prescale value
tmr_val = 65535 - (375 * tmr_val);
data = (tmr_val >> 8);

TMR0H = data;
TMR0L = tmr_val;
T0CONbits.TMR0ON = 1; // starts the timer
while (INTCONbits.TMR0IF == 0);
T0CONbits.TMR0ON = 0; // stops the timer
INTCONbits.TMR0IF = 0;
}

void tpo_us (unsigned short tmr_val)
{
unsigned char data;

T0CON = 0b00001000; // run in 16-bit timer, 1:32 prescale value
tmr_val = 65535 - ( 12 * tmr_val);

data = (tmr_val >> 8);

TMR0H = data;
TMR0L = tmr_val;
T0CONbits.TMR0ON = 1; // starts the timer
while (INTCONbits.TMR0IF == 0);
T0CONbits.TMR0ON = 0; // stops the timer
INTCONbits.TMR0IF = 0;

}

unsigned int readAN0(void)
{
char cT=10;
unsigned int resultat=0;
ADCON0bits.ADON=1; // activation du convertisseur
while(cT--); // tempo
ADCON0bits.GO_DONE=1; // d?marrage conversion
while(ADCON0bits.GO_DONE); // si GO passe ? 0 fin de conversion
resultat=ADRESH;
resultat<<=8;
resultat|=ADRESL; // le r?sultat est contenu dans resultat
ADCON0bits.ADON=0; // convertisseur arr?t?
return resultat;
}

//Initialisation du can
void init_can(void)
{
ADCON2bits.ADCS1=1; ADCON2bits.ADCS0=0; // Fosc 32
ADCON1bits.VCFG1=0; ADCON1bits.VCFG0=0; // configuration de vref
ADCON0bits.CHS3=0; ADCON0bits.CHS2=0; // s?lection du canal AN1
ADCON0bits.CHS1=0; ADCON0bits.CHS0=1;
ADCON1bits.PCFG3=1; ADCON1bits.PCFG2=1; // selection entree analogique
ADCON1bits.PCFG1=1; ADCON1bits.PCFG0=0;
ADCON2bits.ADFM=1; // justification bit ? droite registre de r?ception ADRESH ADRESL
}


/** EOF main.c *************************************************/

(1-1/2)