0% found this document useful (0 votes)
12 views8 pages

Aes Rec Int

The document contains code snippets for controlling a stepper motor, reading keyboard inputs, displaying characters on a seven-segment display, generating waveforms using a dual DAC, and simulating an elevator system. It includes functions for direction control of the motor, key detection, waveform selection, and elevator movement logic. The code is structured for a microcontroller, specifically the MK66F18, utilizing I2C communication and GPIO for hardware interaction.

Uploaded by

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

Aes Rec Int

The document contains code snippets for controlling a stepper motor, reading keyboard inputs, displaying characters on a seven-segment display, generating waveforms using a dual DAC, and simulating an elevator system. It includes functions for direction control of the motor, key detection, waveform selection, and elevator movement logic. The code is structured for a microcontroller, specifically the MK66F18, utilizing I2C communication and GPIO for hardware interaction.

Uploaded by

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

Stepper motor

#include <stdio.H> NVIC_EnableIRQ(PORTC_IRQn);

#include <MK66F18.h> // Enable PORTC interrupt

#include <stdio.h> I2C_init();

#include <stdlib.h> printf(" Stepper Motor\n");

#include "MK66_I2C_Functions.h" printf(" AntiClockwise\n");

while (1)

void PORTC_IRQHandler(void); {

void Direction(void); if (Dir) // Clockwise

static unsigned char Dir = 0x00; portAB_write(0x88, 0x00);

delay(60000);

void PORTC_IRQHandler(void) portAB_write(0x44, 0x00);

{ delay(60000);

PORTC_PCR19 |= (1 << 24); // Clear portAB_write(0x22, 0x00);


interrupt flag
delay(60000);
Direction();
portAB_write(0x11, 0x00);
}
delay(60000);

}
void Direction(void)
else // AntiClockwise
{
{
Dir = ~Dir; // Reverse the direction
portAB_write(0x11, 0x11);
if (Dir)
delay(60000);
printf(" Clockwise\n");
portAB_write(0x22,0x22);
else
delay(60000);
printf(" AntiClockwise\n");
portAB_write(0x44,0x44);
}
delay(60000);

portAB_write(0x88,0x88);
int main()
delay(60000);
{
}
SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK;
}
PORTC_PCR19 = PORT_PCR_MUX(0x1);
}
// Set pin as GPIO

PORTC_PCR19 |= PORT_PCR_IRQC(0x9);

// Configure interrupt
Keyboard {

#include <MK66F18.h> t >>= 1;

#include <stdio.h> if (t == 0) // Find which key was


pressed
#include <stdlib.h>
{
#include "MK66_I2C_Functions.h"
k = indx + j;

int getkey(void);
switch (k)

{
int main()
case 0x00: printf("\rkey Pressed = 00 ");
{
break;
I2C_init();
case 0x01: printf("\rkey Pressed = 01 ");
while (1) break;

getkey(); // Continuously read keypress case 0x02: printf("\rkey Pressed = 02 ");


break;
}
case 0x03: printf("\rkey Pressed = 03 ");
break;
int getkey(void) case 0x04: printf("\rkey Pressed = 04 ");
{ break;

unsigned char i, j, k, indx, t; case 0x05: printf("\rkey Pressed = 05 ");


break;
indx = 0x00;
case 0x06: printf("\rkey Pressed = 06 ");
break;
for (i = 0x00E; i >= 0x00B; i <<= 1) // Loop case 0x07: printf("\rkey Pressed = 07 ");
through scanlines break;
{ case 0x08: printf("\rkey Pressed = 08 ");
portC_write(i, 0xFF); // Write to break;
scanline case 0x09: printf("\rkey Pressed = 09 ");
Read_portAB(); // Read data break;
from port case 0x0A: printf("\rkey Pressed = . "); break;
t = IF_PortA_Read_Value; case 0x0B: printf("\rkey Pressed = + "); break;
t = ~t; case 0x0C: printf("\rkey Pressed = - "); break;

case 0x0D: printf("\rkey Pressed = x "); break;


if (t > 0) // Key pressed case 0x0E: printf("\rkey Pressed = / "); break;
{ case 0x0F: printf("\rkey Pressed = %% ");
delay(6000); // Debounce delay break;

case 0x10: printf("\rkey Pressed = AC ");


break;
for (j = 0; j <= 7; j++)
case 0x11: printf("\rkey Pressed = CE ");
break;

case 0x12: printf("\rkey Pressed = CHK ");


break;

case 0x13: printf("\rkey Pressed = = "); break;

case 0x14: printf("\rkey Pressed = MC ");


break;

case 0x15: printf("\rkey Pressed = MR ");


break;

case 0x16: printf("\rkey Pressed = M- ");


break;

case 0x17: printf("\rkey Pressed = M+ ");


break;

default : printf("\rUnknown Key "); break;

return k;

indx += 8; // Go to next scanline group

return 0;

}
Seven segment display s = display[i][j]; // Get pattern

#include <MK66F18.h>

#include <stdio.h> for (k = 0; k < 8; k++) // For each bit


of the character
#include <stdlib.h>
{
#include "MK66_I2C_Functions.h"
m = s & 0x80;

// Look-Up Table to display characters


if (m == 0)
unsigned char display[5][4] = {
portAB_write(0x00, 0x00); //
{0xff, 0xff, 0xff, 0xff}, // Blank
Send '0'
{0xf9, 0xa4, 0x86, 0xc6}, // E L E C
else
{0x87, 0xde, 0xc0, 0xbf}, // T R O N
portAB_write(0x00, 0x01); //
{0x92, 0x91, 0x92, 0xff}, // I C S (S = 0x92, Send '1'
last one blank)

{0x87, 0x86, 0xc8, 0x92} // S Y S T


portC_write(0x01, 0x00); // Clock
}; pulse

portC_write(0x00, 0x00);

int main()

{ s <<= 1; // Shift to next bit

unsigned int i, j, k, s, m, a; }

I2C_init(); // Initialize I2C for (a = 0; a < 51; a++) // Inter-


character delay
delay(600); // Stabilization delay
delay(90000);
delay(600);
}

}
portC_write(0x01, 0x00); // Clock control
line }

portC_write(0x00, 0x00); // Clear


return 0;

while (1) }

for (i = 0; i < 5; i++) // Number of 4-


character groups (5)

for (j = 0; j < 4; j++) // Number of


characters per group (4)

{
DUAL DAC 0x05,0x05,0x06,0x07,0x08,0x08,0x09,0x0a,

#include <MK66F18.h> 0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,

#include <stdio.h> 0x13,0x14,0x15,0x16,0x17,0x19,0x1a,0x1b,

#include <stdlib.h> 0x1c,0x1e,0x1f,0x20,0x22,0x23,0x25,0x26,

#include "MK66_I2C_Functions.h" 0x28,0x29,0x2a,0x2c,0x2d,0x2f,0x30,0x32,

0x34,0x35,0x37,0x38,0x3a,0x3b,0x3d,0x3e

// Sine wave samples };

unsigned char a[256] = {

0x40,0x42,0x43,0x45,0x46,0x48,0x49,0x4b, int main()

0x4c,0x4e,0x50,0x51,0x53,0x54,0x56,0x57, {

0x58,0x5a,0x5b,0x5d,0x5e,0x60,0x61,0x62, unsigned int i = 0;

0x64,0x65,0x66,0x67,0x69,0x6a,0x6b,0x6c,

0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74, I2C_init();

0x75,0x76,0x77,0x78,0x78,0x79,0x7a,0x7b,

0x7b,0x7c,0x7c,0x7d,0x7d,0x7e,0x7e,0x7e, while (1)

0x7f,0x7f,0x7f,0x80,0x80,0x80,0x80,0x80, {

0x80,0x80,0x80,0x80,0x80,0x80,0x7f,0x7f, printf("\nSelect number for desired


Waveform\n\t 1- Square\n\t 2- Triangular
0x7f,0x7e,0x7e,0x7e,0x7d,0x7d,0x7c,0x7c,
\n\t 3- Ramp/Sawtooth \n\t 4- Sine\n");
0x7b,0x7b,0x7a,0x79,0x78,0x78,0x77,0x76,

0x75,0x74,0x73,0x72,0x71,0x70,0x6f,0x6e,
switch (getchar())
0x6d,0x6c,0x6b,0x6a,0x69,0x67,0x66,0x65,
{
0x64,0x62,0x61,0x60,0x5e,0x5d,0x5b,0x5a,
case '1':
0x58,0x57,0x56,0x54,0x53,0x51,0x50,0x4e,
printf("\nPressed 1: Square Wave is
0x4c,0x4b,0x49,0x48,0x46,0x45,0x43,0x42, Selected\n");

0x40,0x3e,0x3d,0x3b,0x3a,0x38,0x37,0x35, while (1)

0x34,0x32,0x30,0x2f,0x2d,0x2c,0x2a,0x29, {

0x28,0x26,0x25,0x23,0x22,0x20,0x1f,0x1e, portAB_write(0xFF, 0xFF); // High

0x1c,0x1b,0x1a,0x19,0x17,0x16,0x15,0x14, portAB_write(0x00, 0x00); // Low

0x13,0x12,0x11,0x10,0x0f,0x0e,0x0d,0x0c, }

0x0b,0x0a,0x09,0x08,0x08,0x07,0x06,0x05,

0x05,0x04,0x04,0x03,0x03,0x02,0x02,0x02, case '2':

0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00, printf("\nPressed 2: Triangular Wave


is Selected\n");
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,
while (1)
0x01,0x02,0x02,0x02,0x03,0x03,0x04,0x04,
{
for (i = 0; i < 0xFF; i++)

portAB_write(i, i); // Rising

for (i = 0xFF; i > 0; i--)

portAB_write(i, i); // Falling

case '3':

printf("\nPressed 3: Ramp/Sawtooth
Wave is Selected\n");

while (1)

for (i = 0; i < 0xFF; i++)

portAB_write(i, i); // Ramp up

case '4':

printf("\nPressed 4: Sine Wave is


Selected\n");

while (1)

for (i = 0; i < 0xFF; i++)

portAB_write(a[i], a[i]); // Sine


waveform

default:

printf("\n Oops invalid Character\n


");

return 0;

}
Elevator if (c == 0)

#include <MK66F18.h> goto loop2; // No request yet, keep


checking
#include <stdio.h>

#include <stdlib.h>
loop3:
#include "MK66_I2C_Functions.h"
d = b;

b >>= 1;
int main()
c = b;
{

uint8_t samp = 0xFF;


if ((d &= 0x01) == 1)
uint8_t g, f;
{
int8_t b, c, d, temp, i = 0, k;
i += 1;
int8_t floor[9] = {0x00, 0x03, 0x06, 0x09,
0x00, 0xe0, 0xd3, 0xb6, 0x79}; goto loop3; // Find which floor is
requested
int8_t e;
}

I2C_init();
decide:

delay(5000);
e = 0; // Elevator at ground floor

if ((floor[i] - e) < 0x00)


loop1:
goto down;
f = 0x00;

f = e | 0xf0;
if ((floor[i] - e) == 0)
portAB_write(f, 0xFF);
goto reset;
i = 0;

// Elevator moving up
loop2:
e += 1;
Read_portAB();
f = e;
k = IF_PortB_Read_Value & 0xFF;
f |= 0xf0;
delay(5000);
portAB_write(f, 0xFF);
samp = k;
delay(400000);
b = samp;
goto decide;
b |= 0xf0;

c = b;
down:
c = (~c);
// Elevator moving down
e -= 1;

f = e;

f |= 0xf0;

portAB_write(f, 0xFF);

delay(400000);

goto decide;

reset:

temp = 0x05; // Dummy logic for turning off


LED/request

i += temp;

g = floor[i];

portAB_write(g, 0xFF);

goto loop1;

You might also like