Interfacing an MSP430g2553 with a 16x4 LCD screen - msp430

I'm having trouble getting my msp430 to communicate with a 16x4 LCD screen. Data sheet for the LCD screen: https://www.beta-estore.com/download/rk/RK-10290_410.pdf
Here is my code:
#define READMODE P2OUT = (P2OUT | BIT1) //set R/W pin high
#define WRITEMODE P2OUT=(P2OUT & ~BIT1) //set R/W pin low
#define ENABLEON P2OUT=P2OUT | 0x04 //set enable pin high
#define ENABLEOFF P2OUT=P2OUT & 0xFB //set enable pin low
#define RSDATA P2OUT = (P2OUT | BIT0) //set register select bit high so that the databus is sent to display
#define RSINSTRUCTION P2OUT = (P2OUT & ~BIT0) //set register select low so databus is sent to command register for initialization
int main(void){
WDTCTL = WDTPW + WDTHOLD; //stop watchdog timer
P1DIR = 0xFF; //The entire P1 register is output
P2DIR = 0xF7; //The entire P2 register is output except for p2.3
P1OUT = 0x00;
P2OUT = 0x01;
ENABLEOFF;
WRITEMODE;
RSINSTRUCTION;
sendCommand(0x30); //function set for 8 bit mode and display type
sendCommand(0x01); //clear screen
sendCommand(0x02); //Return Home
sendCommand(0x07); //Increment cursor and screen right
sendCommand(0x0B); //Screen display on and blinking cursor
sendChar('a');
}
/**This function checks if the LCD is busy
*/
void isBusy(void){
P1DIR &= ~BIT7; //Set bit 7 of P1 register as input
READMODE;
RSINSTRUCTION;
while((P1IN & BIT7) == BIT7){
dataRead();
}
P1DIR |= BIT7; //Set bit 7 of P1 register back to output
WRITEMODE;
}
/**Allows databus to be sent to LCD
*/
void dataWrite(void){
ENABLEOFF;
__delay_cycles(1000000);
ENABLEON;
__delay_cycles(1000000);
ENABLEOFF;
__delay_cycles(1000000);
}
/**This function is only for checking if the LCD is busy. If it is busy
* it will blink the enable light on and off
*/
void dataRead(void){
ENABLEOFF;
__delay_cycles(1000000);
ENABLEON;
__delay_cycles(1000000);
ENABLEOFF;
__delay_cycles(1000000);
}
/**This function sends a command to the LCD screen
*/
void sendCommand(unsigned char command){
isBusy();
WRITEMODE;
RSINSTRUCTION;
P1OUT = command;
dataWrite();
}
void sendChar(char letter){
RSDATA;
WRITEMODE;
P1OUT = letter;
dataWrite();
}
I'm pretty positive all my pins are connected correctly. The LCD is projecting light but that's it. I can't even get the cursor to show up and blink on the screen. I have my bus connected to 8 LED lights so I can be sure the correct commands are being sent. That is also why I have the long delay in between each write operation. Not sure what to do from here to troubleshoot. Any help would be greatly appreciated.

Sorry for the delay. The solution to the issue was adding a potentiometer to the circuit so I could change the contrast of the LCD. I also had to change the line
sendCommand(0x0B); //Screen display on and blinking cursor
to
sendCommand(0x0F); //Screen display on and blinking cursor
Thank you all for the contributions.

Related

STM32F4 EXTI interrupts interfere with each other

I am working with STM32F407VGT6 MCU and I am having problem with external interrupts (EXTI). I have configured two pins as EXTI and they are PE7 and PE15. They are connected to HALL sensors driver and are detecting toot edges of trigger wheel. Ones is primary source with multiple tooth and other wheel is single tooth wheel which confirms position. The thing is they can work individually on their own without issues, but if I connect both of them, they start to interfere with each other and I am loosing position sync, because MCU is detecting false edges. I can recreate same behavior by connecting either pin to low signal and other to HALL driver. But if I disable EXTI and leave pin as a input the problem goes away. I have no idea what's going on here.
Also, I had issues with PE15 and EXTI before and it might be related to this. The EXTI was working only in EXTI_Trigger_Rising and EXTI_Trigger_Rising_Falling mode, but EXTI_Trigger_Falling was giving random edge detection, and only solution to this was to listen for both edges and debounce the one that I don't need. I couldn't find anything about this in datasheet.
This STM32F4 is giving me head aches and I running out of options. Well, the last option is to reroute the hall drivers to other pins and use input capture/timer.
Primary wheel config:
void Trigger_Configure_Primary(void) {
// GPIO
GPIOE->OSPEEDR |= (0x03 << (2 * 15)); // high speed
// EXTI
SYSCFG->EXTICR[3] = SYSCFG_EXTICR4_EXTI15_PE; // Tell system that you will use PE15 for EXTI15
EXTI->RTSR |= (1 << 15); // rising edge
EXTI->FTSR |= (1 << 15); // falling edge
EXTI->IMR |= (1 << 15); // Unmask EXTI15 interrupt
EXTI->PR |= (1 << 15); // Clear pending bit
/* Add IRQ vector to NVIC */
NVIC_SetPriority(EXTI15_10_IRQn, 0);
NVIC_EnableIRQ(EXTI15_10_IRQn);
}
Secondary wheel config:
void Trigger_Configure_Secondary(void) {
// GPIO
GPIOE->OSPEEDR |= (0x03 << (2 * 7)); // high speed
// EXTI
SYSCFG->EXTICR[1] = SYSCFG_EXTICR2_EXTI7_PE; // Tell system that you will use PE7 for EXTI7
EXTI->RTSR |= (1 << 7); // rising edge
EXTI->FTSR |= (1 << 7); // falling edge
EXTI->IMR |= (1 << 7); // Unmask EXTI7 interrupt
EXTI->PR |= (1 << 7); // Clear pending bit
/* Add IRQ vector to NVIC */
NVIC_SetPriority(EXTI9_5_IRQn, 0);
NVIC_EnableIRQ(EXTI9_5_IRQn);
}
IRQ handlers:
void EXTI9_5_IRQHandler(void) {
__disable_irq();
/* Make sure that interrupt flag is set */
if ((EXTI->PR & EXTI_Line7) != 0) {
// Secondary trigger IRQ
uint32_t now_nt = GET_TIMESTAMP();
uint8_t edge = ((GPIOE->IDR & SECONDARY_PIN) == 0 ? 0 : 1);
TD_Decode_Secondary_Trigger_Event(
now_nt,
edge
);
#ifdef DEBUG
// Stats
secondary_ticks = (GET_TIMESTAMP() - now_nt);
#endif
/* Clear interrupt flag */
EXTI->PR |= EXTI_Line7;
++s_cnt;
}
__enable_irq();
}
void EXTI15_10_IRQHandler(void) {
__disable_irq();
/* Make sure that interrupt flag is set */
if ((EXTI->PR & EXTI_Line15) != 0) {
// Primary trigger IRQ
uint32_t now_nt = GET_TIMESTAMP();
uint8_t edge = ((GPIOE->IDR & PRIMARY_PIN) == 0 ? 0 : 1);
if (primary_edge == edge) {
TD_Decode_Primary_Trigger(now_nt);
}
#ifdef DEBUG
// Stats
primary_ticks = (GET_TIMESTAMP() - now_nt);
#endif
/* Clear interrupt flag */
EXTI->PR |= EXTI_Line15;
++p_cnt;
}
__enable_irq();
}
RM0090, 12.3.6 Pending register (EXTI_PR):
This bit is cleared by programming it to ‘1’.
Thus, this code
/* Clear interrupt flag */
EXTI->PR |= EXTI_Line7;
Clears not only EXTI_Line7 but all pending interrupts because it reads EXTI-PR with 1 for all triggered interrupts, then OR bit EXTI_Line7 and writes all the 1-es back.
Use
/* Clear interrupt flag */
EXTI->PR = EXTI_Line7;

SPI not working on atmega328p

I'm trying to program an atmega328p, but the SPI bus isn't working. It's not sending any data over the bus.
My code is as follows:
#include <asf.h>
#include <stdio.h>
#include <main.h>
int main (void)
{
board_init();
SPI_MasterInit();
DDRD = (1<<DDD5);
while(1)
{
PORTB &= ~(1<<DDB2);
SPI_MasterTransmit(0xAB);
PORTB |= (1<<DDB2);
PORTD ^= (1<<DDD5);
}
}
void SPI_MasterInit(void)
{
/* Set MOSI and SCK output, all others input */
DDRB = (1<<5) || (1<<3) || (1<<2);
/* Enable SPI, Master, set clock rate fck/16 */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
void SPI_MasterTransmit(char cData)
{
/* Start transmission */
SPDR = cData;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)));
}
I'm trying to send some random data over the bus (0xAB) to check if the bus works properly so I can add further code. In the while loop I also set a pin.
On my scope image I see no data being send on the SPI pin MOSI and the CLK pin is also not sending information. PB5 (the pin I'm inverting everytime I try to send data) is working and has a period of about 20 microseconds.
I'm programming the Atmega328p through an Atmel ICE. The programming interface is also through SPI, I read somewhere that this might be an issue. I'm not completely sure.
Does anyone know what might be the problem?
My first guess was not setting SS pin (PB2) as output. It may cause switching to the slave mode almost randomly. But it seems to be set as an output (it's not stated in comments).
But after closer look to this expression, it's obvious it's not set at all:
DDRB = (1<<5) || (1<<3) || (1<<2);
There is a huge difference between logical or || and bitwise or |.

'Keyboard' was not declared in this scope - Arduino coding

I'm writing a simple program for an Arduino that will take the input of 2 buttons and output simulated keys for 2 different functions to use in Clone Hero.
Arduino editor (both online and local versions) spit out
'Keyboard' was not declared in this scope
The offline editor asks if Keyboard.h is included... Which it obviously is.
Any ideas why?
// Keyboard - Version: Latest
#include <Keyboard.h>
//btnWhammy is the button to replace whammy bar function
//btnSP is the button to replace star power activation
//Set Clone Hero to register j for whammy and k for star power
//declaring constant integers for the pins on the Arduino
const int btnWhammy = 2;
const int btnSP = 13;
//Declaring integers for the state of the button press
int btnWhammyState = 0;
int btnSPState = 0;
void setup() {
//Initialisation of the pins as inputs
pinMode(btnWhammy, INPUT);
pinMode(btnSP, INPUT);
}
void loop() {
//Setting the button states to the read of the digital pin (LOW is off, HIGH is on)
btnWhammyState = digitalRead(btnWhammy);
btnSPState = digitalRead(btnSP);
//If the whammy button is pressed, send 'j' to the keyboard, wait 100ms then release all keys
if (btnWhammyState == HIGH) {
Keyboard.press('j');
delay(100);
Keyboard.releaseAll();
}
//If the Star Power button is pressed, send 'k' to the keyboard, wait 100ms then release all keys
if (btnSPState == HIGH) {
Keyboard.press('k');
delay(100);
Keyboard.releaseAll();
}
}
This is a classic mistake -- you are probably compiling for a non-Leonardo board, like a Uno. The Keyboard.h library is not included because it is not present for the board you are compiling with.
I took your code and compiled it for Leonardo -- no issues. For Uno, I get the same error as you...
keyboard key was not declared in this scope i found a very simple and working trick of this problem just go to your main file where you declare all other keys that you are facing not declare
#include <DigiKeyboard.h>
#define KEY_UP_ARROW 0x52
#define KEY_DOWN_ARROW 0x51
#define KEY_LEFT_ARROW 0x50
#define KEY_RIGHT_ARROW 0x4F
#define KEY_TAB 0x2B

Programming Arduino with ADXL345 to raise interrupt on inactivity

I need to use a sparkfun breakout board ADXL345 to detect when my motor system has stopped vibrating. I am also using a Sparkfun RedBoard (Arduino uno).
Things I am doing to configure for this behavior:
enable INACTIVITY event
route INACTIVITY events to INT 1 (pin 2 on the RedBoard)
raise INACTIVITY interrupt without delay
set low threshold for INACTIVITY (rule out too high of a setting)
INACTIVITY considers all axes
clear interrupt data register
Having done all these things I do not receive interrupts after going from shaking the devise to setting it down.
//Add the SPI library so we can communicate with the ADXL345 sensor
#include <SPI.h>
//Assign the Chip Select signal to pin 10.
int CS=10;
//This is a list of some of the registers available on the ADXL345.
//To learn more about these and the rest of the registers on the ADXL345, read the datasheet!
char POWER_CTL = 0x2D; //Power Control Register
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32; //X-Axis Data 0
char DATAX1 = 0x33; //X-Axis Data 1
char DATAY0 = 0x34; //Y-Axis Data 0
char DATAY1 = 0x35; //Y-Axis Data 1
char DATAZ0 = 0x36; //Z-Axis Data 0
char DATAZ1 = 0x37; //Z-Axis Data 1
char THRESH_ACT = 0x24; // Activity threshold
char THRESH_INACT = 0x38; // Inactivity threshold to 3g
char TIME_INACT = 0x26; // time before raising interrupt
char INT_ENABLE = 0x2E; // Enabling the interrupt lines
char INT_MAP = 0x2F;
char ACT_INACT_CTL = 0x27; // mask byte for controlling
char INT_SOURCE = 0x30;
//This buffer will hold values read from the ADXL345 registers.
char values[10];
//These variables will be used to hold the x,y and z axis accelerometer values.
int x,y,z;
void setup(){
//Initiate an SPI communication instance.
SPI.begin();
//Configure the SPI connection for the ADXL345.
SPI.setDataMode(SPI_MODE3);
//Create a serial connection to display the data on the terminal.
Serial.begin(9600);
//Set up the Chip Select pin to be an output from the Arduino.
pinMode(CS, OUTPUT);
//Before communication starts, the Chip Select pin needs to be set high.
digitalWrite(CS, HIGH);
// Create an interrupt that will trigger when inactivity is detected
attachInterrupt(0, interruptHandler, RISING);
//Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
writeRegister(DATA_FORMAT, 0x01);
//Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
writeRegister(POWER_CTL, 0x08); //Measurement mode
// Send the inactivity && activity to PIN 1
// 0xF7 && 0xEF
writeRegister(INT_MAP,0xF7 && 0xEF);
// Set the inactivity threshold to 3g (0x38)
// writeRegister(THRESH_INACT,0x38);
writeRegister(THRESH_INACT,1);
// Raise the inact interrupt immediately after going below threshold
writeRegister(TIME_INACT,0);
// Map INACT event (only) to PIN 1
writeRegister(ACT_INACT_CTL, 0x0F);
// Enab le inactivity to generate interrupts
writeRegister(INT_ENABLE, 0x08);
readRegister(INT_SOURCE, 1, values); // Clear the INT_SOURCE register
Serial.println("Waiting for interrupt!");
}
void interruptHandler(){
// readRegister(INT_SOURCE, 1, values); // Clear the INT_SOURCE register
Serial.println("something raise an interrupt!");
}
void loop(){
//Reading 6 bytes of data starting at register DATAX0 will retrieve the x,y and z acceleration values from the ADXL345.
//The results of the read operation will get stored to the values[] buffer.
readRegister(DATAX0, 6, values);
//The ADXL345 gives 10-bit acceleration values, but they are stored as bytes (8-bits). To get the full value, two bytes must be combined for each axis.
//The X value is stored in values[0] and values[1].
x = ((int)values[1]<<8)|(int)values[0];
//The Y value is stored in values[2] and values[3].
y = ((int)values[3]<<8)|(int)values[2];
//The Z value is stored in values[4] and values[5].
z = ((int)values[5]<<8)|(int)values[4];
//Print the results to the terminal.
Serial.print(x, DEC);
Serial.print(',');
Serial.print(y, DEC);
Serial.print(',');
Serial.println(z, DEC);
delay(500);
}
//This function will write a value to a register on the ADXL345.
//Parameters:
// char registerAddress - The register to write a value to
// char value - The value to be written to the specified register.
void writeRegister(char registerAddress, char value){
//Set Chip Select pin low to signal the beginning of an SPI packet.
digitalWrite(CS, LOW);
//Transfer the register address over SPI.
SPI.transfer(registerAddress);
//Transfer the desired register value over SPI.
SPI.transfer(value);
//Set the Chip Select pin high to signal the end of an SPI packet.
digitalWrite(CS, HIGH);
}
//This function will read a certain number of registers starting from a specified address and store their values in a buffer.
//Parameters:
// char registerAddress - The register addresse to start the read sequence from.
// int numBytes - The number of registers that should be read.
// char * values - A pointer to a buffer where the results of the operation should be stored.
void readRegister(char registerAddress, int numBytes, char * values){
//Since we're performing a read operation, the most significant bit of the register address should be set.
char address = 0x80 | registerAddress;
//If we're doing a multi-byte read, bit 6 needs to be set as well.
if(numBytes > 1)address = address | 0x40;
//Set the Chip select pin low to start an SPI packet.
digitalWrite(CS, LOW);
//Transfer the starting register address that needs to be read.
SPI.transfer(address);
//Continue to read registers until we've read the number specified, storing the results to the input buffer.
for(int i=0; i<numBytes; i++){
values[i] = SPI.transfer(0x00);
}
//Set the Chips Select pin high to end the SPI packet.
digitalWrite(CS, HIGH);
}
Here is a tutorial, arduino library and example sketch. If you haven't run through something like this, might be worth a try starting with someone else's code that is working (maybe you've already done that).
In the example sketch from above, they are enabling interrupts in the code, they just don't seem to tie them into the Arduino's external interrupt system. Once you verify that the example code is working, you can call attachInterrupt() and abandon the polling approach (as you are doing in your example).

How can Arduino detect the state of an LED?

I'm working on a blinking/fading lights program for Arduino. I'm trying to build in some interactivity that's based on LED state and certain switch combinations. Consider: When a button is pressed, if an LED is ON I want to turn it off and if an LED is OFF I want to turn it on.
However, I've not been able to find any information about determining LED state. The closest was this question about Android, but I'm trying to find out if I can do this from the Arduino platform. Does anyone have any practical experience or advice?
It is absolutely OK to read output ports. That is
digitalWrite(LED_PORT, !digitalRead(LED_PORT));
will toggle the pin.
You might also want to consider the toggle library: http://playground.arduino.cc/Code/DigitalToggle
You have several options:
One, you can store the LED state in a boolean, and on button press, negate that and write it to the LED port:
void loop()
{
static int ledState = 0; // off
while (digitalRead(BUTTON_PIN) == 0)
; // wait for button press
ledState = !ledState;
digitalWrite(LED_PORT, ledState);
}
Two, if you don't mind accessing the ports of the AVR directly:
void init()
{
DDRD = 0x01; // for example: LED on port B pin 0, button on port B pin 1
PORTB = 0x00;
}
void loop()
{
while (PINB & 0x02 == 0)
; // loop until the button is pressed
PORTB ^= 0x01; // flip the bit where the LED is connected
}

Resources