int main(void)
{
	T7SegDisplay SevenSegDisplay;
	T7SegDisplay *p7SegDisplay = &SevenSegDisplay;

	// Init SPI hardware as master.
	SPI_Master_Init();
	
	// Init one raw 7Seg Display driven by MAX7221
	MAX7221_initDisplay(p7SegDisplay,5,&PORTB,PORTB2);
	MAX7221_refreshDisplay(p7SegDisplay);

	// Set the value for DIGIT5 in the display buffer.	
	MAX7221_setDigit(p7SegDisplay,DIGIT5,CHAR_MINUS);

	// Set the value for DIGIT4 in the display buffer.	
	MAX7221_setDigit(p7SegDisplay,DIGIT4,8);

	// Set the value for DIGIT3 in the display buffer.	
	MAX7221_setDigit(p7SegDisplay,DIGIT3,(0x02|DECIMAL_POINT));

	// Set the value for DIGIT2 in the display buffer.	
	MAX7221_setDigit(p7SegDisplay,DIGIT2,1);

	// Set the value for DIGIT1 in the display buffer.	
	MAX7221_setDigit(p7SegDisplay,DIGIT1,5);

	//Display the content of the buffer on the dispaly module
	MAX7221_refreshDisplay(p7SegDisplay);
	
	while(1);
	
	return 0;
}
示例#2
0
文件: Ej01.c 项目: queq/just-stuff
int main( void )
{

    WDTCTL = WDTPW + WDTHOLD;          // Stop watchdog timer

    BCSCTL1 = CALBC1_1MHZ;             // Set range  (1 Mhz)
    DCOCTL = CALDCO_1MHZ;              // Set DCO step + modulation */

    SPI_Master_Init();

    P1OUT &= ~(BIT0 + BIT1);            // Set P1.0 and P1.1 to 0 (when output)
    P1DIR |= (BIT0 + BIT1);             // Set P1.0 and P1.1 to output direction

    timerInterruptFlag=CLEAR;

    ConfigTimer0();                    //Configure Timer0 Module
    //for 10 ms interrupt

    _EINT();                           // enable (general) interrupts
    //eint <-----> bis #8,sr

    //example of a timed loop:

    for(;;) {                               // loop forever

        while(timerInterruptFlag != SET);     // loop forever while Flag not SET
        timerInterruptFlag=CLEAR;             // Clear timer flag

        Counter();

    }

    return 0;
}
示例#3
0
文件: Ej01.c 项目: queq/just-stuff
int main( void )
{
  
  WDTCTL = WDTPW + WDTHOLD;          // Stop watchdog timer
 
  BCSCTL1 = CALBC1_1MHZ;             // Set range  (1 Mhz)
  DCOCTL = CALDCO_1MHZ;              // Set DCO step + modulation */ 

  SPI_Master_Init();
  
  
//Pull up resistor in P1.3++++++++++++++++++++++++++++++++++++++++++++++++++ 
  P1OUT |= BIT3;                      // Bit 3 of P1OUT in 1
  P1REN |= BIT3;                      // Bit 3 of P1REN in 1
                                      // REN:Pullup/Pulldown Resistor Enable
                                      // register
//--------------------------------------------------------------------------  
  
//Hi/Low transition detection+++++++++++++++++++++++++++++++++++++++++++++++
  P1IES |= BIT3;                      // P1.3 Hi/Low transition detection
                                      // IES:Interrupt Edge Select register
  P1IFG &= ~BIT3;                     // P1.3 IFG cleared 
                                      // IFG:Interupt Flag register
//--------------------------------------------------------------------------  
  

  ConfigTimer0();                    //Configure Timer0 Module
                                     //for 10 ms interrupt 
  
  _EINT();                           // enable (general) interrupts
                                     //eint <-----> bis #8,sr
  
  //example of a timed loop:
  
  for(;;){                                // loop forever 

    while(timerInterruptFlag != SET);     // loop forever while Flag not SET
    timerInterruptFlag=CLEAR;             // Clear timer flag
  
//    LedsShifter_a();
//    LedsShifter_b();  
//    EvaluateInputTransition();            //Find if a transition has happened 
//    SequenceController();                 //Select desired sequence

  }

  return 0;
}
示例#4
0
文件: qdebug.c 项目: kerichsen/asf
/*============================================================================
Name    :   QDebug_Init
------------------------------------------------------------------------------
Purpose :   Initialize Debug Interface
Input   :   n/a
Output  :   n/a
Notes   :   Must be called before using any other QDebug API.
============================================================================*/
void QDebug_Init(void)
{
      int16_t touch_ret;

#if (defined QDEBUG_SPI)
      SPI_Master_Init();
#elif (defined QDEBUG_SERIAL)
      SERIAL_Init();
#endif

      touch_ret = QDEBUG_GET_LIBINFO_FUNC(&QDEBUG_LIBINFO);
      if(touch_ret != TOUCH_SUCCESS)
      {
        while(1);
      }

      Init_Buffers();
}
示例#5
0
文件: RFM70.c 项目: bzyx/smiw-rfm70
/*
configures RFM70 as receiver, Send_Packet automatically switches device to TX-mode,
switch back manually to RX-mode with Select_RX_Mode()
returns true when handshake with module is successful
*/
bool RFM70_Initialize(uint8_t Channel, uint8_t* Address){
	uint8_t i;//loop counter
	 
	SPI_Master_Init(CLK_Div_16);
	Enable_Chip_Enable();
	Chip_Enable();
	
	if(!RFM70_Present()){
	    return false;	
	}
	
	//start at Bank0
	Activate_Feature_registers();
	
	SPI_Write_Register(CONFIG,MASK_RX_DR|MASK_TX_DS|MAX_RT|EN_CRC|CRCO|PWR_UP|PRIM_RX);
	SPI_Write_Register(EN_AA,0);
	SPI_Write_Register(EN_RXADDR,ERX_P0);
	SPI_Write_Register(SETUP_AW,Address_5_Bytes);
	SPI_Write_Register(RF_SETUP,dBm_5|LNA_HCURR);   //todo: uses defines for power and data rate
	SPI_Write_Register(RX_PW_P0, Payload_Length);
	
	Set_Channel(Channel);

	SPI_Write_Buffer(W_REGISTER|RX_ADDR_P0,Address,5);
	SPI_Write_Buffer(W_REGISTER|TX_ADDR   ,Address,5);    //TX-address is address of receiver, Master and slave use same address so they are equal
	
	Switch_Bank(1);

	for(i=0;i<=6;i++){	
		SPI_Write_Buffer((W_REGISTER|i),&Bank1_Reg0_13[i][0],4);
	}
	//registers 7-11 are reserved
	for(i=12;i<=13;i++){	
		SPI_Write_Buffer((W_REGISTER|i),&Bank1_Reg0_13[i][0],4);
	}
	SPI_Write_Buffer((W_REGISTER|14),Bank1_Reg14,11);   //register 14 is extra wide

	Switch_Bank(0);  //back again
	//Select_RX_Mode();//RX-mode is default, switch back manually after TX-mode
	
	return true;    //success
}
示例#6
0
int main_controller_tick(int state){
    static unsigned short main_hold_time = 0;
    switch(state){//main_task transitions/actions
        case st_main_start:
            state = st_main_init;
            break;
        case st_main_init:
            SPI_Master_Init();
            PCD_Init();
            state = st_main_wait_new;
            QueueEnqueue(lcd_command_queue, lcd_write_ready);
            break;
        case st_main_wait_new:
            if(PICC_IsNewCardPresent()){
                storeTagID();
                PICC_HaltA();
                state = st_main_checkID;
            } else {
                state = st_main_wait_new;
            }
            break;
        case st_main_checkID:
            if(isMaster(tag_id)){
                state = st_main_master;
                QueueEnqueue(lcd_command_queue, lcd_write_master);
            } else if(search_valid_tag(tag_id) != -1){
                state = st_main_hold;
                main_hold_time = 0;
                if(current_lock_position == locked || current_lock_position == malfunction){
                    QueueEnqueue(lock_command_queue, lc_unlock);
                } else if(current_lock_position == unlocked){
                    QueueEnqueue(lock_command_queue, lc_lock);
                }
            } else {
                state = st_main_hold;
                main_hold_time = 0;
                QueueEnqueue(lcd_command_queue, lcd_write_invalid);
            }
            break;
        case st_main_master:
            if(!(PIND & 0x10)){
                if(isTagDBFull()){
                    state = st_main_hold;
                    main_hold_time = 0;
                    QueueEnqueue(lcd_command_queue, lcd_write_add_full);
                } else {
                    state = st_main_add_pressed;
                    QueueEnqueue(lcd_command_queue, lcd_write_add);
                }                    
            } else if(!(PIND & 0x40)){
                if(isTagDBEmpty()){
                    state = st_main_hold;
                    main_hold_time = 0;
                    QueueEnqueue(lcd_command_queue, lcd_write_remove_empty);
                } else {
                    state = st_main_rm_pressed;
                    current_remove_option = 0;
                    QueueEnqueue(lcd_command_queue, lcd_write_remove);
                }                    
            } else {
                state = st_main_master;
            }
            break;
        case st_main_add_pressed:
            if(PIND & 0x10){
                state = st_main_add_tag;
            } else {
                state = st_main_add_pressed;
            }
            break;
        case st_main_add_tag:
            if(PICC_IsNewCardPresent()){
                storeTagID();
                PICC_HaltA();
                if(isMaster(tag_id)){
                    state = st_main_add_tag;
                } else if(search_valid_tag(tag_id) == -1){
                    state = st_main_hold;
                    main_hold_time = 0;
                    add_valid_tag(tag_id);
                    QueueEnqueue(lcd_command_queue, lcd_write_added);
                } else {
                    state = st_main_hold;
                    main_hold_time = 0;
                    QueueEnqueue(lcd_command_queue, lcd_write_add_exist);
                }
            } else {
                state = st_main_add_tag;
            }
            break;
        case st_main_rm_pressed:
            if(PIND & 0x40){
                state = st_main_rm_tag;
            } else {
                state = st_main_rm_pressed;
            }
            break;
        case st_main_rm_tag:
            if(!(PIND & 0x10)){
                current_remove_option = (current_remove_option + (numValidKeys - 1)) % numValidKeys;
                QueueEnqueue(lcd_command_queue, lcd_write_remove);
                state = st_main_lr_pressed;
            } else if(!(PIND & 0x40)){
                current_remove_option = (current_remove_option + 1) % numValidKeys;
                QueueEnqueue(lcd_command_queue, lcd_write_remove);
                state = st_main_lr_pressed;
            } else if(!(PIND & 0x20)){
                remove_valid_tag(current_remove_option);
                QueueEnqueue(lcd_command_queue, lcd_write_removed);
                state = st_main_hold;
                main_hold_time = 0;
            } else {
                state = st_main_rm_tag;
            }
            break;
        case st_main_lr_pressed:
            if( (PIND & 0x10) && (PIND & 0x40) ){
                state = st_main_rm_tag;
            } else {
                state = st_main_lr_pressed;
            }
            break;
        case st_main_hold:
            if(main_hold_time < 20){
                main_hold_time++;
                state = st_main_hold;
            } else {
                state = st_main_wait_new;
            }
            break;
        default:
            state = st_main_start;
            break;
    }//end main_task transitions/actions
    return state;
};
示例#7
0
int
main(int argc, char* argv[])
{
	volatile FATFS 		fatfs = {0};
	volatile FRESULT 	result;
	uint8_t 			fifo_command = 0;
	uint8_t				file_index = 0;



    ResetRCC();
    RCC_SetClockFrequency(PLLM_macro, PLLN_macro, PLLQ_macro, PLLP_macro);

    //  Enable clocks for the peripherals
    /*
     * 	GPIOA	-	NEC_CONTROLLER, TIM1 PWM, DAC
     * 	GPIOB	- 	SPI2
     * 	GPIOC	-	HD44780 Led Display
     * 	GPIOD	-	LED diodes, USART2(Log)
     */
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN |  RCC_AHB1ENR_GPIOCEN | RCC_AHB1ENR_GPIODEN;
    RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
    RCC->APB1ENR |= RCC_APB1ENR_TIM7EN | RCC_APB1ENR_SPI2EN | RCC_APB1ENR_USART2EN | RCC_APB1ENR_DACEN;


    /*< Configure board's leds to signal states */
    GPIO_OutputConfigure(GPIOD, PIN_12 | PIN_13 | PIN_14 | PIN_15, gpio_otyper_push_pull, gpio_speed_high, gpio_pupd_pull_down);
    /*< Configure NVIC Interrupt controller */
    //  Set two bits (out of four) as the main priority. The rest bits are used for preemptive priorities
    NVIC_SetPriorityGrouping(NVIC_PriorityGroup_2);
    NVIC_Enable_Interrupts();

    /*< Configure USART2 module to create program log */
    UART_Config(USART2, USART_CR1_UE | USART_CR1_TE, 19200,false);
    GPIO_AlternateFunctionPrepare(GPIOD, PIN_5, gpio_otyper_push_pull, gpio_speed_medium, gpio_pupd_pull_up);
    GPIO_AlternateFunctionSet(GPIOD,PIN_5, AF7);
    Log_Uart("##### LOG START #####\n\r");

    /*<	MCO2 Pin configuration to watch the CPU Clock signal with an oscilloscope*/
    Log_Uart("Clock output pin configuration in progress...\n\r");
    RCC->CFGR |= RCC_CFGR_MCO2PRE_2 | RCC_CFGR_MCO2PRE_1;
    RCC->CFGR &= ~RCC_CFGR_MCO2;
    GPIO_AlternateFunctionPrepare(GPIOC, PIN_9, gpio_otyper_push_pull, gpio_speed_fast, gpio_pupd_no_pull);
    GPIO_AlternateFunctionSet(GPIOC,PIN_9, AF0);

    /*<	SysTick configuration */
    Log_Uart("SysTick configuration in progress...\n\r");
    SysTick_Config(SYSTICK_CLK_DIVIDER);	//	Configure SysTick to make a tick every 1 us
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);


    /*< PWM signal configuration */
    Log_Uart("PWM generation module configuration in progress...\n\r");
    GPIO_AlternateFunctionPrepare(GPIOA, PIN_8, gpio_otyper_push_pull, gpio_speed_fast, gpio_pupd_pull_down);
    GPIO_AlternateFunctionSet(GPIOA, PIN_8, AF1);	//	Pin for PWM signal
    TIM_PWMConfigure(TIM1, 168, 5000, 4000, TIM_Channel_1);
    TIM_Start(TIM1);
    /*< HD44780 display configuration */
    LCD_Config();


    /*< Remote controller receiver initialization */
    Log_Uart("IR remote controller configuration in progress...\n\r");
    NEC_Remote_Init();
    TIM_Start(TIM6);

    /*< SPI Module configuration. It is used to communicate with the SD card */
    Log_Uart("SPI module configuration in progress...\n\r");
    SPI_Master_Init(SPI2, SPI_FREQ_PCLK_DIV_256, SPI_CPOL0_CPHA0, SPI_BIT_ORDER_MSB_FIRST, true);

    /*< DAC configuration */
    DAC_Init(dac_dual_channel_simultanous, dac_trigger_tim7, true);

    /*< Timer 7 used to trigger the DAC config */
    TIM_Basic_Continuous_Counting(TIM7, 12);

    /*< Remote Controller command fifo configuration */
    Log_Uart("FIFO configuration in progress...\n\r");
    Fifo_Init(&remote_command_fifo, remote_command_queue, sizeof(remote_command_queue));


    TIM6->CR1 |= TIM_CR1_CEN;	/*< Continuously ticking timer, used in NEC IR remote */

    Log_Uart("Configuration OK!\n\r");




    TCHAR disk[] = "0";
    UINT byte_number;
    result = f_mount(&fatfs, disk, 1);

    uint16_t bytes;
   /* result = SD_Find_File_Name_Containing("/", "*.wav");
    result = SD_Get_File_List("/");
    result = f_open(&sd_current_file, &sd_files_list[3], FA_READ);
    result = Wav_Get_File_Header(&sd_current_file);
*/
    //	Initially, get the files list
    state = STATE_GET_FILES_LIST;

  while(1)
  {
	  switch(state)
	  {
		 /* case STATE_WAIT:
			  break;*/

		  case	STATE_GET_FILES_LIST:
		  {
			  result = SD_Find_File_Name_Containing("/", "*.wav");
			  if(result == FR_NO_FILE)
			  {
				  LCD_WriteText("Brak plikow .wav");
				  // Go to sleep in this case
				  while(1)
				  {
					  __WFI();
				  }
			  }
			  else
			  {
				  LCD_WriteText(sd_files_list[0]);
			  }
			  state = STATE_EXECUTE_USER_REQUESTS;
			  break;
		  }

		  case STATE_READ_SAMPLES:
		  {
			  //	If we didn't get to the end of file yet...
			  if(!f_eof(&sd_current_file))
				  //	... Then read next sample chunk
				  f_read(&sd_current_file, empty_data_buf_ptr, 512, &read_data_byte_counter);
			  else
				  // 	...	Else set the end of file flag
				  wav_eof = true;
			  state = STATE_EXECUTE_USER_REQUESTS;
			  break;
		  }

		  case STATE_EXECUTE_USER_REQUESTS:
		  {
			  do
			  {
				  Fifo_Get(&remote_command_fifo, &fifo_command);

				  switch(fifo_command)
				  {
					  case NEC_CH_PLUS:
					  {
						  //	Disable when the wav_file is currently played
						  if(!wav_file_playing)
						  {
							  wav_file_chosen = false;
							  if(file_index < sd_number_of_files_in_dir-1)
								  file_index++;

							  LCD_WriteText(sd_files_list[file_index]);
						  }
						  break;
					  }
					  case NEC_CH_MINUS:
					  {
						  //	Disable when the wav_file is currently played
						  if(!wav_file_playing)
						  {
							  wav_file_chosen = false;
							  if(file_index > 0)
								  file_index--;
							  LCD_WriteText(sd_files_list[file_index]);
						  }
						  break;
					  }
					  case NEC_CH:
					  {
						  //	Disable when the wav_file is currently played
						  if(!wav_file_chosen)
						  {

							  //	If the file is already opened then close it
							  if(sd_current_file.fs != 0)
								  f_close(&sd_current_file);
							  //	...Open the chosen file
							  f_open(&sd_current_file, sd_files_list[file_index], FA_READ);
							  //	Get the chosen files header
							  WAV_Get_File_Header(&sd_current_file);
							  //	Prepare the triggering timer frequency
							 // WAV_Set_Trigger_Frequency(TIM7);
							  TIM_Set_Timer_Max_Count(TIM7, (uint16_t)(TIM7_FREQ/current_wave_header.byte_field.sample_rate));
							  //	Get the rest audio file info
							  //f_read(&sd_current_file, sd_data_buffer, current_wave_header.byte_field.subchunk_2_size, &read_data_byte_counter);
							  //	Get the first portion of data

							  //	Set the wav_file_chosen flag
							  wav_file_chosen = true;
							  //	Clear the wav_eof flag
							  wav_eof  = false;

						  }
						  break;
					  }
					  case NEC_PLAY_PAUSE:
					  {
						 if(!wav_file_playing && wav_file_chosen)
						 {
							//	Clear the timer's counter
							TIM_Clear(TIM7);
							TIM7->DIER |= TIM_DIER_UIE;
							empty_data_buf_ptr = sd_data_buffer;
							  f_read(&sd_current_file, sd_data_buffer, sizeof(sd_data_buffer), &read_data_byte_counter);

							  f_read(&sd_current_file, sd_data_buffer_additional, sizeof(sd_data_buffer_additional), &read_data_byte_counter);
							//	Start the DAC triggering timer
							TIM_Start(TIM7);
							//	Set the wav_file_playing_flag
							wav_file_playing = true;
						 }
						 else
						 {
							 //	Stop the DAC triggering timer
							 TIM_Stop(TIM7);

							 //	Clear the wav_file_playing_ flag
							 wav_file_playing = false;
						 }
						 break;
					  }

					  default:
						  break;
				  }
				  fifo_command = 0;
			  }while(!Fifo_Empty(&remote_command_fifo));
			  break;
		  }
	  }
  }
}
main()
{
   uint32 idata Current_directory, Entry_clus;
   uint16 idata i, num_entries, entry_num;
   uint8 idata error_flag;
   uint8 idata name_buf[8];

   /////////////////////
   // INIT
   /////////////////////
   AMBERLED=OFF;
   YELLOWLED=OFF;
   GREENLED=OFF;
   REDLED = ON;
   STA013_RESET=0;
   i=0;
   while(i<=60000) i++;
   REDLED = OFF;
   AUXR=0x0c;   // make all of XRAM available
   if(OSC_PER_INST==6)
   {
      CKCON0=0x01;  // set X2 clock mode
   }
   else if(OSC_PER_INST==12)
   {
      CKCON0=0x00;  // set standard clock mode
   } 

   // Init LCD
   LCD_Init();
   LCD_Print(10, "Sys. Init.");

   // UART Init
   uart_init();

   // I2C Test
   printf("I2C Test Program\n\r\n\n");
   error_flag=SPI_Master_Init(400000UL);
   if(error_flag!=no_errors)
   {
      REDLED=ON;
      while(1);
   }

   // SD Card Init
   printf("SD Card Initialization ... \n\r");
   error_flag=SD_card_init();
   if(error_flag!=no_errors)
   {
      REDLED=ON;
      while(1);
   }
   error_flag=SPI_Master_Init(20000000UL);
   if(error_flag!=no_errors)
   {
      REDLED=ON;
      while(1);
   }
   for(i=0;i<512;i++)
   {
      buf1[i]=0xff;  // erase valout for debug
      buf2[i]=0xff;
   }
   error_flag=Mount_Drive(buf1);
   if(error_flag!=no_errors)
   {
      REDLED=ON;
      while(1);
   }
   Current_directory=FirstRootDirSec_g;
   
    // Init MP3 Decoder
    STA013_init(); 
   
    //////////////////
    // Main Loop
	//////////////////
    while(1)
    {
		LCD_Write(COMMAND,clear_display);
        DELAY_MS_T0(5);
		LCD_Print(11, "Select Song");
        printf("Directory Sector = %lu\n\r",Current_directory);
        num_entries=Print_Directory(Current_directory, buf1);
	    printf("Enter Selection = ");
  	    entry_num=(uint16)long_serial_input();
	    if(entry_num<=num_entries)
	    {
	       Entry_clus=Read_Dir_Entry(Current_directory, entry_num, buf1, name_buf);
		   if(Entry_clus&directory_bit)
		   {
		     Entry_clus&=0x0FFFFFFF;
             Current_directory=First_Sector( Entry_clus );
		   }
 	        else // File Selected
		    {           
                // Start OS to play song
                seos_run( Entry_clus, name_buf );
		    }	  
	   }
	   else
	   {
	      printf("Invalid Selection\n\r");
	   }  
   }
}