Exemplo n.º 1
0
void uart_task(){
//*
	if(buttons_old._1) USART_Transmit('1');
	if(buttons_old._2) USART_Transmit('2');
	if(buttons_old._3) USART_Transmit('3');
	if(buttons_old._4) USART_Transmit('4');
	if(buttons_old._5) USART_Transmit('5');
	if(buttons_old._6) USART_Transmit('6');
	if(buttons_old.next) USART_Transmit('n');
	if(buttons_old.prev) USART_Transmit('p');
	if(buttons_old.eject) USART_Transmit('e');
	if(buttons_old.tim) USART_Transmit('t');
	if(buttons_old.info) USART_Transmit('i');
	if(buttons_old.as) USART_Transmit('a');
	if(buttons_old.scan) USART_Transmit('s');
	if(buttons_old.flag) USART_Transmit('f');
	if(buttons_old.light) USART_Transmit('l');
	if(buttons_old.navi) USART_Transmit('N');
	if(buttons_old.back) USART_Transmit('b');
	if(buttons_old.audio) USART_Transmit('A');
	if(buttons_old.tone) USART_Transmit('T');
	if(buttons_old.left_right){
		int8_t c = buttons_old.left_right;
		if(c > 0){
			//while(c--){
				USART_Transmit('+');
				_delay_us(WAIT);
				USART_Transmit(c);
				_delay_us(WAIT);
				USART_Transmit('0');
			//}
		}else{
			//while(c++){
				c *= -1;
				USART_Transmit('-');
				_delay_us(WAIT);
				USART_Transmit(c);
				_delay_us(WAIT);
				USART_Transmit('0');
			//}							
		}
	}	
	if(buttons_old.enter) USART_Transmit('E');
	if(buttons_old.traffic){ 
		
		if(PIPIN & (1<<PIACTIVE)){
			PIPORT |= (1<<PISHUTDOWN);
			_delay_ms(1000);
			PIPORT &= ~(1<<PISHUTDOWN);
		}else{
			PISTARTPORT &= ~(1<<PISTART);
			_delay_ms(1000);
		}

	}
	if(buttons.next == false && buttons.prev == false && buttons.eject == false && buttons.tim == false && buttons.info == false && buttons.as == false && buttons.scan == false && buttons._1 == false &&	buttons._2 == false && buttons._3 == false && buttons.flag == false && buttons.light == false && buttons.navi == false && buttons.back == false && buttons.audio == false && buttons.tone == false && buttons.left_right == 0 && buttons.enter == false && buttons._4 == false && buttons._5 == false && buttons._6 == false && buttons.traffic == false){
		USART_Transmit('0');
	}
	buttons_old = buttons;
//*/	
	buttons_init();
	
}
Exemplo n.º 2
0
Arquivo: lcd.c Projeto: jcmvbkbc/stove
void lcd_set_entry_mode(uint8_t mode)
{
	lcd_write(0, 0x4 | (mode & 3));
	_delay_us(40);
}
Exemplo n.º 3
0
static void commandWriteNibble(int nibble) {
  digitalWrite(RS_PORT, RS_BIT, 0);
  pushNibble(nibble);
  _delay_us(27);
}
Exemplo n.º 4
0
DHT22_ERROR_t readDHT22(DHT22_DATA_t* data)
{

	uint8_t retryCount = 0;
	uint8_t csPart1, csPart2, csPart3, csPart4;
	uint16_t rawHumidity = 0;
	uint16_t rawTemperature = 0;
	uint8_t checkSum = 0;
	uint8_t i;

	// Pin needs to start HIGH, wait until it is HIGH with a timeout
	retryCount = 0;
	cli();
	DHT22_DDR &= ~(1 << ( DHT22_PIN ));
	sei();
	do
	{
		if(retryCount > 125) return DHT_BUS_HUNG;
		retryCount++;
		_delay_us(2);
	} while( !( DHT22_PORT_IN & ( 1 << DHT22_PIN ) ) );				//!DIRECT_READ(reg, bitmask)

	// Send the activate pulse
	cli();
	DHT22_PORT_OUT &= ~(1 << ( DHT22_PIN )); 							//DIRECT_WRITE_LOW(reg, bitmask);
	DHT22_DDR |= 1 << ( DHT22_PIN );								//DIRECT_MODE_OUTPUT(reg, bitmask); // Output Low
	sei();
	_delay_ms(20); 										// spec is 1 to 10ms
	cli();
	DHT22_DDR &= ~(1 << ( DHT22_PIN ));;							// Switch back to input so pin can float
	sei();

	// Find the start of the ACK signal
	retryCount = 0;
	do
	{
		if (retryCount > 25) 							//(Spec is 20 to 40 us, 25*2 == 50 us)
		{
			return DHT_ERROR_NOT_PRESENT;
		}
		retryCount++;
		_delay_us(2);
	} while( DHT22_PORT_IN & ( 1 << DHT22_PIN ) );
	
	// Here sensor responded pulling the line down DHT22_PIN = 0

	// Find the transition of the ACK signal
	retryCount = 0;
	do
	{
		if (retryCount > 50) 							//(Spec is 80 us, 50*2 == 100 us)
		{
			return DHT_ERROR_ACK_TOO_LONG;
		}
		retryCount++;
		_delay_us(2);
	} while( !(DHT22_PORT_IN & ( 1 << DHT22_PIN )) );

	// Here sensor pulled up DHT22_PIN = 1

	// Find the end of the ACK signal
	retryCount = 0;
	do
	{
		if (retryCount > 50) 							//(Spec is 80 us, 50*2 == 100 us)
		{
			return DHT_ERROR_ACK_TOO_LONG;
		}
		retryCount++;
		_delay_us(2);
	} while( DHT22_PORT_IN & ( 1 << DHT22_PIN ) );
	
	// Here sensor pulled down to start transmitting bits.

	// Read the 40 bit data stream
	for(i = 0; i < DHT22_DATA_BIT_COUNT; i++)
	{
		// Find the start of the sync pulse
		retryCount = 0;
		do
		{
			if (retryCount > 35) 						//(Spec is 50 us, 35*2 == 70 us)
			{
				return DHT_ERROR_SYNC_TIMEOUT;
			}
			retryCount++;
			_delay_us(2);
		} while( !(DHT22_PORT_IN & ( 1 << DHT22_PIN )) );

		// Measure the width of the data pulse
		retryCount = 0;
		do
		{
			if (retryCount > 50) 						//(Spec is 80 us, 50*2 == 100 us)
			{
				return DHT_ERROR_DATA_TIMEOUT;
			}
			retryCount++;
			_delay_us(2);
		} while( DHT22_PORT_IN & ( 1 << DHT22_PIN ) );

		// Identification of bit values.
		if (retryCount > 20) // Bit is 1: 20*2 = 40us (specification for bit 0 is 26 a 28us).
		{
			if (i < 16) // Humidity 
			{
				rawHumidity |= (1 << (15 - i));
			}
			if ((i > 15) && (i < 32))  // Temperature
			{
				rawTemperature |= (1 << (31 - i));
			}
			if ((i > 31) && (i < 40))  // CRC data
			{
				checkSum |= (1 << (39 - i));
			}
		}
	}

	// translate bitTimes
	// 26~28us == logical 0
	// 70us	   == logical 1
	// here threshold is 40us

	// calculate checksum
	csPart1 = rawHumidity >> 8;
	csPart2 = rawHumidity & 0xFF;
	csPart3 = rawTemperature >> 8;
	csPart4 = rawTemperature & 0xFF;
	
	if( checkSum == ( (csPart1 + csPart2 + csPart3 + csPart4) & 0xFF ) )
	{
		// raw data to sensor values
		data->humidity_integral = (uint8_t)(rawHumidity / 10);
		data->humidity_decimal = (uint8_t)(rawHumidity % 10);

		if(rawTemperature & 0x8000)	// Check if temperature is below zero, non standard way of encoding negative numbers!
		{
			rawTemperature &= 0x7FFF; // Remove signal bit
			data->temperature_integral = (int8_t)(rawTemperature / 10) * -1;
			data->temperature_decimal = (uint8_t)(rawTemperature % 10);
		} else
		{
			data->temperature_integral = (int8_t)(rawTemperature / 10);
			data->temperature_decimal = (uint8_t)(rawTemperature % 10);			
		}

		return DHT_ERROR_NONE;
	}
	return DHT_ERROR_CHECKSUM;
}
Exemplo n.º 5
0
Arquivo: lcd.c Projeto: jcmvbkbc/stove
static void lcd_shift(uint8_t shift)
{
	lcd_write(0, 0x10 | (shift & 0xc));
	_delay_us(40);
}
Exemplo n.º 6
0
static void execute(void)
{
	PGMW2 &= ~RESETb;
	_delay_us(2); // Worst-case 2 CPU ticks (assuming min. of 1MHz clock)
	PGMW2 |= RESETb;
}
int8_t dht_getdata(int8_t *temperature, int8_t *humidity) {
#endif
	uint8_t bits[5];
	uint8_t i,j = 0;

	memset(bits, 0, sizeof(bits));

	//reset port
	DHT_DDR |= (1<<DHT_INPUTPIN); //output
	DHT_PORT |= (1<<DHT_INPUTPIN); //high
	
	_delay_ms(16); //was 100
	_delay_ms(16); //was 100
	_delay_ms(16); //was 100
	_delay_ms(16); //was 100
	_delay_ms(16); //was 100
	_delay_ms(16); //was 100
	_delay_ms(4); //was 100
	
	
	//send request
	DHT_PORT &= ~(1<<DHT_INPUTPIN); //low
	#if DHT_TYPE == DHT_DHT11
	_delay_ms(18); // was 18
	
	#elif DHT_TYPE == DHT_DHT22
	_delay_us(48); // was 500
	_delay_us(48); // was 500
	_delay_us(48); // was 500
	_delay_us(48); // was 500
	_delay_us(48); // was 500
	_delay_us(48); // was 500
	_delay_us(48); // was 500
	_delay_us(48); // was 500
	_delay_us(48); // was 500
	_delay_us(48); // was 500
	_delay_us(48); // was 500
	_delay_us(20); // was 500
	
	
	#endif
	DHT_PORT |= (1<<DHT_INPUTPIN); //high
	DHT_DDR &= ~(1<<DHT_INPUTPIN); //input
	_delay_us(40); // was 40

	//check start condition 1
	if((DHT_PIN & (1<<DHT_INPUTPIN))) {
		return -1;
	}
	_delay_us(40);	// was80
	_delay_us(40);	// was80
	//required delays to get accurate readings. With only 40uS Temperatures are ~half
	while (!DHT_PIN)
	{
		
	}
	//check start condition 2
	if(!(DHT_PIN & (1<<(DHT_INPUTPIN)))) {
		return -2;
	}
	_delay_us(40);	// was80
	_delay_us(40);	// was80

	//read the data
	uint16_t timeoutcounter = 0;
	for (j=0; j<5; j++) { //read 5 byte
		uint8_t result=0;
		for(i=0; i<8; i++) {//read every bit
			timeoutcounter = 0;
			while(!(DHT_PIN & (1<<DHT_INPUTPIN))) { //wait for an high input (non blocking)
				timeoutcounter++;
				if(timeoutcounter > DHT_TIMEOUT) {
					return -1; //timeout
				}
			}
			_delay_us(30);	//was 30
			if(DHT_PIN & (1<<DHT_INPUTPIN)) //if input is high after 30 us, get result
				result |= (1<<(7-i));
			timeoutcounter = 0;
			while(DHT_PIN & (1<<DHT_INPUTPIN)) { //wait until input get low (non blocking)
				timeoutcounter++;
				if(timeoutcounter > DHT_TIMEOUT) {
					return -1; //timeout
				}
			}
		}
		bits[j] = result;
	}

	//reset port
	DHT_DDR |= (1<<DHT_INPUTPIN); //output
	DHT_PORT |= (1<<DHT_INPUTPIN); //low
	_delay_ms(16); //was 100
	_delay_ms(16); //was 100
	_delay_ms(16); //was 100
	_delay_ms(16); //was 100
	_delay_ms(16); //was 100
	_delay_ms(16); //was 100
	_delay_ms(4); //was 100
	

	//check checksum
	if ((uint8_t)(bits[0] + bits[1] + bits[2] + bits[3]) == bits[4]) {
		//return temperature and humidity
		#if DHT_TYPE == DHT_DHT11
		*temperature = bits[2];
		*humidity = bits[0];
		#elif DHT_TYPE == DHT_DHT22
		uint16_t rawhumidity = bits[0]<<8 | bits[1];
		uint16_t rawtemperature = bits[2]<<8 | bits[3];
		if(rawtemperature & 0x8000) {
			*temperature = (float)((rawtemperature & 0x7FFF) / 10.0) * -1.0;
		} else {
			*temperature = (float)(rawtemperature)/10.0;
		}
		*humidity = (float)(rawhumidity)/10.0;
		#endif
		return 0;
	}

	return -1;
}
Exemplo n.º 8
0
/*
 * shift a byte from the SR into the processor.  
 *
 * Parameters:
 *      in_byte        The byte to shift in from the '299.
*/ 
uint8_t websr_shift_byte_in()
{
	// Request vom Partner testen, warten auf Antwort
	// Listen-Bit zuruecksetzen
	uint8_t in_byte=0;
	//websr_pulse_ms(4);
	if (websrstatus &(1<< SR_LISTEN_BIT)); // Bit 7 Listen-Bit gesetzt, Data wird gesendet
	{
		
		//lcd_clr_line(3);
		//lcd_gotoxy(4,3);
		//lcd_puts("in: \0");
		//lcd_putint(out_byte);
		//_delay_ms(4);
		//lcd_clr_line(1);
		uint8_t i=0;
		_delay_us(SR_PULSE_DELAY);
		while (i<8)
		{
			//lcd_gotoxy(5,3);
			//lcd_putint1(i);
			
			// Clock senden
			//websr_pulse_ms(1);
	//		websr_pulse(100);
			//_delay_us(100);
			//_delay_ms(2);
					
					// PIN lesen
					if (SR_DATA_PORTPIN & (1<<SR_DATA_IO_PIN)) // bit ist H
					{
						//in_byte |= (1<<(8-1-i));
						in_byte |= (1<<i);
						//lcd_gotoxy(6,2);
						//lcd_puts("1");
						
					}
					else
					{
						//in_byte &=~(1<<(8-1-i));
						in_byte |= (0<<i);
						//lcd_gotoxy(6,2);
						//lcd_puts("0");
					}
					
					//i++;
					
				
				websr_pulse(SR_PULSE_DELAY);
				
			
			
			
			/* read bit Q7 */
			//	lcd_gotoxy(6,3);
			//lcd_puts("B: \0");
			//lcd_putint(i);
			//_delay_ms(10);
			i++;
		} // while i
		
	//	SR_DATA_PORT |= _BV(SR_CLK_PIN);
		
	
	} // if SR_LISTEN_BIT
	//	lcd_clr_line(1);
	
	
	// Listen-Bit zuruecksetzen
//	websrstatus &= ~(1<<SR_LISTEN_BIT);						// nur noch in ListenForRequest
	//_delay_ms(10);
	
	// Listen-Bereitschaft an Partner zuruecknehmen
//	SR_HANDSHAKE_PORT |= (1<<SR_TALK_PIN);					// nur noch in ListenForRequest
	
	return in_byte;
}
Exemplo n.º 9
0
/*
  Name:     lcd_init_4d
  Purpose:  initialize the LCD module for a 4-bit data interface
  Entry:    equates (LCD instructions) set up for the desired operation
  Exit:     no parameters
  Notes:    uses time delays rather than checking the busy flag
*/
void lcd_init_4d(void)
{
// Power-up delay
    _delay_ms(100);                                 // initial 40 mSec delay

// IMPORTANT - At this point the LCD module is in the 8-bit mode and it is expecting to receive  
//   8 bits of data, one bit on each of its 8 data lines, each time the 'E' line is pulsed.
//
// Since the LCD module is wired for the 4-bit mode, only the upper four data lines are connected to 
//   the microprocessor and the lower four data lines are typically left open.  Therefore, when 
//   the 'E' line is pulsed, the LCD controller will read whatever data has been set up on the upper 
//   four data lines and the lower four data lines will be high (due to internal pull-up circuitry).
//
// Fortunately the 'FunctionReset' instruction does not care about what is on the lower four bits so  
//   this instruction can be sent on just the four available data lines and it will be interpreted 
//   properly by the LCD controller.  The 'lcd_write_4' subroutine will accomplish this if the 
//   control lines have previously been configured properly.

// Set up the RS and E lines for the 'lcd_write_4' subroutine.
    lcd_RS_port &= ~(1<<lcd_RS_bit);                // select the Instruction Register (RS low)
    lcd_E_port &= ~(1<<lcd_E_bit);                  // make sure E is initially low

// Reset the LCD controller
    lcd_write_4(lcd_FunctionReset);                 // first part of reset sequence
    _delay_ms(10);                                  // 4.1 mS delay (min)

    lcd_write_4(lcd_FunctionReset);                 // second part of reset sequence
    _delay_us(200);                                 // 100uS delay (min)

    lcd_write_4(lcd_FunctionReset);                 // third part of reset sequence
    _delay_us(200);                                 // this delay is omitted in the data sheet

// Preliminary Function Set instruction - used only to set the 4-bit mode.
// The number of lines or the font cannot be set at this time since the controller is still in the
//  8-bit mode, but the data transfer mode can be changed since this parameter is determined by one 
//  of the upper four bits of the instruction.
 
    lcd_write_4(lcd_FunctionSet4bit);               // set 4-bit mode
    _delay_us(80);                                  // 40uS delay (min)

// Function Set instruction
    lcd_write_instruction_4d(lcd_FunctionSet4bit);   // set mode, lines, and font
    _delay_us(80);                                  // 40uS delay (min)

// The next three instructions are specified in the data sheet as part of the initialization routine, 
//  so it is a good idea (but probably not necessary) to do them just as specified and then redo them 
//  later if the application requires a different configuration.

// Display On/Off Control instruction
    lcd_write_instruction_4d(lcd_DisplayOff);        // turn display OFF
    _delay_us(80);                                  // 40uS delay (min)

// Clear Display instruction
    lcd_write_instruction_4d(lcd_Clear);             // clear display RAM
    _delay_ms(4);                                   // 1.64 mS delay (min)

// ; Entry Mode Set instruction
    lcd_write_instruction_4d(lcd_EntryMode);         // set desired shift characteristics
    _delay_us(80);                                  // 40uS delay (min)

// This is the end of the LCD controller initialization as specified in the data sheet, but the display
//  has been left in the OFF condition.  This is a good time to turn the display back ON.
 
// Display On/Off Control instruction
    lcd_write_instruction_4d(lcd_DisplayOn);         // turn the display ON
    _delay_us(80);                                  // 40uS delay (min)
}
Exemplo n.º 10
0
//-----------------------------------------------------------------------------------------
// FUNCTION: lcd_clrscr
// PURPOSE:  Clear display and set cursor to home position
void lcd_clrscr(void)
{
	g_nCurrentLine = 0;
   	lcd_instr(1<<LCD_CLR);
    _delay_us(500);
}
Exemplo n.º 11
0
/*
 * shifts a byte into the SR*
 * Parameters:
 *      out_byte        The byte to load .
*/
uint8_t websr_shift_byte_out(uint8_t out_byte)
{ 
	
	// Listen-Bit zuruecksetzen
	//	websrstatus &= ~(1<< SR_LISTEN_BIT); // Bit 7
	
	// Talk-Request an Partner senden, warten auf Antwort
	SR_HANDSHAKE_PORT &= ~(1<<SR_TALK_PIN);	// Bit 3
	
	// Talk-Bit setzen
	websrstatus |= (1<< SR_TALK_BIT); // Bit 6
	//_delay_ms(10);
	uint16_t z=1;
	uint16_t zz=0;
	while (((SR_HANDSHAKE_PORTPIN & (1<< SR_LISTEN_PIN)))) // noch keine Antwort vom Partner > warten
	{	
		// Timer2 zuruecksetzen
		TCNT2=0; 
		timer2_counter=0;
		//lcd_gotoxy(16,3);
		//lcd_putint1(z);
		_delay_us(10); // warten
		z++;
		if (z == 0) //
		{
			zz++;
		}
	}
	z &= 0xFF00;
	z >>= 8;
	//zz &= 0xFF00;
	//zz >>= 8;
	
	if (ByteCounter == 0xFF)
	{
		//lcd_gotoxy(16,3);
		//lcd_puthex(z);
		//lcd_puthex(zz);
		
		if (zz >= 0x0005)
		{
			lcd_gotoxy(19,2);
			lcd_putc('E');
			websrstatus |= (1<< SR_ERR_BIT); 
			websr_reset();
			return 2;
		}
		else
		{
			//lcd_putc('G');
		}
		
	}
	
	// Antwort da
	//lcd_gotoxy(18,3);
	//lcd_puts("OK\0");
	//_delay_ms(2); // Eventuell mehr
	//websrstatus |= (1<< SR_LISTEN_BIT); // Bit 7
	int i;
	//	lcd_gotoxy(10,2);
	//	lcd_putc('*');
	
	//lcd_gotoxy(11,2);
	//lcd_puts("        \0");
	//_delay_ms(1);
	//lcd_gotoxy(11,2);
	
	for(i=0; i<8; i++)
	{
		if (out_byte & 0x80)
		{
			//lcd_putc('1');
			/* this bit is high */
			SR_DATA_PORT |=_BV(SR_DATA_IO_PIN); 
		}
		else
		{
			//lcd_putc('0');
			/* this bit is low */
			SR_DATA_PORT &= ~_BV(SR_DATA_IO_PIN);						
		}
		//_delay_us(2*SR_PULSE_DELAY);
		//_delay_us(255);
		//		_delay_us(255);
		//		_delay_us(50);
		websr_pulse(SR_PULSE_DELAY);
		//_delay_ms(1);
		//_delay_us(2*SR_PULSE_DELAY);
		out_byte = out_byte << 1;	//	Byte um eine Stelle nach links schieben
		
	}
	//lcd_putc('*');
	
	// Talk-Request zuruecknehmen
	SR_HANDSHAKE_PORT |= (1<<SR_TALK_PIN);
	
	// +++ Partner Zeit lassen
	_delay_ms(8);
	return 0;
}
Exemplo n.º 12
0
//-----------------------------------------------------------------------------------------
// FUNCTION: lcd_toggle_e
// PURPOSE: flush channel E
void lcd_toggle_e(void)
{
    lcd_e_high();
    _delay_us(10);
    lcd_e_low();
}
int main(void)
{
	/*Ждем пока все включится*/
	_delay_ms(100);
	
	/*Настраиваем порты ввода-вывода*/
	DDRB = 1<<PORTB0|1<<PORTB1|1<<PORTB2|1<<PORTB3|1<<PORTB4|1<<PORTB5|1<<PORTB6|1<<PORTB7;
	DDRC = 1<<PORTC0|1<<PORTC1|1<<PORTC2|0<<PORTC3|0<<PORTC4|0<<PORTC5|0<<PORTC6|0<<PORTC7;
	DDRD = 0<<PORTD0|0<<PORTD1|0<<PORTD2|0<<PORTD3|1<<PORTD4|0<<PORTD5|0<<PORTD6|1<<PORTD7;

	PORTB = 1;
	PORTD = 1 << PORTD2;
    
	/*Тяга двигателей на минимум*/
    for(uint8_t k = 0; k < CHANNELS_COUNT; ++k)
    {
        counter[k] = LOW;
    }
	
	/*Настраиваем I2C*/
	TWSR = 0x00;
	TWBR = ((F_CPU / I2C_SPEED) - 16) / 2;  
	_delay_us(10);

	/*Включаем Таймер0*/
	TCCR0 = 1<<CS02 | 0<<CS01 | 0<<CS00;
	
	/*Включаем Таймер1*/
	OCR1A=HIGH; //TOP
	TCCR1A=0<<COM1A1|0<<COM1A0|1<<COM1B1|0<<COM1B0|0<<FOC1A|0<<FOC1B|1<<WGM11|1<<WGM10;
	TCCR1B=0<<ICNC1|0<<ICES1|1<<WGM13|1<<WGM12|0<<CS12|0<<CS11|1<<CS10;
	TIMSK= 1<<TOIE2 | 1<<OCIE1A|1<<OCIE1B|0<<TOIE1|1<<TOIE0|0<<OCIE0;
	OCR1B=LOW;
	
	/*Включаем АЦП*/
	ADC_Init();   
    
	/*Включаем прерывание INT0(высотомер)*/
	INT0_Init();
	
	/*Разрешаем работу прерываний*/
	sei();
	
	/*Настраиваем Modbus*/
	eMBErrorCode eStatus = eMBInit( MB_RTU, 0x01, 0, 57600, MB_PAR_NONE );
	eStatus = eMBEnable();
    
	/*Настраиваем сенсоры*/    
	SensorsInit();
	
	/*Загружаем в Holding Registers и в массив параметров значения из EEPROM*/
	ModbusInitValues();
	
	filterInit();	
		
	while(1)
	{
		/*Актуализируем значения Modbus-регистров в соответствии со значениями параметров*/
		ModbusLoader();
		/*Актуализируем значения параметров в соответствии со значениями Holding Registers*/
		ModbusSaver();
		
		/*Итерация Modbus*/
		eMBPoll();
		
		/*Ресурсоемкий расчет курса*/		
		Course_Calc();
	}
}
Exemplo n.º 14
0
// Try to be more accuracy
void inline delay(uint8_t ms25)
{
	while (ms25--)
		_delay_us(25);
}
Exemplo n.º 15
0
void lcd_home(void) {
    lcd_command(LCD_INSTRUCTION_HOME);
    _delay_us(LCD_LONGDELAY);
}
Exemplo n.º 16
0
void lcdInit()
{
    // based on http://www.mimuw.edu.pl/~marpe/mikrokontrolery/w5_klawisze_lcd.pdf 
    // slide no. 26
    
    // set input/output direction
    SBI(LCD_DATA_DDR, LCD_D4);
    SBI(LCD_DATA_DDR, LCD_D5);
    SBI(LCD_DATA_DDR, LCD_D6);
    SBI(LCD_DATA_DDR, LCD_D7);      
    SBI(LCD_E_DDR, LCD_OE);   
    SBI(LCD_RS_DDR, LCD_RS);
    
    CBI(LCD_RS_PORT, LCD_RS);
    CBI(LCD_E_PORT, LCD_OE);
    
    _delay_ms(40);
    
    SBI(LCD_E_PORT, LCD_OE);
    SBI(LCD_DATA_PORT, LCD_D4);
    SBI(LCD_DATA_PORT, LCD_D5);
    CBI(LCD_DATA_PORT, LCD_D6);
    CBI(LCD_DATA_PORT, LCD_D7);
    
    _delay_loop_1(1); // 3+ cycles

    CBI(LCD_E_PORT, LCD_OE);
    
    _delay_ms(4.1);
    
    SBI(LCD_E_PORT, LCD_OE);
    
    _delay_loop_1(1); // 3+ cycles
    
    CBI(LCD_E_PORT, LCD_OE);
    
    _delay_us(100);
    
    SBI(LCD_E_PORT, LCD_OE);
    
    _delay_loop_1(1); // 3+ cycles
    
    CBI(LCD_E_PORT, LCD_OE);
    
    _delay_us(100);

    SBI(LCD_E_PORT, LCD_OE);
    CBI(LCD_DATA_PORT, LCD_D4);
    
    _delay_loop_1(1); // 3+ cycles
    
    CBI(LCD_E_PORT, LCD_OE); 
    
    _delay_us(100);
        
    lcdInstr(0x28); //0b00101000
    
    lcdInstr(0x06); //0b00000110
    
    lcdInstr(0x0f); //0b00001111

    lcdInstr(0x01); //0b00000001
    
    _delay_ms(2);    
}    
Exemplo n.º 17
0
static void
xmas_end() {
	cbi(XMAS_PORT,XMAS_PIN);
	_delay_us(30);
}
Exemplo n.º 18
0
int SendPacket(char dest, char* Spacket) {
    /*
    Packet length = 122  => max 6 frames
    split packet into frames, send frame, await acknowledgement  
    */
    struct frame data[FRAMECOUNT];
    int no_frames;
    put_number(millis());
    put_char('\n');
    no_frames = makeframe(&data, dest, Spacket, 0);
    put_number(millis());
    put_char('\n');    

    uint8_t *bufptr;
    char temp[50];

    int i,k, send_complete;
    struct frame ack;
    unsigned long time;

    for(i = 0; i < no_frames; i++) {
        send_complete = 0;
        char test[FRAMELEN] = "0123456789012345678901234";
        while(!send_complete) {
            ///////////////////send//////////////////////
            rfm12_tx(strlen(data[i].frame), 0, (uint8_t*)data[i].frame);
            for (uint8_t j = 0; j < 100; j++)   
            {   
                //put_string(". ");
                rfm12_tick();   
                _delay_us(500); 
            }

            time = millis() + 500;

            while((millis() != time)) {
                ///////////check for acknowledgemt/////////////////
                if(rfm12_rx_status() == STATUS_COMPLETE) {
                    //send_complete = 1;
                    bufptr = rfm12_rx_buffer();
                    for(k = 0; k < (rfm12_rx_len()); k++) {
                        temp[k] = bufptr[k];
                    }
                    temp[rfm12_rx_len()] = '\0';
                    rfm12_rx_clear();
                    // put_string("\nRECEIVED: ");
                    // put_string(temp);
                    // put_string("\n\n");
                    ////////////////check if acknowledgemnt valid////////////////
                    if(decode_frame(ack, temp) & (1<<1)) {
                        //if(ack.checksum[0] == data[i].checksum[0]) {
                        put_string("\nSend Complete!\n");
                        send_complete = 1;
                        break;  
                        //}
                    }
                 }
            }
            if(!send_complete) {
                put_string("\nTIMEOUT\n");
            }

        }
    }

    /*
    for frame in frames:
        send_complete = 0;
        while not send complete:
            send frame
            start timer
            while timer:
                if acknowledgement:
                    send_complete = 1;

    */





    return 0;
}
Exemplo n.º 19
0
void home_xy(void)
{
    bool at_x_min, at_y_min;

    // 1st: move down and left until both X and Y min limit hit.
    printf("Step 1\n");
    set_x_direction_negative();
    set_y_direction_negative();
    at_x_min = at_y_min = false;
    // uint8_t z = 0;
    while (true) {
        at_x_min = at_x_min || c_x_min_reached();
        at_y_min = at_y_min || c_y_min_reached();
        // if (!++z)
        //     printf("at_x_min=%d at_y_min=%d\n", at_x_min, at_y_min);
        if (at_y_min && at_x_min)
            break;
        if (!at_x_min)
            step_x();
        if (!at_y_min)
            step_y();
        _delay_us(uSEC_per_uSTEP(HOME_SPEED_1));
    }
    printf("Step 1 end\n");

    // 2nd: move up and right until off both X and Y min limits.
    // Travel 1 cm max.
    printf("Step 2\n");
    set_x_direction_positive();
    set_y_direction_positive();
    uint16_t ustep_count = 0;
    while (true) {
        at_x_min = at_x_min && c_x_min_reached();
        at_y_min = at_y_min && c_y_min_reached();
        if (!at_y_min && !at_x_min)
            break;
        if (at_x_min)
            step_x();
        if (at_y_min)
            step_y();
        _delay_us(uSEC_per_uSTEP(HOME_SPEED_2));
        if (ustep_count == MM_to_uSTEPS(10))
            break;
    }
    printf("Step 2 end\n");

    // 3rd: move down and left until both X and Y min limits hit again.
    printf("Step 3\n");
    set_x_direction_negative();
    set_y_direction_negative();
    while (true) {
        at_x_min = at_x_min || c_x_min_reached();
        at_y_min = at_y_min || c_y_min_reached();
        if (at_y_min && at_x_min)
            break;
        if (!at_x_min)
            step_x();
        if (!at_y_min)
            step_y();
        _delay_us(uSEC_per_uSTEP(HOME_SPEED_3));
    }
    printf("Step 3 end\n");
}
Exemplo n.º 20
0
int RecievePacket(char* Rpacket) {
    /*
    see http://www.hansinator.de/rfm12lib/ for rfm12b libray details
    receive frame, send acknowledgement

    if received a frame:
        de-bytestuff
        check crc
        if recipient:
            acknowledge
            pass to network
    */
    uint8_t* bufptr;
    char Rframe[50], ackstr[50];
    struct frame ack;
    struct frame Nrframe[FRAMECOUNT];
    int Received_Final_frame = 0;
    int i = 0;
    while(!Received_Final_frame){
        //int Rframe_len;
        if (rfm12_rx_status() == STATUS_COMPLETE) {
            bufptr = rfm12_rx_buffer();
            for(uint8_t k = 0; k < (rfm12_rx_len()); k++) {
                Rframe[k] = bufptr[k];
            }
            Rframe[rfm12_rx_len()] = '\0';
            rfm12_rx_clear();
            put_string(Rframe);
            strcpy(ackstr, Rframe);
            int Rframe_check = decode_frame(Nrframe[i], Rframe);
            put_string("\nRframe_check: ");
            put_number(Rframe_check);
            if(Rframe_check & (1<<1)) {
                if(Rframe_check & 1<<3) {
                    Received_Final_frame = 1;
                }
                /*
                frame received, frame for me
                acknowledge
                */
                rfm12_tx(strlen(ackstr), 0, (uint8_t*)ackstr);
                for (uint8_t j = 0; j < 100; j++)   
                {   
                    //put_string(". ");
                    rfm12_tick();   
                    _delay_us(500); 
                }
                i++;
                
            }
            else if(!Rframe_check) {
                ;
            }
            else {
                break;
            }

        }
    }
    // if(i && i < FRAMECOUNT) {
    //     put_string("\nPacketComplete");
    //     strcpy(Rpacket, Nrframe[0].frame);
    
    //     for(int l = 1; l<i; l++) {
    //         strcat(Rpacket, Nrframe[l].frame);
    //     }
    //     strcat(Rpacket, "\n");
    // }
    return i;

}
Exemplo n.º 21
0
void LCD_init(uint8_t mode, uint8_t cursor){
	//R/W tied to GND => always write 
	LCD_CTRL_DDR |= (1<<LCD_E) | (1<<LCD_RS);
	LCD_CTRL_PORT |= (1<<LCD_E) | (1<<LCD_RS); // set E high --> no data yet, set RS high --> input interpreted as data
		
	LCD_DATA_DDR |= 0x0F;
	LCD_DATA_PORT &= ~(0x0F);
	
	_delay_ms(30); // wait for LCD 

	//###############################################
	// this is the initial soft reset bootup sequence
	LCD_CTRL_PORT &= ~(1<<LCD_RS); // set RS to low --> bytes interpreted as commands 
	LCD_CTRL_PORT |= (1<<LCD_E);
	
	LCD_DATA_PORT = (1<<LCD_DB5) | (1<<LCD_DB4);
	
	LCD_CTRL_PORT &= ~(1<<LCD_E);
	_delay_us(220);
	LCD_CTRL_PORT |= (1<<LCD_E);
	_delay_ms(5);
	
	LCD_CTRL_PORT &= ~(1<<LCD_E);
	_delay_us(220);
	LCD_CTRL_PORT |= (1<<LCD_E);
	_delay_us(64);
	
	LCD_CTRL_PORT &= ~(1<<LCD_E);
	_delay_us(220);
	LCD_CTRL_PORT |= (1<<LCD_E);
	_delay_us(64);
	
	LCD_DATA_PORT &= ~(1<<LCD_DB4);
	
	LCD_CTRL_PORT &= ~(1<<LCD_E);
	_delay_us(220);
	LCD_CTRL_PORT |= (1<<LCD_E);
	_delay_us(64);
	//###############################################
	
	
	LCD_write(mode); // 4 bits, 2 lines, 5x8 point matrix
	_delay_us(55);
	LCD_write(0x08); // display off
	_delay_us(55);
	LCD_write(0x01); // clear display
	_delay_ms(2);
	LCD_write(0x06); // cursor increments, no display shift
	_delay_us(55);
	LCD_write(cursor); // enable display 
	_delay_us(100);	
	LCD_write(0x02);
	_delay_ms(30);
}
Exemplo n.º 22
0
void lcdClear() {
	lcdCmd(0x01);
	_delay_us(3300);
}
Exemplo n.º 23
0
Arquivo: lcd.c Projeto: jcmvbkbc/stove
void lcd_putc(char c)
{
	lcd_write(1, c);
	_delay_us(40);
	lcd_addr = (lcd_addr + 1) & LCD_ADDR_MASK;
}
Exemplo n.º 24
0
void sftbit(uint8_t c)
{
	if(c) sbi(TXPORT,TXD); else cbi(TXPORT,TXD);
	_delay_us(100);
}
Exemplo n.º 25
0
Arquivo: lcd.c Projeto: jcmvbkbc/stove
void lcd_on(uint8_t mode)
{
	lcd_write(0, 0x8 | (mode & 7));
	_delay_us(40);
}
Exemplo n.º 26
0
void lcd_init(char cursor_onoff, char cursor_blinking) {
#if (LCD_USED_CONTROLLER==LCD_CONTROLLER_KS0066) || (LCD_USED_CONTROLLER==LCD_CONTROLLER_HD44780)
    #warning "                 LCD library uses KS0066 or HD44780 controller"
    LCDPORTS_OUT();
    LCD_RS_DDR |= (1<<LCD_RS);
#if LCD_RW_CONNECTED!=0
    LCD_RW_DDR |= (1<<LCD_RW);
#endif
    LCD_EN_DDR |= (1<<LCD_EN);
    _delay_ms(40);

    // muss 3mal hintereinander gesendet werden zur Initialisierung

    RW0();        // set RW=0
    RS0();        // RS auf 0 setzen
    EN0();        // RS auf 0 setzen
    _delay_ms(40);

    if (LCD_DATASTART==0) {
        LCD_PORT &= 0xF0;
        LCD_PORT |= 0x03;               // setzen
    } else {
        LCD_PORT &= 0x0F;
        LCD_PORT |= (0x03<<4);               // setzen
    }

    LCD_RS_PORT &= ~(1<<LCD_RS);      // RS auf 0
    lcd_enable();

    _delay_ms(1);
    lcd_enable();

    _delay_ms(5);
    lcd_enable();
    _delay_ms(5);

    // 4 Bit Modus aktivieren
    if (LCD_DATASTART==0) {
        LCD_PORT &= 0xF0;
        LCD_PORT |= 0x02;               // setzen
    } else {
        LCD_PORT &= 0x0F;
        LCD_PORT |= (0x02<<4);               // setzen
    }
    lcd_enable();
    _delay_ms(5);

    // 4Bit / 2 Zeilen / 5x7
    lcd_command(0x28);

    // Display ein / Cursor aus / kein Blinken
    lcd_command(0x0C);

    // inkrement / kein Scrollen
    lcd_command(0x06);

    lcd_clear();
#elif (LCD_USED_CONTROLLER==LCD_CONTROLLER_ST7036)
    #warning "                 LCD library uses ST7036 controller"
    // set 4 data ports as outputs and all control signals as outputs
    LCDPORTS_OUT();
    LCD_RS_DDR |= (1<<LCD_RS);
    LCD_RW_DDR |= (1<<LCD_RW);
    LCD_EN_DDR |= (1<<LCD_EN);
    _delay_ms(20);

    RW0();
    RS0();
    EN0();
    _delay_ms(100);

    #if (LCD_INTERFACE == LCD_INTERFACE_4BIT)
        #warning "                 4 Bit Interface"
            // init 4-bit by sending 0b0011XXXX three times to the display
            lcd_data_out(0x03);
            _delay_ms(10);
            //lcd_enable();
            lcd_data_out(0x03);
            _delay_ms(10);
            lcd_data_out(0x03);
            _delay_ms(10);

            lcd_data_out(0x02); // function set 0b0010XXXX
            _delay_ms(10);
    #elif (LCD_INTERFACE == LCD_INTERFACE_8BIT || LCD_INTERFACE == LCD_INTERFACE_8BIT_SHIFTREG)
        #warning "                 8 Bit Interface"
            lcd_data_out(0x39);
            _delay_us(LCD_SHORTDELAY);
            lcd_data_out(0x39);
            _delay_us(LCD_SHORTDELAY);
    #endif
    uint8_t c=0x31;
    #if (LCD_VOLTAGE == LCD_VOLTAGE_5V)
        #warning "                 5V supply voltage"
        #if (LCD_LINECOUNT>1)
        c=c + 0x08;
        #endif
        lcd_command(c);    // function set        0b0011<N><DH><IS2><IS1>
                           //                     0b0011 N  0   0    1    =0x31 & (N*0x08)
        lcd_command(0x1D); // intern. osc freq.   0b0001<BS><F2><F1><F0>
                           //                     0b0001 1   1   0   1    =0x1D
        lcd_command(0x50); // power/ICON/contrast 0b0101<Ion><Bon><C5><C4>
                           //                     0b0101 0    0    0   0    =0x50
        lcd_command(0x6C); // follower control    0b0110<Fon><Rab2><Rab1><Rab0>
                           //                     0b0110  1    1     0     0    =0x6C
        lcd_command(0x7C); // contrast set        0b0111<C3><C2><C1><C0>
                           //                     0b0111 1   1   0   0    =0x7C
    #elif (LCD_VOLTAGE == LCD_VOLTAGE_3V)
        #warning "                 3V supply voltage"
        #if (LCD_LINECOUNT>1)
        c=c + 0x08;
        #endif
        lcd_command(c);    // function set        0b0011<N><DH><IS2><IS1>
                           //                     0b0011 N  0   0    1    =0x31 & (N*0x08)
        lcd_command(0x15); // intern. osc freq.   0b0001<BS><F2><F1><F0>
                           //                     0b0001 0   1   0   1    =0x15
        lcd_command(0x55); // power/ICON/contrast 0b0101<Ion><Bon><C5><C4>
                           //                     0b0101 0    1    0   1    =0x55
        lcd_command(0x6E); // follower control    0b0110<Fon><Rab2><Rab1><Rab0>
                           //                     0b0110  1    1     1     0    =0x6E
        lcd_command(0x72); // contrast set        0b0111<C3><C2><C1><C0>
                           //                     0b0111 0   0   1   0    =0x72
    #endif

    lcd_command(c & 0b11111100); // switch back to instruction table 0  0b0011<N>000  =0x38


#endif
    lcd_setcursor(1, cursor_onoff, cursor_blinking);
    lcd_setcontrast(LCD_DEFAULTCONTRAST);
    lcd_clear();
    lcd_home();
}
Exemplo n.º 27
0
Arquivo: lcd.c Projeto: jcmvbkbc/stove
static inline void lcd_set_addr(uint8_t addr)
{
	lcd_addr = addr & LCD_ADDR_MASK;
	lcd_write(0, 0x80 | lcd_addr);
	_delay_us(40);
}
Exemplo n.º 28
0
// Sendet den Befehl zur Löschung des Displays
void lcd_clear(void) {
    lcd_command(LCD_INSTRUCTION_CLEAR);
    _delay_us(LCD_LONGDELAY);
}
Exemplo n.º 29
0
void set_display_cursor_blink(char s)
{
  commandWrite(0x08 | (s&0x07));
  _delay_us(60);
}
/**
 Turn on transmitter, and transmits the data loaded into the buffer
*/
void NRF24L01_R_RF_TX(void) {
	NRF24L01_R_CE_LOW;
	NRF24L01_R_CE_HIGH;
	_delay_us(10);
	NRF24L01_R_CE_LOW;
}