void send_at_command_sci1(char* str){ long i; status = 'c'; for (i = 0; str[i] != '\0'; i++) outchar1(str[i]); outchar1('\n'); at_timeout = 10 * 100; // set at_timeout to ~10 sec while(at_timeout){ while(qempty() && at_timeout); c = getq(); if (c == 'O'){ while(qempty() && at_timeout); c = getq(); if (c == 'K') break; else continue; } } // XXX: maybe clearing out nXXXXXXXXXX? //empty_queue(); // clear the queue of any part of the message we may have ignored. if (at_timeout) status = 'i'; else{ status = 'e'; set_lcd_addr(0); type_lcd("writing timed"); set_lcd_addr(0x40); type_lcd("out."); } }
void display(char *out, int idx, char curs){ out[idx] = '\0'; set_lcd_addr(curs); //set cursor to location curs type_lcd(out); //display ASCIIZ string on LCD set_lcd_addr(0x40); }
void subString() { strncpy(sub, oper+substart, 16); clrLine(2); set_lcd_addr(0x40); type_lcd(sub); set_lcd_addr(0x40); }
void main(void) { PLL_init(); // set system clock frequency to 24 MHz ad0_enable(); // enable a/d converter 0 ad1_enable(); // enable a/d converter 1 led_enable(); seg7_disable(); servo54_init(); lcd_init(); // enable lcd motor1_init(); // enable 8-bit pwm1 for computer fan while(1) { for (width = 3000; width < 14500; width = width + 150) { flame = ad0conv(3); // read flame sensor output on channel 3 adc0 range = ad0conv(5); // read range sensor output on channel 7 adc1 set_lcd_addr(0x40); if (range > 550) { type_lcd("Flame is near! "); } else { type_lcd("Out of range "); } set_servo54(width); ms_delay(10); if (flame > 1000) { set_lcd_addr(0x00); ms_delay(10); type_lcd("Flame Detected! "); if (range > 550) { ms_delay(10); //Turn the motor on for 8 seconds set_lcd_addr(0x40); ms_delay(10); type_lcd("Flame is near! "); ms_delay(10); leds_on(0b11111000); //Motor is on ms_delay(8000); motor1(speed); ms_delay(10); } } else { leds_on(0b11111100); //Motor is off set_lcd_addr(0x00); type_lcd("No Flame "); } ms_delay(10); } } }
/*** get keypad input ****/ void get_keypad_input() { char keypad_key; keypad_enable(); while(training) { keypad_key=getkey(); // get value from keypad wait_keyup(); lcd_init(); set_lcd_addr(0x00); if(keypad_key==1) { // 1 = forward move_forward(); }else if(keypad_key==4) { // 4 = left move_left(); }else if(keypad_key==7) { // 7 = right move_right(); }else if(keypad_key==0) { // 0 = reverse move_reverse(); }else if(keypad_key==6) { // 7 = pause move_pause(); }else if(keypad_key==9) { // break out of training mode training=0; move_stop(); display_movement(&stop_training); break; }else{ display_movement(&error); } ms_delay(50); } }
void main(void) { long op1, op2; //32-bit operands char* ptr; //pointer to keypad buffer char* blanks; char kbuf[12]; //keypad buffer char c, a; int i; ptr = kbuf; blanks = " "; PLL_init(); //set system clock freq to 24MHz lcd_init(); //enable lcd keypad_enable(); //enable keypad set_lcd_addr(0x00); //display on 1st line i = 0; while(1) { c = getkey(); //read keypad a = hex2asc(c); //convert to ascii kbuf[i] = a; // and store in kbuf if(c<10) { //if 0 - 9 data8(a); // display on lcd wait_keyup(); // wait to release key i++; //increment index } else { switch(c) { case 0xE: //if enter (*) key op1 = number(ptr); //convert to binary set_lcd_addr(0x40); //display on 2nd line write_long_lcd(op1); set_lcd_addr(0x00); //clear 1st line type_lcd(blanks); wait_keyup(); //wait to release key i = 0; //reset kbuf index set_lcd_addr(0x00); //display on 2nd line break; case 0xA: //if key A op2 = number(ptr); //convert to binary op1 = op1 + op2; //compete sum set_lcd_addr(0x40); //display on 2nd line write_long_lcd(op1); set_lcd_addr(0x00); //clear 1st line type_lcd(blanks); wait_keyup(); //wait to release key i = 0; //reset kbuf index set_lcd_addr(0x00); //display on 1st line break; case 0xF: //if clear (#) key clear_lcd(); //clear lcd display wait_keyup(); //wait to release key i = 0; //reset kbuf index break; default: break; } } } }
void main(void) { PLL_init(); lcd_init(); SCI0_init(9600); SCI1_int_init(9600); // Channel to talk to ESP8266 motor0_init(); // These functions actually control PWM outputs motor1_init(); // We use them to run the RGB LED. motor2_init(); RTI_init(); SW_enable(); initq(); DDRH = 0; // PORTH is an input. result = 0; status = 'b'; // Populate binary search tree: set_lcd_addr(0); send_at_command_sci1("ATE0"); // change to ATE1 for debug status = 'i'; // Establish connection to server. send_at_command_sci1("AT+CWMODE=1"); // Set ESP to station mode send_at_command_sci1("AT+CIPMODE=0"); // Set ESP to normal transmission mode send_at_command_sci1("AT+CIPMUX=0"); // Set ESP to single-connection mode send_at_command_sci1("AT+CWJAP=\"Freynet\",\"\""); // Connect to network send_at_command_sci1("AT+CIPSTART=\"TCP\",\"fpf3.net\",12345"); // connect to server while(1){ command = '\0'; while(qempty()); command = getq(); switch (command) { case 'n': status = 'w'; result = new_sequence(); ms_delay(500); // If we finish too quickly, we open a connection the ESP thinks is already open, and it breaks. send_at_command_sci1("AT+CIPSTART=\"TCP\",\"fpf3.net\",12345"); // connect to server break; } outchar0(result); } }
void main() { int period1; // period of pulse train int HI_time1; // High time (pulse width) int LO_time1; // Low time lcd_init(); // start the LCD HILO1_init(); while (1) { HI_time1 = get_HI_time1(); LO_time1 = get_LO_time1(); set_lcd_addr(0x00); write_int_lcd(HI_time1); write_int_lcd(LO_time1); period1 = HI_time1 + LO_time1; set_lcd_addr(0x40); write_int_lcd(period1); ms_delay(100); } }
int new_sequence(void) { unsigned long num = 0, newnum = 0; char c; for(i = 0; i < 10; i++) { while(qempty()); c = getq(); newnum = (c - '0') * pow(10, 9 - i); if (num + newnum < num) { // Check for rollover status = 'e'; set_lcd_addr(0); type_lcd("Sequence goes"); set_lcd_addr(0x40); type_lcd("above 2^32!"); return -1; } num += newnum; // we passed test } while (num != 1){ set_lcd_addr(0); if (display_mode) write_long_lcd(num); if (num % 2 == 0) num /= 2; else{ newnum = (num * 3) + 1; if (newnum < num){ // Check for rollover. status = 'e'; set_lcd_addr(0); type_lcd("Sequence goes"); set_lcd_addr(0x40); type_lcd("above 2^32!"); return -1; } num = newnum; // we passed test } if (slow_mode) ms_delay(750);// to make sure we see what numbers we're getting } if (display_mode) write_long_lcd(num); status = 'i'; return num; }
void clrLine(int ln){ char *blank = " "; switch(ln) { case 1: //clear 1st line set_lcd_addr(0x00); //set cursor to 1st line type_lcd(blank); break; case 2: //clear 2nd line set_lcd_addr(0x40); type_lcd(blank); //set cursor to 2nd line break; default: //clear entire display clear_lcd(); break; } //set_lcd_addr(curs); //set cursor to location curs }
/************** MAIN **************/ void main(void) { seg7_disable(); /* Stop Motors on initial load */ MOTORDDR=0xFF; // enable motor port as output MOTORPORT=0xFF; // turn off all motor ports lcd_init(); // initialize lcd set_lcd_addr(0x00); // set starting lcd address /* check training or playback mode */ while(1){ if(SW2_down()){ // switch2 starts training mode playback=0; training=1; display_movement(&start_training); } if(SW3_down()){ // switch3 stops training mode training=0; display_movement(&stop_training); } if(SW4_down()){ // switch4 starts playback mode if(array_length) { training=0; playback=1; } else { display_movement(&array_empty); } } if(SW5_down()){ // switch5 erases array playback=0; array_length=0; // reset array counter memset(array1, 9, sizeof(array1)); // reset array display_movement(&stop_playback); } get_keypad_input(); playback_robot(); } }
void main(void) { char key; //key pressed Flag *flag = createFlag(); PLL_init(); //set system clock freq to 24MHz lcd_init(); //enable lcd display (clear display) keypad_enable(); //enable keypad led_enable(); //enable leds seg7_disable(); //turn off and disable seg7 display set_lcd_addr(0x40); //set display on 2nd line while(1){ key = getkey(); //read keypad if(flag->shift == true) //shift key was pressed shiftMode(key, flag); //operator input keys else stdMode(key, flag); //standard input keys } //end while(1) } //end main
void main(void){ unsigned long int edge1,edge2, edge3, period, duty; unsigned long int r = 0, q = 0, i, n =0; unsigned int k[8] = {0,0,0,0,0,0,0,0}; char *msg1 = "period(ms):"; char *msg2 = "period(us):" ; char *msg3 = "Duty:" ; char *percent = "%"; char scale = 0x06; begin: TSCR1 = 0x90; //enable timer counter TIOS = 0x00; //enable input-capture 0 TSCR2 = scale; //disable TCNT overflow interrupt TCTL4 = 0x01; //capture rising edge TFLG1 = 0x00; //clear flags register while(!(TFLG1 & 0x01)){} //wait until rising edge edge1 = TC0; //save first rising edge //decrease prescaler by 1 //if count less than value if((edge1 < 2000) || (edge1 < 1000)|| (edge1 < 500)|| (edge1 < 200) ||(edge1 < 100) || (edge1 < 50 )){ if(scale == 0x00){ //if prescaler already 1 skip goto skip; } scale = scale - 1; //decrease prescaler by 1 goto begin; //take input again } skip: TCTL4 = 0x02; //caputre falling edge while(!(TFLG1 & 0x01)){} //wait until falling edge edge2 = TC0; //save first falling edge TCTL4 = 0x01; //detect second rising edge while(!(TFLG1 & 0x01)){} //wait until edge detected edge3 = TC0; //save value period = edge3 - edge1; //calculate period duty = (edge2 - edge1); //calculate duty cycle duty = duty*100; //calculate duty cycle duty = duty/period; //calculate duty cycle lcd_init (); if(scale == 0){ //if prescaler is 1 period = period *1; //multiply period by scaler period = period/24; //divide by 24 for us type_lcd(msg2); } if(scale == 0x01){ //if prescaler is 2 period = period *2; //multiply period by scaler period = period/24; type_lcd(msg2); } if(scale == 0x02){ //if prescaler is 4 period = period *4; //multiply period by scaler period = period/24; //divide by 24 for us type_lcd(msg2); } if(scale == 0x03){ //if prescaler is 8 period = period *8; //multiply period by scaler period = period/24; //divide by 24 for us type_lcd(msg2); } if(scale == 0x04){ //if prescaler is 16 period = period *16; //multiply period by scaler period = period/24; //divide by 24 for us type_lcd(msg2); } if(scale == 0x05){ //if prescaler is 32 period = period *32; //multiply period by scaler period = period/24000; //divide by 24000 for ms type_lcd(msg1); } if(scale == 0x06){ //if prescaler is 64 period = period *64; //multiply period by scaler period = period/24000; //divide by 24000 for ms type_lcd(msg1); } do { //convert period from hex to dec q = period/10; r = period%10; k[n] =r; //store values to be displayed period /= 10; n++; //increase index }while(q != 0); while (n != 0 ) { //display decimal value of period k[n-1] = (char)k[n-1]; //convert to char k[n-1] = k[n-1] + 0x30; //convert to ASCII data8(k[n-1]); //display to LCD n--; } n= 0; //reset n do { //convert duty from hex to dec q = (duty/10); r =(duty%10); k[n] =r; //store values to be displayed duty /= 10; n++; //Increment index }while(q != 0); set_lcd_addr(0x40); type_lcd(msg3); while (n != 0 ) { //display decimal value of duty k[n-1] = (char)k[n-1]; //convert to char k[n-1] = k[n-1] + 0X30; //convert to ASCII data8(k[n-1]); //display to LCD n--; } type_lcd(percent); }
void shiftMode(char key, Flag *flag){ switch(key){ case 0xA: //if memory set (A) key - save number midx = kidx; strcpy(mem, kbuf); //copy data from kbuf to mem flag->save = true; led_on(1); //turn on 2nd led to indicate saved number wait_keyup(); break; case 0xB: //if memory recall (B) key - get saved number if(flag->save == true){ flag->olock = false; flag->nlock = true; strcpy(kbuf, mem); kidx = midx; //kbuf[kidx] = '\0'; clrLine(2); //clear line 2 display(kbuf, kidx, 0x40); wait_keyup(); } break; case 0x1: //if plus '+' (1) key addOper('+', flag); break; case 0x2: //if minus '-' (2) key addOper('-', flag); break; case 0x3: //if mod '%' (3) key addOper('%', flag); break; case 0x4: //if multiply '*' (4) key addOper('*', flag); break; case 0x5: //if divide key '/' (5) key addOper('/', flag); break; case 0x6: //if power '^' (6) key addOper('^', flag); break; case 0x7: //if left parenthesis '(' (7) key if(flag->olock == true){ flag->leftpar += 1; kbuf[kidx++] = '('; display(kbuf,kidx, 0x40); wait_keyup(); } break; case 0x8: //if right parenthesis ')' (8) key if(flag->leftpar>0 && flag->olock==false){ //if '(' present flag->leftpar -= 1; kbuf[kidx++] = ')'; display(kbuf,kidx, 0x40); flag->nlock = true; //nlock = true to stop taking number inputs wait_keyup(); } break; case 0x9: //if factorial '!' (9) key addOper('!', flag); break; case 0xC: //if clear (C) key flag->nlock = false; flag->olock = true; flag->deci = false; flag = createFlag(); clear_lcd(); kidx = 0; //reset kbuf index kbuf[kidx] = '\0'; //clear kbuf oidx = 0; //reset oper index oper[oidx] = '\0'; //clear oper substart = 0; set_lcd_addr(0x40); wait_keyup(); //wait to release key break; case 0xD: // if shift (D) key flag->shift = false; wait_keyup(); //wait to release key break; case 0xF: //if equal (#) key clrLine(1); //-------------------- if(oidx > 0){ strcat(oper, kbuf); oidx += kidx; } else{ oidx = kidx; strcpy(oper, kbuf); } oper[oidx++] = ' '; flag->nlock = false; flag->deci = false; kidx = 0; //------------------- --oidx; //remove space display(oper, oidx, 0x40); wait_keyup(); break; case 0x0: break; default: break; } //end switch }
void main(){ long int n; int i; float ix = 0.0,duty, period; char j, scale = 0x06; char *msg1 = "Period(us):"; char *msg2 = "Duty(%):"; char *msg3 = "Not valid"; char *msg4 = "Displaying..."; char *msg5 = "Go again?"; lcd_init(); type_lcd(msg1); n = 0; getperiod: i= getkey1(); //get key from keypad delayms(10); if(i == 0x0A){ //if i=A (enter key) clear_lcd(); //clear lcd type_lcd(msg2); //diplay duty message delayms(1000); //diplay message for 1s period = n; //save period value n= 0; //reset n to be used for duty goto getduty; //get duty cycle } else if (i > 0x0A) { //if i not a valid input clear_lcd(); //clear lcd type_lcd(msg3); //display error message delayms(1000); //display for 1s n = 0; //reset n clear_lcd(); type_lcd(msg1); goto getperiod; //get period again } if((n*10+i) > 1360){ //if n exceeds limit clear_lcd(); //clear lcd type_lcd(msg3); //display error message delayms(1000); //display for 1s n = 0; //reset n clear_lcd(); type_lcd(msg1); goto getperiod; //get period again } j= (char)i; //turn i into char j=j+0x30; //convert hex to ASCII data8(j); //display ASCII to LCD n = n*10 +i; //keep track of decimal value goto getperiod; //get another key getduty: i= getkey1(); //get key from keypad delayms(10); if(i == 0x0A){ //if i=A (enter key) clear_lcd(); //clear lcd duty = n; //save duty value n = 0; //reset n goto signal; //output signal } else if (i > 0x0A) { //if i not a valid input clear_lcd(); //clear lcd type_lcd(msg3); //display error message delayms(1000); //display for 1s n = 0; //reset n clear_lcd(); type_lcd(msg2); goto getduty; //get duty cycle again } if((n*10+i) > 100){ //if n exceeds limit clear_lcd(); //clear lcd type_lcd(msg3); //display error message delayms(1000); //display for 1s n = 0; //reset n clear_lcd(); type_lcd(msg2); goto getduty; //get duty again } j= (char)i; //turn i into char j=j+0x30; //convert hex to ASCII data8(j); //display ASCII to LCD n = n*10 +i; //keep track of decimal value goto getduty; //get another key signal: if(period < 10){ period= period*24; //calculate count duty = duty/100; //calculate duty duty = duty*period; scale = 0x00; }else if(period >680){ period= period*24; //calculate count period = period/128; //calculate count duty = duty/100; //calculate duty duty = duty*period; scale = 0x07; }else{ period= period*24; //calculate count period = period/64; //calculate count duty = duty/100; //calculate duty duty = duty*period; //calculate duty scale = 0x06; } PWMCLK = 0; //select clock A as source PWMPRCLK = scale; //set clock A prescaler to 64 PWMPOL = 1; //channel 9 output high at start PWMCAE = 0; //left-aligned mode PWMCTL = 0x0C; //8-bit mode, stop in wait and freeze mode PWMPER0 = period; //set period value PWMDTY0 = duty; //set duty value PWME = 0x01; //enalbe PWM0 to output signal while (1){ type_lcd(msg4); //displaying signal set_lcd_addr(0x40); type_lcd(msg5); //ask if want to create new waveform i= getkey1(); //get key from keypad delayms(10); if(i == 0x0A){ //if i=A (enter key) clear_lcd(); //clear lcd type_lcd(msg1); //diplay period message delayms(1000); //diplay message for 1s n= 0; //reset n goto getperiod; //get new period }else{ clear_lcd(); } } }