Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write the code in C for a MSP430 using Code Composer Studio that would display the number of button presses in real time on a

Write the code in C for a MSP430 using Code Composer Studio that would display the number of button presses in real time on a 16x2 LCD in 4 bit mode. i have the button counter working and can display words to the LCD. I just dont know how to get the "count' value displayed and updated. I have posted my code and the header file i used. The button is the p1.3 button on the msp board. As it is right now all it displays is the word "count", not the actual value, though i can read the presses from the expression window.

#include

#include "lcd.h"

#define BUTTON BIT3

/**

* main.c

*/

unsigned int count = 0;

void main(void)

{

WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer

P1IE = BUTTON; //enable interrupt

P1IES = BUTTON; //interrupt edge

P1REN = BUTTON; //enable resistor tied to button

P1IFG = 0x00; //clear the interrupt flag

_enable_interrupts(); //enable all interrupts

lcd_init();

lcd_out ("count");

while(1);

}

#pragma vector = PORT1_VECTOR //define interrupt vector

__interrupt void PORT1_ISR(void){ //interrupt service routine for port 1

if((P1IN & BUTTON) == 0x00)//when button is pushed

count++; // count increments to keep track of pushes

P1IFG = 0x00; //clear the interrupt flag

}

header file lcd.h below

#include

#define LCD_BIT_4 0x28 //4 bit mode(2 lines and 57 matrix) #define LCD_BIT_8 0x38 //8 bit mode(2 lines and 57 matrix)

#define LCD_FIRST_ROW 0x80 //first row #define LCD_SECOND_ROW 0xC0 //2nd row #define LCD_THIRD_ROW 0x94 //3rd row #define LCD_FOURTH_ROW 0xD4 //4th roW

#define LCD_DISPLAY_ON_CURSOR_ON 0x0F //Display On Cursor Blink #define LCD_DISPLAY_OFF 0x08 //DISPLAY OFF CURSOR OFF -TURN OFF LCD #define LCD_DISPLAY_ON_CURSOR_OFF 0x0C //DISPLAY ON CURSOR OFF #define LCD_UNDERLINE_ON 0x0E //UNDERLINE ON

#define LCD_CLEAR 0x01 //Clear Display #define LCD_RETURN_HOME 0x02 //RETURN HOME

#define LCD_INCREMENT_CURSOR 0x06 //Increment Cursor #define LCD_DECREENT_CURSOR 0x04 //decrement Cursor

#define LCD_MOVE_CURSOR_LEFT 0x10 //MOVE CURSOR POS TO LEFT #define LCD_MOVE_CURSOR_RIGHT 0x14 //MOVE CURSOR POS TO RIGHT

#define LCD_SHIFT_LEFT 0x18 //SHIFT ENTIRE DISPLAY TO LEFT #define LCD_SHIFT_RIGHT 0x1C //SHIFT ENTIRE DISPLAY TO LEFT

#define DATA P2OUT = P2OUT | BIT4 // define RS high #define COMMAND P2OUT = P2OUT & (~BIT4) // define RS low

#define ENABLE_HIGH P2OUT = P2OUT | BIT5 // define Enable high signal #define ENABLE_LOW P2OUT = P2OUT & (~BIT5) // define Enable Low signal unsigned int i; unsigned int j;

void delay(unsigned int k) { for(j=0;j<=k;j++) { for(i=0;i<100;i++); } }

void trigger_write(void) { ENABLE_HIGH; delay(2); ENABLE_LOW; }

void lcd_cmd(unsigned char cmd) { COMMAND; P2OUT = (P2OUT & 0xF0)|((cmd>>4) & 0x0F); // transfer higher nibble trigger_write(); // trigger enable P2OUT = (P2OUT & 0xF0)|(cmd & 0x0F); // write lower nibble trigger_write(); // trigger enable }

void lcd_chr(unsigned char data) { DATA; P2OUT = (P2OUT & 0xF0)|((data>>4) & 0x0F); // transfer higher nibble trigger_write(); // enable P2OUT = (P2OUT & 0xF0)|(data & 0x0F); // transfer lower nibble trigger_write(); // enable }

void lcd_out(char *str) { while(*str) { lcd_chr(*str); str++; } }

void lcd_init(void) { P2DIR |= 0xFF; //Port2 as output P2OUT &= 0x00; //port2 intial value as 0 lcd_cmd(0x33); lcd_cmd(0x32); lcd_cmd(LCD_BIT_4); lcd_cmd(0x0E); //display on cursor blink lcd_cmd(LCD_CLEAR); lcd_cmd(LCD_INCREMENT_CURSOR); lcd_cmd(LCD_FIRST_ROW); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

=+ Understand workplace aggression.

Answered: 1 week ago