Latest Article

Simple Python Compiler

From: http://www.jroller.com/languages/entry/python_writing_a_compiler_and I started experimenting with Python a few weeks ago. As a learning project I set myself the task of writing a compiler and interpreter for a simple 'while' language. I was very please with the result. Writing Python is like a dream. The work flow goes like this: think of the problem you [...]

Continue reading

About

My Name is Robert Cardona and I am a Computer Engineering and Mathematics Major in California. I have a few projects I am currently working on like Pastie.tk

Recent articles

8051 Project 04: Traffic Light Controller

/***********************************************************************
	Robert Cardona
		007900930
	Spring 2012
		M/W 1-3:15 PM
	Project04.c
	Project 04: Traffic Light Controller
		The purpose of this project is to review 8051 I/O port
		programming, hardware timer, and interrupt, learn how
		to interface character-based LCD, and learn how to design
		and implement a multi-state embedded system.
***********************************************************************/

// Includes
#include <reg52.h>
#include <intrins.h> // for _nop_()

// Definitions
#define LCD_BUS P1

// Lights
sbit ns_pedestrian_light = P3^0;
sbit ew_pedestrian_light = P2^2;

sbit ns_green = P2^0;
sbit ns_yellow = P0^7;
sbit ns_red = P0^6;

sbit ew_green = P0^5;
sbit ew_yellow = P0^4;
sbit ew_red = P0^3;

// Pedestrian Buttons
sbit ns_pedestrian_button = P3^2; // INT0
sbit ew_pedestrian_button = P3^3; // INT1

bit alert_mode = 1; // 1: alert / 0: time
unsigned char alert_line = 0;
unsigned char seconds = 0;

// LCD
sbit LCD_RS = P0^2;
sbit LCD_RW = P0^1;
sbit LCD_EN = P0^0;
sbit LCD_BUSY = P1^7;// connected to LCD_BUS_DB7 on the LCD

// Prototypes
void wait_lcd(void);
void enable_pulse(unsigned char direction);
void lcd_command_write(unsigned char command);
char lcd_gotoxy(unsigned char x, unsigned char y);
void lcd_data_write(unsigned char lcd_data);
void lcd_putchar(unsigned char c);
void lcd_putstring(unsigned char line, unsigned char *string);
void lcd_clear(void);
void init_lcd(void);
void delay_us(unsigned char n_usec);
void delay_ms(unsigned char n_msec);
void print_alert(unsigned char row_start);

// Traffic Lights
typedef enum{STATE_ONE, STATE_TWO, STATE_THREE, STATE_FOUR} light_state;
void traffic_lights_init(const light_state);
void traffic_lights_update(void);
void init_pedestrian_buttons(void);

static light_state system_light_state;
static unsigned char time_in_state;
static unsigned char interrupt_count = 0;

#define ON 0
#define OFF 1
#define S1_DURATION 6
#define S2_DURATION 2
#define S3_DURATION 6
#define S4_DURATION 2

// Operating System
void os_init_timer2(const unsigned char tick_ms);
void os_go_to_sleep(void);

void main(void) {
	init_lcd();
	init_pedestrian_buttons();
	traffic_lights_init(STATE_ONE);
	os_init_timer2(50);

	while(1) {
		os_go_to_sleep();
	}
}

// Operating System

void os_isr() interrupt 5 {
	TF2 = 0;
	if (++interrupt_count < 20) {
	 	return;
	}

	seconds++;
	interrupt_count = 0;
	traffic_lights_update();
}

void os_init_timer2(const unsigned char tick_ms) {
	unsigned long inc = ((unsigned long) tick_ms * (11059200UL / 1000))
		 				/ (unsigned long) 12;
	unsigned int reload = (unsigned int) (65536UL - inc);

	T2CON = 0x04;
	TH2 	= (unsigned char) (reload / 256);
	RCAP2H 	= (unsigned char) (reload / 256);
	TL2 	= (unsigned char) (reload % 256);
	RCAP2L 	= (unsigned char) (reload % 256);

	ET2 	= 1;
	TR2		= 1;
	EA		= 1;
}

void os_go_to_sleep(void) {
	PCON = PCON | 0x01;
}

// Traffic Lights

void traffic_lights_init(const light_state START_STATE) {
	system_light_state = START_STATE;
}

void traffic_lights_update(void) {
	switch(system_light_state) {
		case STATE_ONE: {
			ns_green = ON;
			ns_yellow = OFF;
			ns_red = OFF;

			ew_green = OFF;
			ew_yellow = OFF;
			ew_red = ON;

			if (time_in_state < 3) {
				ns_pedestrian_light = ON;
			} else {
				ns_pedestrian_light = ~ns_pedestrian_light;
			}

			if (alert_mode) {
				if (seconds % 3 == 0){
					print_alert(alert_line--);
				}
			} else {
				lcd_putstring(0, "Seconds Left NS: ");
				lcd_putchar(((6 - time_in_state) & 0x0F) + '0');
			}

			if (++time_in_state == S1_DURATION) {
				ns_pedestrian_light = OFF;
				alert_mode = 1;
			 	system_light_state = STATE_TWO;
				time_in_state = 0;
			}
			break;
		}
		case STATE_TWO: {
			ns_green = OFF;
			ns_yellow = ON;
			ns_red = OFF;

			ew_green = OFF;
			ew_yellow = OFF;
			ew_red = ON;

			if (seconds % 3 == 0){
				print_alert(alert_line--);
			}

			if (++time_in_state == S2_DURATION) {
			 	system_light_state = STATE_THREE;
				time_in_state = 0;
			}
			break;
		}
		case STATE_THREE: {
			ns_green = OFF;
			ns_yellow = OFF;
			ns_red = ON;

			ew_green = ON;
			ew_yellow = OFF;
			ew_red = OFF;

			if (time_in_state < 3) {
				ew_pedestrian_light = ON;
			} else {
				ew_pedestrian_light = ~ew_pedestrian_light;
			}

			if (alert_mode) {
				if (seconds % 3 == 0){
					print_alert(alert_line--);
				}
			} else {
				lcd_putstring(0, "Seconds Left EW: ");
				lcd_putchar(((6 - time_in_state) & 0x0F) + '0');
			}

			if (++time_in_state == S3_DURATION) {
				ew_pedestrian_light = OFF;
				alert_mode = 1;
			 	system_light_state = STATE_FOUR;
				time_in_state = 0;
			}
			break;

		}
		case STATE_FOUR: {
			ns_green = OFF;
			ns_yellow = OFF;
			ns_red = ON;

			ew_green = OFF;
			ew_yellow = ON;
			ew_red = OFF;

			if (seconds % 3 == 0){
				print_alert(alert_line--);
			}

			if (++time_in_state == S4_DURATION) {
			 	system_light_state = STATE_ONE;
				time_in_state = 0;
			}
			break;
		}
	}
}

void init_pedestrian_buttons(void) {
	IE = IE | 0x85;
	TCON = TCON | 0x05; // INT0/INT1 Edge Triggered
}

void ns_pedestrian_button_press(void) interrupt 0 {
	if (system_light_state == STATE_ONE) {
		lcd_clear();
		alert_mode = 0;
	} else if (system_light_state == STATE_THREE) {
		time_in_state = S3_DURATION - 1;
	}

}

void ew_pedestian_button_press(void) interrupt 2 {
	if (system_light_state == STATE_THREE) {
		lcd_clear();
		alert_mode = 0;
	} else if (system_light_state == STATE_ONE) {
		time_in_state = S1_DURATION - 1;
	}
}

// LCD Functions

/***********************************************************************
  Function Name :	wait_lcd()
  Description   :	This function waits for the lcd to finish processing
						data. It waits for the busy flag, which is
						LCD_D7, to be low, meaning that the LCD is now
						ready to recieve another command or data.
  Input         :	Void
  Output        :	Void
  Note          :	The do-while block has an enable pulse. The required
						minimum width for the enable to be high is
						450 ns, so we delay 1 us, approximately double
						the required delay.
***********************************************************************/
void wait_lcd(void) {

	LCD_BUSY	= 1;	// Make LCD_BUS_DB7 an input
	LCD_EN		= 1;
	LCD_RS		= 0;	// Command
	LCD_RW		= 1;	// Reading
	while (LCD_BUSY) {
		enable_pulse(0);// Low to High Pulse
	}
	delay_ms(1);
}

/***********************************************************************
  Function Name :	enable_pulse()
  Description   :	This provides an enable pulse to the lcd, which
						lasts for 1 ms in order for the LCD to latch
						the data present at the data pins. Since it
						must be a mininmun of 450 ns wide, we provide
						a 1 us delay
  Input         :	direction - if L->H or H->L Pulse
  						direction != 0 : Hight to Low
						direction == 0 : Low to High
  Output        :	Void
  Note          :
***********************************************************************/
void enable_pulse(unsigned char direction) {
	if (direction != 0){	// Hight to Low Pulse
		LCD_EN	= 1;
		delay_us(1);
		LCD_EN	= 0;
	} else { 		// Low to High Pulse
		LCD_EN	= 0;
		delay_us(1);
		LCD_EN	= 1;
	}
}

/***********************************************************************
  Function Name :	lcd_command_write()
  Description   :	This function sends a command to the LCD. Waits
  						until command has been processed.
  Input         :	command - the command you want to send
  Output        :	Void
  Note          :
***********************************************************************/
void lcd_command_write(unsigned char command) {
	LCD_BUS 	= command;
	LCD_RS		= 0;	// Command
	LCD_RW		= 0;	// Writing
	enable_pulse(1);	// Latch Data on LCD; Hight to Low Pulse
	wait_lcd();		// Wait until not busy

}

/***********************************************************************
  Function Name :	lcd_gotoxy()
  Description   :	Moves the cursor to a specified position.
  Input         :	x - which row: 0 or 1
  					y - which column 0 to 15
  Output        :	Void
  Note          :
***********************************************************************/
char lcd_gotoxy(unsigned char y, unsigned char x) {
	char return_value = 0;

	if ( (y > 3) && (x > 19) ) {
		return_value = -1;	// Error: Invalid Input
	} else {
		if (y == 0) {
			lcd_command_write(0x80 + x);	//First Line Position Move
		} else if (y == 1) {
			lcd_command_write(0xC0 + x);	//Second Line Position Move
		} else if (y == 2) {
			lcd_command_write(0x94 + x);	//Third Line Position Move
		} else if (y == 3) {
			lcd_command_write(0xD4 + x);	//Fourth Line Position Move
		}
	}

	return return_value;
}

/***********************************************************************
  Function Name :	lcd_data_write()
  Description   :	This functions sends data to write onto the LCD.
  						Waits for the LCD to finish processing.
  Input         :	lcd_data - the data to send to LCD.
  Output        :	Void
  Note          :
***********************************************************************/
void lcd_data_write(unsigned char lcd_data) {
	LCD_BUS 	= lcd_data;
	LCD_RS		= 1;	// Data
	LCD_RW		= 0;	// Writing
	enable_pulse(1);	// Latch Data on LCD; Hight to Low Pulse
	wait_lcd();			// Wait until not busy
}

/***********************************************************************
  Function Name :	lcd_putchar()
  Description   :	This function just outputs a character on the lcd
  						by calling the appropriate function. It is used
						to rename the data_write function into something
						more intuitive.
  Input         :	c - character to put on the lcd where the cursor is
  Output        :	Void
  Note          :
***********************************************************************/
void lcd_putchar(unsigned char c) {
	lcd_data_write(c);
}

/***********************************************************************
  Function Name :	lcd_putstring()
  Description   :	This function puts a string onto a specified line
  						on the LCD. It also will cur off the line if
						it goes over 16 characters.
  Input         :	line - Which line is to be written on: 0 or 1.
  					*string - a character array to put in LCD
  Output        :	Void
  Note          :
***********************************************************************/
void lcd_putstring(unsigned char line, unsigned char *string) {
	unsigned char len = 20;		// Max string size

	lcd_gotoxy(line, 0);
	while (*string != '\0' && len--) {
		lcd_putchar(*string);
		string++;
	}
}

void lcd_clear(void) {
	lcd_command_write(0x01);
}

void print_alert(unsigned char row_start) {
	unsigned char xdata alert_A[] = "Drive safe! Have a  ";
	unsigned char xdata alert_B[] = "good night and dont ";
	unsigned char xdata alert_C[] = "forget not to drink ";
	unsigned char xdata alert_D[] = "and drive!          ";

	// row_start = {0, 1, 2, 3}
	lcd_putstring(((row_start + 0) % 4), alert_A);
	lcd_putstring(((row_start + 1) % 4), alert_B);
	lcd_putstring(((row_start + 2) % 4), alert_C);
	lcd_putstring(((row_start + 3) % 4), alert_D);
}

/***********************************************************************
  Function Name :	lcd_init()
  Description   :	This function initializes the LCD; Specifically
  						it sets it to be an 8-bit interface, two lines,
						5x7 dots, it clears the screen and takes the
						cursor home, to locaton (0, 0). It also sets
						the LCD to increment the cursor after every
						write. It turns the display on, cursor on,
						and turns off the blink.
  Input         :	Void
  Output        :	Void
  Note          :
***********************************************************************/
void init_lcd(void) {
	delay_ms(15);				// Delay 15ms for Power Up

	// Send Setup Command Three Times
	LCD_BUS = 0x38;     //Function set: 2 Line, 8-bit, 5x7 dots
	LCD_RS   = 0;        //Selected command register
	LCD_RW   = 0;        //We are writing in data register
	LCD_EN   = 1;        //Enable H->L
	LCD_EN   = 0;
	delay_ms(15);

	LCD_BUS = 0x38;     //Function set: 2 Line, 8-bit, 5x7 dots
	LCD_RS   = 0;        //Selected command register
	LCD_RW   = 0;        //We are writing in data register
	LCD_EN   = 1;        //Enable H->L
	LCD_EN   = 0;
	delay_ms(15);

	LCD_BUS = 0x38;     //Function set: 2 Line, 8-bit, 5x7 dots
	LCD_RS   = 0;        //Selected command register
	LCD_RW   = 0;        //We are writing in data register
	LCD_EN   = 1;        //Enable H->L
	LCD_EN   = 0;
	delay_ms(15);

	lcd_command_write(0x38);	// 8-bit interface, two lines, 5x7 dots
	lcd_clear();				// Clear Screen, Curser (0,0)[HOME]
	lcd_command_write(0x06);	// Increment cursor position
	lcd_command_write(0x0C);	// Display On; Cursor Off w/ Blink Off

}

/***********************************************************************
  Function Name :	delay us()
  Description   :	Delays for a specified amount of micro seconds
  Input         :	n_usec - The amount of microseconds to delay
  Output        :	Void
  Note          :	Must include intrins.h
***********************************************************************/
void delay_us(unsigned char n_usec) {
	do {
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
	} while (--n_usec);
}

/***********************************************************************
  Function Name :	delay_ms()
  Description   :	Delays for a specified amount of milli seconds
  Input         :	n_msec - The amount of milleseconds to delay
  Output        :	Void
  Note          :	Must include intrins.h
***********************************************************************/
void delay_ms(unsigned char n_msec) {
	do
		delay_us(131);
	while (--n_msec);
}

ARM LCD

First working prototype :D

#include <LPC214x.H>                       /* LPC214x definitions */

void init_lcd(void);
void lcd_putstring(unsigned char line, unsigned char *string);
void lcd_clear(void);
void lcd_backlight_on(void);
int lcd_gotoxy(unsigned char x, unsigned char y);
void lcd_putchar(unsigned char c);

void delay(int count) {
	int i = 0, j = 0;

	for(i = 0;i < count; i++) {
		for(j = 0;j < 35; j++); // 10 us
	}
}

void lcd_rs(unsigned char bit) {
	if (bit != 0) {
		IO1SET = IO1SET | 0x01000000;
	} else {
		IO1CLR = IO1CLR | 0x01000000;
	}
}

void lcd_rw(unsigned char bit) {
	if (bit != 0) {
		IO1SET = IO1SET | 0x00800000;
	} else {
		IO1CLR = IO1CLR | 0x00800000;
	}
}

void lcd_en(unsigned char bit) {
	if (bit != 0) {
		IO1SET = IO1SET | 0x00400000;
	} else {
		IO1CLR = IO1CLR | 0x00400000;
	}
}

/***********************************************************************
  Function Name :	enable_pulse()
  Description   :	This provides an enable pulse to the lcd, which
						lasts for 1 ms in order for the LCD to latch
						the data present at the data pins. Since it
						must be a mininmun of 450 ns wide, we provide
						a 1 us delay
  Input         :	direction - if L->H or H->L Pulse
  						direction != 0 : Hight to Low
						direction == 0 : Low to High
  Output        :	Void
  Note          :
***********************************************************************/
void enable_pulse(unsigned char direction) {
	if (direction != 0){	// Hight to Low Pulse
		lcd_en(1);
		delay(1);
		lcd_en(0);
	} else { 				// Low to High Pulse
		lcd_en(0);
		delay(1);
		lcd_en(1);
	}
}

void lcd_bus_direction(unsigned char direction) {
	if (direction != 0) {
		IO0DIR = IO0DIR | 0x00003C00;
	} else {
		IO0DIR = IO0DIR & 0xFFFFC3FF;
	}
}

void lcd_bus_output(int lcd_port) {
  	IO0CLR = 0x00003C00;
  	IO0SET = lcd_port;
}

/***********************************************************************
  Function Name :	wait_lcd()
  Description   :	This function waits for the lcd to finish processing
						data. It waits for the busy flag, which is
						LCD_D7, to be low, meaning that the LCD is now
						ready to recieve another command or data.
  Input         :	Void
  Output        :	Void
  Note          :	The do-while block has an enable pulse. The required
						minimum width for the enable to be high is
						450 ns, so we delay 1 us, approximately double
						the required delay.
***********************************************************************/
void wait_lcd( void ) {
	lcd_bus_direction(0); //input
	lcd_en(1);
	lcd_rs(0);			// Command
	lcd_rw(1);			// Reading
	while(IO0PIN & 0x00002000){		/* wait for busy flag to become low */
		enable_pulse(0);
	}
	lcd_bus_direction(1);
	delay(100);
}

/***********************************************************************
  Function Name :	lcd_command_write()
  Description   :	This function sends a command to the LCD. Waits
  						until command has been processed.
  Input         :	command - the command you want to send
  Output        :	Void
  Note          :
***********************************************************************/
void lcd_command_write( unsigned char command ) {
	unsigned int lcd_port;

	lcd_port = ((command >> 4) & 0x0F) << 10;
  	lcd_rs(0);
  	lcd_rw(0);
  	lcd_bus_output(lcd_port);
  	enable_pulse(1);

  	lcd_port = ((command) &0x0F) << 10;
	lcd_rs(0);
	lcd_rw(0);
  	lcd_bus_output(lcd_port);
	enable_pulse(1);

  	wait_lcd();
}

void set_lcd_port_output(void) {
	// Make EN, RS, RW outputs
	IO1DIR = IO1DIR | 0x01C00000;

	// Clear EN, RS, RW
	IO1CLR = IO1CLR | 0x01C00000;

	// Make 4-pin Data Bus Output
	lcd_bus_direction(1);
}

void lcd_clear( void) {
	lcd_command_write( 0x01 );
}

/***********************************************************************
  Function Name :	lcd_gotoxy()
  Description   :	Moves the cursor to a specified position.
  Input         :	x - which row: 0 or 1
  					y - which column 0 to 15
  Output        :	Void
  Note          :
***********************************************************************/
int lcd_gotoxy( unsigned char x, unsigned char y) {
	int retval = 0;
	if( (x > 1) && (y > 15) ) {
		retval = -1;
	} else {
		if( x == 0 ) {
			lcd_command_write( 0x80 + y );		/* command - position cursor at 0x00 (0x80 + 0x00 ) */
		} else if( x==1 ){
			lcd_command_write( 0xC0 + y );		/* command - position cursor at 0x40 (0x80 + 0x00 ) */
		}
	}
	return retval;
}

/***********************************************************************
  Function Name :	lcd_data_write()
  Description   :	This functions sends data to write onto the LCD.
  						Waits for the LCD to finish processing.
  Input         :	lcd_data - the data to send to LCD.
  Output        :	Void
  Note          :
***********************************************************************/
void lcd_data_write( unsigned char data ) {
	unsigned int lcd_port = 0;

	lcd_port = ((data >> 4) & 0x0F) << 10;
	lcd_rs(1);
	lcd_rw(0);
  	lcd_bus_output(lcd_port);
 	enable_pulse(1);

  	lcd_port = ((data) & 0x0F) << 10;
	lcd_rs(1);
	lcd_rw(0);
	lcd_bus_output(lcd_port);
  	enable_pulse(1);

  	wait_lcd();
}

/***********************************************************************
  Function Name :	lcd_putchar()
  Description   :	This function just outputs a character on the lcd
  						by calling the appropriate function. It is used
						to rename the data_write function into something
						more intuitive.
  Input         :	c - character to put on the lcd where the cursor is
  Output        :	Void
  Note          :
***********************************************************************/
void lcd_putchar( unsigned char c ) {
	lcd_data_write( c );
}

/***********************************************************************
  Function Name :	lcd_putstring()
  Description   :	This function puts a string onto a specified line
  						on the LCD. It also will cur off the line if
						it goes over 16 characters.
  Input         :	line - Which line is to be written on: 0 or 1.
  					*string - a character array to put in LCD
  Output        :	Void
  Note          :
***********************************************************************/
void lcd_putstring( unsigned char line, unsigned char *string ) {
	unsigned char len = 16;

	lcd_gotoxy( line, 0 );
	while(*string != '\0' && len--) {
		lcd_putchar( *string );
	string++;
	}
}

/***********************************************************************
  Function Name :	lcd_init()
  Description   :	This function initializes the LCD; Specifically
  						it sets it to be an 8-bit interface, two lines,
						5x7 dots, it clears the screen and takes the
						cursor home, to locaton (0, 0). It also sets
						the LCD to increment the cursor after every
						write. It turns the display on, cursor on,
						and turns off the blink.
  Input         :	Void
  Output        :	Void
  Note          :
***********************************************************************/
void init_lcd( void ) {
	set_lcd_port_output();
	delay(100*100);
	lcd_command_write(0x28);     /*   4-bit interface, two line, 5X7 dots.        */
	lcd_clear() ;                /*   LCD clear                                   */
	lcd_command_write(0x02);     /*   cursor home                                 */
	lcd_command_write(0x06);     /*   cursor move direction                       */
	lcd_command_write(0x0C) ;    /*   display on      */
	lcd_gotoxy(0, 0);
	//lcd_clear();
	lcd_putstring(0,"Robert -- Karina");
	lcd_putstring(1,"Cardona  Rosales");
}

8051 LCD

The thing to note is that to read busy flag you need to output a low to high pulse. and when latching data or commands, its a high to low pulse :(

/*
	Robert Cardona
		007900930
	Spring 2012
		M/W 1-3:15 PM
	Project04.c
	Project 04: Traffic Light Controller
		The purpose of this project is to review 8051 I/O port programming,
		hardware timer, and interrupt, learn how to interface character-based
		LCD, and learn how to design and implement a multi-state embedded system.
*/
//---------------------------------------------------------------------------------------;

// Includes
#include <reg51.h>
#include <intrins.h> // for _nop_()

// Definitions
#define LCD_BUS P1

sbit LCD_RS = P0^2;
sbit LCD_RW = P0^1;
sbit LCD_EN = P0^0;
sbit LCD_BUSY = P1^7;// connected to LCD_BUS_DB7 on the LCD

// Prototypes
void wait_lcd(void);
void enable_pulse(unsigned char direction);
void lcd_command_write(unsigned char command);
char lcd_gotoxy(unsigned char x, unsigned char y);
void lcd_data_write(unsigned char lcd_data);
void lcd_putchar(unsigned char c);
void lcd_putstring(unsigned char line, unsigned char *string);
void init_lcd(void);
void delay_us(unsigned char n_usec);
void delay_ms(unsigned char n_msec);

void main(void) {
	init_lcd();
	while (1){
	lcd_putstring(0,"Robert Cardona");
	lcd_putstring(1,"<3");
	}
}

/***********************************************************************
  Function Name :	wait_lcd()
  Description   :	This function waits for the lcd to finish processing
						data. It waits for the busy flag, which is
						LCD_D7, to be low, meaning that the LCD is now
						ready to recieve another command or data.
  Input         :	Void
  Output        :	Void
  Note          :	The do-while block has an enable pulse. The required
						minimum width for the enable to be high is
						450 ns, so we delay 1 us, approximately double
						the required delay.
***********************************************************************/
void wait_lcd(void) {

	LCD_BUSY	= 1;	// Make LCD_BUS_DB7 an input
	LCD_EN		= 1;
	LCD_RS		= 0;	// Command
	LCD_RW		= 1;	// Reading
	while (LCD_BUSY) {
		enable_pulse(0);// Low to High Pulse
	}
}

/***********************************************************************
  Function Name :	enable_pulse()
  Description   :	This provides an enable pulse to the lcd, which
						lasts for 1 ms in order for the LCD to latch
						the data present at the data pins. Since it
						must be a mininmun of 450 ns wide, we provide
						a 1 us delay
  Input         :	direction - if L->H or H->L Pulse
  						direction != 0 : Hight to Low
						direction == 0 : Low to High
  Output        :	Void
  Note          :
***********************************************************************/
void enable_pulse(unsigned char direction) {
	if (direction){	// Hight to Low Pulse
		LCD_EN	= 1;
		delay_us(1);
		LCD_EN	= 0;
	} else { 		// Low to High Pulse
		LCD_EN	= 0;
		delay_us(1);
		LCD_EN	= 1;
	}
}

/***********************************************************************
  Function Name :	lcd_command_write()
  Description   :	This function sends a command to the LCD. Waits
  						until command has been processed.
  Input         :	command - the command you want to send
  Output        :	Void
  Note          :
***********************************************************************/
void lcd_command_write(unsigned char command) {
	LCD_BUS 	= command;
	LCD_RS		= 0;	// Command
	LCD_RW		= 0;	// Writing
	enable_pulse(1);	// Latch Data on LCD; Hight to Low Pulse
	wait_lcd();		// Wait until not busy

}

/***********************************************************************
  Function Name :	lcd_gotoxy()
  Description   :	Moves the cursor to a specified position.
  Input         :	x - which row: 0 or 1
  					y - which column 0 to 15
  Output        :	Void
  Note          :
***********************************************************************/
char lcd_gotoxy(unsigned char x, unsigned char y) {
	char return_value = 0;

	if ( (x > 1) && (y > 15) ) {
		return_value = -1;	// Error: Invalid Input
	} else {
		if (x == 0) {
			lcd_command_write(0x80 + y);	//First Line Position Move
		} else {
			lcd_command_write(0xC0 + y);	//Second Line Position Move
		}
	}

	return return_value;
}

/***********************************************************************
  Function Name :	lcd_data_write()
  Description   :	This functions sends data to write onto the LCD.
  						Waits for the LCD to finish processing.
  Input         :	lcd_data - the data to send to LCD.
  Output        :	Void
  Note          :
***********************************************************************/
void lcd_data_write(unsigned char lcd_data) {
	LCD_BUS 	= lcd_data;
	LCD_RS		= 1;	// Data
	LCD_RW		= 0;	// Writing
	enable_pulse(1);	// Latch Data on LCD; Hight to Low Pulse
	wait_lcd();			// Wait until not busy
}

/***********************************************************************
  Function Name :	lcd_putchar()
  Description   :	This function just outputs a character on the lcd
  						by calling the appropriate function. It is used
						to rename the data_write function into something
						more intuitive.
  Input         :	c - character to put on the lcd where the cursor is
  Output        :	Void
  Note          :
***********************************************************************/
void lcd_putchar(unsigned char c) {
	lcd_data_write(c);
}

/***********************************************************************
  Function Name :	lcd_putstring()
  Description   :	This function puts a string onto a specified line
  						on the LCD. It also will cur off the line if
						it goes over 16 characters.
  Input         :	line - Which line is to be written on: 0 or 1.
  					*string - a character array to put in LCD
  Output        :	Void
  Note          :
***********************************************************************/
void lcd_putstring(unsigned char line, unsigned char *string) {
	unsigned char len = 16;		// Max string size

	lcd_gotoxy(line, 0);
	while (*string != '\0' && len--) {
		lcd_putchar(*string);
		string++;
	}
}

/***********************************************************************
  Function Name :	lcd_init()
  Description   :	This function initializes the LCD; Specifically
  						it sets it to be an 8-bit interface, two lines,
						5x7 dots, it clears the screen and takes the
						cursor home, to locaton (0, 0). It also sets
						the LCD to increment the cursor after every
						write. It turns the display on, cursor on,
						and turns off the blink.
  Input         :	Void
  Output        :	Void
  Note          :
***********************************************************************/
void init_lcd(void) {
	delay_ms(15);				// Delay 15ms for Power Up
	lcd_command_write(0x38);	// 8-bit interface, two lines, 5x7 dots
	lcd_command_write(0x01);	// Clear Screen, Curser (0,0)[HOME]
	lcd_command_write(0x06);	// Increment cursor position
	lcd_command_write(0x0C);	// Display On; Cursor Off w/ Blink Off

}

/***********************************************************************
  Function Name :	delay us()
  Description   :	Delays for a specified amount of micro seconds
  Input         :	n_usec - The amount of microseconds to delay
  Output        :	Void
  Note          :	Must include intrins.h
***********************************************************************/
void delay_us(unsigned char n_usec) {
	do {
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
	} while (--n_usec);
}

/***********************************************************************
  Function Name :	delay_ms()
  Description   :	Delays for a specified amount of milli seconds
  Input         :	n_msec - The amount of milleseconds to delay
  Output        :	Void
  Note          :	Must include intrins.h
***********************************************************************/
void delay_ms(unsigned char n_msec) {
	do
		delay_us(131);
	while (--n_msec);
}

ARM Lab06: DAC

;----------------------------------------------------------------------------------------;
;	Lab07
;	Robert Cardona CSULB CECS 347
;	Spring 2012
;	Lab07.s
;
;	ARM Assembly Language / Fundamentals and Techniques
;		William Hohl, 1st Edition
;----------------------------------------------------------------------------------------;

; RVMDK requires a reset handler - normally found in startup.s code
; name should be "Reset_Handler" (not case sensitive)
GDR				RN		r4
Level			RN		r5
ADCR			EQU		0xE0034000
ADGDR			EQU		0xE0034004
ADCR_Val		EQU		0x00210E08
DACREG			EQU		0xE006C000

SRAM_BASE		EQU		0x40000000

Mode_USR        EQU     0x10
Mode_FIQ        EQU     0x11
Mode_IRQ        EQU     0x12
Mode_SVC        EQU     0x13
Mode_ABT        EQU     0x17
Mode_UND        EQU     0x1B
Mode_SYS        EQU     0x1F

I_Bit           EQU     0x80            ; when I bit is set, IRQ is disabled
F_Bit           EQU     0x40            ; when F bit is set, FIQ is disabled

UND_Stack_Size  EQU     0x00000000
SVC_Stack_Size  EQU     0x00000008
ABT_Stack_Size  EQU     0x00000000
FIQ_Stack_Size  EQU     0x00000000
IRQ_Stack_Size  EQU     0x00000080
USR_Stack_Size  EQU     0x00000400

ISR_Stack_Size  EQU     (UND_Stack_Size + SVC_Stack_Size + ABT_Stack_Size + \
                         FIQ_Stack_Size + IRQ_Stack_Size)

                AREA    STACK, NOINIT, READWRITE, ALIGN=3

Stack_Mem       SPACE   USR_Stack_Size
__initial_sp    SPACE   ISR_Stack_Size

Stack_Top

Heap_Size       EQU     0x00000000

                AREA    HEAP, NOINIT, READWRITE, ALIGN=3
__heap_base
Heap_Mem        SPACE   Heap_Size
__heap_limit

; VPBDIV definitions
VPBDIV          EQU     0xE01FC100      ; VPBDIV Address
VPBDIV_SETUP    EQU     1
VPBDIV_Val      EQU     0x01
VPBDIV_Val_0    EQU     0x00			; One-fourth of processor clock
VPBDIV_Val_1    EQU     0x01			; Same as processor clock
VPBDIV_Val_2    EQU     0x02			; One-half of processor clock

; Phase Locked Loop (PLL) definitions
PLL_BASE        EQU     0xE01FC080      ; PLL Base Address
PLLCON_OFS      EQU     0x00            ; PLL Control Offset
PLLCFG_OFS      EQU     0x04            ; PLL Configuration Offset
PLLSTAT_OFS     EQU     0x08            ; PLL Status Offset
PLLFEED_OFS     EQU     0x0C            ; PLL Feed Offset
PLLCON_PLLE     EQU     (1<<0)          ; PLL Enable
PLLCON_PLLC     EQU     (1<<1)          ; PLL Connect
PLLCFG_MSEL     EQU     (0x04<<0)       ; PLL Multiplier  (M=5)
PLLCFG_PSEL     EQU     (0x02<<5)       ; PLL Divider	  (P=2)
PLLSTAT_PLOCK   EQU     (1<<10)         ; PLL Lock Status

PLL_SETUP       EQU     1
PLLCFG_Val      EQU     PLLCFG_MSEL:OR:PLLCFG_PSEL ; 60Mhz

; Memory Accelerator Module (MAM) definitions
MAM_BASE        EQU     0xE01FC000      ; MAM Base Address
MAMCR_OFS       EQU     0x00            ; MAM Control Offset
MAMTIM_OFS      EQU     0x04            ; MAM Timing Offset

MAM_SETUP       EQU     1
MAMCR_Val       EQU     0x00000002
MAMTIM_Val      EQU     0x00000004

; External Memory Controller (EMC) definitions
EMC_BASE        EQU     0xFFE00000      ; EMC Base Address
BCFG0_OFS       EQU     0x00            ; BCFG0 Offset
BCFG1_OFS       EQU     0x04            ; BCFG1 Offset
BCFG2_OFS       EQU     0x08            ; BCFG2 Offset
BCFG3_OFS       EQU     0x0C            ; BCFG3 Offset

;// <e> External Memory Controller (EMC)
EMC_SETUP       EQU     0

BCFG0_SETUP EQU         0
BCFG0_Val   EQU         0x0000FBEF

BCFG1_SETUP EQU         0
BCFG1_Val   EQU         0x0000FBEF

BCFG2_SETUP EQU         0
BCFG2_Val   EQU         0x0000FBEF

BCFG3_SETUP EQU         0
BCFG3_Val   EQU         0x0000FBEF

;// </e> End of EMC

; External Memory Pins Definitions
PINSEL0			EQU		0xE002C000
PINSEL1			EQU		0xE002C004
PINSEL2			EQU		0xE002C014

PINSEL0_Val     EQU     0x00000000
PINSEL1_Val     EQU     0x10080000      ; PINSEL2 Address
PINSEL2_Val     EQU     0x00000000      ; CS0..3, OE, WE, BLS0..3,
                                        ; D0..31, A2..23, JTAG Pins

; UART 0 Registers and Settings
UART0_BASE		EQU		0xE000C000		; UART0 Base Address
U0RBR_0FS		EQU		0x00			; Recieve Buffer Register
U0THR_0FS		EQU		0x00			; Transmit Holding Register
U0DLL_0FS		EQU		0x00			; Divisor Latch LSB (DLA=1)
U0DLM_0FS		EQU		0x04			; Divisor Latch MSB (DLA=1)
U0IER_0FS		EQU		0x04			; Interrupt Enable Register
U0IID_0FS		EQU		0x08			; Interrupt ID Register
U0FCR_0FS	    EQU		0x08			; FIFO Control Register
U0LCR_0FS		EQU		0x0C			; Line Control Register
U0LSR_0FS		EQU		0x14			; Line Status Register
U0SCR_0FS		EQU		0x1C			; Scratch Pad Register (8-bits)
U0ACR_0FS		EQU		0x20			; Auto-Baud Control Register
U0FDR_0FS		EQU		0x28			; Fractional Divider Register
U0TER_0FS		EQU		0x30			; Tx Enable

; UART 1 Registers and Settings
UART1_BASE		EQU		0xE0010000		; UART0 Base Address
U1RBR_0FS		EQU		0x00			; Recieve Buffer Register
U1THR_0FS		EQU		0x00			; Transmit Holding Register
U1DLL_0FS		EQU		0x00			; Divisor Latch LSB (DLA=1)
U1DLM_0FS		EQU		0x04			; Divisor Latch MSB (DLA=1)
U1IER_0FS		EQU		0x04			; Interrupt Enable Register
U1IID_0FS		EQU		0x08			; Interrupt ID Register
U1FCR_0FS	    EQU		0x08			; FIFO Control Register
U1LCR_0FS		EQU		0x0C			; Line Control Register
U1LSR_0FS		EQU		0x14			; Line Status Register
U1SCR_0FS		EQU		0x1C			; Scratch Pad Register (8-bits)
U1ACR_0FS		EQU		0x20			; Auto-Baud Control Register
U1FDR_0FS		EQU		0x28			; Fractional Divider Register
U1TER_0FS		EQU		0x30			; Tx Enable

                PRESERVE8

                AREA    RESET, CODE, READONLY
                ARM

; Exception Vectors
;  Mapped to Address 0.
;  Absolute addressing mode must be used.
;  Dummy Handlers are implemented as infinite loops which can be modified.

Vectors         LDR     PC, Reset_Addr
                LDR     PC, Undef_Addr
                LDR     PC, SWI_Addr
                LDR     PC, PAbt_Addr
                LDR     PC, DAbt_Addr
                NOP                            ; Reserved Vector
;               LDR     PC, IRQ_Addr
                LDR     PC, [PC, #-0x0FF0]     ; Vector from VicVectAddr
                LDR     PC, FIQ_Addr

Reset_Addr      DCD     Reset_Handler
Undef_Addr      DCD     Undef_Handler
SWI_Addr        DCD     SWI_Handler
PAbt_Addr       DCD     PAbt_Handler
DAbt_Addr       DCD     DAbt_Handler
                DCD     0                      ; Reserved Address
IRQ_Addr        DCD     IRQ_Handler
FIQ_Addr        DCD     FIQ_Handler

Undef_Handler   B       Undef_Handler
SWI_Handler     B       SWI_Handler
PAbt_Handler    B       PAbt_Handler
DAbt_Handler    B       DAbt_Handler
IRQ_Handler     B       IRQ_Handler
FIQ_Handler     B       FIQ_Handler

; Reset Handler

                EXPORT  Reset_Handler
Reset_Handler 

; Setup External Memory Pins
                IF      :D EF:EXTERNAL_MODE
                LDR     R0, =PINSEL2
                LDR     R1, =PINSEL2_Val
                STR     R1, [R0]
                ENDIF

; Setup External Memory Pins
                IF      :D EF:EXTERNAL_MODE
                LDR     R0, =PINSEL0
                LDR     R1, =PINSEL0_Val
                STR     R1, [R0]
                ENDIF

; Setup External Memory Controller
                IF      EMC_SETUP <> 0
                LDR     R0, =EMC_BASE

                IF      BCFG0_SETUP <> 0
                LDR     R1, =BCFG0_Val
                STR     R1, [R0, #BCFG0_OFS]
                ENDIF

                IF      BCFG1_SETUP <> 0
                LDR     R1, =BCFG1_Val
                STR     R1, [R0, #BCFG1_OFS]
                ENDIF

                IF      BCFG2_SETUP <> 0
                LDR     R1, =BCFG2_Val
                STR     R1, [R0, #BCFG2_OFS]
                ENDIF

                IF      BCFG3_SETUP <> 0
                LDR     R1, =BCFG3_Val
                STR     R1, [R0, #BCFG3_OFS]
                ENDIF

                ENDIF   ; EMC_SETUP

; Setup VPBDIV
                IF      VPBDIV_SETUP <> 0
                LDR     R0, =VPBDIV
                LDR     R1, =VPBDIV_Val
                STR     R1, [R0]
                ENDIF

; Setup PLL
                IF      PLL_SETUP <> 0
                LDR     R0, =PLL_BASE
                MOV     R1, #0xAA
                MOV     R2, #0x55

;  Configure and Enable PLL
                MOV     R3, #PLLCFG_Val
                STR     R3, [R0, #PLLCFG_OFS]
                MOV     R3, #PLLCON_PLLE
                STR     R3, [R0, #PLLCON_OFS]
                STR     R1, [R0, #PLLFEED_OFS]
                STR     R2, [R0, #PLLFEED_OFS]

;  Wait until PLL Locked
PLL_Loop        LDR     R3, [R0, #PLLSTAT_OFS]
                ANDS    R3, R3, #PLLSTAT_PLOCK
                BEQ     PLL_Loop

;  Switch to PLL Clock
                MOV     R3, #(PLLCON_PLLE:OR:PLLCON_PLLC)
                STR     R3, [R0, #PLLCON_OFS]
                STR     R1, [R0, #PLLFEED_OFS]
                STR     R2, [R0, #PLLFEED_OFS]
                ENDIF   ; PLL_SETUP

; Setup MAM
                IF      MAM_SETUP <> 0
                LDR     R0, =MAM_BASE
                MOV     R1, #MAMTIM_Val
                STR     R1, [R0, #MAMTIM_OFS]
                MOV     R1, #MAMCR_Val
                STR     R1, [R0, #MAMCR_OFS]
                ENDIF   ; MAM_SETUP

; Memory Mapping (when Interrupt Vectors are in RAM)
MEMMAP          EQU     0xE01FC040      ; Memory Mapping Control
                IF      :D EF:REMAP
                LDR     R0, =MEMMAP
                IF      :D EF:EXTMEM_MODE
                MOV     R1, #3
                ELIF    :D EF:RAM_MODE
                MOV     R1, #2
                ELSE
                MOV     R1, #1
                ENDIF
                STR     R1, [R0]
                ENDIF

; Setup Stack for each mode

                LDR     R0, =Stack_Top

;  Enter Undefined Instruction Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_UND:OR:I_Bit:OR:F_Bit
                MOV     SP, R0
                SUB     R0, R0, #UND_Stack_Size

;  Enter Abort Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_ABT:OR:I_Bit:OR:F_Bit
                MOV     SP, R0
                SUB     R0, R0, #ABT_Stack_Size

;  Enter FIQ Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_FIQ:OR:I_Bit:OR:F_Bit
                MOV     SP, R0
                SUB     R0, R0, #FIQ_Stack_Size

;  Enter IRQ Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_IRQ:OR:I_Bit:OR:F_Bit
                MOV     SP, R0
                SUB     R0, R0, #IRQ_Stack_Size

;  Enter Supervisor Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_SVC:OR:I_Bit:OR:F_Bit
                MOV     SP, R0
                SUB     R0, R0, #SVC_Stack_Size

;  Enter User Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_USR
                IF      :D EF:__MICROLIB

                EXPORT __initial_sp

                ELSE

                MOV     SP, R0
                SUB     SL, SP, #USR_Stack_Size

                ENDIF

;Start of Deleted Area
;----------------------------------------------------------------------------------------;
				AREA		Lab02, CODE, READONLY
				ENTRY

; Start Book Code
;----------------------------------------------------------------------------------------;
MAIN			LDR			sp, 	=SRAM_BASE
				BL			Initialize					; Initialize UART0

OLOOP			MOV 		R6,	 	#360

				LDR			r2,		=ADGDR
				LDR			GDR,	[r2]
				MOV			r3,		#0x80000000
				AND			r1,		r3,		GDR
				CMP			r1,		r3
				BNE			OLOOP

				LSR			GDR,	GDR,	#6
				LDR			r3,		=0x3FF
				MOV			Level,	#0x1F
				AND			GDR,	r3,		GDR

;CASES
				CMP			GDR,	Level
				LDRLS		r2,		=34
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=33
				BLS			ILOOP

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=32
				BLS			ILOOP

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=31
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=30
				BLS			ILOOP

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=29
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=28
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=27
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=26
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=25
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=24
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=23
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=22
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=21
				BLS			ILOOP

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=20
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=19
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=18
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=17
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=16
				BLS			ILOOP

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=15
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=14
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=13
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=12
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=11
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=10
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=9
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=8
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=7
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=6
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=5
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=4
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=3
				BLS			ILOOP	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r2,		=2
				BLS			ILOOP	

				LDR			r2,		=1

ILOOP			RSB			R1, 	R6, #360
				BL			SIN

				MOV 		R0, 	R0, ASR#16
				MOV			R0, 	R0, LSL#9
				MOV			R0, 	R0, ASR#15
				ADD			R0, 	R0, #512
				MOV 		R0, 	R0, LSL#6
				STRH		R0, 	[R8]
				SUBS		R6, 	R6, R2
				CMP			R6, 	#0x00
				BGT			ILOOP
				B			OLOOP

SIN				STMIA		sp!, 	{R4, R5, R7, lr}
				MOV 		R7, 	R1
				LDR			R5, 	=270

				ADR			R4, 	SIN_DATA
				CMP			R1, 	#90
				BLE			RET_VAL

				CMP			R1, 	#180
				RSBLE		R1, 	R1, #180
				BLE			RET_VAL
				CMP			R1, 	R5
				SUBLE		R1, 	R1, #180
				BLE			RET_VAL
				RSB			R1, 	R1, #360

RET_VAL			LDR			R0, 	[R4, R1, LSL #2]
				CMP			R7, 	#180
				RSBGT		R0, 	R0,#0
				LDMDB		sp!, 	{R4, R5, R7, pc}

Initialize		STMIA		sp!,	{r0, r1, r2, r3, r5, r6, r7, lr}
                LDR     	R0, 	=PINSEL0
                LDR     	R1, 	=PINSEL0_Val
                STR     	R1, 	[R0]

				LDR     	R0, 	=PINSEL1
                LDR     	R1, 	=PINSEL1_Val
                STR     	R1, 	[R0]

				LDR			r8, 	=DACREG

				LDR     	R0, 	=ADCR
                LDR     	R1, 	=ADCR_Val
                STR     	R1, 	[R0]

				LDMDB		sp!,	{r0, r1, r2, r3,r5, r6, r7, pc}	

				ALIGN
SIN_DATA
	DCD		0x00000000, 0x023be164, 0x04779630, 0x06b2f1d8
	DCD		0x08edc7b0, 0x0b27eb50, 0x0d613050, 0x0f996a30
	DCD		0x11d06ca0, 0x14060b80, 0x163a1a80, 0x186c6de0
	DCD		0x1a9cd9c0, 0x1ccb3220, 0x1ef74c00, 0x2120fb80
	DCD		0x234815c0, 0x256c6f80, 0x278dde80, 0x29ac3780
	DCD		0x2bc750c0, 0x2ddf0040, 0x2ff31bc0, 0x32037a40
	DCD		0x340ff240, 0x36185b00, 0x381c8bc0, 0x3a1c5c80
	DCD		0x3c17a500, 0x3e0e3dc0, 0x40000000, 0x41ecc480
	DCD		0x43d46500, 0x45b6bb80, 0x4793a200, 0x496af400
	DCD		0x4b3c8c00, 0x4d084600, 0x4ecdff00, 0x508d9200
	DCD		0x5246dd00, 0x53f9be00, 0x55a61280, 0x574bb900
	DCD		0x58ea9100, 0x5a827980, 0x5c135380, 0x5d9cff80
	DCD		0x5f1f5f00, 0x609a5280, 0x620dbe80, 0x63798500
	DCD		0x64dd8900, 0x6639b080, 0x678dde80, 0x68d9f980
	DCD		0x6a1de700, 0x6b598f00, 0x6c8cd700, 0x6db7a880
	DCD		0x6ed9ec00, 0x6ff38a00, 0x71046d00, 0x720c8080
	DCD		0x730baf00, 0x7401e500, 0x74ef0f00, 0x75d31a80
	DCD		0x76adf600, 0x777f9000, 0x7847d900, 0x7906c080
	DCD		0x79bc3880, 0x7a683200, 0x7b0a9f80, 0x7ba37500
	DCD		0x7c32a680, 0x7cb82880, 0x7d33f100, 0x7da5f580
	DCD		0x7e0e2e00, 0x7e6c9280, 0x7ec11a80, 0x7f0bc080
	DCD		0x7f4c7e80, 0x7f834f00, 0x7fb02e00, 0x7fd31780
	DCD		0x7fec0a00, 0x7ffb0280, 0x7fffffff
;----------------------------------------------------------------------------------------;
; End Book Code

                END	 		;End of Deleted Area

Python CECS 328 Project One

__author__ = 'Robert'

from string import maketrans
from string import whitespace
import operator

def removePunctuation(string):
    marks = '\'",.!?`<>()[]{}\\/_+-=*@#$1234567890%^&|~:;\t\n'
    rep   = '                                            '
    return string.translate(maketrans(marks, rep))

def occurDict(items):
    d = {}
    for i in items:
        if i in d:
            d[i] = d[i]+1
        else:
            d[i] = 1
    return d

file = open('moby.txt')
line, book = '', ''

while 1:
    line = file.readline()
    book = book + line
    if not line:
        break
    pass
print 'The Size of Book with Punctuation:', len(book)

book = removePunctuation(book).upper()
print 'The Size of Book without Punctuation:', len(book)

book = book.split()

book = occurDict(book)
print 'The Number of Words in Book:', len(book)
book = sorted(book.iteritems(), key = operator.itemgetter(1))

book = book[-10:]
for i, j in book:
    print i, ':', j

ARM Lab05: AD0 AD0.3 POT Final

;----------------------------------------------------------------------------------------;
;	Lab06
;	Robert Cardona CSULB CECS 347
;	Spring 2012
;	Lab06.s
;
;	ARM Assembly Language / Fundamentals and Techniques
;		William Hohl, 1st Edition
;----------------------------------------------------------------------------------------;

; RVMDK requires a reset handler - normally found in startup.s code
; name should be "Reset_Handler" (not case sensitive)
GDR				RN		r4
Level			RN		r5
ADCR			EQU		0xE0034000
ADGDR			EQU		0xE0034004
ADCR_Val		EQU		0x00210E08

SRAM_BASE		EQU		0x40000000

Mode_USR        EQU     0x10
Mode_FIQ        EQU     0x11
Mode_IRQ        EQU     0x12
Mode_SVC        EQU     0x13
Mode_ABT        EQU     0x17
Mode_UND        EQU     0x1B
Mode_SYS        EQU     0x1F

I_Bit           EQU     0x80            ; when I bit is set, IRQ is disabled
F_Bit           EQU     0x40            ; when F bit is set, FIQ is disabled

UND_Stack_Size  EQU     0x00000000
SVC_Stack_Size  EQU     0x00000008
ABT_Stack_Size  EQU     0x00000000
FIQ_Stack_Size  EQU     0x00000000
IRQ_Stack_Size  EQU     0x00000080
USR_Stack_Size  EQU     0x00000400

ISR_Stack_Size  EQU     (UND_Stack_Size + SVC_Stack_Size + ABT_Stack_Size + \
                         FIQ_Stack_Size + IRQ_Stack_Size)

                AREA    STACK, NOINIT, READWRITE, ALIGN=3

Stack_Mem       SPACE   USR_Stack_Size
__initial_sp    SPACE   ISR_Stack_Size

Stack_Top

Heap_Size       EQU     0x00000000

                AREA    HEAP, NOINIT, READWRITE, ALIGN=3
__heap_base
Heap_Mem        SPACE   Heap_Size
__heap_limit

; VPBDIV definitions
VPBDIV          EQU     0xE01FC100      ; VPBDIV Address
VPBDIV_SETUP    EQU     1
VPBDIV_Val      EQU     0x01
VPBDIV_Val_0    EQU     0x00			; One-fourth of processor clock
VPBDIV_Val_1    EQU     0x01			; Same as processor clock
VPBDIV_Val_2    EQU     0x02			; One-half of processor clock

; Phase Locked Loop (PLL) definitions
PLL_BASE        EQU     0xE01FC080      ; PLL Base Address
PLLCON_OFS      EQU     0x00            ; PLL Control Offset
PLLCFG_OFS      EQU     0x04            ; PLL Configuration Offset
PLLSTAT_OFS     EQU     0x08            ; PLL Status Offset
PLLFEED_OFS     EQU     0x0C            ; PLL Feed Offset
PLLCON_PLLE     EQU     (1<<0)          ; PLL Enable
PLLCON_PLLC     EQU     (1<<1)          ; PLL Connect
PLLCFG_MSEL     EQU     (0x04<<0)       ; PLL Multiplier  (M=5)
PLLCFG_PSEL     EQU     (0x02<<5)       ; PLL Divider	  (P=2)
PLLSTAT_PLOCK   EQU     (1<<10)         ; PLL Lock Status

PLL_SETUP       EQU     1
PLLCFG_Val      EQU     PLLCFG_MSEL:OR:PLLCFG_PSEL ; 60Mhz

; Memory Accelerator Module (MAM) definitions
MAM_BASE        EQU     0xE01FC000      ; MAM Base Address
MAMCR_OFS       EQU     0x00            ; MAM Control Offset
MAMTIM_OFS      EQU     0x04            ; MAM Timing Offset

MAM_SETUP       EQU     1
MAMCR_Val       EQU     0x00000002
MAMTIM_Val      EQU     0x00000004

; External Memory Controller (EMC) definitions
EMC_BASE        EQU     0xFFE00000      ; EMC Base Address
BCFG0_OFS       EQU     0x00            ; BCFG0 Offset
BCFG1_OFS       EQU     0x04            ; BCFG1 Offset
BCFG2_OFS       EQU     0x08            ; BCFG2 Offset
BCFG3_OFS       EQU     0x0C            ; BCFG3 Offset

;// <e> External Memory Controller (EMC)
EMC_SETUP       EQU     0

BCFG0_SETUP EQU         0
BCFG0_Val   EQU         0x0000FBEF

BCFG1_SETUP EQU         0
BCFG1_Val   EQU         0x0000FBEF

BCFG2_SETUP EQU         0
BCFG2_Val   EQU         0x0000FBEF

BCFG3_SETUP EQU         0
BCFG3_Val   EQU         0x0000FBEF

;// </e> End of EMC

; External Memory Pins Definitions
PINSEL0			EQU		0xE002C000
PINSEL1			EQU		0xE002C004
PINSEL2			EQU		0xE002C014

PINSEL0_Val     EQU     0x00050000
PINSEL1_Val     EQU     0x10000000      ; PINSEL2 Address
PINSEL2_Val     EQU     0x00000000      ; CS0..3, OE, WE, BLS0..3,
                                        ; D0..31, A2..23, JTAG Pins
; UART 0 Registers and Settings
UART0_BASE		EQU		0xE000C000		; UART0 Base Address
U0RBR_0FS		EQU		0x00			; Recieve Buffer Register
U0THR_0FS		EQU		0x00			; Transmit Holding Register
U0DLL_0FS		EQU		0x00			; Divisor Latch LSB (DLA=1)
U0DLM_0FS		EQU		0x04			; Divisor Latch MSB (DLA=1)
U0IER_0FS		EQU		0x04			; Interrupt Enable Register
U0IID_0FS		EQU		0x08			; Interrupt ID Register
U0FCR_0FS	    EQU		0x08			; FIFO Control Register
U0LCR_0FS		EQU		0x0C			; Line Control Register
U0LSR_0FS		EQU		0x14			; Line Status Register
U0SCR_0FS		EQU		0x1C			; Scratch Pad Register (8-bits)
U0ACR_0FS		EQU		0x20			; Auto-Baud Control Register
U0FDR_0FS		EQU		0x28			; Fractional Divider Register
U0TER_0FS		EQU		0x30			; Tx Enable

; UART 1 Registers and Settings
UART1_BASE		EQU		0xE0010000		; UART0 Base Address
U1RBR_0FS		EQU		0x00			; Recieve Buffer Register
U1THR_0FS		EQU		0x00			; Transmit Holding Register
U1DLL_0FS		EQU		0x00			; Divisor Latch LSB (DLA=1)
U1DLM_0FS		EQU		0x04			; Divisor Latch MSB (DLA=1)
U1IER_0FS		EQU		0x04			; Interrupt Enable Register
U1IID_0FS		EQU		0x08			; Interrupt ID Register
U1FCR_0FS	    EQU		0x08			; FIFO Control Register
U1LCR_0FS		EQU		0x0C			; Line Control Register
U1LSR_0FS		EQU		0x14			; Line Status Register
U1SCR_0FS		EQU		0x1C			; Scratch Pad Register (8-bits)
U1ACR_0FS		EQU		0x20			; Auto-Baud Control Register
U1FDR_0FS		EQU		0x28			; Fractional Divider Register
U1TER_0FS		EQU		0x30			; Tx Enable

                PRESERVE8

                AREA    RESET, CODE, READONLY
                ARM

; Exception Vectors
;  Mapped to Address 0.
;  Absolute addressing mode must be used.
;  Dummy Handlers are implemented as infinite loops which can be modified.

Vectors         LDR     PC, Reset_Addr
                LDR     PC, Undef_Addr
                LDR     PC, SWI_Addr
                LDR     PC, PAbt_Addr
                LDR     PC, DAbt_Addr
                NOP                            ; Reserved Vector
;               LDR     PC, IRQ_Addr
                LDR     PC, [PC, #-0x0FF0]     ; Vector from VicVectAddr
                LDR     PC, FIQ_Addr

Reset_Addr      DCD     Reset_Handler
Undef_Addr      DCD     Undef_Handler
SWI_Addr        DCD     SWI_Handler
PAbt_Addr       DCD     PAbt_Handler
DAbt_Addr       DCD     DAbt_Handler
                DCD     0                      ; Reserved Address
IRQ_Addr        DCD     IRQ_Handler
FIQ_Addr        DCD     FIQ_Handler

Undef_Handler   B       Undef_Handler
SWI_Handler     B       SWI_Handler
PAbt_Handler    B       PAbt_Handler
DAbt_Handler    B       DAbt_Handler
IRQ_Handler     B       IRQ_Handler
FIQ_Handler     B       FIQ_Handler

; Reset Handler

                EXPORT  Reset_Handler
Reset_Handler 

; Setup External Memory Pins
                IF      :D EF:EXTERNAL_MODE
                LDR     R0, =PINSEL2
                LDR     R1, =PINSEL2_Val
                STR     R1, [R0]
                ENDIF

; Setup External Memory Pins
                IF      :D EF:EXTERNAL_MODE
                LDR     R0, =PINSEL0
                LDR     R1, =PINSEL0_Val
                STR     R1, [R0]
                ENDIF

; Setup External Memory Controller
                IF      EMC_SETUP <> 0
                LDR     R0, =EMC_BASE

                IF      BCFG0_SETUP <> 0
                LDR     R1, =BCFG0_Val
                STR     R1, [R0, #BCFG0_OFS]
                ENDIF

                IF      BCFG1_SETUP <> 0
                LDR     R1, =BCFG1_Val
                STR     R1, [R0, #BCFG1_OFS]
                ENDIF

                IF      BCFG2_SETUP <> 0
                LDR     R1, =BCFG2_Val
                STR     R1, [R0, #BCFG2_OFS]
                ENDIF

                IF      BCFG3_SETUP <> 0
                LDR     R1, =BCFG3_Val
                STR     R1, [R0, #BCFG3_OFS]
                ENDIF

                ENDIF   ; EMC_SETUP

; Setup VPBDIV
                IF      VPBDIV_SETUP <> 0
                LDR     R0, =VPBDIV
                LDR     R1, =VPBDIV_Val
                STR     R1, [R0]
                ENDIF

; Setup PLL
                IF      PLL_SETUP <> 0
                LDR     R0, =PLL_BASE
                MOV     R1, #0xAA
                MOV     R2, #0x55

;  Configure and Enable PLL
                MOV     R3, #PLLCFG_Val
                STR     R3, [R0, #PLLCFG_OFS]
                MOV     R3, #PLLCON_PLLE
                STR     R3, [R0, #PLLCON_OFS]
                STR     R1, [R0, #PLLFEED_OFS]
                STR     R2, [R0, #PLLFEED_OFS]

;  Wait until PLL Locked
PLL_Loop        LDR     R3, [R0, #PLLSTAT_OFS]
                ANDS    R3, R3, #PLLSTAT_PLOCK
                BEQ     PLL_Loop

;  Switch to PLL Clock
                MOV     R3, #(PLLCON_PLLE:OR:PLLCON_PLLC)
                STR     R3, [R0, #PLLCON_OFS]
                STR     R1, [R0, #PLLFEED_OFS]
                STR     R2, [R0, #PLLFEED_OFS]
                ENDIF   ; PLL_SETUP

; Setup MAM
                IF      MAM_SETUP <> 0
                LDR     R0, =MAM_BASE
                MOV     R1, #MAMTIM_Val
                STR     R1, [R0, #MAMTIM_OFS]
                MOV     R1, #MAMCR_Val
                STR     R1, [R0, #MAMCR_OFS]
                ENDIF   ; MAM_SETUP

; Memory Mapping (when Interrupt Vectors are in RAM)
MEMMAP          EQU     0xE01FC040      ; Memory Mapping Control
                IF      :D EF:REMAP
                LDR     R0, =MEMMAP
                IF      :D EF:EXTMEM_MODE
                MOV     R1, #3
                ELIF    :D EF:RAM_MODE
                MOV     R1, #2
                ELSE
                MOV     R1, #1
                ENDIF
                STR     R1, [R0]
                ENDIF

; Setup Stack for each mode

                LDR     R0, =Stack_Top

;  Enter Undefined Instruction Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_UND:OR:I_Bit:OR:F_Bit
                MOV     SP, R0
                SUB     R0, R0, #UND_Stack_Size

;  Enter Abort Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_ABT:OR:I_Bit:OR:F_Bit
                MOV     SP, R0
                SUB     R0, R0, #ABT_Stack_Size

;  Enter FIQ Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_FIQ:OR:I_Bit:OR:F_Bit
                MOV     SP, R0
                SUB     R0, R0, #FIQ_Stack_Size

;  Enter IRQ Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_IRQ:OR:I_Bit:OR:F_Bit
                MOV     SP, R0
                SUB     R0, R0, #IRQ_Stack_Size

;  Enter Supervisor Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_SVC:OR:I_Bit:OR:F_Bit
                MOV     SP, R0
                SUB     R0, R0, #SVC_Stack_Size

;  Enter User Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_USR
                IF      :D EF:__MICROLIB

                EXPORT __initial_sp

                ELSE

                MOV     SP, R0
                SUB     SL, SP, #USR_Stack_Size

                ENDIF

;Start of Deleted Area
;----------------------------------------------------------------------------------------;
				AREA		Lab02, CODE, READONLY
				ENTRY
				LDR			sp,		=SRAM_BASE	 		; Set up Stack Pointer
				BL			Initialize					; Initialize UART0
START			LDR			r2,		=ADGDR
				LDR			GDR,	[r2]
				MOV			r3,		#0x80000000
				AND			r1,		r3,		GDR
				CMP			r1,		r3
				BNE			START

				LSR			GDR,	GDR,	#6
				LDR			r3,		=0x3FF
				MOV			Level,	#0x00
				AND			GDR,	r3,		GDR

			  	;CASES
				CMP			GDR,	Level
				LDRLS		r1,		=V00
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V01
				BLLS		TransString
				BLS		START

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V02
				BLLS		TransString
				BLS		START

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V03
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V04
				BLLS		TransString
				BLS		START

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V05
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V06
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V07
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V08
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V09
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V10
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V11
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V12
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V13
				BLLS		TransString
				BLS		START

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V14
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V15
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V16
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V17
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V18
				BLLS		TransString
				BLS		START

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V19
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V20
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V21
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V22
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V23
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V24
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V25
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V26
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V27
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V28
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V29
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V30
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V31
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V32
				BLLS		TransString
				BLS		START	

				LDR		r1,		=V33
				BL		TransString
				B 			START

Transmit		STMIA		sp!,	{r5, r6, lr}
				LDR			r5,		=UART1_BASE
wait_t			LDRB		r6,		[r5, #U1LSR_0FS]	; Get status of buffer
				AND			r6,		r6,		#0x20
				CMP			r6,		#0x20				; Buffer empty?
				BNE			wait_t						; spin untill buffer's empty

				STRB		r0,		[r5, #U1THR_0FS]
				LDMDB		sp!,	{r5, r6, pc}

TransString		STMIA		sp!,	{r0, r1, lr}

Loop			LDRB		r0,		[r1],		#1	   	; Load Character, increment ADDR
				CMP			r0,		#0					; Null Terminated?
				BLNE		Transmit					; Send character to UART
				CMP			r0,		#0					; Flags changed in TRANSMIT
				BNE			Loop						; Continue if not a '0'

				LDMDB		sp!,	{r0, r1, pc}

Initialize		STMIA		sp!,	{r0, r1, r2, r3, r5, r6, lr}
                LDR     	R0, 	=PINSEL0
                LDR     	R1, 	=PINSEL0_Val
                STR     	R1, 	[R0]

				LDR     	R0, 	=PINSEL1
                LDR     	R1, 	=PINSEL1_Val
                STR     	R1, 	[R0]

			   	LDR			r5,		=UART1_BASE
				MOV			r6,		#0x83				; set 8 bits no parity one stop
				STRB		r6,		[r5, #U1LCR_0FS] 	; Write control back to LCR

				MOV			r6,		#0xC3				; 19200 Baud @60Mhz VPB clk
				STRB		r6,		[r5]	; Store control byte

				MOV			r6,		#0x03				; set DLAB = 0
				STRB		r6, 	[r5, #U1LCR_0FS]	; Tx and Rx buffers set up

				LDR     R0, =ADCR
                LDR     R1, =ADCR_Val
                STR     R1, [R0]

				LDMDB		sp!,	{r0, r1, r2, r3,r5, r6, pc}	

V00				DCB			0x0D, "0.0 VOLTS", 0
V01				DCB			0x0D, "0.1 VOLTS", 0
V02				DCB			0x0D, "0.2 VOLTS", 0
V03				DCB			0x0D, "0.3 VOLTS", 0
V04				DCB			0x0D, "0.4 VOLTS", 0
V05				DCB			0x0D, "0.5 VOLTS", 0
V06				DCB			0x0D, "0.6 VOLTS", 0
V07				DCB			0x0D, "0.7 VOLTS", 0
V08				DCB			0x0D, "0.8 VOLTS", 0
V09				DCB			0x0D, "0.9 VOLTS", 0
V10				DCB			0x0D, "1.0 VOLTS", 0
V11				DCB			0x0D, "1.1 VOLTS", 0
V12				DCB			0x0D, "1.2 VOLTS", 0
V13				DCB			0x0D, "1.3 VOLTS", 0
V14				DCB			0x0D, "1.4 VOLTS", 0
V15				DCB			0x0D, "1.5 VOLTS", 0
V16				DCB			0x0D, "1.6 VOLTS", 0
V17				DCB			0x0D, "1.7 VOLTS", 0
V18				DCB			0x0D, "1.8 VOLTS", 0
V19				DCB			0x0D, "1.9 VOLTS", 0
V20				DCB			0x0D, "2.0 VOLTS", 0
V21				DCB			0x0D, "2.1 VOLTS", 0
V22				DCB			0x0D, "2.2 VOLTS", 0
V23				DCB			0x0D, "2.3 VOLTS", 0
V24				DCB			0x0D, "2.4 VOLTS", 0
V25				DCB			0x0D, "2.5 VOLTS", 0
V26				DCB			0x0D, "2.6 VOLTS", 0
V27				DCB			0x0D, "2.7 VOLTS", 0
V28				DCB			0x0D, "2.8 VOLTS", 0
V29				DCB			0x0D, "2.9 VOLTS", 0
V30				DCB			0x0D, "3.0 VOLTS", 0
V31				DCB			0x0D, "3.1 VOLTS", 0
V32				DCB			0x0D, "3.2 VOLTS", 0
V33				DCB			0x0D, "3.3 VOLTS", 0

                END	 		;End of Deleted Area

ARM LCD Scroll String

With this code you can scroll an arbitrarily long string using the LCD on the LPC2148.

void printString(char *str) {
	unsigned int i = 0;

	char rowOne[17];
	char rowTwo[17];

	char *orig;
	orig = str;
	while (*str != '\0') {
		for (i = 0; i < 16 && *str != '\0'; i++) {
			rowOne[i] = *str;
			str = str + 1;
		}

		for (i = 0; i < 16 && *str != '\0'; i++) {
			rowTwo[i] = *str;
			str = str + 1;
		}
		orig = orig + 1;
		str = orig;

		lcd_putstring(LINE1, rowOne);
		lcd_putstring(LINE2, rowTwo);
		wait(200000);
	}
}

and and example main would be:

int main (void)
{
	char *str;
	str =  "ETYMOLOGY."
"(Supplied by a Late Consumptive Usher to a Grammar School)"
"The pale Usher--threadbare in coat, heart, body, and brain; I see him"
"now.  He was ever dusting his old lexicons and grammars, with a queer"
"handkerchief, mockingly embellished with all the gay flags of all the"
"known nations of the world.  He loved to dust his old grammars; it"
"somehow mildly reminded him of his mortality."
"\"While you take in hand to school others, and to teach them by what"
"name a whale-fish is to be called in our tongue leaving out, through"
"ignorance, the letter H, which almost alone maketh the signification"
"of the word, you deliver that which is not true.\" --HACKLUYT"
"\"WHALE. ... Sw. and Dan. HVAL.  This animal is named from roundness"
"or rolling; for in Dan. HVALT is arched or vaulted.\" --WEBSTER'S"
"DICTIONARY"
"A.S. WALW-IAN, to roll, to wallow.\" --RICHARDSON'S DICTIONARY"
"KETOS,               GREEK."
"CETUS,               LATIN."
"WHOEL,               ANGLO-SAXON."
"HVALT,               DANISH."
"WAL,                 DUTCH."
"HWAL,                SWEDISH."
"WHALE,               ICELANDIC."
"WHALE,               ENGLISH."
"BALEINE,             FRENCH."
"BALLENA,             SPANISH."
"PEKEE-NUEE-NUEE,     FEGEE."
"PEKEE-NUEE-NUEE,     ERROMANGOAN.";

  init_adc0();						// Initialize ADC
  init_lcd();						// Initialize LCD

  wait(200000);
  lcd_clear();						// clear display
  while(1)
  {
  	//lcd_putstring(LINE2, "Hello World!");
    //process_adc();					// Read ADC value and display it on first line of LCD
    wait(100000);
	printString(str);

  }
}

ARM Lab05: AD0 AD0.3 POT

;----------------------------------------------------------------------------------------;
;	Lab06
;	Robert Cardona CSULB CECS 347
;	Spring 2012
;	Lab06.s
;
;	ARM Assembly Language / Fundamentals and Techniques
;		William Hohl, 1st Edition
;----------------------------------------------------------------------------------------;

; RVMDK requires a reset handler - normally found in startup.s code
; name should be "Reset_Handler" (not case sensitive)
GDR				RN		r4
Level			RN		r5
ADCR			EQU		0xE0034000
ADGDR			EQU		0xE0034004
ADCR_Val		EQU		0x00210E08

SRAM_BASE		EQU		0x40000000

Mode_USR        EQU     0x10
Mode_FIQ        EQU     0x11
Mode_IRQ        EQU     0x12
Mode_SVC        EQU     0x13
Mode_ABT        EQU     0x17
Mode_UND        EQU     0x1B
Mode_SYS        EQU     0x1F

I_Bit           EQU     0x80            ; when I bit is set, IRQ is disabled
F_Bit           EQU     0x40            ; when F bit is set, FIQ is disabled

UND_Stack_Size  EQU     0x00000000
SVC_Stack_Size  EQU     0x00000008
ABT_Stack_Size  EQU     0x00000000
FIQ_Stack_Size  EQU     0x00000000
IRQ_Stack_Size  EQU     0x00000080
USR_Stack_Size  EQU     0x00000400

ISR_Stack_Size  EQU     (UND_Stack_Size + SVC_Stack_Size + ABT_Stack_Size + \
                         FIQ_Stack_Size + IRQ_Stack_Size)

                AREA    STACK, NOINIT, READWRITE, ALIGN=3

Stack_Mem       SPACE   USR_Stack_Size
__initial_sp    SPACE   ISR_Stack_Size

Stack_Top

Heap_Size       EQU     0x00000000

                AREA    HEAP, NOINIT, READWRITE, ALIGN=3
__heap_base
Heap_Mem        SPACE   Heap_Size
__heap_limit

; VPBDIV definitions
VPBDIV          EQU     0xE01FC100      ; VPBDIV Address
VPBDIV_SETUP    EQU     1
VPBDIV_Val      EQU     0x01
VPBDIV_Val_0    EQU     0x00			; One-fourth of processor clock
VPBDIV_Val_1    EQU     0x01			; Same as processor clock
VPBDIV_Val_2    EQU     0x02			; One-half of processor clock

; Phase Locked Loop (PLL) definitions
PLL_BASE        EQU     0xE01FC080      ; PLL Base Address
PLLCON_OFS      EQU     0x00            ; PLL Control Offset
PLLCFG_OFS      EQU     0x04            ; PLL Configuration Offset
PLLSTAT_OFS     EQU     0x08            ; PLL Status Offset
PLLFEED_OFS     EQU     0x0C            ; PLL Feed Offset
PLLCON_PLLE     EQU     (1<<0)          ; PLL Enable
PLLCON_PLLC     EQU     (1<<1)          ; PLL Connect
PLLCFG_MSEL     EQU     (0x04<<0)       ; PLL Multiplier  (M=5)
PLLCFG_PSEL     EQU     (0x02<<5)       ; PLL Divider	  (P=2)
PLLSTAT_PLOCK   EQU     (1<<10)         ; PLL Lock Status

PLL_SETUP       EQU     1
PLLCFG_Val      EQU     PLLCFG_MSEL:OR:PLLCFG_PSEL ; 60Mhz

; Memory Accelerator Module (MAM) definitions
MAM_BASE        EQU     0xE01FC000      ; MAM Base Address
MAMCR_OFS       EQU     0x00            ; MAM Control Offset
MAMTIM_OFS      EQU     0x04            ; MAM Timing Offset

MAM_SETUP       EQU     1
MAMCR_Val       EQU     0x00000002
MAMTIM_Val      EQU     0x00000004

; External Memory Controller (EMC) definitions
EMC_BASE        EQU     0xFFE00000      ; EMC Base Address
BCFG0_OFS       EQU     0x00            ; BCFG0 Offset
BCFG1_OFS       EQU     0x04            ; BCFG1 Offset
BCFG2_OFS       EQU     0x08            ; BCFG2 Offset
BCFG3_OFS       EQU     0x0C            ; BCFG3 Offset

;// <e> External Memory Controller (EMC)
EMC_SETUP       EQU     0

BCFG0_SETUP EQU         0
BCFG0_Val   EQU         0x0000FBEF

BCFG1_SETUP EQU         0
BCFG1_Val   EQU         0x0000FBEF

BCFG2_SETUP EQU         0
BCFG2_Val   EQU         0x0000FBEF

BCFG3_SETUP EQU         0
BCFG3_Val   EQU         0x0000FBEF

;// </e> End of EMC

; External Memory Pins Definitions
PINSEL0			EQU		0xE002C000
PINSEL1			EQU		0xE002C004
PINSEL2			EQU		0xE002C014

PINSEL0_Val     EQU     0x00050000
PINSEL1_Val     EQU     0x10000000      ; PINSEL2 Address
PINSEL2_Val     EQU     0x00000000      ; CS0..3, OE, WE, BLS0..3,
                                        ; D0..31, A2..23, JTAG Pins
; UART 0 Registers and Settings
UART0_BASE		EQU		0xE000C000		; UART0 Base Address
U0RBR_0FS		EQU		0x00			; Recieve Buffer Register
U0THR_0FS		EQU		0x00			; Transmit Holding Register
U0DLL_0FS		EQU		0x00			; Divisor Latch LSB (DLA=1)
U0DLM_0FS		EQU		0x04			; Divisor Latch MSB (DLA=1)
U0IER_0FS		EQU		0x04			; Interrupt Enable Register
U0IID_0FS		EQU		0x08			; Interrupt ID Register
U0FCR_0FS	    EQU		0x08			; FIFO Control Register
U0LCR_0FS		EQU		0x0C			; Line Control Register
U0LSR_0FS		EQU		0x14			; Line Status Register
U0SCR_0FS		EQU		0x1C			; Scratch Pad Register (8-bits)
U0ACR_0FS		EQU		0x20			; Auto-Baud Control Register
U0FDR_0FS		EQU		0x28			; Fractional Divider Register
U0TER_0FS		EQU		0x30			; Tx Enable

; UART 1 Registers and Settings
UART1_BASE		EQU		0xE0010000		; UART0 Base Address
U1RBR_0FS		EQU		0x00			; Recieve Buffer Register
U1THR_0FS		EQU		0x00			; Transmit Holding Register
U1DLL_0FS		EQU		0x00			; Divisor Latch LSB (DLA=1)
U1DLM_0FS		EQU		0x04			; Divisor Latch MSB (DLA=1)
U1IER_0FS		EQU		0x04			; Interrupt Enable Register
U1IID_0FS		EQU		0x08			; Interrupt ID Register
U1FCR_0FS	    EQU		0x08			; FIFO Control Register
U1LCR_0FS		EQU		0x0C			; Line Control Register
U1LSR_0FS		EQU		0x14			; Line Status Register
U1SCR_0FS		EQU		0x1C			; Scratch Pad Register (8-bits)
U1ACR_0FS		EQU		0x20			; Auto-Baud Control Register
U1FDR_0FS		EQU		0x28			; Fractional Divider Register
U1TER_0FS		EQU		0x30			; Tx Enable

                PRESERVE8

                AREA    RESET, CODE, READONLY
                ARM

; Exception Vectors
;  Mapped to Address 0.
;  Absolute addressing mode must be used.
;  Dummy Handlers are implemented as infinite loops which can be modified.

Vectors         LDR     PC, Reset_Addr
                LDR     PC, Undef_Addr
                LDR     PC, SWI_Addr
                LDR     PC, PAbt_Addr
                LDR     PC, DAbt_Addr
                NOP                            ; Reserved Vector
;               LDR     PC, IRQ_Addr
                LDR     PC, [PC, #-0x0FF0]     ; Vector from VicVectAddr
                LDR     PC, FIQ_Addr

Reset_Addr      DCD     Reset_Handler
Undef_Addr      DCD     Undef_Handler
SWI_Addr        DCD     SWI_Handler
PAbt_Addr       DCD     PAbt_Handler
DAbt_Addr       DCD     DAbt_Handler
                DCD     0                      ; Reserved Address
IRQ_Addr        DCD     IRQ_Handler
FIQ_Addr        DCD     FIQ_Handler

Undef_Handler   B       Undef_Handler
SWI_Handler     B       SWI_Handler
PAbt_Handler    B       PAbt_Handler
DAbt_Handler    B       DAbt_Handler
IRQ_Handler     B       IRQ_Handler
FIQ_Handler     B       FIQ_Handler

; Reset Handler

                EXPORT  Reset_Handler
Reset_Handler 

; Setup External Memory Pins
                IF      :D EF:EXTERNAL_MODE
                LDR     R0, =PINSEL2
                LDR     R1, =PINSEL2_Val
                STR     R1, [R0]
                ENDIF

; Setup External Memory Pins
                IF      :D EF:EXTERNAL_MODE
                LDR     R0, =PINSEL0
                LDR     R1, =PINSEL0_Val
                STR     R1, [R0]
                ENDIF

; Setup External Memory Controller
                IF      EMC_SETUP <> 0
                LDR     R0, =EMC_BASE

                IF      BCFG0_SETUP <> 0
                LDR     R1, =BCFG0_Val
                STR     R1, [R0, #BCFG0_OFS]
                ENDIF

                IF      BCFG1_SETUP <> 0
                LDR     R1, =BCFG1_Val
                STR     R1, [R0, #BCFG1_OFS]
                ENDIF

                IF      BCFG2_SETUP <> 0
                LDR     R1, =BCFG2_Val
                STR     R1, [R0, #BCFG2_OFS]
                ENDIF

                IF      BCFG3_SETUP <> 0
                LDR     R1, =BCFG3_Val
                STR     R1, [R0, #BCFG3_OFS]
                ENDIF

                ENDIF   ; EMC_SETUP

; Setup VPBDIV
                IF      VPBDIV_SETUP <> 0
                LDR     R0, =VPBDIV
                LDR     R1, =VPBDIV_Val
                STR     R1, [R0]
                ENDIF

; Setup PLL
                IF      PLL_SETUP <> 0
                LDR     R0, =PLL_BASE
                MOV     R1, #0xAA
                MOV     R2, #0x55

;  Configure and Enable PLL
                MOV     R3, #PLLCFG_Val
                STR     R3, [R0, #PLLCFG_OFS]
                MOV     R3, #PLLCON_PLLE
                STR     R3, [R0, #PLLCON_OFS]
                STR     R1, [R0, #PLLFEED_OFS]
                STR     R2, [R0, #PLLFEED_OFS]

;  Wait until PLL Locked
PLL_Loop        LDR     R3, [R0, #PLLSTAT_OFS]
                ANDS    R3, R3, #PLLSTAT_PLOCK
                BEQ     PLL_Loop

;  Switch to PLL Clock
                MOV     R3, #(PLLCON_PLLE:OR:PLLCON_PLLC)
                STR     R3, [R0, #PLLCON_OFS]
                STR     R1, [R0, #PLLFEED_OFS]
                STR     R2, [R0, #PLLFEED_OFS]
                ENDIF   ; PLL_SETUP

; Setup MAM
                IF      MAM_SETUP <> 0
                LDR     R0, =MAM_BASE
                MOV     R1, #MAMTIM_Val
                STR     R1, [R0, #MAMTIM_OFS]
                MOV     R1, #MAMCR_Val
                STR     R1, [R0, #MAMCR_OFS]
                ENDIF   ; MAM_SETUP

; Memory Mapping (when Interrupt Vectors are in RAM)
MEMMAP          EQU     0xE01FC040      ; Memory Mapping Control
                IF      :D EF:REMAP
                LDR     R0, =MEMMAP
                IF      :D EF:EXTMEM_MODE
                MOV     R1, #3
                ELIF    :D EF:RAM_MODE
                MOV     R1, #2
                ELSE
                MOV     R1, #1
                ENDIF
                STR     R1, [R0]
                ENDIF

; Setup Stack for each mode

                LDR     R0, =Stack_Top

;  Enter Undefined Instruction Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_UND:OR:I_Bit:OR:F_Bit
                MOV     SP, R0
                SUB     R0, R0, #UND_Stack_Size

;  Enter Abort Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_ABT:OR:I_Bit:OR:F_Bit
                MOV     SP, R0
                SUB     R0, R0, #ABT_Stack_Size

;  Enter FIQ Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_FIQ:OR:I_Bit:OR:F_Bit
                MOV     SP, R0
                SUB     R0, R0, #FIQ_Stack_Size

;  Enter IRQ Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_IRQ:OR:I_Bit:OR:F_Bit
                MOV     SP, R0
                SUB     R0, R0, #IRQ_Stack_Size

;  Enter Supervisor Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_SVC:OR:I_Bit:OR:F_Bit
                MOV     SP, R0
                SUB     R0, R0, #SVC_Stack_Size

;  Enter User Mode and set its Stack Pointer
                MSR     CPSR_c, #Mode_USR
                IF      :D EF:__MICROLIB

                EXPORT __initial_sp

                ELSE

                MOV     SP, R0
                SUB     SL, SP, #USR_Stack_Size

                ENDIF

;Start of Deleted Area
;----------------------------------------------------------------------------------------;
				AREA		Lab02, CODE, READONLY
				ENTRY
				LDR			sp,		=SRAM_BASE	 		; Set up Stack Pointer
				BL			Initialize					; Initialize UART0
START			LDR			r2,		=ADGDR
				LDR			GDR,	[r2]
				MOV			r3,		#0x80000000
				AND			r1,		r3,		GDR
				CMP			r1,		r3
				BNE			START

				LSR			GDR,	GDR,	#6
				LDR			r3,		=0x3FF
				MOV			Level,	#0x1F
				AND			GDR,	r3,		GDR

			  	;CASES
				CMP			GDR,	Level
				LDRLS		r1,		=V00
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V01
				BLLS		TransString
				BLS		START

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V02
				BLLS		TransString
				BLS		START

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V03
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V04
				BLLS		TransString
				BLS		START

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V05
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V06
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V07
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V08
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V09
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V10
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V11
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V12
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V13
				BLLS		TransString
				BLS		START

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V14
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V15
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V16
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V17
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V18
				BLLS		TransString
				BLS		START

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V19
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V20
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V21
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V22
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V23
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V24
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V25
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V26
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V27
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V28
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V29
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V30
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V31
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V32
				BLLS		TransString
				BLS		START	

				ADD			Level,	Level,	#0x1F
				CMP			GDR,	Level
				LDRLS		r1,		=V33
				BLLS		TransString
				BLS		START	

				B 			START

Transmit		STMIA		sp!,	{r5, r6, lr}
				LDR			r5,		=UART1_BASE
wait_t			LDRB		r6,		[r5, #U1LSR_0FS]	; Get status of buffer
				AND			r6,		r6,		#0x20
				CMP			r6,		#0x20				; Buffer empty?
				BNE			wait_t						; spin untill buffer's empty

				STRB		r0,		[r5, #U1THR_0FS]
				LDMDB		sp!,	{r5, r6, pc}

TransString		STMIA		sp!,	{r0, r1, lr}

Loop			LDRB		r0,		[r1],		#1	   	; Load Character, increment ADDR
				CMP			r0,		#0					; Null Terminated?
				BLNE		Transmit					; Send character to UART
				CMP			r0,		#0					; Flags changed in TRANSMIT
				BNE			Loop						; Continue if not a '0'

				LDMDB		sp!,	{r0, r1, pc}

Initialize		STMIA		sp!,	{r0, r1, r2, r3, r5, r6, lr}
                LDR     	R0, 	=PINSEL0
                LDR     	R1, 	=PINSEL0_Val
                STR     	R1, 	[R0]

				LDR     	R0, 	=PINSEL1
                LDR     	R1, 	=PINSEL1_Val
                STR     	R1, 	[R0]

			   	LDR			r5,		=UART1_BASE
				MOV			r6,		#0x83				; set 8 bits no parity one stop
				STRB		r6,		[r5, #U1LCR_0FS] 	; Write control back to LCR

				MOV			r6,		#0xC3				; 19200 Baud @60Mhz VPB clk
				STRB		r6,		[r5]	; Store control byte

				MOV			r6,		#0x03				; set DLAB = 0
				STRB		r6, 	[r5, #U1LCR_0FS]	; Tx and Rx buffers set up

				LDR     R0, =ADCR
                LDR     R1, =ADCR_Val
                STR     R1, [R0]

				LDMDB		sp!,	{r0, r1, r2, r3,r5, r6, pc}	

V00				DCB			0x0D, "0.0 VOLTS", 0
V01				DCB			0x0D, "0.1 VOLTS", 0
V02				DCB			0x0D, "0.2 VOLTS", 0
V03				DCB			0x0D, "0.3 VOLTS", 0
V04				DCB			0x0D, "0.4 VOLTS", 0
V05				DCB			0x0D, "0.5 VOLTS", 0
V06				DCB			0x0D, "0.6 VOLTS", 0
V07				DCB			0x0D, "0.7 VOLTS", 0
V08				DCB			0x0D, "0.8 VOLTS", 0
V09				DCB			0x0D, "0.9 VOLTS", 0
V10				DCB			0x0D, "1.0 VOLTS", 0
V11				DCB			0x0D, "1.1 VOLTS", 0
V12				DCB			0x0D, "1.2 VOLTS", 0
V13				DCB			0x0D, "1.3 VOLTS", 0
V14				DCB			0x0D, "1.4 VOLTS", 0
V15				DCB			0x0D, "1.5 VOLTS", 0
V16				DCB			0x0D, "1.6 VOLTS", 0
V17				DCB			0x0D, "1.7 VOLTS", 0
V18				DCB			0x0D, "1.8 VOLTS", 0
V19				DCB			0x0D, "1.9 VOLTS", 0
V20				DCB			0x0D, "2.0 VOLTS", 0
V21				DCB			0x0D, "2.1 VOLTS", 0
V22				DCB			0x0D, "2.2 VOLTS", 0
V23				DCB			0x0D, "2.3 VOLTS", 0
V24				DCB			0x0D, "2.4 VOLTS", 0
V25				DCB			0x0D, "2.5 VOLTS", 0
V26				DCB			0x0D, "2.6 VOLTS", 0
V27				DCB			0x0D, "2.7 VOLTS", 0
V28				DCB			0x0D, "2.8 VOLTS", 0
V29				DCB			0x0D, "2.9 VOLTS", 0
V30				DCB			0x0D, "3.0 VOLTS", 0
V31				DCB			0x0D, "3.1 VOLTS", 0
V32				DCB			0x0D, "3.2 VOLTS", 0
V33				DCB			0x0D, "3.3 VOLTS", 0

                END	 		;End of Deleted Area

8051 Project 03: Serial Port and Memory Access

/*
	Robert Cardona
		007900930
	Spring 2012
		M/W 1-3:15 PM
	Project03.c
	Project 03: Serial Port and Memory Access
*/
//---------------------------------------------------------------------------------------;
#include <reg51.h>
#include <absacc.h>
#include <ctype.h> //Used for toUpper, etc...

//#define DBYTE ((unsigned char volatile data  *) 0)
//#define CBYTE ((unsigned char volatile code  *) 0)
//#define XBYTE ((unsigned char volatile xdata *) 0)

#define CR 0x0D //Carriage Return
#define LF 0x0A	//Line Feed

void initialize(void);

void transmitChar(unsigned char c);
unsigned char receiveChar(void);
void transmitString(unsigned char *c);
void receiveString(unsigned char *c);

unsigned int asciiToHex(unsigned char *ascii);
void hexToAscii(unsigned char hex, unsigned char *ascii);

void initialize(void) {
	SCON = 0x50; // 8-bit, 1 Start, 1 Stop
	PCON = PCON | 0x80; // 19200 BAUD
	TMOD = 0x20; // Timer 1 Mode 2
	TH1 = 0xFD;
	TR1 = 1; //Start Timer 1
}

void main(void) {
	unsigned char menuChoice; //Menu Choice
	unsigned char rwChoice; //Read/Write Choice
	bit errorS, errorE, print;
	unsigned char code readOrWrite[] = {"\nDo you want to read or write? (Enter R for read and W for write): "};
	unsigned char code start[] = {"\nPlease enter starting address: "};
	unsigned char code end[] = {"\nPlease enter the ending address: "};
	unsigned char code menu[] = {"\nThis program allows you access different memory areas in 8051.\nPlease choose a memory area:\n1.\tInternal RAM\n2.\tFlash ROM\n\tChoice: "};
	unsigned char code errorPrompt[] = {"\nInvalid Command. Try Again!"};	

	unsigned char addrS[5]; //Mem Addr: 4 Digits + Null Terminator
	unsigned char addrE[5];
	unsigned int startAddr, endAddr;
	unsigned int i;
	unsigned char memValueString[4]; //2 Hex Digits, 1 Space, 1 Null Terminator
	unsigned char newValue[2];

	initialize();
	while (1) {
	 	transmitString(menu); // Output Menu
		menuChoice = receiveChar(); // Receive User Choice
		rwChoice = 0x00; // Clear
		if (menuChoice == '1') { // Internal RAM
			transmitString(readOrWrite); 

			rwChoice = receiveChar(); // If Read or Write
			if (rwChoice == 'r' || rwChoice == 'R'
					|| rwChoice == 'w' || rwChoice == 'W') {
				do {
					addrS[0] = addrS[1] = addrS[2] = addrS[3] = addrS[4] = 0;
					transmitString(start); // Enter Starting Addr
					receiveString(addrS);
					startAddr = asciiToHex(addrS);

					if (addrS[0] == 0x00 || addrS[2] != 0 || addrS[3] != 0
							|| addrS[4] != 0) {
						transmitString(errorPrompt);
						transmitString(" Invalid Address (Start)(Too Long or Too Short)!");
						errorS = 1;
					} else if (!startAddr && addrS[0] != '0') {
						transmitString(errorPrompt);
						transmitString(" Incorrect Address (Start)");
						errorS = 1;
					} else {
						errorS = 0;
					}
				} while (errorS);

				do {
					addrE[0] = addrE[1] = addrE[2] = addrE[3] = addrE[4] = 0;
					transmitString(end); // Enter Ending Addr
					receiveString(addrE);
					endAddr = asciiToHex(addrE);

					if (addrE[0] == 0x00 || addrE[2] != 0 || addrE[3] != 0
							|| addrE[4] != 0) {
						transmitString(errorPrompt);
						transmitString(" Invalid Address (End)(Too Long or Too Short)!");
						errorE = 1;
					} else if (!endAddr && addrE[0] != '0') {
						transmitString(errorPrompt);
						transmitString(" Incorrect Address (End)");
						errorE = 1;
					} else {
						errorE = 0;
					}
				} while (errorE);

				if (startAddr <= endAddr) {
					if (rwChoice == 'w' || rwChoice == 'W') {
						transmitString("\nPlease enter new data in hex (Ex: 11 22):");
						for (i = startAddr; i <= endAddr; i++) {
							newValue[0] = receiveChar(); newValue[1] = 0x00;
							DBYTE[i] = (unsigned char)asciiToHex(newValue) << 4;

							newValue[0] = receiveChar(); newValue[1] = 0x00;
							DBYTE[i] = DBYTE[i] + (unsigned char)asciiToHex(newValue);

							transmitChar(' ');
						}
					}
					transmitString("\nThe contents in Internal RAM locations ");
					print = 1;
				} else { // Error
					transmitString(errorPrompt);
					transmitString(" Start Adress Greater Than End Adress");
					print = 0;
				}
			} else {
				transmitString(errorPrompt);
				transmitString(" Incorrect R/W Choice");
				print = 0;
			}
		} else if (menuChoice == '2') { // FLASH ROM
			do {
				addrS[0] = addrS[1] = addrS[2] = addrS[3] = addrS[4] = 0;
				transmitString(start); // Enter Starting Addr
				receiveString(addrS);
				startAddr = asciiToHex(addrS);

				if (addrS[0] == 0x00 || (addrS[1] != 0x00 && addrS[2] != 0x00 &&
						addrS[3] != 0x00 && addrS[4] != 0x00)) {
					transmitString(errorPrompt);
					transmitString(" Invalid Address (Start)(Too Long or Too Short)!");
					errorS = 1;
				} else if (!startAddr && addrS[0] != '0') {
					transmitString(errorPrompt);
					transmitString(" Incorrect Address (Start)");
					errorS = 1;
				} else {
					errorS = 0;
				}
			} while (errorS);

			do {
				addrE[0] = addrE[1] = addrE[2] = addrE[3] = addrE[4] = 0;
				transmitString(end); // Enter Ending Addr
				receiveString(addrE);
				endAddr = asciiToHex(addrE);

				if (addrE[0] == 0x00 || (addrE[1] != 0x00 && addrE[2] != 0x00 &&
						addrE[3] != 0x00 && addrE[4] != 0x00)) {
					transmitString(errorPrompt);
					transmitString(" Invalid Address (End)(Too Long or Too Short)!");
					errorE = 1;
				} else if (!endAddr && addrE[0] != '0') {
					transmitString(errorPrompt);
					transmitString(" Incorrect Address (End)");
					errorE = 1;
				} else {
					errorE = 0;
				}
			} while (errorE);

			if (startAddr > endAddr) {
				transmitString(errorPrompt);
				transmitString(" Start Adress Greater Than End Adress");
				print = 0;
			} else {
				transmitString("\nThe contents in Flash ROM locations ");
				print = 1;
			}
		} else { // Error
			transmitString(errorPrompt);
			transmitString(" Incorrect Menu Choice!");
			print = 0;
		}

		if (print) { //Print
			transmitString(addrS);
			transmitString("H - ");
			transmitString(addrE);
			if (rwChoice == 'w' || rwChoice == 'W'){
				transmitString("H are updated with: ");
			} else {
				transmitString("H are: ");
			}

			for (i = startAddr; i <= endAddr; i++){
				switch (menuChoice) {
					case '1': // Internal RAM
						hexToAscii(DBYTE[i], memValueString);
						transmitString(memValueString);
						break;
					case '2': // Flash ROM
						hexToAscii(CBYTE[i], memValueString);
						transmitString(memValueString);
						break;
					default: break;
				}
			}
		}
	}
}

unsigned char receiveChar(void) {
 	unsigned char receivedByte;
	while(RI == 0);
	receivedByte = SBUF;
	RI = 0;
	transmitChar(receivedByte); // Echo
	return receivedByte;
}

void transmitChar(unsigned char c) {
	SBUF = c;
	while (TI == 0);
	TI = 0;
}

void transmitString(unsigned char *c) {
	while (*c != 0x00) {
	 	SBUF = *c;
		while (!TI);
		TI = 0;
		c = c + 1;
	}
}

void receiveString(unsigned char *c) {
	unsigned char receivedByte;

	do {
		while (RI == 0);
		receivedByte = SBUF;
		transmitChar(receivedByte);
		*c = receivedByte;
		c = c + 1;
		RI = 0;
	} while (receivedByte != CR);

	//transmitChar(LF);

	c = c - 1;
	*c = '\0'; //0x00?
}

unsigned int asciiToHex(unsigned char *ascii) {
	unsigned char c;
	unsigned int value = 0;
	unsigned char index = 0;

	while (c = toupper(ascii[index++])) { // Calculate Hex Value
	 	if (c >= '0' && c <= '9'){
			value = (value << 4) + (c - '0');
		} else if ((c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) {
			value = (value << 4) + (c - 'A' + 10);
		} else {
			return 0; // Wrong Hex Number
		}
	}

	return value; // Right Hex Number
}
void hexToAscii(unsigned char hex, unsigned char *ascii) {
	// Calculate/Find ASCII Value MSB
	ascii[0] = (hex & 0xF0) >> 4;
	if (ascii[0] > 9) {
		ascii[0] = ascii[0] - 10 + 'A';
	} else {
		ascii[0] = ascii[0] + '0';
	}

	// Calculate/Find ASCII Value LSB
	ascii[1] = (hex & 0x0F);
	if (ascii[1] > 9) {
		ascii[1] = ascii[1] - 10 + 'A';
	} else {
		ascii[1] = ascii[1] + '0';
	}

	ascii[2] = ' '; // Add Space
	ascii[3] = '\0'; // Add Null Terminator
}

Arduino: Parallax Con. Servo Continued...

int servoPan = 9;
int servoTilt = 10;
int servoPin;
int pulseWidth;
int flag;

void setup() {
  pinMode(servoPan, OUTPUT);
  pinMode(servoTilt, OUTPUT);
  Serial.begin(9600);
  flag = LOW;
}

//STOP 1530
void servoPulse(int servoPin, int pulseWidth) {
  digitalWrite(servoPin, HIGH);
  delayMicroseconds(pulseWidth);
  digitalWrite(servoPin, LOW);
}

void panRight() {
  servoPulse(servoPan, 1400);
  delay(20);
}

void panLeft() {
  servoPulse(servoPan, 1600);
  delay(20);
}

void tiltUp() {
  servoPulse(servoTilt, 1400);
  delay(20);
}

void tiltDown() {
  servoPulse(servoTilt, 1600);
  delay(20);
}

void loop() {
  if (Serial.available()){
    switch( Serial.read() ) {
      case 'u': tiltUp(); break;
      case 'd': tiltDown(); break;
      case 'r': panRight(); break;
      case 'l': panLeft(); break;
      default: break;
    }

  }
}

Twitter

  • Finished watching Star Trek TNG S07
  • Finished The Walking Dead S02
  • Finished watching Star Trek TNG S06
  • Finished watching Star Trek TNG S05
  • Pumped! Its raining! :D
  • Finished watching Star Trek TNG S04
  • Downloading BitNami WAMPStack Stack. Check it out!
    http://t.co/mvXnqxW2
  • Finished watching Star Trek TNG S03.
  • Finished The Walking Dead S01.
  • 9 Common Interview Questions That Are Actually Illegal | Secrets to Your Success - Yahoo! Finance via @YahooFinance
    http://t.co/7nICz3Wd
  • Pumped. Finished Star Trek TNG S02 :)
  • Pumped. Finished Star Trek TNG S01 :)
  • Pumped ;)
  • I liked a @YouTube video Windows 8 Consumer Preview Official Demo
    http://t.co/HyuDWmnO
  • CHUCK Movie!!!