unsigned char FlagReg;
sbit ZC at FlagReg.B0;
void interrupt(){
if (INTCON.INTF){ //INTF flag raised, so external interrupt occured
ZC = 1;
INTCON.INTF = 0;
}
}
void main() {
PORTB = 0;
TRISB = 0x01; //RB0 input for interrupt
PORTA = 0;
ADCON1 = 7; //Disable ADC
TRISA = 0xFF; //Make all PORTA inputs
PORTD = 0;
TRISD = 0; //PORTD all output
OPTION_REG.INTEDG = 0; //interrupt on falling edge
INTCON.INTF = 0; //clear interrupt flag
INTCON.INTE = 1; //enable external interrupt
INTCON.GIE = 1; //enable global interrupt
while (1){
if (ZC){ //zero crossing occurred
delay_ms(2); // Change to get different angle = 360*delay/16.67
PORTD.B0 = 1; //Send a pulse
delay_us(250); //pulse width
PORTD.B0 = 0;
ZC = 0;
}
}
}
newfile.c:3:2: error: unknown type name 'sbit'
sbit ZC at FlagReg.B0;
^
newfile.c:3:9: error: expected ';' after top level declarator
sbit ZC at FlagReg.B0;
^
;
newfile.c:7:6: error: use of undeclared identifier 'INTCON'
if (INTCON.INTF){ //INTF flag raised, so external interrupt occured
^
newfile.c:9:2: error: use of undeclared identifier 'INTCON'
INTCON.INTF = 0;
^
newfile.c:14:2: error: use of undeclared identifier 'PORTB'
PORTB = 0;
^
newfile.c:15:2: error: use of undeclared identifier 'TRISB'
TRISB = 0x01; //RB0 input for interrupt
^
newfile.c:16:2: error: use of undeclared identifier 'PORTD'
PORTD = 0;
^
newfile.c:17:2: error: use of undeclared identifier 'TRISD'
TRISD = 0; //PORTD all output
^
newfile.c:18:2: error: use of undeclared identifier 'OPTION_REG'
OPTION_REG.INTEDG = 0; //interrupt on falling edge
^
newfile.c:19:2: error: use of undeclared identifier 'INTCON'
INTCON.INTF = 0; //clear interrupt flag
^
newfile.c:20:2: error: use of undeclared identifier 'INTCON'
INTCON.INTE = 1; //enable external interrupt
^
newfile.c:21:2: error: use of undeclared identifier 'INTCON'
INTCON.GIE = 1; //enable global interrupt
^
newfile.c:25:2: error: use of undeclared identifier 'PORTD'
PORTD.f0 = 1; //Send a 1ms pulse
^
newfile.c:26:2: warning: implicit declaration of function 'delay_ms' is invalid in C99 [-Wimplicit-function-declaration]
delay_ms(1);
^
newfile.c:27:2: error: use of undeclared identifier 'PORTD'
PORTD.f0 = 0;
^
1 warning and 14 errors generated.
(908) exit status = 1
nbproject/Makefile-default.mk:107: recipe for target 'build/default/production/newfile.p1' failed
make[2]: Leaving directory 'C:/Users/Omar/Documents/New Folder'
nbproject/Makefile-default.mk:91: recipe for target '.build-conf' failed
make[1]: Leaving directory 'C:/Users/Omar/Documents/New Folder'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make[2]: *** [build/default/production/newfile.p1] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
'sbit' is MikroC specific and is not recognised by XC8. (This is the sort of error I was referring to in your other thread with the same title.) You have three options: 1) 'FlagReg' is not referred to anywhere else in your code, and Z0 refers to the least significant bit, so everywhere Z0 appears, simply write 'FlagReg'. 2) Everywhere where Z0 is set, use code such as 'FlagReg |= 0x01;' and everywhere it is cleared used 'FlagReg &= ~0x01;' 3) Create a struct that defines bit fields (read up on bitfields in C structs).
I strongly recommend the first option.
The other errors about undeclared identifiers are all due to you not including the 'xc.h' file at the start of the code file. In the MPLABx IDE you need to specify the MCU you are using and it will then tell the 'xc.h' include file which SFRs to define for you.
The errors such as 'INTCON.GIE' (again mentioned in the other thread) need to be changed to 'INTCONbits.GIE'. Also 'PORTD.f0' nedds to be changed to 'PORTDbits.RD0' - look at the data sheet for the MCU and you will see they specifiyt all of the bit names for all SFRs that the XC8 expects.
If you want to use the 'delay_xx' macros then you need first #define _XTAL_FREQ to be whatever your system clock frequency is, and then use the "_delay_ms(xx)" form. Look at the XC8 User Guide that explains all of this.
Susan
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#define _XTAL_FREQ 20000000
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config BOREN = OFF
#pragma config LVP = ON
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF
unsigned char FlagReg;
void __interrupt() myISR(void)
{
if (INTCONbits.INTF){ //INTF flag raised, so external interrupt occured
FlagReg = 1;
INTCONbits.INTF = 0;
}
}
void main() {
PORTB = 0;
TRISB = 0x01; //RB0 input for interrupt
PORTA = 0;
ADCON1 = 7; //Disable ADC
TRISA = 0xFF; //Make all PORTA inputs
PORTD = 0;
TRISD = 0; //PORTD all output
OPTION_REG.INTEDG = 0; //interrupt on falling edge
INTCONbits.INTF = 0; //clear interrupt flag
INTCONbits.INTE = 1; //enable external interrupt
INTCONbits.GIE = 1; //enable global interrupt
while (1){
if (FlagReg){ //zero crossing occurred
__delay_ms(2); // Change to get different angle = 360*delay/16.67
RB0 = 1; //Send a pulse
__delay_us(250); //pulse width
RB0 = 0;
FlagReg = 0;
}
}
}
I was using that as an example - a 'template' if you like of how to refer to the bit fields. the "...such as..." is the clue here.The errors such as 'INTCON.GIE' (again mentioned in the other thread) need to be changed to 'INTCONbits.GIE'.
Code:volatile unsigned int MyCounter; char MyTrigger; void __interrupt() ISR(void) { if (INTF == 1) { INTF =0; //cleat int flag MyCounter++; // add 1 to the mycounter if(MyCounter == 32000) MyTrigger = 1; } } void main(void) { PORTB = 0; TRISB = 0x01; //RB0 input for interrupt PORTA = 0; ADCON1 = 7; //Disable ADC TRISA = 0xFF; //Make all PORTA inputs PORTD = 0; TRISD = 0; //PORTD all output OPTION_REG = 1; //Set rising edge trigger INTCON =1; // enable gloabal INT INTE = 1; //enable external interrupt MyCounter =0; //initial values at start up MyTrigger =0; while (1) { if (MyTrigger == 1 )//zero crossing occurred { __delay_ms(2); // Change to get different angle = 360*delay/16.67 RB0 = 1; //Send a pulse __delay_us(250); //pulse width RB0 = 0; MyTrigger = 0; MyCounter = 0; } } }
ok i think i am figuring this out i need help with the interrupts and what i have been seeing from the other posts that there void interrupt is a little different to what i have and for the intcon i dont need all of them rightWhy not follow the pattern for all other SFRs and use 'OPTION_REGbits.INTEDG'?
When I said:
I was using that as an example - a 'template' if you like of how to refer to the bit fields. the "...such as..." is the clue here.
Susan
void __interrupt() myISR(void)
{
if (INTCONbits.INTF == 1){ //INTF flag raised, so external interrupt occured
PORTBbits.RB1 =1;
PORTBbits.RB1 =0;
INTCONbits.INTF = 0;
}
}
void main() {
TRISB0 =1;
TRISB1 =0;
INTCONbits.INTF = 0;
INTCONbits.GIE = 1;
INTCONbits.INTE = 1;
OPTION_REGbits.INTEDG = 1;
PORTBbits.RB1 =0;
ei();
while(1){
if (INTCONbits.INTF){ //zero crossing occurred
__delay_ms(2); // Change to get different angle = 360*delay/16.67
RB0 = 1; //Send a pulse
__delay_us(250); //pulse width
RB0 = 0;
PORTBbits.RB1 = 0;
}
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?