Skip to content

Instantly share code, notes, and snippets.

@kizzlebot
Created March 24, 2014 13:17
Show Gist options
  • Save kizzlebot/9739863 to your computer and use it in GitHub Desktop.
Save kizzlebot/9739863 to your computer and use it in GitHub Desktop.
#include <msp430fg4618.h>
#include <msp430.h>
#include "Init_Interrupts.h"
int Init_Interrupts(unsigned int timerStop) {
// Clock Set up
CCTL0 = CCIE; // Set up Timer+Clock
TACTL = DEFAULT_CLOCK + DEFAULT_MODE; // [timerA -> ACLK][MODE: UP]
TACCR0 = timerStop; // TACCR0 is the upper limit of the timer = 32768
__enable_interrupt();
__bis_SR_register(LPM0 + GIE);
// LPM0 with interrupts enabled
return 0;
}
#ifndef INIT_INTERRUPTS_
#define INIT_INTERRUPTS_
#define DEFAULT_CLOCK TASSEL_1 // Default clock: ACLK
#define DEFAULT_MODE MC_1
int Init_Interrupts(unsigned int timerStop);
#endif
#include <msp430fg4618.h>
#include "UART_Ctrl.h"
#include "Init_Interrupts.h"
#include "LCD_Ctrl.h"
unsigned int asciiToValue(unsigned char ch);
unsigned char ch = 0x00 ;
unsigned int val = 0 ;
int main(void) {
volatile unsigned char a;
volatile unsigned char b; // volatile to prevent optimization
volatile unsigned char c;
volatile unsigned char d;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P2DIR |= BIT1;
P2OUT = 0x00 ;
Init_LCD();
Init_UART(); // Intialize UART for hyperterminal
Init_Interrupts(0xCCC);
}
unsigned int asciiToValue(unsigned char ch) {
if (ch < 0x3A && ch > 0x2F) return ch - 48;
else if (ch < 0x47 && ch > 0x40) return ch - 0x37;
else if (ch < 0x67 && ch > 0x60) return ch - 0x57;
return 0x00;
}
// Every tenth of a second: (1/10) = no.Ticks/CPUFREQ
#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A (void){
P2OUT ^= BIT1 ;
unsigned int a[2] ;
unsigned int b[2] ;
b[1] = asciiToValue(OUTA_UART(getValidHexIn()));
b[0] = asciiToValue(OUTA_UART(getValidHexIn()));
OUTA_UART(SPACE);
OUTA_UART(PLUS);
OUTA_UART(SPACE);
a[1] = asciiToValue(OUTA_UART(getValidHexIn()));
a[0] = asciiToValue(OUTA_UART(getValidHexIn()));
OUTA_UART(SPACE);
OUTA_UART(EQUAL);
OUTA_UART(SPACE);
OUTA_UART('0');
OUTA_UART('x');
writeSumValUART(a,b);
clearLCD();
writeValToLCD((((a[0]+b[0])/16)+(a[1]+b[1]))/16,2);
writeValToLCD((((a[0]+b[0])/16)+(a[1]+b[1]))%16,1);
writeValToLCD((a[0]+b[0])%16,0);
OUTA_UART(CR);
}
#include <msp430fg4618.h>
#include "LCD_Ctrl.h"
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
extern unsigned int characters[] = { 0x5f, 0x06, 0x6b, 0x2f, 0x36, 0x3d, 0x7d, 0x07, 0x7f, 0x37, 0x77, 0x7c, 0x59, 0x6E, 0x79, 0x71};
extern unsigned char *LCDSeg = (unsigned char *) &LCDM3;
//---------------------------------------------------------------------
// Initialize the LCD system
//---------------------------------------------------------------------
void Init_LCD(void) {
unsigned char *LCDSeg = (unsigned char *) &LCDM3;
/* Using the LCD A controller for the MSP430fg4618
* the pins of the LCD are memory mapped onto the mp430F4xxx
* memory bus and are accessed via LCDSeg[i] array */
/* <See page 260 of Davie's text>
* LCD_SIZE-4 only gives the 7 segment displays plus DP, and
* (colons are the same bit setting)*/
/* LCD_SIZE-4 only gives the 7 segment displays plus DP, and
* colons (colons / dp)
* Right most seven segment display is at LCDSeg[0]; */
// Clear LCD segment memory
int n = 0;
for (n = 0; n < LCD_SIZE; n++) {
/* initialize the segment memory to zero to clear the LCD
* writing a zero in the LCD memory location clears turns
* off the LCD segment
* Including all of the special characters*/
*(LCDSeg + n) = 0; // or LCDSeg[n]=0;
}
/* - Port 5 ports 5.2-5.4 are connected to com1, com2, com3 of LCD and
* com0 is fixed and already assigned
* - Need to assign com1 - com3 to port5 */
P5SEL = 0x1C; // BIT4 | BIT3 |BIT2 = 1 P5.4, P.3, P5.2 = 1
/* - Used the internal voltage for the LCD bit 4 = 0 (VLCDEXT=0)
* - Internal bias voltage set to 1/3 of Vcc, charge pump disabled,
* (page 26-25 of MSP430x4xx user manual) */
LCDAVCTL0 = 0x00;
/* LCDS28-LCDS0 pins LCDS0 = lsb and LCDS28 = MSB need LCDS4 through LCDS24
* from the experimenter board schematic the LCD uses S4-S24, S0-S3 are not used here.
* Only use up to S24 on the LCD 28-31 not needed.
* Also LCDACTL1 not required since not using S32 - S39 ( Davie's book page 260 )
* page 26-23 of MSP430x4xx user manual */
LCDAPCTL0 = 0x7E;
/* - The LCD uses the ACLK as the master clock as the scan rate for
* the display segments
* - The ACLK has been set to 32768 Hz with the external 327768 Hz crystal
* - Let's use scan frequency of 256 Hz (This is fast enough not to see the display flicker)
* or a divisor of 128
* - LCDFREQ division(3 bits), LCDMUX (2 bits), LCDSON segments on, Not used, LCDON LCD module on
* - 011 = freq /128, 11 = 4 mux's needed since the display uses for
* common inputs com0-com3, need to turn the LCD on LCDON = 1
* - LCDSON allows the segments to be blanked good for blinking but
* needs to be on to display the LCD segments LCDSON = 1
* - Bit pattern required = 0111 1101 = 0x7d (page 26-22 of MSP430x4xx user manual) */
LCDACTL = 0x7d;
}
void clearLCD(void){
int i = 0 ;
for (i = 0 ; i < LCD_SIZE ; i++ ) LCDSeg[i] = 0x00 ;
}
void scrollLCD(){
unsigned int fi = LCDSeg[6] ;
int i = 0 ;
for ( i = 0 ; i < 6; i++ ) {
LCDSeg[i+1] = LCDSeg[i];
LCDSeg[i] = 0x00 ;
}
LCDSeg[6] = fi ;
}
unsigned int writeValToLCD(unsigned int val, int seg){
LCDSeg[seg] = characters[val%16];
return val ;
}
#ifndef LCD_CTRL_H_
#define LCD_CTRL_H_
#define LCD_SIZE 11
extern unsigned int characters[16];
extern unsigned char *LCDSeg ;
void Init_LCD(void) ;
void clearLCD(void);
void scrollLCD(void);
unsigned int writeValToLCD(unsigned int val, int seg);
#endif /* LCD_CTRL_H_ */
#include <msp430fg4618.h>
#include <msp430.h>
#include "UART_Ctrl.h"
void Init_UART(void) {
P2SEL = 0x30; // transmit and receive to port 2 bits 4 and 5
UCA0CTL0 = 0; // 8 data, no parity 1 stop, uart, async
UCA0CTL1 = 0x41;
UCA0BR1 = 0; // upper byte of divider clock word
UCA0BR0 = 3; // clock divide from a clock to bit clock 32768/9600
UCA0MCTL = 0x06;
UCA0STAT = 0; // do not loop the transmitter back to the
UCA0CTL1 = 0x40;
IE2 = 0; // turn transmit interrupts off
}
unsigned char OUTA_UART(unsigned char A) {
do {
} while ((IFG2 & 0x02) == 0);
UCA0TXBUF = A;
return A;
}
unsigned char INCHAR_UART(void) {
do {
} while ((IFG2 & 0x01) == 0);
return (UCA0RXBUF);
}
unsigned char quickIn(){
if ((IFG2 & 0x01) != 0) return (UCA0RXBUF);
return 0x00 ;
}
unsigned char quickOut(unsigned char ch){
if ((IFG2 & 0x02) != 0 && ch != 0x00){
UCA0TXBUF = ch;
return ch;
}
return ch ;
}
unsigned char valueToAscii(unsigned int i){
if ( i < 10 && i >= 0 ) return i+0x30 ;
else if (i > 9 && i <= 15 ) return i+0x37 ;
}
void writeSumValUART(unsigned int a[2], unsigned int b[2]){
OUTA_UART(valueToAscii((((a[0]+b[0])/16)+(a[1]+b[1]))/16));
OUTA_UART(valueToAscii((((a[0]+b[0])/16)+(a[1]+b[1]))%16));
OUTA_UART(valueToAscii((a[0]+b[0])%16));
}
unsigned char getValidHexIn(void){
unsigned char a = 0x00;
// while havn't gotten a valid hex digit ( character 0-9 or character A-F or a-f )
while (1) {
// get a char
a = INCHAR_UART();
// If a is 0-9
if ((a > 0x2F && a < 0x3A) || (a > 0x40 && a < 0x47)) {
return a;
}
// elif a-f
else if (a > 0x60 && a < 0x67) {
return a - 0x20;
}
else continue ;
}
}
#ifndef UART_CTRL_H_
#define UART_CTRL_H_
#define SPACE 0x20
#define LF 0x0A
#define CR 0x0D
#define PLUS 0x2B
#define EQUAL 0x3D
void Init_UART(void) ;
unsigned char OUTA_UART(unsigned char A) ;
unsigned char INCHAR_UART(void);
unsigned char getValidHexIn(void);
unsigned char valueToAscii(unsigned int i);
void writeSumValUART(unsigned int a[2], unsigned int b[2]);
unsigned char quickOut(unsigned char ch);
unsigned char quickIn();
#endif /* UART_CTRL_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment