Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views7 pages

Arm Lab Test

The document contains various C code snippets for controlling hardware components using the LPC214x microcontroller. It includes functionalities for DC motor control, UART communication, LED scrolling, ADC reading, and a 7-segment display, among others. Each code segment demonstrates specific tasks such as motor direction control, button press handling, and visual feedback through LEDs and displays.

Uploaded by

fakeemail4145
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Arm Lab Test

The document contains various C code snippets for controlling hardware components using the LPC214x microcontroller. It includes functionalities for DC motor control, UART communication, LED scrolling, ADC reading, and a 7-segment display, among others. Each code segment demonstrates specific tasks such as motor direction control, button press handling, and visual feedback through LEDs and displays.

Uploaded by

fakeemail4145
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

DCMOTOR:

#include<lpc214x.h> void timer_delay_ms(unsigned int ms) {T0PR=59999; T0TCR=0x02;


T0TCR=0x01;T0TC=0; while(T0TC<ms);T0TCR=0x00;} intmain()
{PINSEL0=0x00000000;PINSEL1=0x00000000;IO0DIR|=(1<<8)|(1<<11);while(1)
{IO0CLR=(1<<8)|(1<<11);timer_delay_ms(100);IO0SET=(1<<8)|
(1<<11);timer_delay_ms(3000);IO0CLR=(1<<8)|(1<<11);timer_delay_ms(500);IO0CLR=(1<<8)|
(1<<11);timer_delay_ms(100);IO0SET=(1<<8);timer_delay_ms(3000);IO0CLR=(1<<8)|
(1<<11);timer_delay_ms(500);}} // Motor CW: IN1=1, IN2=1 for 3s; then stop 0.5s; then CCW:
IN1=1, IN2=0 for 3s; loop forever

dc motor is running cloakwise dirrection suddenly if a button is pressed the motor should rotate
anticlockwise
#include <LPC214x.h>
unsigned int direction = 0; // 0 = clockwise, 1 = anticlockwise
void delay_ms(unsigned int ms) { T0PR = 59999; T0TCR = 0x02; T0TCR = 0x01; T0TC = 0; while
(T0TC < ms); T0TCR = 0x00; } // Delay function using Timer0
int main() { PINSEL0 = 0x00000000; // Set all pins as GPIO
PINSEL1 = 0x00000000; IO0DIR |= (1 << 8) | (1 << 11); // Set P0.8 and P0.11 as output (motor)
IO1DIR &= ~(1 << 23); // Set P1.23 as input (button)
IO0CLR = (1 << 8) | (1 << 11); // Initially stop motor
IO0SET = (1 << 8) | (1 << 11); // Start motor in clockwise direction
while (1) { if ((IO1PIN & (1 << 23)) == 0) { // If button pressed (LOW)
delay_ms(200); // Debounce delay
while ((IO1PIN & (1 << 23)) == 0); // Wait for release
delay_ms(200); // Debounce delay
direction ^= 1; // Toggle direction variable
if (direction == 0) { IO0CLR = (1 << 8) | (1 << 11); IO0SET = (1 << 8) | (1 << 11); } // Clockwise:
both IN1 and IN2 HIGH
else { IO0CLR = (1 << 8) | (1 << 11); IO0SET = (1 << 8); } // Anticlockwise: only IN1 HIGH
}}}

Interrupt based Uart and buzzer


#include<LPC214x.h> unsigned int press_count=0; void UART0_Init(void){PINSEL0|
=0x00000005;U0LCR=0x83;U0DLL=97;U0DLM=0;U0LCR=0x03;} void UART0_SendChar(char
c){while(!(U0LSR&0x20));U0THR=c;} void delay_ms(unsigned int ms){T0PR=15000-
1;T0TCR=0x02;T0TCR=0x01;T0TC=0;while(T0TC<ms);T0TCR=0x00;} void
EXTINT0_ISR(void)__irq{press_count++;if(press_count==1){UART0_SendChar('C');}else
if(press_count==2){IO0SET=(1<<9);delay_ms(1000);IO0CLR=(1<<9);press_count=0;}EXTINT|
=0x01;VICVectAddr=0;} void EINT0_Init(void){PINSEL0|=(1<<29);EXTMODE|
=0x01;EXTPOLAR&=~(0x01);VICIntSelect&=~(1<<14);VICIntEnable|
=(1<<14);VICVectCntl0=(1<<5)|14;VICVectAddr0=(unsigned long)EXTINT0_ISR;} int main(void)
{IO0DIR|=(1<<9);IO0CLR=(1<<9);UART0_Init();EINT0_Init();while(1);} // Press switch on P0.14:
first press sends 'C' via UART0, second press triggers buzzer on P0.9 for 1 sec
interrupt toggle one led from the p0.16 to p0.23
#include <LPC214x.h> // Include header for LPC214x
unsigned int led_index = 16; // Start with LED on P0.16
void delay_ms(unsigned int ms)
{T0PR=59999;T0TCR=0x02;T0TCR=0x01;T0TC=0;while(T0TC<ms);T0TCR=0x00;}//1ms delay
using Timer0
void EINT0_ISR(void)__irq{ IO0PIN^=(0xFF<<16); EXTINT|=(0<<1);VICVectAddr=0;} //
Acknowledge end of ISR to VIC
int main(){IO0DIR|=0xFF<<16; // Set P0.16–P0.23 as output for LEDsIO0CLR=0xFF<<16; // Turn
off all LEDs initially : PINSEL1|=(1<<0); // Enable EINT0 on P0.16 EXTMODE|=1<<0; // EINT0 as
edge-sensitive {.}EXTPOLAR&=~(1<<0); // Trigger on falling edge (active low) {.}VICIntEnable|
=1<<14; // Enable EINT0 interrupt in VIC
VICVectCntl0=(1<<5)|14; // Set slot 0 for EINT0, enable slot {.}VICVectAddr0=(unsigned
long)EINT0_ISR; // Set address of ISR while(1);} // Infinite loop; wait for interrupts

7segment display(switch)
#include<LPC214x.h>
unsigned int Disp[1]={0x00390000}; // Segment pattern for 'C' (common cathode)
#define SELDISP1 (1<<28) // P0.28
#define SELDISP2 (1<<29) // P0.29
#define SELDISP3 (1<<30) // P0.30
#define SELDISP4 (1<<31) // P0.31
#define ALLDISP 0xF0000000 // All display select lines unsigned int position=0; // Current display
index (0–3) void delay_ms(unsigned int ms)
{T0PR=59999;T0TCR=0x02;T0TCR=0x01;T0TC=0;while(T0TC<ms);T0TCR=0x00;} // Simple
delay using Timer0 void display_char(unsigned int pos)
{IO0SET=ALLDISP;IO0CLR=0x00FF0000;IO0SET=Disp[0]; // Show 'C' on segment switch(pos)
{case 0:IO0CLR=SELDISP1;break;case 1:IO0CLR=SELDISP2;break;case
2:IO0CLR=SELDISP3;break;case 3:IO0CLR=SELDISP4;break;} delay_ms(5);} // Activate
corresponding display int main(){PINSEL0=0x00000000;PINSEL1=0x00000000;IO0DIR|
=(0xFF<<16)|ALLDISP;IO1DIR&=~(1<<23); // Set segment & digit pins as output, button as input
while(1){display_char(position);if((IO1PIN&(1<<23))==0)
{delay_ms(200);while((IO1PIN&(1<<23))==0);delay_ms(200);position+
+;if(position>3)position=0;}}} // On button press, move 'C' to next digit

ADC:
#include <LPC21xx.h> #include <stdio.h> void delay_ms(unsigned int ms) { unsigned int i, j; for (i =
0; i < ms; i++) for (j = 0; j < 6000; j++); } void UART0_Init() { PINSEL0 |= 0x00000005; U0LCR =
0x83; U0DLL = 97; U0DLM = 0x00; U0LCR = 0x03; } void UART0_SendChar(char c) { while (!
(U0LSR & 0x20)); U0THR = c; } void UART0_SendString(char *str) { while (*str)
{ UART0_SendChar(*str++); } } void ADC0_Init() { PINSEL1 |= (1 << 26); AD0CR = 0x00200602;
} unsigned int Read_ADC0() { AD0CR |= (1 << 24); while (!(AD0GDR & (1 << 31))); return
(AD0GDR >> 6) & 0x3FF; } int main() { char buffer[20]; unsigned int adc_value; UART0_Init();
ADC0_Init(); while (1) { adc_value = Read_ADC0(); sprintf(buffer, "ADC: %d\r\n", adc_value);
UART0_SendString(buffer); delay_ms(500); } }

Smart Conveyor Belt Direction controller:create a miniature conveyor belt system where the
direction of the DC motor changes automatically after a set time
#include <lpc214x.h>
// Function to initialize Timer0
void timer_delay_ms(unsigned int ms) {
T0PR = 59999; // Prescaler for 1ms (PCLK = 60MHz)
T0TCR = 0x02; // Reset Timer
T0TCR = 0x01; // Enable Timer
T0TC = 0;
while (T0TC < ms); // Wait for match
T0TCR = 0x00; // Disable Timer
} int main() { // Set P0.8 and P0.11 as output
PINSEL0 = 0x00000000;
PINSEL1 = 0x00000000;
IO0DIR |= (1 << 8) | (1 << 11); // P0.8 and P0.11
while (1) {// Clockwise rotation (both HIGH)
IO0CLR = (1 << 8) | (1 << 11); // Clear first
timer_delay_ms(100); // Small delay
IO0SET = (1 << 8) | (1 << 11);
timer_delay_ms(3000); // Run for 3 seconds// Stop motor (optional)
IO0CLR = (1 << 8) | (1 << 11);
timer_delay_ms(500); // Pause 0.5 sec
// Anticlockwise rotation (only IN1 HIGH)
IO0CLR = (1 << 8) | (1 << 11);
timer_delay_ms(100);
IO0SET = (1 << 8); // Only IN1 = HIGH
timer_delay_ms(3000); // Run for 3 seconds// Stop motor again
IO0CLR = (1 << 8) | (1 << 11);timer_delay_ms(500);
}}

C and Buzzer
#include <LPC214x.h> unsigned int press_count = 0; void delay_ms(unsigned int ms)
{T0PR=59999;T0TCR=0x02;T0TCR=0x01;T0TC=0;while(T0TC<ms);T0TCR=0x00;} int
main(void){PINSEL0=0x00000000; // Set P0 as GPIO
PINSEL1=0x00000000; // Set P1 as GPIO
IO0DIR|=0x00FF0000; // P0.16 to P0.23 as output for 7-segment
IO0DIR|=(1<<9); // P0.9 as output for buzzer
IO1DIR&=~(1<<23); // P1.23 as input for switch
IO0CLR=0x00FF0000; // Clear 7-segment
IO0CLR=(1<<9); // Buzzer OFF
while(1){if((IO1PIN&(1<<23))==0){delay_ms(200); // Debounce delay
while((IO1PIN&(1<<23))==0);delay_ms(200); // Wait for release and debounce
press_count++;if(press_count==1){IO0CLR=0x00FF0000;IO0SET=0x00390000;} // Show 'C' on 7-
segment
else if(press_count==2)
{IO0CLR=0x00FF0000;IO0SET=(1<<9);delay_ms(1000);IO0CLR=(1<<9);press_count=0;} //
Buzzer ON for 1s, then OFF and reset count
}}}

Left to right /right to left


#include <LPC214x.h> void delay_ms(unsigned int ms)
{T0PR=59999;T0TCR=0x02;T0TCR=0x01;T0TC=0;while(T0TC<ms);T0TCR=0x00;} // Timer
delay function for ms
void led_scroll_forward(){for(int i=17;i<=20;i++) {IO0SET=(1<<i);
delay_ms(200);IO0CLR=(1<<i);}} // LEDs scroll left to right
void led_scroll_reverse(){for(int i=20;i>=17;i--)
{IO0SET=(1<<i);delay_ms(200);IO0CLR=(1<<i);}} // LEDs scroll right to left
int main(){PINSEL0=0x00000000;PINSEL1=0x00000000; // Set GPIO mode for all pins
IO0DIR|=(1<<8)|(1<<11); // P0.8 and P0.11 as output for motor
IO0DIR|=0x001E0000; // P0.17 to P0.20 as output for LEDs
while(1){
IO0SET=(1<<8);IO0CLR=(1<<11); // Motor clockwise (IN1=HIGH, IN2=LOW)
for(int i=0;i<5;i++){led_scroll_forward();} // Scroll LEDs forward
delay_ms(500); // Delay between directions
IO0CLR=(1<<8);IO0SET=(1<<11); // Motor anticlockwise (IN1=LOW, IN2=HIGH)
for(int i=0;i<5;i++){led_scroll_reverse();} // Scroll LEDs reverse
delay_ms(500); // Delay between loops
}}

Locked/Unlocked using putty


#include <LPC214x.h> unsigned int state = 0; // 0 = LOCKED, 1 = UNLOCKED void
delay_ms(unsigned int ms) { T0PR = 59999; T0TCR = 0x02; T0TCR = 0x01; T0TC = 0; while
(T0TC < ms); T0TCR = 0x00; } void uart_init(void) { PINSEL0 |= 0x00000005; U0LCR = 0x83;
U0DLL = 97; U0DLM = 0x00; U0LCR = 0x03; } // UART0 init: 9600 baud, 8N1 void
uart_tx_char(char c) { while (!(U0LSR & 0x20)); U0THR = c; } // Send one char over UART0 void
uart_tx_string(char *str) { while (*str) { uart_tx_char(*str++); } } // Send string over UART0 int
main(void) { IO1DIR &= ~(1 << 23); uart_init(); // Button on P1.23 as input; initialize UART0 while
(1) { if ((IO1PIN & (1 << 23)) == 0) { delay_ms(200); while ((IO1PIN & (1 << 23)) == 0);
delay_ms(200); state ^= 1; // Toggle state if (state == 1) { uart_tx_string("UNLOCKED\r\n"); } else {
uart_tx_string("LOCKED\r\n"); } } } } // On button press: toggle and send LOCKED/UNLOCKED
over UART

PWM
#include <LPC21xx.h> // Include LPC2148 header

void delay_ms(unsigned int ms) { unsigned int i,j; for(i=0;i<ms;i++) for(j=0;j<6000;j++); } //


Approximate 1ms delay
int main() {
PINSEL0 |= 0x00000002; // Enable PWM1 function on P0.0 (01 at bits 1:0)
PWMPR = 14; // Set prescaler to divide 15MHz by (14+1) = 1MHz timer clock
PWMMR0 = 1000; // Set PWM period (1kHz = 1ms period)
PWMMR1 = 500; // Set PWM1 duty cycle (500/1000 = 50%)
PWMMCR = 2; // Reset PWMTC on match with MR0
PWMLER = 0x03; // Enable latch for MR0 and MR1
PWMPCR = 0x020; // Enable PWM output on channel 1 (PWMENA1)
PWMTCR = 0x09; // Enable PWM mode and counter (PWMEN and COUNTEREN)
while(1) {
delay_ms(1000); // Wait 1 second
PWMMR1 = 800; PWMLER |= (1 << 1); // Change duty cycle to 80%
delay_ms(1000); // Wait 1 second
PWMMR1 = 200; PWMLER |= (1 << 1); // Change duty cycle to 20%
}}
Square: 0x0000FC0
#include <LPC21xx.h> #define DACR (*(volatile unsigned long *)0xE006C000)
void delay() { unsigned int i; for (i = 0; i < 10000; i++); }int main() { PINSEL1 |= (1 << 19);
//Enable DAC on P0.25 (AOUT) ;while (1) { DACR = (1 << 16) | (0xFF << 6); // High output
delay(); DACR = (1 << 16) | (0x00 << 6); // Low output ; delay(); } }

Ramp:
#include <LPC21xx.h> #define DACR (*(volatile unsigned long *)0xE006C000)
void delay() { unsigned int i; for (i = 0; i < 1000; i++); } int main() {
unsigned int i; PINSEL1 |= (1 << 19); // Enable DAC output on P0.25
while (1) { for (i = 0; i <= 1023; i++) { DACR = (1 << 16) | (i << 6); delay();
}}}

Triangular
#include <LPC21xx.h> #define DACR (*(volatile unsigned long *)0xE006C000)
void delay() { unsigned int i; for (i = 0; i < 1000; i++); }
int main() { unsigned int i;
\ PINSEL1 |= (1 << 19); // Enable DAC output on P0.25
while (1) { for (i = 0; i <= 1023; i++) { DACR = (1 << 16) | (i << 6); delay(); }
for (i = 1023; i >= 0; i--) { DACR = (1 << 16) | (i << 6); delay();
}}}

Staircase:
#include <LPC21xx.h> #define DACR (*(volatile unsigned long *)0xE006C000) void delay()
{ unsigned int i; for (i = 0; i < 3000; i++); } int main() { unsigned int level; PINSEL1 |= (1 << 19); //
Enable DAC pin while (1) { for (level = 0; level <= 1023; level += 128) { DACR = (1 << 16) | (level
<< 6); delay(); } } }

Sawtooth wave:
#include <LPC21xx.h> #define DACR (*(volatile unsigned long *)0xE006C000) void delay()
{ unsigned int i; for (i = 0; i < 3000; i++); } int main() { unsigned int value; PINSEL1 |= (1 << 19); //
Enable DAC pin while (1) { for (value = 0; value <= 1023; value++) { DACR = (1 << 16) | (value <<
6); delay(); } DACR = (1 << 16) | (0 << 6); delay(); } }

Sine wave:
#include <LPC21xx.h> #define DACR (*(volatile unsigned long *)0xE006C000) void delay()
{ unsigned int i; for (i = 0; i < 3000; i++); } unsigned char sine_tab[49] =
{ 0x80,0x90,0xA1,0xB1,0xC0,0xCD,0xDA,0xE5,0xEE,0xF6,0xFB,0xFE,0xFF,0xFE,0xFB,0xF6,0x
EE,0xE5,0xDA,0xCD,0xC0,0xB1,0xA1,0x90,0x80,0x70,0x5F,0x4F,0x40,0x33,0x26,0x1B,0x12,0x0
A,0x05,0x02,0x00,0x02,0x05,0x0A,0x12,0x1B,0x26,0x33,0x40,0x4F,0x5F,0x70,0x80 }; int
main(void) { unsigned int count; PINSEL1 |= (1 << 19); while (1) { for (count = 0; count < 49;
count++) { DACR = (1 << 16) | (sine_tab[count] << 6); delay(); } } }
UART
#include <LPC21xx.h> // Include header for LPC2148
void UART0_Init() { PINSEL0 |= 0x00000005; /* Enable UART0 TXD (P0.0) and RXD (P0.1) */
U0LCR = 0x83; /* 8-bit data, 1 stop bit, no parity, enable DLAB */ U0DLL = 97; /* Set baud rate to
9600 (PCLK = 15MHz) */ U0DLM = 0x00; /* High byte of baud rate */ U0LCR = 0x03; /* Disable
DLAB to access THR/RBR */ }
void UART0_SendChar(char c) { while (!(U0LSR & 0x20)); /* Wait until THR is empty */ U0THR
= c; /* Transmit character */ }
void UART0_SendString(char *str) { while (*str) UART0_SendChar(*str++); /* Send each character
from string */ }
void delay_ms(unsigned int ms) { unsigned int i, j; for (i = 0; i < ms; i++) for (j = 0; j < 6000; j++); /*
Approx 1 ms delay loop */ }
int main() { UART0_Init(); /* Initialize UART0 for serial communication */ while (1)
{ UART0_SendString("Hello from LPC2148 via UART!\r\n"); /* Transmit message */
delay_ms(1000); /* Wait 1 second before next send */ } }

EXTINT LED Toggle


#include <LPC214x.h> // Include header for LPC214x
unsigned int led_index = 16; // Start with LED on P0.16
void delay_ms(unsigned int ms)
{T0PR=59999;T0TCR=0x02;T0TCR=0x01;T0TC=0;while(T0TC<ms);T0TCR=0x00;} //1ms delay using
Timer0
void EINT0_ISR(void)__irq{IO0PIN^=(0xFF<<16);EXTINT|=(0<<1);VICVectAddr=0;
} // Acknowledge end of ISR to VIC
int main(){IO0DIR|=0xFF<<16; // Set P0.16–P0.23 as output for LEDs%%IO0CLR=0xFF<<16; // Turn
off all LEDs initially %%PINSEL1|=(1<<0); // Enable EINT0 on P0.16 %% EXTMODE|=1<<0; // EINT0
as edge-sensitive %%EXTPOLAR&=~(1<<0); // Trigger on falling edge (active low) %%VICIntEnable|
=1<<14; // Enable EINT0 interrupt in VIC %% VICVectCntl0=(1<<5)|14; // Set slot 0 for EINT0, enable
slot %%VICVectAddr0=(unsigned long)EINT0_ISR; // Set address of ISR %%while(1); } // Infinite loop;
wait for interrupts

Button-Anticlockwise
#include <LPC214x.h> void delay_ms(unsigned int ms)
{T0PR=59999;T0TCR=0x02;T0TCR=0x01;T0TC=0;while(T0TC<ms);T0TCR=0x00;} // Delay function
using Timer0 (1ms)
VoidEINT0_ISR(void)__irq{IO0SET|=(1<<8);delay_ms(500);if(IO0PIN&(1<<11)){IO0CLR|
=(1<<11);}else{IO0SET|=(1<<11);} // Toggle motor direction on interrupt%% IO0SET|=(1<<8);
delay_ms(1000);EXTINT|=(1<<0);VICVectAddr=0;} // Clear interrupt flag and return int main(void)
{IO0DIR|=(1<<8)|(1<<11); // Set P0.8 (buzzer) and P0.11 (motor) as output IO0SET|=(1<<8);IO0SET|
=(1<<11); // Initially buzzer and motor ON PINSEL1|=(1<<0);PINSEL1&=~(1<<1); // Configure P0.16
as EINT0 EXTMODE|=(1<<0);EXTPOLAR&=~(1<<0); // Edge-sensitive and falling edge triggered
VICIntEnable|=(1<<14);VICVectCntl0=(1<<5)|14;VICVectAddr0=(unsigned long)EINT0_ISR; // Enable
and assign EINT0 ISR while(1);} // Infinite loop

One switch buzzer+direction


#include <LPC214x.h> void delay_ms(unsigned int ms)
{T0PR=59999;T0TCR=0x02;T0TCR=0x01;T0TC=0;while(T0TC<ms);T0TCR=0x00;} // Delay function
using Timer0 (1ms) void beep(){IO0SET=(1<<9);delay_ms(200);IO0CLR=(1<<9);delay_ms(200);} //
Beep buzzer on P0.9 for 200ms void EINT0_ISR(void)__irq{IO0SET=(1<<8); // Turn ON motor power
on P0.8 beep();if(IO0PIN&(1<<11)){IO0CLR|=(1<<11);beep();}else{IO0SET|=(1<<11);beep();} //
Toggle motor direction on P0.11 EXTINT|=(1<<0);VICVectAddr=0;} // Clear EINT0 interrupt and
return int main(void){IO0DIR|=(1<<8)|(1<<9)|(1<<11); // Set P0.8 (motor), P0.9 (buzzer), P0.11
(direction) as output IO0SET=(1<<8); // Turn ON motor power PINSEL1|
=(1<<0);PINSEL1&=~(1<<1); // Configure P0.16 as EINT0 EXTMODE|=(1<<0);EXTPOLAR&=~(1<<0); //
EINT0 is edge-sensitive and falling-edge triggered VICIntEnable|=(1<<14); // Enable EINT0 interrupt
VICVectCntl1=(1<<5)|14; // Assign ISR to slot 1 VICVectAddr1=(unsigned long)EINT0_ISR;while(1);} //
Infinite loop

Right inicator
#include<lpc214x.h>
int i;void delay();
int main(){PINSEL0 = 0X00;IO0DIR = 0xFF0000;while(1){for(i=16;i<=23;i++){IO0SET =(1<<i);k
delay();IO0CLR =(1<<i); } } }
void delay(){int k;for(k=0;k<1000000; k++);}

STEPPER
#include <LPC214x.h> // Include LPC214x header for register definitions
#define PHASE_A 0x00009000 // Phase A: P0.12 & P0.15 HIGH
#define PHASE_B 0x0000C000 // Phase B: P0.14 & P0.15 HIGH
#define PHASE_C 0x00006000 // Phase C: P0.13 & P0.14 HIGH
#define PHASE_D 0x00003000 // Phase D: P0.12 & P0.13 HIGH
#define STEP_ANGLE 1.8 // Each step rotates motor by 1.8 degrees
#define STEPS_PER_REV 200 // 360 / 1.8 = 200 steps per full rotation
void delay_ms(unsigned int ms) { T0PR = 59999; T0TCR = 0x02; T0TCR = 0x01; while (T0TC <
ms); T0TCR = 0x00; T0TC = 0; } // Millisecond delay using Timer0
void activate_phase(unsigned int phase) { IO0CLR = 0x0000F000; IO0SET = phase; delay_ms(10); }
// Activate a phase and wait 10 ms
void stepper_forward(int steps) { for (int i = 0; i < steps; i++) { activate_phase(PHASE_A);
activate_phase(PHASE_B); activate_phase(PHASE_C); activate_phase(PHASE_D); } } // Rotate
stepper clockwise
void stepper_reverse(int steps) { for (int i = 0; i < steps; i++) { activate_phase(PHASE_D);
activate_phase(PHASE_C); activate_phase(PHASE_B); activate_phase(PHASE_A); } } // Rotate
stepper counter-clockwise
void rotate_angle_forward(float angle_deg) { int steps = (int)(angle_deg / STEP_ANGLE);
stepper_forward(steps); } // Convert angle to steps and rotate forward
void rotate_angle_reverse(float angle_deg) { int steps = (int)(angle_deg / STEP_ANGLE);
stepper_reverse(steps); } // Convert angle to steps and rotate reverse
int main(void) { IO0DIR |= 0x0000F000; while (1) { rotate_angle_forward(90); delay_ms(1000);
rotate_angle_reverse(90); delay_ms(1000); } } // Main loop: Rotate 90° forward, wait, then 90°
reverse, repeat

You might also like