|
|
View previous topic :: View next topic |
Author |
Message |
aldina
Joined: 09 Oct 2009 Posts: 26
|
12F1822 I/O |
Posted: Fri Sep 25, 2015 9:15 am |
|
|
Hi,
I'm implementing the following code:
Code: |
#include <12F1822.h>
#device ADC=10
#fuses INTRC_IO,WDT,NOMCLR,PROTECT,BROWNOUT,NOPUT
#use delay(clock=4000000)
#BYTE OSCCON = 0x119 //OSCCON register (pg 26 datasheet)
//#BYTE WDTCON = 0x117 //WDTCON (pg 26 datasheet)
#include <stdlib.h>
#include <math.h>
int16 i=0;
// Ler sinal Direito
float Right()
{
float RH,RH_aux;
RH_aux=0;
for(i=0;i<5;i++)
{
set_adc_channel(0);
delay_us(1);
RH = read_adc();
RH = RH+RH_aux;
RH_aux = RH;
}
RH = (float) RH/5;
return RH;
}
// Ler sinal Esquerdo
float Left()
{
float LH,LH_aux;
LH_aux=0;
for(i=0;i<5;i++)
{
set_adc_channel(1);
delay_us(1);
LH = read_adc();
LH = LH+LH_aux;
LH_aux = LH;
}
LH = (float) LH/5;
return LH;
}
// Função TOFF 4
void T_OFF4()
{
float LH_OFF, RH_OFF;
int16 x = 0;
LH_OFF = 0;
RH_OFF = 0;
while (true)
{
restart_wdt();
LH_OFF = 0;
RH_OFF = 0;
LH_OFF = Left();
RH_OFF = Right();
delay_us(10);
if(RH_OFF < 150 || LH_OFF < 150)
{
OUTPUT_LOW(PIN_A5); // OFF direito
OUTPUT_LOW(PIN_A4); // OFF esquerdo
delay_ms(20);
x++;
if (x>=35) break; // 35*20ms = 700ms --> SAI
}
else
{
break;
}
} // while
} // main
// Função TOFF R
void T_OFF_RS()
{
float RH_OFF;
int16 x = 0;
RH_OFF = 0;
while (true)
{
restart_wdt();
RH_OFF = 0;
RH_OFF = Right();
delay_us(10);
if (RH_OFF < 150 && input(PIN_A2) == 0)
{
OUTPUT_LOW(PIN_A5); // OFF direito
OUTPUT_LOW(PIN_A4); // OFF esquerdo
delay_ms(20);
x++;
if (x>=35) break; // 35*20ms = 700ms --> SAI
}
else
{
break;
}
} // While
} // main
void T_OFF_RC()
{
float RH_OFF;
int16 x = 0;
RH_OFF = 0;
while (true)
{
restart_wdt();
RH_OFF = 0;
RH_OFF = Right();
delay_us(10);
if (RH_OFF < 150 && input(PIN_A2) == 1)
{
OUTPUT_LOW(PIN_A5); // OFF direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
delay_ms(20);
x++;
if (x>=35) break; // 35*20ms = 700ms --> SAI
}
else
{
break;
}
} // While
} // main
// Função TOFF L
void T_OFF_LS()
{
float LH_OFF;
int16 x = 0;
LH_OFF = 0;
while (true)
{
restart_wdt();
LH_OFF = 0;
LH_OFF = Left();
delay_us(10);
if (LH_OFF < 150 && input(PIN_A2) == 0)
{
OUTPUT_LOW(PIN_A5);
OUTPUT_LOW(PIN_A4);
delay_ms(20);
x++;
if (x>=35) break; // 35*20ms = 700ms --> SAI
}
else
{
break;
}
} // while
} // main
// Função TOFF L
void T_OFF_LC()
{
float LH_OFF;
int16 x = 0;
LH_OFF = 0;
while (true)
{
restart_wdt();
LH_OFF = 0;
LH_OFF = Left();
delay_us(10);
if (LH_OFF < 150 && input(PIN_A2) == 1)
{
OUTPUT_HIGH(PIN_A5);
OUTPUT_LOW(PIN_A4);
delay_ms(20);
x++;
if (x>=35) break; // 35*20ms = 700ms --> SAI
}
else
{
break;
}
} // while
} // main
// Função pisca
void Pisca()
{
float RH_Signal, LH_Signal;
//int16 x=0;
RH_Signal = 0;
LH_Signal = 0;
while (true)
{
RH_Signal = 0;
LH_Signal = 0;
RH_Signal = Right();
LH_Signal = Left();
delay_us(10);
if(RH_Signal >= 150 && LH_Signal >= 150 && input(PIN_A2)==0) // 110
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
T_OFF4();
}
else if(RH_Signal >= 150 && LH_Signal >= 150 && input(PIN_A2)==1) // 111
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
T_OFF4();
/*
delay_ms(20);
x++;
if (x>=25) break; // 25*20ms = 500ms --> SAI
*/
}
else if(RH_Signal >= 150 && LH_Signal < 150 && input(PIN_A2)==0) // 100
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_LOW(PIN_A4); // OFF esquerdo
T_OFF_RS();
}
else if(RH_Signal >= 150 && LH_Signal < 150 && input(PIN_A2)==1) // 101
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
T_OFF_RC();
/*
delay_ms(20);
x++;
if (x>=25) break; // 25*20ms = 500ms --> SAI
*/
}
else if(RH_Signal < 150 && LH_Signal >= 150 && input(PIN_A2)==1) // 011
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
T_OFF_LC();
/*
delay_ms(20);
x++;
if (x>=25) break; // 25*20ms = 500ms --> SAI
*/
}
else if(RH_Signal < 150 && LH_Signal >= 150 && input(PIN_A2)==0) // 010
{
OUTPUT_LOW(PIN_A5); // OFF direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
T_OFF_LS();
}
else if(RH_Signal < 150 && LH_Signal < 150 && input(PIN_A2)==1) // 001
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
}
else if(RH_Signal < 150 && LH_Signal < 150 && input(PIN_A2)==0) // 000
{
OUTPUT_LOW(PIN_A5); // OFF direito
OUTPUT_LOW(PIN_A4); // OFF esquerdo
break;
}
}
}
// ****** MAIN ******* //
void main(void)
{
OSCCON=0x61; //Configuração do registo OSCCON 61->4MHZ, 71->8MHZ
setup_adc_ports(3);
setup_adc(ADC_CLOCK_DIV_16); // ver pág.143 datasheet - Tconv = 16/4M = 4us por amostra
//setup_adc(ADC_CLOCK_INTERNAL);
// Configuração das saídas
output_low(PIN_A5); //Saida para sector Lateral Direito
output_low(PIN_A4); //Saida para sector Lateral Esquerdo
// Configuração das entradas
// A0 - Entrada AN0 para leitura do sinal lado Direito
// A1 - Entrada AN1 para leitura do sinal lado Esquerdo
// A2 - Entrada Digital para leitura do sinal Speed
// A3 - Entrada Digital para leitura do sinal de presença
SET_TRIS_A(0x07); // 00000111 - 0x07 - ainda sem entrada de SM no MCLR (A3)
while (true)
{
restart_wdt();
Pisca();
} // while
} // main
|
And I have a problem that I can't found a resolution. The problem is: when I have no input A2 (input(PIN_A2 == 0) all is fine, outputs A4 and A5 work well, but when I have input(PIN_A2) == 1, some times it works well but some times not.
In my hardware I have input A2 connected through a 4.7V zener diode to GND and a 100k Ohms resistor connected to 24V (Vcc) (it is a control input that control if I have normally the outputs ON (with input A2 == 1) or normally OFF (with input A2 == 0).
Other 2 inputs are connected through a voltage divider and the VCC signal comes from an automotive turn light indicator relay. So, outputs A4 and A5 should flash according the relay signal is HIGH or LOW. And it is happened if the control input A2 is LOW --> outputs are OFF and, when Relay is High --> outputs are ON (the right side - A5, the left side - A4 or both). The same procedure should happen when control input is HIGH --> outputs are ON with HIGH relay signal and OFF with LOW signal, but this is not happening all the time.
I'm simulating outputs with 2 yellow LEDs connected on A4 and A5 thought a 120 Ohms resistor.
If someone have idea why it is happening, I appreciate so much your help.
BR,
Aldina |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Sep 25, 2015 11:50 am |
|
|
Quote: | setup_adc_ports(3); |
I don't know what your compiler version is, but for vs. 5.049 this magic
number is totally wrong. It will not make pins A0 and A1 into analog pins.
You shouldn't be using magic numbers anyway. Do it like this:
Code: | setup_adc_ports(sAN0 | SAN1); |
Quote: |
set_adc_channel(0);
delay_us(1);
set_adc_channel(1);
delay_us(1);
|
The delay of 1 usec after changing the channel is totally wrong.
Look in section 16.1 A/D Acquisition Requirements in the latest 12F1822
data sheet. In the equation section, it shows down at the bottom that
the delay should be 5 usec, minimum.
Quote: |
I have input A2 connected through a 4.7V zener diode to GND and a
100k Ohms resistor connected to 24V (Vcc) |
Is that sufficient zener current ? You didn't give your zener diode part
number but 100K with 24v would give a zener current way below the
normal test current given in a typical data sheet.
Latest 12F1822 data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/40001413E.pdf |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Sep 25, 2015 2:19 pm |
|
|
unless i am missing something,
your use of FLOATS is silly and costing you performance.
5120 is my rough estimate of the
greatest value the vars ever need to handle.
I don't see a single use where the float vars & functions could not be
converted to simple unsigned int16 vars or at worst case int32.
It will speed your divide by 5 too.
but especially bad is this:
Doing comparisons you have programmed with FLOATS eats cycles like crazy.
look at you .LST file to see all the waste........
for this line and the others like it:
Code: |
if(RH_Signal >= 150 && LH_Signal >= 150 && input(PIN_A2)==1)
|
why do you think you need floats?? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Sat Sep 26, 2015 1:11 am |
|
|
Yes.
I'd point to this thread where I just had the same comment:
<http://www.ccsinfo.com/forum/viewtopic.php?t=54396>
People just don't understand that floats are not really a good thing. There are places where they are needed, but these are rare. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Sat Sep 26, 2015 7:16 am |
|
|
After 25+ PIC years and 40 total in micros I've never, ever come across a project that actually, truly needed floating point numbers. Maybe it's just my 'mindset' that computers deal in ones and zeros and not 'points of numbers' that I try to think as the computer and NOT try to get the computer to think like humans.
Long before GPS, man set foot on the moon and I'm fairly sure they didn't use FP calculations back then. If they did, they'd have have overshot the Moon waiting for the computer to crunch the numbers.
Jay |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sat Sep 26, 2015 12:14 pm |
|
|
FYI: The Apollo program's computer OS, even mark II , did not have any capability beyond 24 bit signed integer.
The bulk of the calcs were done with signed 16 bit words.
And flying along at a smoking 2.048 mhz system clock |
|
|
aldina
Joined: 09 Oct 2009 Posts: 26
|
|
Posted: Sun Sep 27, 2015 2:24 pm |
|
|
thank you all
I've made all your recommendations:
All my functions and vars are now int16, I have 16 samples in the A/D acquisition and then divide by 16, I have 5us as acquisition A/D time.
I've changed my control input A2 to analog (change zener to a voltage divider).
All is working while I have no A2 input with one or both other 2 inputs (A0, A1).
If I have only A2 input it's Ok, but as soon as I have right (A0) or left (A1) signals outputs working irregular.
My code now:
Code: |
#include <12F1822.h>
#device ADC=10
#fuses INTRC_IO,WDT,NOMCLR,PROTECT,BROWNOUT,NOPUT
#use delay(clock=4000000)
#BYTE OSCCON = 0x119 //OSCCON register (pg 26 datasheet)
//#BYTE WDTCON = 0x117 //WDTCON (pg 26 datasheet)
#include <stdlib.h>
#include <math.h>
int16 i=0;
// Ler speed
int16 Speed_H()
{
int16 speed,speed_aux;
speed_aux=0;
for(i=0;i<16;i++)
{
set_adc_channel(2);
delay_us(5);
speed = read_adc();
speed = speed+speed_aux;
speed_aux = speed;
}
speed = speed/16;
return speed;
}
// Ler sinal Direito
int16 Right()
{
int16 RH,RH_aux;
RH_aux=0;
for(i=0;i<16;i++)
{
set_adc_channel(0);
delay_us(5);
RH = read_adc();
RH = RH+RH_aux;
RH_aux = RH;
}
RH = RH/16;
return RH;
}
// Ler sinal Esquerdo
int16 Left()
{
int16 LH,LH_aux;
LH_aux=0;
for(i=0;i<16;i++)
{
set_adc_channel(1);
delay_us(5);
LH = read_adc();
LH = LH+LH_aux;
LH_aux = LH;
}
LH = LH/16;
return LH;
}
// Função TOFF 4
void T_OFF4()
{
int16 LH_OFF, RH_OFF;
int16 x = 0;
while (true)
{
restart_wdt();
LH_OFF = 0;
RH_OFF = 0;
LH_OFF = Left();
RH_OFF = Right();
delay_us(10);
if(RH_OFF < 400 || LH_OFF < 400)
{
OUTPUT_LOW(PIN_A5); // OFF direito
OUTPUT_LOW(PIN_A4); // OFF esquerdo
delay_ms(20);
x++;
if (x>=35) break; // 35*20ms = 700ms --> SAI
}
else
{
break;
}
} // while
} // main
// Função TOFF R
void T_OFF_RS()
{
int16 RH_OFF;
int16 x = 0;
while (true)
{
restart_wdt();
RH_OFF = 0;
RH_OFF = Right();
delay_us(10);
if (RH_OFF < 400)
{
OUTPUT_LOW(PIN_A5); // OFF direito
OUTPUT_LOW(PIN_A4); // OFF esquerdo
delay_ms(20);
x++;
if (x>=35) break; // 35*20ms = 700ms --> SAI
}
else
{
break;
}
} // While
} // main
void T_OFF_RC()
{
int16 RH_OFF;
int16 x = 0;
while (true)
{
restart_wdt();
RH_OFF = 0;
RH_OFF = Right();
delay_us(10);
if (RH_OFF < 400)
{
OUTPUT_LOW(PIN_A5); // OFF direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
delay_ms(20);
x++;
if (x>=35) break; // 35*20ms = 700ms --> SAI
}
else
{
break;
}
} // While
} // main
// Função TOFF L
void T_OFF_LS()
{
int16 LH_OFF;
int16 x = 0;
while (true)
{
restart_wdt();
LH_OFF = 0;
LH_OFF = Left();
delay_us(10);
if (LH_OFF < 400)
{
OUTPUT_LOW(PIN_A5);
OUTPUT_LOW(PIN_A4);
delay_ms(20);
x++;
if (x>=35) break; // 35*20ms = 700ms --> SAI
}
else
{
break;
}
} // while
} // main
// Função TOFF L
void T_OFF_LC()
{
int16 LH_OFF;
int16 x = 0;
while (true)
{
restart_wdt();
LH_OFF = 0;
LH_OFF = Left();
delay_us(10);
if (LH_OFF < 400)
{
OUTPUT_HIGH(PIN_A5);
OUTPUT_LOW(PIN_A4);
delay_ms(20);
x++;
if (x>=35) break; // 35*20ms = 700ms --> SAI
}
else
{
break;
}
} // while
} // main
// Função pisca
void Pisca()
{
int16 RH_Signal, LH_Signal, Hex;
//while (true)
//{
RH_Signal = 0;
LH_Signal = 0;
Hex = 0;
RH_Signal = Right();
LH_Signal = Left();
Hex = Speed_H();
delay_us(10);
if(RH_Signal >= 400 && LH_Signal >= 400 && Hex < 400) // 110
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
T_OFF4();
}
else if(RH_Signal >= 400 && LH_Signal >= 400 && Hex >= 400) // 111
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
T_OFF4();
}
else if(RH_Signal >= 400 && LH_Signal < 400 && Hex < 400) // 100
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_LOW(PIN_A4); // OFF esquerdo
T_OFF_RS();
}
else if(RH_Signal >= 400 && LH_Signal < 400 && Hex >= 400) // 101
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
T_OFF_RC();
}
else if(RH_Signal < 400 && LH_Signal >= 400 && Hex >= 400) // 011
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
T_OFF_LC();
}
else if(RH_Signal < 400 && LH_Signal >= 400 && Hex < 400) // 010
{
OUTPUT_LOW(PIN_A5); // OFF direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
T_OFF_LS();
}
else if(RH_Signal < 400 && LH_Signal < 400 && Hex >= 400) // 001
{
OUTPUT_HIGH(PIN_A5); // OFF direito
OUTPUT_HIGH(PIN_A4); // OFF esquerdo
}
else if(RH_Signal < 400 && LH_Signal < 400 && Hex < 400) // 000
{
OUTPUT_LOW(PIN_A5); // OFF direito
OUTPUT_LOW(PIN_A4); // OFF esquerdo
break;
}
//}
}
// ****** MAIN ******* //
void main(void)
{
OSCCON=0x61; //registo OSCCON 61->4MHZ, 71->8MHZ
setup_adc_ports(SAN0 | SAN1 | SAN2);
setup_adc(ADC_CLOCK_DIV_16); // ver pág.143 datasheet - Tconv = 16/4M = 4us por amostra
//setup_adc(ADC_CLOCK_INTERNAL);
// Configuração das saídas
output_low(PIN_A5); //Saida para sector Lateral Direito
output_low(PIN_A4); //Saida para sector Lateral Esquerdo
// Configuração das entradas
// A0 - Entrada AN0 para leitura do sinal lado Direito
// A1 - Entrada AN1 para leitura do sinal lado Esquerdo
// A2 - Entrada Digital para leitura do sinal Speed
// A3 - Entrada Digital para leitura do sinal de presença
SET_TRIS_A(0x07); // 00000111 - 0x07 - ainda sem entrada de SM no MCLR (A3)
while (true)
{
restart_wdt();
Pisca();
} // while
} // main |
Not working |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Sep 27, 2015 3:59 pm |
|
|
I don't know what you mean by "not working". You need to tell us what
it would do if it was working, and also tell us what it is doing when it is
"not working". How do you know it's not working ? What do you see ?
Your functions have some problems:
1. You don't have to set the adc channel every time in the loop.
Just set the channel once, before the loop begins.
2. You are not setting 'speed' to 0, before you use it in the function.
3. Your speed averaging code seems excessively complex.
4. The variable 'i' used as the loop counter should be local, not global.
It seems that your code could be written much more cleanly, like this:
Code: |
// Ler speed
int16 Speed_H(void)
{
int8 i;
int16 speed;
int16 speed_sum;
int16 speed_average;
speed = 0;
speed_sum = 0;
speed_average = 0;
set_adc_channel(2);
delay_us(5);
for(i=0;i<16;i++)
{
speed = read_adc();
speed_sum += speed;
}
speed_average = speed_sum/16;
return speed_average;
}
|
You have several routines that could use this same cleanup. |
|
|
aldina
Joined: 09 Oct 2009 Posts: 26
|
|
Posted: Sun Sep 27, 2015 5:57 pm |
|
|
I mean: I can have 2 situations:
1st: when I have A2 input --> Outputs (A4 and A5) stay HIGH;
2nd: when I don't have A2 input --> Outputs (A4 and A5) stay LOW;
This happens with LOW A0 and LOW A1:
A0 A1 A2 --> A4 A5
0 0 0 --> 0 0
0 0 1 --> 1 1
These are the 2 main status of outputs (LOW with LOW A2 and High with HIGH A2)
Then I have a flash relay that give constant flash ON/OFF signal to A0 and A1 (for example 500ms ON, 500ms OFF, 500ms ON, 500ms OFF, ... --> 1Hz). So, I want to blink outputs A4 and A5 according inputs A0 and A1:
1. If I have flash signal input A0 --> A5 will flash at the same frequency of the input signal (input High --> Output HIGH; input LOW --> Output LOW) and A4 stay ON or OFF according A2 control input is High or Low.
2. If I have flash signal input A1 --> A4 will flash at the same frequency of the input signal (input High --> Output HIGH; input LOW --> Output LOW) and A5 stay ON or OFF according A2 control input is High or Low.
3. If I have flash signal input A0 and A1 --> A5 and A4 will flash at the same frequency of the input signal (input High --> Output HIGH; input LOW --> Output LOW), then (when there are no more A0 and A1 signals), both stay ON or OFF according A2 is High or LOW
4. If I have no control signal input A2 --> outputs will stay Low, and waiting for control input signal A2 to turn ON or new flash signal to flash
5. If I have control signal input A2 --> outputs will stay HIGH, and waiting for control input signal LOW to turn OFF or new flash signal to flash
IN Right IN Left IN Control Out Left Out Right
A0 A1 A2 --> A4 A5
0 0 0 --> 0 0
0 0 1 --> 1 1
0 1 0 --> 1 0
0 1 1 --> 1 1
1 0 0 --> 0 1
1 0 1 --> 1 1
1 1 0 --> 1 1
1 1 1 --> 1 1
I use Pisca() function to detect which situation is happening with A0, A1 and A2 inputs (000, 001, 010, ... 111), then turn ON or OFF outputs according and cal OFF function:
T_OFF4() --> when A0 && A1 are HIGH (110 or 111)
T_OFF_RS() --> 100
T_OFF_RC() --> 101
T_OFF_LS() --> 010
T_OFF_LC() --> 011
OFF function I use to detect OFF of relay to turn OFF outputs and stay OFF until relay comes HIGH again, but if relay doesn't come HIGH again between 700ms it means that there is no more relay signal, it goes out to "see" if there is or not A2 to stay ON or OFF.
Hope you understand my explanation. So the PROBLEM I have is only when I have A2 input at the same time of A0 (101), or at the same time of A1 (011) or with both (111). Al other cases are working very well. In these cases outputs should flash according A0 and A1 and it is not happening. Sometimes they flash sometimes not and stay HIGH when relay(input) are LOW and in the following ON/OFF cycle will flash again. Why? If it works/flash well without A2 input??? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Sep 27, 2015 6:13 pm |
|
|
Post your CCS compiler version so I have complete information. |
|
|
aldina
Joined: 09 Oct 2009 Posts: 26
|
|
Posted: Sun Sep 27, 2015 6:28 pm |
|
|
CCS compiler 4.3.0.337 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Sep 27, 2015 7:59 pm |
|
|
That's not the compiler version.
Look at the top of the .LST file after a successful compilation. It will
have a 4-digit number such as 4.141 or 5.039, etc. Post that number.
The .LST file will be in your project directory. |
|
|
aldina
Joined: 09 Oct 2009 Posts: 26
|
|
Posted: Sun Sep 27, 2015 8:21 pm |
|
|
CCS PCM C Compiler, Version 4.130 |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Sep 28, 2015 2:38 pm |
|
|
In terms of your part - thats a pretty old compiler version. Bug free
support for parts that are "new" especially with new hardware features in them sometimes take a while for CCS to get right. my 2 cents
my suspicion is still the code though.
can we all see your present best version of said code ? |
|
|
aldina
Joined: 09 Oct 2009 Posts: 26
|
|
Posted: Mon Sep 28, 2015 4:22 pm |
|
|
Yes, my new version:
#include <12F1822.h>
#device ADC=10
#fuses INTRC_IO,WDT,NOMCLR,PROTECT,BROWNOUT,NOPUT
#use delay(clock=4000000)
#BYTE OSCCON = 0x119 //OSCCON register (pg 26 datasheet)
//#BYTE WDTCON = 0x117 //WDTCON (pg 26 datasheet)
#include <stdlib.h>
#include <math.h>
// Ler speed
int16 Speed_H()
{
int8 i;
int16 speed;
int16 speed_sum;
int16 speed_average;
speed = 0;
speed_sum = 0;
speed_average = 0;
set_adc_channel(2);
delay_us(5);
for(i=0;i<16;i++)
{
speed = read_adc();
speed_sum += speed;
}
speed_average = speed_sum/16;
return speed_average;
}
// Ler sinal Direito
int16 Right()
{
int8 i;
int16 RH;
int16 RH_sum;
int16 RH_average;
RH = 0;
RH_sum =0;
RH_average = 0;
set_adc_channel(0);
delay_us(5);
for(i=0;i<16;i++)
{
RH = read_adc();
RH_sum += RH;
}
RH_average = RH_sum/16;
return RH_average;
}
// Ler sinal Esquerdo
int16 Left()
{
int8 i;
int16 LH;
int16 LH_sum;
int16 LH_average;
LH = 0;
LH_sum = 0;
LH_average = 0;
set_adc_channel(1);
delay_us(5);
for(i=0;i<16;i++)
{
LH = read_adc();
LH_sum += LH;
}
LH_average = LH_sum/16;
return LH_average;
}
// Função TOFF 4
void T_OFF4()
{
int16 LH_OFF, RH_OFF;
int8 x = 0;
while (true)
{
restart_wdt();
LH_OFF = 0;
RH_OFF = 0;
LH_OFF = Left();
RH_OFF = Right();
//delay_us(10);
if(RH_OFF < 400 || LH_OFF < 400)
{
OUTPUT_LOW(PIN_A5); // OFF direito
OUTPUT_LOW(PIN_A4); // OFF esquerdo
delay_ms(20);
x++;
if (x>=35) break; // 35*20ms = 700ms --> SAI
}
return;
} // while
} // main
// Função TOFF R
void T_OFF_RS()
{
int16 RH_OFF;
int8 x = 0;
while (true)
{
restart_wdt();
RH_OFF = 0;
RH_OFF = Right();
//delay_us(10);
if (RH_OFF < 400)
{
OUTPUT_LOW(PIN_A5); // OFF direito
OUTPUT_LOW(PIN_A4); // OFF esquerdo
delay_ms(20);
x++;
if (x>=35) break; // 35*20ms = 700ms --> SAI
}
return;
} // While
} // main
void T_OFF_RC()
{
int16 RH_OFF;
int8 x = 0;
while (true)
{
restart_wdt();
RH_OFF = 0;
RH_OFF = Right();
//delay_us(10);
if (RH_OFF < 400)
{
OUTPUT_LOW(PIN_A5); // OFF direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
delay_ms(20);
x++;
if (x>=35) break; // 35*20ms = 700ms --> SAI
}
return;
} // While
} // main
// Função TOFF L
void T_OFF_LS()
{
int16 LH_OFF;
int8 x = 0;
while (true)
{
restart_wdt();
LH_OFF = 0;
LH_OFF = Left();
//delay_us(10);
if (LH_OFF < 400)
{
OUTPUT_LOW(PIN_A5);
OUTPUT_LOW(PIN_A4);
delay_ms(20);
x++;
if (x>=35) break; // 35*20ms = 700ms --> SAI
}
return;
} // while
} // main
// Função TOFF L
void T_OFF_LC()
{
int16 LH_OFF;
int8 x = 0;
while (true)
{
restart_wdt();
LH_OFF = 0;
LH_OFF = Left();
//delay_us(10);
if (LH_OFF < 400)
{
OUTPUT_HIGH(PIN_A5);
OUTPUT_LOW(PIN_A4);
delay_ms(20);
x++;
if (x>=35) break; // 35*20ms = 700ms --> SAI
}
return;
} // while
} // main
// Função pisca C
void PiscaC()
{
int16 RH_Signal, LH_Signal;
while (true)
{
RH_Signal = 0;
LH_Signal = 0;
RH_Signal = Right();
LH_Signal = Left();
if(RH_Signal >= 400 && LH_Signal >= 400) // 111
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
T_OFF4();
}
else if(RH_Signal >= 400 && LH_Signal < 400) // 101
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
T_OFF_RC();
}
else if(RH_Signal < 400 && LH_Signal >= 400) // 011
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
T_OFF_LC();
}
else // 000
{
break;
}
}
}
// Função pisca S
void PiscaS()
{
int16 RH_Signal, LH_Signal;
while (true)
{
RH_Signal = 0;
LH_Signal = 0;
RH_Signal = Right();
LH_Signal = Left();
if(RH_Signal >= 400 && LH_Signal >= 400) // 110
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
T_OFF4();
}
else if(RH_Signal >= 400 && LH_Signal < 400) // 100
{
OUTPUT_HIGH(PIN_A5); // ON direito
OUTPUT_LOW(PIN_A4); // OFF esquerdo
T_OFF_RS();
}
else if(RH_Signal < 400 && LH_Signal >= 400) // 010
{
OUTPUT_LOW(PIN_A5); // OFF direito
OUTPUT_HIGH(PIN_A4); // ON esquerdo
T_OFF_LS();
}
else // 000
{
break;
}
}
}
// ****** MAIN ******* //
void main(void)
{
int16 HEXA;
OSCCON=0x61; //registo OSCCON 61->4MHZ, 71->8MHZ
setup_adc_ports(SAN0 | SAN1 | SAN2);
setup_adc(ADC_CLOCK_DIV_16); // ver pág.143 datasheet - Tconv = 16/4M = 4us por amostra
//setup_adc(ADC_CLOCK_INTERNAL);
// Configuração das saídas
output_low(PIN_A5); //Saida para sector Lateral Direito
output_low(PIN_A4); //Saida para sector Lateral Esquerdo
// Configuração das entradas
// A0 - Entrada AN0 para leitura do sinal lado Direito
// A1 - Entrada AN1 para leitura do sinal lado Esquerdo
// A2 - Entrada Digital para leitura do sinal Speed
// A3 - Entrada Digital para leitura do sinal de presença
SET_TRIS_A(0x07); // 00000111 - 0x07 - ainda sem entrada de SM no MCLR (A3)
while (true)
{
restart_wdt();
HEXA = 0;
HEXA = Speed_H();
if(HEXA >= 400) // A2 - HIGH
{
OUTPUT_HIGH(PIN_A5); // OFF direito
OUTPUT_HIGH(PIN_A4); // OFF esquerdo
PiscaC();
}
if(HEXA < 400) // A2 - LOW
{
OUTPUT_LOW(PIN_A5); // OFF direito
OUTPUT_LOW(PIN_A4); // OFF esquerdo
PiscaS();
}
} // while
} // main
Is it possible that problem will be in my free version? Can someone compile my code to try?
I have a .exe version - 4.128, I can try it. |
|
|
|
|
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
|