Jump to content
Sign in to follow this  
RobG

LaunchPad, ADC, and LCD

Recommended Posts

It's time to play with ADC. I put together 2 examples, single channel and sequence of channels.Both use Vcc as reference.
In the first example value of ADC10MEM register is displayed and the same value converted to show actual voltage.
In the second example, 4 inputs are sampled, values are not converted, binary data is displayed (don't get fooled by the period.)

 

#include "msp430g2231.h"

#define sendData(data) send(data, 1)
#define sendInstruction(data) send(data, 0)
#define initDisplay() sendInstruction(0x3C); sendInstruction(0x0C); clearDisplay(); sendInstruction(0x06)
#define clearDisplay() sendInstruction(0x01); _delay_cycles(2000)
#define DATAPIN BIT6
#define CLOCKPIN BIT5
#define ENABLEPIN BIT4

const char charMap[10] = {
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39
};

void send(char data, char registerSelect);
void sendDataArray(char data[], char length);

char charIndex = 0;
char bitCounter = 0;

char charsToSend[5] = {0,0,0,0,0};

void main(void) {
WDTCTL = WDTPW + WDTHOLD;

BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;

_delay_cycles(100000);

P1OUT &= ~(CLOCKPIN + DATAPIN);
P1OUT |= ENABLEPIN;
P1DIR |= ENABLEPIN + CLOCKPIN + DATAPIN;

initDisplay();

ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE;
ADC10CTL1 = INCH_1;
ADC10AE0 |= BIT1;

CCTL0 = CCIE;
CCR0 = 12500;
TACTL = TASSEL_2 + MC_1 + ID_3;

_bis_SR_register(LPM0_bits + GIE);
}

// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void) {
clearDisplay();

charIndex = 0;
while(charIndex < 4) {
charsToSend[charIndex] = charMap[0];
charIndex++;
}

unsigned int binary = ADC10MEM;

charIndex = 3;
while(binary > 0) {
charsToSend[charIndex] = charMap[binary % 10];
binary /= 10;
charIndex--;
}
sendDataArray(charsToSend, 4);


charIndex = 0;
while(charIndex < 5) {
charsToSend[charIndex] = charMap[0];
charIndex++;
}
charsToSend[1] = 0x2E;

binary = ADC10MEM * 3.52;

charIndex = 4;
while(binary > 0) {
charsToSend[charIndex] = charMap[binary % 10];
binary /= 10;
charIndex--;
if(charIndex == 1)
charIndex--;
}
sendInstruction(0xC0);
sendDataArray(charsToSend, 5);
sendData(0x56);
}

// Timer A0 interrupt service routine
#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A (void) {
ADC10CTL0 |= ENC + ADC10SC;
}


void sendDataArray(char data[], char length) {
charIndex = 0;
while(charIndex < length) {
sendData(data[charIndex]);
charIndex++;
}
}

void send(char data, char registerSelect) {
bitCounter = 0;
while(bitCounter < 8) {
(data & BIT7) ? (P1OUT |= DATAPIN) : (P1OUT &= ~DATAPIN);
data <<= 1;
P1OUT |= CLOCKPIN;
P1OUT &= ~CLOCKPIN;
bitCounter++;
}
registerSelect ? (P1OUT |= DATAPIN) : (P1OUT &= ~DATAPIN);
P1OUT &= ~ENABLEPIN;
P1OUT |= ENABLEPIN;
}



#include "msp430g2231.h"

#define sendData(data) send(data, 1)
#define sendInstruction(data) send(data, 0)
#define initDisplay() sendInstruction(0x3C); sendInstruction(0x0C); clearDisplay(); sendInstruction(0x06)
#define clearDisplay() sendInstruction(0x01); _delay_cycles(2000)
#define DATAPIN BIT6
#define CLOCKPIN BIT5
#define ENABLEPIN BIT4

const char charMap[10] = {
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39
};
const char addressMap[4] = {
0x80, 0x88, 0xC0, 0xC8
};

void send(char data, char registerSelect);
void sendDataArray(char data[], char length);

char charIndex = 0;
char valueIndex = 0;
char bitCounter = 0;

char charsToSend[5] = {0,0,0,0,0};
int values[4] = {0,0,0,0};

void main(void) {
WDTCTL = WDTPW + WDTHOLD;

BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;

_delay_cycles(100000);

P1OUT &= ~(CLOCKPIN + DATAPIN);
P1OUT |= ENABLEPIN;
P1DIR |= ENABLEPIN + CLOCKPIN + DATAPIN;

initDisplay();

ADC10CTL1 = INCH_3 + CONSEQ_1;
ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON + ADC10IE;
ADC10DTC1 = 0x04;
ADC10AE0 |= 0x0F;

CCTL0 = CCIE;
CCR0 = 12500;
TACTL = TASSEL_2 + MC_1 + ID_3;

_bis_SR_register(LPM0_bits + GIE);
}

// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void) {

clearDisplay();
while (ADC10CTL1 & BUSY)
;

valueIndex = 0;
while(valueIndex < 4) {

charIndex = 0;
while(charIndex < 5) {
charsToSend[charIndex] = charMap[0];
charIndex++;
}

charsToSend[1] = 0x2E;

unsigned int binary = values[valueIndex];

charIndex = 4;
while(binary > 0) {
charsToSend[charIndex] = charMap[binary % 10];
binary /= 10;
charIndex--;
if(charIndex == 1)
charIndex--;
}
sendInstruction(addressMap[valueIndex]);
sendDataArray(charsToSend, 5);
valueIndex++;
}
}

// Timer A0 interrupt service routine
#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A (void) {

ADC10CTL0 &= ~ENC;
while (ADC10CTL1 & BUSY)
;
ADC10SA = (unsigned int)&values[0];
ADC10CTL0 |= ENC + ADC10SC;
}


void sendDataArray(char data[], char length) {
charIndex = 0;
while(charIndex < length) {
sendData(data[charIndex]);
charIndex++;
}
}

void send(char data, char registerSelect) {
bitCounter = 0;
while(bitCounter < 8) {
(data & BIT7) ? (P1OUT |= DATAPIN) : (P1OUT &= ~DATAPIN);
data <<= 1;
P1OUT |= CLOCKPIN;
P1OUT &= ~CLOCKPIN;
bitCounter++;
}
registerSelect ? (P1OUT |= DATAPIN) : (P1OUT &= ~DATAPIN);
P1OUT &= ~ENABLEPIN;
P1OUT |= ENABLEPIN;
}

Share this post


Link to post
Share on other sites

hi robG can you tell me the pins to where i can connect the LCD and also how to make it only 3 wires for the lcd. thanks 

quoting:

#define DATAPIN BIT6
#define CLOCKPIN BIT5
#define ENABLEPIN BIT4

of port 1.

 

Apparently the LCD @@RobG used is an SPI display, so you'l need an SPI display to do this, which will use 3 pins (MISO is not connected, since there is no data read from the display).

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...