//main function
void main(void)
{
    unsigned char st1[]="STARTING";
    unsigned char st2[]=".";

    //pins for drive LCD
    TRISB = 0x80;

    //Initialize modules
    Init_LCD();
    i2c_config();

    set_ds1307_time(AM,0,0,6);
    set_ds1307_day(8,11,8,13);

    lcd_putstr(st1);

    for(i=0;i<7;i++)
    {
        lcd_putstr(st2);
        Delay1KTCYx(200);
    }
    LCD_CMD(LCD_clear);
    
    while(1)
    {
        lcd_gotoxy(1,1);//hang 1, cot 1.
        Display_time(get_ds1307_time());

        lcd_gotoxy(1,2);//hang 2, cot 1
        Display_day(get_ds1307_day());

        Delay10KTCYx(70);
    };
}
//===============================================================================
//	Main Program
//===============================================================================
void main(void)
{
	//clear data port
	PORTA = 0;
	PORTB = 0;
	PORTC = 0;
	
	// Initialize the I/O port direction.
	TRISA = 0b00000000;
	TRISB = 0b00000001;

	//setup ADC
	ADCON1 = 0b00000110;		//All AN pin become digital pin
	
	LED=0;	//LED is not activated initially

	// LCD in 4-bit mode
	//-----------------------------------
	lcd_init();				//Initialize LCD

	lcd_set_cursor(1,0);	//Set cursor to column 1, line 1
	lcd_putstr("Cytron");	//Display Cytron
	lcd_goto(0x40);		//Set cursor to 2nd line using another function
	lcd_putstr("SK28A :)");	//Display SK28A
	
	while(1)
	{	
		LED ^= 1;		// toggle LED on SK28A
	    	delay_ms(50);	//delay for around 50ms	
			
		while(SW1==0)LED = 0;	//Halt and Off LED when SW1 is press, continue the program when SW1 is released or not pressed
	}

	while(1);	//infinate loop to prevent the program to reset at the end of main
}
Example #3
0
/*******************************************************************************
* PRIVATE FUNCTION: test_uart1a
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Test the UART1 Alternative.
*
*******************************************************************************/
void test_uart1a(void)
{	
	unsigned char i = 0;
	while (SW1==1) // Loop until SW1 is press
	{
	lcd_clear();
	lcd_putstr("SW1 test UART1A\nConnect UC00A");	
	for (i = 0; i < 50; i++)
		{
		if(SW1 == 0) break;			
		delay_ms(30);		
		}
	
	lcd_clear();
	lcd_putstr("TX Selector=TX1A\nRX Selector=RX1A");	
	for (i = 0; i < 50; i++) 
		{	
		if(SW1 == 0) break;			
		delay_ms(30);		
		}
	lcd_clear();
	lcd_putstr("HyperTerminal\n9600, 8, N, 1");	
	for (i = 0; i < 50; i++) 
		{	
		if(SW1 == 0) break;			
		delay_ms(30);		
		}
	}//while (SW1 == 1)	
		
	while(SW1==0); //wait for SW1 to be release
	
	uart1a_initialize();	// initialize UART1A
	lcd_clear();
	lcd_putstr("Press enter on\nkeyboard to exit");	
	
	// Sending message to the PC through UART1
	uart1_transmit(0x0C);	//clear HyperTerminal
	uart1_putstr("\r\n\nThanks for using Cytron Technologies product\r\n");
	uart1_putstr("\r\n\nDemo UART1 Alternative on SKds40A...\r\n");
	uart1_putstr("Press any key on keyboard to demo.\r\n");
	uart1_putstr("It will be echo back to HyperTerminal.\r\n");
	uart1_putstr("Press enter to exit.\r\n\n");
	
	do {
		c_received_data = uc_uart1_receive();	//echo what is received
		LED1 = ~LED1;		// toggle LED 1
		uart1_transmit(c_received_data);
	}
	while (c_received_data != '\r' && c_received_data != '\n');	
	
	// Sending message to the PC.
	uart1_putstr("\r\n\nUART1 Alternative Demo Completed.\r\n");
	LED1 = 0;	
	beep(2);	
	lcd_clear();
	lcd_putstr("UART1 Alternative\nPass!");	
	delay_ms(500);	
}
Example #4
0
/*******************************************************************************
* PRIVATE FUNCTION: test_uart1a
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Test the UART1 Alternative.
*
*******************************************************************************/
void test_uart1a(void)
{	
	unsigned char i = 0;
	while (SW1==1) // Loop until SW1 is press
	{
	lcd_clear();
	lcd_putstr("SW1 test UART1A\nConnect UC00A");	
	for (i = 0; i < 50; i++)
		{
		if(SW1 == 0) break;			
		delay_ms(30);		
		}
	
	lcd_clear();
	lcd_putstr("TX Selector=TX1A\nRX Selector=RX1A");	
	for (i = 0; i < 50; i++) 
		{	
		if(SW1 == 0) break;			
		delay_ms(30);		
		}
	lcd_clear();
	lcd_putstr(arr_hyperterminal);	
	for (i = 0; i < 50; i++) 
		{	
		if(SW1 == 0) break;			
		delay_ms(30);		
		}
	}//while (SW1 == 1)	
		
	while(SW1==0); //wait for SW1 to be release
	
	uart1a_initialize();	// initialize UART1A
	lcd_clear();
	lcd_putstr(arr_enter);	
	
	// Sending message to the PC through UART1
	uart1_transmit(0x0C);	//clear HyperTerminal
	uart1_putstr(arr_thanks);
	uart1_putstr("\r\n\nDemo UART1 Alternative on SKds40A...\r\n");
	uart1_putstr(arr_keyboard);
	uart1_putstr(arr_echo);
	uart1_putstr(arr_enter_keyboard);
	
	do {
		c_received_data = uc_uart1_receive();	//echo what is received
		LED1 = ~LED1;		// toggle LED 1
		uart1_transmit(c_received_data);
	}
	while (c_received_data != '\r' && c_received_data != '\n');	
	
	// Sending message to the PC.
	uart1_putstr("\r\n\nUART1 Alternative Demo Completed.\r\n");
	LED1 = 0;	
	beep(2);	
	lcd_clear();
	lcd_putstr("UART1 Alternative\nPass!");	
	delay_ms(500);	
}
Example #5
0
int
main ()
{
  DDRD = 0x00;
  PORTD = 0xff;

  lcd_init ();

  /*display on, cursor & blink off */
  lcd_control (1, 0, 0);
  timer1_init ();
  lcd_cls ();
  lcd_clear_CRGAM ();
  //az elso sorba irunk
  lcd_putstr ("Vertical", LCD_FIRST_LN_ADDR);
  
/* main -loop */
  for (;;)
    {
      if (cnt1 == CNT1_THRESHOLD)
	{
	  cnt1 = 0;
	  cnt3_flag = 1;
	  //clear second line
	  for (ch = 0; ch < 20; ch++)
	    {
	      lcd_set_DDRAM_addr (LCD_SECND_LN_ADDR + ch);
	      lcd_putch (' ');
	    }
	  /* 
	   * array2d: custom chars array
	   * ROWS: number of chars
	   * 30: vertical delay in ms
	   * 0: horiz. delay
	   * LCD_FIRST_LN_ADDR + 9 : ddram addr.
	   * 1: set CGRAM clear mode; 0: no CGRAM clear 
	   */
	   
	  //nem clear modban erdekes hatast kelt ;-)
	  lcd_vertical_scroll (array2d, ROWS, 40, 0 /* delay ms */ ,
			       LCD_FIRST_LN_ADDR + 9, 0);
	}

      if (cnt3 == CNT3_THRESHOLD)
	{
	  cnt3 = 0;
	  cnt3_flag = 0;
	  
	  lcd_putstr ("Is COOL!", LCD_SECND_LN_ADDR + 5);
	}
    }
  return 0;
}
Example #6
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
}
void DBUF_Handler(void) {
    // Handle display communication updates
    I2CM_Handler();
    // Only continue if the display isn't idle or in error
    if (!(I2CM_state == I2CM_IDLE) && !(I2CM_state == I2CM_ERROR_NAK)) {
        return;
    }
    // Handle buffer to display
    switch (LCDM_State) {
        case LCD_GOTO_LINE1_ORIGIN:
            lcd_goto(0, 0);
            LCDM_State++; // Go to LCD_UPDATE_LINE_1;
            break;

        case LCD_UPDATE_LINE_1:
            // Popup or normal display on line 1
            if (DisplayPopupState & 0x01) { // Popup
                lcd_putstr(&DisplayPopupBuffer[0], 16);
            } else { //Normal
                lcd_putstr(&DisplayBuffer[0], 16);
            }
            LCDM_State++; // Go to LCD_GOTO_LINE2_ORIGIN;
            break;

        case LCD_GOTO_LINE2_ORIGIN:
            lcd_goto(0, 1);
            LCDM_State++; // Go to LCD_UPDATE_LINE_2;
            break;

        case LCD_UPDATE_LINE_2:
            // Same as above for line 2
            if (DisplayPopupState & 0x02) {
                lcd_putstr(&DisplayPopupBuffer[16], 16);
            } else {
                lcd_putstr(&DisplayBuffer[16], 16);
            }
            LCDM_State++; // Go to LCD_BUSY;
            break;

        case LCD_BUSY: // Wait for the update of line 2 to complete
            LCDM_State++; // Go to LCD_DONE;
            break;

        case LCD_DONE: // Idle/Done  - Use this state to wait for LCD to complete operation
            LCDM_State = LCD_GOTO_LINE1_ORIGIN;
            break;
    }
}
Example #8
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 #9
0
void main (void){


    initIO();
    pwm_init();
    motor(0,0);
    lcd_init();
    LSA08_Init();
    __delay_ms(100);

    lcd_clr();
    lcd_goto(0);
    lcd_putstr("LSA08");
    lcd_goto(20);
    lcd_putstr("Demo");

    
    while(SW1);
    while(SW1==0);
    lcd_clr();
   
    PID_MENU();

    lcd_clr();
    lcd_goto(0);   
    lcd_putstr("line\nfollow");

    if (junction_mode){

          junction_follow();
    }

    else {
    
        while(1){
           line_follow();
        }
    }





}
Example #10
0
/*******************************************************************************
* PRIVATE FUNCTION: test_switch
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Test the push buttons.
*
*******************************************************************************/
void test_switch(void)
{
	// Display message
	lcd_clear();
	lcd_putstr("Test Switches");
	delay_ms(1000);		
	
	lcd_1stline_msg("Press SW1");
	
	// Waiting for user to press SW1
	while (SW1 == 1);
	
	// If SW1 is pressed but other switches also become low, trap the error.
	if (SW2 == 0) {			
	lcd_clear();
	lcd_putstr("SW2 detected, Error!");
	while (1);		// trap
	}		
	// Waiting for user to release SW1.
	while (SW1 == 0);	
	beep(1);
	
	// Display message
	lcd_clear();
	lcd_putstr("Press SW2");
	
	// Waiting for user to press SW2.
	while (SW2 == 1);
	
	// If SW2 is pressed but other switches also become low, trap the error.
	if (SW1 == 0) 
	{
	lcd_clear();
	lcd_putstr("SW1 detected, Error!");
	while (1);		// trap
	}	
	
	// Waiting for user to release SW2.
	while (SW2 == 0);	
	beep(2);	
	lcd_clear();
	lcd_putstr("Switches Pass!");	
	delay_ms(500);	
}	
void Display_time(unsigned char *pdata)
{
    unsigned char a[] = " AM";
    unsigned char b[] = " PM";
    unsigned char ch1[] = ":";
    unsigned char c[] = "H";

    LCD_IN_INTER(pdata[2]);//gio
    lcd_putstr(ch1);

    LCD_IN_INTER(pdata[1]);//phut
    lcd_putstr(ch1);

    LCD_IN_INTER(pdata[0]);//giay

    switch(udata[3])
    {
        case AM : lcd_putstr(a);break;
        case PM : lcd_putstr(b);break;
        default : lcd_putstr(c); break;
    }
}
Example #12
0
//=======giu so thuc len lcd=============================
void LCD_IN_FLOAT( float d)
{
char ch[]=".";
unsigned int e, i,j,k,l;
// d=round(d,1);
e=(int)(d*10);
l=e/1000;//nghin
k=(e%1000)/100;//tram
j=((e%1000)%100)/10;//chuc
i=((e%1000)%100)%10;//donvi
LCD_DATA(0x30+l);
LCD_DATA(0x30+k);
LCD_DATA(0x30+j);
lcd_putstr(ch);
LCD_DATA(0x30+i);
}
Example #13
0
void junction_follow(void){
    signed char lineposition=0;
    unsigned char i=0;
    unsigned char status=0;
    
    lcd_clr();
    lcd_goto(0);
    lcd_putstr("Junction\nCount");

    __delay_ms(500);
    lcd_clr(); 

    while(1){

        do{
            status=LSA08_ClearJunction();
        }while(status==0);
       
        jfolo(2);

        RotateRight();
        motor(180,180);
        __delay_ms(500);
         
        do{
            lineposition=LSA08_GetPosition();
            LED1=1;
        }while(lineposition>20);       
        Brake();
        LED1=0;
        __delay_ms(100);

        RotateRight();
        motor(180,180);
        __delay_ms(500);
        
        do{
            LED1=1;
            lineposition=LSA08_GetPosition();
        }while(lineposition>20);        
        Brake();
        LED1=0;
        __delay_ms(100);
        

    }
}
Example #14
0
/*-----------------31.5.99 18:18--------------------
 *  Display unsigned long 0-4294967296 (32bit)
 *--------------------------------------------------*/
void print10(unsigned long x)
{
  unsigned int y;
  if (x<4294967297ul)
  {
    y=x/1000000000;lcd_putch(y+0x30);x-=(y*1000000000);
    y=x/100000000;lcd_putch(y+0x30);x-=(y*100000000);
    y=x/10000000;lcd_putch(y+0x30);x-=(y*10000000);
    y=x/1000000;lcd_putch(y+0x30);x-=(y*1000000);
    y=x/100000;lcd_putch(y+0x30);x-=(y*100000);
    y=x/10000;lcd_putch(y+0x30);x-=(y*10000);
    y=x/1000;lcd_putch(y+0x30);x-=(y*1000);
    y=x/100;lcd_putch(y+0x30);x-=(y*100);
    y=x/10;lcd_putch(y+0x30);x-=(y*10);
    lcd_putch(x+0x30);
  }
  else lcd_putstr("Err");
}
Example #15
0
/*******************************************************************************
* PRIVATE FUNCTION: test_led
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Test the LEDs.
*
*******************************************************************************/
void test_led(void)
{
	unsigned char i= 0;
	// Display message
	lcd_clear();
	lcd_putstr("Test LEDs");
	delay_ms(1000);		
	
	lcd_1stline_msg("LED1 ON");	
	LED1 = 1;				// activate LED1
	delay_ms(1000);			// delay for a while
	lcd_1stline_msg("LED1 OFF");		
	LED1 = 0;				// deactivate LED1
	delay_ms(1000);			// delay for a while
	
	lcd_1stline_msg("LED2 ON");	
	LED2 = 1;				// activate LED1
	delay_ms(1000);			// delay for a while
	lcd_1stline_msg("LED2 OFF");	
	LED2 = 0;				// deactivate LED1
	delay_ms(1000);			// delay for a while
	
	lcd_1stline_msg("LED1 BLINK");
	for(i=0; i < 20; i++)	// Blink LED1
	{
	LED1 = ~LED1;
	delay_ms(70);	
	}
	LED1 = 0;
	delay_ms(500);
	
	lcd_1stline_msg("LED2 BLINK");
	for(i=0; i < 20; i++)	// Blink LED2
	{
	LED2 = ~LED2;
	delay_ms(70);	
	}
	LED2 = 0;
	delay_ms(500);
	
	beep(2);	
	lcd_1stline_msg("LEDs Pass!");	
	delay_ms(500);	
}	
Example #16
0
/*******************************************************************************
* PRIVATE FUNCTION: lcd_2ndline_msg
*
* PARAMETERS:
* ~ csz_string	- The null terminated string to display on 2nd line
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* clear LCD on 2nd line (lower row) and display message from 1st column of 2nd row
*
*******************************************************************************/
void lcd_2ndline_msg(const char* csz_string)
{
	unsigned char i = 0;
	lcd_2ndline();
	lcd_putstr(csz_string);
	
	while (*csz_string != '\0') {		
	i++;		
	csz_string++;
	}
	
	if(i < 16)
	{
		for(; i<16;i++)
		{
		lcd_putchar(' ');	
		}
	}	
}
Example #17
0
void adc_task(void) {
    uint8_t update_time = 0;
    adc_init();
    while (1) {
        char light_str[6], temp_str[6], time_str[6];
        uint8_t light = adc_acquire(0);
        uint8_t temperature = adc_acquire(1);
        uint8_t_to_ascii(light, &(light_str[0]));
        uint8_t_to_ascii(temperature, &(temp_str[0]));
        uint8_t_to_ascii(ticks, &(time_str[0]));
        os_semaphore_wait(&lcd_sem);
        lcd_set_cursor(0, 0);
        lcd_putstr("Time: ");
        lcd_putstr(time_str);
        lcd_set_cursor(1, 0);
        lcd_putstr("Light: ");
        lcd_putstr(light_str);
        lcd_set_cursor(2, 0);
        lcd_putstr("Temp: ");
        lcd_putstr(temp_str);
        os_semaphore_signal(&lcd_sem);
        
        os_semaphore_wait(&stt_sem);
        if (state) {
            update_time = 1;
        } else {
            update_time = 0;
        }
        os_semaphore_signal(&stt_sem);
        os_semaphore_wait(&tck_sem);
        if (update_time) {
            ticks++;
        }
        os_semaphore_signal(&tck_sem);
        
        if (state == 1) {
            usart_puts("Time: ");
            usart_puts(time_str);
            usart_puts("\r\n");
            usart_puts("Light: ");
            usart_puts(light_str);
            usart_puts("\r\n");
            usart_puts("Temperature: ");
            usart_puts(temp_str);
            usart_puts("\r\n");
        }
        
        os_delay(os_get_current_pid(), 1000);
    }
}
Example #18
0
unsigned char lcd_num(unsigned short number,unsigned char digit){

    unsigned char num[16];
    unsigned char k=digit-1;
    unsigned char i,j;

    num[digit]=0;

    for(i=0;i<digit;i++){
        num[k-i]=number%10+'0';
        number=number/10;
        if(number==0) break;
    }

    lcd_putstr(&num[k-i]);

    for(j=0;j<k-i;j++)
        lcd_putchar(' ');

    return(i);

}
Example #19
0
int
main ()
{
  DDRD = 0x00;
  PORTD = 0xff;

  lcd_init ();
  /*display on, cursor & blink off */
  lcd_control (1, 0, 0);
  lcd_cls ();
  lcd_putstr ("Animate is cool !", LCD_FIRST_LN_ADDR + 2);

  timer1_init ();

/* main -loop */
  for (;;)
    {
      // 2*0.065 Sec-kent vegrehajtodik az 1. es 3. animacio egy reszlete

      if (cnt1 == CNT1_THRESHOLD)
	{
	  cnt1 = 0;
	  //1. animacio 
	  if (i > 3)
	    {
	      i = 0;
	    }
	  uint8_t buf[8];

	  for (n = 0; n < 8; n++)
	    {
	      buf[n] = pgm_read_byte_near (&anim[i][n]);
	    }
	    i++;
	  lcd_put_custom_char (0x00, buf);	//CGRAM 0x00 cimre beirjuk a sajat karaktert.
	  lcd_set_DDRAM_addr (LCD_FIRST_LN_ADDR);	//vissza DDRAMhoz, elso sor 1 karakterehez
	  lcd_putch (0x00);	//megjelenitjuk a CGRAM 0x00 cimen levo karaktert.

	  //2. animacio
	  lcd_set_DDRAM_addr (LCD_FIRST_LN_ADDR + 19);	//masodik sor 8. karakterehez
	  lcd_putch (0x00);	//megjelenitjuk a CGRAM 0x00 cimen levo karaktert.
	}

      // 7 * 0.065 Sec(OVF) -kent vegrehajtodik a 3. animacio egy reszlete

      if (cnt2 == CNT2_THRESHOLD)
	{
	  cnt2 = 0;
	  //3. animacio
	  if (j > 1)
	    {
	      j = 0;
	    }
	  uint8_t buf[8];

	  //read 8byte(one char) from FLASH
	  for (n = 0; n < 8; n++)
	    {
	      buf[n] = pgm_read_byte_near (&man[j][n]);
	    }
	    j++;
	  lcd_put_custom_char (0x02, buf);
	  lcd_set_DDRAM_addr (LCD_SECND_LN_ADDR);
	  lcd_putch (0x02);
	}
    }
  return 0;
}
Example #20
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 #21
0
void button_task(void) {
    uint8_t start = 0, stop = 0, pause = 0;
    os_semaphore_wait(&btn_sem);
    // Port A with no pull ups for buttons, input
    DDRA &= ~(0b01110000);
    os_semaphore_signal(&btn_sem);
    os_semaphore_wait(&lcd_sem);
    lcd_set_cursor(4, 0);
    lcd_putstr("Stopped");
    os_semaphore_signal(&lcd_sem);
    while (1) {
        os_semaphore_wait(&btn_sem);
        if (!(PINA & 0b00010000)) {
            start = 1;
        } else {
            start = 0;
        }
        if (!(PINA & 0b00100000)) {
            pause = 1;
        } else {
            pause = 0;
        }
        if (!(PINA & 0b01000000)) {
            stop = 1;
        } else {
            stop = 0;
        }
        os_semaphore_signal(&btn_sem);
        
        if (state == 1 && pause == 1) {
            os_semaphore_wait(&btn_sem);
            pause = 0;
            os_semaphore_signal(&btn_sem);
            os_semaphore_wait(&stt_sem);
            state = 2;
            os_semaphore_signal(&stt_sem);
            os_semaphore_wait(&lcd_sem);
            lcd_set_cursor(4, 0);
            lcd_putstr("Paused ");
            os_semaphore_signal(&lcd_sem);
        }
        
        if (start) {
            os_semaphore_wait(&btn_sem);
            start = 0;
            os_semaphore_signal(&btn_sem);
            os_semaphore_wait(&stt_sem);
            state = 1;
            os_semaphore_signal(&stt_sem);
            os_semaphore_wait(&lcd_sem);
            lcd_set_cursor(4, 0);
            lcd_putstr("Running");
            os_semaphore_signal(&lcd_sem);
        }
        
        if (stop) {
            os_semaphore_wait(&btn_sem);
            stop = 0;
            os_semaphore_signal(&btn_sem);
            os_semaphore_wait(&stt_sem);
            state = 0;
            os_semaphore_signal(&stt_sem);
            os_semaphore_wait(&lcd_sem);
            lcd_set_cursor(4, 0);
            lcd_putstr("Stopped");
            os_semaphore_signal(&lcd_sem);
            os_semaphore_wait(&tck_sem);
            ticks = 0;
            os_semaphore_signal(&tck_sem);
        }
        
        os_delay(os_get_current_pid(), 50);
    }
}
void Display_day(unsigned char *pdata)
{
    unsigned char ch1[]=":";
    unsigned char hai[]="MON";
    unsigned char ba[]="TUE";
    unsigned char bon[]="WED";
    unsigned char nam[]="THUS";
    unsigned char sau[]="FRI";
    unsigned char bay[]="SAT";
    unsigned char cn[]="SUN";
    unsigned char er[]="ERROR";

    switch(pdata[0])
    {
        case 2: lcd_putstr(hai);break;
        case 3: lcd_putstr(ba);break;
        case 4: lcd_putstr(bon);break;
        case 5: lcd_putstr(nam);break;
        case 6: lcd_putstr(sau);break;
        case 7: lcd_putstr(bay);break;
        case 8: lcd_putstr(cn);break;
        default :lcd_putstr(er); break;
    }
    lcd_putstr(ch1);

    LCD_IN_INTER(pdata[1]);//ngay
    lcd_putstr(ch1);

    LCD_IN_INTER(pdata[2]);//thang
    lcd_putstr(ch1);

    LCD_IN_INTER(20);//nam
    LCD_IN_INTER(pdata[3]);
}
Example #23
0
void PID_MENU(void){
    unsigned char mode=0;
    unsigned long count=0;
    unsigned char kval;

    while(1){

        if(SW1==0){
            while(SW1==0);  //wait for switch release
            mode++;
            if (mode>2) mode=0;

        }
        else if(SW2==0 ){
            while(SW2==0);  //wait for switch release

            switch (mode){
                case 0:   return;                   //start
                case 1:   junction_mode^=1;  break; //mode change line follow/ junction count
                case 2:   Calibrate();       break; //calibrate LSA08
            }
        }
        //LCD display
        lcd_goto(0);
        switch (mode){

            case 0: lcd_putstr("Start\n");
                    lcd_putstr("PressSW2");
                    break;
            case 1: lcd_putstr("Mode \n");
                    if(junction_mode) lcd_putstr("Junction");
                    else            lcd_putstr("Line Flw");
                    break;
            case 2: lcd_putstr("Calb \n");
                    lcd_putstr("PressSW2");
                    break;
        }

    }
        
//         out:
//        if(SW1==0){
//            count=0;
//            while(SW1==0){
//                count++;
//
//                if(count>60000){
//                    mode++;
//                    if(mode>5) mode=0;
//                    lcd_clr();
//
//                    while(SW1==0);
//                    goto out;
//                   }
//            }
//
//            switch (mode){
//            case 0: Kp-=0.1;
//                    if(Kp<0) Kp=0;
//                    break;
//            case 1: Ki-=0.001;
//                    if(Ki<0) Ki=0;
//                    break;
//            case 2: Kd-=1.0;
//                    if(Kd<0.0) Kd=0.0;
//                    break;
//
//            }
//
//        }
//
//
//        if(SW2==0){
//
//            while(SW2==0);
//
//            switch (mode){
//            case 0: Kp+=0.1;
//                    if(Kp>9) Kp=9;
//                    break;
//            case 1: Ki+=0.001;
//                    if(Ki>5) Ki=5;
//                    break;
//            case 2: Kd+=1.0;
//                    if(Kd>50.0) Kd=50.0;
//                    break;
//            case 3: return;
//            case 5: Calibrate(); break;
//            case 4: junction_mode^=1;  break;
//
//            }
//        }
//
//
//        lcd_goto(0);
//        switch(mode){
//
//            case 0: lcd_putstr("Kp  \n");
//                    kval=(unsigned char)Kp;
//                    lcd_num(kval,1); lcd_putchar('.');
//                    lcd_num((Kp-(float)kval)*10,1);
//                    lcd_putchar(' ');
//                    break;
//            case 1: lcd_putstr("Ki  \n");lcd_goto(20);
//                    kval=(unsigned char)Ki;
//                    lcd_num(kval,1); lcd_putchar('.');
//                    if((Ki-(float)kval)*1000<100) lcd_putchar('0');
//                    if((Ki-(float)kval)*1000<10) lcd_putchar('0');
//                    lcd_num((Ki-(float)kval)*1000,3);
//                    lcd_putchar(' ');
//                    break;
//            case 2:lcd_putstr("Kd  \n");lcd_goto(20);
//                   kval=(unsigned char)Kd;
//                   lcd_num(kval,3);
//                   lcd_putchar(' ');
//                    break;
//            case 3: lcd_putstr("start\n");
//                    lcd_putstr("     ");
//                    break;
//            case 5: lcd_putstr("calb\n");
//                    lcd_putstr("      ");
//                    break;
//            case 4: lcd_putstr("mode\n");
//                    if(junction_mode) lcd_putstr("junction");
//                    else            lcd_putstr("line_flw");
//                    break;
//        }
//
//    }



}
/*******************************************************************************
* MAIN FUNCTION                                                                *
*******************************************************************************/
int main(void)
{
	//unsigned int data[22] = {0};
	unsigned int data_in = 0;
	unsigned char data_out = 0;
	unsigned char data_chk = 0;
	unsigned int data_ver = 0;
	unsigned char i = 0;
	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 = 0b00110001;
	TRISB = 0b00001111;
	TRISC = 0b10011011;
	TRISD = 0;
	TRISE = 0;

	//Initialize LCD to be use
	lcd_initialize();
	
	lcd_goto(0x82);		
	lcd_putstr("Cytron Tech");
	lcd_goto(0xc4);
	lcd_putstr("PS/2");
	delay_ms(1000);
	lcd_clear();

	while(1) 	// create an infinite loop
	{
		while(RC3==1);
		{	
			for(i=0;i<=10;i++)
			{
				while(RC3==1);
				while(RC3==0);
				data_in = (data_in | RC4) << 1;
			}
			data_in >>=1;  				//shift back the data once to become 8 bits
			data_out=data_in>>2 ;		//shift out the parity bit and put to char to cancel up the first bits
			data_chk=conv(data_out);	//turn the data to become normal data
			lcd_goto(0x00);
			lcd_bcd(5,data_chk);  //convert and display the original data value
			data_ver=(data_in & 0x0002)>>1;
			chk(data_chk,data_ver);
			
			
			
		}
		AtoZ(data_chk);
		delay_ms(500);
		
		
	}	
		while(1) continue;	// infinite loop to prevent PIC from reset if there is no more program	
}
Example #25
0
/*******************************************************************************
* MAIN FUNCTION                                                                *
*******************************************************************************/
int main(void)
{	
	unsigned char test_no = 0;
	// Clear all ports.
	LATA = 0;
	LATB = 0;
	LATC = 0;
	LATD = 0;
	LATF = 0;
	
	// Initialize I/O directions.
	TRISA = 0;					// dsPIC30F4013 & 3014 have PORTA
	TRISAbits.TRISA11 = 1;		// SW1 is connected at RA11
	TRISB = 0;
	TRISC = 0;
	TRISD = 0;
	TRISDbits.TRISD8 = 1;		// SW2 is connected at RD8	
	TRISF = 0;
	
	// Wait 1ms before we start the loop.
	beep(2);
	delay_ms(200);	
	lcd_initialize();	// initialize LCD
	
	lcd_clear();
	lcd_putstr("     Cytron\n  Technologies");	//Buzzer and LED share same output pin	
	delay_ms(1500);
	
	lcd_clear();
	lcd_putstr("    SKds40A\n  dsPIC30F4013");	
	delay_ms(1500);	
	LED1 = 0;
	LED2 = 0;
		
	test_switch();	//need to ensure switches is working before other test
	
	test_no = 1;
// Testing LEDs, UARTs (1, 2 and Alternative)
	while(1){
		lcd_2ndline_msg("1-> +,  2->Test");	//display the message for test menu navigation
				
		switch (test_no) //start the switch case to check which test mode to execute
			{
			case 1:	
				lcd_1stline_msg("1. Test All");	//mode 1 is to run all test sequentially			
				if (SW2 == 0) 			// if SW2 is press, to select the mode and run
				{	while (SW2 == 0);	// waiting for SW3 to be let go
					test_led();			// Test LEDs					
					test_uart1();		// Test UART1
					test_uart2();		// Test UART2
					test_uart1a();		// Test UART1 Alternative
				}	
				break;
				
			case 2:		// mode 2, test LEDs on SKds40A												
				lcd_1stline_msg("2: Test LEDs");	
				if (SW2 == 0) 
				{
					while (SW2 == 0);
					test_led();			// Test LEDs
				}	
				break;
				
			case 3:		// mode 3, test UART1, ensure jumper at Tx and RX Selector is connected to TX1 and RX1 respectively
				lcd_1stline_msg("3: Test UART1");
				if (SW2 == 0) 
				{
					while (SW2 == 0);
					test_uart1();		// Test UART1
				}	
				break;
				
			case 4:		// mode 4, test UART2, ensure jumper at Tx and RX Selector is connected to TX2 and RX2 respectively
				lcd_1stline_msg("4: Test UART2");
				if (SW2 == 0) 
				{
					while (SW2 == 0);
					test_uart2();		// Test UART2
				}	
				break;	
				
			case 5:		// mode 5, test UART1A, ensure jumper at Tx and RX Selector is connected to TX1A and RX1A respectively
				lcd_1stline_msg("5: Test UART1A");
				if (SW2 == 0) 
				{
					while (SW2 == 0);
					test_uart1a();		// Test UART1A
				}	
				break;			
		}//switch (test_no)	
			
		
		// If SW1 is pressed...		
		if (SW1 == 0) 
		{
			if (++test_no > 5) // if SW1 is press increase the test number until it is max and loop back
			{
				test_no = 1;
			}				
			while (SW1 == 0);	//wait for SW1 to be released
			beep(1);
		}		
	} // while (1)
}
Example #26
0
void main() {
    InitializeSystem();


    red = FALSE;
    green = FALSE;

    #if defined(USB_INTERRUPT)
        USBDeviceAttach();
    #endif

    while (1){
         #if defined(USB_POLLING)
		// Check bus status and service USB interrupts.
        USBDeviceTasks(); // Interrupt or polling method.  If using polling, must call
        				  // this function periodically.  This function will take care
        				  // of processing and responding to SETUP transactions
        				  // (such as during the enumeration process when you first
        				  // plug in).  USB hosts require that USB devices should accept
        				  // and process SETUP packets in a timely fashion.  Therefore,
        				  // when using polling, this function should be called
        				  // regularly (such as once every 1.8ms or faster** [see
        				  // inline code comments in usb_device.c for explanation when
        				  // "or faster" applies])  In most cases, the USBDeviceTasks()
        				  // function does not take very long to execute (ex: <100
        				  // instruction cycles) before it returns.
        #endif
        Tran_String_UART(init);
        lcd_gotoxy(1,2);
        lcd_putstr(init);
        if((USBGetDeviceState() \< CONFIGURED_STATE) ||(USBIsDeviceSuspended() == TRUE))
            {
            #if defined USE_UART
               Tran_String_UART(error);

            #endif
            #if defined USE_LCD
               lcd_clear();
               lcd_gotoxy(3,1);
               lcd_putstr(error);
            #endif
                //Either the device is not configured or we are suspended
                //  so we don't want to do execute any application code
                continue;   //go back to the top of the while loop
            }
            else

            #if defined USE_UART
                Tran_String_UART(success);

            #endif
            #if defined USE_LCD
                lcd_clear();
                lcd_gotoxy(3,1);
                lcd_putstr(success);
            #endif
//            if(!HIDRxHandleBusy(USBOutHandle))
//            {
//                if (outbuffer[0] == 0) {
//                    if(!HIDTxHandleBusy(USBInHandle))
//                    {
//                        inbuffer[0] = green;
//                        inbuffer[1] = red;
//                        USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&inbuffer[0], HID_IN_SIZE);
//                    }
//                }
//                if (outbuffer[0] == 1) {
//                    if (outbuffer[1] == 1) green = TRUE; else green = FALSE;
//                    if (outbuffer[2] == 1) red = TRUE; else red = FALSE;
//                    if (green) LATCbits.LATC6 = 1; else LATCbits.LATC6 = 0;
//                    if (red) LATCbits.LATC7 = 1; else LATCbits.LATC7 = 0;
//                }
//                if(outbuffer[0]==2){
//                    LATCbits.LATC6=0;
//                    LATCbits.L4ATC7=0;
//                }
//                USBOutHandle = HIDRxPacket(HID_EP, (BYTE*)&outbuffer, HID_OUT_SIZE);
//            } 
    }
}