void main(void) { //initialize portc PORTC = 0x00; TRISC = 0x00; //initialize pwm PWM_Init(5000); //initialize pwm at 5khz PWM1_Duty(0); //set initial duty cycle at 0 on pin RC2 PWM2_Duty(0); //start pwm PWM1_Start(); PWM2_Start(); while (1) { for (int i=0; i<1023; i+=4) { PWM1_duty(); PWM2_Duty(1023-i); __delay_ms(2); } } }
void main() { InitMain(); current_duty = 200; // initial value for current_duty current_duty1 = 200; // initial value for current_duty1 TRISA = 0xFF; // PORTA is input TRISB = 0xFF; // Set PORTB as input //PORTB = 0x00; PWM1_Start(); // start PWM1 PWM2_Start(); // start PWM2 PWM1_Set_Duty(0); // Set current duty for PWM1 PWM2_Set_Duty(0); // Set current duty for PWM2 adcon1 = 0b1000010; while (1) { // endless loop temp_res = ADC_Read(0); temp_res=temp_res*255.0/1024.0; newduty=temp_res; if(RB0_bit==1 && RB1_bit==0){ PWM1_Set_Duty(newduty); PWM2_Set_Duty(0); }else if(RB0_bit==0 && RB1_bit==1){ PWM1_Set_Duty(0); PWM2_Set_Duty(newduty); } else{ PWM1_Set_Duty(0); PWM2_Set_Duty(0); } Delay_ms(5); // slow down change pace a little } }
void custom_start_flashlight(kal_uint8 red_level, kal_uint8 green_level, kal_uint8 blue_level, kal_uint8 duty) { custom_cfg_gpio_set_level(GPIO_DEV_LED_STATUS_1, ((red_level>0) ? LED_LIGHT_LEVEL5 : LED_LIGHT_LEVEL0)); custom_cfg_gpio_set_level(GPIO_DEV_LED_STATUS_2, ((green_level>0) ? LED_LIGHT_LEVEL5 : LED_LIGHT_LEVEL0)); custom_cfg_gpio_set_level(GPIO_DEV_LED_STATUS_3, ((blue_level>0) ? LED_LIGHT_LEVEL5 : LED_LIGHT_LEVEL0)); PWM2_Configure(PWM2_Level_Info[0][0], duty); PWM2_Start(); }
void PWM2_level(kal_uint8 level) { if (level > 5) ASSERT(0); PWM2_LEVEL_SAVE = level; if (level) { PWM2_Configure(PWM2_Level_Info[level-1][0],PWM2_Level_Info[level-1][1]); PWM2_Start(); } else { PWM2_Stop(); } }
void main(void) { // Enable Global Interrupt M8C_EnableGInt; // Clear the flags FlagsElevator = 0; FlagsAileron = 0;// new for motorcontroll2 FlagUltrasoon = 0; // Start timers and enable interrupt Timer1_Start(); Timer1_EnableInt(); Timer2_Start();// new for motorcontroll2 Timer2_EnableInt();// new for motorcontroll2 Timer3_Start(); Timer3_EnableInt(); TriggerUltrasoon(); // Init motors PWM1_Start(); PWM2_Start(); #if (DEBUG_LCD) LCD_Start(); #endif while (TRUE) { float aileronNormalized, elevatorNormalized; float distance; float speed, direction; float motorLeft, motorRight; BOOL forward; aileronNormalized = EvaluateAileron(PulseWidthAileron); direction = fabs(aileronNormalized); elevatorNormalized = EvaluateElevator(PulseWidthElevator); speed = fabs(elevatorNormalized); forward = (elevatorNormalized >= 0); distance = EvaluateUltrasoonSensor(); if (distance < MIN_SAFE_DISTANCE) { if (forward) speed = 0; } motorLeft = speed; // default is straight forward motorRight = speed; if (aileronNormalized < 0) // turning left { motorRight = speed; motorLeft = speed * (1 - direction); } else if (aileronNormalized > 0) // turning right { motorLeft = speed; motorRight = speed * (1 - direction); } if (forward) { // ccw PRT1DR |= 0x08; // AIN1 PRT1DR &= ~0x02; // AIN2 PRT1DR |= 0x20; // BIN1 PRT1DR &= ~0x80; // BIN2 } else { //cw PRT1DR &= ~0x08; // AIN1 PRT1DR |= 0x02; // AIN2 PRT1DR &= ~0x20; // BIN1 PRT1DR |= 0x80; // BIN2 } // Denormalize to Engine motorLeft *= (MAX_POWER - MIN_POWER); motorLeft += MIN_POWER; motorRight *= (MAX_POWER - MIN_POWER); motorRight += MIN_POWER; PWM1_WritePulseWidth(motorLeft); PWM2_WritePulseWidth(motorRight); #if (DEBUG_LCD) LCD_Position(0,7); LCD_PrHexInt(motorLeft); LCD_Position(1,7); LCD_PrHexInt(motorRight); LCD_Position(0,12); LCD_PrCString(forward ? "F" : "B"); #endif } }