void update_pwm() { pwm_set(PWM_CHANNEL_1, _pwmChannel1); pwm_set(PWM_CHANNEL_2, _pwmChannel2); pwm_set(PWM_CHANNEL_3, _pwmChannel3); pwm_set(PWM_CHANNEL_4, _pwmChannel4); }
/* 关闭电机 */ void pwm_off(void) { pwm_set(PWM_FRONT, 0); pwm_set(PWM_RIGHT, 0); pwm_set(PWM_BACK, 0); pwm_set(PWM_LEFT, 0); }
void moveDistance(int distance) { motorInit(); // Start PWM process. Period 1 ms, Freq 1 kHz pwm_start(basepwm); // Turn motors counterclockwise for 3 s. if(distance >=0){ high(M1forward); high(M2forward); } else { high(M1reverse); high(M2reverse); distance=abs(distance); } pwm_set(M1enable, 0, 1000); pwm_set(M2enable, 1, 1000); pause(distance); // Stop again pwm_set(M1enable, 0, 0); pwm_set(M2enable, 1, 0); // End the PWM process pwm_stop(); }
void moveMotorT(int left, int right, int time) { motorInit(); // Start PWM process. Period 1 ms, Freq 1 kHz pwm_start(basepwm); if(left >=0){ high(M1forward); } else { high(M1reverse); left=abs(left); } if(right >=0){ high(M2forward); } else { high(M2reverse); right=abs(right); } pwm_set(M1enable, 0, left); pwm_set(M2enable, 1, right); pause(time); // Stop again pwm_set(M1enable, 0, 0); pwm_set(M2enable, 1, 0); // End the PWM process pwm_stop(); }
int main(void) { WDT_DISABLE; pwm_enable(PWM0, PWMSRC_SMCLK, PWM_UP, BIT1, M); // enable 4 TA0 out, up mode pwm_enable(PWM1, PWMSRC_SMCLK, PWM_UP_DOWN, BIT1 + BIT2, M); // enable 2 TA1 out, u_down mode // pwm_enable(PWM2, PWMSRC_SMCLK, PWM_UP, BIT1 + BIT2, M); // enable 2 TA 2out, continuous mode P1DIR |= BIT0; // Loop indicator P2DIR = ~0; P2SEL = ~0; int i = 0; while (1){ if (i++> M) { i = 1; P1OUT ^= BIT0; } pwm_set( PWM0, 1, i, PWM_POUT); // +duty% pwm_set( PWM1, 1, i, PWM_POUT); pwm_set( PWM1, 2, i, PWM_NOUT); // pwm_set( PWM2, 1, i, PWM_POUT); // pwm_set( PWM2, 2, i, PWM_NOUT); __delay_cycles(1000); } // _BIS_SR(CPUOFF); }
int main(void) { int16_t signe = 1; DDRG=0x3; PORTG=0x0; pwm_init(); while(1) { pwm_set(NUM, 1 * signe); // not 0 to test for sign problems wait_ms(2* DELAY); pwm_set(NUM, P_MAX /2 * signe); wait_ms(DELAY); pwm_set(NUM, P_MAX* signe); wait_ms(DELAY); pwm_set(NUM, P_MAX /2 * signe); wait_ms(DELAY); signe *= -1; } return 0; }
static inline void _pwm_set(pwm_t dev, uint8_t chan, uint16_t val) { if(val > 5) { pwm_set(dev, chan, MIN(val+MIN_PWM, MAX_PWM)); } else { pwm_set(dev, chan, 0); } }
void stopMotors() { motorInit(); // Stop pwm_set(M1enable, 0, 0); pwm_set(M2enable, 1, 0); // End the PWM process pwm_stop(); }
static void fade_to(uint8_t target, uint8_t delay) { uint8_t cur = pwm_get(); int8_t delta = (target > cur) ? 1 : -1; for(; cur != target; cur += delta) { pwm_set(cur); delay_ticks(delay); } pwm_set(target); }
void on_value() { switch (the_value) { case 0xEE11FB04: the_pwm -= 5; break; case 0xED12FB04: the_pwm += 5; break; case 0xEF10FB04: the_pwm = 0; break; case 0xE619FB04: the_pwm = 100; break; case 0xEC13FB04: the_pwm = 10; break; } if(the_pwm>100) { the_pwm = 100; } if(the_pwm<0) { the_pwm = 0 ; } pwm_set(the_pwm); debugf("on_value: %X\n", the_value); for(int i=0; i<300; i++) { if(buf[i]!=0) { debugf(", %d", buf[i]); buf[i] = 0 ; } } debugf("\n"); }
void KeyScan_PlayTone(UINT8 bType) { PWM_struct PWMInfo; UINT32 uiKeyToneTimerID; UINT32 uiKeyPlayTime = 100; //ms if((ubIsPlaying)||(!bKeyToneEn)) return; PWMInfo.uiDiv = 180; PWMInfo.uiPrd = 20; PWMInfo.uiOnCycle = 0; PWMInfo.uiInv = 0; PWMInfo.uiRise = 0; PWMInfo.uiFall = 10; pwm_open(PWMID_0); pwm_set(PWMID_0, &PWMInfo); if(timer_openAutoClose((UINT *)&uiKeyToneTimerID, (FP)KeyScan_PlayToneStop) == E_OK) { pwm_en(PWMID_0); timer_set(uiKeyToneTimerID, uiKeyPlayTime, _TIMER_CTRL_ONE_SHOT|_TIMER_CTRL_INT_ENABLE, _TIMER_PLAY); } ubIsPlaying = TRUE; }
/* sending functions */ void send_raw(void) { /* load pwm timing, retransmit and retransmit timeout */ struct params_t params; params.raw = next_word(); uint16_t retransmit_delay = next_word(); /* remember positing in timing table */ uint16_t pos = 0; uint16_t *ptr; for (uint8_t r = 0; r <= params.repeat; r++) { /* load the beginning of the current timing sequence */ ptr = (uint16_t*)current_code; /* copy all timing values up to the terminating nullword */ uint16_t t; while ( (t = pgm_read_word(ptr++)) != 0 ) timing[pos++] = t; /* terminate this sequency with the repeat delay */ timing[pos++] = retransmit_delay; } /* terminate the whole sequency with a zero */ timing[pos] = 0; /* remember positing in code table */ current_code = ptr; /* set loaded pwm value */ pwm_set(params.pwm); }
int parse_text(char *text, int n) { const char *delims = " \r\n\t"; int pwm1 = 0, pwm2 = 0; int rc; char *s; do { s = strsep(&text, delims); if (s == NULL) break; if (strcmp(s, "pwm1") == 0) { rc = parse_int(&text, &pwm1); if (rc < 0) { goto error; } } else if (strcmp(s, "pwm2") == 0) { rc = parse_int(&text, &pwm2); if (rc < 0) { goto error; } } } while (1); pwm_set(pwm1, pwm2); return 0; error: return -1; }
void send_nec(void) { /* load pwm timing, retransmit and retransmit timeout */ struct params_t params; params.raw = next_word(); uint16_t retransmit_delay = next_word(); /* load data */ uint16_t data[2]; data[0] = next_word(); data[1] = next_word(); /* add header */ timing[0] = NEC_HEADER_ON; timing[1] = NEC_HEADER_OFF; /* remember positing in timing table, calculate retransmit timeout */ uint16_t pos = 2; uint16_t retransmit = retransmit_delay - NEC_HEADER_OFF; /* compute 32 bits */ for (uint8_t j = 0; j < 2; j++) { for (uint8_t i = 0; i < 16; i++) { /* on timing */ timing[pos++] = NEC_ON; if (data[j] & 1) /* one */ timing[pos] = NEC_OFF_ONE; else /* zero */ timing[pos] = NEC_OFF_ZERO; /* subtract cycle length from retransmit */ retransmit -= timing[pos]; pos++; data[j] >>= 1; } } /* insert last on pulse */ timing[pos++] = NEC_ON; /* insert retransmits */ for (uint8_t i = 0; i < params.repeat; i++) { timing[pos++] = retransmit; timing[pos++] = NEC_REPEAT_ON; timing[pos++] = NEC_REPEAT_OFF; timing[pos++] = NEC_ON; retransmit = retransmit_delay - NEC_REPEAT_OFF - NEC_ON; } /* mark end of code */ timing[pos] = 0; /* set loaded pwm value */ pwm_set(params.pwm); }
static void power_inc(char idx) { if (++power[idx] > 10) power[idx] = 0; if (!idx) pwm_set(power[0]); }
void app_register_set (uint8_t uRegNo, uint32_t uValue) { uint16_t tempval16; uint8_t tempval, index; tempval16 = (uint16_t)(uValue & 0x0000FFFF); tempval = (uint8_t)(uValue & 0x000000FF); // registers saved in EEProm if (uRegNo >= APP_eReg_RemoteAddr00 && uRegNo <= APP_eReg_RemoteAddr31) { index = (uRegNo - APP_eReg_RemoteAddr00) * 2; index += APP_eCfg_RemoteAddr00; eeprom_write_word((uint16_t*)®ister_eeprom_array[index], tempval16); } else if (uRegNo >= APP_eReg_RemoteReg00 && uRegNo <= APP_eReg_RemoteReg31) { index = uRegNo - APP_eReg_RemoteReg00; index += APP_eCfg_RemoteReg00; eeprom_write_byte(®ister_eeprom_array[index], tempval); } else if (uRegNo >= APP_eReg_TargetReg00 && uRegNo <= APP_eReg_TargetReg31) { index = uRegNo - APP_eReg_TargetReg00; index += APP_eCfg_TargetReg00; eeprom_write_byte(®ister_eeprom_array[index], tempval); } else switch (uRegNo) { // registers saved in EEProm case APP_eReg_PWMChn1: tempval = (uint8_t)(uValue & 0x000000FF); pwm_set(0,tempval); break; case APP_eReg_PWMChn2: tempval = (uint8_t)(uValue & 0x000000FF); pwm_set(1,tempval); break; case APP_eReg_PWMChn3: tempval = (uint8_t)(uValue & 0x000000FF); pwm_set(2,tempval); break; // registers in ROM/RAM default: break; } }
static void power_dec(char idx) { if (--power[idx] > 10) power[idx] = 10; if (!idx) pwm_set(power[0]); }
void pwm_init() { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM5); GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM5); /* TIM1 Full remapping pins */ // GPIO_PinRemapConfig(GPIO_FullRemap_TIM5, ENABLE); TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /* Time Base configuration */ TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = PWM_PERIOD; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); pwm_set(50, 30); /* TIM1 counter enable */ TIM_Cmd(TIM5, ENABLE); /* TIM1 Main Output Enable */ TIM_CtrlPWMOutputs(TIM5, ENABLE); GPIO_InitStructure.GPIO_Pin = (1<<2)|(1<<3); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_SetBits(GPIOC, (1<<2)|(1<<3)); }
void main() { uint8_t brightness=0; //Initialize PWM pwm_init(); while(1){ for(i = 0 ; i < 255; i++){ //Setting pwm value pwm_set(i); pwm_wait(); } for(i = 255 ; i > 0; i--){ pwm_set(i); pwm_wait(); } } }
int main(){ DDRC = 0xDB; PORTC = 0x7E;; sei(); pwm_init(); pwm_set_scalar(3); pwm_set(0x00, 120); pwm_set(0x01, 255); int a; while(1){ a++; } }
int set_angle(int channel, int angle) { // -90 degrees at 42% duty or 430 cnt .93ms width // 0 degrees at 68% or 696 1.51 width // +90 degrees at 97% or 933 2.15 width auto int pwm; pwm =(int)(690 + (float)angle*512/180); switch(channel) { case 0: pwm_set(0, pwm, PWM0_OPTION); break; case 1: pwm_set(1, pwm, PWM1_OPTION); break; } return pwm; }
void TIMER2_IRQHandler(void) { // Clear interrupt if ((NRF_TIMER2->EVENTS_COMPARE[2] == 1) && (NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE2_Msk)) { NRF_TIMER2->EVENTS_COMPARE[2] = 0; } // Process buttons if (nrf_gpio_pin_read(BUTTON1) == 0) { pwm_set(LED_INTENSITY_HIGH); } else if (nrf_gpio_pin_read(BUTTON0) == 0) { pwm_set(LED_INTENSITY_LOW); } else { pwm_set(LED_OFF); } }
void tim2_isr(void) { /* reset interrupt flag */ if (timer_get_flag(TIM2, TIM_DIER_UIE)) { timer_clear_flag(TIM2, TIM_DIER_UIE); } /* call user callback */ uint16_t next_value; pwm_update_callback(&next_value); pwm_set(next_value); }
/******************************************* * Main */ int main(void) { uint32_t i; uint8_t d1 = 0; int8_t delta = +1; // Step for testing clock_init(); gpio_setup(); tim_setup(); i2c_setup(); lcd_init(); lcd_seekto(1, 0 ); lcd_writes("................"); while (1) { // Blink gpio_toggle(GPIOB, GPIO1); // Put chars to LCD //lcd_putchar(' ' + d1); lcd_home(); lcd_writes("Count "); lcd_write_uint(d1, 3); if( d1 % 10 == 0 ) { lcd_seekto(1, d1 / 10 ); lcd_writes(".<^^>."); } // Spinwait s bit for (i = 0; i < ( 20 * 72000 ); i++) { __asm__("nop"); } // Sweep PWM d1 += delta; // Pingpong if( d1 >= 100 ) delta = -1; else if( d1 <= 0 ) delta = +1; pwm_set( d1 ); } // Never reached return 0; }
static void next_timer(void) { d_h[0] = d_h[1]; d_m[0] = d_m[1]; if (d_m[0] || d_h[0]) power[0] = power[1]; else power[0] = 0; d_h[1] = d_m[1] = power[1] = 0; pwm_set(power[0]); }
void motorTest() { motorInit(); // Start PWM process. Period 1 ms, Freq 1 kHz pwm_start(basepwm); // Turn motors counterclockwise for 3 s. high(M1forward); high(M2forward); pwm_set(M1enable, 0, 1000); pwm_set(M2enable, 1, 1000); pause(2000); // Stop again pwm_set(M1enable, 0, 0); pwm_set(M2enable, 1, 0); // End the PWM process pwm_stop(); }
int main(void) { DDRB |= 1<<PB0; usart_init(); pwm_init(); pwm_set(0); char ch; while(1) { if(char_fifo_is_empty(usart_recv_fifo())) char_fifo_pop(usart_recv_fifo(), &ch); usart_send_char(ch); } return 0; }
/** Play sound (beep) by using PWM Play sound (beep) by using PWM. @param void @return void */ void GPIOMap_SoundPlayPWM(void) { PWM_CFG PWMInfo; PWMInfo.uiDiv = 99; // 1500Hz (base-clock (3MHz) / (div + 1) / basic-period) PWMInfo.uiPrd = 20; // basic period PWMInfo.uiRise = 0; // +50 PWMInfo.uiFall = 10; // -50 PWMInfo.uiOnCycle = 50; // 33 ms PWMInfo.uiInv = 0; // not invert pwm_set(PWM_BEEP_SOUND, &PWMInfo); pwm_en(PWM_BEEP_SOUND); }
int main(void) { timer1_initialize(); DDRA = 0xFF; PORTD |= 0x0F; while (1) //sei(); { scan_key(); show_direction(); pwm_set(); _delay_ms(10); } }
void process_boucle_courant(void) { static unsigned int i; float temp; pi_boucle_courant.mesure = current_read; pi_boucle_courant.reference = current_ref; /* i++; if(i>=1000) { printf("Current rx : [%i] ; Current res [%i]\r\n",(int16_t)(current_read*1000.0),(int16_t)(current_ref*1000.0)); i=0; } */ if(current_loop_enable) { do_pi_float(&pi_boucle_courant); temp = (pi_boucle_courant.commande); if(temp > 510.0) temp = 510.0; if(temp < -510.0) temp = -510.0; pwm_set((signed int)temp); // debug with manual pwm input from current ref //pwm_set((signed int)((float)(current_ref*500.0))); } else { pwm_set(0); } }