SK40C and PIC18F4550 to frequency generation

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

SK40C and PIC18F4550 to frequency generation

Postby abuayyash » Fri Aug 11, 2017 11:20 am

Dear Cytron Team,

I am currently using your product which is SK40C plugged with PIC18F4550 for my project. For coding I am using MPLab IDE and PICKit 2 Programmer.

Initially, to confirm all the connection , the build and upload process into the PIC worked successfully, I have done a simple blink LED project and it worked well.

But when I tried to execute my other project , which is the about generating the frequency using interrupt and external clock, it does not seems to display the desired frequency output. What shown on the oscilloscope is only a high 5V signal without any visible waveform.

Is there any additional configurations needed since I am using external clock and using interrupt, either in MPLab or PICKit?

Here's the source file:


CODE: SELECT_ALL_CODE
#include <p18f4550.h>
#define HZ 8000000   // 16MHz HS xtal = 16000000

#define TOGGLE (HZ * 125)

// RAM vars
volatile unsigned long accum;
volatile unsigned long freqHz;


//=============================================================================
//   INTERRUPT
//=============================================================================
void interrupt(void)
{
  //-------------------------------------------------------
  // This is the TMR2 interrupt, set to 100 timer ticks.
  // Frequency generation is done by a combination of DDS accumulator
  // and a decimal bresenham subtraction that makes the value
  // in actual decimal fractions of Hz.
  //-------------------------------------------------------
  PIR1bits.TMR2IF = 0;      // clear TMR2 roll flag
  accum += freqHz;      // decimal DDS addition into accumulator
  if(accum >= TOGGLE)
  {
    PORTB = (PORTB ^ 0x01);   // toggle output pin to make frequency
    accum -= TOGGLE;    // decimal bresenham subtraction, keep remainder
  }
}
//-----------------------------------------------------------------------------


//=============================================================================
//   MAIN
//=============================================================================
void main(void)
{
  //-------------------------------------------------------
  // setup PIC 16F628A
  CMCON = 0x07;   // comparators OFF

  PORTA = 0b00000000;   // PORTA all outputs
  TRISA = 0b00000000;

  PORTB = 0b00000000;   // RB0 is freq output, rest of PORTB also outputs
  TRISB = 0b00000000;

  // setup TMR2 to roll every 200 ticks
  T2CON = 0b00000100;   // TMR2 ON, 1:1 prescale
  PR2 = (100-1);        // TMR2 int occurs every 100 ticks

  // enable interrupt here (and just leave TNR2 int running in this example)
  INTCON = 0b11000000;  // GIE = ON, PEIE = ON
  PIE1bits.TMR2IE = 1;      // TMR2 interrupt ON
 
  // To set the frequency just set the freqHz variable.
  // If working with low frequencies it may be good to
  // clear the accumulator. Also, start the interrupt if needed.
  // Note! This variable is in Hz*100000, so 23Hz = 2300000
  freqHz = 2312345;  // 23.12345 Hz
  accum = 0;

  //-------------------------------------------------------
  // This is the main running loop/
  // To turn the output freq ON, enable TMR2 interrupt; (PIE1.TMR2IE = 1;)
  // To turn the output freq OFF, disable TMR2 interrupt; (PIE1.TMR2IE = 0;)
  while(1)
  {
    //---------------------------------------------
    // your code goes here!
    PIE1bits.TMR2IE = 1;


  }
}
//-----------------------------------------------------------------------------



Looking forward for your kind assistance.
abuayyash
Greenhorn
 
Posts: 3
Joined: Fri Aug 11, 2017 11:14 am

Re: SK40C and PIC18F4550 to frequency generation

Postby Idris » Fri Aug 11, 2017 4:14 pm

Hi abuayyash,

I have go through the program. It is long time no see PIC program, since everything is Arduino now. :)

I would like to know a few things
1. Did you develop the code or you find this code in Cytron Tutorial website? Or any website, mind to share the link?
2. What compiler and version did you used?
3. Could you share a few clear photos of your hardware setup. SK40C comes with 20MHz crystal, did you change the crystal to 8MHz to match with the code?
4. The code will generate wave with what frequency?
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 409
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Re: SK40C and PIC18F4550 to frequency generation

Postby abuayyash » Tue Aug 15, 2017 10:42 am

Idris WROTE:Hi abuayyash,

I have go through the program. It is long time no see PIC program, since everything is Arduino now. :)

I would like to know a few things
1. Did you develop the code or you find this code in Cytron Tutorial website? Or any website, mind to share the link?
2. What compiler and version did you used?
3. Could you share a few clear photos of your hardware setup. SK40C comes with 20MHz crystal, did you change the crystal to 8MHz to match with the code?
4. The code will generate wave with what frequency?



Hi Idris,

Thank you for your time spent to try my code.

Actually I am not much into PIC, since Arduino is vastly available nowadays.
But I'm not sure whether we Arduino is able to do what the PIC is (claimed to be) capable of. :D

1. The code is from this link http://www.romanblack.com/onesec/High_Acc_Timing.htm#decfreq .
2. I am using MPLab IDE (not the MPLab X but the older version) and C18 compiler. PICKit2 Programmer is used to load the hex file into the PIC.
3. a) Yes I have replaced the 20MHz clock with 8MHz clock.
b) The pin RB7 is also probed into an oscilloscope to display the frequency.
c) The other hardware connection is the common setup of ICSP and the power supply.
4. From the link provided at 1, he proclaims that the code can generate a very fine frequency resolution of more than 0.001 Hz, from the range of 0.001 Hz up until 1 kHz (Actually the range can be larger but what is critical to me is only within the range up to 1kHz only). Which means, if we wanted to generate 123.45 Hz, the oscilloscope reading shows the exact frequency of 123.45 Hz.

ADDITIONAL QUESTION:

1. If we succeed in generating the frequency, is it possible to make it communicate via SPI or I2C in Arduino? Instead of directly changing the frequency inside the code, I want the Arduino to control the frequency value , transmit the connection into the PIC via SPI/I2C , and then the PIC18F will generate the frequency displayable in oscilloscope.
2. After you read the link I've provided, and you think that Arduino/ or your CIKU is capable to perform such function, please let me know and we can ignore this messiness of using PIC.
abuayyash
Greenhorn
 
Posts: 3
Joined: Fri Aug 11, 2017 11:14 am

Re: SK40C and PIC18F4550 to frequency generation

Postby Aethelred » Wed Aug 23, 2017 2:42 pm

Hi abuayyash,

1. The code is from this link http://www.romanblack.com/onesec/High_A ... tm#decfreq .
2. I am using MPLab IDE (not the MPLab X but the older version) and C18 compiler. PICKit2 Programmer is used to load the hex file into the PIC.
3.
a) Yes I have replaced the 20MHz clock with 8MHz clock.
b) The pin RB7 is also probed into an oscilloscope to display the frequency.
c) The other hardware connection is the common setup of ICSP and the power supply.
4. From the link provided at 1, he proclaims that the code can generate a very fine frequency resolution of more than 0.001 Hz, from the range of 0.001 Hz up until 1 kHz (Actually the range can be larger but what is critical to me is only within the range up to 1kHz only). Which means, if we wanted to generate 123.45 Hz, the oscilloscope reading shows the exact frequency of 123.45 Hz.

I think you can use the PWM module in your code to generate the frequency.
Refer from this link for calculation PWM http://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator.html.
Register for duty cycle and PWM Frequency .
*Example: 8Mhz Crystal means Fosc = 8/4 = 2Mhz
Aethelred
Fledgling
 
Posts: 1
Joined: Wed Aug 23, 2017 12:49 pm

Re: SK40C and PIC18F4550 to frequency generation

Postby abuayyash » Wed Aug 23, 2017 6:01 pm

Aethelred WROTE:Hi abuayyash,

1. The code is from this link http://www.romanblack.com/onesec/High_A ... tm#decfreq .
2. I am using MPLab IDE (not the MPLab X but the older version) and C18 compiler. PICKit2 Programmer is used to load the hex file into the PIC.
3.
a) Yes I have replaced the 20MHz clock with 8MHz clock.
b) The pin RB7 is also probed into an oscilloscope to display the frequency.
c) The other hardware connection is the common setup of ICSP and the power supply.
4. From the link provided at 1, he proclaims that the code can generate a very fine frequency resolution of more than 0.001 Hz, from the range of 0.001 Hz up until 1 kHz (Actually the range can be larger but what is critical to me is only within the range up to 1kHz only). Which means, if we wanted to generate 123.45 Hz, the oscilloscope reading shows the exact frequency of 123.45 Hz.

I think you can use the PWM module in your code to generate the frequency.
Refer from this link for calculation PWM http://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator.html.
Register for duty cycle and PWM Frequency .
*Example: 8Mhz Crystal means Fosc = 8/4 = 2Mhz


thanks for the kind assist and effort,Aethelred. But the resolution I wanted is 0.01Hz from 0.01Hz to 999.99Hz, which means the PIC can generate the frequency of 756.53Hz. The PWM calculaotr has the freq resolution around -/+2Hz..
abuayyash
Greenhorn
 
Posts: 3
Joined: Fri Aug 11, 2017 11:14 am


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 10 guests