void Play_song(struct note_struct *song, int size) { int i; for (i=0; i<size; i++) { PWM_set_load(Get_Load_Value(g_multiplier*song[i].note)); PWM_set_duty_cycle(95); Timer_delay(song[i].delay); PWM_set_duty_cycle(100); Timer_delay(50); } }
// ISR for GPIOF void GPIOF_Handler(void) { if (GetBit32(GPIOF->RIS, 0)) { if (g_mode == MODE1) { // Switch audio range } else { g_enable_play = 1; } SetBit32( &(GPIOF->ICR), 0, 1); } else if (GetBit32(GPIOF->RIS, 4)) { //SetBit32( &(GPIOF->DATA), 3, 1 ); if (!g_enable_play) { if (g_mode == MODE1) { g_mode = MODE2; SetBit32( &(GPIOF->DATA), 1, 0 ); SetBit32( &(GPIOF->DATA), 2, 1 ); SetBit32( &(GPIOF->DATA), 3, 0 ); PWM_set_duty_cycle(100); } else if (g_mode == MODE2) { g_mode = MODE3; SetBit32( &(GPIOF->DATA), 1, 0 ); SetBit32( &(GPIOF->DATA), 2, 0 ); SetBit32( &(GPIOF->DATA), 3, 1 ); PWM_set_duty_cycle(100); } else { g_mode = MODE1; SetBit32( &(GPIOF->DATA), 1, 1 ); SetBit32( &(GPIOF->DATA), 2, 0 ); SetBit32( &(GPIOF->DATA), 3, 0 ); } } SetBit32( &(GPIOF->ICR), 4, 1); } // Clear pending bit NVIC->ICPR[0] = 0x1UL << 30; }
void set_gun_angle(int angle){ //REQUIRES: pwm_init() has been called // angle is a number between 0 and 204 //EFFECTS: Sets angle of gun on robot angle += 46; PWM_set_duty_cycle(&servos, PWM_1, angle); return; }
int Lua_SetPWMDutyCycle(lua_State *L) { if (lua_gettop(L) >= 2) { unsigned int channel = (unsigned int)lua_tointeger(L,1); unsigned int dutyCycleRaw = (unsigned int)lua_tointeger(L,2); if (channel >= 0 && channel < CONFIG_PWM_CHANNELS) { PWM_set_duty_cycle( channel, (unsigned short)dutyCycleRaw); } } return 0; }
void wheel3(int pwm){ //REQUIRES: pwm_init() has been called // Takes a number from -255 to 255 where 0 is stopped and // 255 is full speed ahead //EFFECTS: Modifies the speed of wheel3 if (pwm == 0){ PWM_disable(&motors, PWM_5); PWM_disable(&motors, PWM_6); } else if (pwm > 0){ PWM_set_duty_cycle(&motors, PWM_5, pwm); PWM_enable(&motors, PWM_5); PWM_disable(&motors, PWM_6); } else { pwm = pwm * -1; PWM_set_duty_cycle(&motors, PWM_6, pwm); PWM_enable(&motors, PWM_6); PWM_disable(&motors, PWM_5); } return; }
int Lua_SetAnalogOut(lua_State *L) { if (lua_gettop(L) >= 2) { PWMConfig *c = getPwmConfigChannel(lua_tointeger(L,1)); if (NULL != c) { unsigned int channel = (unsigned int)lua_tointeger(L,1); if (channel >= 0 && channel < CONFIG_PWM_CHANNELS) { float dutyCycle = (float)lua_tonumber(L,2) / PWM_VOLTAGE_SCALING; PWM_set_duty_cycle(channel,(unsigned short)dutyCycle); } } } return 0; }
int main(void) { struct note_struct happy_birthday[25]; struct note_struct sweep[8]; happy_birthday[0].note = NOTE_D; happy_birthday[0].delay = 250; happy_birthday[1].note = NOTE_D; happy_birthday[1].delay = 250; happy_birthday[2].note = NOTE_E; happy_birthday[2].delay = 300; happy_birthday[3].note = NOTE_D; happy_birthday[3].delay = 250; happy_birthday[4].note = NOTE_G; happy_birthday[4].delay = 250; happy_birthday[5].note = NOTE_F_SHARP; happy_birthday[5].delay = 350; happy_birthday[6].note = NOTE_D; happy_birthday[6].delay = 250; happy_birthday[7].note = NOTE_D; happy_birthday[7].delay = 250; happy_birthday[8].note = NOTE_E; happy_birthday[8].delay = 300; happy_birthday[9].note = NOTE_D; happy_birthday[9].delay = 250; happy_birthday[10].note = NOTE_A; happy_birthday[10].delay = 250; happy_birthday[11].note = NOTE_G; happy_birthday[11].delay = 350; happy_birthday[12].note = NOTE_D; happy_birthday[12].delay = 250; happy_birthday[13].note = NOTE_D; happy_birthday[13].delay = 250; happy_birthday[14].note = NOTE_DH; happy_birthday[14].delay = 300; happy_birthday[15].note = NOTE_B; happy_birthday[15].delay = 250; happy_birthday[16].note = NOTE_G; happy_birthday[16].delay = 250; happy_birthday[17].note = NOTE_F_SHARP; happy_birthday[17].delay = 250; happy_birthday[18].note = NOTE_E; happy_birthday[18].delay = 350; happy_birthday[19].note = NOTE_CH; happy_birthday[19].delay = 250; happy_birthday[20].note = NOTE_CH; happy_birthday[20].delay = 250; happy_birthday[21].note = NOTE_B; happy_birthday[21].delay = 300; happy_birthday[22].note = NOTE_G; happy_birthday[22].delay = 250; happy_birthday[23].note = NOTE_A; happy_birthday[23].delay = 250; happy_birthday[24].note = NOTE_G; happy_birthday[24].delay = 350; sweep[0].note = NOTE_C; sweep[0].delay = 500; sweep[1].note = NOTE_D; sweep[1].delay = 500; sweep[2].note = NOTE_E; sweep[2].delay = 500; sweep[3].note = NOTE_F; sweep[3].delay = 500; sweep[4].note = NOTE_G; sweep[4].delay = 500; sweep[5].note = NOTE_A; sweep[5].delay = 500; sweep[6].note = NOTE_B; sweep[6].delay = 500; sweep[7].note = NOTE_C*2; sweep[7].delay = 500; GPIO_init(); Timer_init(); PWM_init(); ADC_init(); NVIC_init(); // infinite loop while (1) { if (g_enable_play) { if (g_mode == MODE2) { Play_song(happy_birthday, sizeof(happy_birthday)/sizeof(struct note_struct)); } else if (g_mode == MODE3) { Play_song(sweep, sizeof(sweep)/sizeof(struct note_struct)); } g_enable_play = 0; PWM_set_duty_cycle(100); // Turn off speaker } if (g_mode == MODE1 && ADC_changed) { //PWM_set_duty_cycle((ADC_value*100)/MAX_ADC_VALUE); //PWM_load = (((ADC_value*100)/MAX_ADC_VALUE)*MAX_PWM_LOAD)/100/4; PWM_load = map_ADC_to_load(); PWM_set_load(PWM_load); PWM_set_duty_cycle(95); ADC_changed = 0; } } while(1); }