Exemplo n.º 1
0
void interrupt high_isr (void) {
    if (PIR1bits.RCIF == 1) {
        data = getcUSART();
        dataFlag = 1;
        PIR1bits.RCIF = 0;
    }
}
Exemplo n.º 2
0
void terminalTask() {
        if (DataRdyUSART()) {\
            while(BusyUSART());
            cmd = getcUSART();
            while(BusyUSART());
            putcUSART(cmd);
        }
    
        switch (cmd) {
        case '?':
            sendTerminalCommandList();
            break;
        case '1':
            terminalSendPString(TERMINAL_RETURN);
            terminalSendPString("Measuring carbon...");
            measureCarbon();
            terminalSendPString(TERMINAL_RETURN);
            updateTerminal();
            break;
        case '2':
            terminalSendPString(TERMINAL_RETURN);
            terminalSendPString("Measuring salinity...");
            measureSalinity();
            terminalSendPString(TERMINAL_RETURN);
            updateTerminal();
            break;
        case '3':
            terminalSendPString(TERMINAL_RETURN);
            terminalSendPString("Measuring flow rate...");
            measureFlowRate();
            terminalSendPString(TERMINAL_RETURN);
            updateTerminal();
            break;
        case '4':
            terminalSendPString(TERMINAL_RETURN);
            terminalSendPString("Measuring temperature...");
            measureTemperature();
            terminalSendPString(TERMINAL_RETURN);
            updateTerminal();
            break;
        case 'c':
            updateTerminal();
            break;
        case 'd':
            terminalSendPString(TERMINAL_RETURN);
            terminalSendPString("Revealing display");
            sendTerminalCommandLine();
            break;
        case 0xff:
            break;
        default:
            terminalSendPString(TERMINAL_RETURN);
            terminalSendPString("Invalid command");
            sendTerminalCommandLine();
        }
        cmd = 0xff;
}
Exemplo n.º 3
0
void ProcessIO(void)
{	
	uchar i;
	BlinkUSBStatus();		//Blink	the	LEDs according to the USB device status
	// User	Application	USB	tasks
	if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1))	return;

	// only check	for	new	USB	buffer if the old RS232	buffer is
	// empty.	 This will cause additional	USB	packets	to be NAK'd

	// RS232_送信バッファに HostPC から届いたデータをFillする.
	if( mDataRdyUSART() < 32 ) 

	if (LastRS232Out == 0) {						  
		LastRS232Out = getsUSBUSART(RS232_Out_Data,64);	//until	the	buffer is free.
		if(LastRS232Out	> 0) {
//			RS232_Out_Data_Rdy = 1;	 //	signal buffer full
			RS232cp	= 0;  // Reset the current position
		}
	}

#if	USART_USE_TX_INTERRUPT		// 送信割り込みを使用する.
	//USARTが送信可であれば RS232_送信バッファの内容を USARTに 1文字送る.
	if(LastRS232Out && mTxRdyUSART())	{
//		PIR1bits.TXIF = 0;			// 送信割り込みフラグ.
		PIE1bits.TXIE = 1;			// 送信割り込み許可.
	}
#else
	//USARTが送信可であれば RS232_送信バッファの内容を USARTに 1文字送る.
	if(LastRS232Out && mTxRdyUSART())	{
		putcUSART(RS232_Out_Data[RS232cp]);	//1文字送る.
		++RS232cp;
		if (RS232cp	== LastRS232Out) {
//			RS232_Out_Data_Rdy = 0;
			LastRS232Out=0;
			RS232cp=0;
		}
	}
#endif

	//可能なら、RS232_受信バッファの内容をHostPCに送る.
	if((USBUSARTIsTxTrfReady())	&& (mDataRdyUSART())){
		i=0;
		while( mDataRdyUSART() ) {
			if(i>=CDC_DATA_OUT_EP_SIZE) break;
			USB_Out_Buffer[i++]=getcUSART();
		}
		putUSBUSART(&USB_Out_Buffer[0],	i);
		NextUSBOut = 0;
	}

	CDCTxService();
}
Exemplo n.º 4
0
void Rx_Mensagem(void)
{
	/*while(PIR1bits.RCIF==0); // Wait until RCIF gets low
	data[j++] = RCREG;*/
	
     if(PIE1bits.RCIE && PIR1bits.RCIF)
     {
		timer = 0;
        while(!DataRdyUSART());
		data[j++] = getcUSART();
		
		PIR1bits.RCIF = 0;
    }
}
Exemplo n.º 5
0
void getsUSART(char *buffer, unsigned char len)
{
  char i;    // Length counter
  unsigned char data;

  for(i=0;i<len;i++)  // Only retrieve len characters
  {
    while(!DataRdyUSART());// Wait for data to be received

    data = getcUSART();    // Get a character from the USART
                           // and save in the string
    *buffer = data;
    buffer++;              // Increment the string pointer
  }
}
Exemplo n.º 6
0
void interrupt SerialRxPinInterrupt(void)
{
    unsigned char data;
    //check if the interrupt is caused by RX pin
    if(PIR1bits.RCIF == 1)
    {
    do{    
    while(!DataRdyUSART());// Wait for data to be received

    data = getcUSART();    // Get a character from the USART
    reception[i] = data;
    i++;              
    }while(PIR1bits.RCIF);
    }
}
Exemplo n.º 7
0
void ProcessIO(void)
{	
	uchar cnt;
	//Blink	the	LEDs according to the USB device status
	BlinkUSBStatus();
	// User	Application	USB	tasks
	if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1))	return;

	// only check	for	new	USB	buffer if the old RS232	buffer is
	// empty.	 This will cause additional	USB	packets	to be NAK'd

	// RS232_送信バッファに HostPC から届いたデータをFillする.
	if (LastRS232Out == 0) {						  
		LastRS232Out = getsUSBUSART(RS232_Out_Data,64);	//until	the	buffer is free.
		if(LastRS232Out	> 0) {
			RS232cp	= 0;  // Reset the current position
		}
	}

	for(cnt=0;cnt<32;cnt++) {

		//USARTが送信可であれば RS232_送信バッファの内容を USARTに 1文字送る.
		if(LastRS232Out && mTxRdyUSART())	{
			putcUSART(RS232_Out_Data[RS232cp]);	//1文字送る.
			++RS232cp;
			if (RS232cp	== LastRS232Out) {
				LastRS232Out=0;
				RS232cp=0;
			}
		}

		//USARTがデータを受信済みであれば RS232_受信バッファに溜める.
		if(mDataRdyUSART()) {
			USB_Out_Buffer[NextUSBOut] = getcUSART();
			++NextUSBOut;
			//USB_Out_Buffer[NextUSBOut] = 0;
		}

	}

	//可能なら、RS232_受信バッファの内容をHostPCに送る.
	if((USBUSARTIsTxTrfReady())	&& (NextUSBOut > 0)){
		putUSBUSART(&USB_Out_Buffer[0],	NextUSBOut);
		NextUSBOut = 0;
	}

	CDCTxService();
}
Exemplo n.º 8
0
void Read_string(unsigned char *buffer, unsigned char len)      /* Reads a string data from UART of having specific length*/
  {
  char i;
  unsigned char data;
      Write(0x0D);
      Write('\n');
  for(i=0;i<len;i++)
    {
    while(!DataRdyUSART());                                          /* Wait for data to be received                           */
    data = getcUSART();                                              /* Get a character from the USART                         */
    *buffer = data;                                                  /* save data in a string                                  */
    WriteUSART(data);
    while(BusyUSART());
    buffer++;                                                        /* Increment the string pointer                           */
    }
  }
Exemplo n.º 9
0
/********************************************************************
 * Function:        void ProcessIO(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function is a place holder for other user
 *                  routines. It is a mixture of both USB and
 *                  non-USB tasks.
 *
 * Note:            None
 *******************************************************************/
void ProcessIO(void)
{   
    //Blink the LEDs according to the USB device status
    BlinkUSBStatus();
    // User Application USB tasks
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;

	// only check for new USB buffer if the old RS232 buffer is
	// empty.
	// Additional USB packets will be NAK'd
	// until the buffer is free.
	if (RS232_Out_Data_Rdy == 0)
		{
			LastRS232Out = getsUSBUSART(RS232_Out_Data,64);
			
			if(LastRS232Out > 0)
			{
				RS232_Out_Data_Rdy = 1; // signal
										//buffer full
				RS232cp = 0;// Reset the current position
			}
		}
		
	if(RS232_Out_Data_Rdy && mTxRdyUSART())
	{
		putcUSART(RS232_Out_Data[RS232cp]);
		++RS232cp;
		if (RS232cp == LastRS232Out)
			RS232_Out_Data_Rdy = 0;
	}

	if(mDataRdyUSART())
	{
		USB_Out_Buffer[NextUSBOut] = getcUSART();
		++NextUSBOut;
		USB_Out_Buffer[NextUSBOut] = 0;
	}

	if((mUSBUSARTIsTxTrfReady()) && (NextUSBOut > 0))
	{
		putUSBUSART(&USB_Out_Buffer[0], NextUSBOut);
		NextUSBOut = 0;
	}

    CDCTxService();
}		//end ProcessIO
Exemplo n.º 10
0
void uartLoopbackSM(LoopbackData *data){

  switch(data->state){
    case WAIT_DATA:
      if(DataRdyUSART() == 0)
        break;
      data->dataByte = getcUSART();
      data->state = WAIT_TO_TX;
      break;

    case WAIT_TO_TX:
      if(BusyUSART() == 1)
        break;
      putcUSART(data->dataByte);
      data->state = WAIT_DATA;
      break;
      
    default:
      break;
  }
}
Exemplo n.º 11
0
void interrupcao()
{
if (serial_interrompeu)  {  serial_interrompeu=0; avisa_interrompeu=1;

    inputstr[str_pos] = getcUSART(); //ou le_serial - vai buscar a inputstr à porta série caracter a caracter (em cada iterada)
    str_pos++;//incrementa a posição de escrita na string de destino
    if(str_pos == 30){str_pos = 0;}  //verifica se a string de 80 caracteres
   //está cheia e caso o esteja faz reset da posição de escrita na string a 0
 
//comando = le_serial();//apenas um byte

    switch (inputstr[0]) //teste da comunicacao serial no Terminal
      {
case 'L' : {pisca=0; nivel_alto(pin_b7);putrsUSART ( (const far rom char *) " Ligado\r\n");while (envia_byte());} //Recebe A e Transmite byte B para teste
break;
case 'D' : {pisca=0; nivel_baixo(pin_b7);putrsUSART ( (const far rom char *) " Desligado");while (envia_byte());}
break;
case 'P' : {pisca=1;  printf_text(Txdata); printf_data(10);
           }
break;
     }

                            }
}
Exemplo n.º 12
0
/********************************************************************
 * Function:        void ProcessIO(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function is a place holder for other user
 *                  routines. It is a mixture of both USB and
 *                  non-USB tasks.
 *
 * Note:            None
 *******************************************************************/
void ProcessIO(void)
{   
    //Blink the LEDs according to the USB device status
    BlinkUSBStatus();
    // User Application USB tasks
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;

	if (RS232_Out_Data_Rdy == 0)  // only check for new USB buffer if the old RS232 buffer is
	{						  // empty.  This will cause additional USB packets to be NAK'd
		LastRS232Out = getsUSBUSART(RS232_Out_Data,64); //until the buffer is free.
		if(LastRS232Out > 0)
		{	
			RS232_Out_Data_Rdy = 1;  // signal buffer full
			RS232cp = 0;  // Reset the current position
		}
	}

    //Check if one or more bytes are waiting in the physical UART transmit
    //queue.  If so, send it out the UART TX pin.
	if(RS232_Out_Data_Rdy && mTxRdyUSART())
	{
    	#if defined(USB_CDC_SUPPORT_HARDWARE_FLOW_CONTROL)
        	//Make sure the receiving UART device is ready to receive data before 
        	//actually sending it.
        	if(UART_CTS == USB_CDC_CTS_ACTIVE_LEVEL)
        	{
        		putcUSART(RS232_Out_Data[RS232cp]);
        		++RS232cp;
        		if (RS232cp == LastRS232Out)
        			RS232_Out_Data_Rdy = 0;
    	    }
	    #else
	        //Hardware flow control not being used.  Just send the data.
    		putcUSART(RS232_Out_Data[RS232cp]);
    		++RS232cp;
    		if (RS232cp == LastRS232Out)
    			RS232_Out_Data_Rdy = 0;	    
	    #endif
	}

    //Check if we received a character over the physical UART, and we need
    //to buffer it up for eventual transmission to the USB host.
	if(mDataRdyUSART() && (NextUSBOut < (CDC_DATA_OUT_EP_SIZE - 1)))
	{
		USB_Out_Buffer[NextUSBOut] = getcUSART();
		++NextUSBOut;
		USB_Out_Buffer[NextUSBOut] = 0;
	}
	
	#if defined(USB_CDC_SUPPORT_HARDWARE_FLOW_CONTROL)
    	//Drive RTS pin, to let UART device attached know if it is allowed to 
    	//send more data or not.  If the receive buffer is almost full, we 
    	//deassert RTS.
    	if(NextUSBOut <= (CDC_DATA_OUT_EP_SIZE - 5u))
    	{
            UART_RTS = USB_CDC_RTS_ACTIVE_LEVEL;
        }   	
        else
        {
        	UART_RTS = (USB_CDC_RTS_ACTIVE_LEVEL ^ 1);
        }    
    #endif	

    //Check if any bytes are waiting in the queue to send to the USB host.
    //If any bytes are waiting, and the endpoint is available, prepare to
    //send the USB packet to the host.
	if((USBUSARTIsTxTrfReady()) && (NextUSBOut > 0))
	{
		putUSBUSART(&USB_Out_Buffer[0], NextUSBOut);
		NextUSBOut = 0;
	}

    CDCTxService();

}//end ProcessIO
Exemplo n.º 13
0
void main()
{
    int i;

    TRISAbits.TRISA0 = 1;   //Direciona o pino 0 da porta A como entrada
    TRISAbits.TRISA3 = 1;   //Direciona o pino 3 da porta A como entrada
    TRISCbits.TRISC2 = 0;	//Direciona o pino 2 da porta C como saída (Aquecimento)
    TRISAbits.TRISA4 = 1;   //Direciona o pino 4 da porta A como entrada (B2)

    OpenUSART(USART_TX_INT_OFF		//desabilita interrupção de transmissão
              & USART_RX_INT_OFF	//desabilita interrupção de recepção
              & USART_ASYNCH_MODE	//modo assíncrono
              & USART_EIGHT_BIT		//transmissão e recepção em 8 bits
              & USART_BRGH_HIGH,	//Baud Rate em alta velocidade
              25);					//SPBRG p/ 19200 bps

    ADCON2 = 0b10100001;     /*ADFM1 = 1 -> Resultado da conversão AD
                                    ... justificado à direita
                                    -
                                    ****Velocidade de aquisição em 8TAD
                                    ACQT2 = 1
                                    ACQT1 = 0
                                    ACQT0 = 0
                                    ****Fonte de clock em Fosc/8
                                    ADCS2 = 0
                                    ADCS1 = 0
                                    ADCS0 = 1*/


    ADCON1 = 0b00001011;      /* -
                           	 -
                             VCFG1 = 0 -> Vref- = terra
                             VCFG0 = 1 -> Vref+ = VDD
                             *****Seleciona os canais AN0 e AN3 como analógicos
                             PCFG3 = 1
                             PCFG2 = 1
                             PCFG1 = 0
                             PCFG0 = 1*/

    msg_inicial();


    while(1)
    {
        while(!DataRdyUSART());  //Aguarda a chegada de um caractere no buffer de recepção

        rec[i] = getcUSART();	    //recebe o caractere e armazena no índice n_dado da
        //matriz n_dado

        putcUSART(rec[i]);
        Delay10KTCYx(1);   	//Gera um delay de 5ms
        i++;

        if(rec[i-1]==0x0D)
        {
            trata_serial();
            i=0;
            msg_inicial();
        }



    }
}
Exemplo n.º 14
0
void isr (void)
{
	///////////////////////////////////////////////////////////////////////////////
	//******************************** USART ************************************//
	///////////////////////////////////////////////////////////////////////////////
	if(PIR1bits.RCIF == 1){
		PIR1bits.RCIF = 0;
		byte_USART = getcUSART();
		if(byte_USART == EOM && SOM_int == 1){ // verifica se EOM
			EOM_int = 1;
			SOM_int = 0;
		}
		if(SOM_int == 1){ // verifica se ja houve SOM
			if(index < 4){
				msg_rec[index] = byte_USART;
				index++;
			}
		}
		if(byte_USART == SOM){ // verifica se SOM
			SOM_int = 1;
			index = 0;
			for(i = 0; i < 4; i++){
				msg_rec[i] = 0;
			}
		}
		if(EOM_int == 1){
			EOM_int = 0;
			protocolo_OK = 1;
		}
	}

	///////////////////////////////////////////////////////////////////////////////
	//******************************** LVD **************************************//
	///////////////////////////////////////////////////////////////////////////////
	if(PIR2bits.LVDIF == 1){
		escrita_EEPROM_interna(0x07, seconds);
		while(1);
	}

	///////////////////////////////////////////////////////////////////////////////
	//******************************** Relógio **********************************//
	///////////////////////////////////////////////////////////////////////////////
	if(PIR1bits.TMR1IF == 1){ // timer interrupt
		WriteTimer1( 0x8000 ); // reload timer: 1 second
		PIR1bits.TMR1IF = 0; /* clear flag to avoid another interrupt */
		segundos_mudou = 1;
		ler_sensores=1;
		seconds++;
		if(disp_ahoras == 0){
			disp_ahoras = 1;
		}
		if(seconds == 60){
			seconds = 0;
			minutos_mudou = 1;
			minutes++;
			if(minutes == 60){
				minutes = 0;
				horas_mudou = 1;
				hours++;
				if(hours == 24){
					hours = 0;
				}
			}
	  }
	}

	///////////////////////////////////////////////////////////////////////////////
	//***************************** Botão S3 ************************************//
	//**************** Entrar ou sair do modo de modificação ********************//
	//************************** Mudar o cursor *********************************//
	///////////////////////////////////////////////////////////////////////////////
	if (INTCONbits.INT0IF == 1){ // button S3 interrupt
		INTCONbits.INT0IF = 0;
		//WriteCmdXLCD( CURSOR_OFF );// Enable display with no cursor
		if(alarme_OFF == 0){ //alarmes estao ligados
			alarme_OFF = 1; //desligar alarmes
			alarme_lum_ON = 0;
			alarme_temp_ON = 0;
			alarme_clock_ON = 0;
			desliga_alarmes = 1;
		}
		else{
			if(cursor_pos == 4 && change_A == 1){
				if(change_AH == 1){ // acabei de mudar as horas do alarme
					change_AM = 1;
					change_AH = 0;
					SetDDRamAddr(0x03);
				}
				else if(change_AM == 1){ // acabei de mudar os minutos do alarme
					change_AS = 1;
					change_AM = 0;
					SetDDRamAddr(0x06);
				}
				else if(change_AS == 1){ // acabei de mudar os segundos do alarme
					change_AS = 0;
					change_A = 0; // ja defini o alarme do relogio
					disp_ahoras = 1;
					cursor_pos = 5;
					mudei_ahoras = 1; //flag para EEPROM
				}
			}
			else{
				cursor_pos++;
				if (cursor_pos >=1 && cursor_pos <= 8){
					modo_modificacao = 1;
					if(cursor_pos==8){
						d_a_alarmes = 1;
					}
					if(change_L == 1){ //acabei de definir a luminosidade do alarme
						change_L = 0;
						mudei_alum = 1;
					}
					if(change_T == 1){ //acabei de definir a temperatura do alarme
						change_T = 0;
						mudei_atemp = 1;
					}
				}
				else{
					modo_modificacao = 0; //sair do modo de modificacaoo
					sai_modificacao = 1; //acabei de sair do modo de modificacao
					cursor_pos = 0;
				}
				if (modo_sleep == 1){
					modo_sleep = 0; //sair do modo sleep
					cursor_pos = 0;	//nao estamos em modo modificacao
				}
			}
		}
	}
}
Exemplo n.º 15
0
/********************************************************************
 * Function:        void ProcessIO(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function is a place holder for other user
 *                  routines. It is a mixture of both USB and
 *                  non-USB tasks.
 *
 * Note:            None
 *******************************************************************/
void ProcessIO(void)
{
    BYTE numBytesRead;

    //Blink the LEDs according to the USB device status
    BlinkUSBStatus();
    // User Application USB tasks
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;

//    if(buttonPressed)
//    {
//        if(stringPrinted == FALSE)
//        {
//            if(mUSBUSARTIsTxTrfReady())
//            {
//                putrsUSBUSART("Button Pressed -- \r\n");
//                stringPrinted = TRUE;
//            }
//        }
//    }
//    else
//    {
//        stringPrinted = FALSE;
//    }
//
//    if(mUSBUSARTIsTxTrfReady())
//    {
//		numBytesRead = getsUSBUSART(USB_Out_Buffer,64);
//		if(numBytesRead != 0)
//		{
//			BYTE i;
//
//			for(i=0;i<numBytesRead;i++)
//			{
//				switch(USB_Out_Buffer[i])
//				{
//					case 0x0A:
//					case 0x0D:
//						USB_In_Buffer[i] = USB_Out_Buffer[i];
//						break;
//					default:
//						USB_In_Buffer[i] = USB_Out_Buffer[i] + 1;
//						break;
//				}
//
//			}
//
//			putUSBUSART(USB_In_Buffer,numBytesRead);
//		}
//	}

    //Check if we received a character over the physical UART, and we need
    //to buffer it up for eventual transmission to the USB host.
    if(mDataRdyUSART() && (NextUSBOut < (CDC_DATA_OUT_EP_SIZE - 1)))
    {
        USB_Out_Buffer[NextUSBOut] = getcUSART();
        ++NextUSBOut;
        USB_Out_Buffer[NextUSBOut] = 0;
    }

    //Check if any bytes are waiting in the queue to send to the USB host.
    //If any bytes are waiting, and the endpoint is available, prepare to
    //send the USB packet to the host.
    if((USBUSARTIsTxTrfReady()) && (NextUSBOut > 0))
    {
        putUSBUSART(&USB_Out_Buffer[0], NextUSBOut);
        NextUSBOut = 0;
    }

    CDCTxService();
}		//end ProcessIO