From 17f4d96a723d821101a983115ae8c6deb4459a2c Mon Sep 17 00:00:00 2001 From: sfokin Date: Thu, 13 Mar 2025 17:31:47 +0300 Subject: [PATCH] init --- .gitignore | 32 +++++ Makefile | 113 +++++++++++++++ main.c | 128 +++++++++++++++++ nbproject/configurations.xml | 264 +++++++++++++++++++++++++++++++++++ nbproject/project.xml | 23 +++ 5 files changed, 560 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.c create mode 100644 nbproject/configurations.xml create mode 100644 nbproject/project.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..10bc1d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,32 @@ +#Ignore List for Microchip's MPLAB X IDE +#It's a form of NetBeans with vendor specific changes +#Taken from zeha on GIST https://gist.github.com/zeha/5999375 +#Updated by Cristian Cristea (https://github.com/cristiancristea00) + +*.d +*.pre +*.p1 +*.lst +*.sym +*.obj +*.o +*.sdb +*.obj.dmp +*.mk +*.map +*.properties +*.production +*.debug +html/ +nbproject/private/ +nbproject/Package-*.bash +build/ +nbbuild/ +dist/ +nbdist/ +nbactions.xml +nb-configuration.xml +funclist +nbproject/Makefile-* +disassembly/ +.generated_files/ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fca8e2c --- /dev/null +++ b/Makefile @@ -0,0 +1,113 @@ +# +# There exist several targets which are by default empty and which can be +# used for execution of your targets. These targets are usually executed +# before and after some main targets. They are: +# +# .build-pre: called before 'build' target +# .build-post: called after 'build' target +# .clean-pre: called before 'clean' target +# .clean-post: called after 'clean' target +# .clobber-pre: called before 'clobber' target +# .clobber-post: called after 'clobber' target +# .all-pre: called before 'all' target +# .all-post: called after 'all' target +# .help-pre: called before 'help' target +# .help-post: called after 'help' target +# +# Targets beginning with '.' are not intended to be called on their own. +# +# Main targets can be executed directly, and they are: +# +# build build a specific configuration +# clean remove built files from a configuration +# clobber remove all built files +# all build all configurations +# help print help mesage +# +# Targets .build-impl, .clean-impl, .clobber-impl, .all-impl, and +# .help-impl are implemented in nbproject/makefile-impl.mk. +# +# Available make variables: +# +# CND_BASEDIR base directory for relative paths +# CND_DISTDIR default top distribution directory (build artifacts) +# CND_BUILDDIR default top build directory (object files, ...) +# CONF name of current configuration +# CND_ARTIFACT_DIR_${CONF} directory of build artifact (current configuration) +# CND_ARTIFACT_NAME_${CONF} name of build artifact (current configuration) +# CND_ARTIFACT_PATH_${CONF} path to build artifact (current configuration) +# CND_PACKAGE_DIR_${CONF} directory of package (current configuration) +# CND_PACKAGE_NAME_${CONF} name of package (current configuration) +# CND_PACKAGE_PATH_${CONF} path to package (current configuration) +# +# NOCDDL + + +# Environment +MKDIR=mkdir +CP=cp +CCADMIN=CCadmin +RANLIB=ranlib + + +# build +build: .build-post + +.build-pre: +# Add your pre 'build' code here... + +.build-post: .build-impl +# Add your post 'build' code here... + + +# clean +clean: .clean-post + +.clean-pre: +# Add your pre 'clean' code here... +# WARNING: the IDE does not call this target since it takes a long time to +# simply run make. Instead, the IDE removes the configuration directories +# under build and dist directly without calling make. +# This target is left here so people can do a clean when running a clean +# outside the IDE. + +.clean-post: .clean-impl +# Add your post 'clean' code here... + + +# clobber +clobber: .clobber-post + +.clobber-pre: +# Add your pre 'clobber' code here... + +.clobber-post: .clobber-impl +# Add your post 'clobber' code here... + + +# all +all: .all-post + +.all-pre: +# Add your pre 'all' code here... + +.all-post: .all-impl +# Add your post 'all' code here... + + +# help +help: .help-post + +.help-pre: +# Add your pre 'help' code here... + +.help-post: .help-impl +# Add your post 'help' code here... + + + +# include project implementation makefile +include nbproject/Makefile-impl.mk + +# include project make variables +include nbproject/Makefile-variables.mk diff --git a/main.c b/main.c new file mode 100644 index 0000000..57df6af --- /dev/null +++ b/main.c @@ -0,0 +1,128 @@ +#include +#include +#include + +// CONFIGURATION +#pragma config FOSC = INTRCIO // Internal oscillator +#pragma config WDTE = OFF // Watchdog timer off +#pragma config PWRTE = ON // Power-up timer enabled +#define _XTAL_FREQ 4000000 + +void setup() { + // Set RA0 as analog input, rest of PORTA as outputs + TRISA = 0x01; + + // Set PORTC as outputs except RC5 which will toggle PWM + TRISC = 0x00; + + // Enable analog input on RA0 + ANSEL = 0x01; + + // Configure ADC + ADCON0 = 0b00000101; // Channel 0, ADC enabled + ADCON1 = 0b10001110; // Right justified, Vdd/Vss reference + + // Timer0 initialization + OPTION_REG = 0b11110111; // Prescale = 256, Internal clock source + TMR0 = 0; // Clear timer register +} + +#define SERVO_MIN_PULSE_US 750 // Minimum pulse width (approximately 0°) +#define SERVO_MAX_PULSE_US 2250 // Maximum pulse width (approximately 180°) +void delay_us(uint16_t us) { + uint16_t i; + for(i = 0; i < us; i++) { + _nop(); // Insert assembly-level no-operation instruction + } +} + +void generate_pwm(uint16_t pulse_width_us) { + RC5 = 1; // Start high + delay_us(pulse_width_us); // Keep high for desired pulse width + RC5 = 0; // Go low until next pulse + delay_us(20 - (pulse_width_us / 1000)); // Wait remaining 20ms period +} + +#define bool uint8_t +#define false 0 +#define true 1 +volatile bool button_pressed = false; +uint8_t current_mode = 0; + +void check_button() { + if (!RA1 && !button_pressed) { // Detect falling edge of button press + button_pressed = true; + __delay_ms(50); // Basic debounce delay + current_mode++; + if (current_mode > 2) current_mode = 0; // Cycle through modes + + // Update LEDs based on mode + switch(current_mode) { + case 0: + RC0 = 1; RC1 = 0; RC2 = 0; break; // Manual mode LED + case 1: + RC0 = 0; RC1 = 1; RC2 = 0; break; // Center mode LED + case 2: + RC0 = 0; RC1 = 0; RC2 = 1; break; // Oscillator mode LED + } + } + if (RA1) { + button_pressed = false; // Release button + } +} + +void manual_mode() { + uint16_t adc_value = (ADRESH << 8) | ADRESL; // Get ADC value + uint16_t pulse_width = (SERVO_MIN_PULSE_US + (adc_value * (SERVO_MAX_PULSE_US - SERVO_MIN_PULSE_US) / 1023)); + generate_pwm(pulse_width); +} + +void center_mode() { + generate_pwm((SERVO_MIN_PULSE_US + SERVO_MAX_PULSE_US) / 2); +} + +int8_t direction = 1; // Direction of movement +uint8_t osc_duty = 75; // Start at min duty cycle + +uint16_t osc_pulse_width = SERVO_MIN_PULSE_US; +bool increasing = true; + +void oscillator_mode() { + generate_pwm(osc_pulse_width); + + if (increasing) { + osc_pulse_width += 10; // Gradual increment + if (osc_pulse_width >= SERVO_MAX_PULSE_US) { + increasing = false; + } + } else { + osc_pulse_width -= 10; // Gradual decrement + if (osc_pulse_width <= SERVO_MIN_PULSE_US) { + increasing = true; + } + } +} + +void main() { + setup(); // Initialize peripherals + + while(1) { + check_button(); // Check for button presses + + switch(current_mode) { + case 0: + manual_mode(); + break; + case 1: + center_mode(); + break; + case 2: + oscillator_mode(); + break; + } + + // Perform ADC conversion + GO_nDONE = 1; // Start ADC conversion + while(GO_nDONE); // Wait for completion + } +} \ No newline at end of file diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml new file mode 100644 index 0000000..838bf67 --- /dev/null +++ b/nbproject/configurations.xml @@ -0,0 +1,264 @@ + + + + + + + + + main.c + + + Makefile + + + Makefile + + + + localhost + PIC16F676 + + + PICkit3PlatformTool + XC8 + 2.45 + 3 + + + + + + + + + + + + + + + false + false + + + + + + + false + false + + false + + false + false + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..9bf8e0d --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,23 @@ + + + com.microchip.mplab.nbide.embedded.makeproject + + + ServoTester + ca52f996-3768-4f5b-ad65-14d5c74e4bf3 + 0 + ISO-8859-1 + + + + + default + 2 + + + + false + + + +