void main (void) 
{
	float period;
	
	PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer enable)
	PORT_Init();     // Initialize Port I/O
	SYSCLK_Init ();  // Initialize Oscillator
	UART0_Init();    // Initialize UART0
	TIMER0_Init();

	printf("\x1b[2J"); // Clear screen using ANSI escape sequence.

	printf ("Period measurement at pin P0.1 using Timer 0.\n"
	        "File: %s\n"
	        "Compiled: %s, %s\n\n",
	        __FILE__, __DATE__, __TIME__);

    while (1)
    {
    	// Reset the counter
		TL0=0; 
		TH0=0;
		TF0=0;
		overflow_count=0;
		
		while(P0_1!=0); // Wait for the signal to be zero
		while(P0_1!=1); // Wait for the signal to be one
		TR0=1; // Start the timer
		while(P0_1!=0) // Wait for the signal to be zero
		{
			if(TF0==1) // Did the 16-bit timer overflow?
			{
				TF0=0;
				overflow_count++;
			}
		}
		while(P0_1!=1) // Wait for the signal to be one
		{
			if(TF0==1) // Did the 16-bit timer overflow?
			{
				TF0=0;
				overflow_count++;
			}
		}
		TR0=0; // Stop timer 0, the 24-bit number [overflow_count-TH0-TL0] has the period!
		period=(overflow_count*65536.0+TH0*256.0+TL0)*(12.0/SYSCLK);
		// Send the period to the serial port
		printf( "\rf=%fs" , period);
		
		heart_rate = 1.0/(period/60.0); 
		bpm = (int) heart_rate; 
		
		LCDprint(bpm, 1, 5); //print heart rate bpm to LCD display
		
    }
  
	
}
예제 #2
0
int main (void)
{


	// enable pull ups for the 4 keys
	PORTA |= (1<<PORTA4)|(1<<PORTA5)|(1<<PORTA6)|(1<<PORTA7);

	
	// set LED Pins to output

	DDRC |= (1<<DDC2); // BL-LED1
	DDRD |= (1<<DDD6); // BL-LED2
	DDRD |= (1<<DDD7); // BL-LED3
	DDRC |= (1<<DDC6); // buzzer


	// disable bootloader LED
	DDRC |= (1<<DDC3);
	PORTC |= (1<<PORTC3);




	LCD_Init ();
	USART_Init ();
	USART1_Init ();
	TWIM_Init (200000);
	
	TIMER0_Init ();
	
	sei ();


	lcd_cls ();
	lcd_printp (PSTR("Hallo"), 0);
	
	
	
	for (;;)
	{
	}
}
예제 #3
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;
}
예제 #4
0
void main (void)
{
	float period;
   	float bpm;
   	unsigned int intbpm;
   	char stringbpm[3]; 
	// Configure the LCD
	LCD_4BIT();
	//initialize string
	stringbpm[2] = '\0';
   
	//PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer enable)
	PORT_Init();     // Initialize Port I/O
	//SYSCLK_Init ();  // Initialize Oscillator
	//UART0_Init();    // Initialize UART0
	TIMER0_Init();

	printf("\x1b[2J"); // Clear screen using ANSI escape sequence.

	printf ("Period measurement at pin P0.1 using Timer 0.\n"
	        "File: %s\n"
	        "Compiled: %s, %s\n\n",
	        __FILE__, __DATE__, __TIME__);

    while (1)
    {
    	// Reset the counter
		TL0=0; 
		TH0=0;
		TF0=0;
		overflow_count=0;
		
		while(P0_1!=0); // Wait for the signal to be zero
		while(P0_1!=1); // Wait for the signal to be one
		TR0=1; // Start the timer
		while(P0_1!=0) // Wait for the signal to be zero
		{
			if(TF0==1) // Did the 16-bit timer overflow?
			{
				TF0=0;
				overflow_count++;
			}
		}
		while(P0_1!=1) // Wait for the signal to be one
		{
			if(TF0==1) // Did the 16-bit timer overflow?
			{
				TF0=0;
				overflow_count++;
			}
		}
		TR0=0; // Stop timer 0, the 24-bit number [overflow_count-TH0-TL0] has the period!
		period=(overflow_count*65536.0+TH0*256.0+TL0)*(12.0/SYSCLK);
		// Send the period to the serial port
		printf( "\rf=%fs" , period);
		bpm = 1.0/(period/60.0);
		intbpm = bpm;
		//printf("\nbpm=%d\n", intbpm);
		LCDprint("BPM:",1,1);
		int2char(stringbpm, intbpm, 2);
		LCDprint(stringbpm,2,1);
	}
	
}