Пример #1
0
int main()
{
	get_mcusr(); //Why did we wake up?


        ADCSRA=0b10000000;                      //ADC Enable,ADC Start Conversion,division factor 128,Auto trigger enable
        sbi(MCUCR, PUD);
    	// PB0, PB1, PB3, PB4 are output
	// PB2 is the sensor
	//setup
	TCCR0A = 0x00;  //Timer/Counter Control Register A (normal operation 0)
	TCCR0B = 0x05;  //Timer/Counter Control Register B (2 = clkI/O/8 (From prescaler))
	TIMSK = 0x02;   //Timer/Counter Interrupt Mask Register  (2 = Timer/Counter1 Overflow Interrupt Enable)
	DDRB = 0x1b;    //Port B Data Direction Register
			//1e = DDB4 DDB3 DDB2 DDB1 are outputs?
	PORTB = 0x1b;   //Port B Data Register
			//1e = DDB4 DDB3 DDB2 DDB1 are inputs?
			//DD PO IO     Pullup  Comment
			//1  1  Output No      Output High (Source) (page 57)
	

	sei();  //Set Interrupst enabled
	

//	testpattern();
	wdt_enable(WDTO_8S);

	for(;;)
	{
	check_if_sleepy();
	for(int j = 0; j < NumberOfFlys; j++)
	      {

	        if (flystatus[j] == 1)
       		 {
        	   if (brightness[j] < 500) brightness[j]+=10;
       		 }
     	       else //wantedstate=0
       		 { 
    		   if (brightness[j] > 0) brightness[j]-=10;
                }
       
       
     	      if (brightness[j] > (rand() % 512))
      		 {
        	    currentstate[j]=1;
     		  }
      		     else
      		 {
   	         currentstate[j]=0;
      		 }
		DDRB = 0;
		PORTB = 1<<anode[j]|~currentstate[j]<<cathode[j];
		DDRB = 1<<anode[j]|1<<cathode[j];
        	}
	
	 wdt_reset();
	}//End Loop
}//End Main
Пример #2
0
int main(void)
{	
	unsigned char tempSec;
	get_mcusr(); // Disable the watchdog timer
	uartInit(); // Init both UARTs
	uartSetBaudRate(0,BAUD);
	uartSetBaudRate(1,BAUD);
	stdout = stdin = &uart1_str; // Init the stdout and stdin (printf, scanf...) functions
	stderr = &uart0_str; // Init the stderror functions
	i2cInit();
	i2cSetBitrate(100);	
	ds1307_enable(DS1307_I2C_ADDR); // Enable the DS1307 RTC
	// Make a flash on LEDs connected to PORTB when starting / restarting
	DDRB=0xff; // PORTB output
	PORTB=0xff; // All bits on port B = 1 
	_delay_ms(1000); // Wait until XBee modem has started
	PORTB=0x00; // All bits on port B = 0
	// Empty the receive buffer of both UARTs
	uartFlushReceiveBuffer(EthUART);
	uartFlushReceiveBuffer(XBeeUART);
	wdt_enable(WDTO_2S); // Enable watchdog, reset after 2 seconds
	wdt_reset();
	while(1)
	{	
		wdt_reset(); // Reset the watchdog timer		
		tempSec=time.sec;
		if(ds1307_gettime(DS1307_I2C_ADDR, &time))
		{	//if(tempSec!=time.sec)	
			if((time.sec%2==0)&&(tempSec!=time.sec)) // True every 2nd second
			{
				while(!uartReceiveBufferIsEmpty(XBeeUART))
				{
					int i;
					i=0;
					signed char XBeePacket[12];
					// Read data from slave module 
					while (i<13)
					{
						while (uartReceiveBufferIsEmpty(XBeeUART))
							;;
						// Syncronization
						uartReceiveByte(XBeeUART, &XBeePacket[i]);
						if (XBeePacket[i]=='~') // Counter is set to 0 if the current character is '~'
							i=0;
						if (XBeePacket[0]=='~') //Increment if the first character in received data is '~'
							i++;
					}					
					sm[XBeePacket[7]-'0'].temp=XBeePacket[12]-7; // Compensate for heat from components in slave module
					// Check status of slave module and make status ready to send to webserver
					if (XBeePacket[10]=='f')
						sm[XBeePacket[7]-'0'].status=0;
					else if (XBeePacket[10]=='n')
						sm[XBeePacket[7]-'0'].status=1;
					XBeePacketCounter[XBeePacket[7]-'0']=1; // Set packet counter for this slave module to 1
				}
				// Check packet counter of all modules, if false: status=2 is sent to the webserver
				// Webserver will then now which modules that are unconnected
				for (int i=0; i<noOfModules; i++)
				{		
					if (!XBeePacketCounter[i])
						sm[i].status=2;
					XBeePacketCounter[i]=0;
				}
				// Receive data from webserver
				if (checkForEthPacket(ethPacket))
				{
					if (ethPacket[pHour]<24)// edit time 
					{
						time.yr=ethPacket[pYear];
						time.mon=ethPacket[pMonth];
						time.dat=ethPacket[pDate];
						time.hr=ethPacket[pHour];
						time.min=ethPacket[pMin];
						ds1307_settime(DS1307_I2C_ADDR,time);
					}
					int number=0;
					while (number<noOfModules)
					{
						sm_web[number].type=ethPacket[pType+number*pFieldsModules];
						sm_web[number].status=ethPacket[pStatus+number*pFieldsModules];
						sm_web[number].temp=ethPacket[pTemp+number*pFieldsModules];
						switch (sm_web[number].type)
						{
							case 0:
								if (sm_web[number].status==0) // Check if the current slave module is disabled on the website
									sendSlaveModule(number,0); // Send data to slave module: turn off relay
								else
								{
									if ((sm[number].temp)<(sm_web[number].temp-1)) // Measured temperature < wanted temperature - 1 ?
										sendSlaveModule(number,1); // Send data to slave module: turn on relay
									else if ((sm[number].temp)>(sm_web[number].temp+1)) // Measured temperature > wanted temperature + 1 ?
										sendSlaveModule(number,0); // Send data to slave module: turn off relay
									else 
										sendSlaveModule(number,2); // Send data to slave module
								}
								break;
						}
						number++; // next slave module
					}
				}				
				sendEthPacket(time, sm); // send packet with data to webserver
			}
		}
	}				
	return 0;	
}