|
|
View previous topic :: View next topic |
Author |
Message |
Jim McBride
Joined: 24 Mar 2004 Posts: 21
|
12f675 lookup table too large? |
Posted: Fri Jul 23, 2010 10:22 am |
|
|
I have this code which simulates perfectly. Better than I had hoped actually.
However, when the chip is programmed, it gives out a constant ~12Hz pulse regardless of the ADC reading or enable input. I suspect that I may be taking up too much space with my two look-up tables and that is causing the weird behavior. I hoped that MPLAB would have kicked me some kind of error if this was the case.... Can anyone verify that or see my problem?
Code: | #include <motor control ramp.h>
//int16 simulate_read_adc = 160;
int8 faux_index = 0;
int16 faux_speed = 0,delay_speed = 0, target_speed = 0,gain = 0;
int8 const gain_table[32] = {
16,15,14,13,12,11,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int16 const delay_table[32] = {
9990,9677,9364,9051,8738,8425,8112,7799,7486,7173,
6860,6547,6234,5921,5608,5295,4982,4669,4356,4043,
3730,3417,3104,2791,2478,2165,1852,1539,1226,913,600,0};
void main()
{
setup_adc_ports(AN0_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
set_adc_channel(0);
delay_us(50);
while(TRUE)
{
//target_speed = simulate_read_adc;//simulated read_adc()
//delay_us(40);//simulate adc read delay
target_speed = 480;//read_adc();//get pot reading (0 - 1023)
if(faux_speed > 0) output_high(pin_a1);
if(input(pin_a2)) target_speed = 0;
faux_index = faux_speed/32;//set gain/delay table index
gain = gain_table[faux_index];//find gain
delay_speed = delay_table[faux_index];//find delay
if(faux_speed > target_speed)
{
if(faux_speed > 16)faux_speed -= gain;
else faux_speed = 0;//stop
}
if(faux_speed < target_speed)
{
faux_speed += gain;
if(faux_speed > Target_speed) faux_speed = target_speed;//overshot target
}
output_low(pin_a1);//pulse out low
while(delay_speed) delay_speed --;
}
}
|
Last edited by Jim McBride on Fri Jul 23, 2010 1:54 pm; edited 1 time in total |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jul 23, 2010 12:25 pm |
|
|
Quote: | I have this code which simulates perfectly.
|
What simulator ? Simulators don't simulate everything. If you have
a problem with your #fuses settings, the simulator probably won't catch
it. If you have a problem with your physical voltage regulator, or
grounding (broken wires, solder shorts, etc.), the simulator won't catch it.
Quote: |
I suspect that I may be taking up too much space with my two look-up tables.
|
That won't be the reason. The compiler would have given you an error.
The CCS manual says:
Quote: |
ROM arrays may not occupy more than 256 locations.
|
(Note: This applies to 12F and 16F. Not to 18F). Each of your 'const'
arrays are far less than 256 bytes.
Post your preprocessor statements: #include, #fuses, #use delay, etc.
Also post your compiler version.
Quote: |
it gives out a constant ~12Hz pulse regardless of the ADC reading or enable input.
|
What pin does the PIC do this on ? How are you measuring it ?
With an oscilloscope ? |
|
|
Jim McBride
Joined: 24 Mar 2004 Posts: 21
|
|
Posted: Fri Jul 23, 2010 2:21 pm |
|
|
OOPs, typo on the uC model. it is a 675 not 674...
I have used the CCS Wizard to setup the basics of the code.
Header file:
Code: | #include <12F675.h>
#device adc=10
#use delay(clock=4000000)
#fuses NOWDT,INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOPUT, BROWNOUT
|
compiler is PCH 3.188, IDE 3.43
By simulator I am referring to the MPLAB simulator. CCS only has a debugger right?
I am using a scope to measure the output pin in the program pin_a1 (GP1). I have used the same setup for for first two versions of the code I am working on and the code worked as expected. Well actually the code worked as written, not as I expected. I have a high level of confidence that the circuit is not the culprit. I have already run it through the paces. I will check again but all signals have measured what I expect them too.
The thing that jumps out at me as suspect to my code logic is that if the input pin_a2 (pull-up) is not activated (low) no pulses should come out. And they come out at approximately the slowest loop speed.
Good to know it is not a space issue. But now I am really confused. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jul 23, 2010 3:00 pm |
|
|
I ran it with a 16F877 with vs. 4.109 on a PicDem2-Plus, and I get
pulses at a 12 Hz rate. I grounded pin A2. I don't think there is
a hardware problem.
When you said you simulated it, I assumed you were using Proteus
or something like that, and you had used the little oscilloscope display
in Proteus to see your desired waveforms. I don't see how you can
do that in MPSIM (or at least the version that I have). |
|
|
Jim McBride
Joined: 24 Mar 2004 Posts: 21
|
|
Posted: Fri Jul 23, 2010 3:23 pm |
|
|
Quote: | compiler is PCH 3.188, IDE 3.43
By simulator I am referring to the MPLAB simulator. CCS only has a debugger right?
I am using a scope to measure the output pin in the program pin_a1 (GP1). |
Although I like the CCS interface better, it is easier to just code in MPLAB to have access to the simulator. |
|
|
Jim McBride
Joined: 24 Mar 2004 Posts: 21
|
|
Posted: Fri Jul 23, 2010 5:53 pm |
|
|
Quote: | I ran it with a 16F877 with vs. 4.109 on a PicDem2-Plus, and I get
pulses at a 12 Hz rate. I grounded pin A2. |
You should get a pulse if a2 is grounded, NO pulse if a2 is at 5 volts. A higher adc reading should speed up the pulse frequency. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jul 23, 2010 7:08 pm |
|
|
Right, I did ground A2. But I just used your test program which has
a constant value substituted for the A/D reading. So, I got a constant
12 Hz pulse. I can't test it with the read_adc() put back in, until Sunday
maybe. |
|
|
Jim McBride
Joined: 24 Mar 2004 Posts: 21
|
|
Posted: Fri Jul 23, 2010 7:27 pm |
|
|
OK, leaving for the weekend anyway. But just so I am clear. One of the problems is that when you put A2 to a logic high level, there should not be any pulse coming out. But there is...... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jul 25, 2010 1:25 pm |
|
|
The problem in the code is the design. If you don't want any output
under certain conditions, then qualify the line of code that creates the
output (setting the pin high) with a test for your condition. If the
condition is not true, then don't set the pin high (no matter what the
other variables tell you to do). |
|
|
Jim McBride
Joined: 24 Mar 2004 Posts: 21
|
|
Posted: Mon Jul 26, 2010 8:59 am |
|
|
I guess I will have to rethink how to do this then. The variables are what manipulates the ramping of the speed up and down. Granted what you are saying is easy for when the "go" button is pushed. But the code still has to ramp down to zero after the button is released. I guess I can break up the code to pull that off. I am going to first start commenting out things to force the code to do what I want, I am really curious as to what the logic error is. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 26, 2010 10:45 pm |
|
|
Quote: |
One of the problems is that when you put A2 to a logic high level, there
should not be any pulse coming out. But there is......
|
Look at your posted code. This line is the operative one:
Code: | if(faux_speed > 0) output_high(pin_a1);
|
If 'faux_speed' ever becomes non-zero, you will get pulses out of pin A1.
These next two lines are important. 'faux_speed' is initialized to 0 in its
declaration. So 'faux_index' below will also be 0. The gain will be the
first element in gain_table, which is 16.
Code: | faux_index = faux_speed/32;
gain = gain_table[faux_index];
|
Since 'target_speed' is initialized to 480, and 'faux_speed' is initialized
to 0, the following test will be true.
Code: |
if(faux_speed < target_speed)
{
faux_speed += gain;
if(faux_speed > Target_speed) faux_speed = target_speed; //overshot target
}
|
Therefore 'gain' (which is 16) will be added to 'faux_speed' (which is 0)
and it will now become 16. Then, the line mentioned at the start, will
become true and you'll get pin A1 set high:
Code: |
if(faux_speed > 0) output_high(pin_a1);
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|