int main()
{
	// Process Ctrl+C to terminate the application
	signal(SIGINT, close);

	if (-1 == wiringPiSetup())
	{
		printf("setup wiringPi failed!\n");
		return 1;
	}

	// Enable PWM and set max value
	enablePWM(pinCtrl, 5);

	// Infinite loop
	for(;;) 
	{
		// Increase brightness from 20% to 100%
		for (int fadeIn=1; fadeIn<6;fadeIn++)
		{
			printf("Brightness: %d%\n", fadeIn*20);
			softPwmWrite(pinCtrl, fadeIn);
			delay(3000);
		}

		// Decrease brightness from 80% to 40%
		for (int fadeOut=4; fadeOut>1; fadeOut--)
		{
			printf("Brightness: %d%\n", fadeOut*20);
			softPwmWrite(pinCtrl, fadeOut);
			delay(3000);
		}
	}
	return 0;
}
void SPIPWMworker(void)
{
	unsigned char token, instruction, fInst=0,fb=0,bf=0;
	initSPI();
	SSP2BUF = 0xFD;
	enablePWM();
   while (1)
	{
		while(PORTCbits.RC0 ==0)//CS LOW
 	  	{
  	 		if(SSP2STATbits.BF)	// we received something :)
			{
				bf=1;
      		if(fb==0)
				{
					token = SSP2BUF;
					instruction = token & SPIEEMASK;
				}
				fb=1;
				switch (instruction)
				{
					case 0:
						if(fInst)
						{
							if(fInst==1)setPeriod(SSP2BUF);
							fInst++;
						}
						else
						{
							fInst=1;
						}
						SSP2BUF = PWMperiod;
						break;
					case 1:
						if(fInst)
						{
							if(fInst==1)setDuty(SSP2BUF);
							fInst++;
						}
						else
						{
							fInst=1;
						}
						SSP2BUF = PWMduty;
						break;
				}
			}
		}
		if(bf)	//CS HIGH
		{
			fb=0;
			bf=0;
			fInst = 0;
		}	
	}

}
Exemplo n.º 3
0
int main(void)

{
        int fileHandleGPIO_LED;
        int fileHandleGPIO_PROXY;
        int i=0;

        puts("Starting proximity reader on Galileo board.");
        fileHandleGPIO_PROXY = openGPIO(GP_PROXY, GPIO_DIRECTION_IN);
        if(ERROR ==  fileHandleGPIO_PROXY)
        {
        		puts("Unable to open toggle Proximity port #8");
        		return(-1);
        }
        fileHandleGPIO_LED = openGPIO(GP_LED, GPIO_DIRECTION_OUT);

        if(ERROR ==  fileHandleGPIO_LED)

        {
        		puts("Unable to open toggle LED port #13");
        		return(-1);

        }


        //Switch off the LED before starting.
        writeGPIO(fileHandleGPIO_LED, 0);

        //set PWM parameters
        openPWM(GP_PWM);
        setPWMPeriod(1000000,GP_PWM);
        enablePWM(1,GP_PWM);
		setPWMDutyCycle(0,GP_PWM);


        //Start an infinite loop to keep polling for proximity info
        int proxyValue = 0;
        while(1==1)
        {
			proxyValue = readGPIO(fileHandleGPIO_PROXY,GP_PROXY);
			if(proxyValue == 1)
			{
				if(duty_cycle == 500000)
				{
					duty_cycle = 200000;
		        	writeGPIO(fileHandleGPIO_LED, 0);
				}
				else
				{
					duty_cycle = 500000;
		        	writeGPIO(fileHandleGPIO_LED, 1);
				}
				setPWMDutyCycle(duty_cycle,GP_PWM);
			}
			else
			{
				duty_cycle = 50000;
				setPWMDutyCycle(0,GP_PWM);
	        	writeGPIO(fileHandleGPIO_LED, 0);
			}
        	usleep(1000*400);
        }


        closeGPIO(GP_LED, fileHandleGPIO_LED);
        closeGPIO(GP_PROXY, fileHandleGPIO_PROXY);
        closePWM(GP_PWM);

        puts("Finished BURGLER ALARM on Galileo board.");
        return 0;
}
void UARTworker(void)
{
	unsigned char c,mode=0,addr=0,instruction=0,EEaddrF=0,EEaddr=0,adcc=0,helpC;
	initUART();
	//write start message (menu)
	UARTwriteString(msgMenu[0]);
	UARTwrite('\n');
	while(1)
	{	

		if(RCIF)						
		{	
			RCIF=0;
			LED2ON;						
			if(!(RCSTA&0b00000110))		
			{	rhead++;				
				rhead&=RINGBUFFMASK;	
				ringbuff[rhead]=RCREG;	
			}							
			LED2OFF;
			c=UARTread();
			UARTwrite(c);
			//c=UARTcharFromString(c);
			switch (mode)
			{
				case 0:
					mode=c-48;
					UARTwriteString(msgMenu[c-48]);
					if(mode==2)enablePWM();
					else if(mode==3)enableDAC();
					break;
				case 1://ADC
					switch(c)
					{
						case 'r'://single read
							UARTwriteString("\n\nADC value: ");
							helpC=getADC(adcc);
							UARTwriteDecimal(helpC);
							UARTwriteString(msgMenu[1]);
							break;
						case '1'://chanell one
							UARTwriteString("\n\nchannel 1 selected");
							adcc=0;
							UARTwriteString(msgMenu[1]);
							break;
						case '2'://chanel two
							UARTwriteString("\n\nchannel 2 selected");
							adcc=1;
							UARTwriteString(msgMenu[1]);
							break;
						case '3'://chanell three
							UARTwriteString("\n\nchannel 3 selected");
							adcc=2;
							UARTwriteString(msgMenu[1]);
							break;
						case 't'://temp
							UARTwriteString("\n\nTemp sensor selected");
							adcc=3;
							UARTwriteString(msgMenu[1]);
							break;
						case 'm'://back to start
							mode = 0;
							UARTwriteString(msgMenu[0]);	
							break;
						default:
							break;
					}
					break;
				case 2://PWM
					if(instruction)
					{
						switch(instruction)
						{	
							case 'p':
								//pwm period = c;
								setPeriod(UARTcharFromString(c));
								UARTwriteString(msgMenu[2]);
								break;
							case 'd':
								setDuty(UARTcharFromString(c));
								UARTwriteString(msgMenu[2]);
								//pwm period =c;	
								break;
							case 'm':
								mode =0;
								//pwm off
								UARTwriteString(msgMenu[0]);	
								break;
							default:
								break;
						}
						instruction = 0;						
					}
					else
					{
						instruction = c;				//loads the instruction
						if(instruction == 'p')
						{
							UARTwriteString("\n\nEnter the PWM Period: ");
							
						}
						else if(instruction == 'd')
						{
							UARTwriteString("\n\nEnter the PWM Duty Cycle: ");
						}
						else if(instruction == 'm')		//if it's m goes back to the start menu...
						{
							mode =0;						
							instruction =0;
							disablePWM();
							UARTwriteString(msgMenu[0]);
						}
					}
					break;
				case 3://DAC
					if(instruction)
					{
						switch(instruction)
						{
							case 'v':	//enter woltage
								setDAC(UARTcharFromString(c));
								UARTwriteString(msgMenu[3]);
								break;
							case 'm':
								mode = 0;
								UARTwriteString(msgMenu[0]);
								break;
							default:	
								break;
						}
						instruction =0;
					}
					else
					{
						instruction = c;				//loads the instruction
						if(instruction == 'v')
						{
							UARTwriteString(msgDACsetV);
						}
						else if(instruction == 'm')		//if it's m goes back to the start menu...
						{
							mode =0;						
							instruction =0;
							disableDAC();
							UARTwriteString(msgMenu[0]);
						}
					}
					break;
				case 4://MEM
					if(instruction)		//if instruction has been sent previusly
					{
						if(EEaddrF)			//instruction was sent previusly, check if address was sent
						{						//address was sent
							if(instruction == 'w')		//if instruction was W-writes recived character to EEProm[ADDR]
							{
								EEPROMwrite(EEaddr,UARTcharFromString(c));
								UARTwriteString(msgMenu[4]);
								//write c to eeprom
							}
							else if (instruction == 'r')	//if instruction was R-reads EEprom[addr] from eeprom
							{
								UARTwriteDecimal(EEPROMread(EEaddr));
								UARTwriteString(msgMenu[4]);
							}
							else if (instruction == 'm')	//if instruction was m --returns to start menu...
							{
								mode = 0;
								UARTwriteString(msgMenu[0]);
							}
							EEaddrF=0;							//clears the addressing flag
							instruction =0;					//clears the istruction flag
						}
						else
						{
							EEaddrF=1;						//sets the address flage
							EEaddr=UARTcharFromString(c);
							
							if(instruction=='w')UARTwriteString(msgEEw);
							else if(instruction == 'r')UARTwriteString("\n\nHit any key to read from EEPROM.\n\n"); 
						}
					}
					else 
					{
						instruction = c;				//loads the instruction
						if((instruction == 'w')||(instruction == 'r'))
						{
							UARTwriteString(msgEEaddr);
						}
						else if(instruction == 'm')		//if it's m goes back to the start menu...
						{
							mode =0;						
							instruction =0;
							UARTwriteString(msgMenu[0]);
						}
					}
					break;
				default:
					mode=0;
					UARTwriteString(msgMenu[0]);
					break;				
			}				
		}								
	}
}