Experiments manual as per NPTEL series¶
List of Experiments¶
Experiment 1 - LunchBox Hello LED¶
Description¶
This example code provides the functions and register settings to switch ON the LED present on LunchBox.
Hardware needed¶
None
Connections¶
None
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <msp430.h>
/*@brief entry point for the code*/
int main(void) {
WDTCTL = WDTPW | WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
//P1DIR |= BIT7;
P1DIR |= 0x80; // Set P1.7 to output direction
//P1OUT |= BIT7;
//P1OUT |= 0x80; // Set P1.7 to HIGH voltage
//P1OUT &=~ BIT7;
P1OUT &=~ 0x80; // Set P1.7 to LOW voltage
return 0;
}
|
Experiment 2 - LunchBox Hello Blink (using MSP430.h)¶
Description¶
This example code provides the functions and register settings to toggle the LED present on LunchBox.
Course link¶
Covered in lecture number - Lecture 21
Hardware needed¶
None
Schematic image¶
NA
Connections¶
None
Output¶
On board user led starts blinking
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <msp430.h>
/*@brief entry point for the code*/
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
//P1DIR |= BIT7;
P1DIR |= 0x80; // P1.7 (Red LED)
while(1)
{
volatile unsigned long i;
//P1OUT |= BIT7;
P1OUT |= 0x80; //Red LED -> ON
for(i = 0; i<10000; i++); //delay
//P1OUT &=~ BIT7;
P1OUT &= ~0x80; //Red LED -> OFF
for(i = 0; i<10000; i++); //delay
}
}
|
Experiment 2 - LunchBox Hello Blink (using pointers)¶
Description¶
This example code provides the functions and register settings to toggle the LED present on LunchBox. In this example, we have used direct memory mapped address of register to perform the blinking of led. We can rephrase this statement by saying no #include <MSP430.h> header is used, which contains mapping for register names and addresses. Comments are provided to see the correlation of addresses to register names. These addresses can be verfied from the MSP430G2553 datasheet.
Course link¶
Covered in lecture number - Lecture 21
Hardware needed¶
NA
Schematic image¶
NA
Connections¶
NA
Output¶
On board user led starts blinking
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | //#include <msp430.h>
/*@brief entry point for the code*/
void main(void)
{
// WDTCTL = WDTPW | WDTHOLD;
(*(volatile unsigned int *) 0x0120) = 0x5A00 | 0x0080;
/* ! Stop Watchdog (Not recommended for code in production
and devices working in field)
*/
//P1DIR |= BIT7;
(*(volatile unsigned char *) 0x0022) |= 0x80;
// P1.7 (Red LED)
volatile unsigned long i;
while (1)
{
//P1OUT |= BIT7;
// P1OUT |= 0x80; //Red LED -> ON
(*(volatile unsigned char *) 0x0021) |= 0x80;
//Red LED -> ON
for (i = 0; i < 10000; i++)
; //delay
//P1OUT &=~ BIT7;
//P1OUT &= ~0x80; //Red LED -> OFF
(*(volatile unsigned char *) 0x0021) &= ~0x80;
for (i = 0; i < 10000; i++)
; //delay
}
}
|
Experiment 3 - LunchBox Hello Switch¶
Description¶
This example code provides the functions and register settings for toggling the state of onboard LED with switch present on LunchBox.
Hardware needed¶
None
Connections¶
None
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <msp430.h>
#define SW BIT3 // Switch -> P1.3 (On-board Switch, Pull-Up configuration)
#define LED BIT7 // Red LED -> P1.7 (On-Board LED, Active High Configuration)
/*@brief entry point for the code*/
void main(void) {
WDTCTL = WDTPW | WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
P1DIR |= LED; // Set LED pin -> Output
P1DIR &= ~SW; // Set SW pin -> Input
while(1)
{
if(!(P1IN & SW)) // If SW is Pressed
{
while(!(P1IN & SW)); // Wait till SW Released
P1OUT ^= LED; // Toggle LED
}
}
}
|
Experiment 4 - LunchBox Debouncing the Switch¶
Description¶
This example code is similar to that of LunchBox Hello Switch. The only difference is that in this code Switch bounces are ignored.
Hardware needed¶
None
Connections¶
None
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <msp430.h>
#define SW BIT3 // Switch -> P1.3
#define LED BIT7 // Red LED -> P1.7
/*@brief entry point for the code*/
void main(void) {
WDTCTL = WDTPW | WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
P1DIR |= LED; // Set LED pin -> Output
P1DIR &= ~SW; // Set SW pin -> Input
while(1)
{
if(!(P1IN & SW)) // If SW is Pressed
{
__delay_cycles(20000); // Wait 20ms to debounce
while(!(P1IN & SW)); // Wait till SW Released
__delay_cycles(20000); // Wait 20ms to debounce
P1OUT ^= LED; // Toggle LED
}
}
}
|
Experiment 5 - LunchBox Hello Switch Pull Down¶
Description¶
This example code provides the functions and register settings for toggling the state of LED on LunchBox with an external switch connected with a Pull Down resistor.
Hardware needed¶
Push Button Switch, Resistor (1 kohm), Breadboard, Connecting Wires
Connections¶
One end of Switch is connected to Vcc (3.3 V) and another end is at pin 1.4 of LunchBox. This pin is also connected to ground through a Pull Down resistor.
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <msp430.h>
#define SW BIT4 // Switch -> P1.4 (External Switch, Pull-Down configuration)
#define LED BIT7 // Red LED -> P1.7 (External Switch, Active-High configuration)
/*@brief entry point for the code*/
void main(void) {
WDTCTL = WDTPW | WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
P1DIR |= LED; // Set LED pin -> Output
P1DIR &= ~SW; // Set SW pin -> Input
P1REN |= SW; // Enable Resistor for SW pin
P1OUT &=~ SW; // Select Pull down for SW pin
while(1)
{
if(P1IN & SW) // If SW is Pressed
{
__delay_cycles(20000); // Wait 20ms to debounce
while((P1IN & SW)); // Wait till SW Released
__delay_cycles(20000); // Wait 20ms to debounce
P1OUT ^= LED; // Toggle LED
}
}
}
|
Experiment 6 - LunchBox HelloSwitch Pull Up¶
Description¶
This example code provides the functions and register settings for toggling the state of LED on LunchBox with an external switch connected with a Pull Up resistor.
Hardware needed¶
Push Button Switch, Resistor (1 kohm), Breadboard, Connecting Wires.
Connections¶
One end of Switch is connected to Ground and another end is at pin 1.4 of LunchBox. This pin is also connected to Vcc (3.3 V) through a Pull Up resistor.
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <msp430.h>
#define SW BIT4 // Switch -> P1.4 (External Switch, Pull-Up configuration)
#define LED BIT7 // Red LED -> P1.7 (External Switch, Active-High configuration)
/*@brief entry point for the code*/
void main(void) {
WDTCTL = WDTPW | WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
P1DIR |= LED; // Set LED pin -> Output
P1DIR &= ~SW; // Set SW pin -> Input
P1REN |= SW; // Enable Resistor for SW pin
P1OUT |= SW; // Select Pull Up for SW pin
while(1)
{
if(!(P1IN & SW)) // If SW is Pressed
{
__delay_cycles(20000); // Wait 20ms to debounce
while(!(P1IN & SW)); // Wait till SW Released
__delay_cycles(20000); // Wait 20ms to debounce
P1OUT ^= LED; // Toggle LED
}
}
}
|
Experiment 7 - LunchBox HelloClock¶
Description¶
This example code provides the functions and register settings to toggle the LED present on LunchBox.
Hardware needed¶
None
Connections¶
None
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #include <msp430.h>
#define LED BIT7 // Led Pin
#define SW1 BIT3 // 1.5Khz
#define SW2 BIT4 // 3Khz
#define SW3 BIT5 // 12Khz
volatile unsigned int i; // Volatile to prevent removal
/**
* @brief
* Function to take input from 3 switches and change CPU clock accordingly
**/
void switch_input()
{
if(!(P1IN & SW1)) // If SW1 is Pressed
{
__delay_cycles(2000); // Wait 20ms to debounce
while(!(P1IN & SW1)); // Wait till SW Released
__delay_cycles(2000); // Wait 20ms to debounce
BCSCTL2 &=~ (BIT5 + BIT4); //Reset VLO divider
BCSCTL2 |= (BIT5 + BIT4); //VLO = 12kHz/8 = 1.5kHz
}
if(!(P1IN & SW2)) // If SW is Pressed
{
__delay_cycles(2000); // Wait 20ms to debounce
while(!(P1IN & SW2)); // Wait till SW Released
__delay_cycles(2000); // Wait 20ms to debounce
BCSCTL2 &=~ (BIT5 + BIT4); //Reset VLO divider
BCSCTL2 |= (BIT4); //VLO = 12kHz/4 = 3kHz
}
if(!(P1IN & SW3)) // If SW is Pressed
{
__delay_cycles(2000); // Wait 20ms to debounce
while(!(P1IN & SW3)); // Wait till SW Released
__delay_cycles(2000); // Wait 20ms to debounce
BCSCTL2 &=~ (BIT5 + BIT4); //VLO = 12kHz/1 = 12kHz
}
}
/**
* @brief
* These settings are wrt enabling GPIO on Lunchbox
**/
void register_settings_for_GPIO()
{
P1DIR |= LED; //P1.7 is set as Output
P1DIR &=~ (SW1 + SW2 + SW3); //P1.3, P1.4, P1.5 are set as Input
}
/**
* @brief
* These settings are w.r.t enabling VLO on Lunch Box
**/
void register_settings_for_VLO()
{
BCSCTL3 |= LFXT1S_2; // LFXT1 = VLO
do{
IFG1 &= ~OFIFG; // Clear oscillator fault flag
for (i = 50000; i; i--); // Delay
} while (IFG1 & OFIFG); // Test osc fault flag
BCSCTL2 |= SELM_3; // MCLK = VLO
}
/**
* main.c
*/
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
register_settings_for_VLO(); //Register settings for VLO
register_settings_for_GPIO(); //Register settings for GPIO
while(1)
{
switch_input(); //Switch Input
P1OUT ^= LED; //LED Toggle
for (i = 100; i > 0; i--);
}
}
|
Experiment 8 - LunchBox HelloResetSource¶
Description¶
This example code provides the functions and register settings to toggle the LED present on LunchBox.
Hardware needed¶
None
Connections¶
None
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #include <msp430.h>
#define LED1 BIT0 // LED to indicate that main() has started
#define LED2 BIT1 // LED to indicate that program is executing
#define LEDA BIT2 // To indicate Power On Reset
#define LEDB BIT3 // To indicate Watch dog Reset
#define LEDC BIT4 // To indicate External Reset (by reset switch)
/**
* @brief
* These settings are wrt enabling GPIO on Lunchbox
**/
void register_settings_for_GPIO()
{
P1DIR |= (LED1 + LED2 + LEDA + LEDB + LEDC); // P1.0 to P1.4 as output
P1OUT |= LED1; // LED1 representing main() is set
P1OUT &=~ (LED2 + LEDA + LEDB + LEDC); // Turning all other LEDs OFF
}
/**
* @brief
* These settings are w.r.t check source of reset on Lunch Box
**/
void checking_reset_source()
{
if (IFG1 & PORIFG) // Check for Power on Reset flag (POR)
{
P1OUT|= LEDA; // Turning LEDA On
P1OUT &= ~LEDB; // Turning LEDB Off
P1OUT &= ~LEDC; // Turning LEDC Off
/* Settings for Watchdog Timer register
Giving Watchdog Timer Password
Clearing Watchdog counter to 0000h
Watchdog Source select --> ACLK
Watchdog Timer interval set to generate watchdog reset at 2 secs
*/
WDTCTL = (WDTPW + WDTCNTCL + WDTSSEL + WDTIS0);
IFG1 &=~ PORIFG; // Clearing Power on Reset flag
}
else if (IFG1 & RSTIFG) // Check for External Reset flag (RST)
{
P1OUT|= LEDC; // Turning LEDC On
P1OUT &= ~LEDA; // Turning LEDA Off
P1OUT &= ~LEDB; // Turning LEDB Off
/* Settings Watchdog Timer register
Giving Watchdog Timer Password
Clearing Watchdog counter to 0000h
Watchdog Source select --> ACLK
Watchdog Timer interval set to generate watchdog reset at 2 secs
*/
WDTCTL = (WDTPW + WDTCNTCL + WDTSSEL + WDTIS0);
IFG1 &=~ RSTIFG; // Clearing External Reset flag
}
else if (IFG1 & WDTIFG) // Check for Watch Dog Reset flag (WDT)
{
P1OUT|= LEDB; // Turning LEDB On
P1OUT &= ~LEDA; // Turning LEDA Off
P1OUT &= ~LEDC; // Turning LEDC Off
IFG1 &=~ PORIFG; // Clearing Power on Reset flag
IFG1 &=~ RSTIFG; // Clearing External Reset flag
}
}
/*@brief entry point for the code*/
void main(void)
{
WDTCTL = WDTPW | WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
volatile unsigned int i;
BCSCTL1 |= DIVA_3; // Dividing ACLK by 8, 32768Hz/8 = 4096Hz
register_settings_for_GPIO();
/* This loop checks for Oscillator fault flag to reset means
it delays execution until external crystal is Power On */
do
{
IFG1 &= ~OFIFG; // Clear oscillator fault flag
for (i = 10000; i ; i--); // Delay, minimum value of i = 5000
} while (IFG1 & OFIFG); // Test osc fault flag
checking_reset_source();
while(1)
{
P1OUT ^= LED2; // Toggle LED
__delay_cycles(1000000);
}
}
|
Experiment 9 - LunchBox HelloInterrupt¶
Description¶
This example code constitutes functions and register settings for toggling the state of onboard LED with onboard switch having interrupt on rising edge on Lunchbox.
Hardware needed¶
None
Connections¶
None
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include <msp430.h>
#define SW BIT3 // Switch -> P1.3
#define RED BIT7 // Red LED -> P1.7
/*@brief entry point for the code*/
void main(void) {
WDTCTL = WDTPW | WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
P1DIR |= RED; // Set LED pin -> Output
P1DIR &= ~SW; // Set SW pin -> Input
P1REN |= SW; // Enable Resistor for SW pin
P1OUT |= SW; // Select Pull Up for SW pin
P1IES &= ~SW; // Select Interrupt on Rising Edge
P1IE |= SW; // Enable Interrupt on SW pin
__bis_SR_register(GIE); // Enable CPU Interrupt
while(1);
}
/*@brief entry point for switch interrupt*/
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
if(P1IFG & SW) // If SW is Pressed
{
P1OUT ^= RED; // Toggle RED LED
volatile unsigned long i;
for(i = 0; i<10000; i++); //delay
P1IFG &= ~SW; // Clear SW interrupt flag
}
}
|
Experiment 10 - LunchBox HelloInterrupt Rising¶
Description¶
This example code provides the functions and register settings for toggling the state of onboard LED with an external switch having interrupt on rising edge.
Hardware needed¶
Push Button Switch, Resistor (1 kohm), Breadboard, Connecting wires.
Connections¶
One end of switch is connected to Ground and another end is connected to pin 1.4 of Lunchbox. This pin is also connected to Vcc (3.3 V) through a pull-up resistor.
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <msp430.h>
#define SW BIT4 // Switch -> P1.4
#define RED BIT7 // Red LED -> P1.7
/*@brief entry point for the code*/
void main(void) {
WDTCTL = WDTPW | WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
P1DIR |= RED; // Set LED pin -> Output
P1DIR &= ~SW; // Set SW pin -> Input
P1REN |= SW; // Enable Resistor for SW pin
P1OUT |= SW; // Select Pull Up for SW pin
P1IES &= ~SW; // Select Interrupt on Rising Edge
P1IE |= SW; // Enable Interrupt on SW pin
__bis_SR_register(GIE); // Enable CPU Interrupt
while(1);
}
/*@brief entry point for switch interrupt*/
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
if(P1IFG & SW) // If SW is Pressed
{
P1OUT ^= RED; // Toggle RED LED
P1IFG &= ~SW; // Clear SW interrupt flag
}
}
|
Experiment 11 - LunchBox Hello Interrupt on Falling Edge¶
Description¶
This example code provides the functions and register settings for toggling the state of onboard LED with an external switch having interrupt on falling edge.
Hardware needed¶
Push Button Switch, Resistor (1 kohm), Breadboard, Connecting wires.
Connections¶
One end of switch is connected to Ground and another end is connected to pin 1.4 of Lunchbox. This pin is also connected to Vcc (3.3 V) through a pull-up resistor.
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <msp430.h>
#define SW BIT4 // Switch -> P1.4
#define RED BIT7 // Red LED -> P1.7
/*@brief entry point for the code*/
void main(void) {
WDTCTL = WDTPW | WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
P1DIR |= RED; // Set LED pin -> Output
P1DIR &= ~SW; // Set SW pin -> Input
P1REN |= SW; // Enable Resistor for SW pin
P1OUT |= SW; // Select Pull Up for SW pin
P1IES |= SW; // Select Interrupt on Falling Edge
P1IE |= SW; // Enable Interrupt on SW pin
__bis_SR_register(GIE); // Enable CPU Interrupt
while(1);
}
/*@brief entry point for switch interrupt*/
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
if(P1IFG & SW) // If SW is Pressed
{
P1OUT ^= RED; // Toggle RED LED
P1IFG &= ~SW; // Clear SW interrupt flag
}
}
|
Experiment 12 - LunchBox Hello Seven Segment Display¶
Description¶
This example code provides function and register settings for increasing count on every press of external switch. The count will increase from 0 to F (in Hexadecimal).
Hardware needed¶
Single Seven Segment Display (SSD) - Common Cathode, Resistors - 270 ohm (quanitity - 8), 1 kohm (quanitity - 1), Toggle Push Button Switch, Connecting wires.
Connections¶
One end of switch is connected to Ground and another end is connected to pin 2.3 of Lunchbox, this pin is also connected to Vcc (3.3 V) through a pull-up resistor. Ground pin of SSD is connected to Ground of LunchBox, pins a,b,c,d,e,f,g,decimal of SSD are connected to pins 1.0 to 1.7 of LunchBox respectively through resistor of 270 ohm.
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include <msp430.h>
#define SW BIT3 // Switch -> P2.3
// Define Pin Mapping of 7-segment Display
// Segments are connected to P1.0 - P1.7
#define SEG_A BIT0
#define SEG_B BIT1
#define SEG_C BIT2
#define SEG_D BIT3
#define SEG_E BIT4
#define SEG_F BIT5
#define SEG_G BIT6
#define SEG_DP BIT7
// Define each digit according to truth table
#define D0 (SEG_A + SEG_B + SEG_C + SEG_D + SEG_E + SEG_F)
#define D1 (SEG_B + SEG_C)
#define D2 (SEG_A + SEG_B + SEG_D + SEG_E + SEG_G)
#define D3 (SEG_A + SEG_B + SEG_C + SEG_D + SEG_G)
#define D4 (SEG_B + SEG_C + SEG_F + SEG_G)
#define D5 (SEG_A + SEG_C + SEG_D + SEG_F + SEG_G)
#define D6 (SEG_A + SEG_C + SEG_D + SEG_E + SEG_F + SEG_G)
#define D7 (SEG_A + SEG_B + SEG_C)
#define D8 (SEG_A + SEG_B + SEG_C + SEG_D + SEG_E + SEG_F + SEG_G)
#define D9 (SEG_A + SEG_B + SEG_C + SEG_D + SEG_F + SEG_G)
#define DA (SEG_A + SEG_B + SEG_C + SEG_E + SEG_F + SEG_G)
#define DB (SEG_C + SEG_D + SEG_E + SEG_F + SEG_G)
#define DC (SEG_A + SEG_D + SEG_E + SEG_F)
#define DD (SEG_B + SEG_C + SEG_D + SEG_E + SEG_G)
#define DE (SEG_A + SEG_D + SEG_E + SEG_F + SEG_G)
#define DF (SEG_A + SEG_E + SEG_F + SEG_G)
// Define mask value for all digit segments except DP
#define DMASK ~(SEG_A + SEG_B + SEG_C + SEG_D + SEG_E + SEG_F + SEG_G)
// Store digits in array for display
const unsigned int digits[16] = {D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, DA, DB, DC, DD, DE, DF};
volatile unsigned int i = 0;
/*@brief entry point for the code*/
void main(void) {
WDTCTL = WDTPW | WDTHOLD; //! Stop Watch dog (Not recommended for code in production and devices working in field)
// Initialize 7-segment pins as Output
P1DIR |= (SEG_A + SEG_B + SEG_C + SEG_D + SEG_E+ SEG_F + SEG_G + SEG_DP);
P2DIR &= ~SW; // Set SW pin -> Input
while(1)
{
if(!(P2IN & SW)) // If SW is Pressed
{
__delay_cycles(20000); //Delay to avoid Switch Bounce
while(!(P2IN & SW)); // Wait till SW Released
i++; //Increment count
if(i>15)
{
i=0;
}
}
P1OUT = (P1OUT & DMASK) + digits[i]; // Display current digit
}
}
|
Experiment 13 - LunchBox Hello Low Power Mode (LPM)¶
Description¶
This example code provides the functions and register settings for operating MSP430 controller in Low Power Mode. In this example code state of onboard LED on LunchBox is toggled with onboard switch having interrupt on rising edge while running the microcontroller in Low Power Mode.
Hardware needed¶
None
Connections¶
None
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <msp430.h>
#define SW BIT3 // Switch -> P1.3
#define RED BIT7 // Red LED -> P1.7
/*@brief entry point for the code*/
void main(void) {
WDTCTL = WDTPW | WDTHOLD; //! Stop Watch dog (Not recommended for code in production and devices working in field)
P1DIR |= RED; // Set LED pin -> Output
P1DIR &= ~SW; // Set SW pin -> Input
P1REN |= SW; // Enable Resistor for SW pin
P1OUT |= SW; // Select Pull Up for SW pin
P1OUT &= ~RED; // Turn RED LED off
P1IES &= ~SW; // Select Interrupt on Rising Edge
P1IE |= SW; // Enable Interrupt on SW pin
while(1)
{
//__bis_SR_register(GIE); // Enable CPU Interrupt
__bis_SR_register(LPM4_bits + GIE); // Enter LPM4 and Enable CPU Interrupt
P1OUT ^= RED; // Toggle RED LED
}
}
/*@brief entry point for switch interrupt*/
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
__bic_SR_register_on_exit(LPM4_bits + GIE); // Exit LPM4 on return to main
P1IFG &= ~SW; // Clear SW interrupt flag
}
|
Experiment 14 - LunchBox HelloLCD¶
Description¶
This example code provides the functions and register settings to toggle the LED present on LunchBox.
Hardware needed¶
None
Connections¶
None
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | #include <msp430.h>
#include <inttypes.h>
#define CMD 0
#define DATA 1
#define LCD_OUT P1OUT
#define LCD_DIR P1DIR
#define D4 BIT4
#define D5 BIT5
#define D6 BIT6
#define D7 BIT7
#define RS BIT2
#define EN BIT3
/**
*@brief Delay function for producing delay in 0.1 ms increments
*@param t milliseconds to be delayed
*@return void
**/
void delay(uint16_t t)
{
uint16_t i;
for(i=t; i > 0; i--)
__delay_cycles(100);
}
/**
*@brief Function to pulse EN pin after data is written
*@return void
**/
void pulseEN(void)
{
LCD_OUT |= EN; // Giving a falling edge at EN pin
delay(1);
LCD_OUT &= ~EN;
delay(1);
}
/**
*@brief Function to write data/command to LCD
*@param value Value to be written to LED
*@param mode Mode -> Command or Data
*@return void
**/
void lcd_write(uint8_t value, uint8_t mode)
{
if(mode == CMD)
LCD_OUT &= ~RS; // Set RS -> LOW for Command mode
else
LCD_OUT |= RS; // Set RS -> HIGH for Data mode
LCD_OUT = ((LCD_OUT & 0x0F) | (value & 0xF0)); // Write high nibble first
pulseEN();
delay(1);
LCD_OUT = ((LCD_OUT & 0x0F) | ((value << 4) & 0xF0)); // Write low nibble next
pulseEN();
delay(1);
}
/**
*@brief Function to print a string on LCD
*@param *s pointer to the character to be written.
*@return void
**/
void lcd_print(char *s)
{
while(*s)
{
lcd_write(*s, DATA);
s++;
}
}
/**
*@brief Function to move cursor to desired position on LCD
*@param row Row Cursor of the LCD
*@param col Column Cursor of the LCD
*@return void
**/
void lcd_setCursor(uint8_t row, uint8_t col)
{
const uint8_t row_offsets[] = { 0x00, 0x40};
lcd_write(0x80 | (col + row_offsets[row]), CMD);
delay(1);
}
/**
*@brief Initialize LCD
**/
void lcd_init()
{
LCD_DIR |= (D4+D5+D6+D7+RS+EN);
LCD_OUT &= ~(D4+D5+D6+D7+RS+EN);
delay(150); // Wait for power up ( 15ms )
lcd_write(0x33, CMD); // Initialization Sequence 1
delay(50); // Wait ( 4.1 ms )
lcd_write(0x32, CMD); // Initialization Sequence 2
delay(1); // Wait ( 100 us )
// All subsequent commands take 40 us to execute, except clear & cursor return (1.64 ms)
lcd_write(0x28, CMD); // 4 bit mode, 2 line
delay(1);
lcd_write(0x0C, CMD); // Display ON, Cursor OFF, Blink OFF
delay(1);
lcd_write(0x01, CMD); // Clear screen
delay(20);
lcd_write(0x06, CMD); // Auto Increment Cursor
delay(1);
lcd_setCursor(0,0); // Goto Row 1 Column 1
}
/*@brief entry point for the code*/
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
lcd_init(); // Initialising LCD
lcd_setCursor(0,1); // Cursor position (0,1)
lcd_print("Hello Embedded"); // Print
lcd_setCursor(1,5); // Cursor position (1,3)
lcd_print("Systems!"); // Print
while(1);
}
|
Experiment 15 - LunchBox Hello LCD With Custom Character¶
Description¶
This example code provides the functions and register settings to toggle the LED present on LunchBox.
Hardware needed¶
None
Connections¶
None
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | #include <msp430.h>
#include <inttypes.h>
#define CMD 0
#define DATA 1
#define LCD_OUT P1OUT
#define LCD_DIR P1DIR
#define D4 BIT4
#define D5 BIT5
#define D6 BIT6
#define D7 BIT7
#define RS BIT2
#define EN BIT3
#define SW BIT3
#define LCD_SETCGRAMADDR 0x40
//Heart character
uint8_t heart[8] = {
0x00,
0x0A,
0x1F,
0x1F,
0x1F,
0x0E,
0x04,
0x00
};
//
/**
*@brief Delay function for producing delay in 0.1 ms increments
*@param t milliseconds to be delayed
*@return void
**/
void delay(uint16_t t)
{
uint16_t i;
for(i=t; i > 0; i--)
__delay_cycles(100);
}
/**
*@brief Function to pulse EN pin after data is written
*@return void
**/
void pulseEN(void)
{
LCD_OUT |= EN; // Giving a falling edge at EN pin
delay(1);
LCD_OUT &= ~EN;
delay(1);
}
/**
*@brief Function to write data/command to LCD
*@param value Value to be written to LED
*@param mode Mode -> Command or Data
*@return void
**/
void lcd_write(uint8_t value, uint8_t mode)
{
if(mode == CMD)
LCD_OUT &= ~RS; // Set RS -> LOW for Command mode
else
LCD_OUT |= RS; // Set RS -> HIGH for Data mode
LCD_OUT = ((LCD_OUT & 0x0F) | (value & 0xF0)); // Write high nibble first
pulseEN();
delay(1);
LCD_OUT = ((LCD_OUT & 0x0F) | ((value << 4) & 0xF0)); // Write low nibble next
pulseEN();
delay(1);
}
/**
*@brief Function to print a string on LCD
*@param *s pointer to the character to be written.
*@return void
**/
void lcd_print(char *s)
{
while(*s)
{
lcd_write(*s, DATA);
s++;
}
}
/**
*@brief Function to move cursor to desired position on LCD
*@param row Row Cursor of the LCD
*@param col Column Cursor of the LCD
*@return void
**/
void lcd_setCursor(uint8_t row, uint8_t col)
{
const uint8_t row_offsets[] = { 0x00, 0x40};
lcd_write(0x80 | (col + row_offsets[row]), CMD);
delay(1);
}
/**
*@brief Allows us to fill the first 8 CGRAM locations with custom characters
*@param location Row Cursor of the LCD
*@param charmap Column Cursor of the LCD
*@return void
**/
void lcd_createChar(uint8_t location, uint8_t charmap[]) {
location &= 0x7; // we only have 8 locations 0-7
lcd_write(LCD_SETCGRAMADDR | (location << 3), CMD);
int i = 0;
for (i=0; i<8; i++) {
lcd_write(charmap[i], DATA);
}
}
/**
*@brief Initialize LCD
**/
void lcd_init()
{
LCD_DIR |= (D4+D5+D6+D7+RS+EN);
LCD_OUT &= ~(D4+D5+D6+D7+RS+EN);
delay(150); // Wait for power up ( 15ms )
lcd_write(0x33, CMD); // Initialization Sequence 1
delay(50); // Wait ( 4.1 ms )
lcd_write(0x32, CMD); // Initialization Sequence 2
delay(1); // Wait ( 100 us )
// All subsequent commands take 40 us to execute, except clear & cursor return (1.64 ms)
lcd_write(0x28, CMD); // 4 bit mode, 2 line
delay(1);
lcd_write(0x0C, CMD); // Display ON, Cursor OFF, Blink OFF
delay(1);
lcd_write(0x06, CMD); // Auto Increment Cursor
delay(1);
lcd_write(0x01, CMD); // Clear screen
delay(20);
lcd_setCursor(0,0); // Goto Row 1 Column 1
}
/*@brief entry point for the code*/
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
uint8_t count = 0;
P2DIR &=~ SW;
lcd_init(); // Initialising LCD
lcd_createChar(0, heart); // Creating Custom Character
lcd_setCursor(0,1); // Cursor position (0,1)
lcd_print("Hello Embedded"); // Print
lcd_setCursor(1,4); // Cursor position (1,3)
lcd_print("Systems"); // Print
while(1)
{
if(!(P2IN & SW)) // If SW is Pressed
{
__delay_cycles(20000); // Wait 20ms to debounce
while(!(P2IN & SW)); // Wait till SW Released
__delay_cycles(20000); // Wait 20ms to debounce
switch(count)
{
case 0:
{
lcd_setCursor(1,3); // Cursor position (1,3)
lcd_write(0x00, DATA); // Printing Custom Char (Heart)
lcd_setCursor(1,11); // Cursor position (1,11)
lcd_write(0x00, DATA); // Printing Custom Char (Heart)
count = 1;
break;
}
case 1:
{
lcd_setCursor(1,3); // Cursor position (1,3)
lcd_write(0x20, DATA); // Printing Space
lcd_setCursor(1,11); // Cursor position (1,11)
lcd_write(0x20, DATA); // Printing Space
count = 0;
break;
}
}
}
}
}
|
Experiment 16 - LunchBox Hello Timer¶
Description¶
This example code provides function and register settings for generating Timer interrupt at 1 second. Using these settings, an eight bit binary up counter implementation has been represented on eight LEDs.
Hardware needed¶
Eight LEDs, Eight Resistors, Breadboard, Connecting Wires.
Connections¶
Port 1 pins of Lunchbox are connected to Eight LEDs via resistors in series. Pin 1.0 will represent LSB and Pin 1.7 will represent MSB of that Binary up counter.
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #include <msp430.h>
#include <inttypes.h>
volatile char count = 0;
volatile unsigned int i;
/**
* @brief
* These settings are wrt enabling GPIO on Lunchbox
**/
void register_settings_for_GPIO()
{
P1DIR |= 0xFF; //P1.0 to P1.7 are set as Output
P1OUT |= 0x00; //Initially they are set to logic zero
}
/**
* @brief
* These settings are w.r.t enabling TIMER0 on Lunch Box
**/
void register_settings_for_TIMER0()
{
CCTL0 = CCIE; // CCR0 interrupt enabled
TACTL = TASSEL_1 + MC_1; // ACLK = 32768 Hz, upmode
CCR0 = 32768; // 1 Hz
}
/*@brief entry point for the code*/
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //! Stop Watch dog (Not recommended for code in production and devices working in field)
do{
IFG1 &= ~OFIFG; // Clear oscillator fault flag
for (i = 50000; i; i--); // Delay
} while (IFG1 & OFIFG); // Test osc fault flag
register_settings_for_TIMER0();
register_settings_for_GPIO();
_BIS_SR(LPM3_bits + GIE); // Enter LPM3 w/ interrupt
}
/*@brief entry point for TIMER0 interrupt vector*/
#pragma vector= TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{
count++;
P1OUT = count; //Assign value of Count to PORT 1 to represent it as binary number
}
|
Experiment 17 -LunchBox HelloSoftwarePWM (Large Delay)¶
Description¶
This example code provides function and register settings for generating PWM signal with help of delay using C commands (Software PWM). In this code, intensity of onboard LED on Lunchbox is controlled with Software PWM.
Hardware needed¶
None
Connections¶
None
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <msp430.h>
#define RED BIT7 // Red LED -> P1.7
/**
*@brief This function provides delay
*@param unsigned int
*@return void
**/
void delay(unsigned int t) // Custom delay function
{
unsigned int i;
for(i = t; i > 0; i--)
__delay_cycles(50); // __delay_cycles accepts only constants !
}
/*@brief entry point for the code*/
void main(void) {
unsigned int j;
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR |= RED; // Set LED pin -> Output
while(1)
{
for(j = 0; j < 256; j++) // Increasing Intensity
{
P1OUT |= RED; // LED ON
if (j !=0) delay(j); // Delay for ON Time
P1OUT &= ~RED; // LED OFF
if ((255-j)!=0) delay(255-j); // OFF Time = Period - ON Time
}
for(j = 255; j > 0; j--) // Decreasing Intensity
{
P1OUT |= RED; // LED ON
if (j !=0) delay(j); // Delay for ON Time
P1OUT &= ~RED; // LED OFF
if ((255-j) != 0 ) delay(255-j); // OFF Time = Period - ON Time
}
}
}
|
Experiment 18 - LunchBox Hello Hardware PWM (8Bit)¶
Description¶
This example code provides function and register settings for using Hardware PWM on LunchBox. In this example code, internal 16 bit Timer is used for generating PWM signal of fixed duty cycle. Pin 1.6 of LunchBox is used for getting PWM output.
Hardware needed¶
Breadboard, Connecting wires, LED, Resistor (1 kohm).
Connections¶
Pin 1.6 of Lunchbox is connected to anode of LED through a resistor of 1 kohm and cathode to Ground.
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include <msp430.h>
#define GREEN BIT6 // Green LED -> P1.6
/**
* @brief
* These settings are w.r.t enabling TIMER0 on Lunchbox
**/
void register_settings_for_TIMER0()
{
P1DIR |= GREEN; // Green LED -> Output
P1SEL |= GREEN; // Green LED -> Select Timer Output
CCR0 = 255; // Set Timer0 PWM Period
CCTL1 = OUTMOD_7; // Set TA0.1 Waveform Mode - Clear on Compare, Set on Overflow
CCR1 = 0; // Set TA0.1 PWM duty cycle
CCTL0 = CCIE; // CCR0 Enable Interrupt
TACTL = TASSEL_2 + MC_1; // Timer Clock -> SMCLK, Mode -> Up Count
}
/**
* @brief
* Entry point for the code
**/
void main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
register_settings_for_TIMER0();
__bis_SR_register(GIE); // Enable CPU Interrupt
while(1)
{
}
}
/**
* @brief
* Entry point for TIMER0_interrupt vector
**/
#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A(void)
{
CCR1 = CCR1 + 1; // Increment CCR1
if(CCR1 == 256)
{
CCR1 = 0;
}
}
|
Experiment 19 - LunchBox Hello Hardware PWM (16 bit)¶
Description¶
This example code provides function and register settings for using Hardware PWM on LunchBox. In this example code, internal 16 bit Timer is used for generating PWM signal of fixed duty cycle. Pin 1.6 of LunchBox is used for getting PWM output.
Hardware needed¶
Breadboard, Connecting wires, LED, Resistor (1 kohm).
Connections¶
Pin 1.6 of Lunchbox is connected to anode of LED through a resistor of 1 kohm and cathode to Ground.
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #include <msp430.h>
#define GREEN BIT6 // Green LED -> P1.6
/**
* @brief
* These settings are w.r.t enabling TIMER0 on Lunchbox
**/
void register_settings_for_TIMER0()
{
P1DIR |= GREEN; // Green LED -> Output
P1SEL |= GREEN; // Green LED -> Select Timer Output
CCR0 = 65535; // Set Timer0 PWM Period
CCTL1 = OUTMOD_7; // Set TA0.1 Waveform Mode - Clear on Compare, Set on Overflow
CCR1 = 0; // Set TA0.1 PWM duty cycle
CCTL0 = CCIE; // CCR0 Enable Interrupt
TACTL = TASSEL_2 + MC_1; // Timer Clock -> SMCLK, Mode -> Up Count
}
/**
* @brief
* Entry point for the code
**/
void main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
BCSCTL1 |= (BIT0 + BIT1 + BIT2 + BIT3); // Selecting RSELx as 15
DCOCTL |= (BIT6 + BIT5 + BIT4); // Selecting DCOx as 7, DCO_freq = 15.6 MHz, Room Temp. ~ 25 deg. Celsius, Operating voltage 3.3 V
register_settings_for_TIMER0();
__bis_SR_register(GIE); // Enable CPU Interrupt
while(1)
{
}
}
/**
* @brief
* Entry point for TIMER0_interrupt vector
**/
#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A(void)
{
CCR1 = CCR1 + 1; // Increment CCR1
}
|
Experiment 20 - LunchBox Hello ADC¶
Description¶
This example code provides function and register settings for reading analog data. This code converts the analog value provided by potentiometer into a corresponding 10 bit digital value. The converted digital value is then used to change the brightness of an external LED using PWM.
Hardware needed¶
Potentiometer (100 k), Resistor (330 ohms), LED, Breadboard, Connecting wires.
Connections¶
Pin 1.6 of LunchBox is connected to anode of LED through a resistor of 1 kohm and cathode to Ground. Pin 1.0 of LunchBox is connected to Analog pin (middle pin) of potentiometer and the other two ends are connected to Vcc and Ground respectively.
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include <msp430.h>
#define GREEN BIT6
#define AIN BIT0
/**
*@brief This function maps input range to the required range
*@param long Input value to be mapped
*@param long Input value range, minimum value
*@param long Input value range, maximum value
*@param long Output value range, minimum value
*@param long Output value range, maximum value
*@return Mapped output value
**/
long map(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/**
* @brief
* These settings are wrt enabling ADC10 on Lunchbox
**/
void register_settings_for_ADC10()
{
ADC10AE0 |= AIN; // P1.0 ADC option select
ADC10CTL1 = INCH_0; // ADC Channel -> 1 (P1.0)
ADC10CTL0 = SREF_0 + ADC10SHT_3 + ADC10ON; // Ref -> Vcc, 64 CLK S&H
}
/**
* @brief
* These settings are wrt enabling TIMER0 on Lunchbox
**/
void register_settings_for_TIMER0()
{
P1DIR |= GREEN; // Green LED -> Output
P1SEL |= GREEN; // Green LED -> Select Timer Output
CCR0 = 255; // Set Timer0 PWM Period
CCTL1 = OUTMOD_7; // Set TA0.1 Waveform Mode
CCR1 = 1; // Set TA0.1 PWM duty cycle
TACTL = TASSEL_2 + MC_1; // Timer Clock -> SMCLK, Mode -> Up Count
}
/*@brief entry point for the code*/
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
register_settings_for_ADC10();
register_settings_for_TIMER0();
while(1)
{
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
while(ADC10CTL1 & ADC10BUSY); // Wait for conversion to end
CCR1 = map(ADC10MEM, 0, 1024, 1, 255);
}
}
|
Experiment 21 - LunchBox Hello ADC LCD¶
Description¶
This example code provides function and register settings for reading analog data. This code converts the analog value provided by potentiometer into a corresponding 10 bit digital value. The converted digital value is then used to change the brightness of an external LED using PWM.
Hardware needed¶
Potentiometer (100 k), Resistor (330 ohms), LED, Breadboard, Connecting wires.
Connections¶
Pin 1.6 of LunchBox is connected to anode of LED through a resistor of 1 kohm and cathode to Ground. Pin 1.0 of LunchBox is connected to Analog pin (middle pin) of potentiometer and the other two ends are connected to Vcc and Ground respectively.
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | #include <msp430.h>
#include <inttypes.h>
#include<stdio.h>
#define CMD 0
#define DATA 1
#define AIN BIT0 // Channel A0
#define LCD_OUT P1OUT
#define LCD_DIR P1DIR
#define D4 BIT4
#define D5 BIT5
#define D6 BIT6
#define D7 BIT7
#define RS BIT2
#define EN BIT3
/**
*@brief Delay function for producing delay in 0.1 ms increments
*@param t milliseconds to be delayed
*@return void
**/
void delay(uint16_t t)
{
uint16_t i;
for(i=t; i > 0; i--)
__delay_cycles(100);
}
/**
*@brief Function to pulse EN pin after data is written
*@return void
**/
void pulseEN(void)
{
LCD_OUT |= EN;
delay(1);
LCD_OUT &= ~EN;
delay(1);
}
/**
*@brief Function to write data/command to LCD
*@param value Value to be written to LED
*@param mode Mode -> Command or Data
*@return void
**/
void lcd_write(uint8_t value, uint8_t mode)
{
if(mode == CMD)
LCD_OUT &= ~RS; // Set RS -> LOW for Command mode
else
LCD_OUT |= RS; // Set RS -> HIGH for Data mode
LCD_OUT = ((LCD_OUT & 0x0F) | (value & 0xF0)); // Write high nibble first
pulseEN();
delay(1);
LCD_OUT = ((LCD_OUT & 0x0F) | ((value << 4) & 0xF0)); // Write low nibble next
pulseEN();
delay(1);
}
/**
*@brief Function to print a string on LCD
*@param *s pointer to the character to be written.
*@return void
**/
void lcd_print(char *s)
{
while(*s)
{
lcd_write(*s, DATA);
s++;
}
}
/**
*@brief Function to move cursor to desired position on LCD
*@param row Row Cursor of the LCD
*@param col Column Cursor of the LCD
*@return void
**/
void lcd_setCursor(uint8_t row, uint8_t col)
{
const uint8_t row_offsets[] = { 0x00, 0x40};
lcd_write(0x80 | (col + row_offsets[row]), CMD);
delay(1);
}
/**
*@brief Function to change numeric value into it's corresponding char array
*@param num Number which has to be displayed
*@return void
**/
void lcd_printNumber(unsigned int num)
{
char buf[3]; // Creating a array of size 3
char *str = &buf[2]; // Initializing pointer to end of the array
*str = '\0'; // storing null pointer at end of string
do
{
unsigned long m = num; // Storing number in variable m
num /= 10; // Dividing number by 10
char c = (m - 10 * num) + '0'; // Finding least place value and adding it to get character value of digit
*--str = c; // Decrementing pointer value and storing character at that character
} while(num);
lcd_print(str);
}
/**
*@brief Initialize LCD
**/
void lcd_init()
{
LCD_DIR |= (D4+D5+D6+D7+RS+EN);
LCD_OUT &= ~(D4+D5+D6+D7+RS+EN);
delay(150); // Wait for power up ( 15ms )
lcd_write(0x33, CMD); // Initialization Sequence 1
delay(50); // Wait ( 4.1 ms )
lcd_write(0x32, CMD); // Initialization Sequence 2
delay(1); // Wait ( 100 us )
// All subsequent commands take 40 us to execute, except clear & cursor return (1.64 ms)
lcd_write(0x28, CMD); // 4 bit mode, 2 line
delay(1);
lcd_write(0x0C, CMD); // Display ON, Cursor OFF, Blink OFF
delay(1);
lcd_write(0x01, CMD); // Clear screen
delay(20);
lcd_write(0x06, CMD); // Auto Increment Cursor
delay(1);
lcd_setCursor(0,0); // Goto Row 1 Column 1
}
/**
* @brief
* These settings are wrt enabling ADC10 on Lunchbox
**/
void register_settings_for_ADC10()
{
ADC10AE0 |= AIN; // P1.0 ADC option select
ADC10CTL1 = INCH_0; // ADC Channel -> 1 (P1.0)
ADC10CTL0 = SREF_0 + ADC10SHT_3 + ADC10ON; // Ref -> Vcc, 64 CLK S&H , ADC - ON
}
/*@brief entry point for the code*/
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
lcd_init(); // Initializing LCD
register_settings_for_ADC10();
while(1)
{
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
while(ADC10CTL1 & ADC10BUSY); // Wait for conversion to end
float adc_value = 0;
adc_value = (ADC10MEM) * (3.30) / (1023.00); // mapping 10-bit conversion result of ADC to corresponding voltage
int int_part = adc_value; // Integer part of calculated ADC value
int decimal_part = (adc_value - (float)int_part) * 10.0 ; // Decimal part of calculated ADC value
lcd_write(0x01, CMD); // Clear screen
delay(20);
lcd_setCursor(0,0);
lcd_print("Pot Reading");
lcd_setCursor(1,3);
lcd_printNumber(int_part); // Displaying integer part of ADC value
lcd_print(".");
lcd_printNumber(decimal_part); // Displaying decimal part of ADC value
lcd_setCursor(1,7);
lcd_print("V");
delay(6000);
}
}
|
Experiment 22 - LunchBox Hello ADC Internal Temperature Sensor¶
Description¶
This example code provides function and register settings for reading analog data. This code converts the analog value provided by potentiometer into a corresponding 10 bit digital value. The converted digital value is then used to change the brightness of an external LED using PWM.
Hardware needed¶
Potentiometer (100 k), Resistor (330 ohms), LED, Breadboard, Connecting wires.
Connections¶
Pin 1.6 of LunchBox is connected to anode of LED through a resistor of 1 kohm and cathode to Ground. Pin 1.0 of LunchBox is connected to Analog pin (middle pin) of potentiometer and the other two ends are connected to Vcc and Ground respectively.
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | #include <msp430.h>
#include <inttypes.h>
#include<stdio.h>
#define CMD 0
#define DATA 1
#define LCD_OUT P1OUT
#define LCD_DIR P1DIR
#define D4 BIT4
#define D5 BIT5
#define D6 BIT6
#define D7 BIT7
#define RS BIT2
#define EN BIT3
/**
*@brief Delay function for producing delay in 0.1 ms increments
*@param t milliseconds to be delayed
*@return void
**/
void delay(uint16_t t)
{
uint16_t i;
for(i=t; i > 0; i--)
__delay_cycles(100);
}
/**
*@brief Function to pulse EN pin after data is written
*@return void
**/
void pulseEN(void)
{
LCD_OUT |= EN;
delay(1);
LCD_OUT &= ~EN;
delay(1);
}
/**
*@brief Function to write data/command to LCD
*@param value Value to be written to LED
*@param mode Mode -> Command or Data
*@return void
**/
void lcd_write(uint8_t value, uint8_t mode)
{
if(mode == CMD)
LCD_OUT &= ~RS; // Set RS -> LOW for Command mode
else
LCD_OUT |= RS; // Set RS -> HIGH for Data mode
LCD_OUT = ((LCD_OUT & 0x0F) | (value & 0xF0)); // Write high nibble first
pulseEN();
delay(1);
LCD_OUT = ((LCD_OUT & 0x0F) | ((value << 4) & 0xF0)); // Write low nibble next
pulseEN();
delay(1);
}
/**
*@brief Function to print a string on LCD
*@param *s pointer to the character to be written.
*@return void
**/
void lcd_print(char *s)
{
while(*s)
{
lcd_write(*s, DATA);
s++;
}
}
/**
*@brief Function to move cursor to desired position on LCD
*@param row Row Cursor of the LCD
*@param col Column Cursor of the LCD
*@return void
**/
void lcd_setCursor(uint8_t row, uint8_t col)
{
const uint8_t row_offsets[] = { 0x00, 0x40};
lcd_write(0x80 | (col + row_offsets[row]), CMD);
delay(1);
}
/**
*@brief Function to change numeric value into it's corresponding char array
*@param num Number which has to be displayed
*@return void
**/
void lcd_printNumber(unsigned int num)
{
char buf[3]; // Creating a array of size 3
char *str = &buf[2]; // Initializing pointer to end of the array
*str = '\0'; // storing null pointer at end of string
do
{
unsigned long m = num; // Storing number in variable m
num /= 10; // Dividing number by 10
char c = (m - 10 * num) + '0'; // Finding least place value and adding it to get character value of digit
*--str = c; // Decrementing pointer value and storing character at that character
} while(num);
lcd_print(str);
}
/**
*@brief Initialize LCD
**/
void lcd_init()
{
LCD_DIR |= (D4+D5+D6+D7+RS+EN);
LCD_OUT &= ~(D4+D5+D6+D7+RS+EN);
delay(150); // Wait for power up ( 15ms )
lcd_write(0x33, CMD); // Initialization Sequence 1
delay(50); // Wait ( 4.1 ms )
lcd_write(0x32, CMD); // Initialization Sequence 2
delay(1); // Wait ( 100 us )
// All subsequent commands take 40 us to execute, except clear & cursor return (1.64 ms)
lcd_write(0x28, CMD); // 4 bit mode, 2 line
delay(1);
lcd_write(0x0C, CMD); // Display ON, Cursor OFF, Blink OFF
delay(1);
lcd_write(0x01, CMD); // Clear screen
delay(20);
lcd_write(0x06, CMD); // Auto Increment Cursor
delay(1);
lcd_setCursor(0,0); // Goto Row 1 Column 1
}
/**
*@brief This function maps input range to the required range
*@param long Input value to be mapped
*@param long Input value range, minimum value
*@param long Input value range, maximum value
*@param long Output value range, minimum value
*@param long Output value range, maximum value
*@return Mapped output value
**/
void register_settings_for_ADC10()
{
ADC10CTL1 = INCH_10 + ADC10SSEL_3; // ADC Channel -> 10 (Internal Temperature Sensor), SMCLK
ADC10CTL0 = SREF_1 + ADC10SHT_3 + ADC10ON + REFON; // Ref -> Vref+ (1.5V) , 64 CLK S&H, ADC-ON, Reference generator-ON
}
/*@brief entry point for the code*/
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
lcd_init();
register_settings_for_ADC10();
while(1)
{
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
//lcd_display();
while(ADC10CTL1 & ADC10BUSY); // Wait for conversion to end
float temp = 0;
float Int_Deg_C = 0;
temp = ADC10MEM; // Storing 10-bit conversion result of ADC in variable temp
Int_Deg_C = ((temp-673)*423) / 1023; // Formula to find internal temperature in Celsius
int int_part = Int_Deg_C; // Integer part of calculated temperature value
int decimal_part = (Int_Deg_C - (float)int_part) * 10.0 ; // Decimal part of calculated temperature value
lcd_write(0x01, CMD); // Clear screen
delay(20);
lcd_setCursor(0,1);
lcd_print("Internal Temp");
lcd_setCursor(1,3);
lcd_printNumber(int_part); // Displaying integer part of temperature
lcd_print(".");
lcd_printNumber(decimal_part); // Displaying decimal part of temperature
lcd_print(" deg C");
delay(6000);
}
}
|
Experiment 23 - LunchBox Hello LFSR 8Bit¶
Description¶
This example code provides function and register settings for reading analog data. This code converts the analog value provided by potentiometer into a corresponding 10 bit digital value. The converted digital value is then used to change the brightness of an external LED using PWM.
Hardware needed¶
Potentiometer (100 k), Resistor (330 ohms), LED, Breadboard, Connecting wires.
Connections¶
Pin 1.6 of LunchBox is connected to anode of LED through a resistor of 1 kohm and cathode to Ground. Pin 1.0 of LunchBox is connected to Analog pin (middle pin) of potentiometer and the other two ends are connected to Vcc and Ground respectively.
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | #include <msp430.h>
#include <inttypes.h>
#define SW BIT3 // Switch -> P1.3 (On-board Switch, Pull-Up configuration)
#define CMD 0
#define DATA 1
#define AIN BIT0 // Channel A0
#define LCD_OUT P1OUT
#define LCD_DIR P1DIR
#define D4 BIT4
#define D5 BIT5
#define D6 BIT6
#define D7 BIT7
#define RS BIT1
#define EN BIT2
/**
*@brief Delay function for producing delay in 0.1 ms increments
*@param t milliseconds to be delayed
*@return void
**/
void delay(uint16_t t)
{
uint16_t i;
for(i=t; i > 0; i--)
__delay_cycles(100);
}
/**
*@brief Function to pulse EN pin after data is written
*@return void
**/
void pulseEN(void)
{
LCD_OUT |= EN;
delay(1);
LCD_OUT &= ~EN;
delay(1);
}
/**
*@brief Function to write data/command to LCD
*@param value Value to be written to LED
*@param mode Mode -> Command or Data
*@return void
**/
void lcd_write(uint8_t value, uint8_t mode)
{
if(mode == CMD)
LCD_OUT &= ~RS; // Set RS -> LOW for Command mode
else
LCD_OUT |= RS; // Set RS -> HIGH for Data mode
LCD_OUT = ((LCD_OUT & 0x0F) | (value & 0xF0)); // Write high nibble first
pulseEN();
delay(1);
LCD_OUT = ((LCD_OUT & 0x0F) | ((value << 4) & 0xF0)); // Write low nibble next
pulseEN();
delay(1);
}
/**
*@brief Function to print a string on LCD
*@param *s pointer to the character to be written.
*@return void
**/
void lcd_print(char *s)
{
while(*s)
{
lcd_write(*s, DATA);
s++;
}
}
/**
*@brief Function to move cursor to desired position on LCD
*@param row Row Cursor of the LCD
*@param col Column Cursor of the LCD
*@return void
**/
void lcd_setCursor(uint8_t row, uint8_t col)
{
const uint8_t row_offsets[] = { 0x00, 0x40};
lcd_write(0x80 | (col + row_offsets[row]), CMD);
delay(1);
}
/**
*@brief Function to change numeric value into it's corresponding char array
*@param num Number which has to be displayed
*@return void
**/
void lcd_printNumber(unsigned int num)
{
char buf[4]; // Creating a array of size 4
char *str = &buf[3]; // Initializing pointer to end of the array
*str = '\0'; // storing null pointer at end of string
do
{
unsigned long m = num; // Storing number in variable m
num /= 10; // Dividing number by 10
char c = (m - 10 * num) + '0'; // Finding least place value and adding it to get character value of digit
*--str = c; // Decrementing pointer value and storing character at that character
} while(num);
lcd_print(str);
}
/**
*@brief Initialize LCD
**/
void lcd_init()
{
LCD_DIR |= (D4+D5+D6+D7+RS+EN);
LCD_OUT &= ~(D4+D5+D6+D7+RS+EN);
delay(150); // Wait for power up ( 15ms )
lcd_write(0x33, CMD); // Initialization Sequence 1
delay(50); // Wait ( 4.1 ms )
lcd_write(0x32, CMD); // Initialization Sequence 2
delay(1); // Wait ( 100 us )
// All subsequent commands take 40 us to execute, except clear & cursor return (1.64 ms)
lcd_write(0x28, CMD); // 4 bit mode, 2 line
delay(1);
lcd_write(0x0C, CMD); // Display ON, Cursor OFF, Blink OFF
delay(1);
lcd_write(0x01, CMD); // Clear screen
delay(20);
lcd_write(0x06, CMD); // Auto Increment Cursor
delay(1);
lcd_setCursor(0,0); // Goto Row 1 Column 1
}
/**
* @brief
* These settings are wrt enabling ADC10 on Lunchbox
**/
void register_settings_for_ADC10()
{
ADC10AE0 |= AIN; // P1.0 ADC option select
ADC10CTL1 = INCH_0; // ADC Channel -> 1 (P1.0)
ADC10CTL0 = SREF_0 + ADC10SHT_3 + ADC10ON; // Ref -> Vcc, 64 CLK S&H , ADC - ON
}
/*@brief entry point for the code*/
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
P1DIR &= ~SW; // Set SW pin -> Input
lcd_init(); // Initializing LCD
register_settings_for_ADC10();
while(1)
{
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
while(ADC10CTL1 & ADC10BUSY); // Wait for conversion to end
uint8_t seed = (ADC10MEM & 0xFF); // Lower 8-bits of ADC conversion result as seed
uint8_t lfsr = seed;
uint8_t count = 0;
lcd_write(0x01, CMD); // Clear screen
delay(20);
lcd_setCursor(0,1);
lcd_print("Seed Value :");
lcd_printNumber(seed); // Printing Seed Value
lcd_setCursor(1,2);
lcd_print("Press Switch");
while(count<=255) // Changing Seed Value after reading 255 LFSR values
{
if(!(P1IN & SW)) // If SW is Pressed
{
__delay_cycles(20000); // Wait 20ms to debounce
while(!(P1IN & SW)); // Wait till SW Released
__delay_cycles(20000); // Wait 20ms to debounce
uint8_t bit;
bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 4)); // Taking taps
lfsr = (lfsr >> 1) | (bit << 7); // Shifting LFSR
count = count + 1;
lcd_write(0x01, CMD); // Clear screen
delay(20);
lcd_setCursor(0,1);
lcd_print("Seed Value :");
lcd_printNumber(seed); // Printing Seed Value
lcd_setCursor(1,1);
lcd_print("LFSR Value :");
lcd_printNumber(lfsr); // Printing LFSR Value
delay(3000);
}
}
}
}
|
Experiment 24 - LunchBox Hello LFSR 32Bit¶
Description¶
This example code provides function and register settings for reading analog data. This code converts the analog value provided by potentiometer into a corresponding 10 bit digital value. The converted digital value is then used to change the brightness of an external LED using PWM.
Hardware needed¶
Potentiometer (100 k), Resistor (330 ohms), LED, Breadboard, Connecting wires.
Connections¶
Pin 1.6 of LunchBox is connected to anode of LED through a resistor of 1 kohm and cathode to Ground. Pin 1.0 of LunchBox is connected to Analog pin (middle pin) of potentiometer and the other two ends are connected to Vcc and Ground respectively.
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <msp430.h>
#include <inttypes.h>
#include<stdio.h>
#define RED BIT7 // Switch -> P1.3 (On-board Switch, Pull-Up configuration)
#define AIN BIT0 // Channel A0
/**
*@brief Delay function for producing delay in 0.1 ms increments
*@param t milliseconds to be delayed
*@return void
**/
void delay(uint16_t t)
{
uint16_t i;
for(i=t; i > 0; i--)
__delay_cycles(100);
}
/**
* @brief
* These settings are wrt enabling ADC10 on Lunchbox
**/
void register_settings_for_ADC10()
{
ADC10AE0 |= AIN; // P1.0 ADC option select
ADC10CTL1 = INCH_0; // ADC Channel -> 1 (P1.0)
ADC10CTL0 = SREF_0 + ADC10SHT_3 + ADC10ON; // Ref -> Vcc, 64 CLK S&H , ADC - ON
}
/*@brief entry point for the code*/
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
P1DIR |= RED; // P1.7 (Red LED)
register_settings_for_ADC10();
while(1)
{
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
while(ADC10CTL1 & ADC10BUSY); // Wait for conversion to end
uint32_t lfsr = 0;
lfsr |= ADC10MEM;
uint32_t count = 0;
while(count < 0xFFFFFFFF)
{
uint32_t bit;
bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 6) ^ (lfsr >> 7)); // Taking taps
lfsr = (lfsr >> 1) | (bit << 31); // Shifting LFSR
P1OUT = (lfsr & 0x00000001)<<7; ; // Setting RED LED according to one bit of 32*bit LFSR
count = count + 1;
delay(150);
}
}
}
|
Experiment 25 - LunchBox Hello DAC¶
Description¶
This example code provides function and register settings for reading analog data. This code converts the analog value provided by potentiometer into a corresponding 10 bit digital value. The converted digital value is then used to change the brightness of an external LED using PWM.
Hardware needed¶
Potentiometer (100 k), Resistor (330 ohms), LED, Breadboard, Connecting wires.
Connections¶
Pin 1.6 of LunchBox is connected to anode of LED through a resistor of 1 kohm and cathode to Ground. Pin 1.0 of LunchBox is connected to Analog pin (middle pin) of potentiometer and the other two ends are connected to Vcc and Ground respectively.
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <msp430.h>
#include <inttypes.h>
char count = 0;
unsigned int i;
/**
* @brief
* These settings are w.r.t enabling GPIO on LunchBox
**/
void register_settings_for_GPIO()
{
P1DIR |= 0xFF; //P1.0 to P1.7 are set as Output
P1OUT &= ~(0xFF); //Initially they are set to logic zero
}
/*@brief entry point for the code*/
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //! Stop Watch dog (Not recommended for code in production and devices working in field)
register_settings_for_GPIO();
while(1)
{
P1OUT = count; // Assign value of Count to PORT 1 to represent it as binary number
for(i = 0; i < 5000; i++);
count ++; // Increment count
}
}
|
Experiment 26 LunchBox Lemon Battery LED Blink¶
Description¶
This example code provides function and register settings for enabling serial communication with UART on LunchBox. Baud Rate for UART communication is set to 9600.
Hardware needed¶
None
Connections¶
For using UART, Jumper J1 and J2 needs to be changed from Programming mode to UART mode.
Output¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include <msp430.h>
/**
* @brief
* These settings are wrt enabling GPIO on Lunchbox
**/
void register_settings_for_GPIO()
{
P1DIR |= BIT7; // P1.7 as Output
P1OUT &= ~BIT7; // Initialize with zero
}
/**
* @brief
* These settings are w.r.t enabling TIMER0 on Lunch Box
**/
void register_settings_for_TIMER0()
{
CCTL0 = CCIE; // CCR0 interrupt enabled
TACTL = TASSEL_1 + MC_1; // ACLK = 32768 Hz, Up mode
CCR0 = 32768; // 1 Hz
}
/*@brief entry point for the code*/
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //! Stop Watch dog (Not recommended for code in production and devices working in field)
unsigned int i;
do{
IFG1 &= ~OFIFG; // Clear oscillator fault flag
for (i = 50000; i; i--); // Delay
} while (IFG1 & OFIFG); // Test osc fault flag
register_settings_for_TIMER0();
register_settings_for_GPIO();
_BIS_SR(LPM3_bits + GIE); // Enter LPM3 w/ interrupt
}
/*@brief entry point for TIMER0 interrupt vector*/
#pragma vector= TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{
P1OUT |= BIT7; // LED HIGH
__delay_cycles(200);
P1OUT &=~ BIT7;
}
|
Experiment 27 - LunchBox Hello Serial¶
Description¶
This example code provides function and register settings for enabling serial communication with UART on LunchBox. Baud Rate for UART communication is set to 9600. It utilises on board CH340, which acts as USB-to-UART bridge. While recieving UART data, J1 and J2 selection should be on side 2.
Course link¶
Covered in lecture number - Lecture 33
Hardware needed¶
NA
Schematic image¶
NA
Connections¶
For using UART, Jumper J1 and J2 needs to be changed from Programming mode to UART mode.
Output¶
An incremented character will be recieved on putty (or any serial terminal) wrt whatever you will send.
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <msp430.h>
/**
* @brief
* These settings are wrt enabling uart on Lunchbox
**/
void register_settings_for_UART()
{
P1SEL = BIT1 + BIT2; // Select UART RX/TX function on P1.1,P1.2
P1SEL2 = BIT1 + BIT2;
UCA0CTL1 |= UCSSEL_1; // UART Clock -> ACLK
UCA0BR0 = 3; // Baud Rate Setting for 32kHz 9600
UCA0BR1 = 0; // Baud Rate Setting for 32kHz 9600
UCA0MCTL = UCBRF_0 + UCBRS_3; // Modulation Setting for 32kHz 9600
UCA0CTL1 &= ~UCSWRST; // Initialize UART Module
IE2 |= UCA0RXIE; // Enable RX interrupt
}
/*@brief entry point for the code*/
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //! Stop Watchdog (Not recommended for code in production and devices working in field)
register_settings_for_UART();
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, Enable Interrupt
}
/**
* @brief
* Interrupt Vector for UART on LunchBox
**/
#pragma vector=USCIAB0RX_VECTOR // UART RX Interrupt Vector
__interrupt void USCI0RX_ISR(void)
{
while (!(IFG2&UCA0TXIFG)); // Check if TX is ongoing
UCA0TXBUF = UCA0RXBUF + 1; // TX -> Received Char + 1
}
|