Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Help with Zero Crossing Detector with the 16F1517 on MPLAB XC8

Status
Not open for further replies.

sulfur101

Newbie level 6
Joined
Jul 20, 2022
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
77
Help me I am more familiar with MPLAB but barely and I was wondering if some one can convert this for me. it is a Zero crossing detector

Code:
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
 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
 PORTD.f0 = 1; //Send a 1ms pulse
 delay_ms(1);
 PORTD.f0 = 0;
 ZC = 0;
 }
 }
 }

--- Updated ---

1658340186831.png

here is the schematic
 
Last edited by a moderator:

It's a quite small code.
What prevent you from doing on your own ?
The compilation tells you line by line where the syntax error is.
 

i am not code savy to build my own i have tried for this past month but no advail.
 

If you are using the XC8 compiler (which I assume form the thread title) then get the User Guide as you really will need to refer to it often.
One thing you wil note is that XC8 tends to put 'bits' at the end of register names when you reference the individual bits and bit fields. For example 'PORTD.f0 = 1;' becomes something like 'PORTDbits.RD0 = 1;'. Look at the PIC16F877A data sheet for the bit (field) names as XC8 generally uses the same name for the register bits as the data sheet.
You will also need to read up the XC8 user guide on how to declare the ISR. It is very straight forward but you need to follow the XC8 syntax.
Once you have those changes made, try compiling. You will probably get a whole bunch of error and warning messages but start with the first one, read up what the error message means and make the correction and then try the compile again.
If you get stuck then show us what you have and the problem you are encountering and we can help from there.
Susan
 

[THREAD MERGED]

I need help with coding the PIC said above in doing a Zero Crossing detection out putting into an Optocoupler i am not good at coding, i have done a couple of tests with the 16F877A PIC but my professor now wants me to use the 16F1517 because the experiment was conducted on proteous with the mikro C now i need it in MPLABS but i dont know coding well enough to know what i am doing

here is the code
Code:
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
 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
 PORTD.f0 = 1; //Send a 1ms pulse
 delay_ms(1);
 PORTD.f0 = 0;
 ZC = 0;
 }
 }
 }

1658426933914.png

and here is the schematic
 
Last edited by a moderator:

All of the above comments still apply.
As for using a different chip, that is not an issue as the compiler will take care of any differences in the instruction set (although,without looking at the data sheets, I would assume that the newer chip has the same or a superset of the older chip instruction set.) Also as long as you include the 'xc.h' file, the compiler will also take care of any differences in SFR addresses etc..
What *might* change are the register names and bit field names (although again I would assume this would be minimal and only have new registers and bit fields added) and a compile will quickly show you any of those. You ca then compare the data sheets to see what the 'new' name should be.
You as dealing with a fairly simply piece of code so set up the MPLABx IDE and install the XC8 compiler and give it a go. I think you will be surprised how far you get but you can always come back here when you get stuck.
Susan
 

so i am really stupid
i didnt know that the code was in c++ i was already assuming it was in c
 

but i want to be able to input a POT to adjust the delay i dont know how to do that

this is the code:

Code:
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(5);  //pulse width

              PORTD.B0 = 0;

              ZC = 0;

           }

     }

}

[Moderator action: added code tags]
 
Last edited by a moderator:

so i am really stupid
i didnt know that the code was in c++ i was already assuming it was in c
If it was for a PIC16 using MikroC (or XC8) then the code was actually C. Given that C++ is a super-set of C, most C++ compilers will compile that code OK - but the other way around certainly will not work.
As for adjusting the delay, remember that the 'delay_xx' 'functions' in XC8 are actually macros and the argument cannot be a variable.
The new MCU you are using (PIC16F1517) has a PWM module so you can use that to set the pulse width. (It will not give you a dead-zone at the start of the pulse - you will need a more advanced PWM module for that - but I don;t think that is what you really want.)
As for reading the POT you will need to read up on using the ADC module.
Susan
 

You have a 'delay_ms(2)' before you raise the pulse and then a 'delay_us(5)' as the pulse width.
The PWM module on that MCU will raise the pulse right at the start of the overall output cycle and lower it according to the pulse width setting. Some more advanced PWM modules allow you to also delay the 'raise' of the pulse - and given your comment about 'different angle' I thought that is what you are referring to.
Susan
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top