Any good C programmers in here

RU482

Lifer
Apr 9, 2000
12,689
3
81
A guy I used to work with does some contract mechanical engineering work on the side. He's got a product that he is co developing, and he asked me to take a look at some code that is 'doing something' (something to do with fan control on a filter) in the product. He asked me to take a look at it because he knows I took a C class a few years ago.

Anyway, It's been to long since I've even looked at any code, and with school I really don't have the time.

Anyone want to take a look at it??
I'm willing to compensate!!
PM me if interested
 

RU482

Lifer
Apr 9, 2000
12,689
3
81
Functional descriptions:

Motor is to run at a high speed of approximately 1900 RPM and low speed approximately 1363 RPM based on assigning a duty cycle for the motor controller (further downstream in the circuit) to run at.

On/off/speed switch is a single pole momentary closed/ normally open switch. There two separate LED?s. There is a green LED for power and reset indications. There is a bi-color LED that is flashed as amber to indicate when it is recommended to change the pre-filter then every 4th time the LED flashes red instead of amber indicating the main filter is to be changed also.

Green LED is on max brightness for high speed indication.

Green LED is on low brightness for low speed operation.

Motor is to run 24/7 for approximately 3 months at low speed (accuracy of actual time is based on correlating RPM to internal clock freq without actually having a feedback loop for RPM for RPM does not matter) and then based on counter which is based on total number of revolutions that have accumulated, flash the bi-color LED to amber color, this is to occur for 3 ? 3 month cycles then on the 4th cycle instead of flashing the LED amber, then flash the LED Red. Counter and thus total run times will be proportionately shorter for high speed operations.

Holding the power switch down for at least 4 seconds continuous (maybe it is a bit longer or shorter, that?s OK) will reset the amber counter while the red control counter is still accumulating. After the red LED is flashing, then holding the power switch down will reset both counters back to zero. Reset completion is verified to user by the green LED going off completely while the power switch is being held down.

Also, includes some commands to reset configuration values when chip is going through the program process in a chip programming station.

Following is text version of C code. It appears the code below has the counters total being too low to achieve approximately 90 days of continuous operation at low speed.

If the low speed counter increments by 2 every two minutes then essentially each count equals one minute at low speed. As the program currently had counter at 28,600 before turning on the amber light - that is only about 19.8 days.

A correction appears to be needed to set the first counter value to 129,600 and the red LED counter will be 4 times that.



-----------------

#include "C:\Projects\Halo\filtertest818.h"

#define REDLED PIN_B5 // ANODE

#define GRNLED PIN_B4 // ANODE

#define FILTER PIN_B1

#define BUTTON PIN_B0

#define ENABLE PIN_B2 //????

#define RUNLEDLO PIN_A0

#define RUNLEDHI PIN_A1

#define FAST 3

#define SLOW 2

#define OFF 0

int32 timer = 0;

int32 maintimer = 0;

char state = OFF;

char ticks = 0;

char ticks2 = 0;

char steps = 50;

char storetimer = 0;

//char cycle;

//char flash;

char counter = 0;

#separate

void store_timer(void);

#separate

void set_speed(void);

void main() {

setup_adc_ports(NO_ANALOGS);

setup_oscillator(OSC_4MHZ);

setup_adc(ADC_OFF);

setup_spi(FALSE);

setup_counters(RTCC_INTERNAL,WDT_2304MS);

setup_timer_1(T1_DISABLED);

setup_timer_2(T2_DIV_BY_1,50,1);

output_high(ENABLE);

setup_ccp1(CCP_PWM);

//set_pwm1_duty(0);

//setup_comparator(FALSE);

//setup_vref(FALSE);

SET_TRIS_B(0xC7);

//timer = read EEPROM after test

if (read_eeprom(0) == 'R')

{

timer =

make32(read_eeprom(4),read_eeprom(3),read_eeprom(2),read_eeprom(1));

maintimer =

make32(read_eeprom(8),read_eeprom(7),read_eeprom(6),read_eeprom(5));

//cycle = read_eeprom(15);

//flash = read_eeprom(16);

}

else

{

timer = 0;

maintimer = 0;

//cycle = 0;

store_timer();

write_eeprom(0,'R');

//write_eeprom(15,0);

//write_eeprom(16,FALSE);

}

output_high(GRNLED);

output_low(REDLED);

while (1)

{

if (maintimer > 114400)

//if (maintimer > 20)

{

if (ticks < 6)

{

output_low(GRNLED);

output_high(REDLED);

}

else

{

output_low(REDLED);

output_high(GRNLED);

}

}

else if (timer > 28600)

//else if (timer > 2)

{

if (ticks < 6)

{

output_high(GRNLED);

output_high(REDLED);

}

else

{

output_low(REDLED);

output_high(GRNLED);

}

}

if (ticks2 >= 120)

{

if (state == FAST)

{

maintimer += FAST;

timer += FAST;

}

else if (state == SLOW)

{

maintimer += SLOW;

timer += SLOW;

}

ticks2 = 0;

storetimer++;

}

if (storetimer > 10)

{

store_timer();

storetimer = 0;

}

if (!input(BUTTON))

{

output_low(GRNLED);

output_low(REDLED);

while (!input(BUTTON))

{

restart_wdt();

delay_ms(50);

counter++;

if (counter > 100)

{

output_high(REDLED);

if (maintimer > 114400)

//if (maintimer > 10)

maintimer = 0;

if (timer > 28600)

//if (timer > 2)

timer = 0;

storetimer = 11;

while (!input(BUTTON))

delay_ms(50);

output_low(REDLED);

}

}

output_high(GRNLED);

delay_ms(200);

counter = 0;

switch(state) {

case OFF:

state = SLOW;

set_speed();

break;

case SLOW:

state = FAST;

set_speed();

break;

case FAST:

state = OFF;

set_speed();

break;

}

}

delay_ms(50);

ticks++;

if (ticks > 20)

{

ticks = 0;

ticks2++;

}

}

}

#separate

void store_timer(void)

{

write_eeprom(0,'Z'); // Clear Reference Byte

write_eeprom(1,(char)(timer&0x000000FF)); // total counts

write_eeprom(2,(char)((timer&0x0000FF00)>>8));

write_eeprom(3,(char)((timer&0x00FF0000)>>16));

write_eeprom(4,(char)((timer&0xFF000000)>>24));

write_eeprom(5,(char)(maintimer&0x000000FF)); // total counts

write_eeprom(6,(char)((maintimer&0x0000FF00)>>8));

write_eeprom(7,(char)((maintimer&0x00FF0000)>>16));

write_eeprom(8,(char)((maintimer&0xFF000000)>>24));

write_eeprom(0,'R'); // Restore Reference Byte

// Reference Byte is used to control EEPROM corruption. If during the write

process

// the data gets corrupted, this will cause the timers to be reset on next

// power up.

}

#separate

void set_speed(void)

{

switch(state) {

case OFF:

//setup_timer_2(T2_DIV_BY_16,51,1);

setup_timer_2(T2_DIV_BY_1,50,1); // 20K hz

set_pwm1_duty(50);

output_high(ENABLE);

output_low(RUNLEDLO);

output_low(RUNLEDHI);

break;

case SLOW:

output_high(ENABLE);

//setup_timer_2(T2_DIV_BY_16,46,1); // 1350 Hz

setup_timer_2(T2_DIV_BY_1,50,1); // 20K hz

set_pwm1_duty(29); // 50%

delay_ms(50);

output_low(ENABLE);

output_high(RUNLEDLO);

output_float(RUNLEDHI);

break;

case FAST:

output_high(ENABLE);

setup_timer_2(T2_DIV_BY_1,50,1); // 20K hz

set_pwm1_duty(10); // 50-99%

delay_ms(50);

output_low(ENABLE);

output_float(RUNLEDLO);

output_high(RUNLEDHI);

break;

}

}

#include <16F819.h>

#device adc=8

#use delay(clock=4000000,RESTART_WDT)

#fuses NOWDT,INTRC_IO, PUT, NOMCLR, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG,

NOPROTECT, CCPB3
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
Please use the Software, Programming, and Applications Forum. The more people who use it for programming related questions, the better chance there is of finally getting a forum devoted to Programming.
 

RU482

Lifer
Apr 9, 2000
12,689
3
81
Originally posted by: BigJ
Please use the Software, Programming, and Applications Forum. The more people who use it for programming related questions, the better chance there is of finally getting a forum devoted to Programming.


Bangs head on desk, knows he should have posted it there
Sorry folks,
mods, please move
 

Reel

Diamond Member
Jul 14, 2001
4,484
0
76
Use the attach code function when it gets there. It will preserve the tabs (I assume they exist in the original) making it a LOT more readable.