Esempio n. 1
0
int main()
{
	uint16_t potentiometerValue;		//Compare Potentiometer Value to certain number ranges
	initADC0();							//Initialize ADC

	DDRD=0x00;		//Port D pins as input
	PORTD=0xFF;		//Enable internal pull ups
	DDRB=0xFF;		//Set PORTB1 pin as output
	
	//Output compare OC1A 8 bit non inverted PWM
	//Clear OC1A on Compare Match, set OC1A at TOP
	ICR1=20000;	//ICR1=20000 defines 50Hz PWM, TOP=ICR1;
	TCCR1A|=(0<<COM1A0)|(1<<COM1A1)|(0<<COM1B0)|(0<<COM1B1)|(0<<FOC1A)|(0<<FOC1B)|(1<<WGM11)|(0<<WGM10);
	TCCR1B|=(0<<ICNC1)|(0<<ICES1)|(1<<WGM13)|(1<<WGM12)|(0<<CS12)|(1<<CS11)|(0<<CS10);	//Start timer with prescaler 8

	OCR1A = 0;

	for (;;)
	{
		ADCSRA |= (1 << ADSC);					//Start ADC conversion
		loop_until_bit_is_clear(ADCSRA, ADSC);	//Wait until ADC conversion is done
		potentiometerValue = ADC;				//Read ADC in


		OCR1A = 1200 + (potentiometerValue*3.4);	//Every turn = 180 degrees of servo
	}
}
Esempio n. 2
0
int main(void) 
{
	int16_t potentiometerValue;		//Compare Potentiometer Value to certain number ranges
		
	DDRC |= (1 << ENABLE);			//Data Direction Register C: writing a 1 to the bit enables output

	initADC0();						//Initialize ADC0

	while (1) 
	{
		ADCSRA |= (1 << ADSC);						//Start ADC conversion
		loop_until_bit_is_clear(ADCSRA, ADSC);		//Wait until ADC conversion is done
		potentiometerValue= ADC;					//Read ADC value in
		
		if (potentiometerValue >= 0  && potentiometerValue < 250) 
		{
			//100% duty cycle 
			PORTC = PORTC | (1<<ENABLE);
			_delay_ms(0);				
			PORTC = PORTC & (~(1<<ENABLE));
			_delay_ms(100);
		}
		else if (potentiometerValue >= 250 && potentiometerValue < 500)
		{
			//75% duty cycle 
			PORTC = PORTC | (1<<ENABLE);
			_delay_ms(25); 
			PORTC = PORTC & (~(1<<ENABLE));
			_delay_ms(100);
		}
		else if (potentiometerValue >= 500 && potentiometerValue < 750) 
		{
			//50% duty cycle pulse
			PORTC = PORTC | (1<<ENABLE);
			_delay_ms(100); 
			PORTC = PORTC & (~(1<<ENABLE));
			_delay_ms(100);
		}
		else 
		{
			//0% duty cycle pulse
			PORTC = PORTC | (1<<ENABLE);
			_delay_ms(0); 
		}
	}
	return (0);		//Never reached
}
Esempio n. 3
0
int main(void)
{
	DisableInterrupts(); // Permet de ne pas avoir d'interruption durant l'initialisation
	LCD_Init();
	initGPIO();	//Appel des fonctions d'initialisation 
	initSysTick();
	initUART();
	initADC0();	
	EnableInterrupts(); // Active les interruptions pour la suite de l'exécution du programme

	__asm
	{
		WFI
	}

	while(1){} // Boucle vide
}
Esempio n. 4
0
  int main(void)
  {
		uint16_t adcValue = 0;						//default 0
		int numb[10];
		uint8_t temp = 0;							//default 0
		int instelling = 0;  						//ingestelde waarde temperatuur default 0
		int bool = 0;								//welke weergave op scherm

		//PORT instellingen
		DISPLAYDDR = 0xFF; //Alle D als output.
		DDRB = 0xFF; //alle B as output
		LED_PORT_NR = 0xFF;

		//Waardes hexa 1 -15
	  numb[0] = 0b00111111;  	//0
	  numb[1] = 0b00000110;   	//1
	  numb[2] = 0b01011011;   	//2
	  numb[3] = 0b01001111;	  	//3
	  numb[4] = 0b01100110;	  	//4
	  numb[5] = 0b01101101;	  	//5
	  numb[6] = 0b01111101;	  	//6
	  numb[7] = 0b00000111;		//7
	  numb[8] = 0b01111111;		//8
	  numb[9] = 0b01101111;		//9



	  DISPLAYDDR = 0xFF; //Alle D als output.
	  DDRB = 0xFF; //alle B as output
	  LED_PORT_NR = 0xFF;



	  //adc aan
	  initADC0();

	  //thermometer instellingen
	  mcp_init();
	  mcp_set_power_mode(MCP_POWER_UP);



  while(1) //loop
  {

      temp = mcp_read_temp_float();


		ADCSRA |= (1<< ADSC) ;  //start ad conversion
		loop_until_bit_is_clear(ADCSRA,ADSC);  //wacht to die klaar is
		adcValue = ADC;

			//check of pb0 hoog is, is die hoog. verander led getal naar instelling of temp.
			if((PIN_INPUT_DRUK & (1<<PIN_INPUT)) == 1)
			  	  	  {
				  			  if(bool == 1)
				  			  {
				  				  bool = 0;
				  			  }
				  			  else
				  			  {
				  				  	 bool = 1;
				  			  }
				  			_delay_ms(200);
				  		  //vertraging anders telt hij door

				  	  }

			PIN_INPUT_DRUK &= ~(1<<PIN_INPUT);  //pin weer 0 nadat hij hoog is, anders doet die niks meer
	if((bool == 1) && (instelling == temp))
	{
	  	LED_PORT_NR = (1<<LINKSLED)|(1<<PWM_BLAUW);  	//eerste getal + rood en groen aan
		LED_PORT =~ numb[temp%10];
		_delay_ms(10);
		LED_PORT_NR = (1<<RECHTSLED)|(1<<PWM_BLAUW) ; //tweede getal + rood en groen aan
		LED_PORT =~ numb[temp/10];
		_delay_ms(10);
	}
	if((bool == 1) && (instelling > temp))
	{
	  	LED_PORT_NR = (1<<LINKSLED)|(1<<PWM_BLAUW)| (1<<PWM_GROEN);  //eerste getal + rood aan
		LED_PORT =~ numb[temp%10];
		_delay_ms(10);
		LED_PORT_NR = (1<<RECHTSLED)|(1<<PWM_BLAUW)| (1<<PWM_GROEN);  //tweede getal + rood aan
		LED_PORT =~ numb[temp/10];
		_delay_ms(10);
	}
	if((bool == 1) && (instelling < temp))
	{
	  	LED_PORT_NR = (1<<LINKSLED)|(1<<PWM_BLAUW)| (1<<PWM_ROOD); 	//eerste getal + groen aan
		LED_PORT =~ numb[temp%10];
		_delay_ms(10);
		LED_PORT_NR = (1<<RECHTSLED)|(1<<PWM_BLAUW)| (1<<PWM_ROOD); 	//tweede getal + groen
		LED_PORT =~ numb[temp/10];
		_delay_ms(10);
	}
if(bool == 0)
	{
	instelling = adcValue/30;  //max instelbare waarde is 34 hierdoor
	LED_PORT_NR = (1<<LINKSLED)|(1<<PWM_GROEN)| (1<<PWM_ROOD); //eerste getal + blauw aan
		LED_PORT =~ numb[instelling%10];
		_delay_ms(10);
		LED_PORT_NR = (1<<RECHTSLED)|(1<<PWM_GROEN)| (1<<PWM_ROOD); //led 1+ blauw aan
		LED_PORT =~ numb[instelling/10];
		_delay_ms(10);
	}



 }
Esempio n. 5
0
int main()
{
	DDRC = 0x02;	//Set Port C Pin 1 to output and the rest input
	DDRB = 0xFF;	//Set Port B of all pins to output

	int16_t potentiometerValue;	//Compare Potentiometer Value to certain number ranges
	
	initADC0();					//Initialize ADC0

	while (1)
	{
		ADCSRA |= (1 << ADSC);					//Start ADC conversion
		loop_until_bit_is_clear(ADCSRA, ADSC);	//Wait until ADC conversion is done
		potentiometerValue = ADC;				//Read ADC value in

		if ((PINC  & 0x08) == 0)
		{
			if (potentiometerValue >= 0  && potentiometerValue < 250)
			{
				//75 speed
				PORTB = 0x66;
				_delay_ms(75);
				PORTB = 0xCC;
				_delay_ms(75);
				PORTB = 0x99;
				_delay_ms(75);
				PORTB = 0x33;
				_delay_ms(75);
			}
			else if (potentiometerValue >= 250 && potentiometerValue < 500)
			{
				//120 speed
				PORTB = 0x66;
				_delay_ms(115);
				PORTB = 0xCC;
				_delay_ms(115);
				PORTB = 0x99;
				_delay_ms(115);
				PORTB = 0x33;
				_delay_ms(115);
			}
			else if (potentiometerValue >= 500 && potentiometerValue < 750)
			{
				//165 speed
				PORTB = 0x66;
				_delay_ms(165);
				PORTB = 0xCC;
				_delay_ms(165);
				PORTB = 0x99;
				_delay_ms(165);
				PORTB = 0x33;
				_delay_ms(165);
			}
			else
			{
				//190 speed
				PORTB = 0x66;
				_delay_ms(190);
				PORTB = 0xCC;
				_delay_ms(190);
				PORTB = 0x99;
				_delay_ms(190);
				PORTB = 0x33;
				_delay_ms(190);
			}
		}
		else
		{
			if (potentiometerValue >= 0  && potentiometerValue < 250)
			{
				//75 speed
				PORTB = 0x66;
				_delay_ms(75);
				PORTB = 0x33;
				_delay_ms(75);
				PORTB = 0x99;
				_delay_ms(75);
				PORTB = 0xCC;
				_delay_ms(75);
			}
			else if (potentiometerValue >= 250 && potentiometerValue < 500)
			{
				//120 speed
				PORTB = 0x66;
				_delay_ms(120);
				PORTB = 0x33;
				_delay_ms(120);
				PORTB = 0x99;
				_delay_ms(120);
				PORTB = 0xCC;
				_delay_ms(120);
			}
			else if (potentiometerValue >= 500 && potentiometerValue < 750)
			{
				//165 speed
				PORTB = 0x66;
				_delay_ms(165);
				PORTB = 0x33;
				_delay_ms(165);
				PORTB = 0x99;
				_delay_ms(165);
				PORTB = 0xCC;
				_delay_ms(165);
			}
			else
			{
				//190 speed
				PORTB = 0x66;
				_delay_ms(190);
				PORTB = 0x33;
				_delay_ms(190);
				PORTB = 0x99;
				_delay_ms(190);
				PORTB = 0xCC;
				_delay_ms(190);
			}
		}
	}
	
	return (0);	//Never reached
}
Esempio n. 6
0
int main(void)
{
  //init USART serial connection
  
  initUSART();
  printString("USART Initialized!\r\n");
  DDRB |= (1 << PB2);
  
  toggle('B', 2);
  _delay_ms(200);
  toggle('B', 2);
  _delay_ms(200);
  toggle('B', 2);
  _delay_ms(200);
  toggle('B', 2);
  _delay_ms(200);
  /*
  _delay_ms(2000);
  printString("Press Enter to continue\r\n");
  char null_string[32];
  readString(null_string, 32);
  */
  uint8_t i;
  uint32_t sum;
  
  //init ADC
  initADC0();
  uint16_t adcValue;
  
  //moving average variables
  uint8_t values = 200;
  uint16_t pulseValue [values];
  
  for(i=0;i<values;i++){
	  pulseValue[i] = 1500;
  }
  
  uint16_t avgPulseValue;
  
  float converter;
  //initPWM
  OCR1A = 1500;           /* set it to middle position initially */
  initTimer1Servo();
	
  
  
  while(1) { 
    
    ADCSRA |= (1 << ADSC);                     // start ADC conversion
    loop_until_bit_is_clear(ADCSRA, ADSC);     // wait until done
    
    adcValue = ADC;
    converter = adcValue*1.955;
    
    for(i=values-1;i>0;i--){
		pulseValue[i] = pulseValue[i-1];
	}
	pulseValue[0] = converter + 500;
    
    sum = 0;
    for(i=0;i<values;i++){
		sum = sum + pulseValue[i];
	}
	
	avgPulseValue = sum/values;
	
    //char pulseString[15];
    //sprintf(pulseString, "%d", avgPulseValue);
    
    //printString(pulseString);
    //printString("\r\n");
    
    OCR1A = avgPulseValue;
    //_delay_ms(50);
    
 }
 
 return(0);
 
}