void gsm_send_command(const char *command) { unsigned char rec_data; while (*command != 0) { uart_send(*command++); // Send the AT command. rec_data = uart_rec(); // Read the echo. } // Send <ENTER>. uart_str("\r\n"); // Read the echo until Line Feed character is received. do { rec_data = uart_rec(); } while (rec_data != '\n'); }
int main (void) { init(); led_on(); uart_str("Hello!\r\n"); spi_slave(); led_off(); while (1) { switch (spi_recv()) { case 0: break; case 1: // CPU wants to read a value. this means we are now the master, to transfer the value back. spi_master(); spi_send(uart_read()); spi_slave(); break; case 2: // CPU wrote a value. Output it to serial. uart_write(spi_recv()); break; default: break; } } }
int main() { uart_init(); uart_str("Hello World"); while(1); }
//------------------------------------------------------------------------------ void main() { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer #if 1 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0; DCOCTL = 0x00; BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; BCSCTL1 |= XT2OFF | DIVA_0; BCSCTL3 = XT2S_0 | LFXT1S_0 | XCAP_1; /*do { IFG1 &= ~OFIFG; // Clear OSC fault flag __delay_cycles(50); // 50us delay } while (IFG1 & OFIFG); */ WDTCTL = WDTPW | WDTTMSEL | WDTSSEL; #else BCSCTL3 |= LFXT1S_2; // VLOCLK BCSCTL1 = CALBC1_1MHZ; // Set DCO to calibrated 1 MHz. DCOCTL = CALDCO_1MHZ; //BCSCTL1 |= DIVA_0; // ACLK / 8 BCSCTL3 |= XCAP_3; #endif // Init timer1 // TA0CCR0 = 0x7FFF; TA0CCTL0 = CCIE; TA0CTL = TASSEL_1 + MC_1 + ID_0; // ACLK, up to CCR0, divider /1 // Init timer2 // TA1CCR0 = 0x3FFF; TA1CCTL0 = CCIE; TA1CTL = TASSEL_1 + MC_1 + ID_0; // ACLK, up to CCR0, divider /1 // Init timer3 // WDTCTL = WDT_ADLY_1000; IE1 |= WDTIE; uart_init(); // main loop // config_port_init(); app_init(); uart_num(BCSCTL1); uart_str("main\n"); //if (BCSCTL3 & LFXT1OF) // uart_str("osc fault\n"); //__enable_interrupt(); while (1) { if (g_event == 0) __bis_SR_register(LPM0_bits + GIE); if (g_event & EV_TIMER1) { if (cb_timer1) cb_timer1(); g_event &= ~EV_TIMER1; } if (g_event & EV_TIMER2) { if (cb_timer2) cb_timer2(); g_event &= ~EV_TIMER2; } if (g_event & EV_TIMER3) { if (cb_timer3) cb_timer3(); g_event &= ~EV_TIMER3; } if (g_event & EV_TX) { if (cb_uart_tx) cb_uart_tx(); g_event &= ~EV_TX; } } }
// main function (main fucntion of the program) //========================================================================== void main() { char read_sms_command[] = "AT+CMGR=1"; char delete_sms_command[] = "AT+CMGD=1"; //set I/O input output TRISB = 0b00000011; //configure PORTB I/O direction TRISD = 0b00000000; //configure PORTD I/O direction TRISA = 0b00000111; //configure PORTA I/O direction LED1=0; // OFF LED1 LED2=0; // OFF LED2 //Configure UART SPBRG=10; //set baud rate as 115200 baud BRGH=1; //baud rate high speed option TXEN=1; //enable transmission TX9 =0; //8-bit transmission RX9 =0; //8-bit reception CREN=1; //enable reception SPEN=1; //enable serial port //setup ADC ADCON1 = 0b00000110; //set ADx pin digital I/O //configure lcd send_config(0b00000001); //clear display at lcd send_config(0b00000010); //lcd return to home send_config(0b00000110); //entry mode-cursor increase 1 send_config(0b00001100); //display on, cursor off and cursor blink off send_config(0b00111000); //function set //display startup message lcd_clr(); //clear lcd send_string("Cytron Tech."); //display "Cytron Tech." lcd_goto(20); //set the lcd cursor to location 20 send_string("Smart Home"); //display "Smart Home" // Delay for a while. delay(100000); // Clear the LCD and display the new message. lcd_clr(); send_string("Testing Com..."); // Make sure we can communicate with the GSM modem. gsm_send_command("AT"); // Read the response. gsm_read_line(&gsm_response); // We should receive "OK" from the GSM modem. // If we don't, display "Error". if (memcmp("OK", &gsm_response, 2) != 0) { lcd_clr(); send_string("Error..."); while(1); } // Clear the LCD and display the new message. lcd_clr(); send_string("Waiting for"); lcd_goto(20); send_string("Call Ready"); // Waiting for the GSM modem to search for the network. // We will receive a blank line before receiving "Call Ready". gsm_read_line(&gsm_response); gsm_read_line(&gsm_response); // We should receive "Call Ready" from the GSM modem. // If we don't, display "Error". if (memcmp("Call Ready", &gsm_response, 10) != 0) { lcd_clr(); send_string("Error..."); while(1); } // Clear the LCD and display the new message. lcd_clr(); send_string("Setting Text"); lcd_goto(20); send_string("Mode..."); // Set text mode for SMS. gsm_send_command("AT+CMGF=1"); // Read the "OK". gsm_read_line(&gsm_response); // Clear the LCD and display the new message. lcd_clr(); send_string("Ready"); while(1) { // Check whether there is new data from the GSM modem. if (RCIF == 1) { // Check is there a new SMS? gsm_read_line(&gsm_response); if (memcmp("+CMTI: \"SM\",", &gsm_response, 12) == 0) { // Get the SMS index. // The command array is already initialized as "AT+CMGR=1" and "AT+CMGD=1", // we need to changed the index to the actual one. read_sms_command[8] = gsm_response[12]; delete_sms_command[8] = gsm_response[12]; // Clear the LCD and display the new message. lcd_clr(); send_string("Reading SMS..."); // Send command to read the SMS. gsm_send_command(&read_sms_command); // Read the response. // The first line is the SMS info which we don't need. // We only need the second line which is the message content. gsm_read_line(&gsm_response); gsm_read_line(&command); // Read the newline and "OK". gsm_read_line(&gsm_response); gsm_read_line(&gsm_response); // Switch on the light if we received "on". if (memcmp("on", &command, 2) == 0) { LED1 = 1; LED2 = 1; } // Switch off the light if we received "off". else if (memcmp("off", &command, 3) == 0) { LED1 = 0; LED2 = 0; } // Send command to delete the SMS. gsm_send_command(&delete_sms_command); // Read the "OK". gsm_read_line(&gsm_response); // Clear the LCD and display the new message. lcd_clr(); send_string("Ready..."); } } // Check whether SW1 is pressed. if (SW1 == 0) { // Clear the LCD and display the new message. lcd_clr(); send_string("Sending SMS..."); // Send the SMS to notify the owner. // Please change this to your own number. gsm_send_command("AT+CMGS=\"0123456789\""); // We should receive '>' from the modem. // If we don't, display "Error". if (uart_rec() != '>') { lcd_clr(); send_string("Error..."); while(1); } // Send the message. uart_str("Warning: Switch 1 has been triggered !"); // Send <CTRL+Z>. uart_send(0x1a); // Read the newline and response. gsm_read_line(&gsm_response); gsm_read_line(&gsm_response); // We should receive "+CMGS: <Message ID>" from the GSM modem. // If we don't, display "Error". if (memcmp("+CMGS:", &gsm_response, 6) != 0) { lcd_clr(); send_string("Error..."); while(1); } // Read the newline and "OK". gsm_read_line(&gsm_response); gsm_read_line(&gsm_response); // Clear the LCD and display the new message. lcd_clr(); send_string("Ready..."); // Wait until SW1 is released so that we don't send duplicated message. while (SW1 == 0); } } }