Hello fellows,
I have spent much time trying to solve an issue in my application,with the hope that they can help me, I decided to ask for your help once again.
So I'm trying to show some symbols in my msp430g2553, but the characters appear wrong. The symbol is the symbol of battery.
main.c
#include <msp430.h>
#include <stdint.h>
#include "main.h"
#include "lcd.h"
int main(void){
WatchDogHold();
LCD_PortConfig();
LCD_Initialize();
LCD_Clear();
LCD_SetSymbol(SYMB_BAT0,0,ROW_ONE);
LCD_SetSymbol(SYMB_BAT25,1,ROW_ONE);
LCD_SetSymbol(SYMB_BAT50,2,ROW_ONE);
LCD_SetSymbol(SYMB_BAT75,3,ROW_ONE);
LCD_SetSymbol(SYMB_BAT100,12,ROW_TWO);
LCD_SetSymbol(SYMB_BAT75,13,ROW_TWO);
LCD_SetSymbol(SYMB_BAT50,14,ROW_TWO);
LCD_SetSymbol(SYMB_HEART,15,ROW_TWO);
_BIS_SR(GIE);
while(1){
}
}
void WatchDogHold(void)
{
WDTCTL = WDTPW + WDTHOLD;
}
lcd.h
#ifndef LCD_H_ #define LCD_H_ /*****************************************************************/ /********* LCD Pinout *********/ /*****************************************************************/ /* */ /* Pin 1 VSS */ /* Pin 2 VDD */ /* Pin 3 VO */ /* Pin 4 RS */ /* Pin 5 RW */ /* Pin 6 E */ /* Pin 7 D0 */ /* Pin 8 D1 */ /* Pin 9 D2 */ /* Pin 10 D3 */ /* Pin 11 D4 */ /* Pin 12 D5 */ /* Pin 13 D6 */ /* Pin 14 D7 */ /* Pin 15 K */ /* Pin 16 A */ /* */ /*****************************************************************/ /*****************************************************************/ /********* INCLUDE *********/ /*****************************************************************/ #include "main.h" #include <string.h> #include <stdio.h> #define ADDR_RAM 0x40 #define ROW_ONE 0x80 #define ROW_TWO 0xC0 #define CMD_END 0x01 #define SYMB_EMPTY 0x00 #define SYMB_HEART 0x01 #define SYMB_BAT100 0x02 #define SYMB_BAT75 0x03 #define SYMB_BAT50 0x04 #define SYMB_BAT25 0x05 #define SYMB_BAT0 0x06 void LCD_PortConfig(void); void LCD_Initialize(void); void LCD_SetText(char* text, int x, int y); void LCD_SetInt(int val, int x, int y); void LCD_Clear(void); void LCD_LoadSymbols(void); void LCD_SetSymbol(unsigned char symbol, unsigned char offset, unsigned char line); #endif /* LCD_H_ */
lcd.c
#include "lcd.h"
#define P1orP2lcd 0x0200
#define EN (PP2 + BIT5)
#define RS (PP1 + BIT3)
#define D4 (PP2 + BIT1)
#define D5 (PP2 + BIT2)
#define D6 (PP2 + BIT3)
#define D7 (PP2 + BIT4)
//#define BACKLIGHT (PP1 + BIT0)
// Commands
#define CLEAR 0x01
void lcdDirPinout(unsigned int pin)
{
if(pin < P1orP2lcd){
P1DIR |= (pin & 0x00FF);
}else{
P2DIR |= (pin & 0x00FF);
}
}
/*
*
*/
void lcdSetPinout(unsigned int pin)
{
if(pin < P1orP2lcd){
P1OUT |= (pin & 0x00FF);
}else{
P2OUT |= (pin & 0x00FF);
}
}
void lcdClrPinout(unsigned int pin)
{
if(pin < P1orP2lcd){
P1OUT &= ~(pin & 0x00FF);
}else{
P2OUT &= ~(pin & 0x00FF);
}
}
void lcdSetValue(unsigned char value)
{
if(value & 0x08){
lcdSetPinout(D7);
}else{
lcdClrPinout(D7);
}
if(value & 0x04){
lcdSetPinout(D6);
}else{
lcdClrPinout(D6);
}
if(value & 0x02){
lcdSetPinout(D5);
}else{
lcdClrPinout(D5);
}
if(value & 0x01){
lcdSetPinout(D4);
}else{
lcdClrPinout(D4);
}
delay_us(10);
}
void lcdTriggerEN()
{
lcdSetPinout(EN);
delay_us(10);
lcdClrPinout(EN);
delay_us(10);
}
void lcdWriteData(unsigned char data)
{
lcdSetPinout(RS); // Set RS to Data
lcdSetValue(data >> 4); // Upper nibble
lcdTriggerEN();
lcdSetValue(data); // Lower nibble
lcdTriggerEN();
delay_us(50); // Delay > 47 us
}
void lcdWriteCmd(unsigned char cmd)
{
lcdClrPinout(RS); // Set RS to Cmd
lcdSetValue(cmd >> 4); // Upper nibble
lcdTriggerEN();
lcdSetValue(cmd); // Lower nibble
lcdTriggerEN();
delay_ms(5); // Delay > 1.5ms
}
/*
*
*/
void LCD_PortConfig(void)
{
// Direction
lcdDirPinout(D4);
lcdDirPinout(D5);
lcdDirPinout(D6);
lcdDirPinout(D7);
lcdDirPinout(EN);
lcdDirPinout(RS);
}
/*
*
*/
void LCD_Initialize(void)
{
delay_ms(100);
P2OUT = 0x03; // Start LCD (send 0x03)
lcdTriggerEN(); // Send 0x03 3 times at 5ms then 100 us
delay_ms(5);
lcdTriggerEN();
delay_ms(5);
lcdTriggerEN();
delay_ms(5);
P2OUT = 0x02; // Switch to 4-bit mode
lcdTriggerEN();
delay_ms(5);
lcdWriteCmd(0x28); // 4-bit, 2 line, 5x8
lcdWriteCmd(0x08); // Instruction Flow
lcdWriteCmd(0x0C); // Display On, No blink
lcdWriteCmd(0x01); // Clear LCD
lcdWriteCmd(0x06); // Auto-Increment
LCD_LoadSymbols();
}
void LCD_SetText(char* text, int x, int y){
unsigned int i;
if (x < 16) {
x |= 0x80; // Set LCD for first line write
switch (y){
case 1:
x |= 0x40; // Set LCD for second line write
break;
case 2:
x |= 0x60; // Set LCD for first line write reverse
break;
case 3:
x |= 0x20; // Set LCD for second line write reverse
break;
}
lcdWriteCmd(x);
}
i = 0;
while (text[i] != '\0') {
lcdWriteData(text[i]);
i++;
}
}
void LCD_SetInt(int val, int x, int y)
{
char number_string[16];
sprintf(number_string, "%d", val); // Convert the integer to character string
LCD_SetText(number_string, x, y);
}
void LCD_Clear()
{
lcdWriteCmd(CLEAR);
}
void LCD_LoadSymbols(void)
{
int i;
static char empty[] = {0x0,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
static char heart[] = {0x00,0xa,0x1f,0x1f,0xe,0x4,0x00,0x00};
static char bat_100[] = {0x4,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f};
static char bat_75[] = {0x4,0x1b,0x11,0x1f,0x1f,0x1f,0x1f,0x1f};
static char bat_50[] = {0x4,0x1b,0x11,0x11,0x1f,0x1f,0x1f,0x1f};
static char bat_25[] = {0x4,0x1b,0x11,0x11,0x11,0x11,0x1f,0x1f};
static char bat_0[] = {0x4,0x1b,0x11,0x11,0x11,0x11,0x11,0x1f};
lcdWriteCmd(0x40);
for(i=0;i!=8;i++){
lcdWriteData(empty[i]);
}
for(i=0;i!=8;i++){
lcdWriteData(heart[i]);
}
for(i=0;i!=8;i++){
lcdWriteData(bat_100[i]);
}
for(i=0;i!=8;i++){
lcdWriteData(bat_75[i]);
}
for(i=0;i!=8;i++){
lcdWriteData(bat_50[i]);
}
for(i=0;i!=8;i++){
lcdWriteData(bat_25[i]);
}
for(i=0;i!=8;i++){
lcdWriteData(bat_0[i]);
}
lcdWriteCmd(CMD_END);
}
void LCD_SetSymbol(unsigned char symbol, unsigned char offset, unsigned char line)
{
lcdWriteCmd(line+offset);
lcdWriteData(symbol);
}
