100% found this document useful (1 vote)
274 views

Lab 07 Full Code

This document contains code for a main project that initializes systems, reads user input of step direction, mode, and RPM from a serial port, decodes and validates the input, and outputs the settings to an LCD display. It includes functions for initializing systems, reading buttons, communicating over serial, and getting and displaying string input from the user.

Uploaded by

api-302703706
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
274 views

Lab 07 Full Code

This document contains code for a main project that initializes systems, reads user input of step direction, mode, and RPM from a serial port, decodes and validates the input, and outputs the settings to an LCD display. It includes functions for initializing systems, reading buttons, communicating over serial, and getting and displaying string input from the user.

Uploaded by

api-302703706
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

/*************************MAIN PROJECT FUNCTIONS***************************/

#include "headers.h"
int main()
{
system_init();
printf("Enter step direction (CW or CCW), step mode (HS or FS), and RPM
(1-30)\n");
printf("separated by spaces, then press enter.\n");
while(1)
{
while(!getstrU1(str_buf, sizeof(str_buf)));
mCNIntEnable(FALSE);
writeLCD(0, CLR_DISPLAY);
sscanf(str_buf, "%s %s %d", dir_txt , mode_txt, &step_rate);
decode_string();
if(step_rate > 30)
{
step_rate = 30;
printf("RPM reset to allowed maximum of 30.\n");
sprintf(str_buf, "%s %s %d", dir_txt, mode_txt, step_rate);
}
LCD_puts(str_buf);
putsU1(str_buf);
step_period = rate_to_period(step_rate);
mCNIntEnable(TRUE);
}
}

return(1);
/* End of main */

void system_init(void)
{
Cerebot_mx7cK_setup();
initialize_uart1(19200, ODD_PARITY);
PMP_init();
LCD_init();
interrupt_init();
button_init();
}
/*************************CEREBOT INITIALIZATION***************************/
/*************************** CerebotMX7cK.c ******************************
**
** Author:
Richard Wall
** Date:
August 15, 2013
**
** Revised: 17Jan2014 (JFF)
**
** Configures the Crerbot MX7cK processor board for:
**
Sets LED1 through LED4 for output
**
Sets BTN1, BTN2 and BTN3 for input
**
** **************************************************************************/

/* The config_bits.h must be included once and only once in any project */
#include "config_bits.h"
/* Processor configuration definitions */
#include <plib.h>
#include "CerebotMX7cK.h"

/* PIC32 Perihperal Library header file */


/* Cerebot MX7cK pin definitions */

/* Cerebot_mx7ck_setup FUNCTION DESCRIPTION ********************************


* SYNTAX:
void Cerebot_mx7ck_setup(void);
* KEYWORDS:
Cerebot MX7cK, PIC32, setup
* Parameters:
None
* Return:
None
* Notes:
None
* END DESCRIPTION ********************************************************/
void Cerebot_mx7cK_setup(void)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Statement configures cache and wait states for maximum performance
* Given the options, this function will change the flash wait states, RAM
* wait state and enable prefetch cache but will not change the PBDIV.
* The PBDIV value is set via pragma FPBDIV in config_bits.h.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
DDPCONbits.JTAGEN = 0; // Statement is required to use Pin RA0 as IO
PORTSetPinsDigitalIn(IOPORT_A, BTN3); /* Set BTN3 as input */
PORTSetPinsDigitalIn(IOPORT_G, BTN1 | BTN2); /* Set BTN1 & BTN2 as inputs
*/

PORTSetPinsDigitalOut(IOPORT_G, BRD_LEDS);
/* Set BRD LEDs as output */
LATGCLR = BRD_LEDS;
/* Turn off LED1 through LED4 */
} /* End of Cerebot_mx7cK_setup */
/* End of CerebotMX7cK.c */
/**********************BUTTON READ/CONTROL FUNCTIONS************************/
void button_init()
{
PORTSetPinsDigitalIn(IOPORT_G, BTN1 | BTN2);
PORTSetPinsDigitalOut(IOPORT_B, SM_LEDS | BRD_BITS);
LATBCLR = SM_LEDS;
}
void decode_buttons(int button1, int button2)
{
if(button1 == 0 && button2 == 0)
{
step_dir = CW;
step_mode = HS;
step_period = 20;
}
else if(button1 == 64 && button2 == 0)
{
step_dir = CW;
step_mode = FS;
step_period = 40;

}
else if(button1 == 0 && button2 == 128)
{
step_dir = CCW;
step_mode = HS;
step_period = 30;
}
else if(button1 == 64 && button2 == 128)
{
step_dir = CCW;
step_mode = FS;
step_period = 24;
}
else
{
// change nothing
}

void button_output()
{
writeLCD(0, CLR_DISPLAY);
step_rate = period_to_rate(step_period);
switch(step_dir)
{
case CW:
strcpy(dir_txt,"CW");
break;
case CCW:
strcpy(dir_txt,"CCW");
break;
default:
break;
}

switch(step_mode)
{
case FS:
strcpy(mode_txt,"FS");
break;
case HS:
strcpy(mode_txt,"HS");
break;
default:
break;
}
sprintf(str_buf, "%s %s %d", dir_txt, mode_txt, step_rate);
LCD_puts(str_buf);
putsU1(str_buf);

/**************************COMMUNICATIONS SETUP*****************************/
/******************************
* Author:
Richard Wall
* Date:
August 12, 2013
*

comm.c

*********************************

* This code also uses the "printf" function on UART Serial Port 1
*
* Cerebot MX7cK requires UART crossover cable if connecting with PmodRS232
* Connector Pin
Pmod Pin
Function
*
2
4
MX7 Rx
*
3
3
MX7 Tx
*
5
5
Gnd
*
6
6
Vcc
***************************************************************************/
#include
#include
#include
#include

<plib.h>
<stdio.h>
"comm.h"
"CerebotMX7cK.h"

/* Required for printf */


/* Has info regarding the PB clock */

/* initialize_comm FUNCTION DESCRIPTION *************************************


* SYNTAX:
void initialize_comm(unsigned int baud, int parity);
* KEYWORDS:
UART, initialization, parity
* DESCRIPTION:
Initializes UART1 comm port for specified baud rate using
*
the assigned parity
* PARAMETER 1:
integer Baud rate
* PARAMETER 1:
integer (parity, NO_PARITY, ODD_PARITY, or EVEN_PARITY)
* RETURN VALUE:
None
*
* NOTES:
9 bit mode MARK or SPACE parity is not supported
* END DESCRIPTION **********************************************************/
void initialize_uart1(unsigned int baud, int parity)
{
unsigned int BRG;

}
/*
*
*
*
*
*
*
*

BRG=(unsigned short)(((float)FPB / ((float)4 * (float) baud))-(float)0.5);


switch(parity)
{
case NO_PARITY:
OpenUART1( (UART_EN | UART_BRGH_FOUR | UART_NO_PAR_8BIT),
(UART_RX_ENABLE | UART_TX_ENABLE) , BRG );
break;
case ODD_PARITY:
OpenUART1( (UART_EN | UART_BRGH_FOUR | UART_ODD_PAR_8BIT),
(UART_RX_ENABLE | UART_TX_ENABLE) , BRG );
break;
case EVEN_PARITY:
OpenUART1( (UART_EN | UART_BRGH_FOUR | UART_EVEN_PAR_8BIT),
(UART_RX_ENABLE | UART_TX_ENABLE) , BRG );
break;
}
printf("\n\nCerebot MX7ck Serial Port 1 ready\r\n");
_mon_putc FUNCTION DESCRIPTION ******************************************
SYNTAX:
void _mon_putc(char c);
KEYWORDS:
printf, console, monitor
DESCRIPTION:
Sets up serial port to function as console for printf.
Used only by system.
PARAMETER 1:
Character to send to monitor
RETURN VALUE:
None
NOTES:
This function will block until space is available

*
in the transmit buffer
* END DESCRIPTION **********************************************************/
void _mon_putc(char c)
{
while(BusyUART1());
WriteUART1((unsigned int) c);
} /* End of _mon_putc */
/* putcU1 FUNCTION DESCRIPTION ********************************************
* SYNTAX:
int putcU1( int c);
* KEYWORDS:
UART, character
* DESCRIPTION:
Waits while UART1 is busy (buffer full) and then sends a
*
single byte to UART1
* PARAMETER 1:
character to send
* RETURN VALUE:
character sent
* NOTES:
This function will block until space is available
*
in the transmit buffer
* END DESCRIPTION **********************************************************/
int putcU1( int c)
{
while(BusyUART1());
WriteUART1((unsigned int) c);
return c;
} /* End of putU1 */
/* getcU1 FUNCTION DESCRIPTION ********************************************
* SYNTAX:
int getcU1( char *ch);
* KEYWORDS:
character, get
* DESCRIPTION:
Checks for a new character to arrive to the UART1 serial
port.
* PARAMETER 1:
character pointer to character variable
* RETURN VALUE:
TRUE = new character received
*
FALSE = No new character
* NOTES:
This function does not block for no character received
* END DESCRIPTION ********************************************************/
int getcU1( char *ch)
{
if( !DataRdyUART1())
/* wait for new char to arrive */
return FALSE;
/* Return new data not available flag */
else
{
*ch = ReadUART1();
/* read the char from receive buffer */
return TRUE;
/* Return new data available flag */
}
}/* End of getU1 */
/* putsU1 FUNCTION DESCRIPTION ********************************************
* SYNTAX:
int putsU1( const char *s);
* KEYWORDS:
UART, string
* DESCRIPTION:
Sends a NULL terminates text string to UART1 with
*
CR and LF appended
* PARAMETER 1:
pointer to text string
* RETURN VALUE:
Logical TRUE
* NOTES:
This function will block until space is available
*
in the transmit buffer
* END DESCRIPTION **********************************************************/
int putsU1( const char *s)

putsUART1(s);
putcUART1( '\r');
putcUART1( '\n');
return 1;
} /* End of putsU1 */
/* getstrU1 FUNCTION DESCRIPTION ********************************************
* SYNTAX:
int getstrU1( char *s, unsigned int len );
* KEYWORDS:
string, get, UART
* DESCRIPTION:
This function assembles a line of text until the number of
*
characters assembled exceed the buffer length or an ASCII
*
CR control character is received. This function echo each
*
received character back to the UART. It also implements a
*
destructive backspace. ASCII LF control characters are
*
filtered out. The returned string has the CR character
*
removed and a NULL character appended to terminate the
text
*
string.
* PARAMETER 1:
character pointer to string
* PARAMETER 2:
integer maximum string length
* RETURN VALUE:
TRUE = EOL signaled by receiving return character
*
FALSE = waiting for end of line
* NOTES:
It is presumed that the buffer pointer or the buffer
length
*
does not change after the initial call asking to
*
receive a new line of text. This function does not block
*
for no character received. A timeout can be added to this
*
to free up resource. There is no way to restart the
function
*
after the first call until a EOL has been received. Hence
*
this function has denial of service security risks.
* END DESCRIPTION **********************************************************/
int getstrU1( char *s, unsigned int len )
{
static int eol = TRUE; /* End of input string flag*/
static unsigned int buf_len;
static char *p1;
/* copy #1 of the buffer pointer */
static char *p2;
/* copy #2 of the buffer pointer */
char ch;
/* Received new character */
if(eol)
{
p1 = s;
*/

/* Initial call to function - new line


*/
/* Make two copies of pointer - one for
*/
/* receiving characters and one for marking

p2 = s;

*/
}

/* the starting address of the string.

The

eol = FALSE;
/* second copy is needed for backspacing.
*/
buf_len = len - 1; /* Save max buffer length with room for NULL */

if(!(getcU1(&ch)))
{
return FALSE;
}
else
{

/*

Check for character received */

/* Bail out if not */

*p1 = ch;
/* Save new character in string buffer */
putcU1( *p1);
/* echo character */
switch(ch) /* Test for control characters */
{
case BACKSPACE:
if ( p1>p2)
/* prevent backing up past the start! */
{
putcU1( ' ');
/* overwrite the last character */
putcU1( BACKSPACE);
buf_len++;
p1--;
/* back off the pointer */
}
break;
case '\r':
/* carriage return */
putcU1( '\r');
/* echo character */
eol = TRUE;
/* Set end of line */
break;
case '\n':
/* newline (line feed), not EOL */
putcU1('\n');
// ignore but echo anyway
break;
// PuTTY only sends \r for Enter Key
default:
p1++;
/* increment buffer pointer */
buf_len--;
/* decrement length counter */
} // end of switch
} // end of else
if( buf_len == 0 || eol)
/* Check for buffer full or end of line */
{
*p1 = '\0';
/* add null terminate the string */
eol = TRUE;
return TRUE;
/* Set EOL flag */
}
return FALSE;
/* Not EOL */
} /* End of getstr */
/* End of comm.c */
/*******************************DELAY FUNCTION******************************/
void sw_delay(int ms)
{
int i;
while (ms --)
{
for (i = 0; i < COUNTS_PER_MS; i++)
{
// do nothing
}
}
}
/********************INTERRUPT INITIALIZATION AND ISRS*********************/
void interrupt_init()
{
timer1_interrupt_initialize();
cn_interrupt_initialize();
INTEnableSystemMultiVectoredInt();

INTEnableInterrupts();

void cn_interrupt_initialize()
{
unsigned int dummy;
// used to hold PORT read value
//Enable CN for BTN1 and BTN2
mCNOpen(CN_ON, (CN8_ENABLE | CN9_ENABLE), 0);
// Set CN interrupts priority level 1 sub priority level 0
mCNSetIntPriority(1);
mCNSetIntSubPriority(0);
// read port to clear difference
dummy = PORTReadBits(IOPORT_G, BTN1 | BTN2);
mCNClearIntFlag();
// Clear CN interrupt flag
mCNIntEnable(1);
// Enable CN interrupts
// Global interrupts must be enabled to complete the initialization
}
void timer1_interrupt_initialize()
{
// configure Timer 1 with internal clock; 1:1 prescale, PR1 for ms period
OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, T1_INTR_RATE-1);
// set up the timer interrupt with a priority of 2, sub priority of 0
mT1SetIntPriority(2);
mT1SetIntSubPriority(0);
mT1IntEnable(1);
// Global interrupts must be enabled to complete the initialization
}
void __ISR(_TIMER_1_VECTOR, IPL2) Timer1Handler(void)
{
while(!mT1GetIntFlag());
// Wait for interrupt flag to be set
mPORTBToggleBits(LEDA);
(step_delay) --;
if (step_delay <= 0)
{
step_code = stepper_state_machine(step_dir, step_mode);
output_to_stepper_motor(step_code);
step_delay = step_period;
}
mT1ClearIntFlag();
// Clear the interrupt flag
}
void __ISR(_CHANGE_NOTICE_VECTOR, IPL1) CNIntHandler(void)
{
LATBSET = LEDC;
sw_delay(20);
button1 = PORTReadBits(IOPORT_G, BTN1);
button2 = PORTReadBits(IOPORT_G, BTN2);
decode_buttons(button1, button2);
button_output();

mCNClearIntFlag();
LATBCLR = LEDC;
}
/*********************LCD SCREEN CONTROL FUNCTIONS**************************/
void LCD_init()
{
sw_delay(20);
writeLCD(0, SET_FUNCTION);
sw_delay(37);
writeLCD(0, SET_DISPLAY);
sw_delay(37);
writeLCD(0, CLR_DISPLAY);
sw_delay(2);
}
void LCD_puts(char *char_string)
{
while(*char_string)
// Look for end of string NULL character
{
LCD_putc(*char_string);
// Write character to LCD
sw_delay(10);
char_string++;
// Increment string pointer
}
}
void writeLCD(int addr, char c)
{
while(busyLCD());
// Wait for LCD to be ready
PMPSetAddress(addr);
// Set LCD RS control
PMPMasterWrite(c);
// initiate write sequence
}
char readLCD(int addr)
{
PMPSetAddress(addr);
// Set LCD RS control
mPMPMasterReadByte();
// initiate dummy read sequence
return mPMPMasterReadByte();// read actual data
}
void LCD_putc(char c)
{
char addr = readLCD(0);
addr = addr & ADDR_MASK;
switch(c)
{
case carriage_return:
if (addr >= 0x00 && addr
writeLCD(0, 0x80);
if (addr >= 0x40 && addr
writeLCD(0, 0xC0);
break;
case newline:
if (addr >= 0x00 && addr
writeLCD(0, 0xC0);
if (addr >= 0x40 && addr

<= 0x0f)
<= 0x4f)

<= 0x0f)
<= 0x4f)

writeLCD(0, 0x80);
break;
case space:
if(addr == 0x00 || addr == 0x40)
break;
else
writeLCD(1,c);
break;
default:
if (addr > 0x0f && addr < 0x40)
writeLCD(0, 0xC0);
else if (addr > 0x4f)
writeLCD(0, 0x80);
else
while(busyLCD());
writeLCD(1, c);
break;

}
char busyLCD()
{
char busy_flag = readLCD(0);
busy_flag = busy_flag & FLAG_MASK;
return busy_flag;
}
void clr_display()
{
writeLCD(0, CLR_DISPLAY);
writeLCD(0, SET_DISPLAY_HOME);
}
/******************PARALLEL MASTER PORT INITIALIZATION**********************/
void PMP_init()
{
int cfg1 = PMP_ON | PMP_READ_WRITE_EN | PMP_READ_POL_HI |
PMP_WRITE_POL_HI;
int cfg2 = PMP_DATA_BUS_8 | PMP_MODE_MASTER1 |
PMP_WAIT_BEG_4 | PMP_WAIT_MID_15 | PMP_WAIT_END_4;
int cfg3 = PMP_PEN_0;
// only PMA0 enabled
int cfg4 = PMP_INT_OFF;
// no interrupts used
mPMPOpen(cfg1, cfg2, cfg3, cfg4);
}
/**********************STEPPER MOTOR CONTROL FUNCTIONS**********************/
int stepper_state_machine(int step_dir, int step_mode)
{
LATBINV = LEDB;
enum {S0_5 = 1, S1, S1_5, S2, S2_5, S3, S3_5, S4};
static int state = S0_5;
switch(state)
{
case S0_5:

if (step_dir == CCW && step_mode == HS)


{
state = S1;
}
else if (step_dir == CCW && step_mode == FS)
{
state = S1_5;
}
else if (step_dir == CW && step_mode == HS)
{
state = S4;
}
else if (step_dir == CW && step_mode == FS)
{
state = S3_5;
}
break;
case S1:
if (step_dir == CCW && step_mode == HS)
{
state = S1_5;
}
else if (step_dir == CCW && step_mode == FS)
{
state = S2;
}
else if (step_dir == CW && step_mode == HS)
{
state = S0_5;
}
else if (step_dir == CW && step_mode == FS)
{
state = S4;
}
break;
case S1_5:
if (step_dir == CCW && step_mode == HS)
{
state = S2;
}
else if (step_dir == CCW && step_mode == FS)
{
state = S2_5;
}
else if (step_dir == CW && step_mode == HS)
{
state = S1;
}
else if (step_dir == CW && step_mode == FS)
{
state = S0_5;
}
break;
case S2:
if (step_dir == CCW && step_mode == HS)
{
state = S2_5;

}
else if (step_dir == CCW && step_mode == FS)
{
state = S3;
}
else if (step_dir == CW && step_mode == HS)
{
state = S1_5;
}
else if (step_dir == CW && step_mode == FS)
{
state = S1;
}
break;
case S2_5:
if (step_dir == CCW && step_mode == HS)
{
state = S3;
}
else if (step_dir == CCW && step_mode == FS)
{
state = S3_5;
}
else if (step_dir == CW && step_mode == HS)
{
state = S2;
}
else if (step_dir == CW && step_mode == FS)
{
state = S1_5;
}
break;
case S3:
if (step_dir == CCW && step_mode == HS)
{
state = S3_5;
}
else if (step_dir == CCW && step_mode == FS)
{
state = S4;
}
else if (step_dir == CW && step_mode == HS)
{
state = S2_5;
}
else if (step_dir == CW && step_mode == FS)
{
state = S2;
}
break;
case S3_5:
if (step_dir == CCW && step_mode == HS)
{
state = S4;
}
else if (step_dir == CCW && step_mode == FS)
{

state = S0_5;
}
else if (step_dir == CW && step_mode == HS)
{
state = S3;
}
else if (step_dir == CW && step_mode == FS)
{
state = S2_5;
}
break;
case S4:
if (step_dir == CCW && step_mode == HS)
{
state = S0_5;
}
else if (step_dir == CCW && step_mode == FS)
{
state = S1;
}
else if (step_dir == CW && step_mode == HS)
{
state = S3_5;
}
else if (step_dir == CW && step_mode == FS)
{
state = S3;
}
break;

}
if(state == S0_5)
return 0x0A;
else if (state ==
return 0x08;
else if (state ==
return 0x09;
else if (state ==
return 0x01;
else if (state ==
return 0x05;
else if (state ==
return 0x04;
else if (state ==
return 0x06;
else if (state ==
return 0x02;

S1)
S1_5)
S2)
S2_5)
S3)
S3_5)
S4)

}
void output_to_stepper_motor(int hex_code)
{
int stepper_instruct = hex_code << 7;
int MASK = 0x0780;
stepper_instruct = stepper_instruct & MASK;
int port_data = PORTB;
port_data = port_data & (~MASK);
port_data = port_data | stepper_instruct;
PORTB = port_data;

}
int rate_to_period(step_rate)
{
step_period = (60000 / (step_rate * step_mode * 100));
return step_period;
}
int period_to_rate(step_period)
{
step_rate = (60000/(step_period * step_mode * 100));
return step_rate;
}
void decode_string()
{
if ((!(strcmp(dir_txt,"CW")) || (!strcmp(dir_txt,"cw"))))
step_dir = CW;
else if ((!(strcmp(dir_txt,"CCW")) || (!strcmp(dir_txt,"ccw"))))
step_dir = CCW;
else{ };

if((!(strcmp(mode_txt,"FS")) || (!strcmp(mode_txt,"fs"))))
step_mode = FS;
else if ((!(strcmp(mode_txt,"HS")) || (!strcmp(mode_txt,"hs"))))
step_mode = HS;
else{ };

/***********************INCLUDED HEADER FILES*******************************/

Buttons.h:
#ifndef BUTTONS_H
#define
BUTTONS_H
// global variable initialization
int button1, button2;
/* Function prototypes */
void button_init(void);
void decode_buttons(int, int);
void button_output(void);
#endif

/* BUTTONS_H */

CerebotMX7cK.h:
#ifndef __CEREBOT_MX7ck_H__
#define __CEREBOT_MX7ck_H__
/* The following definitions are for IO assigned for the Cerebot MX7cK
* processor board. BIT_6, etc. are masks defined in ports.h. */
#define BTN1
#define BTN2

BIT_6
BIT_7

/* Port G */
/* Port G */

#define BTN3
#define
#define
#define
#define
#define
#define
BIT_10)

BIT_0

/* Port A */

LED1 BIT_12
/*
LED2 BIT_13
/*
LED3 BIT_14
/*
LED4 BIT_15
/*
BRD_LEDS
(LED1 | LED2 |
BRD_BITS
(BIT_2 | BIT_3

Port G */
Port G */
Port G */
Port G */
LED3 | LED4)
| BIT_4 | BIT_7 | BIT_8 | BIT_9 |

/* The following definitions are for IO assigned for the PmodSTEP stepper
* motor driver board, often used for instrumentation */
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define

LEDA
LEDB
LEDC
LEDD
LEDE
LEDF
LEDG
LEDH
SM1
SM2
SM3
SM4

BIT_2
BIT_3
BIT_4
BIT_6
BIT_7
BIT_8
BIT_9
BIT_10

#define SM_COILS
#define SM_LEDS
LEDH)

/*
/*
/*
/*
/*
/*
/*

Port
Port
Port
Port
Port
Port
Port
/*

B
B
B
B
B
B
B

*/
*/
*/
*/
*/
*/
*/
Port B */

LEDE
LEDF
LEDG
LEDH
(SM1 | SM2 | SM3 | SM4)
(LEDA | LEDB | LEDC | LEDD | LEDE | LEDF | LEDG |

/* These values, derived from settings in config_bits.h, affect


* system timing, e.g. Timers, I2C, UART, etc.) */
#define GetSystemClock()
#define GetInstructionClock()
#define GetPeripheralClock()
#define
#define
#define
#define
#define
#define

(80000000ul)
/* 80 MHz */
(GetSystemClock()/2)
(GetSystemClock()/8)

XTAL (8000000UL)
/* 8 MHz Xtal on Cerebot MX7cK */
SYS_FREQ
GetSystemClock()
/* System clock rate */
SYSTEM_FREQ GetSystemClock()
CORE_OSC
GetInstructionClock() /* Core clock rate */
FPB
GetPeripheralClock()
/* Peripheral bus clock rate */
CORE_MS_TICK_RATE (GetInstructionClock()/1000)

#endif
void Cerebot_mx7cK_setup(void);
/* End of CerebotMX7cK.h */

Comm.h:
#ifndef __MX7COMM_H__
#define __MX7COMM_H__

/* Cerebot MX7cK hardware initialization */

#define _UART1
#endif
#ifndef __COMM_H__
#define
#define
#define
#define
#define

__COMM_M__
BACKSPACE
NO_PARITY
ODD_PARITY
EVEN_PARITY

0x08
0
1
2

#endif
char str_buf[32];
char dir_txt[20];
char mode_txt[20];
void system_init();
void initialize_uart1(unsigned int baud, int parity);
void _mon_putc(char c); /* Called by system to implement "printf" functions */
int putcU1( int c);
/* Send single character to UART */
int getcU1( char *ch);
/* Get single character from UART */
int putsU1( const char *s);
/* Send string to UART1 */
int getstrU1( char *s, unsigned int len ); /* Get CR terminated string */
/* End of comm.h */

Config_bits.h:
/* configuration settings */
#pragma config FSRSSEL =
shadow set */
#pragma config FMIIEN
=
#pragma config FETHIO
=
*/
#pragma config FCANIO
=
#pragma config FUSBIDIO =
#pragma config FVBUSONIO=
#pragma config FPLLIDIV =
#pragma config FPLLMUL =
#pragma config UPLLIDIV =
#pragma config UPLLEN
=
#pragma config FPLLODIV =
#pragma config FNOSC
=
w/PLL */
#pragma config FSOSCEN =
#pragma config IESO
=
disabled */
#pragma config POSCMOD =
resonator */
#pragma config OSCIOFNC =
#pragma config FPBDIV
=
8 */
#pragma config FCKSM
=
Monitor disabled */
#pragma config WDTPS
=

PRIORITY_7 /* Interrupt priority 7 assigned to


OFF
ON

/* RMII Ethernet PHY */


/* Default Ethernet I/O pin configuration

OFF
/*
ON
/*
ON
/*
DIV_2 /*
MUL_20
DIV_1 /*
OFF
/*
DIV_1 /*
PRIPLL

Alternate CAN I/O pin configuration */


USBID pin controlled by USB module */
VBUSON pin controlled by USB module */
PLL input divider, divide by 2 */
/* PLL multiplier, multiply by 20 */
USB PLL input divider, divide by 2 */
USB PLL disabled */
PLL output divider, divide by 1 */
/* Primary oscillator (XT,HS, EC)

OFF
OFF

/* Secondary oscillator disabled */


/* Internal/external clock switchover

EC

/* Primary oscillator config,

8MHz

OFF
/* CLK0 disabled on OSCO pin */
DIV_8 /* Peripheral bus clock divisor, divide by
CSDCMD
PS1

/* Clock Switching & Fail Safe Clock


/* Watchdog timer postcaler, 1:1 */

#pragma config
#pragma config
PGC1 and PGD1 */
#pragma config
#pragma config
#pragma config

FWDTEN
ICESEL

= OFF
/* Watchdog timer disabled */
= ICS_PGx1
/* ICE/ICD Comm Channel Select, use

BWP
CP
DEBUG

= OFF
= OFF
= ON

/* Boot Flash Write Protect, disabled */


/* Code Protect disabled */
/* Background debugger */

/* End of Config_bits.h */

Delay.h:
#ifndef DELAY_H
#define
DELAY_H
#define COUNTS_PER_MS 8888
/* Function Prototypes*/
void sw_delay(int);
#endif

/* DELAY_H */

Interrupt.h:
#ifndef INTERRUPTS_H
#define
INTERRUPTS_H
/* Function prototypes */
void interrupt_init();
void cn_interrupt_initialize(void);
void timer1_interrupt_initialize(void);
#endif

/* INTERRUPTS_H */

LCDlib.h:
#ifndef LCDLIB_H
#define
LCDLIB_H
#define
#define
#define
#define
#define
#define
#define
#define
#define

SET_FUNCTION 0x38
SET_DISPLAY 0x0f
CLR_DISPLAY 0x01
SET_DISPLAY_HOME 0x80
FLAG_MASK 0x80
ADDR_MASK 0x7f
carriage_return 0x0D
newline 0x0A
space 0x20

/* Function Prototypes */
void LCD_init();
void LCD_puts(char *);
void writeLCD(int, char);
char readLCD(int);
void LCD_putc(char);
char busyLCD();
void clr_display();

/* End Function Prototypes */


#endif
/* LCDLIB_H */
PMPlib.h:
#ifndef PROJECT6_H
#define
PROJECT6_H
void PMP_init();
#endif

/* PROJECT6_H */

Stepper.h:
#ifndef STEPPER_H
#define
STEPPER_H
#define
#define
#define
#define

T1_PRESCALE
TOGGLES_PER_SEC
T1_TICK
BTN_POLL_PERIOD

#define
#define
#define
#define
#define

T1_INTR_RATE 10000
CW 1
CCW 2
FS 1
HS 2

1
1000
(FPB/T1_PRESCALE/TOGGLES_PER_SEC)
100

// global variable initialization


int step_dir, step_mode, step_period, step_rate;
int step_delay, step_code;
/* Function prototypes */
int stepper_state_machine();
void output_to_stepper_motor();
int rate_to_period(int);
int period_to_rate(int);
void decode_string();
#endif
/* STEPPER_H */
/******************************END HEADER FILES*****************************/

You might also like