Exemple #1
0
void InicializaHardware(void)
{
	S12ADC_init();
	S12ADC_start();
    lcd_initialize();
    lcd_clear();
}
/******************************************************************************
* Function name: main
* Description  : Main program function
* Arguments    : none
* Return value : none
******************************************************************************/
void main(void)
{
    /* Used to pace toggling of the LED */
    uint32_t led_counter;
    
    /* Initialize LCD */
    YRDKRX62N_RSPI_Init();
    lcd_initialize();
    
    /* Clear LCD */
    lcd_clear();
    
    /* Display message on LCD */
    lcd_display(LCD_LINE1, "  RENESAS   ");
    lcd_display(LCD_LINE2, " YRDKRX62N  ");

    /* 
        STDOUT is routed through the virtual console window tunneled through the JTAG debugger.
        Open the console window in HEW to see the output 
    */
    printf("This is the debug console\r\n");
    
    /* The three pushbuttons on the YRDK board are tied to interrupt lines, set them up here */
    switches_initialize();
    
    /* This is the main loop.  It does nothing but toggle LED4 periodically */
    while (1)
    {
        for (led_counter = 0; led_counter < 1000000; led_counter++)
        {
        }
        LED4 = ~LED4;
    }
}
void main(){
	//Init LCD
	__C30_UART=1;	
	lcd_initialize();
	lcd_clear();
        touch_init();

        CLEARBIT(T1CONbits.TON); // Disable Timer
        CLEARBIT(T1CONbits.TCS); // Select internal instruction cycle clock
        CLEARBIT(T1CONbits.TGATE); // Disable Gated Timer mode
        TMR1 = 0x00; // Clear timer register
        T1CONbits.TCKPS = 0b10; // Select 1:64 Prescaler
        PR1 = 2000; // Load the period value
        IPC0bits.T1IP = 0x01; // Set Timer1 Interrupt Priority Level
        CLEARBIT(IFS0bits.T1IF); // Clear Timer1 Interrupt Flag
        SETBIT(IEC0bits.T1IE); // Enable Timer1 interrupt



        while(1) {
            int i = 0;

            
            
            touch_select_dim(0);
            lock = 1;
            TMR1 = 0x00;
            SETBIT(T1CONbits.TON); // Start Timer
            while(lock);
            
            for (i = 0; i < 5; i++) {
                xs[i] = touch_adc();
            }

            // read y
            touch_select_dim(1);
            lock = 1;
            TMR1 = 0x00;
            SETBIT(T1CONbits.TON); // Start Timer
            while(lock);
            for (i = 0; i < 5; i++) {
                ys[i] = touch_adc();
            }

            

            qsort(xs, 5, sizeof(uint16_t), cmpfunc);
            qsort(ys, 5, sizeof(uint16_t), cmpfunc);

            lcd_locate(0,0);
            lcd_printf("x position:             ");
            lcd_locate(0,0);
            lcd_printf("x position: %d", xs[2]);
            lcd_locate(0,1);
            lcd_printf("y position:             ");
            lcd_locate(0,1);
            lcd_printf("y position: %d", ys[2]);
        }
}
Exemple #4
0
/*
 * description:	initialize the device module
*/
static int __init klcd_init(void)
{
    struct device *dev_ret;

    // dynamically allocate device major number
    if( alloc_chrdev_region( &dev_number, MINOR_NUM_START , MINOR_NUM_COUNT , DEVICE_NAME ) < 0)
    {
        printk( KERN_DEBUG "ERR: Failed to allocate major number \n" );
        return -1;
    }

    // create a class structure
    klcd_class = class_create( THIS_MODULE, CLASS_NAME );

    if( IS_ERR(klcd_class) )
    {
        unregister_chrdev_region( dev_number, MINOR_NUM_COUNT );
        printk( KERN_DEBUG "ERR: Failed to create class structure \n" );

        return PTR_ERR( klcd_class ) ;
    }

    // create a device and registers it with sysfs
    dev_ret = device_create( klcd_class, NULL, dev_number, NULL, DEVICE_NAME );

    if( IS_ERR(dev_ret) )
    {
        class_destroy( klcd_class );
        unregister_chrdev_region( dev_number, MINOR_NUM_COUNT );
        printk( KERN_DEBUG "ERR: Failed to create device structure \n" );

        return PTR_ERR( dev_ret );
    }

    // initialize a cdev structure
    cdev_init( &klcd_cdev, &klcd_fops);

    // add a character device to the system
    if( cdev_add( &klcd_cdev, dev_number, MINOR_NUM_COUNT) < 0 )
    {
        device_destroy( klcd_class, dev_number);
        class_destroy(  klcd_class );
        unregister_chrdev_region( dev_number, MINOR_NUM_COUNT );
        printk( KERN_DEBUG "ERR: Failed to add cdev \n" );

        return -1;
    }

    // setup GPIO pins
    lcd_pin_setup_All();

    // initialize LCD once
    lcd_initialize();

    printk(KERN_INFO "klcd Driver Initialized. \n");
    return 0;
}
Exemple #5
0
int up_lcdinitialize(void)
{
  gvdbg("Initializing\n");

  lcd_initialize();

  /* Clear the display  */
  lcd_clear();

  return OK;
}
Exemple #6
0
int board_lcd_initialize(void)
{
  ginfo("Initializing\n");

  lcd_initialize();

  /* Clear the display  */
  lcd_clear();

  return OK;
}
void main(void)
{
    char buffer[LCD_CHARACTERS];

    char item;

    unsigned char degree[8] = {
        0b01100,
    	0b10010,
    	0b10010,
    	0b01100,
    	0b00000,
    	0b00000,
    	0b00000,
    	0b00000
    };

    unsigned char arrow[8] = {
        0b00000,
    	0b01000,
    	0b01100,
    	0b01110,
    	0b01100,
    	0b01000,
    	0b00000,
    	0b00000
    };

    // set RE2 as digital I/O
    ADCON1 = 0x08;

    // set ports as outputs
    TRISC = 0x00;
    DATA_DDR = 0x00;
    // tactile switch
    TRISE = 0x04;

    lcd_initialize();

    lcd_add_character(1, degree);
    lcd_add_character(2, arrow);

    item = menu();
    sprintf(buffer, "Selected item: %c", item);
    lcd_write(buffer);

    /*while (1) {
        while (PORTEbits.RE2 == 1);
        sprintf(message, "TC1:%d\001C TC2:%d\001C", position++, position2++);
        lcd_goto(1, 1);
        lcd_write(message);
    }*/
}
void main(void)
{
    char buffer[LCD_CHARACTERS];

    char item;

    unsigned char degree[8] = {
        0b01100,
    	0b10010,
    	0b10010,
    	0b01100,
    	0b00000,
    	0b00000,
    	0b00000,
    	0b00000
    };

    unsigned char arrow[8] = {
        0b00000,
    	0b01000,
    	0b01100,
    	0b01110,
    	0b01100,
    	0b01000,
    	0b00000,
    	0b00000
    };

    // set RE2 as digital I/O
    ADCON1 = 0x08;

    // set ports as outputs
    TRISC = 0x00;
    LCD_DATA_DDR = 0x00;
    // tactile switch
    TRISE = 0x04;

    lcd_initialize();

    lcd_add_character(1, degree);
    lcd_add_character(2, arrow);

    while (1) {
        item = menu();
        sprintf(buffer, "Selected item: %d", item);
        lcd_goto(2, 1);
        lcd_write(buffer);

        while (PORTEbits.RE2 == 1);
        Delay10KTCYx(255);
    }
}
/*******************************************************************************
* 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
}
Exemple #10
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	
}
Exemple #11
0
void main(){
	//Init LCD
	__C30_UART=1;
        int count = 0;
        int DBcount = 0;
        int Tcount = 0;
        int DBNOcount = 0;
        int LED4Status = 0;
        int PrevStat = 0;
     
	lcd_initialize();
	led_initialize();

	lcd_clear();
	lcd_locate(0,0);
	
	while(1);
}
Exemple #12
0
void main( void )
{
	lcd_initialize();
	lcd_display( LCD_LINE1, "FreeRTOS" );

	/* The configCREATE_LOW_POWER_DEMO setting is described in FreeRTOSConfig.h. */
	#if configCREATE_LOW_POWER_DEMO == 1
	{
		lcd_display( LCD_LINE2, "LP Demo" );
		main_low_power();
	}
	#else
	{
		lcd_display( LCD_LINE2, "Ful Demo" );
		main_full();
	}
	#endif
}
Exemple #13
0
/*******************************************************************************
* Function name:    main
* Description  : 	main function for Can API demo program. Initializes the LCD,
*                   flashes some LEDs, then calls the Can API demo program.
* Arguments    :    none
* Return value : 	none
*******************************************************************************/
void main(void)
{
    /* Setup LCD. */
    lcd_initialize();

    /* Display splash screen. */
   // lcd_display(LCD_LINE1, "Lab 2 ");
    //lcd_display(LCD_LINE4, "Sean");
    //lcd_display(LCD_LINE5, "Tanmay");
    //lcd_display(LCD_LINE4, "CAN Demo");

    /* Blink board LEDs. */
    blink_leds(20);

    switches_initialize(); 

    /* Run the CAN API Demo. */
    can_api_demo();
    
} /* End function main(). */
Exemple #14
0
void main( void )
{
	/* Call the Renesas provided setup. */
	vHardwareSetup();
	lcd_initialize();
	lcd_display( LCD_LINE1, "FreeRTOS" );

	/* The configCREATE_LOW_POWER_DEMO setting is described in FreeRTOSConfig.h. */
	#if configCREATE_LOW_POWER_DEMO == 1
	{
		lcd_display( LCD_LINE2, "LP Demo" );
		main_low_power();
	}
	#else
	{
		lcd_display( LCD_LINE2, "Ful Demo" );
		main_full();
	}
	#endif
}
int main (void)
{
    /* INITIALIZE */
//	LCD_DDR |=(1<<LCD_RSDS_PIN);
//	LCD_DDR |=(1<<LCD_ENABLE_PIN);
//	LCD_DDR |=(1<<LCD_CLOCK_PIN);



    slaveinit();

    lcd_initialize(LCD_FUNCTION_8x2, LCD_CMD_ENTRY_INC, LCD_CMD_ON);
    lcd_puts("Guten Tag\0");
    delay_ms(1000);

    //lcd_cls();
    //lcd_puts("READY\0");
    delay_ms(1000);
    // DS1820 init-stuff begin

    uart_init();

    InitSPI_Slave();

    uint8_t linecounter=0;

    uint8_t SPI_Call_count0=0;
    lcd_cls();
    lcd_puts("UART\0");

#pragma mark while
    // OSZIA_HI;
    //char teststring[] = {"p,10,12"};


    initADC(TASTATURKANAL);

//  vga_start();

    setHomeCentral();
    vga_command("f,1");
    startcounter = 0;
    linecounter=0;
    uint8_t lastrand=rand();
    //srand(1);
    sei();
    uint8_t incounter=0;
    //uint8_t x=0;
    while (1)
    {
        loopCount0 ++;

        if (loopCount0 >=0x00FF)
        {
            //readSR();
            loopCount1++;

            if ((loopCount1 >0x01FF) )//&& (!(Programmstatus & (1<<MANUELL))))
            {
                LOOPLED_PORT ^= (1<<LOOPLED_PIN);
                if ((uartstatus & (1<< SUCCESS_BIT))&& (SPI_CONTROL_PORTPIN & (1<<SPI_CONTROL_CS_HC)))
                {
                    incounter++;
                    lcd_gotoxy(0,0);
                    lcd_puts("OK\0");
                    //lcd_puthex(in_startdaten);
                    lcd_putc(' ');
                    lcd_putint(incounter);
                    cli();
                    delay_ms(100);
                    //vga_command("f,2");
                    //vga_putch('*');
                    //putint(254);


                    uartstatus &= ~(1<< SUCCESS_BIT);


                    sei();

                }
                else
                {
                    //lcd_gotoxy(0,0);
                    //lcd_puts("  \0");

                }
                {
                    //lcd_gotoxy(15,3);
                    //lcd_putint(BitCounter);
                    /*
                    loopCount2++;
                    vga_command("f,2");
                    //puts("HomeCentral ");
                    vga_puts("Tastenwert ");
                    putint(Tastenwert);
                    vga_putch(' ');
                    newline();
                     */
                    /*
                    if (loopCount2 > 2)
                    {
                      newline();
                      linecounter++;
                      if (linecounter>50)
                      {
                         linecounter=0;
                         vga_command("e");
                      }
                      if (linecounter %3==0)
                      {
                         //linecounter =0;
                         linecounter+=1;
                         gotoxy(8,linecounter);
                         vga_command("f,2");
                         vga_puts("Stop");
                         putint_right(linecounter);
                         vga_putch(' ');
                         putint(Tastenwert);
                         //newline();
                         vga_command("f,1");
                         vga_command("e");
                         vga_puts("Daten");
                         putint_right(linecounter);
                         vga_command("f,2");
                         //vga_command("e");

                         gotoxy(0,linecounter);
                         vga_command("f,2");
                         newline();
                      }
                      loopCount2=0;
                    }
                    */
                    loopCount1=0;

                } // if startcounter

            }

            loopCount0 =0;
            //

            goto NEXT;
            {
                cli();

                {
                    Tastenwert=(uint8_t)(readKanal(TASTATURKANAL)>>2);

                    //lcd_gotoxy(12,1);
                    //lcd_puts("TW:\0");
                    //lcd_putint(Tastenwert);
                    if (Tastenwert>5) // ca Minimalwert der Matrix
                    {
                        tastaturstatus |= (1<<1);
                        //			wdt_reset();
                        /*
                         0: Wochenplaninit
                         1: IOW 8* 2 Bytes auf Bus laden
                         2: Menu der aktuellen Ebene nach oben
                         3: IOW 2 Bytes vom Bus in Reg laden
                         4: Auf aktueller Ebene nach rechts (Heizung: Vortag lesen und anzeigen)
                         5: Ebene tiefer
                         6: Auf aktueller Ebene nach links (Heizung: Folgetag lesen und anzeigen)
                         7:
                         8: Menu der aktuellen Ebene nach unten
                         9: DCF77 lesen

                         12: Ebene höher
                         */
                        TastaturCount++;
                        if (tastaturstatus & (1<<1))
                        {

                        }

                        if (TastaturCount>=80)	//	Prellen
                        {
                            tastaturstatus &= ~(1<<1);
                            Taste=Tastenwahl(Tastenwert);
                            lcd_gotoxy(0,1);
                            lcd_puts("T:\0");
                            //
                            lcd_putc(' ');
                            lcd_putint(Tastenwert);
                            lcd_putc(' ');
                            if (Taste >=0)
                            {
                                lcd_putint2(Taste);
                            }
                            else
                            {
                                lcd_putc('*');
                            }

                            //lcd_gotoxy(14,1);


                            //lcd_putint(linecounter);
                            //lcd_putc(' ');
                            //lcd_putint((uint8_t)rand()%40);

                            switch (Taste)
                            {
                            case 1:
                            {
                                vga_command("f,2");
                                gotoxy(0,2);
                                vga_command("f,2");
                                vga_puts("Brenner: ");
                            }
                            break;
                            case 2:
                            {
                                vga_command("f,3");
                                gotoxy(cursorx,cursory);
                                vga_command("f,3");
                                vga_putch(' ');

                                cursory--;
                                gotoxy(cursorx,cursory);
                                vga_command("f,3");
                                vga_putch('>');

                            }
                            break;
                            case 3:
                            {

                            } break;
                            case 4:
                            {
                                if (menuebene)
                                {
                                    menuebene--;
                                    if (cursorx>10)
                                    {
                                        vga_command("f,3");
                                        gotoxy(cursorx,cursory);
                                        vga_command("f,3");
                                        vga_putch(' ');
                                        cursorx-=10;
                                        gotoxy(cursorx,cursory);
                                        vga_command("f,3");
                                        vga_putch('>');
                                    }
                                }

                            }
                            break;
                            case 5:
                            {
                                vga_command("f,3");
                                gotoxy(cursorx,cursory);
                                vga_command("f,3");
                                vga_putch('>');

                            }
                            break;
                            case 6:
                            {
                                if (menuebene<MAXMENUEBENE)
                                {
                                    menuebene++;
                                    if (cursorx<40)
                                    {
                                        vga_command("f,3");
                                        gotoxy(cursorx,cursory);
                                        vga_command("f,3");
                                        vga_putch(' ');

                                        cursorx+=10;
                                        gotoxy(cursorx,cursory);
                                        vga_command("f,3");
                                        vga_putch('>');
                                    }
                                }
                            }
                            break;
                            case 7:
                            {

                                setFeld(3,70,3,30,32,1,"");
                                vga_command("f,3");
                                //vga_putch('x');
                                //lcd_putc('+');

                            }
                            break;
                            case 8:
                            {
                                vga_command("f,3");
                                gotoxy(cursorx,cursory);
                                vga_command("f,3");
                                vga_putch(' ');

                                cursory++;
                                gotoxy(cursorx,cursory);
                                vga_command("f,3");
                                vga_putch('>');

                            }
                            break;
                            case 9:
                            {

                            } break;
                            case 10:
                            {
                                setHomeCentral();
                            }
                            break;
                            case 12:
                            {
                                vga_command("f,3");
                                vga_command("e");
                                gotoxy(cursorx,cursory);
                                vga_command("f,3");
                                vga_putch(' ');
                                cursorx = CURSORX;
                                cursory = CURSORY;
                                gotoxy(CURSORX+1,CURSORY);
                                vga_command("f,3");
                                vga_puts("Alpha");
                                gotoxy(CURSORX+1,CURSORY+1);
                                vga_command("f,3");
                                vga_puts("Beta");
                                gotoxy(CURSORX+1,CURSORY+2);
                                vga_command("f,3");
                                vga_puts("Gamma");
                                gotoxy(CURSORX+1,CURSORY+3);
                                vga_command("f,3");
                                vga_puts("Delta");
                                gotoxy(cursorx,cursory);
                                vga_command("f,3");
                                vga_putch('>');

                            }
                            break;


                            } // switch Taste



                            lastrand = rand();
                            vga_command("f,2");
                            //vga_putch(' ');

                            //gotoxy(4,linecounter);
                            //lcd_gotoxy(16,1);
                            //lcd_putint(erg);
                            /*
                            putint(linecounter);
                            vga_putch(' ');
                            putint_right(Tastenwert);
                            vga_putch(' ');
                            putint_right(Taste);
                            */
                            //newline();
                            linecounter++;
                            TastaturCount=0;

                        }

                    } // if Tastenwert

                }

            }
NEXT:
            //x=0;
            sei();
            //
        }

        /* *** SPI begin **************************************************************/

        //lcd_gotoxy(19,0);
        //lcd_putc('-');

        // ***********************
        //goto ENDSPI;
        if (SPI_CONTROL_PORTPIN & (1<< SPI_CONTROL_CS_HC)) // PIN ist Hi, SPI ist Passiv
        {
            // lcd_gotoxy(19,1);
            //  lcd_putc('*');

            // ***********************
            /*
             Eine Uebertragung hat stattgefunden.
             (Die out-Daten sind auf dem Webserver.)
             Die in-Daten vom Webserver sind geladen.
             Sie muessen noch je nach in_startdaten ausgewertet werden.
             */

            // ***********************
//			SPI_CONTROL_PORT |= (1<<SPI_CONTROL_MISO); // MISO ist HI in Pausen

#pragma mark PASSIVE

            if (spistatus &(1<<ACTIVE_BIT)) // Slave ist neu passiv geworden. Aufraeumen, Daten uebernehmen
            {

                wdt_reset();
                SPI_Call_count0++;
                // Eingang von Interrupt-Routine, Daten von Webserver
                //lcd_gotoxy(19,0);
                //lcd_putc(' ');

                // in lcd verschoben
                lcd_clr_line(2);
                lcd_gotoxy(0,2);

                // Eingang anzeigen
                lcd_puts("iW \0");
                lcd_puthex(in_startdaten);
                lcd_putc(' ');
                lcd_puthex(in_hbdaten);
                lcd_puthex(in_lbdaten);
                lcd_putc(' ');
                uint8_t j=0;
                for (j=0; j<2; j++)
                {
                    //lcd_putc(' ');
                    lcd_puthex(inbuffer[j]);
                    //lcd_putc(inbuffer[j]);
                }
                OutCounter++;

                // Uebertragung pruefen

                //lcd_gotoxy(6,0);
                //lcd_puts("bc:\0");
                //lcd_puthex(ByteCounter);

                //lcd_gotoxy(0,0);
                //lcd_puts("      \0");


                lcd_gotoxy(19,0);
                lcd_putc(' ');

                if (ByteCounter == SPI_BUFSIZE-1) // Uebertragung war vollstaendig
                {
                    if (out_startdaten + in_enddaten==0xFF)
                    {
                        lcd_gotoxy(19,2);
                        lcd_putc('+');
                        spistatus |= (1<<SUCCESS_BIT); // Bit fuer vollstaendige und korrekte  Uebertragung setzen
                        lcd_gotoxy(19,0);
                        lcd_putc(' ');
                        //lcd_clr_line(3);
                        //lcd_gotoxy(0,1);
                        //lcd_puthex(loopCounterSPI++);
                        //lcd_puts("OK \0");

                        //lcd_puthex(out_startdaten + in_enddaten);
                        //					if (out_startdaten==0xB1)
                        {
                            SendOKCounter++;
                        }
                        spistatus |= (1<<SPI_SHIFT_IN_OK_BIT);
                    }

                    else
                    {
                        spistatus &= ~(1<<SUCCESS_BIT); // Uebertragung fehlerhaft, Bit loeschen
                        lcd_putc('-');
                        lcd_clr_line(1);
                        lcd_gotoxy(0,1);
                        lcd_puts("ER1\0");
                        lcd_putc(' ');
                        lcd_puthex(out_startdaten);
                        lcd_puthex(in_enddaten);
                        lcd_putc(' ');
                        lcd_puthex(out_startdaten + in_enddaten);

                        spistatus &= ~(1<<SPI_SHIFT_IN_OK_BIT);
                        {
                            SendErrCounter++;
                        }
                        //errCounter++;
                    }

                }
                else
                {
                    spistatus &= ~(1<<SUCCESS_BIT); //  Uebertragung unvollstaendig, Bit loeschen
                    lcd_clr_line(0);
                    lcd_gotoxy(0,0);
                    lcd_puts("ER2\0");
                    lcd_putc(' ');
                    lcd_puthex(ByteCounter);
                    //lcd_putc(' ');
                    //lcd_puthex(BitCounter);

                    /*
                    	lcd_putc(' ');
                    lcd_puthex(out_startdaten);
                    lcd_puthex(in_enddaten);
                    lcd_putc(' ');
                    lcd_puthex(out_startdaten + in_enddaten);
                    */
                    //delay_ms(100);
                    //errCounter++;
                    IncompleteCounter++;
                    spistatus &= ~(1<<SPI_SHIFT_IN_OK_BIT);
                }

                //lcd_gotoxy(11, 1);							// Events zahelen
                //lcd_puthex(OutCounter);
                /*
                 lcd_puthex(SendOKCounter);
                 lcd_puthex(SendErrCounter);
                 lcd_puthex(IncompleteCounter);
                 */
                /*
                 lcd_gotoxy(0,0);
                 lcd_putc('i');
                 lcd_puthex(in_startdaten);
                 lcd_puthex(complement);
                 lcd_putc(' ');
                 lcd_putc('a');
                 lcd_puthex(out_startdaten);
                 lcd_puthex(in_enddaten);
                 lcd_putc(' ');
                 lcd_putc('l');
                 lcd_puthex(in_lbdaten);
                 lcd_putc(' ');
                 lcd_putc('h');
                 lcd_puthex(in_hbdaten);
                 out_hbdaten++;
                 out_lbdaten--;

                 lcd_putc(out_startdaten);
                 */
                /*
                 lcd_gotoxy(0,0);
                 lcd_puthex(inbuffer[9]);
                 lcd_puthex(inbuffer[10]);
                 lcd_puthex(inbuffer[11]);
                 lcd_puthex(inbuffer[12]);
                 lcd_puthex(inbuffer[13]);
                 */
                //lcd_gotoxy(13,0);								// SPI - Fehler zaehlen
                //lcd_puts("ERR    \0");
                //lcd_gotoxy(17,0);
                //lcd_puthex(errCounter);

                // Bits im Zusammenhang mit der Uebertragung zuruecksetzen. Wurden in ISR gesetzt
                spistatus &= ~(1<<ACTIVE_BIT);		// Bit 0 loeschen
                spistatus &= ~(1<<STARTDATEN_BIT);	// Bit 1 loeschen
                spistatus &= ~(1<<ENDDATEN_BIT);		// Bit 2 loeschen
                spistatus &= ~(1<<SUCCESS_BIT);		// Bit 3 loeschen
                spistatus &= ~(1<<LB_BIT);				// Bit 4 loeschen
                spistatus &= ~(1<<HB_BIT);				// Bit 5 loeschen

                // aufraeumen
                out_startdaten=0x00;
                out_hbdaten=0;
                out_lbdaten=0;
                for (int i=0; i<SPI_BUFSIZE; i++)
                {
                    outbuffer[i]=0;
                }

                /*
                lcd_gotoxy(0,0);				// Fehler zaehlen
                lcd_puts("IC   \0");
                lcd_gotoxy(2,0);
                lcd_puthex(IncompleteCounter);
                lcd_gotoxy(5,0);
                lcd_puts("TW   \0");
                lcd_gotoxy(7,0);
                lcd_puthex(TWI_errCounter);

                lcd_gotoxy(5,1);
                lcd_puts("SE   \0");
                lcd_gotoxy(7,1);
                lcd_puthex(SendErrCounter);
                */
            } // if Active-Bit

#pragma mark HomeCentral-Tasks

        } // neu Passiv

        // letzte Daten vom Webserver sind in inbuffer und in in_startdaten, in_lbdaten, in_hbdaten


        else						// (IS_CS_HC_ACTIVE)
        {
            if (!(spistatus & (1<<ACTIVE_BIT))) // CS ist neu aktiv geworden, Daten werden gesendet, Active-Bit 0 ist noch nicht gesetzt
            {
                // Aufnahme der Daten vom Webserver vorbereiten

                uint8_t j=0;
                in_startdaten=0;
                in_enddaten=0;
                in_lbdaten=0;
                in_hbdaten=0;
                for (j=0; j<SPI_BUFSIZE; j++)
                {
                    inbuffer[j]=0;
                }

                spistatus |=(1<<ACTIVE_BIT); // Bit 0 setzen: neue Datenserie
                spistatus |=(1<<STARTDATEN_BIT); // Bit 1 setzen: erster Wert ergibt StartDaten

                bitpos=0;
                ByteCounter=0;
                //timer0(); // Ueberwachung der Zeit zwischen zwei Bytes. ISR setzt bitpos und ByteCounter zurueck, loescht Bit 0 in spistatus

                // Anzeige, das  rxdata vorhanden ist
                lcd_gotoxy(19,0);
                lcd_putc('$');
                lcd_gotoxy(19,1);
                lcd_putc(' ');




            }//										if (!(spistatus & (1<<ACTIVE_BIT)))
        }//											(IS_CS_HC_ACTIVE)
ENDSPI:
        /* *** SPI end **************************************************************/

# pragma mark Tasten

        if (!(PINB & (1<<PORTB0))) // Taste 0
        {
            //lcd_gotoxy(12,0);
            //lcd_puts("P0 Down\0");
            //wdt_reset();
            if (! (TastenStatus & (1<<PORTB0))) //Taste 0 war nicht nicht gedrueckt
            {
                TastenStatus |= (1<<PORTB0);
                Tastencount=0;
                //lcd_gotoxy(12,0);
                //lcd_puts("P0\0");
                //lcd_putint(TastenStatus);
                //delay_ms(800);
            }
            else
            {

                Tastencount ++;
                //lcd_gotoxy(7,1);
                //lcd_puts("TC \0");
                //lcd_putint(Tastencount);
                wdt_reset();
                if (Tastencount >= Tastenprellen)
                {
                    //lcd_gotoxy(18,0);
                    //lcd_puts("ON\0");
                    //lcd_putint(TastenStatus);

                    Tastencount=0;
                    TastenStatus &= ~(1<<PORTB0);

                }
            }//else

        }


#pragma mark Tastatur

    } // while
/*******************************************************************************
* Function name: main
* Description  : Main program function. Initializes the peripherals used in the
*                demo and executes a loop that reads the ADC and updates the 
*                display once for each press of switch 1. 
* Arguments    : none
* Return value : none
*******************************************************************************/
void main(void)
{
    /* ADC reading */
    uint16_t adc_counts;
    
    uint32_t i; /* Common loop count variable. */
       
    /* Initialize LCD */
    lcd_initialize();
   
	
    /* Clear LCD */
    lcd_clear();
	int freq=0;
	int count=0;
	while(1){
 /*
	uint8_t char_60[] = {
    0x08, 0x08, // width=8, height=8
    0x00, 0x08, 0x1C, 0x36, 0x63, 0x41, 0x00, 0x00, 
};
	uint8_t char_61[] = {
    0x08, 0x08, // width=8, height=8
    0x00, 0x00, 0x00, 0x36, 0x63, 0x41, 0x00, 0x00, 
};*/
	//for(int i=0; i<6;i++)
	{
		uint8_t char_60[] = {
    0x08, 0x08, // width=8, height=8
    0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 
};
	}
	//change(char_60);
	/*
	uint8_t char_61[10] =GLYPH_FONT_8_BY_8[126];
	
	Font8x8_char_126[0] = char_60[0];
	Font8x8_char_126[1] = char_60[1];
	Font8x8_char_126[2] = char_60[2];
	Font8x8_char_126[3] = char_60[3];
	Font8x8_char_126[4] = char_60[4];
	Font8x8_char_126[5] = char_60[5];
	Font8x8_char_126[6] = char_60[6];
	Font8x8_char_126[7] = char_60[7];
	Font8x8_char_126[8] = char_60[8];
	Font8x8_char_126[9] = char_60[9];*/
	
	
    /* Display message on LCD */
	
   int temp=lcd_display2(LCD_LINE2, "~");
   if((freq/count)-10>=temp  &&(freq/count)+10<=temp);
   else{
   freq=freq+temp;
   count++;
   }
   char buf[20];
   sprintf((char *)buf, "FREQ=%5d",freq/count);
   
   lcd_display1(LCD_LINE7, buf);
   delayMS1(150);
   
   
    //lcd_display(LCD_LINE2, " YRDKRX63N  ");
    //lcd_display(LCD_LINE3, "   S12ADC   ");
    //lcd_display(LCD_LINE4, "  ONE SHOT  ");
    //lcd_display(LCD_LINE5, " ADJUST POT,");
    //lcd_display(LCD_LINE6, " PRESS SW1  ");
	

	



	

	
	}
    /* Setup the 12-bit A/D converter */
    //S12ADC_init();

    /* 
        STDOUT is routed through the virtual console window tunneled through the 
        JTAG debugger. Open the console window in HEW to see the output 
    */
    printf("This is the debug console\r\n");
    
    

} /* End function main() */
Exemple #17
0
void Setup() {
	/* Initialize LCD */
	lcd_initialize();

	/* Clear LCD */
	lcd_clear();

	/* Display message on LCD */

	lcd_buffer_print(LCD_LINE2, "    TEST   ");

	/* Initialize motors */
	Motors_Init();
	/* Turn on motors relay */
	Motors_On();
	/* Send arm signal to motors */
	Motor_Arm(MOTOR_UPPER);
	Motor_Arm(MOTOR_BOTTOM);

	/* Initialize servos */
	Servos_Init();

	/* Initialize sonar */
	sonarInitialize(); //must be initialized before IIC, otherwise it will not work
	/* Setup the 12-bit A/D converter */
	S12ADC_init();


	/* Initialize I2C with control */
	riic_ret_t iic_ret = RIIC_OK;
	iic_ret |= riic_master_init();
	while (RIIC_OK != iic_ret) {
		nop(); /* Failure to initialize here means demo can not proceed. */
	}


	/* Setup Compare Match Timer */
	CMT_init();

	/* Initialize PID structure used for PID properties */
	PID_Init(&z_axis_PID, 0.7, 0.05, 0.30, dt, 0, 0.5);	//0.7 0.05 0.15
	PID_Init(&Pitch_PID, 1, 0, 0.01, dt, -30, 30);
	PID_Init(&Roll_PID, 1, 0, 0.01, dt, -30, 30);

	Init_AVG(0, &pitchAVG);
	Init_AVG(0, &rollAVG);

	/* Make the port connected to SW1 an input */
	PORT4.PDR.BIT.B0 = 0;

	/*MPU6050 Initialization*/
	MPU6050_Test_I2C();
	Setup_MPU6050();
	Calibrate_Gyros();
//	Calibrate_Accel();

	/*Kalman Initialization*/
	init_Kalman();

	//MS5611-01BA01 init
//    MS5611_Init();

	desiredState.key.motor_diff_us = 0;
	desiredState.key.abs.pos.z = 0.20;
	altitudeValue = 200;
	mainWDT = WDT_Init(500, Fallback);
	WDT_Start(&mainWDT);
	sonarWDT = WDT_Init(60, Sonar_Fallback);
	WDT_Start(&sonarWDT);
}
/*******************************************************************************
* 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	
}
int main(){
  uint8_t start_r, old_IPL;
  uint8_t hz50_scaler, hz5_scaler, hz1_scaler, sec;
  uint32_t tick = 0;

  hz50_scaler = hz5_scaler = hz1_scaler = sec = 0;

  touch_init();

  __delay_ms(200);
  lcd_initialize();             // Initialize the LCD 

  motor_init();

  lcd_clear();
  lcd_locate(0,0);
  lcd_printf("-- Ball position: --");

  timers_initialize();          // Initialize timers

  while (1) {
    start_r = 0;
    while(!start_r) {      
      // disable all maskable interrupts
      SET_AND_SAVE_CPU_IPL(old_IPL, 7);
      start_r = start;

      // enable all maskable interrupts
      RESTORE_CPU_IPL(old_IPL);
    }

    // Periodic real-time task code starts here = CENTER_X + RADIUS * cos(tick * SPEED);
//      Ypos_set = CENT!!!
    double pidX, pidY;
    uint16_t duty_us_x, duty_us_y;

    // 50Hz control task
    if(hz50_scaler == 0) {
      calcQEI(Xpos_set, Xpos, Ypos_set, Ypos);
//      Xpos_set = CENTER_X;
//      Xpos_set = CENTER_Y;
      Xpos_set = CENTER_X + RADIUS * cos(tick * SPEED);
      Ypos_set = CENTER_Y + RADIUS * sin(tick * SPEED);
      tick++;


      pidX = pidX_controller(Xpos);
      pidY = pidY_controller(Ypos);

      // TODO: Convert PID to motor duty cycle (900-2100 us)

      // setMotorDuty is a wrapper function that calls your motor_set_duty
      // implementation in flexmotor.c. The 2nd parameter expects a value
      // between 900-2100 us
//      duty_us_x = cap((pidX*1000.0), 2100, 900);
//      duty_us_y = cap((pidY*1000.0), 2100, 900);

      duty_us_x = cap((pidX + 1500), 2100, 900);
      duty_us_y = cap((pidY + 1500), 2100, 900);
      motor_set_duty(1, duty_us_x);
      motor_set_duty(2, duty_us_y+100);
//      setMotorDuty(MOTOR_X_CHAN, duty_us_x);
//      setMotorDuty(MOTOR_Y_CHAN, duty_us_y);
    }

    // 5Hz display task
    if(hz5_scaler == 0) {
//      lcd_locate(0,1);
//      lcd_printf("Xp=%.1f,Yp=%.1f", Xpos, Ypos);
//      lcd_locate(0,2);
//      lcd_printf("X*=%.1f, Y*=%.1f", Xpos_set, Ypos_set);
//      lcd_locate(0,3);
//      lcd_printf("pX=%.1f,pY=%.1f", pidX, pidY);
//      lcd_locate(0,4);
//      lcd_printf("dx=%u, dY=%u", duty_us_x, duty_us_y);
//
      if(deadline_miss >= 1) {
        lcd_locate(0,6);
        lcd_printf("%4d d_misses!!!", deadline_miss);
      }
    }

    // 1Hz seconds display task
    if(hz1_scaler == 0) {
      lcd_locate(0,7);
      lcd_printf("QEI: %5u", getQEI());
      sec++;
    }
    
    hz50_scaler = (hz50_scaler + 1) % 2;
    hz5_scaler = (hz5_scaler + 1) % 20;
    hz1_scaler = (hz1_scaler + 1) % 100;

    start = 0;
  }

  return 0;
}
Exemple #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	
}
void main(void)
{    
	//uint16_t adc_count;
	float adc_count;
    /* Initialize LCD */
    lcd_initialize();
	S12ADC_init();
    //UART initialization
	sci_uart_init();
	sci_tx_int_enable();
	sci_rx_int_enable();
	
	
    /* Clear LCD */
    lcd_clear();
    
    /* Display message on LCD */
    //Message will be on Position 1 and it will be stopped
    lcd_display(LCD_LINE1, " Mitch and  ");
	lcd_display(LCD_LINE2, "    Paul    ");
	lcd_display(LCD_LINE3, "Train Track ");
	lcd_display(LCD_LINE4, "  STOPPED   ");
	lcd_display(LCD_LINE5, "       _oo_");
	lcd_display(LCD_LINE6, " Pos1 |[][]|");
	lcd_display(LCD_LINE7, "      |O  O|");
	lcd_display(LCD_LINE8, "      |____|");

    /* 
        STDOUT is routed through the virtual console window tunneled through the JTAG debugger.
        Open the console window in HEW to see the output 
    */
    printf("This is the debug console\r\n");
    
    /* The three pushbuttons on the YRDK board are tied to interrupt lines, set them up here */
    R_SWITCHES_Init();
    
    //Intitalize all global variables
	count=0;
    Forward=0;
	Backword=0;
	Enable=0;
	
	//Character to input from putty
	char new_char;
	
	//Initialize Local variables
	int toggle=0;
	
	//Initialize all LED's starting at position 1
	LED4=LED_ON;
	LED5=LED_ON;
	LED6=LED_ON;
	LED7=LED_ON;
	LED8=LED_OFF;
	LED9=LED_OFF;
	LED10=LED_OFF;
	LED11=LED_OFF;
	LED12=LED_OFF;
	LED13=LED_OFF;
	LED14=LED_OFF;
	LED15=LED_OFF;
	
	while (1)
    {
		S12ADC_start();
		while(false==S12ADC_conversion_complete())
		{}
		new_char= sci_get_char();
		
		//Through the serial communication
		if(new_char =='F' ||new_char=='f')	//If inputted 'F' or 'f' move Forward
		{
			lcd_display(LCD_LINE4, "  FORWARD ");
			Forward=1;
			Enable=1;
			Backword=0;
			sci_put_string("Remote: Forward\n\r");
		}
		if(new_char =='S' ||new_char=='s')	//If inputted 'S' or 's' stop movement
		{
			lcd_display(LCD_LINE4, "  STOPPED   ");
			//Forward=0;
			Enable=0;
			//Backward=0;
			sci_put_string("Remote: Stop\n\r");

		}
		if(new_char =='R' ||new_char=='r')	//If inputted 'R' or 'r' move in reverse
		{
			lcd_display(LCD_LINE4, "  REVERSE  ");
			Forward=0;
			Enable=1;
			Backword=1;
			sci_put_string("Remote: Reverse\n\r");
		}
		
		
		
		adc_count= S12ADC_read();
		
		adc_count=adc_count/4095;
		int adccount=adc_count*100;
		char result[20];
		sprintf(result, "sp%3d%|____|", adccount);
		lcd_display(LCD_LINE8, (const uint8_t *) result);
	//	lcd_display(LCD_LINE8, adc_count);
		if(new_char=='D'||new_char=='d')	//If inputted 'D' or 'd' diplay the current speed
		{
			char speed[20];
			sprintf(speed,"Current Speed: %3d \n\r",adccount);
			sci_put_string(speed);
		}
		
		if(adc_count<=.01 & Enable==1)
		{
			lcd_display(LCD_LINE4, "  STOPPED   ");
			//Forward=0;
			Enable=0;
			//Backward=0;
			sci_put_string("Remote: Stop\n\r");
			while(adc_count<=0.05)
			{
				inloop=1;
				S12ADC_start();
				while(false==S12ADC_conversion_complete())
				{}
				adc_count= S12ADC_read();
				adc_count=adc_count/4095;
				
			}
			if(LastDir==1)
			{
				lcd_display(LCD_LINE4, "  FORWARD ");
				Forward=1;
				Enable=1;
				Backword=0;
				sci_put_string("Forward\n\r");
			}
			else if(LastDir==-1)
			{
				lcd_display(LCD_LINE4, "  REVERSE  ");
				Forward=0;
				Enable=1;
				Backword=1;
				sci_put_string("Reverse\n\r");
			}
			
			inloop=0;
		}
		else if(adc_count<=.25)
		{
			delayMS(1000);
		}
		else if(adc_count<=.5)
		{
			delayMS(500);
		}
		else if(adc_count<=.75)
		{
			delayMS(100);
		}		
		else if(adc_count<=1.0)
		{
			delayMS(10);
		}
		
		
		if(Enable==1)
		{
			trainMove();
			if(toggle==1 && Backword==1)
			{
				lcd_display(LCD_LINE5, "       _**_");
				lcd_display(LCD_LINE7, "      |*  *|");
				toggle=0;
			}
			else if(toggle==0 && Backword==1)
			{
				lcd_display(LCD_LINE5, "       _oo_");
				lcd_display(LCD_LINE7, "      |O  O|");
				toggle=1;
			}
			if(Forward==1)
			{
				lcd_display(LCD_LINE5, "       _oo_");
				lcd_display(LCD_LINE7, "      |*  *|");
			}
		}
		else
		{
			lcd_display(LCD_LINE5, "       _oo_");
			lcd_display(LCD_LINE7, "      |O  O|");
		}
		
		if(Forward==1 && Enable==1)	{count++; 	if(count==12){count=0;}}
		if(Backword==1 && Enable==1){count--;	if(count==-1){count=11;}}
    }
}
/*******************************************************************************
* 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)
}
int main (void)
{
	/* INITIALIZE */
//	LCD_DDR |=(1<<LCD_RSDS_PIN);
//	LCD_DDR |=(1<<LCD_ENABLE_PIN);
//	LCD_DDR |=(1<<LCD_CLOCK_PIN);
	
	slaveinit();
	
   SPI_Init();
   
	lcd_initialize(LCD_FUNCTION_8x2, LCD_CMD_ENTRY_INC, LCD_CMD_ON);
	lcd_puts("Guten Tag\0");
	delay_ms(1000);
   
	lcd_cls();
   lcd_gotoxy(0,0);
	lcd_puts("MASTER\0");
	
	// DS1820 init-stuff begin
	// DS1820 init-stuff end
   
   volatile char incoming=0;
   volatile uint8_t outcounter=0;
   
   sei();
#pragma mark while
	while (1) 
	{
		loopCount0 ++;
		//_delay_ms(2);
		//LOOPLED_PORT ^= (1<<LOOPLED_PIN);
      //incoming = SPDR;
		if (loopCount0 >=0x0F)
		{
			LOOPLED_PORT ^= (1<<LOOPLED_PIN);
			loopCount1++;
         //SPDR = loopCount2;
         
  			if ((loopCount1 >0x0002F) )//&& (!(Programmstatus & (1<<MANUELL))))
			{
            SPI_PORT &= ~(1<<SPI_CS); // SS LO, Start
            
            //_delay_us(100);
            uint8_t outindex=0;
            textpos=0;
            inbuffer[0] = '\0';
            //outbuffer[0] = '\0';
            
            text[0] = 0x30+(outcounter & 0x07);
            for (outindex=0;outindex < strlen(text);outindex++)
            {
               SPDR = text[outindex];
               while(!(SPSR & (1<<SPIF)) && spiwaitcounter<0xFFF)
               {
                  spiwaitcounter++;
               }
               spiwaitcounter=0;
               incoming = SPDR;
               inbuffer[outindex] = incoming;
               outbuffer[outindex] = text[outindex];
               
            }
            
            inbuffer[outindex] = '\0';
            //outbuffer[outindex] = '\0';
            //char rest = SPDR;
            SPI_PORT |= (1<<SPI_CS); // SS HI End

            
            lcd_gotoxy(8,0);
            lcd_putc('i');
            lcd_putc(' ');
            lcd_puts((char*)inbuffer);
            
            lcd_gotoxy(8,1);
            lcd_putc('o');
             lcd_putc(' ');
            lcd_puts((char*)outbuffer);
            
            outcounter++;
            if (outcounter > 9)
            {
               //outcounter =0;
            }
            
            /*
            if (textpos == 0)
            {
               
               SPI_PORT &= ~(1<<SPI_SS); // SS LO
            }
            
            SPDR = text[textpos];
            while(!(SPSR & (1<<SPIF)) && spiwaitcounter<0xFFF)
            {
               spiwaitcounter++;
            }
            incoming = SPDR;
            spiwaitcounter=0;
            
            textpos++;
            if (textpos >= strlen(text))
            {
               textpos=0;
               SPI_PORT |= (1<<SPI_SS); // SS HI
               //SPI_PORT &= ~(1<<SPI_SS); // SS LO
            }
            */
            loopCount2++;
            /*
            lcd_gotoxy(inpos,1);
            
            lcd_putc(incoming);
            inpos++;
            if (inpos >= 19)
            {
               inpos=0;
            }
            */
            
            
            //lcd_gotoxy(18,0);
            //lcd_puthex(loopCount2);
            //LOOPLED_PORT ^= (1<<LOOPLED_PIN);
				
            //LOOPLED_PORT ^= (1<<LOOPLED_PIN);
            loopCount1=0;
            /*
             if ((SPSR & (1<<SPIF)) > 0) // checks to see if the SPI bit is clear
             {
             
             data='a'; // send the data
             }
             */
            //char incoming = SPI_get_put_char('A');
            
            
            
            
				// DS1820 loop-stuff begin
            /*
             start_temp_meas();
             delay_ms(800);
             read_temp_meas();
             
             //Sensor 1
             lcd_gotoxy(0,1);
             lcd_puts("1:\0");
             if (gTempdata[0]/10>=100)
             {
             lcd_gotoxy(1,1);
             lcd_putint((gTempdata[0]/10));
             }
             else
             {
             lcd_gotoxy(2,1);
             lcd_putint2((gTempdata[0]/10));
             }
             
             lcd_putc('.');
             lcd_putint1(gTempdata[0]%10);
             
             // Sensor 2
             lcd_gotoxy(7,1);
             lcd_puts("2:\0");
             if (gTempdata[1]/10>=100)
             {
             lcd_gotoxy(8,1);
             lcd_putint((gTempdata[1]/10));
             }
             else
             {
             lcd_gotoxy(9,1);
             lcd_putint2((gTempdata[1]/10));
             }
             
             lcd_putc('.');
             lcd_putint1(gTempdata[1]%10);
             
             // Sensor 3
             lcd_gotoxy(14,1);
             lcd_puts("3:\0");
             if (gTempdata[2]/10>=100)
             {
             lcd_gotoxy(15,1);
             lcd_putint((gTempdata[2]/10));
             }
             else
             {
             lcd_gotoxy(16,1);
             lcd_putint2((gTempdata[2]/10));
             }
             
             lcd_putc('.');
             lcd_putint1(gTempdata[2]%10);
             
             
             
             lcd_gotoxy(15,0);
             lcd_puts("   \0");
             lcd_gotoxy(15,0);
             lcd_puthex(gTemp_measurementstatus);
             
             */
				
				// DS1820 loop-stuff end
				
				
				//lcd_putint(gTempdata[1]);
				//lcd_putint(gTempdata[2]);
				//delay_ms(1000);
			}
			
			loopCount0 =0;
		}
      
      
		if (!(PINB & (1<<PB0))) // Taste 0
		{
			lcd_gotoxy(10,1);
			lcd_puts("P0 Down\0");
         lcd_puthex(TastenStatus);
			
			if (! (TastenStatus & (1<<PB0))) //Taste 0 war nicht nicht gedrueckt
			{
            lcd_gotoxy(10,1);
            lcd_puts("P0 neu \0");
				TastenStatus |= (1<<PB0);
            delay_ms(1000);
				Tastencount=0;
				//lcd_gotoxy(3,1);
				//lcd_puts("P0 \0");
				//lcd_putint(Servoimpulsdauer);
				//delay_ms(800);
				SPDR = 'x';
            while(!(SPSR & (1<<SPIF)) && spiwaitcounter<0xFFF)
            {
               spiwaitcounter++;
            }
            spiwaitcounter=0;
            delay_ms(1000);
            //lcd_gotoxy(10,1);
				//lcd_puts("       \0");
            lcd_gotoxy(19,1);
            lcd_putc('+');
			}
			else
			{
				lcd_gotoxy(19,1);
            lcd_putc('$');
				//lcd_puts("*         *\0");
				
				Tastencount ++;
				if (Tastencount >= Tastenprellen)
				{
					Tastencount=0;
					TastenStatus &= ~(1<<PB0);
               lcd_gotoxy(10,1);
               lcd_puts("*         *\0");
               

               
				}
			}//	else
			
		} // Taste 0

		
#pragma mark Tastatur 
		/* ******************** */
		initADC(TASTATURPIN);
		Tastenwert=(readKanal(TASTATURPIN)>>2);
		
//		lcd_gotoxy(3,1);
//		lcd_putint(Tastenwert);
//		Tastenwert=0;
		if (Tastenwert>5)
		{
			/*
			 0:											1	2	3
			 1:											4	5	6
			 2:											7	8	9
			 3:											x	0	y
			 4: Schalterpos -
			 5: Manuell ein
			 6: Schalterpos +
			 7: 
			 8: 
			 9: 
			 
			 12: Manuell aus
			 */
			 
			TastaturCount++;
			if (TastaturCount>=200)
			{
				
				 
				 //lcd_gotoxy(17,1);
				 //lcd_puts("T:  \0");
				 //lcd_putint(Tastenwert);
				 
				uint8_t Taste=Tastenwahl(Tastenwert);
				//Taste=0;
				 //lcd_gotoxy(19,1);
				 //lcd_putint1(Taste);
				 //delay_ms(600);
				// lcd_clr_line(1);
				 

				TastaturCount=0;
				Tastenwert=0x00;
				uint8_t i=0;
				uint8_t pos=0;
//				lcd_gotoxy(18,1);
//				lcd_putint2(Taste);
				continue;
				switch (Taste)
				{
					case 0:// Schalter auf Null-Position
					{ 
						if (Programmstatus & (1<<MANUELL))
						{
							Manuellcounter=0;
							Programmstatus |= (1<<MANUELLNEU);
							/*
							lcd_gotoxy(13,0);
							lcd_puts("S\0");
							lcd_gotoxy(19,0);
							lcd_putint1(Schalterposition); // Schalterstellung
							lcd_gotoxy(0,1);
							lcd_puts("SI:\0");
							lcd_putint(ServoimpulsdauerSpeicher); // Servoimpulsdauer
							lcd_gotoxy(5,0);
							lcd_puts("SP\0");
							lcd_putint(Servoimpulsdauer); // Servoimpulsdauer
							*/
						}
						
					}break;
						
					case 1:	//	
					{ 
					if (Programmstatus & (1<<MANUELL))
						{
						uint8_t i=0;
						lcd_gotoxy(0,0);
						lcd_puts("Sens\0");
						lcd_putc('1');
						lcd_putc(' ');
						for (i=0;i<OW_ROMCODE_SIZE;i++)
						{
						lcd_puthex(gSensorIDs[0][i]);
						if (i==3)
						{
						lcd_gotoxy(0,1);
						}
						lcd_putc(' ');
						}
						Manuellcounter=0;
						
						}
					}break;
						
					case 2://
					{ 
					
						if (Programmstatus & (1<<MANUELL))
						{
						uint8_t i=0;
						lcd_gotoxy(0,0);
						lcd_puts("Sens\0");
						lcd_putc('1');
						lcd_putc(' ');
						for (i=0;i<OW_ROMCODE_SIZE;i++)
						{
						lcd_puthex(gSensorIDs[1][i]);
						if (i==3)
						{
						lcd_gotoxy(0,1);
						}
						lcd_putc(' ');
						}
						Manuellcounter=0;
						
						
						}
						
					}break;
						
					case 3: //	Uhr aus
					{ 
						if (Programmstatus & (1<<MANUELL))
						{
						uint8_t i=0;
						lcd_gotoxy(0,0);
						lcd_puts("Sens\0");
						lcd_putc('1');
						lcd_putc(' ');
						for (i=0;i<OW_ROMCODE_SIZE;i++)
						{
						lcd_puthex(gSensorIDs[2][i]);
						if (i==3)
						{
						lcd_gotoxy(0,1);
						}
						lcd_putc(' ');
						}
						Manuellcounter=0;
						

						}
					}break;
						
					case 4://
					{ 
						DS18X20_read_scratchpad(&gSensorIDs[0][0], gScratchPad );
												uint8_t i=0;
						lcd_gotoxy(0,0);
						lcd_puts("Sens\0");
						lcd_putc('0');
						lcd_putc(' ');
						for (i=0;i<OW_ROMCODE_SIZE;i++)
						{
						lcd_puthex(gScratchPad[i]);
						if (i==3)
						{
						lcd_gotoxy(0,1);
						}
						lcd_putc(' ');
						}

					}break;
						
					case 5://
					{ 
						Programmstatus |= (1<<MANUELL);	// MANUELL ON
						Manuellcounter=0;
						MANUELL_PORT |= (1<<MANUELLPIN);
						Programmstatus |= (1<<MANUELLNEU);
						lcd_clr_line(1);
						/*
							lcd_gotoxy(13,0);
							lcd_puts("S\0");
							lcd_putint1(Schalterposition); // Schalterstellung
							lcd_gotoxy(0,1);
							lcd_puts("SP:\0");
							lcd_putint(ServoimpulsdauerSpeicher); // Servoimpulsdauer
							lcd_gotoxy(5,0);
							lcd_puts("SI\0");
							lcd_putint(Servoimpulsdauer); // Servoimpulsdauer
						*/
					}break;
						
					case 6://
					{ 
					sensornummer=0xAF;
					Sensornummerlesen(0,&sensornummer);
						lcd_gotoxy(0,0);
						lcd_puts("Sens\0");
						lcd_putc('1');
						lcd_putc(' ');
						lcd_puthex(sensornummer);
					
					}break;
						
					case 7:// Schalter rückwaerts
					{ 
					sensornummer=0x00;
					Sensornummerlesen(0,&sensornummer);
						lcd_gotoxy(0,0);
						lcd_puts("Sens\0");
						lcd_putc('0');
						lcd_putc(' ');
						lcd_puthex(sensornummer);
					
					}break;
						
					case 8://
					{ 
					sensornummer=0x00;
					Sensornummerlesen(1,&sensornummer);
						lcd_gotoxy(0,0);
						lcd_puts("Sens\0");
						lcd_putc('1');
						lcd_putc(' ');
						lcd_puthex(sensornummer);
					

					}break;
						
					case 9:// Schalter vorwaerts
					{ 
					sensornummer=0x00;
					Sensornummerlesen(2,&sensornummer);
						lcd_gotoxy(0,0);
						lcd_puts("Sens\0");
						lcd_putc('2');
						lcd_putc(' ');
						lcd_puthex(sensornummer);
					
					}break;

					case 10:// *
					{ 
						
					}break;

					case 11://
					{ 
						
					}break;
						
					case 12: // # Normalbetrieb einschalten
					{
						Programmstatus &= ~(1<<MANUELL); // MANUELL OFF
						Programmstatus &= ~(1<<MANUELLNEU);
						MANUELL_PORT &= ~(1<<MANUELLPIN);
					}
						
				}//switch Tastatur
				
//				delay_ms(400);
//				lcd_gotoxy(18,1);
//				lcd_puts("  ");		// Tastenanzeige loeschen

			}//if TastaturCount	
			
		}
	}
	
	
	return 0;
}