Button::Button()
{
    // setup interrupt-driven keypad arrays
    // reset button arrays
    char p;
    
    verticalRepeat = 0;

    for(uint8_t i = 0; i < NUM_KEYS; i++)
    {
        button_count[i] = 0;
        button_status[i] = 0;
        button_flag[i] = 0;
        p = pgm_read_byte(&button_pins[i]);
        
        if(i < 2)
        {
            clrBit(p, FB_DDR);
            setBit(p, FB_PORT);
        } 
        else
        {
            clrBit(p, B_DDR);
            setBit(p, B_PORT);
        }
    }
}
Beispiel #2
0
uint16_t battery_read_raw()
{
  setBit(PF1, DDRF); // Powers Sensor //
  clrBit(PF1, PORTF);
  _delay_ms(10);
  uint16_t battery = hardware_analogRead(2);
  clrBit(PF1, DDRF); // Shuts down Sensor //
  return battery;
}
int main()
{
	int pos = 0;
	int config = 0;
	int flash = 0;

	FIO3DIR = 0xFFFFFFFF;

	for (;;) {
		switch (input_getButtonPress()) {
			case BUTTON_LEFT:
				pos = (pos + 1) & 31;
				break;
			case BUTTON_RIGHT:
				pos = (pos - 1) & 31;
				break;
			case BUTTON_UP:
				config = setBit(config, pos);
				break;
			case BUTTON_DOWN:
				config = clrBit(config, pos);
				break;
			case BUTTON_CENTER:
				config = cpyBit(config, pos, 1 ^ getBit(config, pos));
				break;
			case BUTTON_NONE:
				FIO3PIN = cpyBit(config, pos, flash < 125);
				flash = (flash + 1) % (250);
				wait(1);
				break;
		}
	}

	return 0;
}
Beispiel #4
0
void OLED::initOLED()
{
	//unsigned char i;
	//make scl high
	setBit(OLED_scl_port,OLED_scl_pin);
	//make rst low
	clrBit(OLED_rst_port,OLED_rst_pin);
	//delay 50 ms
	_delay_ms(50);
	//make rst high
	setBit(OLED_rst_port,OLED_rst_pin);
	setDisplayOnOff(0x00);		  // Display Off (0x00/0x01)
	setDisplayClock(0x80);		  // Set Clock as 100 Frames/Sec
	setMultiplexRatio(0x3F);	  // 1/64 Duty (0x0F~0x3F)
	setDisplayOffset(0x00);		  // Shift Mapping RAM Counter (0x00~0x3F)
	setStartLine(0x00);			  // Set Mapping RAM Display Start Line (0x00~0x3F)
	setChargePump(0x04);		  // Enable Embedded DC/DC Converter (0x00/0x04)
	setAddressingMode(0x02);	  // Set Page Addressing Mode (0x00/0x01/0x02)
	setSegmentRemap(0x01);	  // Set SEG/Column Mapping
	setCommonRemap(0x08);	  // Set COM/Row Scan Direction
	setCommonConfig(0x10);	  // Set Sequential Configuration (0x00/0x10)
	setContrastControl(0xCF); // Set SEG Output Current
	setPrechargePeriod(0xF1);	  // Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
	setVCOMH(0x40);			  // Set VCOM Deselect Level
	setEntireDisplay(0x00);		  // Disable Entire Display On (0x00/0x01)
	setInverseDisplay(0x00);	  // Disable Inverse Display On (0x00/0x01)
	setDisplayOnOff(0x01);		  // Display On (0x00/0x01)
	fill(0x00);                               //clear all
	setPos(0,0);
}
Beispiel #5
0
/**
 * @brief Configura as entradas e saídas
 */
void ioSetup(void)
{
	// set as input
	clrBit(DDRD, BIT_aciBomba1);//!<Coloca o pino em zero "Como entrada"
	clrBit(DDRD, BIT_aciBomba2);
	clrBit(DDRD, BIT_aciMPPT);
	clrBit(DDRD, BIT_aux1);
	clrBit(DDRD, BIT_aux2);

	// set pull-up
	setBit(PORTD, BIT_aciBomba1);
	setBit(PORTD, BIT_aciBomba2);
	setBit(PORTD, BIT_aciMPPT);
	setBit(PORTD, BIT_aux1);
	setBit(PORTD, BIT_aux2);
}
Beispiel #6
0
unsigned int hardware_readLight(uint8_t r)
{
  // Need to power off lights //
  hardware_flashlight(0);
  char backlightVal = lcd.getBacklight();
  lcd.backlight(0);
  if(backlightVal > 0) _delay_ms(50);
  if(r > 2) r = 2;
  DDRA &= ~0b00000111; // clear all //
  PORTA &= ~0b00000111; // clear all //
  setBit(r, DDRA); // Powers Sensor //
  clrBit(r, PORTA);
  _delay_ms(50);
  uint16_t light = hardware_analogRead(0);
  clrBit(r, DDRA); // Shuts down Sensor //
  if(backlightVal > lcd.getBacklight()) lcd.backlight(backlightVal);
  return light;
}
Beispiel #7
0
void OLED::write(char data,bool isData)
{
	if(isData==false)
	{
		//execute command
		//make DC pin low
		clrBit(OLED_dc_port,OLED_dc_pin);
		
		
	}
	else
	{
		//execute data
		//make DC pin high
		setBit(OLED_dc_port,OLED_dc_pin);
	}
	
	//make scl low
	clrBit(OLED_scl_port,OLED_scl_pin);
	unsigned char i=8;
	while(i--)
	{
		if(data & 0x80)
		{
			//make data pin high
			setBit(OLED_sda_port,OLED_sda_pin);
		}
		else 
		{
			//make data pin low
			clrBit(OLED_sda_port,OLED_sda_pin);
		}
		//delay
		_delay_us(usDelay);
		//make scl pin high
		setBit(OLED_scl_port,OLED_scl_pin);
		//delay
		_delay_us(usDelay);
		//make scl pin low
		clrBit(OLED_scl_port,OLED_scl_pin);
		data<<=1;		//left shift data by 1
	}
	
}
void hardware_light_enable(uint8_t level)
{
    hardware_flashlight(0);
        
    if(level > 2) 
        level = 2;
    
    DDRA &= ~0b00000111; // clear all //
    PORTA &= ~0b00000111; // clear all //
    
    setBit(level, DDRA); // Powers Sensor //
    clrBit(level, PORTA);
}
Beispiel #9
0
unsigned char OLED::read(void)
{
	char data=0;
	clrBit(OLED_scl_port,OLED_scl_pin);
	setBit(OLED_sda_port,OLED_sda_pin);	//make sda high
	clrBit(OLED_sda_port,OLED_sda_pin);	//make sda input
	for(int i=0 ; i<8 ;i++)
	{
		//make scl high
		setBit(OLED_scl_port,OLED_scl_pin);
		_delay_us(usDelay);//delay
		if(ifHigh(OLED_sda_read,OLED_sda_pin))	//check if sda then update data
		{
			data|=0x01;
		}
		clrBit(OLED_scl_port,OLED_scl_pin);		//make scl low 
		_delay_us(usDelay);						//delay
		data=data<<1;							//left shift data
	}
	//not sending ack!!
	return data;								//return data
}
Beispiel #10
0
Cspi::Cspi(u08 spiNum, Cpin* _pinSS, SPI_INTLVL_t _intLevel,
		SPI_PRESCALER_t _prescaler, SPI_MODE_t _mode, bool msbFirst,
		bool master, bool clk2X) {
	switch (spiNum) {
	case 0:
		base = &SPIC;
		break;
	case 1:
		base = &SPID;
		break;
	default:
		while (1) {
		}
		break;
	}
	this->pinSS = _pinSS;
	this->intLevel = _intLevel;
	this->mode = _mode;
	setGroup(base->INTCTRL, SPI_INTLVL_gm, _intLevel);
	setGroup(base->CTRL, SPI_PRESCALER_gm, _prescaler);
	setGroup(base->CTRL, SPI_MODE_gm, _mode);
	if (msbFirst) {
		clrBit(base->CTRL, SPI_DORD_bp);
	} else {
		setBit(base->CTRL, SPI_DORD_bp);
	}
	if (master) {
		setBit(base->CTRL, SPI_MASTER_bp);
	} else {
		clrBit(base->CTRL, SPI_MASTER_bp);
	}
	if (clk2X) {
		setBit(base->CTRL, SPI_CLK2X_bp);
	} else {
		clrBit(base->CTRL, SPI_CLK2X_bp);
	}
	setBit(base->CTRL, SPI_ENABLE_bp);
}
Beispiel #11
0
int main() {

	usartInit(57600);
	usartEnableReceiver();
	usartEnableTransmitter();
	usartStdio();

	setBit(DDRB, M1_DIR);
	setBit(DDRD, M1_PWM);
	setBit(DDRD, M2_DIR);
	setBit(DDRD, M2_PWM);

	clrBit(PORTB, M1_DIR);
	clrBit(PORTD, M1_PWM);
	clrBit(PORTD, M2_PWM);
	clrBit(PORTD, M2_DIR);

	timer0FastPWMMaxMode();
	timer0ClockPrescaller64();
	timer0OC0ANonInvertedMode();
	timer0OC0BNonInvertedMode();
	timer0SetCompareAValue(0);
	timer0SetCompareBValue(0);
	timer0DeactivateCompareAInterrupt();
	timer0DeactivateCompareBInterrupt();

	timer2ClockPrescaller256();
	timer2NormalMode();
	timer2OC2AOff();
	timer2OC2BOff();
	timer2ActivateOverflowInterrupt();


	i2c_init();

	adxl345_init();

	l3g4200d_init();
	l3g4200d_setoffset(0.11, -1.71, -0.46);
	//l3g4200d_settemperatureref();

	PID_init(&PID, 15,0.2,1, 0);
	PID_SetMode(&PID, 1);
	PID_SetOutputLimits(&PID, -255, 255);

	PID.mySetpoint = 0;

	motor1(0);
	motor2(0);


//	printf("\e[1;1H\e[2J");
//	printf("STARTED!\r\n");
//	_delay_ms(200);

//	motor1(120);
//	printf("Motor1 = 120\r\n");
//	_delay_ms(500);
//	motor1(-120);
//	printf("Motor1 = -120\r\n");
//	_delay_ms(500);
//	motor1(120);
//	printf("Motor1 = 120\r\n");
//	_delay_ms(500);
//	motor1(0);
//	printf("Motor1 = 0\r\n");

	sei();

	while(1);

	return 0;
}
Beispiel #12
0
/**
 * @brief Given a bitplane w by h in curBits, generates a new bitplane in 
 * nextBits, using a cellular automaton defined by rule.
 *  
 *  Generation rules are defined using the convention of NKS chapter 5 
 *  for totalistic 2D cellular automata:  
 *  
 *  The last binary digit specifies what color the center cell should
 *  be if all its neighbors were white on the previous step, and it
 *  too was white. The second to last digit specifies what happens if 
 *  all the cells are white, but the center cell itself is black.
 *  Each earlier digit then specifies what should happen if
 *  progressively more neighbor cells are black.
 * 
 *  Cells can be counted as 'neighbors' four ways:
 * 
 *  1) 4 rectangular positions e.g. above, below, left, right
 *  2) 4 diagonal positions e.g. above-left, above-right, below-left, below-right
 *  3) 6 hexagonal neighbor positions on hexagonal grid
 *  4) 8 adjacent positions, e.g. 4 rectangular plus 4 diagonal positions
 * 
 *  example:
 * 
 *  using rule = RECT_NEIGHBORS | 451,  a single black cell iterated for 44
 *  generations will result in the 1st pattern in the top row on page 174 of NKS
 *
 */
void nextGen( BitSequence *curBits,			///<[in] current bitplane
					BitSequence *nextBits,	///<[out] next bitplane
					int w,					///<[in] width of bitplane
					int h,					///<[in] height of bitplane
					int rule)				///<[in] generation rule
{
	int x,y;
	if((rule & ALL_NEIGHBORS) == ALL_NEIGHBORS)
	{
		for(y=0;y<h;y++)
		{
			for(x=0;x<w;x++)
			{
				bool ibit = getBit(curBits,w,h,x,y);
				bool obit = false; 
				int k = 2*totalNeighbors(curBits,w,h,x,y);
				if(ibit)
				{
					++k;
				}
				obit = ((rule & (1 << k)) != 0);

				if(obit)
				{
					setBit(nextBits,w,h,x,y);
				}
				else 
				{
					clrBit(nextBits,w,h,x,y);
				}
			}
		}
	} 
	else if(rule & RECT_NEIGHBORS)
	{
		for( y=0;y<h;y++)
		{
			for(x=0;x<w;x++)
			{
				bool ibit = getBit(curBits,w,h,x,y);
				bool obit = false;
				int k = 2*totalNeighborsRect(curBits,w,h,x,y);
				if(ibit)
				{
					++k;
				}
				obit = ((rule & (1 << k)) != 0);

				if(obit)
				{
					setBit(nextBits,w,h,x,y);
				}
				else 
				{
					clrBit(nextBits,w,h,x,y);
				}
			}
		}
	} 
	else if(rule & DIAG_NEIGHBORS)
	{
		for(y=0;y<h;y++)
		{
			for(x=0;x<w;x++)
			{
				bool ibit = getBit(curBits,w,h,x,y);
				bool obit = false;
				int k = 2*totalNeighborsDiag(curBits,w,h,x,y);
				if(ibit)
				{
					++k;
				}
				obit = ((rule & (1 << k)) != 0);

				if(obit)
				{
					setBit(nextBits,w,h,x,y);
				}
				else 
				{
					clrBit(nextBits,w,h,x,y);
				}
			}
		}
	}
	else if(rule & HEX_NEIGHBORS)
	{
		for(y=0;y<h;y++)
		{
			for(x=0;x<w;x++)
			{
				bool ibit = getBit(curBits,w,h,x,y);
				bool obit = false;
				int k = 2*totalNeighborsHex(curBits,w,h,x,y);
				if(ibit)
				{
					++k;
				}
				obit = ((rule & (1 << k)) != 0);

				if(obit)
				{
					setBit(nextBits,w,h,x,y);
				}
				else 
				{
					clrBit(nextBits,w,h,x,y);
				}
			}
		}
	}
	return;
}