AT91SAM4 with HD44780

Discussion around product based on ARM Cortex M4 core.
This forum will be discontinued soon.

Moderators: nferre, ncollot

Carline
Posts: 1
Joined: Tue Mar 03, 2015 6:45 pm

AT91SAM4 with HD44780

Tue Mar 03, 2015 9:13 pm

Hey,
I'm trying to get an old LCD with a HD44780 Controller to work. It’s connect to a SAM4S Xplained in the following way:
PA11 -----> RS
PA12 -----> E
GND -----> RW
PA13 -----> DB4
PA14 -----> DB5
PA15 -----> DB6
PA16 -----> DB7
There is no way for me to start with PA0.
At the moment the Display is showing signs but not in the way i want it especially strings and I can't figure out the cause.

Code: Select all

#include "asf.h"
#include "conf_board.h"
#include "conf_clock.h"

#define LCD_RS		0b00000000000000000000100000000000
#define LCD_EN		0b00000000000000000001000000000000

//				PortA: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
#define MASKA		0b00000000000000011111100000000000	// mask all Pins
#define MASKDB		0b00000000000000011110000000000000	// mask pins for DB5 - DB7

//LCD commands
#define LCD_SET_FUNCTION	0x20

#define LCD_CLR				0x01	// clear LCD
#define LCD_4BIT			0x00	// 4-bit mode
#define LCD_INC				0x06	//Increment, display freeze
#define LCD_MOV				0x10	//Cursor move, not shift
#define LCD_ALL				0x0F	// LCD On, LCD display on, cursor on and blink on
#define LCD_ON				0x08	// turn lcd/cursor on
#define LCD_SOFT_RESET		0x30	// soft-reset		
#define LCD_FUNCTION_1LINE	0x00
#define LCD_FUNCTION_2LINE	0x08	
#define LCD_FUNCTION_5X7	0x00
#define LCD_FUNCTION_5X10	0x04

// Set Display ---------------- 0b00001xxx
#define LCD_SET_DISPLAY         0x08
 
#define LCD_DISPLAY_OFF         0x00
#define LCD_DISPLAY_ON          0x04
#define LCD_CURSOR_OFF          0x00
#define LCD_CURSOR_ON           0x02
#define LCD_BLINKING_OFF        0x00
#define LCD_BLINKING_ON         0x01

// Set Entry Mode ------------- 0b000001xx
#define LCD_SET_ENTRY           0x04
 
#define LCD_ENTRY_DECREASE      0x00
#define LCD_ENTRY_INCREASE      0x02
#define LCD_ENTRY_NOSHIFT       0x00
#define LCD_ENTRY_SHIFT         0x01

// Cursor Home ---------------- 0b0000001x
#define LCD_CURSOR_HOME         0x02

// LCD Command delays
#define LCD_BOOTUP_MS           20
#define LCD_ENABLE_US           20
#define LCD_WRITEDATA_US        46
#define LCD_COMMAND_US          42

#define LCD_SOFT_RESET_MS1      6
#define LCD_SOFT_RESET_MS2      2
#define LCD_SOFT_RESET_MS3      2
#define LCD_SET_4BITMODE_MS     6
#define LCD_CLEAR_DISPLAY_MS    3
#define LCD_CURSOR_HOME_MS      3

static void LCDInit (void);
static void lcd_enable(void);
static void LCD_command(uint8_t);
void lcd_data( uint8_t);
static void lcd_out(uint8_t);
void lcd_string(const char *data);
void lcd_clear(void);
void lcd_home(void);

int main (void)
{
	sysclk_init();
	board_init();
	ioport_init();
	ioport_enable_port(IOPORT_PIOA,MASKA);						// enable selected pins
	ioport_set_port_dir(IOPORT_PIOA, MASKA,IOPORT_DIR_OUTPUT);	// set pins as output

	LCDInit();
	while (1) 
	{	
		lcd_data(21);
		delay_ms(1000);

	}
}

static void lcd_enable(void)
{
	ioport_set_port_level(IOPORT_PIOA,LCD_EN,LCD_EN);	// set enable to 1
	delay_us(LCD_ENABLE_US);							// delay
	ioport_set_port_level(IOPORT_PIOA,LCD_EN,0);		// set enable to 0
}

static void lcd_out(uint8_t data)
{
	ioport_set_port_level(IOPORT_PIOA,MASKDB,0);	// set DP-Pins to 0
	ioport_set_port_level(IOPORT_PIOA,MASKDB,data<<12);	// left shift bits for 12 digts (PA13) and load pins
	lcd_enable();
}

void LCDInit (void)
{
	// set all outputs to 0
	ioport_set_port_level(IOPORT_PIOA,MASKA,0);
	delay_ms(LCD_BOOTUP_MS);
	
	// Soft-Reset has to be done three times
	LCD_command(LCD_SOFT_RESET);
	lcd_enable();
	delay_ms(4.1);
	LCD_command(LCD_SOFT_RESET);
	lcd_enable();
	delay_us(100);
	
	LCD_command(LCD_SOFT_RESET);
	lcd_enable();
	delay_us(100);
		
	// 4-bit Mode
	lcd_out(LCD_SET_FUNCTION |LCD_4BIT);
	delay_ms(LCD_SET_4BITMODE_MS);
		 
	// 4-bit Mode / 2 lines / 5x7
	LCD_command(LCD_SET_FUNCTION | LCD_4BIT | LCD_FUNCTION_2LINE | LCD_FUNCTION_5X7);
	delay_us(100);
		
	// Display on / Cursor off / Blinking off
	LCD_command(LCD_SET_DISPLAY | LCD_DISPLAY_ON | LCD_CURSOR_OFF | LCD_BLINKING_OFF); 
	delay_us(100);
	
	// Cursor inkrement / no Scroll
	LCD_command(LCD_SET_ENTRY | LCD_ENTRY_INCREASE | LCD_ENTRY_NOSHIFT);
	delay_us(100);
		
	lcd_clear();
	lcd_home();
}

void lcd_data(uint8_t data)

{	
	ioport_set_port_level(IOPORT_PIOA,LCD_RS,LCD_RS);	// set RS to 1
	lcd_out(data);										// write highest bits first,								
	lcd_out(data<<4);									
	delay_us(LCD_WRITEDATA_US);
	
}

static void LCD_command(uint8_t data)

{
	ioport_set_port_level(IOPORT_PIOA,LCD_RS,0);	// set RS to 0
	lcd_out(data);									// write highest bits first								
	lcd_out(data<<4);								
	delay_us(LCD_COMMAND_US);	
}

void lcd_string( const char *data )
{
	while( *data != '\0' )
	lcd_data( *data++ );
}

void lcd_clear(void)
{
	LCD_command(LCD_CLR);
	delay_ms(LCD_CLEAR_DISPLAY_MS);
}

void lcd_home( void )
{
	LCD_command(LCD_CURSOR_HOME);
	delay_ms(LCD_CURSOR_HOME_MS);
}
I hope someone has an idea if I'm doing something wrong.
Thanks you for every suggestion.
pilnik5
Posts: 5
Joined: Sat Sep 05, 2015 5:43 pm

Re: AT91SAM4 with HD44780

Mon Dec 05, 2016 8:55 am

Hello. I play with LCD and SAM4E16E (ATSAM16-XPRO - kit), and here is function code for LCD with HD44780.

Code: Select all

#include <asf.h>
#include "conf_board.h"
#include "conf_clock.h"

/**********************
PA11 -----> RS
PA12 -----> E
GND -----> RW
PA13 -----> DB4
PA14 -----> DB5
PA15 -----> DB6
PA16 -----> DB7
***********************/

#define LEDnaPD22	IOPORT_CREATE_PIN(PIOD, 22)	//PPin na kterem je pripojena LED

#define LCD_RS		IOPORT_CREATE_PIN(PIOA, 11)
#define LCD_EN		IOPORT_CREATE_PIN(PIOA, 12)
#define LCD_D4		IOPORT_CREATE_PIN(PIOA, 13)
#define LCD_D5		IOPORT_CREATE_PIN(PIOA, 14)
#define LCD_D6		IOPORT_CREATE_PIN(PIOA, 15)
#define LCD_D7		IOPORT_CREATE_PIN(PIOA, 16)

void lcd_strobe(void);
void lcd_write(unsigned char c);
void lcd_clear(void);
void lcd_puts(const char * s);
void lcd_putch(unsigned char c);
void lcd_goto(unsigned char pos,unsigned char line);
void lcd_init(void);

void lcd_strobe(void)
{
	ioport_set_pin_level(LCD_EN, 1);
	delay_us(1);
	ioport_set_pin_level(LCD_EN, 0);
}

void lcd_write(unsigned char c)
{
	//upper
	if(c & 0x80) ioport_set_pin_level(LCD_D7, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D7, IOPORT_PIN_LEVEL_LOW);
	if(c & 0x40) ioport_set_pin_level(LCD_D6, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D6, IOPORT_PIN_LEVEL_LOW);
	if(c & 0x20) ioport_set_pin_level(LCD_D5, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D5, IOPORT_PIN_LEVEL_LOW);
	if(c & 0x10) ioport_set_pin_level(LCD_D4, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D4, IOPORT_PIN_LEVEL_LOW);
	lcd_strobe();
	//lowwer
	if(c & 0x08) ioport_set_pin_level(LCD_D7, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D7, IOPORT_PIN_LEVEL_LOW);
	if(c & 0x04) ioport_set_pin_level(LCD_D6, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D6, IOPORT_PIN_LEVEL_LOW);
	if(c & 0x02) ioport_set_pin_level(LCD_D5, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D5, IOPORT_PIN_LEVEL_LOW);
	if(c & 0x01) ioport_set_pin_level(LCD_D4, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D4, IOPORT_PIN_LEVEL_LOW);
	lcd_strobe();
	delay_us(40);
}

void lcd_clear(void)
{
	ioport_set_pin_level(LCD_RS, IOPORT_PIN_LEVEL_LOW);
	lcd_write(0x1);
	delay_ms(2);
}

void lcd_puts(const char * s)
{
	ioport_set_pin_level(LCD_RS, IOPORT_PIN_LEVEL_HIGH);    // write characters
	while(*s) lcd_write(*s++);
}

void lcd_putch(unsigned char c)
{
	ioport_set_pin_level(LCD_RS, IOPORT_PIN_LEVEL_HIGH);    // write characters
	lcd_write(c);
}

void lcd_goto(unsigned char pos,unsigned char line)
{
	ioport_set_pin_level(LCD_RS, IOPORT_PIN_LEVEL_LOW);
	if (line==0)
		lcd_write(0x80 + pos);
	else
		lcd_write(0x80 + pos+ 0x40);
}

void lcd_init(void)
{
	ioport_set_pin_level(LCD_RS, IOPORT_PIN_LEVEL_LOW);    // write control bytes
	delay_ms(15);// power on delay
	ioport_set_pin_level(LCD_D4, IOPORT_PIN_LEVEL_HIGH);    // init
	ioport_set_pin_level(LCD_D5, IOPORT_PIN_LEVEL_HIGH);	//
	lcd_strobe();
	delay_ms(5);
	lcd_strobe();    // init
	delay_us(100);
	lcd_strobe();    // init
	delay_ms(5);
	ioport_set_pin_level(LCD_D4, IOPORT_PIN_LEVEL_LOW);    // set 4 bit mode
	lcd_strobe();
	delay_us(40);
	
	lcd_write(0x28);// 4 bit mode, 1/16 duty, 5x8 font, 2lines
	lcd_write(0x0C);// display on
	lcd_write(0x06);// entry mode advance cursor
	lcd_write(0x01);// clear display and reset cursor
}

int main (void)
{
	sysclk_init();
	board_init();
	ioport_init();
	
	ioport_set_pin_dir(LEDnaPD22, IOPORT_DIR_OUTPUT);	//PIN PD18 jako vystupni
	
	ioport_set_pin_dir(LCD_RS, IOPORT_DIR_OUTPUT);	//PIN PD18 jako vystupni
	ioport_set_pin_dir(LCD_EN, IOPORT_DIR_OUTPUT);	//PIN PD18 jako vystupni
	ioport_set_pin_dir(LCD_D4, IOPORT_DIR_OUTPUT);	//PIN PD18 jako vystupni
	ioport_set_pin_dir(LCD_D5, IOPORT_DIR_OUTPUT);	//PIN PD18 jako vystupni
	ioport_set_pin_dir(LCD_D6, IOPORT_DIR_OUTPUT);	//PIN PD18 jako vystupni
	ioport_set_pin_dir(LCD_D7, IOPORT_DIR_OUTPUT);	//PIN PD18 jako vystupni
	
	ioport_set_pin_level(LEDnaPD22, IOPORT_PIN_LEVEL_LOW);	//inicializuje jako NULA
	
	lcd_init();
	
	while (1)
	{	
		delay_ms(250);
		ioport_toggle_pin_level(LEDnaPD22);
		
		lcd_clear();
		lcd_goto(0,0);
		lcd_puts("Hello") ;
		lcd_goto(5,1);
		lcd_puts("jelito") ;
	}
}

Return to “SAM4 Cortex-M4 MCU”

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest