//开机初始化
void All_Init( void )
{
	const uint32_t NewDeviationAddr = 0x4000;
	
	/* BootLoad引导程序时,必须在Main中添加,同时更改“魔术棒--Target选项卡的ROM偏移地址” */
	NVIC_SetVectorTable(NVIC_VectTab_FLASH,NewDeviationAddr);				
	
	/* 外设初始化 */
	lcd_init();   
	rtc_init(); 
	led_init(); 	 
	uart3_init(BAUD_38400);
	print_init();	//波特率19200
	timeout_init();
	TIM5_TimeoutInit();	
	link_init(); 
	tf_init();
	eep_init(); 
	delay_init_t2();
 	beep_init();
	SysTick_Config(SYSTICK_10MS);
	
//	PrintSystemParameter();	//串口打印系统参数信息,用于调试
	
	#ifdef ENABLE_BEEP
		BEEP_START();
	#endif
		
	set_page(system_init);
}
void UART_1602_Init()
{
	uart3_init(9600);
	backlightOn() ;
	clearLCD();
    lcdPosition(0,0);
}
Beispiel #3
0
/*---------------------------------------------------------------------------*/
static int
init(void)
{
	uart3_init(0);
	uart3_set_input(xbee_input_handler);
	ringbuf_init(&rxbuf, rxbuf_data, sizeof(rxbuf_data));
	process_start(&xbee_process, NULL);
	return 0;
}
void init_devices(void){
	cli(); //disable all interrupts
	timer1_init();
	lcd_init();
	uart3_init();
	servo2_pin_config(); 
	motion_port_init();
	sei(); //re-enable interrupts
}
Beispiel #5
0
int main(void)
{
	uart3_init();

	printf("Hello World!\n");
	printf("\n\n%s\n", BANNER);

	while (1);
	return 0;
}
Beispiel #6
0
void  main_init( void )
{
  int   sts;
  
  sts = get_reset_reason();
  main_status = MAIN_STS_NO_SNS_ERR;
  main_is_enter_stby_mode = 0;
  
  bio_init();
  clk_init();
  uart_init();
  dbg_wait();
  
#ifdef FEATURE_WDOG_TRIG
  IWDG_Enable();
  bio_led_ctrl( BIO_LED_IX_POWER, 0, 0, 0 );
#else // FEATURE_WDOG_TRIG
  dbg_out( "\r\n**********==> Watchdog Disabled!!!!!\r\n" );
  bio_led_ctrl( BIO_LED_IX_POWER, 500, 500, BIO_LED_ALWAYS_WINK );
#endif  // FEATURE_WDOG_TRIG
  
  dbg_out_pool_const( UART_DBG_MSG_ENTER3 );
  dbg_out( "================== 0x%02X =================\r\n", sts );
  dbg_out( "==== Welcome to %s  Ver %d.%02X ====\r\n",
           MAIN_TIT_STR, MAIN_VER_MAJ, MAIN_VER_MIN );
  dbg_out( "=====   i=%d, si=%d, li=%d, f=%d, d=%d   =====\r\n",
           sizeof( int ), sizeof( short int ),
           sizeof( long  int ),sizeof( float ),sizeof( double ) );
  dbg_out( "=====  %dMHz %s  %s  =====\r\n",
           SystemCoreClock / 1000000, __DATE__, __TIME__ );
  dbg_out_pool_const( UART_DBG_MSG_EQ_LINE );
  dbg_wait();
  
  view_reset_reason( sts );
  eep_param_init();
  IWDG_ReloadCounter();
  
  u3_ctrl.sw_baud = 115200;
  uart3_init();
  sns_init();
  adc_init();
  zb_init();
  encb_init();
  fnd_init();
  indc_init();
  
  bio_set_fan_pwm_base_freq( eep_hw_info.fan_base_f );
  cmd_rx_ptr = u1_ctrl.rx_bptr;
  
  // set monitor timer
  clk_set_timer( &monit_timer, MAIN_MONITOR_MS, MAIN_MONITOR_MS,
                 CLK_SIGS_MONITOR );
  
} // end of main_init()
Beispiel #7
0
void uart_init( void )
{
#ifdef USE_UART1
  uart1_init();
#endif
#ifdef USE_UART2
  uart2_init();
#endif
#ifdef USE_UART3
  uart3_init();
#endif
}
    /*!
	 initialize all devices. call all other initialization function
    */
void init_devices (void)
{
	cli(); //Clears the global interrupts
	port_init();
	adc_init();
	uart3_init();
	timer5_init();
	timer4_init();
	TCCR4B = 0x00;
	TIMSK4 = 0x01; //timer4 overflow interrupt enable
	TIMSK3 = 0x01;
	sei();   //Enables the global interrupts
}
Beispiel #9
0
void init()
{
	delay_init();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
	LED_Init();	//LED端口初始化 	
	TIM3_Int_Init(719,4999);  //主中断0.03
	uart_init(9600);	 //串口初始化为115200,读上位机数据
	uart2_init(9600);  //读陀螺仪数据
	uart3_init(9600);
	ENCODE_Init();  //编码器初始化
	queue_init(&rx_queue);	
	basketballbot.Control_ID = Global;
}
Beispiel #10
0
void uart_init( void )
{
#ifdef USE_UART0
  uart0_init();
#endif
#ifdef USE_UART1
  uart1_init();
#endif
//TODO: add uart2 and greater
#ifdef USE_UART2
  uart2_init();
#endif
#ifdef USE_UART3
  uart3_init();
#endif
}
Beispiel #11
0
void ddk_init(void)
{
    // 1. first, bring buffer enable into well defined state
    buffer_init();
    buf1_enable();
    buf2_enable();
    buf3_disable();
    buf4_disable();
    buf5_disable();
    buf6_disable();
    buf7_disable();
    buf8_disable();

    // 2. visuals
    led_init();
    led1_clr();
    led2_clr();
    led3_clr();
    led4_clr();

    // 3. comms
    uart0_init(115200, 0);
    uart1_init(115200, 0);

#ifdef WITH_UART2
    uart2_init(115200, 0); // temporarily disabled and used as i/o
#endif

#ifdef WITH_UART3
    uart3_init(115200, 0); // temporarily disabled and used as i/o
#endif

    // 4. enable certain buffers if necessary

    // 5. init misc i/o
    io_init();

    // 6. start fpga
    io_fpga_enable();
    //io_fpga_disable();

    // enable systick interrupt. (1msec)
    SysTick_Config(SystemCoreClock/1000 - 1); /* Generate interrupt each 1 ms   */
    SYSTICK_Cmd(DISABLE);
}
Beispiel #12
0
/*!
 * @brief OSメイン関数
 * @attention CPSRの外部割込み無効モードとして起動
 * @param[in] なし
 * @param[out] なし
 * @return 終了値
 * 	@retval 0:OS実効終了
 * @note 正常retvalは返却されない
 */
int main(void)
{
  
  unsigned long *p;

  /* BSSセクションの初期化(BSSセクションの初期化はここでOK) */
  for (p = &_bss_start; p < &_end; p++) {
    *p = 0;
  }

  uart3_init(); /* シリアルの初期化 */

  KERNEL_OUTMSG("kernel boot OK!\n");

  /* OSの動作開始 */
  kernel_init(start_threads, "init tsk", 0, 0x100, 0, NULL); /* initタスク起動 */
  
  /* 正常ならばここには戻ってこない */

  return 0;
}
Beispiel #13
0
int main(void)
{
	// Declare local variables
	//unsigned char switches;
	unsigned char ch;
	
	// Initialize the SAM4 board
	sysclk_init();
	board_init();
	
	// Initialize the IO board
	my_ioport_init();
	
	// Initialize UART3
	uart3_init();


	PutString("\x1B[1c");
	PutString("\x1B[j");
	// Loop section
	// repeate of last week's lab in a condensed manner
	while (true) {
		
		PutString("\x1B[0;0H");
		PutString("drman");
		PutString("\x1B[1;0H");
		PutString("harbour");
		delay_ms(5000);
		
		PutString("\x1B[0;0H");
		PutString("you");
		PutString("\x1B[1;0H");
		PutString("rock");
		delay_ms(5000);
		
	}

} // end of main, never reached
Beispiel #14
0
int main(void)
{ 
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
	delay_init(168);		//延时初始化 
	uart_init(9600);		//串口1初始化波特率为9600
	uart3_init(9600);		//串口3初始化波特率为9600
	LED_Init();				  //初始化LED端口  
	EXTIX_Init();       //初始化外部中断输入 

	
	printf("keyup:写块数据中断!\r\n");
	printf("key0:读块数据中断!\r\n");
	printf("key1:读卡号中断!\r\n");
	
	LoadPassword();			//装载密码


	//等待中断发生
	while(1)
	{

	}
		
}
Beispiel #15
0
int main(void)
{
    // Declare local variables
    unsigned char switch_status;	// used to detect input button switches
    unsigned short potReading;		// used to read the potentiometer Reading

    unsigned char ydirection;		// used to set the y position direction of the joystick
    // Initialize the SAM4 board
    sysclk_init();
    board_init();

    // Initialize the IO board
    my_ioport_init();

    // Initialize UART3
    uart3_init();

    // initialize UART0
    uart0_init();

    // initialize SPI
    spi_init();

    // initialize ADC
    adc_init();


    // welcome message
    puts("Welcome, my friend!");

    // Main Loop
    while (true) {

        // read switch module on port C and mask out all but lowest 2 bits
        switch_status = PORTC_PIO_PDSR & 0b00000011;

        // exchange data with joystick
        spi_joystick_exchange(switch_status);

        // write button status from joystick to matching LEDs
        PORTB_PIO_SODR = (buttons * 2) ;
        // turn off matching leds
        PORTB_PIO_CODR = ~(buttons * 2) ;

        // assign y's direction bit according to the value of the y_position
        if (y_position >= 512) {
            // positive y direction
            ydirection = 0;
            // turn led 0 on
            PORTB_PIO_CODR = 0x4000;
            // reduce y position to the correct range using modulus property
            y_position = y_position % 512;
        } else {
            // negative y direction
            ydirection = 1;
            // turn led 0 on
            PORTB_PIO_SODR = 0x4000;
            // reduce y position to the correct range in the negative direction
            y_position = (512-y_position);
        }

        // read the 10 bit value from Channel 0 of the A/D Converter
        potReading = ADC_CDR0;

        // start the next a/d conversion
        ADC_CR = ADC_START;

        // divide the a/d value by 2 to restrict the range
        potReading = (potReading / 2) ;

        // print the direction, potentiometer Reading, and yposition's transformed value to the console
        printf("\rY position is %d--direction bit: %d--A/D value: %d\n\r ", y_position, ydirection, potReading);

    }

} // end of main, never reached
Beispiel #16
0
int main(void)
{		

	u16 times=0,i=0; 

	NORMALTIME cur_time; //设置RTC的时间用
	
	//Flash_Write(0x08041000,(u8 *)IndCalib,sizeof(IndCalib));

	Flash_Read(0x08041000,(u8 *)IndCalib,sizeof(IndCalib));



	delay_init();	    	 //延时函数初始化	  
  	NVIC_Configuration(); 	 //设置NVIC中断分组2:2位抢占优先级,2位响应优先级

	uart1_init(115200);	 //上位机通信模块
	uart2_init(9600);	 //风速传感器模块 A2 A3 要转为232
	//uart3_init(19200);	 //SD卡模块	(兼职风向-因为3有重映射功能)
	uart4_init(9600);	 //称重模块
	uart5_init(9600);	 //AD模块

 	LED_Init();			 //LED端口初始化
	KEY_Init();          //初始化与按键连接的硬件接口
	SD_init();

	OPEN_SD_POWER;
	CLOSE_SD_POWER;
	OPEN_SD_POWER;
	CLOSE_SD_POWER;
	OPEN_SD_POWER;

	cur_time.tm_year = 2016; //2016-1900
	cur_time.tm_mon = 5;
	cur_time.tm_mday = 29;
	cur_time.tm_hour = 23;
	cur_time.tm_min = 56;
	cur_time.tm_sec = 2;

    RTC_Init(cur_time);//

	while(DS18B20_Init())//初始化DS18B20,兼检测18B20
	{
		printf("DS18B20 Check Failed!"); 
		printf("Please Check!  ");   
	}
	printf("DS18B20 Ready!     ");

 	while(1)
	{		

		if(IS_BUT_DN) 
		{
			BEEP = 0;
		}
		else
		{
			BEEP = 1;
			g_PaOffset= -g_Pa;

			g_IndValOffset[0]= -Volt2Distance(0,g_IndVal[0]);
			g_IndValOffset[1]= -Volt2Distance(1,g_IndVal[1]);
			g_IndValOffset[2]= -Volt2Distance(2,g_IndVal[2]);
			g_IndValOffset[3]= -Volt2Distance(3,g_IndVal[3]);
		}
		
		if(times%60==0) 
		{
			windSpeedDirFlag++;
			sendWindSpeedCmd(); //串口2 带232
			//printf("3\r\n");
			if(5 == windSpeedDirFlag)
			{
				windSpeedDirFlag = 0; 	
			}
			//printf("4\r\n");
			//风速的反应慢 一秒钟最多发一次指令 不然传感器要疯掉
			if(0 == windSpeedDirFlag)
			{
				uart3_init(19200);	 //SD卡模块	(兼职风向-因为3有重映射功能)
				g_uart3_used_for_SD = 1;
								//				日期			时间		  位移1, 2,   3,  4	 |拉力|                    温度 |SD|风速|风向
				sprintf(CmdStr,"#01,%02d-%02d-%02d,%02d:%02d:%02d,%04d,%04d,%04d,%04d,%04d,%04d,%04d,%04d,%04d,%04d,%1d,%04d,%04d!\r\n",
												cur_time.tm_year,cur_time.tm_mon,cur_time.tm_mday,
												cur_time.tm_hour,cur_time.tm_min,cur_time.tm_sec,
												dateSendtoPC[0],dateSendtoPC[1],dateSendtoPC[2],dateSendtoPC[3],
												dateSendtoPC[4],
												dateSendtoPC[6],dateSendtoPC[7],dateSendtoPC[8],dateSendtoPC[9],
												g_Temper,lastSDerr,g_WindSpeed,g_WindDir);
				//printf("7\r\n");
				lastSDerr = write_string_to_files(CmdStr);
			}
			else
			{
				//printf("8\r\n");
				uart3_init2(9600);
				//SD卡的usart3 重映射到 PB10 和PB11口 来读风向
				g_uart3_used_for_SD = 0;
				send_byte_to_usart3(0x02);//初始化完后第一个字节会发不成功
				sendWindDirCmd();//串口3
			}
			//printf("11\r\n");
			cur_time = Time_GetTime();
		}  

		if(times%10==0) 
		{			
			//sendWeightLoad1Cmd();
			//称重标定代码
			//printf("12\r\n");
			switch(PC_Wcmd)
			{
				case 0x30:
					//printf("13\r\n");
					BEEP = 1;
					sendWeightZeroCmd();
					BEEP = 0;
					PC_Wcmd = 0;
				break;
	
				case 0x40:
					//printf("14\r\n");
					BEEP = 1;
					sendWeightLoad1Cmd();					
					PC_Wcmd = 0;
					BEEP = 0;
				break;
				case 0x41:
					//printf("15\r\n");
					BEEP = 1;
					sendWeightLoad2Cmd(g_weightCalib[0],g_weightCalib[1]);
					PC_Wcmd = 0;
					BEEP = 0;
				break;
				default:
					//printf("16\r\n");
					//sendWeightLoad2Cmd(0x27,0x10);
					sendWeightCmd(); //串口4
				break;
			}

			BLED3=1;
			sendADCmd(); //串口5
			BLED3=0;
			dateSendtoPC[0] = g_IndVal[0];
			dateSendtoPC[1] = g_IndVal[1];
			dateSendtoPC[2] = g_IndVal[2];
			dateSendtoPC[3] = g_IndVal[3];
			dateSendtoPC[4] = g_PaOffset + g_Pa;

			dateSendtoPC[6] = g_IndValOffset[0] + Volt2Distance(0,g_IndVal[0]);
			dateSendtoPC[7] = g_IndValOffset[1] + Volt2Distance(1,g_IndVal[1]);
			dateSendtoPC[8] = g_IndValOffset[2] + Volt2Distance(2,g_IndVal[2]);
			dateSendtoPC[9] = g_IndValOffset[3] + Volt2Distance(3,g_IndVal[3]);
		}
		switch(PC_cmd)
		{
			case 1:
				BLED2 = 1;
					//				日期			时间		  位移1, 2,   3,  4	 |拉力|                    温度 |SD|风速|风向
				sprintf(CmdStr,"#01,%02d-%02d-%02d,%02d:%02d:%02d,%04d,%04d,%04d,%04d,%04d,%04d,%04d,%04d,%04d,%04d,%d,%04d,%04d!\r\n",
												cur_time.tm_year,cur_time.tm_mon,cur_time.tm_mday,
												cur_time.tm_hour,cur_time.tm_min,cur_time.tm_sec,
												dateSendtoPC[0],dateSendtoPC[1],dateSendtoPC[2],dateSendtoPC[3],
												dateSendtoPC[4],
												dateSendtoPC[6],dateSendtoPC[7],dateSendtoPC[8],dateSendtoPC[9],
												g_Temper,lastSDerr,g_WindSpeed,g_WindDir);
				send_string_to_usart1(CmdStr);
				BLED2 = 0;
				PC_cmd = 0;
			break;
			case 0x10:
			case 0x11:
			case 0x12:
			case 0x13:
			case 0x14:
				BEEP = 1;
				sprintf(CmdStr,"#%02x",PC_cmd);
				for(i=0;i<21;i++)
				{
					sprintf(CmdStr,"%s,%+05d",CmdStr,IndCalib[PC_cmd-0x10][i]);	
				}
				sprintf(CmdStr,"%s!\r\n",CmdStr);
				send_string_to_usart1(CmdStr);
				PC_cmd = 0;
				BEEP = 0;
			break;
			case 0x20:
			case 0x21:
			case 0x22:
			case 0x23:
			case 0x24:
				sprintf(CmdStr,"#%02x",PC_cmd-0x10);
				for(i=0;i<21;i++)
				{
					sprintf(CmdStr,"%s,%+05d",CmdStr,IndCalib[PC_cmd-0x20][i]);	
				}
				sprintf(CmdStr,"%s!\r\n",CmdStr);
				send_string_to_usart1(CmdStr);
				PC_cmd = 0;

			break; 		
		}
		times++;  
		delay_ms(10);  
	}	 
 }
Beispiel #17
0
static rt_err_t rt_dev_uart_init (rt_device_t dev)
{
	uart3_init(DEV_UART_BAUDRATE);

	return RT_EOK;
}
Beispiel #18
0
void mcu_init(void) 
{

  mcu_arch_init();

#ifdef PERIPHERALS_AUTO_INIT
  sys_time_init();
#ifdef USE_LED
  led_init();
#endif
  /* for now this means using spektrum */
#if defined RADIO_CONTROL & defined RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT & defined RADIO_CONTROL_BIND_IMPL_FUNC
  RADIO_CONTROL_BIND_IMPL_FUNC();
#endif
#ifdef USE_UART0
  uart0_init();
#endif
#ifdef USE_UART1
  uart1_init();
#endif
#ifdef USE_UART2
  uart2_init();
#endif
#ifdef USE_UART3
  uart3_init();
#endif
#ifdef USE_UART4
  uart4_init();
#endif
#ifdef USE_UART5
  uart5_init();
#endif
#ifdef USE_I2C0
  i2c0_init();
#endif
#ifdef USE_I2C1
  i2c1_init();
#endif
#ifdef USE_I2C2
  i2c2_init();
#endif
#ifdef USE_ADC
  adc_init();
#endif
#ifdef USE_USB_SERIAL
  VCOM_init();
#endif

#if USE_SPI
#if SPI_MASTER

#if USE_SPI0
  spi0_init();
#endif
#if USE_SPI1
  spi1_init();
#endif
#if USE_SPI2
  spi2_init();
#endif
#if USE_SPI3
  spi3_init();
#endif
  spi_init_slaves();
#endif // SPI_MASTER

#if SPI_SLAVE
#if USE_SPI0_SLAVE
  spi0_slave_init();
#endif
#if USE_SPI1_SLAVE
  spi1_slave_init();
#endif
#if USE_SPI2_SLAVE
  spi2_slave_init();
#endif
#if USE_SPI3_SLAVE
  spi3_slave_init();
#endif
#endif // SPI_SLAVE
#endif // USE_SPI

#ifdef USE_DAC
  dac_init();
#endif
#else
INFO("PERIPHERALS_AUTO_INIT not enabled! Peripherals (including sys_time) need explicit initialization.")
#endif /* PERIPHERALS_AUTO_INIT */

}
Beispiel #19
0
int main(void)
{
	// Declare local variables
	unsigned char switches;

	
	// Initialize the SAM4 board
	sysclk_init();
	board_init();
	
	// Initialize the IO board
	my_ioport_init();
	
	// Initialize UART3
	uart3_init();
	
	// Set LCD to Cursor Mode
	PutCharacter(0x1B);
	PutCharacter('[');
	PutCharacter('1');
	PutCharacter('c');
	
	// Set LCD to clear display and home cursor
	PutCharacter(0x1B);
	PutCharacter('[');
	PutCharacter('j');
	
		// Loop section
	
	while (true) {
		
		// Send command to move cursor back to start of first line
		PutCharacter(0x1B);
		PutCharacter('[');
		PutCharacter('0');
		PutCharacter(';');
		PutCharacter('0');
		PutCharacter('H');
		
		// Put letter Y to lcd screen
		PutCharacter('Y');
		
		// Send command to move cursor to start of second line
		PutCharacter(0x1B);
		PutCharacter('[');
		PutCharacter('1');
		PutCharacter(';');
		PutCharacter('0');
		PutCharacter('H');
		
		// send letter R
		PutCharacter('R');
        
        // wait 5 seconds
		delay_ms(5000);
		
		// move to start of first line
		PutCharacter(0x1B);
		PutCharacter('[');
		PutCharacter('0');
		PutCharacter(';');
		PutCharacter('0');
		PutCharacter('H');
		
		// send letter U
		PutCharacter('U');
		
		// move to start of second line
		PutCharacter(0x1B);
		PutCharacter('[');
		PutCharacter('1');
		PutCharacter(';');
		PutCharacter('0');
		PutCharacter('H');
		
		// send the word "here"
		PutCharacter('H');
		PutCharacter('E');
		PutCharacter('R');
		PutCharacter('E');
		
		// wait 5 seconds
		delay_ms(5000);
		
	}


	
	

} // end of main, never reached
Beispiel #20
0
int main (void)
{	
	/* variables for the UART0 (USB connection) */
	unsigned int c = 0, c2 = 0, c3 = 0; // Variable for reading UARTS
	char buffer[MAX_MSG_SIZE];
	char buffer2[MAX_MSG_SIZE];
	char buffer3[MAX_MSG_SIZE];
	int  idx = 0, idx2 = -1, idx3 = -1;
	int	 len2 = 0;
	int	 len3 = 0;
	char meas_buffer[TX_BUFF_SIZE];
	int txi = 0;
	int txtop=0;
	unsigned int i = 0;
	char *ptr;
	unsigned char hli_mutex = 0;
	unsigned int gps = 0;
	unsigned int imu = 0;
	signed int ratio = 0;
	uint16_t xacc = 0;
	uint8_t xacca[2];
	char s[64];
	char rmc[256];

	awake_flag = 0;

	#ifdef RF_TEST_IDX
	uint8_t gps_rf_test_idx = 0;
	uint8_t imu_rf_test_idx = 0;
	#endif


  /* set outputs */
	PORTL = 0xff; // Turn off LEDS
  DDRL = (1<<LED1) | (1<<LED2) | (1<<LED3) | (1<<LED4); // Set pins for LED as output

	pwm_init();
	spiInit();

	/* initialize UARTS */
  uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) ); // USB connection
  uart2_init( UART_BAUD_SELECT(UART2_BAUD_RATE,F_CPU) ); // APC220 radio
  uart3_init( UART_BAUD_SELECT(UART3_BAUD_RATE,F_CPU) ); // UP-501 GPS

	/* Interrupt stuff for ADIS */
	PCICR |= 1<<PCIE2; // Enable interrupt PORTK
	PCMSK2 |= (1<<PCINT23); // interrupt in PCINT23

  /* now enable interrupt, since UART library is interrupt controlled */
  sei();

	spiTransferWord(0xBE80); // ADSI software reset
	_delay_ms(500);
	/* Set GPS to a faster baud and update UART speed */
	//uart3_puts("$PMTK251,115200*1F");
	uart3_puts("$PMTK251,57600*2C\r\n");
	//uart3_puts("$PMTK251,38400*27");
	//uart3_puts("$PMTK251,0*28");
	_delay_ms(500);
	//	uart3_init( UART_BAUD_SELECT(115200,F_CPU) );
	uart3_init( UART_BAUD_SELECT(57600,F_CPU) );
	//uart3_init( UART_BAUD_SELECT(38400,F_CPU) );
	/* 115200 seems to be a little bit unstable, at least testing via radio*/

	adis_reset_factory();
	adis_set_sample_rate();

  while (1) {
		/* Read each UART serially and check each of them for data, if there is handle it */ 	
		c = uart_getc();
		c2 = uart2_getc();
		c3 = uart3_getc();

		// Stop motors when connection is lost
		if (awake_flag > AWAKE_THRESHOLD) {
			pwm_set_duty(RC1, 0 );
			pwm_set_duty(RC2, 0 );
		};

		if(tx_counter >= TX_READY) {
			//empty buffer
			for (txi = 0; txi < txtop; txi++) {
				uart2_putc(meas_buffer[txi]); // Sending buffered data to RF
			}
			txtop = 0;
			#ifdef AUTO_SHUTDOWN_ENABLE
			awake_flag++;
			#endif
			PORTL ^= (1<<LED2);
			tx_counter -= TX_READY;
		}


		if (adis_ready_counter >= ADIS_READY) {
			adis_decode_burst_read_pack(&adis_data_decoded);
			adis_reduce_decoded_burst(); // Reduce data ammount
			#ifdef LOG_ENABLE
			hli_send(package(sizeof(adis8_t), 0x14, 0x0D, &adis_data_decoded), sizeof(adis8_t)); // Log to SD card
			#endif

			#ifdef RF_TEST_IDX
			memcpy(&adis_data_decoded_reduced.zgyro[0],&imu_rf_test_idx,1);
			if (imu_rf_test_idx == 255)
				imu_rf_test_idx = 1;
			else
				imu_rf_test_idx++;
			memcpy(&meas_buffer[txtop],	(char *)package(sizeof(adis8_reduced_t), 0x14, 0x0F, &adis_data_decoded_reduced),sizeof(adis8_reduced_t)+6);
			#endif
			#ifndef RF_TEST_IDX
			memcpy(&meas_buffer[txtop],	(char *)package(sizeof(adis8_reduced_t), 0x14, 0x0E, &adis_data_decoded_reduced),sizeof(adis8_reduced_t)+6);
			#endif
			txtop=txtop+sizeof(adis8_reduced_t)+6;

			adis_ready_counter -= ADIS_READY;

			PORTL ^= (1<<LED4);
		}


		/* Reading from radio */
		if ( c2 & UART_NO_DATA ) {} else // Data available
		{ //if data is $, set a flag, read next byte, set that value as the length, read while incrementing index until length reached, parse
//uart_putc(c2);
			if (idx2 == 0) { // We should buffer a packet
				len2 = c2+5; // Set length
			}

			if ( (idx2 < len2) && (idx2 >= 0)) { // We are buffering
				buffer2[idx2] = c2;
				idx2++;

				if (idx2 == len2) { // We now have a full packet

					if (parse(&rfmsg, buffer2)) {
						PORTL ^= (1<<LED1);
						process(&rfmsg);
					}

					idx2 = -1; // Set flag in new packet mode

					#ifdef DEBUG
					//puts_msg(&rfmsg);
					#endif
				}
			}

			if (c2 == '$') { // We have a possible message comming
//				PORTL ^= (1<<LED4);
				idx2 = 0; // Set "flag"
			}
		}

		/* Reading from GPS */
		if ( c3 & UART_NO_DATA ) {} else  // Data available
		{
			/* Transmitting NMEA GPS sentences to the HLI */
			if (c3 == '$') { // We have a possible message comming
				//PORTL ^= (1<<LED3);
				len3 = 0; // Set "flag"
			}

			if (len3 >= 0) { // We are buffering
				buffer3[len3] = c3;
				len3++;
				if (c3 == '\n') { // We now have a full packet
					if(buffer3[4] != 'S') { // Disable GSV and GSA messages
						#ifdef LOG_ENABLE
						hli_send(package(len3, 0x1E, 0x06, buffer3), len3); // Log to SD card
						#endif
						if (rmc_cut(buffer3,rmc)) {
							// Invalid RMC data
						} else {
							#ifdef RF_TEST_IDX
							memcpy(&rmc[0],&gps_rf_test_idx,1);
							if (gps_rf_test_idx == 255)
								gps_rf_test_idx = 1;
							else
								gps_rf_test_idx++;
							memcpy(&meas_buffer[txtop],	(char *)package(rmc_idx, 30, 31, rmc),rmc_idx+6);
							#endif
							#ifndef RF_TEST_IDX
							memcpy(&meas_buffer[txtop],	(char *)package(rmc_idx, 30, 6, rmc),rmc_idx+6);
							#endif
							txtop=txtop+rmc_idx+6;

							PORTL ^= (1<<LED3);
						}

						len3 = -1; // Set flag in new packet mode
					}
				}
			}
		}
  }

  return 1;
}
Beispiel #21
0
/////////////////
//	main routine
/////////////////
void main (void) {
  
  uint8_t   Rx, loop;
  
  
  /////////////////
  //	init peripherals
  /////////////////

  // disable interrupts
  DISABLE_INTERRUPTS;

  // switch to 16MHz (default is 2MHz)
  CLK.CKDIVR.byte = 0x00;  
  
  // configure green LED pin (PH2)
  gpio_init(&PORT_H, PIN_2, OUTPUT_PUSHPULL_FAST);

  // init timer TIM4 for 1ms clock with interrupts
  tim4_init();
  
  // init pins for UART1 Rx(=PA4) and Tx(=PA5)
  gpio_init(&PORT_A, PIN_4, INPUT_PULLUP_NOEXINT);
  gpio_init(&PORT_A, PIN_5, OUTPUT_PUSHPULL_FAST);

  // init UART1 (connected to PC on muBoard)
  uart1_init(115200L);
  
  // init pins for UART3 Rx(=PD6) and Tx(=PD5)
  gpio_init(&PORT_D, PIN_6, INPUT_PULLUP_NOEXINT);
  gpio_init(&PORT_D, PIN_5, OUTPUT_PUSHPULL_FAST);

  // init UART3 (available on io-plug on muBoard)
  uart3_init(115200L);
  
  // enable interrupts
  ENABLE_INTERRUPTS;

    
  /////////////////
  //	main loop
  /////////////////
  loop = 0;
  while (1) {
    
    // if byte received via UART1 send via UART3
    if (uart1_check_Rx())
      uart3_send_byte(uart1_receive());
    
    // if byte received via UART3 send via UART1
    if (uart3_check_Rx())
      uart1_send_byte(uart3_receive());


    // every 1ms do
    if (g_flagClock) {
      g_flagClock = 0;
    
      // every 500ms toggle LED & print text
      if (g_clock > 500) {
        g_clock = 0;
        
        // toggle LED
        PORT_H.ODR.bit.b2 ^= 1;
      
      } // loop 500ms
      
    } // loop 1ms
    
  } // main loop

} // main
Beispiel #22
0
int main(void)
{
	// Declare local variables
	unsigned short potReading;		// used to read the potentiometer Reading
	unsigned char task20_count;		// increments to 5 to denote 20 Hz
	unsigned char task10_count;		// increment to 10 to denote 10 Hz
	unsigned char task5_count;		// increments to 20 to denote 5 Hz
	unsigned char task1_count;		// increments to 100 to denote 1Hz
	unsigned char ydirection;		// used to set the y position direction of the joystick
	unsigned char formattedString[20];	// use to send text to the LCD Screen
	unsigned short delta_count;		// stores change between old count and sa_count
	unsigned short old_count;		// stores old count from the sa_count
	unsigned short counts_per_minute;
	unsigned short wholeRPM;
	unsigned short fractionalRPM;
	
	// Initialize the SAM4 board
	sysclk_init();
	board_init();
	
	// Initialize the IO board
	my_ioport_init();
	
	// Initialize UART3
	uart3_init();
	
	// initialize UART0
	uart0_init();
	
	// initialize SPI
	spi_init();
	
	// initialize ADC
	adc_init();
	
	// initialize timer0 module
	timer_init();
	
	// initialize the pwm module
	pwm_init();
	

	// initialize LCD
	PutString("\x1B[1c");		// lcd cursor mode
	PutString("\x1B[j");		// clear display and move to home position
	PutString("sp=    cnt=    ");	// static text to the LCD
	
	// Main Loop
	while (true) {
		// wait for the counter to reach 10
		while (ms_counter <= 10) {
			
		}
		
		// read the 10 bit value from Channel 0 of the A/D Converter
		potReading = ADC_CDR0;
		
		// start the next a/d conversion
		ADC_CR = ADC_START;
		
		// divide the a/d value by 2 to restrict the range
		potReading = (potReading / 2) ;
		// set ms counter back to 0
		ms_counter = 0;
		// increment time variables appropiately
		task10_count++;
		task20_count++;
		task5_count++;
		task1_count++;
		
		// every 20 Hz
		if (task20_count >= 5)
		{
			// exchange data with joystick (no switch module so use a garbage value)
			spi_joystick_exchange(0);
			
			// assign y's direction bit according to the value of the y_position
			// changed to 508 to ensure center position was 0 value with my motor
			if (y_position >= 508)
			{
				// positive y direction --> counterclockwise
				// turn led 0 off
				PORTB_PIO_CODR = 0x4000;
				// reduce y position to the correct range using modulus property
				y_position = (y_position - 508);
				// clear direction of port B, bit 1 to 1
				PORTB_PIO_CODR = 0x02;
				
			}
			else
			{
				// negative y direction is clockwise
				// turn led 0 on
				PORTB_PIO_SODR = 0x4000;
				// reduce y position to the correct range in the negative direction
				y_position = (508-y_position);
				// clear direction of port B, bit 1 to 1
				PORTB_PIO_SODR = 0x02;
				
			}
			
			// use the smaller of the two values
			if (y_position >= potReading) {
				// update the PWM Duty Cycle with the A/D value
				PWM_CUPD0 = potReading;
				// build a formatted string using the A/D value
				sprintf(formattedString, "%3x", potReading);
				} else {
				// update the PWM Duty Cycle with the y_position value
				PWM_CUPD0 = y_position;
				// build a formatted string using the y_position value
				sprintf(formattedString, "%3x", y_position);
			}
			
			// send the command to LCD to position the cursor on Line 0, column 3
			PutString("\x1B[0;3H");
			PutString(formattedString);
			
			// set task20_count back to 0;
			task20_count = 0;
		}
		
		// every 10Hz
		if ( task10_count >= 10)
		{
			// send a command string to the LCD to position the cursor on Line 0
			PutString("\x1B[0;11H");
			// build a formatted string for the sa_counter value
			sprintf(formattedString, "%4x", sa_counter);
			// send the formattedString to the LCD
			PutString(formattedString);
			// set task10_count back to 0
			task10_count = 0;
		}
		
		// every 5 Hz
		if ( task5_count >= 20)
		{
			// is the direction CW? by looking at direction signal (ie bit 1 on port b)
			if ((PORTB_PIO_ODSR & 0x02) == 0x00) {
				// capture the change in counts
				delta_count = old_count - sa_counter;

				} else {
				// capture the change in counts
				delta_count = sa_counter - old_count;

			}
			
			// save current count as the old count
			old_count = sa_counter;
		
			// computer counts per minute (5 is for 5 times per second and 60 to get from seconds to a minute)
			counts_per_minute = (delta_count * 5 * 60);
			
			// compute whole part of RPM value
			wholeRPM = (counts_per_minute/ CPR);
			
			// computer fractional part of RPM value
			fractionalRPM = (counts_per_minute % CPR) * (10 / CPR);
			
			// send a command string to the LCD to position the cursor on Line 1, row 0
			PutString("\x1B[1;0H");
			
			// is the direction CW? by looking at direction signal (ie bit 1 on port b)
			if ((PORTB_PIO_ODSR & 0x02) == 0x00) {
				// build a formatted string for the RPM value ending in CW
				sprintf(formattedString, "%3d.%1d rpm  cw ", wholeRPM, fractionalRPM);
				} else {
				// build a formatted string for the RPM value ending in CW
				sprintf(formattedString, "%3d.%1d rpm ccw ", wholeRPM, fractionalRPM);
				
			}
			
			// send the formattedString to the LCD
			PutString(formattedString);

			// set task10_count back to 0
			task5_count = 0;
		}
		
		// update every 1 Hz
		if ( task1_count >= 100)
		{
			if ((PORTB_PIO_ODSR & 0x08) == 0x08)
			{
				// clear port B
				PORTB_PIO_CODR = 0x08;
			} else
			{
				// set port b, bit 3
				PORTB_PIO_SODR = 0x08;
			}
			// set task10_count back to 0
			task1_count = 0;
		}
		

	}

} // end of main, never reached