Navigation Menu

Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kates committed Jun 19, 2012
0 parents commit b0c1702
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Makefile
@@ -0,0 +1,21 @@
#MCU = msp430g2553
MCU = msp430g2452

clean:
rm -f main.elf main.lst lcd.o

all: clean flash

flash: compile
mspdebug rf2500 "prog main.elf"

lcd:
msp430-gcc -Os -Wall -mmcu=$(MCU) -c lcd.c

compile: lcd
msp430-gcc -Os -Wall -mmcu=$(MCU) -o main.elf main.c lcd.o
msp430-objdump -DS main.elf > main.lst
msp430-objdump -h main.elf
msp430-size main.elf

.PHONY: all
3 changes: 3 additions & 0 deletions README.md
@@ -0,0 +1,3 @@
# MSP430 LCD

A MSP430 lcd library in 4 bit mode.
124 changes: 124 additions & 0 deletions lcd.c
@@ -0,0 +1,124 @@
#include <msp430.h>
#include <stdint.h>
#include "lcd_config.h"
#include "lcd.h"

uint8_t lcd_pins[4] = {LCD_D4, LCD_D5, LCD_D6, LCD_D7};

void lcd_delay_ms(uint16_t t) {
uint16_t i, j;
for (i = 0; i < (LCD_FCPU/1000); i++) {
for (j = 0; j < t; j++) {
nop();
}
}
}

void lcd_delay_us(uint16_t t) {
uint16_t i;
for (i = 0; i < (LCD_FCPU/1000000) * t; i++) {
nop();
}
}

void lcd_send4(uint8_t data) {
uint8_t i;
LCD_PORT |= LCD_EN;
for (i = 0; i < 4; i++) {
if (data & (1 << i)){
LCD_PORT |= lcd_pins[i];
} else {
LCD_PORT &= ~lcd_pins[i];
}
}
lcd_delay_us(1);
LCD_PORT &= ~LCD_EN;
LCD_PORT &= ~(LCD_DATA_PINS);
lcd_delay_us(1);
}

void lcd_send(uint8_t cmd, uint8_t data) {
LCD_PORT &= ~(LCD_RS | LCD_EN | LCD_DATA_PINS);

if (cmd & LCD_DATA) {
LCD_PORT |= LCD_RS;
}

lcd_send4(data >> 4);

if (cmd & LCD_CMD) {
lcd_delay_ms(5);
} else {
lcd_delay_us(1);
}

lcd_send4(data & 0x0F);

if (cmd & LCD_DATA) {
LCD_PORT &= ~LCD_RS;
}

lcd_delay_us(200);
}

void lcd_char(uint8_t data) {
lcd_send(LCD_DATA, data);
}

void lcd_cmd(uint8_t data) {
lcd_send(LCD_CMD, data);
}

uint8_t lcd_write(const char *str) {
uint8_t i = 0;
while (*str) {
lcd_char(*str++);
i++;
}
return i;
}

void lcd_writeln(const char *str) {
uint8_t i = lcd_write(str);
while(i++ < LCD_COLUMNS) {
lcd_char(' ');
}
}

void lcd_init(void) {
LCD_DIR |= LCD_RS | LCD_EN | LCD_DATA_PINS;
LCD_PORT &= ~(LCD_RS | LCD_EN | LCD_DATA_PINS);

lcd_delay_ms(20); // give lcd time to startup

lcd_send4(0x03);
lcd_delay_ms(5);

if (LCD_MODE == LCD_MODE_4) {
lcd_send4(0x02);
lcd_delay_us(37);
}

lcd_cmd(0x28);
lcd_cmd(0x01);
lcd_cmd(0x06);
lcd_delay_us(1200);
lcd_cmd(0x0C);
}

void lcd_go(uint8_t row, uint8_t col) {
if (row == 4) {
lcd_cmd(LCD_LINE_4 + col);
} else if (row == 3) {
lcd_cmd(LCD_LINE_3 + col);
} else if (row == 2) {
lcd_cmd(LCD_LINE_2 + col);
} else {
lcd_cmd(LCD_LINE_1 + col);
}
}

void lcd_go_line(uint8_t line) {
lcd_go(line, 0);
}

68 changes: 68 additions & 0 deletions lcd.h
@@ -0,0 +1,68 @@
#ifndef __LCD_H__
#define __LCD_H__
#include "lcd_config.h"

#ifndef LCD_PORT
#error "LCD_PORT not defined!"
#endif

#ifndef LCD_DIR
#error "LCD_DIR not defined!"
#endif

#ifndef LCD_RS
#error "LCD_RS not defined!"
#endif

#ifndef LCD_EN
#error "LCD_EN not defined!"
#endif

#ifndef LCD_D4
#error "LCD_D4 not defined!"
#endif

#ifndef LCD_D5
#error "LCD_D5 not defined!"
#endif

#ifndef LCD_D6
#error "LCD_D6 not defined!"
#endif

#ifndef LCD_D7
#error "LCD_D7 not defined!"
#endif

#ifndef LCD_COLUMNS
#define LCD_COLUMNS 16
#endif

#ifndef LCD_ROWS
#define LCD_ROWS 2
#endif

#define LCD_MODE_4 4

#ifndef LCD_MODE
#define LCD_MODE LCD_MODE_4
#endif

#define LCD_DATA_PINS LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7
#define LCD_CMD 0
#define LCD_DATA 1

#define LCD_HOME 0x80
#define LCD_LINE_1 LCD_HOME
#define LCD_LINE_2 LCD_HOME + 0x40
#define LCD_LINE_3 LCD_HOME + 0x60
#define LCD_LINE_4 LCD_HOME + 0x20

void lcd_init(void);
void lcd_writeln(const char *str);
uint8_t lcd_write(const char *str);
void lcd_go(uint8_t row, uint8_t col);
void lcd_go_line(uint8_t line);
void lcd_delay_ms(uint16_t t);
void lcd_delay_us(uint16_t t);
#endif // __LCD_H__
22 changes: 22 additions & 0 deletions lcd_config.h
@@ -0,0 +1,22 @@
/*
* Define pins and ports here
*/

#define LCD_PORT P1OUT
#define LCD_DIR P1DIR

#define LCD_RS BIT0
#define LCD_EN BIT1

#define LCD_D4 BIT4
#define LCD_D5 BIT5
#define LCD_D6 BIT6
#define LCD_D7 BIT7
#define LCD_FCPU 8000000


// how many rows and colums?
// default is 16x2

// #define LCD_COLUMNS 16
// #define LCD_ROWS 2
18 changes: 18 additions & 0 deletions main.c
@@ -0,0 +1,18 @@
#include <msp430.h>
#include <stdint.h>
#include "lcd.h"

int main(void) {
WDTCTL = WDTPW + WDTHOLD;
DCOCTL = CALDCO_8MHZ;
BCSCTL1 = CALBC1_8MHZ;

lcd_init();
lcd_go_line(1);
lcd_writeln("Hello");
lcd_go_line(2);
lcd_writeln("World");
_BIS_SR(LPM3_bits);
while (1) {}
return 0;
}

0 comments on commit b0c1702

Please sign in to comment.