ARM Cortex M4
Hardware Programs
Dr. C. GANESH KUMAR
Push Button Program
02-03-2023 Embedded Computing Lab (19CCE283) 2
Push Button Program
#include "msp.h" while (1)
{
int main(void) if (P1->IN & 2) // Checking P1.1 (Switch
{ Press)
P1->SEL1 &= ~2; // Setting P1.1 as Input P2->OUT &= ~1; // P2.0 (Led On/Off)
P1->SEL0 &= ~2; else
P1->DIR &= ~2; P2->OUT |= 1;
P1->REN |=2; }
P2->SEL1 &= ~1; // Setting P2.0 as Output }
P2->SEL0 &= ~1;
P2->DIR |= 1;
02-03-2023 Embedded Computing Lab (19CCE283) 3
7 Segment Program
02-03-2023 Embedded Computing Lab (19CCE283) 4
7 Segment Program (Common Anode)
#include "msp.h" int i;
for(i=0;i<10;i++)
void delay(int n); {
P4->OUT = digit[i];
int main(void) delay(10000);
{ }
const unsigned char digit[] = }
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82, 0xf8,0x80,0x98}; }
P4->SEL1 &= ~0xFF; /* P4 as Output*/ void delay(int n)
P4->SEL0 &= ~0xFF; {
P4->DIR |= 0xFF; int j;
while(1) for (j = 0; j < n; j++);
{ }
02-03-2023 Embedded Computing Lab (19CCE283) 5
Reload Value to Create Timing Delay
MSP432 – 3MHz of System Clock
• Systick Timer interrupt Time period = (Reload+1) x Clock Period
• Reload = (Interrupt Time Period/ Clock Period) – 1
For Example, Interrupt Time Period is 5 Second,
• Reload = ( 5 / 1/3MHz ) – 1
= 15x106 - 1 = 14999999
02-03-2023 Embedded Computing Lab (19CCE283) 6
SysTick Program
#include "msp.h"
int main(void)
{
P2->SEL1 &= ~0x1;
P2->SEL0 &= ~0x1;
P2->DIR |= 0x1;
SysTick->LOAD = 14999999;
SysTick->VAL = 0;
SysTick->CTRL = 5;
while (1)
{
if(SysTick->CTRL & 0x10000)
P2->OUT ^= 0x1;
}
}
02-03-2023 Embedded Computing Lab (19CCE283) 7
Stepper Motor
X2 Y2 X1 Y1
1 1 0 0 Half Step Sequence
0 1 1 0
Step X1 X2 Y1 Y2 Hex
0 0 1 1
1 0 1 0 1 5
1 0 0 1
2 0 0 0 1 1
Full Step Sequence 3 1 0 0 1 9
4 1 0 0 0 8
Step X1 X2 Y1 Y2 Hex 5 1 0 1 0 A
1 0 1 0 1 5 6 0 0 1 0 2
2 1 0 0 1 9 7 0 1 1 0 6
3 1 0 1 0 A 8 0 1 0 0 4
4 0 1 1 0 6
02-03-2023 Embedded Computing Lab (19CCE283) 8
Stepper Motor Program
#include "msp.h" if (direction)
void delay(int n); P4->OUT = (steps[i++ & 3]);
int delayVal = 400; //P4->OUT = (steps[i++ & 7]);
int direction = 1; else
int main(void) P4->OUT = (steps[i-- & 3]);
{ //P4->OUT = (steps[i-- & 7]);
const char steps[ ] = {0x5, 0x9, 0xA, 0x6}; delay(delayVal);
//const char steps[ ] = }
{0x5,0x1,0x9,0x8,0xA,0x2,0x6,0x4}; }
int i = 0; void delay(int n) In Driver Circuit:
P4.0 – IN1
P4->SEL1 &= ~0xF; { P4.1 – IN3
P4->SEL0 &= ~0xF; int i; P4.2 – IN2
P4->DIR |= 0xF; for (i = 0; i < n; i++); P4.3 – IN4
while (1) }
{
9
02-03-2023 Embedded Computing Lab (19CCE283)
LCD - Pin Details
Pin Number Symbol Pin Function
1 VSS Ground
2 VCC +5v
3 VEE Contrast adjustment (VO) To Microcontroller
4 RS Register Select. 0:Command, 1: Data
5 R/W Read/Write, R/W=0: Write & R/W=1: Read
6 EN Enable. Falling edge triggered
7 D0 Data Bit 0
8 D1 Data Bit 1
9 D2 Data Bit 2
10 D3 Data Bit 3
11 D4 Data Bit 4
12 D5 Data Bit 5
13 D6 Data Bit 6
14 D7 Data Bit 7
15 A/LED+ Back-light Anode(+)
16 K/LED- Back-Light Cathode(-)
02-03-2023 Embedded Computing Lab (19CCE283) 10
LCD Program
#include "msp.h" LCD_command(0x0F); /* turn on display, cursor blinking */
#define RS 0x20 /*P3.5*/ LCD_command(0x80); /* set cursor at beginning of first line */
#define RW 0x40 /*P3.6*/ }
#define EN 0x80 /*P3.7*/ void LCD_command(unsigned char command)
void delayMs(int n); {
void LCD_command(unsigned char command); P3->OUT &= ~(RS | RW); /* RS = 0, R/W = 0 */
void LCD_data(unsigned char data); P4->OUT = command; /* put command on data bus */
void LCD_init(void); P3->OUT |= EN; /* pulse E high */
unsigned char msg[]="WELCOME"; delayMs(2);
int main(void) P3->OUT &= ~EN; /* clear E */
{ delayMs(2);
LCD_init(); }
while(1) void LCD_data(unsigned char data)
{ {
int i; P3->OUT |= RS; /* RS = 1 */
for(i=0;msg[i]!='\0';i++) P3->OUT &= ~RW; /* R/W = 0 */
LCD_data(msg[i]); P4->OUT = data; /* put data on bus */
} P3->OUT |= EN; /* pulse E */
} delayMs(2);
void LCD_init(void) P3->OUT &= ~EN; /* clear E */
{ delayMs(2);
P3->DIR |= RS | RW | EN; /* make P3 pins output for control */ }
P4->DIR = 0xFF; /* make P4 pins output for data */ void delayMs(int n)
LCD_command(0x38); /* set 8-bit data, 2-line, 5x7 font */ {
LCD_command(0x06); /* move cursor right after each char */ int j;
LCD_command(0x01); /* clear screen, move cursor to home */ for (j = 0; j < n; j++);
} 11
02-03-2023 Embedded Computing Lab (19CCE283)