fix change diapason from 500 to 2400 us
This commit is contained in:
parent
b78d822ae3
commit
28bafd1b25
35
main.c
35
main.c
|
|
@ -3,7 +3,7 @@
|
||||||
#include <pic16f676.h>
|
#include <pic16f676.h>
|
||||||
|
|
||||||
// CONFIGURATION
|
// CONFIGURATION
|
||||||
#pragma config FOSC = INTRCIO // Internal oscillator
|
#pragma config FOSC = INTRCCLK // Internal oscillator
|
||||||
#pragma config WDTE = OFF // Watchdog timer off
|
#pragma config WDTE = OFF // Watchdog timer off
|
||||||
#pragma config PWRTE = ON // Power-up timer enabled
|
#pragma config PWRTE = ON // Power-up timer enabled
|
||||||
#pragma config MCLRE = OFF
|
#pragma config MCLRE = OFF
|
||||||
|
|
@ -20,23 +20,20 @@ void __interrupt() ISR(void) {
|
||||||
if (PIR1bits.TMR1IF) {
|
if (PIR1bits.TMR1IF) {
|
||||||
PIR1bits.TMR1IF = 0; // Clear interrupt flag
|
PIR1bits.TMR1IF = 0; // Clear interrupt flag
|
||||||
T1CONbits.TMR1ON = 0;
|
T1CONbits.TMR1ON = 0;
|
||||||
static uint16_t off_ticks; // Retains value between interrupts
|
// Calculate pulse parameters
|
||||||
static uint16_t timer;
|
// 3000 ticks = 20ms
|
||||||
|
uint8_t postoms= (uint16_t)(servo_pos*126) / 255;
|
||||||
|
uint16_t on_ticks = 16 + postoms * 2;
|
||||||
|
uint16_t off_ticks = 3000 - on_ticks;
|
||||||
if (pulse_state == 0) {
|
if (pulse_state == 0) {
|
||||||
// Calculate pulse parameters
|
|
||||||
uint16_t on_ticks = 125 + ((uint16_t)servo_pos * 125) / 255; // Map to 0.5ms-2.5ms
|
|
||||||
off_ticks = 2500 - on_ticks; // 2500 ticks = 20ms
|
|
||||||
|
|
||||||
timer = 65535 - on_ticks;
|
|
||||||
TMR1L = (unsigned char)(timer & 0xFF);
|
|
||||||
TMR1H = (unsigned char)(timer >> 8);
|
|
||||||
RC5 = 1; // Start pulse
|
RC5 = 1; // Start pulse
|
||||||
|
TMR1L = (0xFFFF - on_ticks)& 0xFF;//on_ticks;
|
||||||
|
TMR1H = (0xFFFF - on_ticks) >> 8;
|
||||||
pulse_state = 1;
|
pulse_state = 1;
|
||||||
} else {
|
} else {
|
||||||
timer = 65535 - off_ticks;
|
RC5 = 0; // End pulse
|
||||||
TMR1L = (unsigned char)(timer & 0xFF);
|
TMR1L = (0xFFFF - off_ticks) & 0xFF;
|
||||||
TMR1H = (unsigned char)(timer >> 8);
|
TMR1H = (0xFFFF - off_ticks) >> 8;
|
||||||
RC5 = 0; // End pulse
|
|
||||||
pulse_state = 0;
|
pulse_state = 0;
|
||||||
}
|
}
|
||||||
T1CONbits.TMR1ON = 1;
|
T1CONbits.TMR1ON = 1;
|
||||||
|
|
@ -56,8 +53,8 @@ void setup() {
|
||||||
|
|
||||||
// Configure Timer1
|
// Configure Timer1
|
||||||
T1CON = 0b00110000; // Timer1: prescaler 1:8, internal clock, OFF
|
T1CON = 0b00110000; // Timer1: prescaler 1:8, internal clock, OFF
|
||||||
TMR1H = (65536 - 188) >> 8; // Initial 1.5ms pulse (high byte)
|
TMR1H = (65535 - 188) >> 8; // Initial 1.5ms pulse (high byte)
|
||||||
TMR1L = (65536 - 188) & 0xFF; // Initial 1.5ms pulse (low byte)
|
TMR1L = (65535 - 188) & 0xFF; // Initial 1.5ms pulse (low byte)
|
||||||
PIR1bits.TMR1IF = 0; // Clear interrupt flag
|
PIR1bits.TMR1IF = 0; // Clear interrupt flag
|
||||||
PIE1bits.TMR1IE = 1; // Enable Timer1 interrupt
|
PIE1bits.TMR1IE = 1; // Enable Timer1 interrupt
|
||||||
INTCONbits.PEIE = 1; // Enable peripheral interrupts
|
INTCONbits.PEIE = 1; // Enable peripheral interrupts
|
||||||
|
|
@ -119,18 +116,18 @@ void main() {
|
||||||
__delay_us(100);
|
__delay_us(100);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
smooth_servo(128);
|
smooth_servo(94);
|
||||||
__delay_us(100);
|
__delay_us(100);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
if(isincreasing) {
|
if(isincreasing) {
|
||||||
smooth_servo(servo_pos + 1);
|
smooth_servo(servo_pos + 1);
|
||||||
__delay_ms(10);
|
__delay_ms(5);
|
||||||
if(servo_pos + 1 >= 254) isincreasing = 0;
|
if(servo_pos + 1 >= 254) isincreasing = 0;
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
smooth_servo(servo_pos - 1);
|
smooth_servo(servo_pos - 1);
|
||||||
__delay_ms(10);
|
__delay_ms(5);
|
||||||
if(servo_pos - 1 <= 0) isincreasing = 1;
|
if(servo_pos - 1 <= 0) isincreasing = 1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user