lab 3

Dependencies:   mbed mbed-rtos tsi_sensor SLCD

Committer:
wstapleton
Date:
Wed Feb 19 01:26:00 2020 +0000
Revision:
1:03ee5f19d879
Parent:
0:78b84e1ce9df
Child:
2:b94b72891752
minor fixes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wstapleton 0:78b84e1ce9df 1 #include "mbed.h"
wstapleton 0:78b84e1ce9df 2 #include "rtos.h"
wstapleton 0:78b84e1ce9df 3
wstapleton 0:78b84e1ce9df 4 #include "stdio.h"
wstapleton 0:78b84e1ce9df 5 #include "stdint.h"
wstapleton 0:78b84e1ce9df 6 #include "ctype.h"
wstapleton 0:78b84e1ce9df 7 #include "math.h"
wstapleton 0:78b84e1ce9df 8 #include "errno.h"
wstapleton 0:78b84e1ce9df 9
wstapleton 0:78b84e1ce9df 10 /* These are header files created for EE 3420 that are generally useful but add a minimum of dependencies */
wstapleton 0:78b84e1ce9df 11 #include "Freedom_headers.h"
wstapleton 0:78b84e1ce9df 12 //The following are names for pins by position on the headers
wstapleton 0:78b84e1ce9df 13 /*
wstapleton 0:78b84e1ce9df 14 The USB connections are on this end of the board
wstapleton 0:78b84e1ce9df 15
wstapleton 0:78b84e1ce9df 16 J2_19 OO J2_20
wstapleton 0:78b84e1ce9df 17 J2_17 OO J2_18
wstapleton 0:78b84e1ce9df 18 J2_15 OO J2_16
wstapleton 0:78b84e1ce9df 19 J2_13 OO J2_14
wstapleton 0:78b84e1ce9df 20 J3_2 OO J3_1 J2_11 OO J2_12
wstapleton 0:78b84e1ce9df 21 J3_4 OO J3_3 J2_9 OO J2_10
wstapleton 0:78b84e1ce9df 22 J3_6 OO J3_5 J2_7 OO J2_8
wstapleton 0:78b84e1ce9df 23 J3_8 OO J3_7 J2_5 OO J2_6
wstapleton 0:78b84e1ce9df 24 J3_10 OO J3_9 J2_3 OO J2_4
wstapleton 0:78b84e1ce9df 25 J3_12 OO J3_11 J2_1 OO J2_2
wstapleton 0:78b84e1ce9df 26 J3_14 OO J3_13
wstapleton 0:78b84e1ce9df 27 J3_16 OO J3_15 J1_15 OO J1_16
wstapleton 0:78b84e1ce9df 28 J1_13 OO J1_14
wstapleton 0:78b84e1ce9df 29 J4_2 OO J4_1 J1_11 OO J1_12
wstapleton 0:78b84e1ce9df 30 J4_4 OO J4_3 J1_9 OO J1_10
wstapleton 0:78b84e1ce9df 31 J4_6 OO J4_5 J1_7 OO J1_8
wstapleton 0:78b84e1ce9df 32 J4_8 OO J4_7 J1_5 OO J1_6
wstapleton 0:78b84e1ce9df 33 J4_10 OO J4_9 J1_3 OO J1_4
wstapleton 0:78b84e1ce9df 34 J4_12 OO J4_11 J1_1 OO J1_2
wstapleton 0:78b84e1ce9df 35
wstapleton 0:78b84e1ce9df 36 */
wstapleton 0:78b84e1ce9df 37
wstapleton 0:78b84e1ce9df 38 #include "ASCII.h"
wstapleton 0:78b84e1ce9df 39
wstapleton 0:78b84e1ce9df 40 #ifndef TRUE
wstapleton 0:78b84e1ce9df 41 #define TRUE (1==1)
wstapleton 0:78b84e1ce9df 42 #endif
wstapleton 0:78b84e1ce9df 43
wstapleton 0:78b84e1ce9df 44 #ifndef FALSE
wstapleton 0:78b84e1ce9df 45 #define FALSE (1==0)
wstapleton 0:78b84e1ce9df 46 #endif
wstapleton 0:78b84e1ce9df 47
wstapleton 0:78b84e1ce9df 48 #ifndef BIT
wstapleton 0:78b84e1ce9df 49 #define BIT(N) (1 << (N))
wstapleton 0:78b84e1ce9df 50 #endif
wstapleton 0:78b84e1ce9df 51
wstapleton 0:78b84e1ce9df 52 //Pushbuttons (active-low), digital input or interrupt input
wstapleton 0:78b84e1ce9df 53 //pins need pull-up resistors
wstapleton 1:03ee5f19d879 54 //DigitalIn left_button(PTC12, PullUp);
wstapleton 0:78b84e1ce9df 55 //InterruptIn left_button(PTC12, PullUp);
wstapleton 1:03ee5f19d879 56 //DigitalIn right_button(PTC3, PullUp);
wstapleton 0:78b84e1ce9df 57 //InterruptIn right_button(PTC3, PullUp);
wstapleton 0:78b84e1ce9df 58
wstapleton 0:78b84e1ce9df 59 //User LEDs
wstapleton 1:03ee5f19d879 60 //DigitalOut red_led(PTE29);
wstapleton 1:03ee5f19d879 61 //DigitalOut green_led(PTD5);
wstapleton 0:78b84e1ce9df 62
wstapleton 0:78b84e1ce9df 63 //Light sensor (phototransistor)
wstapleton 1:03ee5f19d879 64 //AnalogIn light_sensor(PTE22);
wstapleton 0:78b84e1ce9df 65
wstapleton 0:78b84e1ce9df 66 //Temperature sensor(LM34)
wstapleton 1:03ee5f19d879 67 //AnalogIn LM34(PTC2);
wstapleton 0:78b84e1ce9df 68
wstapleton 0:78b84e1ce9df 69 //Speakers
wstapleton 1:03ee5f19d879 70 //PwmOut speaker1(PTE21);
wstapleton 1:03ee5f19d879 71 //PwmOut Speaker2(PTE23);
wstapleton 0:78b84e1ce9df 72
wstapleton 0:78b84e1ce9df 73 // All of the follwoing define "pc" identically
wstapleton 0:78b84e1ce9df 74 Serial pc(USBTX, USBRX); // MBED standard pin names
wstapleton 0:78b84e1ce9df 75 //Serial pc(PTA2, PTA1); // KL46Z pin names
wstapleton 0:78b84e1ce9df 76 //Serial pc(J1_4, J1_2); // Jumper position pin names
wstapleton 0:78b84e1ce9df 77 //Serial pc(D1,D0); // Arduino-equivalent pin names
wstapleton 0:78b84e1ce9df 78 /* USBTX=PTA2, USBRX=PTA1 */
wstapleton 0:78b84e1ce9df 79 /* UART0_TX=PTA2, UART0_RX=PTA1, so actually UART0 */
wstapleton 0:78b84e1ce9df 80 /* UART0 on KL46Z routed through OpenSDA chip to USB */
wstapleton 0:78b84e1ce9df 81 /* now "pc" refers to this virtualized com port on host computer*/
wstapleton 0:78b84e1ce9df 82
wstapleton 0:78b84e1ce9df 83 /* available functions */
wstapleton 0:78b84e1ce9df 84 /*
wstapleton 0:78b84e1ce9df 85 pc.baud()
wstapleton 0:78b84e1ce9df 86 pc.format()
wstapleton 0:78b84e1ce9df 87 pc.readable()
wstapleton 0:78b84e1ce9df 88 pc.writeable()
wstapleton 0:78b84e1ce9df 89 pc.read()
wstapleton 0:78b84e1ce9df 90 pc.write()
wstapleton 0:78b84e1ce9df 91 pc.getc()
wstapleton 0:78b84e1ce9df 92 pc.putc()
wstapleton 0:78b84e1ce9df 93 pc.gets()
wstapleton 0:78b84e1ce9df 94 pc.puts()
wstapleton 0:78b84e1ce9df 95 pc.printf()
wstapleton 0:78b84e1ce9df 96 pc.scanf()
wstapleton 0:78b84e1ce9df 97 */
wstapleton 0:78b84e1ce9df 98
wstapleton 0:78b84e1ce9df 99 #include "VT100.h"
wstapleton 0:78b84e1ce9df 100 //The VT100 functions send terminal control commands to the "pc" serial port
wstapleton 0:78b84e1ce9df 101 /*
wstapleton 0:78b84e1ce9df 102 void vt100_home()
wstapleton 0:78b84e1ce9df 103 void vt100_move_cursor(int row, int column)
wstapleton 0:78b84e1ce9df 104 void vt100_save_cursor()
wstapleton 0:78b84e1ce9df 105 void vt100_unsave_cursor()
wstapleton 0:78b84e1ce9df 106 void vt100_up()
wstapleton 0:78b84e1ce9df 107 void vt100_up_by(int count)
wstapleton 0:78b84e1ce9df 108 void vt100_down()
wstapleton 0:78b84e1ce9df 109 void vt100_down_by(int count)
wstapleton 0:78b84e1ce9df 110 void vt100_right()
wstapleton 0:78b84e1ce9df 111 void vt100_right_by(int count)
wstapleton 0:78b84e1ce9df 112 void vt100_left()
wstapleton 0:78b84e1ce9df 113 void vt100_left_by(int count)
wstapleton 0:78b84e1ce9df 114 void vt100_erase_right()
wstapleton 0:78b84e1ce9df 115 void vt100_erase_left()
wstapleton 0:78b84e1ce9df 116 void vt100_erase_line()
wstapleton 0:78b84e1ce9df 117 void vt100_erase_above()
wstapleton 0:78b84e1ce9df 118 void vt100_erase_below()
wstapleton 0:78b84e1ce9df 119 void vt100_erase_screen()
wstapleton 0:78b84e1ce9df 120 void vt100_set_attribute(int attrib)
wstapleton 0:78b84e1ce9df 121 void vt100_text_default()
wstapleton 0:78b84e1ce9df 122 void vt100_text_bright()
wstapleton 0:78b84e1ce9df 123 void vt100_text_dim()
wstapleton 0:78b84e1ce9df 124 void vt100_text_underscore()
wstapleton 0:78b84e1ce9df 125 void vt100_text_blink()
wstapleton 0:78b84e1ce9df 126 void vt100_text_reverse()
wstapleton 0:78b84e1ce9df 127 void vt100_text_hidden()
wstapleton 0:78b84e1ce9df 128 void vt100_set_foreground(vt100_colors color)
wstapleton 0:78b84e1ce9df 129 void vt100_set_background(vt100_colors color)
wstapleton 0:78b84e1ce9df 130 */
wstapleton 0:78b84e1ce9df 131
wstapleton 1:03ee5f19d879 132 //#include "SLCD.h"
wstapleton 0:78b84e1ce9df 133 //slcd is the 4-digit, 7-segment LCD on the KL46Z
wstapleton 1:03ee5f19d879 134 //SLCD slcd;
wstapleton 0:78b84e1ce9df 135 /* available functions */
wstapleton 0:78b84e1ce9df 136 /*
wstapleton 0:78b84e1ce9df 137 slcd.printf("1234"); // standard printf function, only charaters in ASCII_TO_WF_CODIFICATION_TABLE will display
wstapleton 0:78b84e1ce9df 138 slcd.putc('A'); // prints a single character
wstapleton 0:78b84e1ce9df 139 slcd.Write_Char('A'); // prints a single character
wstapleton 0:78b84e1ce9df 140 slcd.All_Segments(y); // y=1 for ALL segments on, 0 for ALL segments off
wstapleton 0:78b84e1ce9df 141 slcd.DPx(y); // x=DP1 to DP3, y=1 for on 0 for off
wstapleton 0:78b84e1ce9df 142 slcd.Colon(y); // y=1 for on, 0 for off
wstapleton 0:78b84e1ce9df 143 slcd.CharPosition=x; // x=0 to 3, 0 is start position
wstapleton 0:78b84e1ce9df 144 slcd.Home(); // sets next charater to posistion 0 (start)
wstapleton 0:78b84e1ce9df 145 slcd.Contrast (x); // set contrast x=0 - 15, 0 lightest, 15 darkest
wstapleton 0:78b84e1ce9df 146 */
wstapleton 0:78b84e1ce9df 147
wstapleton 1:03ee5f19d879 148 //#include "tsi_sensor.h"
wstapleton 0:78b84e1ce9df 149 //Touch Sensitive Interface
wstapleton 0:78b84e1ce9df 150 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
wstapleton 0:78b84e1ce9df 151 #define ELEC0 9
wstapleton 0:78b84e1ce9df 152 #define ELEC1 10
wstapleton 0:78b84e1ce9df 153 #elif defined (TARGET_KL05Z)
wstapleton 0:78b84e1ce9df 154 #define ELEC0 9
wstapleton 0:78b84e1ce9df 155 #define ELEC1 8
wstapleton 0:78b84e1ce9df 156 #else
wstapleton 0:78b84e1ce9df 157 #error TARGET NOT DEFINED
wstapleton 0:78b84e1ce9df 158 #endif
wstapleton 1:03ee5f19d879 159 //TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
wstapleton 0:78b84e1ce9df 160
wstapleton 0:78b84e1ce9df 161 /* available functions */
wstapleton 0:78b84e1ce9df 162 /* float TSIAnalogSlider.readPercentage() */
wstapleton 0:78b84e1ce9df 163
wstapleton 1:03ee5f19d879 164 //#include "keypad4x4.h"
wstapleton 0:78b84e1ce9df 165 //BusOut keypad4x4_columns(PTE19, PTE18, PTE17, PTE16);
wstapleton 0:78b84e1ce9df 166 //BusIn keypad4x4_rows(PTA14, PTA15, PTA16, PTA17);
wstapleton 0:78b84e1ce9df 167 //InterruptIn keypad4x4_interrupt(PTC16);
wstapleton 0:78b84e1ce9df 168 //available functions:
wstapleton 0:78b84e1ce9df 169 /*
wstapleton 0:78b84e1ce9df 170 int keypad4x4_set_type(int type)
wstapleton 0:78b84e1ce9df 171 int keypad4x4_query(char *key_value, int * key_row, int *key_column)
wstapleton 0:78b84e1ce9df 172 char keypad4x4_getc(void)
wstapleton 0:78b84e1ce9df 173 int keypad4x4_input(char *key_value, int * key_row, int *key_column)
wstapleton 0:78b84e1ce9df 174
wstapleton 0:78b84e1ce9df 175 */
wstapleton 0:78b84e1ce9df 176
wstapleton 0:78b84e1ce9df 177 #include "NKC_LCD.h"
wstapleton 1:03ee5f19d879 178 //Serial lcd(PTD3,NC);
wstapleton 0:78b84e1ce9df 179 //Serial lcd(J1_6,NC);
wstapleton 0:78b84e1ce9df 180 //available functions:
wstapleton 0:78b84e1ce9df 181 /*
wstapleton 0:78b84e1ce9df 182 int lcd_baud_decode(int baud)
wstapleton 0:78b84e1ce9df 183 int lcd_baud(int baud);
wstapleton 0:78b84e1ce9df 184 int lcd_default_baud(int baud);
wstapleton 0:78b84e1ce9df 185 void lcd_on()
wstapleton 0:78b84e1ce9df 186 void lcd_off()
wstapleton 0:78b84e1ce9df 187 void lcd_ddram_address(int address)
wstapleton 0:78b84e1ce9df 188 int lcd_cursor(int row, int column)
wstapleton 0:78b84e1ce9df 189 void lcd_home()
wstapleton 0:78b84e1ce9df 190 void lcd_underline_on()
wstapleton 0:78b84e1ce9df 191 void lcd_underline_off()
wstapleton 0:78b84e1ce9df 192 int lcd_underline(int select)
wstapleton 0:78b84e1ce9df 193 void lcd_backspace()
wstapleton 0:78b84e1ce9df 194 void lcd_clear()
wstapleton 0:78b84e1ce9df 195 void lcd_blink_on()
wstapleton 0:78b84e1ce9df 196 void lcd_blink_off()
wstapleton 0:78b84e1ce9df 197 int lcd_blink(int select)
wstapleton 0:78b84e1ce9df 198 void lcd_move_left()
wstapleton 0:78b84e1ce9df 199 void lcd_move_right()
wstapleton 0:78b84e1ce9df 200 void lcd_shift_left()
wstapleton 0:78b84e1ce9df 201 void lcd_shift_right()
wstapleton 0:78b84e1ce9df 202 */
wstapleton 0:78b84e1ce9df 203
wstapleton 0:78b84e1ce9df 204 //Stepper motor driver (ULN2803A)
wstapleton 1:03ee5f19d879 205 //BusOut stepper(PTB0, PTB1, PTB2, PTB3);
wstapleton 0:78b84e1ce9df 206 //BusOut stepper(J4_2, J4_4, J4_6, J4_8);
wstapleton 0:78b84e1ce9df 207
wstapleton 0:78b84e1ce9df 208 //DC Quad Half-Hmotor driver (SN754410)
wstapleton 1:03ee5f19d879 209 //DigitalOut motor_1(PTB20);
wstapleton 1:03ee5f19d879 210 //DigitalOut motor_2(PTE30);
wstapleton 1:03ee5f19d879 211 //DigitalOut motor_3(PTB18);
wstapleton 1:03ee5f19d879 212 //DigitalOut motor_4(PTB19);
wstapleton 1:03ee5f19d879 213 //PwmOut motor_enable_12(PTA6);
wstapleton 1:03ee5f19d879 214 //PwmOut motor_enable_34(PTA7);
wstapleton 0:78b84e1ce9df 215 //DigitalOut motor_enable_12(PTA6);
wstapleton 0:78b84e1ce9df 216 //DigitalOut motor_enable_34(PTA7);
wstapleton 0:78b84e1ce9df 217
wstapleton 0:78b84e1ce9df 218 //Servo motors
wstapleton 1:03ee5f19d879 219 //PwmOut servo1(PTA4);
wstapleton 1:03ee5f19d879 220 //PwmOut servo2(PTA5);
wstapleton 0:78b84e1ce9df 221
wstapleton 0:78b84e1ce9df 222 //Inter-integrated circuit bus
wstapleton 0:78b84e1ce9df 223 //I2C i2c0(PTE25, PTE24); //internal I2C bus for accelerometer and magnetometer
wstapleton 0:78b84e1ce9df 224 //I2C i2c1(PTE0, PTE1); //external I2C bus (commonly real-time clock);
wstapleton 0:78b84e1ce9df 225 //I2C i2c1(D14, D15); //external I2C bus (commonly real-time clock);
wstapleton 0:78b84e1ce9df 226
wstapleton 0:78b84e1ce9df 227 //alternative serial port
wstapleton 0:78b84e1ce9df 228 Serial uart1(PTE0,PTE1);
wstapleton 0:78b84e1ce9df 229
wstapleton 0:78b84e1ce9df 230 //IMPORTANT NOTE: i2c1 and uart1 both use PTE0/PTE1 so cannot be used simultaneously.
wstapleton 0:78b84e1ce9df 231
wstapleton 0:78b84e1ce9df 232 //common SPI (MSI, MISO, SCK, SS, use_gpio_ssel_t)
wstapleton 0:78b84e1ce9df 233 //SPI spi1(PTD6, PTD7, PTD5);
wstapleton 0:78b84e1ce9df 234 //SPI spi1(PTD6, PTD7, PTD5, PTD4, use_gpio_ssel_t);
wstapleton 0:78b84e1ce9df 235 //SPI spi1(D11, D12, D13);
wstapleton 0:78b84e1ce9df 236 //SPI spi1(D11, D12, D13, D10, use_gpio_ssel_t);
wstapleton 0:78b84e1ce9df 237
wstapleton 0:78b84e1ce9df 238 char pc_string[50];
wstapleton 0:78b84e1ce9df 239 char uart1_string[50];
wstapleton 0:78b84e1ce9df 240 int pc_count=0;
wstapleton 0:78b84e1ce9df 241 int uart1_count=0;
wstapleton 0:78b84e1ce9df 242 int pc_ready=0;
wstapleton 0:78b84e1ce9df 243 int uart1_ready=0;
wstapleton 0:78b84e1ce9df 244
wstapleton 0:78b84e1ce9df 245 char tempc;
wstapleton 0:78b84e1ce9df 246
wstapleton 0:78b84e1ce9df 247 int main()
wstapleton 0:78b84e1ce9df 248 {
wstapleton 0:78b84e1ce9df 249 pc.baud(115200);
wstapleton 1:03ee5f19d879 250 pc.printf("\nStarting\n");
wstapleton 1:03ee5f19d879 251 pc.printf("lcd baud decode %i\n",lcd_baud_decode(115200));
wstapleton 0:78b84e1ce9df 252 lcd_default_baud(115200);
wstapleton 1:03ee5f19d879 253 pc.printf("lcd baud %i\n",lcd_baud(115200));
wstapleton 0:78b84e1ce9df 254 uart1.baud(115200);
wstapleton 1:03ee5f19d879 255 // slcd.All_Segments(0);
wstapleton 0:78b84e1ce9df 256
wstapleton 0:78b84e1ce9df 257
wstapleton 0:78b84e1ce9df 258
wstapleton 0:78b84e1ce9df 259 while(true)
wstapleton 0:78b84e1ce9df 260 {
wstapleton 0:78b84e1ce9df 261 if(pc.readable())
wstapleton 0:78b84e1ce9df 262 {
wstapleton 0:78b84e1ce9df 263 tempc=pc.getc();
wstapleton 0:78b84e1ce9df 264 pc_string[pc_count++]=tempc;
wstapleton 0:78b84e1ce9df 265 if(pc_count>=40 || tempc==CR || tempc==LF)
wstapleton 0:78b84e1ce9df 266 {
wstapleton 0:78b84e1ce9df 267 pc_string[pc_count++]='\n';
wstapleton 0:78b84e1ce9df 268 pc_string[pc_count++]=NULL;
wstapleton 0:78b84e1ce9df 269 pc_ready=1;
wstapleton 0:78b84e1ce9df 270 }
wstapleton 0:78b84e1ce9df 271 }
wstapleton 0:78b84e1ce9df 272 if(uart1.readable())
wstapleton 0:78b84e1ce9df 273 {
wstapleton 0:78b84e1ce9df 274
wstapleton 0:78b84e1ce9df 275
wstapleton 0:78b84e1ce9df 276 }
wstapleton 0:78b84e1ce9df 277 if(pc_ready == 1)
wstapleton 0:78b84e1ce9df 278 {
wstapleton 0:78b84e1ce9df 279 pc.putc('\n');
wstapleton 0:78b84e1ce9df 280 vt100_set_foreground(green);
wstapleton 0:78b84e1ce9df 281 //pc.puts(pc_string);
wstapleton 0:78b84e1ce9df 282 pc.printf("%s",pc_string);
wstapleton 0:78b84e1ce9df 283 uart1.printf("%s",pc_string);
wstapleton 0:78b84e1ce9df 284 lcd_cursor(0, 0);
wstapleton 0:78b84e1ce9df 285 lcd.printf("%s",pc_string);
wstapleton 0:78b84e1ce9df 286 pc_ready=0;
wstapleton 0:78b84e1ce9df 287 pc_count=0;
wstapleton 0:78b84e1ce9df 288 vt100_text_default();
wstapleton 0:78b84e1ce9df 289 }
wstapleton 0:78b84e1ce9df 290 if(uart1_ready==1)
wstapleton 0:78b84e1ce9df 291 {
wstapleton 0:78b84e1ce9df 292
wstapleton 0:78b84e1ce9df 293
wstapleton 0:78b84e1ce9df 294
wstapleton 0:78b84e1ce9df 295 }
wstapleton 0:78b84e1ce9df 296
wstapleton 0:78b84e1ce9df 297
wstapleton 0:78b84e1ce9df 298 }
wstapleton 0:78b84e1ce9df 299 /* YOUR CODE GOES HERE */
wstapleton 0:78b84e1ce9df 300
wstapleton 0:78b84e1ce9df 301 }