|
|
View previous topic :: View next topic |
Author |
Message |
sebalitter
Joined: 05 May 2015 Posts: 47
|
ccp pic18f46k20 problem compile |
Posted: Tue Aug 04, 2015 4:12 pm |
|
|
Hello!
I have a problem trying to compile this program in ccs and pic18f46k20 in mplab 8 IDE.
Code: |
#if defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#elif defined(__PCH__)
//#include <18F452.h>
#include <18F46K20.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#endif
#device ADC=10
#include <stdio.h>
#include <delays.h>
#include <string.h>
#pragma config FOSC = XT
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = ON
#pragma config BOREN = OFF
#pragma config WDTEN = OFF
//#pragma config CCP2MX = PORTC
#pragma config PBADEN = OFF
#pragma config LPT1OSC = OFF
#pragma config HFOFST = OFF
#pragma config MCLRE = ON
#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config XINST = OFF
#pragma config DEBUG = OFF
//definiciones del Programa
#define LEDRojo1 PORTAbits.RA0 //anodo1 del led rojo
#define LEDRojo2 PORTAbits.RA1 //anodo2 del led rojo
#define SENSOR PORTAbits.RA2
#define FOTO PORTEbits.RE0; //RELE para la camara de fotos
#define k 0.022 //constante para cambio de unidades (Km/hora, M/seg, etc)
//funciones prototipo
void Print_LED(void);//imprime el LED
float Scan(void);//escanea el teclado de funciones
//Variables globales
int vel, repeticion, repet;
float medicion=0;
int contador,duplicado=0,dup=0;
int dig1,dig2;
float tiempo=1; //tiempo entre cada medicion. Debe ser menor o igual a 1 segundo
long rise,fall,pulse_width,count=0;
#int_ccp2
void isr()
{
rise = CCP_1;
fall = CCP_2;
pulse_width = fall - rise; // CCP_1 is the time the pulse went high
count++;
}
float Scan(void)
{
repeticion= (int)(1/tiempo);
repet=repeticion;
medicion=0;
while(repet>0){
medicion += count/tiempo;
repet--;
}
return (medicion/repeticion)*k;
}
void Print_LED(void)
{
vel=(int)Scan();
if(vel>30){
//if(!duplicado)
// FOTO=1;
//PORTVerde=0;
LEDRojo1=1;
LEDRojo2=1;
}
else if (vel<=30 && vel>8){
LEDRojo1=1;
LEDRojo2=1;
//PORTVerde=1;
}
else{
//PORTVerde=0;
//PORTRojo=0;
LEDRojo1=0;
LEDRojo2=0;
duplicado=0;
return;
}
dig1=vel/10;
dig2=vel%10;
switch(dig1)
{
case 0:
LATB=0b01111111;
break;
case 1:
LATB=0b00000110;
break;
case 2:
LATB=0b01011011;
break;
case 3:
LATB=0b01001111;
break;
case 4:
LATB=0b01100110;
break;
case 5:
LATB=0b01101101;
break;
case 6:
LATB=0b01111101;
break;
case 7:
LATB=0b00000111;
break;
case 8:
LATB=0b01111111;
break;
case 9:
LATB=0b01101111;
break;
}
switch(dig2)
{
case 0:
LATD=0b01111111;
break;
case 1:
LATD=0b00000110;
break;
case 2:
LATD=0b01011011;
break;
case 3:
LATD=0b01001111;
break;
case 4:
LATD=0b01100110;
break;
case 5:
LATD=0b01101101;
break;
case 6:
LATD=0b01111101;
break;
case 7:
LATD=0b00000111;
break;
case 8:
LATD=0b01111111;
break;
case 9:
LATD=0b01101111;
break;
}
}
void main(void)
{
ANSEL=0; //desconfiguramos los canales analogicos
ANSELH=0;
TRISA2=1;
TRISA1=0;
TRISA0=0;
TRISD=0;
TRISB=0;
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_ccp2(CCP_CAPTURE_FE); // Configure CCP2 to capture fall
setup_timer_1(T1_INTERNAL); // Start timer 1
enable_interrupts(INT_CCP2); // Setup interrupt on falling edge
enable_interrupts(GLOBAL);
while(1)
{
delay_ms(1000);
Print_LED();
count=0;
}
} |
the console shows this message:
Code: |
Clean: Done.
Executing: "C:\Program Files (x86)\PICC\Ccsc.exe" +FH "main.c" +DF +LN +T +A +M +Z +Y=9 +EA
*** Error 23 "main.c" Line 16(8,9): Can not change device type this far into the code
*** Error 48 "main.c" Line 16(9,12): Expecting a (
*** Error 43 "main.c" Line 16(13,15): Expecting a declaration
*** Error 18 "main.c" Line 20(10,20): File can not be opened
Not in "C:\Program Files (x86)\PICC\devices\delays.h"
Not in "C:\Program Files (x86)\PICC\drivers\delays.h"
Not in project "C:\Users\Agustina\MPLABXProjects\delays.h"
*** Error 12 "main.c" Line 122(9,10): Undefined identifier PORTAbits
*** Error 12 "main.c" Line 123(9,10): Undefined identifier PORTAbits
*** Error 12 "main.c" Line 126(9,10): Undefined identifier PORTAbits
*** Error 12 "main.c" Line 127(9,10): Undefined identifier PORTAbits
*** Error 12 "main.c" Line 133(9,10): Undefined identifier PORTAbits
*** Error 12 "main.c" Line 134(9,10): Undefined identifier PORTAbits
*** Error 12 "main.c" Line 145(1,5): Undefined identifier LATB
*** Error 12 "main.c" Line 148(1,5): Undefined identifier LATB
*** Error 12 "main.c" Line 151(1,5): Undefined identifier LATB
*** Error 12 "main.c" Line 154(1,5): Undefined identifier LATB
*** Error 12 "main.c" Line 157(1,5): Undefined identifier LATB
*** Error 12 "main.c" Line 160(1,5): Undefined identifier LATB
*** Error 12 "main.c" Line 163(1,5): Undefined identifier LATB
*** Error 12 "main.c" Line 166(1,5): Undefined identifier LATB
*** Error 12 "main.c" Line 169(1,5): Undefined identifier LATB
*** Error 12 "main.c" Line 172(1,5): Undefined identifier LATB
*** Error 12 "main.c" Line 179(1,5): Undefined identifier LATD
*** Error 12 "main.c" Line 182(1,5): Undefined identifier LATD
*** Error 12 "main.c" Line 185(1,5): Undefined identifier LATD
*** Error 12 "main.c" Line 188(1,5): Undefined identifier LATD
*** Error 12 "main.c" Line 191(1,5): Undefined identifier LATD
*** Error 12 "main.c" Line 194(1,5): Undefined identifier LATD
*** Error 12 "main.c" Line 197(1,5): Undefined identifier LATD
*** Error 12 "main.c" Line 200(1,5): Undefined identifier LATD
*** Error 12 "main.c" Line 203(1,5): Undefined identifier LATD
*** Error 12 "main.c" Line 206(1,5): Undefined identifier LATD
*** Error 12 "main.c" Line 224(1,6): Undefined identifier ANSEL
*** Error 12 "main.c" Line 225(1,7): Undefined identifier ANSELH
*** Error 12 "main.c" Line 226(1,7): Undefined identifier TRISA2
*** Error 12 "main.c" Line 227(1,7): Undefined identifier TRISA1
*** Error 12 "main.c" Line 228(1,7): Undefined identifier TRISA0
*** Error 12 "main.c" Line 229(1,6): Undefined identifier TRISD
*** Error 12 "main.c" Line 231(1,6): Undefined identifier TRISB
37 Errors, 0 Warnings. |
can someone help me with this problem?
thanks |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Aug 04, 2015 9:04 pm |
|
|
what compiler was this written for ?
and the repeating error :
Undefined identifier
is exactly spot on correct.
you don't declare any of those VAR/Register names that i can see,
in reading what you coded, i have to ask if
have you read the CCS manual,
ESPECIALLY including the section entitled:
General Purpose I/O ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Wed Aug 05, 2015 3:01 am |
|
|
It's a mix of code for about three different compilers.
CCS header, and some fuses.
Microchip fuses.
Some Microchip code, and a few unidentified bits.....
Even worse the settings differ. So the CCS part is selecting the HS oscillator, while the Microchip part selects XT.
There are occasions when you think a sledgehammer would be the best educational tool...... |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Aug 05, 2015 8:47 am |
|
|
There are a number of awesome expressions in this 'program'
but the logic of following is my hands down favorite:
keeping in mind that tiempo is a float ........
Code: |
repeticion= (int)(1/tiempo); |
i ask myself - what is the possible range of values that
INT repeticion will assume, no matter how long this program runs,
as written ???
i just love the 'logic' shown by tracing tiempo from initial
declaration through each use in the program.
i would enjoy seeing the schematic for this device........
bring me that hammer? |
|
|
sebalitter
Joined: 05 May 2015 Posts: 47
|
working but not counting rise pulse |
Posted: Thu Aug 06, 2015 12:08 pm |
|
|
Thanks for the answers!
I didn't explain what i want to do.
I have a speedometer that makes a rectangular pulse with ups and downs.
The number of ups in a second * k gives the speed.
so, I have to count on the rise.
The signal is on pin_C2.
updated and compiling OK.
It has to display the result in the 7-segment leds but the disply doesn't show anything.
is the CCP declaration correct?
I think that is interrupting wrong in CCP1_isr() .
#include <18F46K20.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=10000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//definiciones del Programa
#define LEDRojo1 PIN_A0 //anodo1 del led rojo
#define LEDRojo2 PIN_A1 //anodo2 del led rojo
#define LEDVerde1 PIN_C2 //anodo2 del led rojo
#define LEDVerde2 PIN_C3 //anodo2 del led rojo
#define SENSOR PIN_A2
#define FOTO PIN_E0 //RELE para la camara de fotos
#define k 0.022 //constante para cambio de unidades (Km/hora, M/seg, etc)
#define INTS_PER_SECOND 10 // (10000000/(4*4*65536)) = 9.53
//funciones prototipo
void Print_LED(void); //imprime el LED
float Scan(void); //escanea el teclado de funciones
//Variables globales
int contador=0,duplicado=0,dup=0, dig1,dig2, repeticion, repet;
float tiempo=1,vel, medicion=0; //tiempo entre cada medicion. Debe ser menor o igual a 1 segundo
long rise,fall,pulse_width,count=0;
char digito[2];
int8 seconds; // A running seconds counter
int8 int_count; // Number of interrupts left before a second has elapsed
//Los eventos serán bajadas o subidas de los pines correspondientes a CCP1 y CCP2 que son, respectivamente, RC2 y RC1
#int_ccp1
void CCP1_isr()
{
count++;
}
#int_timer1 // This function is called every time
//void clock_isr() { // timer 1 overflows (65535->0), which is
void TIMER1_isr(void)
{ // approximately 19 times per second for
if(--int_count==0) { // this program.
++seconds;
int_count = INTS_PER_SECOND;
Print_LED();
}
}
void main(void)
{
SET_TRIS_B( 0xFF ); // pone B0, B1, B2 , B3 B4, B5, B6 y B7 como salidas
SET_TRIS_D( 0xFF ); // pone D0, D1, D2 , D3 D4, D5, D6 y D7 como salidas
SET_TRIS_A( 0xFF ); // pone A0 y A1 como salidas , A2 como entrada
SET_TRIS_E( 0xFF ); // pone E como salida
SET_TRIS_C( 0b01101111 ); // pone C como salida, C2 como entrada (sensor)
int_count = INTS_PER_SECOND;
setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);
set_timer1(0);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_CCP1); // Setup interrupt on falling edge
enable_interrupts(GLOBAL);
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
while(true){}
}
void Print_LED(void)
{
vel= count * k;
//seteo de variables a cero
count=0;
//seconds=0;
//set_timer1(0);
//vel=contador;
if(vel>30){
//if(!duplicado)
// FOTO=1;
output_high(LEDVerde1);
output_high(LEDVerde2);
output_high(LEDRojo1);
output_high(LEDRojo2);
}
else if (vel<=30 && vel>8){
output_high(LEDVerde1);
output_high(LEDVerde2);
output_high(LEDRojo1);
output_high(LEDRojo2);
}
else{
output_high(LEDVerde1);
output_high(LEDVerde2);
output_high(LEDRojo1);
output_high(LEDRojo2);
duplicado=0;
return;
}
sprintf(digito,"%f",vel);
//dig1=(int)(vel/10);
//dig2=(int)(vel%10);
switch(digito[0])
{
case '0':
output_b(0b01111111);
break;
case '1':
output_b(0b00000110);
break;
case '2':
output_b(0b01011011);
break;
case '3':
output_b(0b01001111);
break;
case '4':
output_b(0b01100110);
break;
case '5':
output_b(0b01101101);
break;
case '6':
output_b(0b01111101);
break;
case '7':
output_b(0b00000111);
break;
case '8':
output_b(0b01111111);
break;
case '9':
output_b(0b01101111);
break;
}
switch(digito[1])
{
case '0':
output_d(0b01111111);
break;
case '1':
output_d(0b00000110);
break;
case '2':
output_d(0b01011011);
break;
case '3':
output_d(0b01001111);
break;
case '4':
output_d(0b01100110);
break;
case '5':
output_d(0b01101101);
break;
case '6':
output_d(0b01111101);
break;
case '7':
output_d(0b00000111);
break;
case '8':
output_d(0b01111111);
break;
case '9':
output_d(0b01101111);
break;
}
}
VAMOS RIVER Carajo!! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Thu Aug 06, 2015 12:26 pm |
|
|
Take your program and cut in half.
1st half the 'tachometer' section
2nd half the 'display results' section
For test purposes I'd do the 1st half and send the 'result' vai RS232 to a PC terminal program. This way you can easily confirm the correct operation of the 'tachometer' section.
Next, I'd just code the 'display section' and use 'dummy' variables to confirm the 'display section' is working correctly. Maybe have a 'counter' variable go from 0 to 9999 every 1/2 second.Slow enough to see, fast enough to confirm all segments of all digits are correct.
Once both of these 'sections' are working, 'merge' the codes and it _should_ work....
Jay |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Thu Aug 06, 2015 12:28 pm |
|
|
Hi,
First, learn to to use the 'Code' button when showing us your code. Without this, it's tedious to try to study your posted code......
You said this:
Quote: |
It has to display the result in the 7-segment leds but the disply doesn't show anything. |
Which tells me that you don't know how to properly troubleshoot code during development. You really need to break your problem into at least two components - the speed measurement portion, and the display portion. You should be sending the measured speed to a serial terminal initially to verify its functionality, and you should be sending the display routines some 'canned' data to verify that they are working. Only after they are both working correctly should you attempt to make them work together!
You should also post your schematic! Use an image hosting site to do this, and post the link here. _________________ John
If it's worth doing, it's worth doing in real hardware! |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu Aug 06, 2015 12:57 pm |
|
|
IF you make digito[] actually be an INT8 of the character in question: ie: conform to absolute values 0-9 there is a good shortcut BUT
passing over a '0' to '9' will NOT work in any iteration of your code without parsing through atoi() to make it so.
you never read anything from pin_C2 either.
and of course you never SHOW digito[] receiving any display value anyway, so i have no clue what you expect it to do in the MESS you posted.
i can't help you unless you get better at showing the FULL code in a way we can read.........
B U T
this consolidates a lot of your code for output pattern IF digito contains binary values 0-9 ..
Code: |
const byte ledpattern[10]={0b01111111,0b00000110,0b01011011,0b01001111,0b01100110,0b01101101,
0b01111101,0b00000111,0b01111111,0b01101111}
output_B(ledpattern[digito[0]]);
output_D(ledpattern[digito[1]]);
|
|
|
|
sebalitter
Joined: 05 May 2015 Posts: 47
|
1 half problem |
Posted: Sun Aug 09, 2015 3:23 pm |
|
|
The only problem in the code is the count. It is not counting correctly.
The display shows the numbers correctly.
Now the code is updated with the code that Asmboy gave me.
Im trying to use the RS232 to send the result ( vel ) to the hyperterminal as temtronic proposed.
Can i send the result by pickit2 or the only way is to connect a MAX232 converter? i didn't find a good example.
Also I used the debugger of MPLABX with pickit2 and found that the program is not doing the CCP1_interrupt() so its not counting.
What im doing wrong with the ccp?
Thanks
Code: |
#include <18F46K20.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=10000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, BITS=8, PARITY = N,ERRORS, stream=PC)
//definiciones del Programa
#define LEDRojo1 PIN_A0 //anodo1 del led rojo
#define LEDRojo2 PIN_A1 //anodo2 del led rojo
#define LEDVerde1 PIN_C2 //anodo2 del led rojo
#define LEDVerde2 PIN_C3 //anodo2 del led rojo
#define SENSOR PIN_A2
#define FOTO PIN_E0 //RELE para la camara de fotos
#define k 0.022 //constante para cambio de unidades (Km/hora, M/seg, etc)
#define INTS_PER_SECOND 10 // (10000000/(4*4*65536))
//funciones prototipo
void Print_LED(void); //imprime el LED
//Variables globales
int contador=0,duplicado=0,dup=0, dig1,dig2, repeticion, repet, vel;
float tiempo=1, medicion=0; //tiempo entre cada medicion. Debe ser menor o igual a 1 segundo
long rise,fall,pulse_width,count=6;
char digito[2];
int8 seconds; // A running seconds counter
int8 int_count; // Number of interrupts left before a second has elapsed
const byte ledpattern[10]={0b01111111,0b00000110,0b01011011,0b01001111,0b01100110,0b01101101,0b01111101,0b00000111,0b01111111,0b01101111};
//Los eventos serán bajadas o subidas de los pines correspondientes a CCP1 y CCP2 que son, respectivamente, RC2 y RC1
#int_ccp1
void CCP1_isr()
{
count++;
}
#int_timer1 // This function is called every time
//void clock_isr() { // timer 1 overflows (65535->0), which is
void TIMER1_isr(void)
{ // approximately 19 times per second for
if(--int_count==0) { // this program.
++seconds;
int_count = INTS_PER_SECOND;
Print_LED();
}
}
void main(void)
{
SET_TRIS_B( 0xFF ); // pone B0, B1, B2 , B3 B4, B5, B6 y B7 como salidas
SET_TRIS_D( 0xFF ); // pone D0, D1, D2 , D3 D4, D5, D6 y D7 como salidas
SET_TRIS_A( 0xFF ); // pone A como salidas
SET_TRIS_E( 0xFF ); // pone E como salida
SET_TRIS_C( 0b01101111 ); // pone C como salida, C2 como entrada (Sensor)
int_count = INTS_PER_SECOND;
setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);
set_timer1(0);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_CCP1); // Setup interrupt on falling edge
enable_interrupts(GLOBAL);
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
while(true)
{
}
}
void Print_LED(void)
{
vel = count * k;
// printf("%f\r\n", vel);
fprintf(PC, vel);
//vel = seconds;
//seteo de variables a cero
count=0;
output_high(LEDVerde1);
output_high(LEDVerde2);
output_high(LEDRojo1);
output_high(LEDRojo2);
dig1=(int)(vel/10);
dig2=((int)vel)%10;
output_B(ledpattern[dig1]);
output_D(ledpattern[dig2]);
} |
|
|
|
|
|
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
|