Hi,
Yes, you are correct, that will never work.
A couple of things.
1) The timing to produce the pulses to the motor is depending on the For Next loop and the PauseUS. When you insert a whole lot of other commands in there the timing will be WAY off. For example, the ADCIN alone will take at least 50uS, remember the DEFINE ADC_SAMPLEUS 50 then comes the LCDOUT which will take a "lot" of time also.
Do you need the LCD to update 1000 times / second? That is what it's trying to do since you put it inside the FOR-NEXT loop.
2) It is the For Next loop that pulses the step line to the motor. What are you trying to do with the high motor, low motor? Do you have an enable signal to the motordriver that is mapped to motor
3) There's code missing in the INTERRUPT routine and the ON INTERRUPT statement should be at the beggining of the program with a LABEL telling it where to jump when an interrupt occurs.
4) The ADC DEFINES are missing.
5) The TRIS register set-up are missing.
6) The motor variable(??) declaration is missing.
7) You are checking if the SolarVoltage varible is equal to 300, so even if all the rest where OK the motor would only run when the value was exacly 300. You need to check if it's higher than 300.
This is probably not the best way to this now that you are adding more and more stuff to it but it may get you going if you have decided to go this route instead of the interrupt and tick counting we also showed you.
Again, this is completely untested! You will probably need to tweak the 2160 value in the For Next loop a little bit to get the correct timing.
When you post code please include comments so that it's easier for us to understnad what you're trying to do. Also, please see the vbCode page here: http://www.picbasic.co.uk/forum/misc.php?do=bbcode for how to post code so that it shows in a window like above.Code:Loop var Word 'Loop variable for the FOR-NEXT loop SolarVoltage var word 'Raw value from ADC RunMotor var bit 'Enable bit for step pulse routine. DEFINE ADC_BITS 10 'Set ADC to produce 10bits DEFINE ADC_CLOCK 3 'Set ADC clock to internal RC DEFINE ADC_SAMPLEUS 50 'Set conversion time to 50uS TRISA.0 = 1 'Set RA0 (pin2) to input. TRISB.0 = 0 'Set RB0 as output, step pulse to motor. ADCON1 = %10001110 'Set RA0 to analog, internal Vref, result right justified. (See datasheet) RunMotor = 0 'Do NOT run motor Main: '//The IF statement here checks the flag to determine if the motor should be run or not. '// If it should be run the For-Next loop is executed. If RunMotor = 1 then 'Produce pulses ONLY if supposed to.... High PortB.0 For Loop = 0 to 2160 'Loop here for ~2.16s, tweak to match. PauseUs 1000 Next Low PortB.0 For Loop = 0 to 2160 'Loop here for ~2.16s, tweak to match. PauseUs 1000 Next Endif ADCIN 0, SolarVoltage 'Get value from solarPV LCDOUT $FE,$1,#SolarVoltage 'Display value on LCD '// Here you can insert the code for sending to PC // '// and the LCD code for the time, if that's what your doing // If SolarVoltage > 300 then 'We are over the thershold RunMotor = 1 'so lets enable the step pulses Goto Main 'and start over Else RunMotor = 0 'disable the ste pulses. '// Since the For-next loop that produces the step pulses is not running now we insert a pause here. '// Otherwise the ADCIN and LCDOUT statements will execute VERY often. For Loop = 1 to 1000 PauseUs 1000 Next Goto Main
/Henrik Olsson.




Bookmarks