Пример #1
0
int main()
{
	PINSEL0 = 0x00000000;				//P0.6 como GPIO
	PINSEL1 = 0x00000000;				//P0.28 como ADC0
	IODIR0  = 0x00020000;				//P0.6 como salida
		
	configTimer();
	configExcepcion();
		
		
	T0TCR = 0x01;

	
	while(1);
}
Пример #2
0
void main(void) {
	//setupOsc();
	//RD2 = 1;
	configSystem();
	configSoftPWM();
	configTimer();
	configI2C();
//	configADC();
	//adcOff();
	
	while (1) {	
		
		//**PID Current control**
		// Read current on channel
		//		1. Read ADC value
		//		2. Scale to voltage
		// 		3. V*100=I
		// Calculate Error between current I(current) and setpoint
		//		1. Scale setpoint to 0 - Max current
		//			1. Get Max Current (This needs to be done on init)
		//		2. Err = Setpoint - Current Current
		//		3. Err * Pi = Pterm
		// dutyCycle += Pterm
		
		//Operate on received command
		if(newData){
			switch(i2c_rx_buff[0]){
				/****** Set Motor 1 Speed ********/
				case 0x00:
					if (i2c_rx_buff[1] > 32) {
						dutyCycle2 = i2c_rx_buff[1] - 32;
						dutyCycle1 = 0;
					}
					else{
						dutyCycle1 = 32 - i2c_rx_buff[1];
						dutyCycle2 = 0;
					}
					newData = 0;
				break;
				/****** Set Motor 2 Speed ********/
				case 0x01:
					if (i2c_rx_buff[1] > 32) {
						dutyCycle4 = i2c_rx_buff[1] - 32;
						dutyCycle3 = 0;
					}
					else{
						dutyCycle3 = 32 - i2c_rx_buff[1];
						dutyCycle4 = 0;
					}
					newData = 0;
				break;
				/****** Read Motor 1 Speed ********/
				case 0x10:
					i2c_tx_buff[0] = dutyCycle1;
				break;
				/****** Read Motor 2 Speed ********/
				case 0x20:
					i2c_tx_buff[0] = dutyCycle2;
				break;
				/********* Read ADC ***************/
				case 100:
					i2c_tx_buff[0] = readADC();
				break;
				/****** Enable ADC ****************/
				case 0x99:
					potEnabled = i2c_rx_buff[1];
				break;
				default:
					//RB4 = 1;
					newData = 0;
				break;
			}

		}
		//Unsupported command
		//else RB4 = 1;
		//dutyCycle1 = i2c_rx_buff[0];
	}
}	
int main(int argc, char** argv) {
    unsigned short x = 0; //Blinking led counter 
    SerialFrame frame;

    /* Port and interupts configurations */
    configClock();
    configPorts();
    configInterruptPriority();
    configTimer();

    /* Initialize the serial port */
    Serial_configSerialPort();

    /* Preparing the frame */
    CyclicBitBuffer_init();

    RF433_init();
    
    /* The frame content is assigned with the cyclicBitBuffer */
    frame.content = CyclicBitBuffer_getBuffer();
    
    /* The micro initilisation is over, let's begin the main loop */
    while(1) {

        while( 0 == Serial_readFrame(&frame)) {
             x++;
             if(CyclicBitBuffer_flags.recordOver) {
                 frame.content = CyclicBitBuffer_getBuffer();
                 frame.lenght = CyclicBitBuffer_size();
                 Serial_sendFrame(&frame);
                 CyclicBitBuffer_flags.recordOver = 0;
             }

             if(0 == x) {
                PIN_DEBUG_OUTPUT ^= 1;
             }
        }

        disableInterupts();
        CyclicBitBuffer_clearEvent();
       
        /* Realise the frame management */

        if(DOM_RECORD == frame.header) {//record
            //Delay1KTCYx(255);
            frame.header = DOM_RECORD;
            RF433_receive(&(frame.lenght));
            
        } else if(DOM_TRANSMIT == frame.header) { //transmit;
            RF433_transmit();
            frame.header = DOM_TRANSMIT;
            frame.lenght = 0;
            Serial_sendFrame(&frame);

        } else if(DOM_ECHO == frame.header) {
            frame.header = DOM_ECHO_REPLY;
            Serial_sendFrame(&frame);
        } else {
            frame.header = DOM_ERROR;
            frame.lenght = 0;
            Serial_sendFrame(&frame);
        }

    }
}
Пример #4
0
void main(void){
    TRISDbits.TRISD3 = 0;
    TRISDbits.TRISD4 = 0;
    //timer
    configTimer();
    
    //ad
    configADInterrupt();
    configAD();
    
    //usart
    configRS232();
    configUSARTInterrupts();    
    
    //I2C
    InitI2C_Master();
    
    //interrupts    
    selectChanel(channel);
    
    for(int i = 0; i < 100; i++)
        __delay_ms(10);
    PORTDbits.RD4 = 0;
    for(int i = 0; i < 100; i++)
        __delay_ms(10);
    PORTDbits.RD4 = 1;
    for(int i = 0; i < 100; i++)
        __delay_ms(10);
    PORTDbits.RD4 = 0;
    for(int i = 0; i < 100; i++)
        __delay_ms(10);
    PORTDbits.RD4 = 1;
    for(int i = 0; i < 100; i++)
        __delay_ms(10);
    PORTDbits.RD4 = 0;
    
    srand(TMR0);
    //generate secret number between maxCont and minCont
    contTimer0 = (rand() % (maxCont + minCont)) + minCont;
    
    write_RTC_I2c();
    while (1){

        if(contLedRD4 == TIMER0_INTERRUPTS_PER_SECOND){
            val++;
            if(val == contTimer0){
                ADCON0bits.GO_DONE = 1;
                //Leitura de Posição de data no DS-1307.
                read_RTC_I2c(0x04);
                dia = leitura;            
                __delay_ms(10);

                //Leitura de Posição de mês no DS-1307.
                read_RTC_I2c(0x05);
                mes = leitura;            
                __delay_ms(10);

                //Leitura de Posição de ano no DS-1307.
                read_RTC_I2c(0x06);
                ano = leitura;            
                __delay_ms(10);

                //Leitura de Posição da Hora no DS-1307.
                read_RTC_I2c(0x02);
                hora = leitura;            
                __delay_ms(10);

                //Leitura de Posição de Minuto no DS-1307.
                read_RTC_I2c(0x01);
                minuto = leitura;
                __delay_ms(10);

                //Leitura de Posição de Segundo no DS-1307.
                read_RTC_I2c(0x00);
                segundo = leitura;


                //"id&valor&ano-mes-dia hora:minuto:segundo"
                printf("%u&%u&20%02X-%02X-%02X %02X:%02X:%02X", id, data_ADC, ano, mes, dia, hora, minuto, segundo);

                __delay_ms(10);
                //TODO tirar daqui
                printf("\n");
                
                val = 0;
                contTimer0 = (rand() % (maxCont + minCont)) + minCont;
            }
            led1 = changing;
            contLedRD4 = 0;
        }
    }
}
Пример #5
0
void main(void){
    TRISDbits.TRISD3 = 0;
    //timer
    configTimer();
    
    //ad
    configADInterrupt();
    configAD();
    
    //usart
    configRS232();
    configUSARTInterrupts();    
    
    //I2C
    InitI2C_Master();
    
    //interrupts    
    selectChanel(channel);
    ADCON0bits.GODONE = 1;
    
    
    write_RTC_I2c();
    //ligando o timer 0    
    TMR0ON = 1;
    
    while (1){

        if(val == 30){                        
            //Leitura de Posição de data no DS-1307.
            read_RTC_I2c(0x04);
            dia = leitura;            
            __delay_ms(10);
            
            //Leitura de Posição de mês no DS-1307.
            read_RTC_I2c(0x05);
            mes = leitura;            
            __delay_ms(10);
            
            //Leitura de Posição de ano no DS-1307.
            read_RTC_I2c(0x06);
            ano = leitura;            
            __delay_ms(10);
            
            //Leitura de Posição da Hora no DS-1307.
            read_RTC_I2c(0x02);
            hora = leitura;            
            __delay_ms(10);

            //Leitura de Posição de Minuto no DS-1307.
            read_RTC_I2c(0x01);
            minuto = leitura;
            __delay_ms(10);

            //Leitura de Posição de Segundo no DS-1307.
            read_RTC_I2c(0x00);
            segundo = leitura;
            
            
            //"id&valor&ano-mes-dia hora:minuto:segundo"
            printf("%u&%u&20%02X-%02X-%02X %02X:%02X:%02X", id, data_ADC, ano, mes, dia, hora, minuto, segundo);

            __delay_ms(10);
            //TODO tirar daqui
            printf("\n");
            val = 0;
        }
    }
}