int main(void) { unsigned constrained = 0, shutOffBuzzer = 0; //Set GPIO Pins if(!shutOffBuzzer) DDRB|=0b00100000; //PIN 13 output to buzzer DDRB|=0b00001000; sei(); setupADC(); setupPWM(); startPWM(); count=0; InitTimer0(); StartTimer0(); while(1) { if(state) pwm_val = remap(adc_val,100,900); else { //0-255 steps for tone strength constrained = remap(adc_val,619,890); //Output tone tone(constrained); } } }
void setServoAngle( uint8_t channel, uint8_t angle ){ uint8_t pulseLength = 40 + angle*200/180; pulseLength = pulseLength*PWM_TIME_PERIOD_BASE/PWM_TIME_PERIOD; updatePWM(channel, pulseLength); startPWM(); timer_delay_ms(2000); stopPWM(); timer_delay_ms(2000); }
void initPWM(void) { // PORTB as output DDRB = 0xFF; TCCR1B = 1; startPWM(); }
/** * Initializes a motor object * * mode=freewheel,forward,backward,brake */ void init_motor(motor_t* motor, uintptr_t port, char input_1_pin, char input_2_pin, char pwmPin){ motor->cnt_port=port; motor->input_1_pin=input_1_pin; motor->input_2_pin=input_2_pin; motor_setMode(motor,freewheel); startPWM(&motor->pwm,port,0x1<<pwmPin,0,255);//pwm out on pin A0 }
void __attribute__((__interrupt__, __auto_psv__)) _T1Interrupt(void) { IFS0bits.T1IF = 0; if (listening) { if (timer1_counter == 50) { timer1_counter = 0; listening = false; send_ping = true; RTT1_received = false; RTT2_received = false; RTT1_overflows = 0; RTT2_overflows = 0; RTT1_time = 0; RTT2_time = 0; stopTimerRTT(); timer2_counter = 0; // IEC0bits.ADIE = 0; IFS0bits.ADIF = 0; ADSTATbits.P0RDY= 0; } else { ++timer1_counter; } } else if (ping_send) { stopPWM(); startTimerRTT(); ping_send = false; blocking_sensors = true; } else if (send_ping) { initPWM(); startPWM(); // Sent for 1ms send_ping = false; ping_send = true; } else if (blocking_sensors && timer1_counter == 15) { // Blocked during 15ms timer1_counter = 0; blocking_sensors = false; listening = true; /* Launch ADC */ // IEC0bits.ADIE = 1; ADCPC0bits.SWTRG0 = 1; } else { ++timer1_counter; } }
int main (void) { uint32_t analogValue; uint8_t wantedDutyCycle, wantedDutyCycleB=100; uint8_t state = 0; //Set LED1-LED8 pins as outputs GPIOSetDir( LED1_PORT, LED1_PIN, GPIO_OUTPUT); GPIOSetValue( LED1_PORT, LED1_PIN, LED_OFF); GPIOSetDir( LED2_PORT, LED2_PIN, GPIO_OUTPUT); GPIOSetValue( LED2_PORT, LED2_PIN, LED_OFF); GPIOSetDir( LED3_PORT, LED3_PIN, GPIO_OUTPUT); GPIOSetValue( LED3_PORT, LED3_PIN, LED_OFF); GPIOSetDir( LED4_PORT, LED4_PIN, GPIO_OUTPUT); GPIOSetValue( LED4_PORT, LED4_PIN, LED_OFF); GPIOSetDir( LED5_PORT, LED5_PIN, GPIO_OUTPUT); GPIOSetValue( LED5_PORT, LED5_PIN, LED_OFF); GPIOSetDir( LED6_PORT, LED6_PIN, GPIO_OUTPUT); GPIOSetValue( LED6_PORT, LED6_PIN, LED_OFF); GPIOSetDir( LED7_PORT, LED7_PIN, GPIO_OUTPUT); GPIOSetValue( LED7_PORT, LED7_PIN, LED_OFF); GPIOSetDir( LED8_PORT, LED8_PIN, GPIO_OUTPUT); GPIOSetValue( LED8_PORT, LED8_PIN, LED_OFF); //Set SW2/SW3 pins as inputs GPIOSetDir( SW2_PORT, SW2_PIN, GPIO_INPUT); GPIOSetDir( SW3_PORT, SW3_PIN, GPIO_INPUT); //Extra, turn buzzer off GPIOSetDir( BUZZ_PORT, BUZZ_PIN, GPIO_OUTPUT); GPIOSetValue( BUZZ_PORT, BUZZ_PIN, BUZZ_OFF); //Set RGB-LED pins as outputs GPIOSetDir( LEDB_PORT, LEDB_PIN, GPIO_OUTPUT); GPIOSetValue( LEDB_PORT, LEDB_PIN, LED_OFF); initPWM(1000); //1000us = 1kHz PWM frequency updatePWM(0, 100); //set 100% duty cycle for channel #0 (RED LED will be off) updatePWM(1, 100); //set 100% duty cycle for channel #1 (GREEN LED will be off) startPWM(); //Initialize ADC peripheral and pin-mixing ADCInit(4500000); //4.5MHz ADC clock //get initial value analogValue = getADC(AIN0); //AIN0 = trimming pot for intensity wantedDutyCycleB = 100; state = 0; while(1) { uint8_t loopCounter; uint16_t adcCounter; //Set BLUE LED output high GPIOSetValue( LEDB_PORT, LEDB_PIN, 1); //check if time to read analog input if (adcCounter++ > 100) { adcCounter = 0; //check push-button SW2 if (GPIOGetValue(SW2_PORT, SW2_PIN) == SW_PRESSED) { state++; if (state > 2) state = 0; //wait until push-button is released while(GPIOGetValue(SW2_PORT, SW2_PIN) == SW_PRESSED) ; } //Set wanted duty cycle - valid numbers: 0..100 (low number = LED on more) wantedDutyCycle = getADC(AIN0) / 10; //trimming pot //extra check to that range is valid if (wantedDutyCycle > 99) wantedDutyCycle = 99; switch(state) { case 0: updatePWM(0, wantedDutyCycle); break; case 1: updatePWM(1, wantedDutyCycle); break; case 2: wantedDutyCycleB = wantedDutyCycle; break; default: state = 0; } } //Enter duty cycle generating loop for (loopCounter=0; loopCounter<100; loopCounter++) { //10 corresponds to 1kHz (100*10us), LED flickering starts at around 30-40Hz PWM frequency delayUS(10); if (loopCounter == wantedDutyCycleB) GPIOSetValue( LEDB_PORT, LEDB_PIN, 0); //Set output low } } return 0; }