Пример #1
0
void changePwmCharge(uint8_t mode,uint16_t *array){
	//0 -режим разрядки 1 - зарядки	 
	volatile uint8_t normal=(mode)?normalAmperage:dischargeAmperage; 	
	
	if(array[currentAmperage]!=array[normal]){
		array[currentPWM]=changePWM(array[currentAmperage],array[normal]);
	}	
}
Пример #2
0
void Motor::changeSpeed(int Speed) {
  // Just to change the speed (In percentage)
  if (Speed > 100)
    Speed = 100;
  if (Speed < -100)
    Speed = -100;
  speed = Speed;
  changePWM(Speed * mean_speed / 100);
}
Пример #3
0
void Motor::moveMotor(int Pwm) {           // +ve for CW and -ve for CCW.
  if (Pwm >= 0) {
    digitalWrite(dir1_pin, HIGH);
    digitalWrite(dir2_pin, LOW);
  } else {
    digitalWrite(dir1_pin, LOW);
    digitalWrite(dir2_pin, HIGH);
  }
  changePWM(Pwm);
  // Serial.println("PWM=");
  // Serial.print(Pwm);
}
Пример #4
0
int main( void )
{	
	/*	
	DDRB=1;
	while(1){
		BM_SetBit(PORTB,0);
		_delay_ms(10);
		BM_ClearBit(PORTB,0);
		_delay_ms(10);
	}
	*/
	volatile uint8_t stateButtons=STATE_NO_BUTTONS;
	volatile uint8_t  switchMeasuring = MEASURING_VOLTAGE;
	
	
	volatile uint8_t arrVal8[oldAutomatic+1];
	pCharge=& arrVal8[charge];
	arrVal8[charge]=0;	
	arrVal8[automatic]=1;	
	arrVal8[numDischargeCharge]=2;
	
	
	volatile uint16_t arrVal[oldWindowsSettings+1];
	arrVal[normalAmperage]=120;	
	arrVal[normalVoltage]=15;
	arrVal[dischargeVoltage]=10;	
	arrVal[dischargeAmperage]=100;	
	arrVal[currentPWM]=250;
	arrVal[currentVoltage]=0;
	arrVal[currentAmperage]=0;
	arrVal[windowsSettings]=1;
	
	
	volatile uint8_t currentDisplay=DISPLAY_START;
	volatile uint8_t oldCurrentDisplay=1;
	volatile uint16_t timeResetDisplay=100;
	
	
	USART_Init();	
	
	
	//инициализируем дисплей
	DDRD|=(1<<PD6);
	initCharge(arrVal8);
	
	LCD_Init();	
	ADC_Init();
	TIMER0_Init();
	initButtons();
	initPWM();
	setPwm(arrVal[currentPWM]);		
	
	setZeroOldValue(arrVal,arrVal8);
	
	
	

	
	StartConvAdc();
	sei();
	
	
	
	
	
	while(1){	
		
		
			volatile uint8_t codeButton=PINX_BUTTON & MASK_BUTTONS;
			
			if(stateButtons!=STATE_ENTER_5SEC && codeButton!=NO_BUTTONS  ){
				stateButtons=getStateButton(codeButton);
				timeResetDisplay=currentTime+5;
				_delay_ms(250);	
				
				switch(stateButtons){
				case STATE_ENTER:
					switch(currentDisplay){
						case DISPLAY_START:currentDisplay=DISPLAY_FAST_CHANGE;break;
						case DISPLAY_FAST_CHANGE:currentDisplay=DISPLAY_START;break;
						case DISPLAY_PROGR:currentDisplay=DISPLAY_PROGR+arrVal[windowsSettings];break;
						default:currentDisplay=DISPLAY_PROGR;
					}	
					break;				
				case STATE_ENTER_5SEC:
					if(currentDisplay>DISPLAY_P0){
						currentDisplay=DISPLAY_START;						
						}else{
						currentDisplay=DISPLAY_PROGR;
					}
					break;
				default:					
					switch(currentDisplay){
					case DISPLAY_FAST_CHANGE:
						arrVal8[charge]^=1;	
						setPwm(512);//Установка в 50 % при смене режима 		
						break;
					case DISPLAY_PROGR:						
						arrVal[windowsSettings]=changeVauleFromButtons(arrVal[windowsSettings],LAST_DISPLAY,stateButtons);
						if(arrVal[windowsSettings]==(LAST_DISPLAY+1)){
							arrVal[windowsSettings]=1;
						}else if(arrVal[windowsSettings]==0){
							arrVal[windowsSettings]=1;
						}
						break;
					case DISPLAY_P0:
						arrVal[normalVoltage]=changeVauleFromButtons(arrVal[normalVoltage],240,stateButtons);
						break;
					case DISPLAY_P1:
						arrVal[dischargeVoltage]=changeVauleFromButtons(arrVal[dischargeVoltage],240,stateButtons);
						break;
					case DISPLAY_P2:
						arrVal[dischargeAmperage]=changeVauleFromButtons(arrVal[dischargeAmperage],1000,stateButtons);
						break;
					case DISPLAY_P3:
						arrVal[normalAmperage]=changeVauleFromButtons(arrVal[normalAmperage],1022,stateButtons);
						break;
					case DISPLAY_P4:
						arrVal8[automatic]^=1;
						break;
					case DISPLAY_P5:
						arrVal8[numDischargeCharge]=changeVauleFromButtons(arrVal8[numDischargeCharge],10,stateButtons);
						if(arrVal8[numDischargeCharge]==11){
							arrVal8[numDischargeCharge]=1;
						}
						break;
					}
					
				}				
				
				} else if(codeButton==NO_BUTTONS) {
					if(currentDisplay!=DISPLAY_START){
						if(currentTime>timeResetDisplay){
							currentDisplay=DISPLAY_START;
						}						
					}
					
					if(stateButtons==STATE_ENTER_5SEC){
						stateButtons=STATE_NO_BUTTONS;
					}				
				}
		
		
		if(numberMeasure>=NUMBER_OF_MEASURING){
			
			adcResult=getNormalADC(adcResult);
			
			switch (switchMeasuring)
			{				
				case MEASURING_VOLTAGE:					
					switchMeasuring=MEASURING_CURRENT;
					arrVal[currentVoltage]=adcResult;
					//Отключаем если автомат режим 
					if(arrVal8[automatic] &&						
						(arrVal[currentVoltage]<=arrVal[dischargeVoltage] || arrVal[currentVoltage]>=arrVal[normalVoltage])){
							changeChargeMode(arrVal8);																		
					}			
				break;
				
				case MEASURING_CURRENT:				
					arrVal[currentAmperage]=adcResult;				
					volatile uint8_t normal;
					if(arrVal8[automatic]){
						normal=(arrVal8[charge])?normalAmperage:dischargeAmperage;
						//Изменяем наше текущее значение ШИМ если оно не в норме
						if(arrVal[currentAmperage]!=arrVal[normal]){
							arrVal[currentPWM]=changePWM(arrVal[currentAmperage],arrVal[normal]);
						}
					}
					switchMeasuring=MEASURING_VOLTAGE;								
				break;
			}
			//Смена первым идет измерение напряжение ADC0 - напряжения
			//Потом меняем  на измерение   ADC2 -ток
			SWITCH_ADC_CHANNEL(ADMUX,CHANNEL_CURRENT);
			
			
			_delay_us(250);
			adcResult=0;
			numberMeasure=0;
			StartConvAdc();
		}
		
		
		if(oldCurrentDisplay!=currentDisplay){
			oldCurrentDisplay=currentDisplay;
			setZeroOldValue(arrVal,arrVal8);			
			showCurrentDisplay(currentDisplay);			
		}	
		
		showAndSetValue(currentDisplay,arrVal,arrVal8);
	}
	
	return 0;
}