Je code avec xc8 sur le 16f1937.
J'ai décidé d'utiliser une librairie pour commander mon lcd. (4x20 en mode 4 bits).
une simulation sur PROTEUS me ramène à l'erreur : "controller received command whilst busy".
Autrement dit, il reçoit des commandes alors qu'il est occupé.
La librairie fonctionne pourtant bien sur le 16f877.
Quelqu'un a déjà rencontré se problème sur se pic ?
Câblage :
PORTD[0-3] sur data[4-7]
PORTD(4) sur RS
PORTD(5) sur RW
PORTD(6) sur EN
Ci-joint le code :
main.c
Code : Tout sélectionner
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "lcd2/lcd.h" //Librairie LCD
#define _XTAL_FREQ 8000000
#pragma config WDTE = OFF
int main(int argc, char** argv) {
TRISD=0b00000000;
ANSELD=0b00000000;
LATD=0b00000000;
__delay_ms(1000);
lcd_init();
for(;;)
{
}
}
lcd.h
Code : Tout sélectionner
/* write a byte to the LCD in 4 bit mode */
extern void lcd_write(unsigned char);
/* Clear and home the LCD */
extern void lcd_clear(void);
/* write a string of characters to the LCD */
extern void lcd_puts(const char * s);
/* Go to the specified position */
extern void lcd_goto(unsigned char pos);
/* intialize the LCD - call before anything else */
extern void lcd_init(void);
extern void lcd_putch(char);
/* Set the cursor position */
#define lcd_cursor(x) lcd_write(((x)&0x7F)|0x80)
lcd.c
Code : Tout sélectionner
#ifndef _XTAL_FREQ
// Unless specified elsewhere, 8MHz system frequency is assumed
#define _XTAL_FREQ 8000000
#endif
#include <xc.h>
#include "lcd.h"
#define LCD_RS RD4
#define LCD_RW RD5
#define LCD_EN RD6
#define LCD_DATA LATD
#define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
/* write a byte to the LCD in 4 bit mode */
void
lcd_write(unsigned char c)
{
__delay_us(40);
LCD_DATA = ( ( c >> 4 ) & 0x0F );
LCD_STROBE();
LCD_DATA = ( c & 0x0F );
LCD_STROBE();
}
/*
* Clear and home the LCD
*/
void
lcd_clear(void)
{
LCD_RS = 0;
lcd_write(0x1);
__delay_ms(2);
}
/* write a string of chars to the LCD */
void
lcd_puts(const char * s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}
/* write one character to the LCD */
void
lcd_putch(char c)
{
LCD_RS = 1; // write characters
lcd_write( c );
}
/*
* Go to the specified position
*/
void
lcd_goto(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0x80+pos);
}
/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
char init_value;
init_value = 0x3;
TRISC=0;
TRISD=0;
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;
__delay_ms(15); // wait 15mSec after power applied,
LCD_DATA = init_value;
LCD_STROBE();
__delay_ms(5);
LCD_STROBE();
__delay_us(200);
LCD_STROBE();
__delay_us(200);
LCD_DATA = 2; // Four bit mode
LCD_STROBE();
lcd_write(0x28); // Set interface length
lcd_write(0xF); // Display On, Cursor On, Cursor Blink
lcd_clear(); // Clear screen
lcd_write(0x6); // Set entry Mode
}
Merci et bonne journée.



