Beispiel #1
0
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
	ui->setupUi(this);

	timer=new QTimer();
	uiTimer=new QTimer();
	connect(timer,SIGNAL(timeout()),this,SLOT(timeIsOut()));
	connect(uiTimer,SIGNAL(timeout()),this,SLOT(updateLcd()));

	hms t;
	t.h=t.m=t.s=t.ms=0;
	segTime.push_back(t);
	ui->listWidget->addItem(QString::number(ui->listWidget->count()+1));
	ui->spinBox->setEnabled(false);
	ui->spinBox_2->setEnabled(false);
	ui->spinBox_3->setEnabled(false);
	ui->progressBar->setValue(0);
	ui->progressBar_2->setValue(0);
}
/*******************************************************************************
*   @fn         runRX
*
*   @brief      Puts radio in RX and waits for packets. Function assumes
*               that status bytes are appended in the RX_FIFO
*               Update packet counter and display for each packet received.
*
*   @param      none
*
*   @return     none
*/
static void runRX(void) {

    uint8 rxBuffer[128] = {0};
    uint8 rxBytes;
    uint8 marcState;

    // Connect ISR function to GPIO2
    ioPinIntRegister(IO_PIN_PORT_1, GPIO2, &radioRxISR);

    // Interrupt on falling edge
    ioPinIntTypeSet(IO_PIN_PORT_1, GPIO2, IO_PIN_FALLING_EDGE);

    // Clear ISR flag
    ioPinIntClear(IO_PIN_PORT_1, GPIO2);

    // Enable interrupt
    ioPinIntEnable(IO_PIN_PORT_1, GPIO2);

    // Update LCD
    updateLcd();

    // Set radio in RX
    trxSpiCmdStrobe(CC120X_SRX);

    // Infinite loop
    while(TRUE) {

        // Wait for packet received interrupt
        if(packetSemaphore == ISR_ACTION_REQUIRED) {

            // Read number of bytes in RX FIFO
            cc120xSpiReadReg(CC120X_NUM_RXBYTES, &rxBytes, 1);

            // Check that we have bytes in FIFO
            if(rxBytes != 0) {

                // Read MARCSTATE to check for RX FIFO error
                cc120xSpiReadReg(CC120X_MARCSTATE, &marcState, 1);

                // Mask out MARCSTATE bits and check if we have a RX FIFO error
                if((marcState & 0x1F) == RX_FIFO_ERROR) {

                    // Flush RX FIFO
                    trxSpiCmdStrobe(CC120X_SFRX);
                } else {

                    // Read n bytes from RX FIFO
                    cc120xSpiReadRxFifo(rxBuffer, rxBytes);

                    // Check CRC ok (CRC_OK: bit7 in second status byte)
                    // This assumes status bytes are appended in RX_FIFO
                    // (PKT_CFG1.APPEND_STATUS = 1)
                    // If CRC is disabled the CRC_OK field will read 1
                    if(rxBuffer[rxBytes - 1] & 0x80) {

                        // Update packet counter
                        packetCounter++;
                    }
                }
            }

            // Update LCD
            updateLcd();

            // Reset packet semaphore
            packetSemaphore = ISR_IDLE;

            // Set radio back in RX
            trxSpiCmdStrobe(CC120X_SRX);
        }
    }
}
// main loop
int main(void){
	
	uint8_t stateDS18=DS18B20_STATUS_READY;
	
	#ifdef USE_WDT
	WDT_init(WDTO_8S); 
	#endif
	
	
	initGPIO();
	LCD_init();
	
	showLcdSplash();
		
	setLcdInitialFields();
		
	paramLoadFromEeprom();
		
	USART1_config(USART1_MY_UBBRN,USART_DATA_FORMAT_8BITS|USART_STOP_BITS_1,USART_TRANSMIT_ENABLE|USART_RECEIVE_ENABLE| USART_INTERRUPT_ENABLE);
	
	USART1_sendStr("Hello");
	
	schedulerInit();
	
	// check for the default values
	#ifdef USE_WDT
	WDT_init(WDTO_8S);
	#endif
	
	
	sei(); //enable interrups
	
	// loop while	
    while(1){
		
		
		// cintrol zone
		if(flagTaskControl){
			#ifdef USE_WDT
			WDT_reset();
			#endif
			
			
			LED_RUN_OFF;
			// fire the DS
			if(stateDS18==DS18B20_STATUS_READY){
				DS18B20_startConv();
				stateDS18=DS18B20_STATUS_CONV;
			
			// check if convertion ended	
			}else if(stateDS18==DS18B20_STATUS_CONV){
				if(DS18B20_convComplete()){
					stateDS18=DS18B20_STATUS_END_CONV;
				}
			
			// convertion ready	
			}else if (stateDS18==DS18B20_STATUS_END_CONV){
				LED_RUN_ON;
				currentTemp=DS18B20_getTemp();
				stateDS18=DS18B20_STATUS_READY;
				
				currentStatus = checkTempError(currentTemp,currentTempSetPoint,currentHistSetPoint); // chec the out
				setOutputRelay(currentStatus);
				_delay_ms(50);
				LED_RUN_OFF;
					
				
			}
			
			flagTaskControl=0;
		}
		
		// user bottons area
		if(flagTaskReadButtons){
			#ifdef USE_WDT
			WDT_reset();
			#endif
			
			uint8_t portVal = readButtons();
			uint8_t code = decodeButton(portVal);
			code = debounceKey(code);
			
			#ifdef MAIN_DEBUG
			sprintf(debugBuffer,"Key %d",code);
			USART1_sendStr(debugBuffer);
			#endif
			
			stateMachine(code); // go to machine
			
			flagTaskReadButtons=0;
		}
		
		 
		 // lcd update area
		 if(flagTaskUpdateLcd){
			 #ifdef USE_WDT
			 WDT_reset();
			 #endif
			 //showLcdSavedMessage();
			 updateLcd(); // update the lcd
			 
			 flagTaskUpdateLcd=0;
		 }
		 
		 // save to eeprom
		 if(flagSaveParametersEeprom){
			 #ifdef USE_WDT
			 WDT_reset();
			 #endif
			 
			 paramSavetoEeprom(); // save value sto eeprom
			 showLcdSavedMessage();
			 setLcdInitialFields();
			 updateLcd();
			 flagSaveParametersEeprom=0;
		 }
		 
    }
}
/******************************************************************************
 * @fn          runRX
 *
 * @brief       puts radio in RX and waits for packets. Function assumes
 *              that status bytes are appended in the RX_FIFO
 *              Update packet counter and display for each packet received.
 *                
 * @param       none
 *
 * @return      none
 */
void cc120xRunRX(void){
  
  uint8 rxBuffer[128] = {0};
  uint8 rxBytes;
  uint8 marcStatus;
  
    // Write radio registers
  registerConfig();
  
  // Connect ISR function to GPIO0, interrupt on falling edge
  trxIsrConnect(&radioRxTxISR);
  
  // Enable interrupt from GPIO_0
  trxEnableInt();
   
  // Update LCD
  updateLcd();

  // Set radio in RX
  trxSpiCmdStrobe(CC120X_SRX);

  // Loop until left button is pushed (exits application)
  while(BSP_KEY_LEFT != bspKeyPushed(BSP_KEY_ALL)){
    
    // Wait for packet received interrupt 
    if(packetSemaphore == ISR_ACTION_REQUIRED){
      
      // Read number of bytes in rx fifo
      cc120xSpiReadReg(CC120X_NUM_RXBYTES, &rxBytes, 1);
      
      // Check that we have bytes in fifo
      if(rxBytes != 0){
        
        // Read marcstate to check for RX FIFO error
        cc120xSpiReadReg(CC120X_MARCSTATE, &marcStatus, 1);
        
        // Mask out marcstate bits and check if we have a RX FIFO error
        if((marcStatus & 0x1F) == RX_FIFO_ERROR){
          
          // Flush RX Fifo
          trxSpiCmdStrobe(CC120X_SFRX);
        }
        else{
        
          // Read n bytes from rx fifo
          cc120xSpiReadRxFifo(rxBuffer, rxBytes);  
          
          // Check CRC ok (CRC_OK: bit7 in second status byte)
          // This assumes status bytes are appended in RX_FIFO
          // (PKT_CFG1.APPEND_STATUS = 1.)
          // If CRC is disabled the CRC_OK field will read 1
          if(rxBuffer[rxBytes-1] & 0x80){
            
            // Update packet counter
            packetCounter++;
          }
        }
      }
      
      // Update LCD
      updateLcd();
      
      // Reset packet semaphore
      packetSemaphore = ISR_IDLE;
      
      // Set radio back in RX
      trxSpiCmdStrobe(CC120X_SRX);
      
    }
  }
  // Reset packet counter
  packetCounter = 0;
  // Put radio to sleep and exit application
  trxSpiCmdStrobe(CC120X_SPWD);
}
/*******************************************************************************
*   @fn         main
*
*   @brief      Runs the main routine
*
*   @param      none
*
*   @return     none
*/
void main(void) {

    uint8 writeByte;

    // Initialize MCU and peripherals
    initMCU();

    // Write radio registers (preferred settings from SmartRF Studio)
    registerConfig();

    // Application specific registers
    // FIFO_THR = 120
    // GPIO0 = RXFIFO_THR
    // GPIO2 = PKT_SYNC_RXTX
    // GPIO3 = PKT_SYNC_RXTX
    writeByte = INFINITE_PACKET_LENGTH_MODE;
    cc112xSpiWriteReg(CC112X_PKT_CFG0, &writeByte, 1);
    writeByte = 0x78; cc112xSpiWriteReg(CC112X_FIFO_CFG, &writeByte, 1);
    writeByte = 0x00; cc112xSpiWriteReg(CC112X_IOCFG0,   &writeByte, 1);
    writeByte = 0x06; cc112xSpiWriteReg(CC112X_IOCFG2,   &writeByte, 1);
    writeByte = 0x06; cc112xSpiWriteReg(CC112X_IOCFG3,   &writeByte, 1);

    bspLedSet(BSP_LED_ALL);
    
    // Calibrate the radio according to the errata note
    manualCalibration();

    // Connect ISR function to GPIO0
    ioPinIntRegister(IO_PIN_PORT_1, GPIO0, &rxFifoAboveThresholdISR);

    // Interrupt on falling edge
    ioPinIntTypeSet(IO_PIN_PORT_1, GPIO0, IO_PIN_RISING_EDGE);

    // Clear interrupt
    ioPinIntClear(IO_PIN_PORT_1, GPIO0);

    // Enable interrupt
    ioPinIntEnable(IO_PIN_PORT_1, GPIO0);

    // Connect ISR function to GPIO2
    ioPinIntRegister(IO_PIN_PORT_1, GPIO2, &syncReceivedISR);

    // Interrupt on falling edge
    ioPinIntTypeSet(IO_PIN_PORT_1, GPIO2, IO_PIN_RISING_EDGE);

    // Clear interrupt
    ioPinIntClear(IO_PIN_PORT_1, GPIO2);

    // Enable interrupt
    ioPinIntEnable(IO_PIN_PORT_1, GPIO2);

    // Set up interrupt on GPIO3 (PKT_SYNC_RXTX)
    ioPinIntRegister(IO_PIN_PORT_1, GPIO3, &packetReceivedISR);

    // Interrupt on falling edge
    ioPinIntTypeSet(IO_PIN_PORT_1, GPIO3, IO_PIN_FALLING_EDGE);

    printWelcomeMessage();

    while (TRUE) {
        switch (state) {

            //------------------------------------------------------------------
            case RX_START:
            //------------------------------------------------------------------
            trxSpiCmdStrobe(CC112X_SRX);
            pBufferIndex = rxBuffer;

            // Disable interrupt on GPIO3
            ioPinIntDisable(IO_PIN_PORT_1, GPIO3);

            state = RX_WAIT;

            //------------------------------------------------------------------
            case RX_WAIT:
            //------------------------------------------------------------------
            if (packetReceived) {
                packetReceived = FALSE;

                // Check CRC and update LCD if CRC OK
                if ((rxBuffer[packetLength + 3]) & CRC_OK)
                    updateLcd();

                // Change to infinite packet length mode
                pktFormat = INFINITE;
                writeByte = INFINITE_PACKET_LENGTH_MODE;
                cc112xSpiWriteReg(CC112X_PKT_CFG0, &writeByte, 1);

                state = RX_START;
            }
            break;

            //------------------------------------------------------------------
            default:
            //------------------------------------------------------------------

            break;
        }
    }
}