static void _initialize_adc(void)
{
	/* Initialize ADC */
	adc_initialize();
	adc_set_ts_mode(0);
	/* Set ADC clock */
	adc_set_clock(BOARD_ADC_FREQ);
	/* Set ADC timing */
	adc_set_timing(ADC_MR_STARTUP_SUT512, 0, 0);
	/* Enable channel number tag */
	adc_set_tag_enable(true);
}
Example #2
0
/*******************************************************************************
* MAIN FUNCTION                                                                *
*******************************************************************************/
int main(void)
{
	unsigned char value = 0;	// declare a variable to store
	// ensure all the hardware port in zero initially
	PORTA = 0;
	PORTB = 0;
	PORTC = 0;
	PORTD = 0;
	PORTE = 0;

	// Initialize the I/O port direction, this must be configured according to circuit
	// please refer to PTK40A schematic for details
	// TRISX control pin direction, output pin must be configure as '0'
	// while input must be configure as '1'
	TRISA = 0b00010001;
	TRISB = 0b00001111;
	TRISC = 0b10010011;
	TRISD = 0;
	TRISE = 0;

	// Initialize ADC.
	adc_initialize();	//Ensure pin share with analog is being configured to digital correctly

	// Initialize 7 segments
	seg7_initialize();	//Ensure 7 segment is blank at beginning

	// Initialize LCD
	lcd_initialize();	//Initialize LCD before use

	beep(2); 	//buzzer sound for twice

	// PTK40A have a 4x4 matrix keypad, it has 16 key press.
	// Please refer to PTK40A schematic for the connection
	// Please refer to keypad.c for the details of function works
	// press keypress will display the value on LCD, you will need to release keypad to see value

	LCD_BACKLIGHT = 1;	//activate LCD Backlight

	lcd_putstr("Cytron PTK40A");	//display message on LCD
	lcd_2ndline();					//move cursor to 2nd line
	lcd_putstr("Keypad:");

	while(1) 	// create an infinite loop
	{
		lcd_goto(0x47);	//move cursor to after Keypad: on LCD
		lcd_putchar(c_key_to_ASCII(c_wait_keypad()));	// wait for keypress
														// convert to ASCII and display LCD
	}
	while(1) continue;	// infinite loop to prevent PIC from reset if there is no more program
}
Example #3
0
/*******************************************************************************
* MAIN FUNCTION                                                                *
*******************************************************************************/
int main(void)
{
	// ensure all the hardware port in zero initially
	PORTA = 0;
	PORTB = 0;
	PORTC = 0;
	PORTD = 0;
	PORTE = 0;

	// Initialize the I/O port direction, this must be configured according to circuit
	// please refer to PTK40A schematic for details
	// TRISX control pin direction, output pin must be configure as '0'
	// while input must be configure as '1'
	TRISA = 0b00010001;
	TRISB = 0b00001111;
	TRISC = 0b10010011;
	TRISD = 0;
	TRISE = 0;

	// Initialize ADC.
	adc_initialize();	//Ensure pin share with analog is being configured to digital correctly
	
	// Initialize 7 segments
	seg7_initialize();	//Ensure 7 segment is blank at beginning
	
	// Initialize LCD
	lcd_initialize();	// LCD must be initialize before it can be use
						// after initialize, the black rectangular on LCD will disappear
	
	beep (2);		// buzzer sound twice
	
	LCD_BACKLIGHT = 1;	//activate LCD backlight
	
	// PTK40A is using 2x16 parallel LCD, it is interface in 8-bit mode
	// Require 10 digital pin to send message to LCD
	// Please refer to PTK40A schematic for the connection from PIC and lcd.c for the function details
	// Press SW1 to display "Hello World" to LCD	
	
	while (SW1 == 1) continue; 	//wait for SW1 to be press
	
	lcd_goto(0x01);	 // move LCD cursor to 2nd column
	lcd_putstr("Cytron PTK40A");	// display string at 1st row
	lcd_goto(0x42);	// move LCD cursor to 2nd row, 3rd column
	lcd_putstr("Hello World");		// display hello world at 2nd row
	
	while(1) continue;	// infinite loop to prevent PIC from reset if there is no more program	
}
Example #4
0
/*******************************************************************************
* MAIN FUNCTION                                                                *
*******************************************************************************/
int main(void)
{
	// ensure all the hardware port in zero initially
	PORTA = 0;
	PORTB = 0;
	PORTC = 0;
	PORTD = 0;
	PORTE = 0;

	// Initialize the I/O port direction, this must be configured according to circuit
	// please refer to PTK40A schematic for details
	// TRISX control pin direction, output pin must be configure as '0'
	// while input must be configure as '1'
	TRISA = 0b00010001;
	TRISB = 0b00001111;
	TRISC = 0b10010011;
	TRISD = 0;
	TRISE = 0;

	// Initialize ADC.
	adc_initialize();	//Ensure pin share with analog is being configured to digital correctly
	
	beep(2);	//buzzer sound twice
	// PTK40A come with a external digital input port
	// it can use to connect external digital sensor, e.g. IR01A, SUNX photo-electric, fiber-optic, etc
	// This example will use digital sensor (SN-E18-B03N1) to detect object in sensing range.
	// The IR sensor will detect object in front of it and give the controller signal 
	// the signal is n 5v or 0v. 
	// In this example, we are able to use the IR to sound the buzzer when something is in front of the sensor
	
	
	while(1) 	// create an infinite loop
	{
	
		if(DIGITAL_IN==1)
		{
		BUZZER=0;		
		}
		else
		{
		BUZZER=1;	
		}
	}	
	
	while(1) continue;	// infinite loop to prevent PIC from reset if there is no more program	
}
Example #5
0
int main(void)
{
    clock_initialize();
    adc_initialize();
    rm_encoder_initialize();
    rm_encoder_power_on();
    cc1101_initialize();


    while(1)
    {
        // Periodically update encoder value
        rm_encoder_read_blocking();
        _delay_us(250);
        //spi_master_transact(0xA9);
    }
    return 0; // End return state
}
Example #6
0
int main(void)
{
  // Stop watchdog timer for now
  WDTCTL = WDTPW | WDTHOLD;

  // Enable JTAG (keep this line here)
  SYSCTL |= SYSJTAGPIN;

  // Initialize state variables
//floatswitch_active = 0;
  floatswitches = 0;
  battery_charge = 0;
  solarpanel_voltage = 0;
  pump_active = 0;
  tryagain_timeelapsed = 0;
  last_sent_warningtext = 0;

  // Read in the saved phone number from memory, if it is there
  memset(phone_number, '\0', MAX_PHONE_LENGTH);
  if(strncmp(PHONE_ADDRESS, "+1", 2) == 0) // Phone numbers start with +1
    strncpy(phone_number, PHONE_ADDRESS, MAX_PHONE_LENGTH); // copy from flash into ram

  // Set up float switches
  FLOAT_PORT_DIR &= ~(FLOATSWITCH_0 | FLOATSWITCH_1 | FLOATSWITCH_2 | FLOATSWITCH_3 | FLOATSWITCH_4);
  FLOAT_PORT_REN |= FLOATSWITCH_0 | FLOATSWITCH_1 | FLOATSWITCH_2 | FLOATSWITCH_3 | FLOATSWITCH_4;
  FLOAT_PORT_OUT |= FLOATSWITCH_0 | FLOATSWITCH_1 | FLOATSWITCH_2 | FLOATSWITCH_3 | FLOATSWITCH_4;

  // Set up water pump and solarpanel on/off
  PUMPSOLAR_PORT_DIR |= PUMP_CONTROL | SOLARPANEL_CONTROL;
  PUMPSOLAR_PORT_OUT &= ~(PUMP_CONTROL | SOLARPANEL_CONTROL);

  // Set up msp430 LEDs
  LED_PORT_DIR |= (LED_MSP | LED_MSP_2);
  LED_PORT_OUT &= ~(LED_MSP | LED_MSP_2);
//  P1DIR |= LED_MSP;
//    P4DIR |= LED_MSP_2;
//  P1OUT &= ~LED_MSP;
//    P4OUT &= ~LED_MSP_2;

  // Set up gsm 'power button' (not used now)
//  P1DIR &= ~POWER_BUTTON;
//  P1REN |= POWER_BUTTON;
//  P1OUT |= POWER_BUTTON;
//  P1IE |= POWER_BUTTON; // interrupts
//  P1IES |= POWER_BUTTON; // high->low transition

  // Set up gsm power status input
  GSM_PORT_DIR &= ~GSM_POWER_STATUS; // input

  // Power on the voltage regulator for the gsm
  GSMPOWER_PORT_DIR |= GSMPOWER_ENABLE_PIN; // output mode
  GSMPOWER_PORT_OUT |= GSMPOWER_ENABLE_PIN;

  // Initialize the uart and ADC, start ADC conversion
  uart_initialize();
  adc_initialize();

  // Wait a bit
  __delay_cycles(1048576); // 1 second

  // Enable watchdog interrupts and interrupts in general
  SFRIE1 |= WDTIE;
  _BIS_SR(GIE);

  // Start conversion
  adc_start_conversion();

  // Check if GSM module is on
  while(!(GSM_PORT_IN & GSM_POWER_STATUS)) // is off
  {
    toggle_gsm_power();
    __delay_cycles(20000000); // wait
  }

  // Send an AT first
	LED_PORT_OUT |= LED_MSP;

	tx_buffer_reset();
  strcpy(tx_buffer, "AT\r\n");
  uart_send_command();

  // Start up Timer A0
  TA0CTL = TACLR; // clear first
  TA0CTL = TASSEL__ACLK | ID__8 | MC__STOP; // auxiliary clock (32.768 kHz), divide by 8 (4096 Hz), interrupt enable, stop mode
  TA0CCTL0 = CCIE; // enable capture/compare interrupt
  TA0CCR0 = 4096; // reduces rate to 1 times/sec
  TA0CTL |= MC__UP; // start the timer in up mode (counts to TA0CCR0 then resets to 0)

  // start the clock
  rtc_initialize();

  // Turn CPU off
  LPM0;

  // Main loop
  while(1)
  {
    // Check if a UART command has finished and respond accordingly
    if(uart_command_has_completed)
    {
      switch(uart_command_state)
      {
      case CommandStateSendingAT:
      {
        if(uart_command_result == UartResultOK)
        {
          // Send ATE0 because we do not need a copy of what we send
            uart_command_state = CommandStateTurnOffEcho;
            tx_buffer_reset();
            strcpy(tx_buffer, "ATE0\r\n");
            uart_send_command();
        }
        break;
      }
      case CommandStateTurnOffEcho: // Got a response after sending AT
      {
        if(uart_command_result == UartResultOK)
        {
          // Send cmgf
          // This puts the cell module into SMS mode, as opposed to data mode
          uart_command_state = CommandStateGoToSMSMode;
          tx_buffer_reset();
          strcpy(tx_buffer, "AT+CMGF=1\r\n");
          uart_send_command();
        }
        break;
      }

      case CommandStateGoToSMSMode: // Got a response after sending CMGF
      {
        if(uart_command_result == UartResultOK)
        {
          LED_PORT_OUT &= ~(LED_MSP | LED_MSP_2); // leds off

          // We are now ready to send a text whenever the system needs to
          uart_enter_idle_mode();
        }
        else
          uart_enter_idle_mode();
        break;
      }

      case CommandStatePrepareWarningSMS: // Got a response after sending CMGS
      {
        if(uart_command_result == UartResultInput)
        {
          // Send the text now
          uart_command_state = CommandStateSendWarningSMS;
          tx_buffer_reset();
          strcpy(tx_buffer, "Msg from Sol-Mate: Check your boat; water level is getting high.\r\n\x1A");
          uart_send_command();
        }
        break;
      }

      case CommandStateSendWarningSMS: // Got a response after sending the text
      {
        if(uart_command_result == UartResultOK)
        {
          LED_PORT_OUT &= ~LED_MSP; // red LED off
          sent_text = 1; // Do not send the text again (this is for testing purposes--to send another text you have to restart the MSP)

          // Delete all stored messages.
          uart_command_state = CommandStateDeleteSMS;
          tx_buffer_reset();
          strcpy(tx_buffer, "AT+CMGD=1,4\r\n");
          uart_send_command();
        }
        else if(uart_command_result == UartResultError) // sms failed to send
        {
          LED_PORT_OUT |= LED_MSP;

          // Set up timer to try again
          TA2CTL = TACLR;
          TA2CTL = TASSEL__ACLK | ID__8 | MC__STOP;
          TA2CCTL0 = CCIE;
          TA2CCR0 = TIMEOUT_SMS;

          // Prepare again
          uart_command_state = CommandStatePrepareWarningSMS;
          tx_buffer_reset();
          strcpy(tx_buffer, "AT+CMGS=\"");
          strcat(tx_buffer, phone_number);
          strcat(tx_buffer, "\"\r\n");

          // Enable timer, go to sleep (uart will be disabled too because LPM2)
          TA2CTL |= MC__UP;
          LPM2;
        }
        else
          uart_enter_idle_mode();

        break;
      }

      case CommandStateUnsolicitedMsg: // Received a message from the cell module
      {
        LED_PORT_OUT |= LED_MSP; // red LED on

        // Check what kind of code this is..
        // --SMS--
        // +CMTI: "SM",3\r\n
        if(strstr(rx_buffer, "+CMTI")) // strstr returns null/0 if not found
        {
          // Find the comma
          char *begin_ptr = strchr(rx_buffer, ',');
          if(!begin_ptr) {
            uart_enter_idle_mode();
            break;
          }
          begin_ptr++; // should point to the beginning of the SMS index we need

          // Find the '\r' which is directly following the last character of the SMS index
          char *end_ptr = strchr(begin_ptr, '\r');
          if(!end_ptr) {
            uart_enter_idle_mode();
            break;
          }

          // Create the command to read the sms
          tx_buffer_reset();
          strcat(tx_buffer, "AT+CMGR=");
          strncat(tx_buffer, begin_ptr, end_ptr - begin_ptr); // SMS index
          strcat(tx_buffer, "\r\n");

          // Send the command
          LED_PORT_OUT &= ~LED_MSP; // red LED on
          uart_command_state = CommandStateReadSMS;
          uart_send_command();
        }
        else // unrecognized
        {
          LED_PORT_OUT &= ~LED_MSP;
          uart_enter_idle_mode();
        }

        break;
      }

      case CommandStateReadSMS:
      {
        LED_PORT_OUT |= LED_MSP; // red LED on
        if(uart_command_result == UartResultOK)
        {
          // +CMGR: "<status>","<origin number>","<??>","<timestamp>"\r\n
          // text contents here\r\n
          // \r\n
          // OK\r\n

          // find the 1st comma
          char *begin_ptr_phone = strchr(rx_buffer, ',');
          if(!begin_ptr_phone || *(begin_ptr_phone+1) != '"') {
            uart_enter_idle_mode();
            break;
          }
          begin_ptr_phone += 2; // Move to the beginning of the number

          // find the ending quotation mark
          char *end_ptr_phone = strchr(begin_ptr_phone, '"');
          if(!end_ptr_phone) {
            uart_enter_idle_mode();
            break;
          }

          // Check if it's too long
          if(end_ptr_phone - begin_ptr_phone > MAX_PHONE_LENGTH) {
            uart_enter_idle_mode();
            break;
          }

          // Look at the contents of the text - it starts right after the first \r\n
          char *begin_ptr_sms = strchr(rx_buffer, '\n');
          if(!begin_ptr_sms) {
            uart_enter_idle_mode();
            break;
          }
          begin_ptr_sms++; // Move to the beginning of the text

          // The text ends right before the next \r\n
          char *end_ptr_sms = strchr(begin_ptr_sms, '\r');
          if(!end_ptr_sms) {
            uart_enter_idle_mode();
            break;
          }

          // Check for the "password"
          if(strstr(begin_ptr_sms, "978SolMate"))
          {
            // copy the phone number into ram
            memset(phone_number, '\0', MAX_PHONE_LENGTH);
            strncpy(phone_number, begin_ptr_phone, end_ptr_phone - begin_ptr_phone);

            // Now copy it into flash memory
            flash_erase(PHONE_ADDRESS);
            flash_write_phone_number(phone_number, MAX_PHONE_LENGTH);

            // Send the user an acknowledgement
            LED_PORT_OUT &= ~LED_MSP; // red LED off
            uart_command_state = CommandStatePreparePhoneSMS;
            tx_buffer_reset();
            strcpy(tx_buffer, "AT+CMGS=\"");
            strncat(tx_buffer, phone_number, MAX_PHONE_LENGTH);
            strcat(tx_buffer, "\"\r\n");
            uart_send_command();
          }
          // Status report?
          else if(strstr(begin_ptr_sms, "What's up"))
          {
            // Send user the status report
            LED_PORT_OUT &= ~LED_MSP;
            uart_command_state = CommandStatePrepareStatusSMS;
            tx_buffer_reset();
            strcpy(tx_buffer, "AT+CMGS=\"");
            strncat(tx_buffer, phone_number, MAX_PHONE_LENGTH);
            strcat(tx_buffer, "\"\r\n");
            uart_send_command();
          }
          else // Unrecognized text
          {
            LED_PORT_OUT &= ~LED_MSP;

            // Delete all stored messages.
            uart_command_state = CommandStateDeleteSMS;
            tx_buffer_reset();
            strcpy(tx_buffer, "AT+CMGD=1,4\r\n");
            uart_send_command();
          }
        }
        else
          uart_enter_idle_mode();

        break;
      }

      case CommandStatePreparePhoneSMS:
      {
        LED_PORT_OUT |= LED_MSP; // red LED on

        if(uart_command_result == UartResultInput)
        {
          // Send the text now
          uart_command_state = CommandStateSendPhoneSMS;
          tx_buffer_reset();
          strcpy(tx_buffer, "Msg from Sol-Mate: Your phone number has been successfully changed.\r\n\x1A");
          uart_send_command();
        }
        break;
      }

      case CommandStatePrepareStatusSMS:
      {
        LED_PORT_OUT |= LED_MSP; // red led
        if(uart_command_result == UartResultInput)
        {
          // Put together the status text
          uart_command_state = CommandStateSendStatusSMS;
          tx_buffer_reset();
          strcpy(tx_buffer, "Msg from Sol-Mate: Here's your status report.\r\n");

          // Battery status
          if(battery_charge > 228) // 12.9V
            strcat(tx_buffer, "Battery level: Full\r\n");
          else if(battery_charge > 210) // About 50% - 12.55V
            strcat(tx_buffer, "Battery level: Medium\r\n");
          else if(battery_charge > 190) // 12.2V
            strcat(tx_buffer, "Battery level: Low\r\n");
          else
            strcat(tx_buffer, "Battery level: Very Low\r\n");

          // Solar panel charge
          if(solarpanel_voltage > 186)
            strcat(tx_buffer, "Charge rate: High\r\n");
          else if(solarpanel_voltage > 113)
            strcat(tx_buffer, "Charge rate: Medium\r\n");
          else if(solarpanel_voltage > 39)
            strcat(tx_buffer, "Charge rate: Low\r\n");
          else
            strcat(tx_buffer, "Charge rate: None\r\n");

          // Water depth
          int water_level = get_water_level(floatswitches, 5);
          switch(water_level)
          {
            case 0: // No floatswitches are active.
              strcat(tx_buffer, "Water level: None\r\n");
              break;
            case 1: // Lowest floatswitch is active.
              strcat(tx_buffer, "Water level: Very low\r\n");
              break;
            case 2: // Two lowest floatswitches are active.
              strcat(tx_buffer, "Water level: Low\r\n");
              break;
            case 3: // All three floatswitches are active.
              strcat(tx_buffer, "Water level: Medium\r\n");
              break;
            case 4:
              strcat(tx_buffer, "Water level: High\r\n");
              break;
            case 5:
              strcat(tx_buffer, "Water level: Very high\r\n");
              break;
            default: // Any other combination.
              strcat(tx_buffer, "Water level: ERR INVALID READING\r\n");
              break;
          }

          // Bailer
          if(pump_active)
            strcat(tx_buffer, "Water pump: On");
          else
            strcat(tx_buffer, "Water pump: Off");

          strcat(tx_buffer, "\r\n\x1A");
          uart_send_command();
        }
        break;
      }

      case CommandStateSendPhoneSMS:
      {
        if(uart_command_result == UartResultOK)
        {
          // Delete all stored messages.
          uart_command_state = CommandStateDeleteSMS;
          tx_buffer_reset();
          strcpy(tx_buffer, "AT+CMGD=1,4\r\n");
          uart_send_command();
        }
        else if(uart_command_result == UartResultError) // sms failed to send
        {
          LED_PORT_OUT |= LED_MSP;

          // Set up timer to try again
          TA2CTL = TACLR;
          TA2CTL = TASSEL__ACLK | ID__8 | MC__STOP;
          TA2CCTL0 = CCIE;
          TA2CCR0 = TIMEOUT_SMS; // 4096 times 10 -> 10 seconds

          // Prepare again
          uart_command_state = CommandStatePreparePhoneSMS;
          tx_buffer_reset();
          strcpy(tx_buffer, "AT+CMGS=\"");
          strcat(tx_buffer, phone_number);
          strcat(tx_buffer, "\"\r\n");

          // Enable timer, go to sleep (uart will be disabled too because LPM2)
          TA2CTL |= MC__UP;
          LPM2;
        }

        break;
      }
      case CommandStateSendStatusSMS:
      {
        if(uart_command_result == UartResultOK)
        {
          // Delete all stored messages.
          uart_command_state = CommandStateDeleteSMS;
          tx_buffer_reset();
          strcpy(tx_buffer, "AT+CMGD=1,4\r\n");
          uart_send_command();
        }
        else if(uart_command_result == UartResultError) // sms failed to send
        {
          LED_PORT_OUT |= LED_MSP;

          // Set up timer to try again
          TA2CTL = TACLR;
          TA2CTL = TASSEL__ACLK | ID__8 | MC__STOP;
          TA2CCTL0 = CCIE;
          TA2CCR0 = TIMEOUT_SMS; // 4096 times 10 -> 10 seconds

          // Prepare again
          uart_command_state = CommandStatePrepareStatusSMS;
          tx_buffer_reset();
          strcpy(tx_buffer, "AT+CMGS=\"");
          strcat(tx_buffer, phone_number);
          strcat(tx_buffer, "\"\r\n");

          // Enable timer, go to sleep (uart will be disabled too because LPM2)
          TA2CTL |= MC__UP;
          LPM2;
        }

        break;
      }

      case CommandStateDeleteSMS:
      {
        if(uart_command_result == UartResultOK)
          LED_PORT_OUT &= ~LED_MSP; // red LED off

        uart_enter_idle_mode();
        break;
      }
    }

    // Turn CPU off until someone calls LPM0_EXIT (uart interrupt handler will)
    LPM0;
    }
  }
}
Example #7
0
/*******************************************************************************
* MAIN FUNCTION                                                                *
*******************************************************************************/
int main(void)
{
	unsigned char distance = 0;	// declare a variable to store distance 
	// ensure all the hardware port in zero initially
	PORTA = 0;
	PORTB = 0;
	PORTC = 0;
	PORTD = 0;
	PORTE = 0;

	// Initialize the I/O port direction, this must be configured according to circuit
	// please refer to PTK40A schematic for details
	// TRISX control pin direction, output pin must be configure as '0'
	// while input must be configure as '1'
	TRISA = 0b00010001;
	TRISB = 0b00001111;
	TRISC = 0b10010011;
	TRISD = 0;
	TRISE = 0;

	// Initialize ADC.
	adc_initialize();	//Ensure pin share with analog is being configured to digital correctly
	
	// Initialize LCD
	lcd_initialize();	//Initialize LCD before use
	
	beep(2); 	//buzzer sound for twice
		
	// PTK40A come with an ADC input (RA0), 3 different source can be connected to ADC in
	// There are LM35 temperature sensor, Potentiometer and external sensor
	// This example will use external ADC to read information from SHARP ANALOG DISTANCE SENSOR
	// Connect GND of the sensor to GND of PTK40A (ADC external port)
	// Connect Vcc of sensor to 5V of PTK40A (ADC external port)
	// Connect Vo of senosr to IN of PTK40A (ADC external port)
	// Please refer to PTK40A schematic for the connection
	// Please move JP14 to EXT (under ADC)
	// This example is based on GP2Y0A21 (10 to 80 cm) 	
	adc_on();	//activate ADC module in PIC
		
	LCD_BACKLIGHT = 1;	//activate LCD Backlight
	
	lcd_putstr("Cytron PTK40A");	//display message on LCD
	lcd_2ndline();				//move cursor to 2nd line
	lcd_putstr("DISTANCE:");
	lcd_goto(0X4E);
	lcd_putstr("cm");	// display symbol of cm
	
	while(1) 	// create an infinite loop
	{

		//refer datasheet graph for the convertion calculation
		distance = ui_adc_read()>>2;	// read from Sharp distance sensor and convert to cm
		distance = 256 - distance;
		distance = ((distance*10)+100)/36; //calculation is calculater according to the data sheet graph
		lcd_goto(0x4B);		//move cursor to after DISTANCE: on LCD
		lcd_bcd(3,distance);	// display distance in cm	
		delay_ms(50);
	}	
	
		
	while(1) continue;	// infinite loop to prevent PIC from reset if there is no more program	
}
Example #8
0
/*******************************************************************************
* MAIN FUNCTION                                                                *
*******************************************************************************/
int main(void)
{
	unsigned char step = 0;	// declare a variable to store stepper step sequence
	unsigned int delay = 0;	// declare a variable for stepper delay, need to be 16-bit for > 255
	// ensure all the hardware port in zero initially
	PORTA = 0;
	PORTB = 0;
	PORTC = 0;
	PORTD = 0;
	PORTE = 0;

	// Initialize the I/O port direction, this must be configured according to circuit
	// please refer to PTK40A schematic for details
	// TRISX control pin direction, output pin must be configure as '0'
	// while input must be configure as '1'
	TRISA = 0b00010001;
	TRISB = 0b00001111;
	TRISC = 0b10010011;
	TRISD = 0;
	TRISE = 0;

	// Initialize ADC.
	adc_initialize();	//Ensure pin share with analog is being configured to digital correctly

	// PTK40A come with an ADC input (RA0), 3 different source can be connected to ADC in
	// There are LM35 temperature sensor, Potentiometer and external sensor
	// There is also a stepper motor
	// Please refer to PTK40A schematic for the connection
	// Turning the potentiometer clock wise will change the speed of stepper rotation
	// Please move JP10 to PWM, JP14 to POT, JP20&21 to STEPPER, JP23&24 to UNIPOLAR
	adc_on();	//activate ADC module in PIC
	step = 1;	//initial step is 1
	RC2 = 1;	//RC2 pin is the PWM pin, in stepper case, the PWM is always 1

	while(1) 	// create an infinite loop
	{
		if(step >= 5) step = 1;	// reset the step sequance to 1

		delay = ui_adc_read();
		if (delay > 10) // if delay is larger than 10, enter stepping
		{
			switch (step)
			{
				case 1:
				X = 1;			// step 1
				Y = 0;
				XN = 0;
				YN = 0;
				break;

				case 2:
				X = 0;			// step 2
				Y = 1;
				XN = 0;
				YN = 0;
				break;

				case 3:
				X = 0;			// step 3
				Y = 0;
				XN = 1;
				YN = 0;
				break;

				case 4:
				X = 0;			// step 4
				Y = 0;
				XN = 0;
				YN = 1;
				break;
			}//switch (step)
			delay_ms(delay);
			step ++;	// increase step
		}//if(delay > 10)
	}	// while (1)

	while(1) continue;	// infinite loop to prevent PIC from reset if there is no more program
}
Example #9
0
/*******************************************************************************
* MAIN FUNCTION                                                                *
*******************************************************************************/
int main(void)
{
	unsigned char angle = 0;	// declare a variable to store angle
	unsigned char i = 0,j = 0;
	unsigned char lx = 0, ly = 0, ry = 0;
	unsigned int rx = 0;
	// ensure all the hardware port in zero initially
	PORTA = 0;
	PORTB = 0;
	PORTC = 0;
	PORTD = 0;
	PORTE = 0;

	// Initialize the I/O port direction, this must be configured according to circuit
	// please refer to PTK40A schematic for details
	// TRISX control pin direction, output pin must be configure as '0'
	// while input must be configure as '1'
	TRISA = 0b00010001;
	TRISB = 0b00001111;
	TRISC = 0b10010011;
	TRISD = 0;
	TRISE = 0;

	// Initialize ADC.
	adc_initialize();	//Ensure pin share with analog is being configured to digital correctly

	pwm_initialize();

	uart_initialize();
		
	beep(2); 	//buzzer sound for twice
		
	// PTK40A come SKPS PORT TO USE SKPS
	// SKPS, control DC motor speed and RC servo motor
	// Please connect SKPS at the "Cytron Starter Kit" section
	// Select 9600 as the baud rate on SKPS
	// servo require the PCM at around 20ms period
	// Please move jumper JP9 to SERVO, JP10 to PWM, JP20 & 21 to DC MOTOR
	
	M1 = 1;
	M2 = 0;		// drive motor in a direction
	while(1) 	// create an infinite loop
	{
		lx = uc_skps(p_joy_lx);	//obtain left joystick x axis value
		ly = uc_skps(p_joy_ly); //obtain left joystick y axis value
		rx = uc_skps(p_joy_rx); //obtain right joystick x axis value
		ry = uc_skps(p_joy_ry); //obtain right joystick y axis value
		
		//control dc motor using joyleft
		if(lx==128 && ly==128)
		{
			pwm_set_duty_cycle(0);		
		}	
		//if(lx!=128 && ly!=128)
		else if(ly==0)
		{
			pwm_set_duty_cycle(1000);		//set dc motor speed in differect coordinate
		}
		else if(lx==0)
		{
			pwm_set_duty_cycle(350);
		}
		else if(ly==255)
		{
			pwm_set_duty_cycle(500);
		}
		else if(lx==255)
		{
			pwm_set_duty_cycle(750);
		}
		
		//control servo motor using joyright
		if(ry==0)
		{
		SERVO = 1;		// Servo pin HIGH
		delay_10us(100);
		SERVO = 0;	// Servo pin LOW
		delay_ms(18);	// delay for around 18ms
		}
		else if(rx==0)
		{
		SERVO = 1;		// Servo pin HIGH
		delay_10us(130);
		SERVO = 0;	// Servo pin LOW
		delay_ms(18);	// delay for around 18ms
		}
		else if(ry==255)
		{
		SERVO = 1;		// Servo pin HIGH
		delay_10us(160);
		SERVO = 0;	// Servo pin LOW
		delay_ms(18);	// delay for around 18ms
		}
		else if(rx==255)
		{
		SERVO = 1;		// Servo pin HIGH
		delay_10us(200);
		SERVO = 0;	// Servo pin LOW
		delay_ms(18);	// delay for around 18ms
		}			
	}		
	while(1) continue;	// infinite loop to prevent PIC from reset if there is no more program	
}