Led and Frequencies

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
mastertecnology76
Posts: 46
Joined: 13 Dec 2013 10:25
Contact:

Led and Frequencies

#1 Post by mastertecnology76 » 22 Nov 2018 12:15

I'm doing a project that can turn on the LEDs based on a frequency band, but I do not know how to proceed with some examples?

Code: Select all

//  1 : 312 - 1388
                //  2 : 1388 - 2464
                //  3 : 2464 - 3540
                //  4 : 3540 - 4616
                //  5 : 4616 - 5692
                //  6 : 5692 - 6768
                //  7 : 6768 - 7844
                //  8 : 7844 - 8920
                //  9 : 8920 - 10000

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: Led and Frequencies

#2 Post by Sparky1039 » 23 Nov 2018 00:01

Use the "if" statement and logical AND (&&) relational operator...

Code: Select all

if ((X >= 312) && (X < 1388)) {
   Light up freq band 1 LEDs ;
}
else if ((X >= 1388) && (X < 2464)) {
   Light up freq band 2 LEDs ;
}
else if ...
else if ...
else if ...

else {
All freq band LEDs off ; // if nothing matches shut off LEDs
}

mastertecnology76
Posts: 46
Joined: 13 Dec 2013 10:25
Contact:

Re: Led and Frequencies

#3 Post by mastertecnology76 » 23 Nov 2018 18:39

Code: Select all

/* Project name:
     Spectrum con Cubo 3X3
 * Description:
     Spectrum con Cubo 3X3
 * Configuration:
     MCU:             PIC16F1508
     Oscillator:      16.0000 MHz
  * SW:
     mikroC PRO for PIC */
/*****************************************************************************/
void initSettings();
void OFF();
void Lettura();
/*****************************************************************************/
#define X PORTA.RA2
#define B1 PORTA.RA0
#define B2 PORTA.RA1
#define B3 PORTA.RA3
#define M1 PORTA.RA4
#define M2 PORTA.RA5
#define M3 PORTA.RB4
#define A1 PORTA.RB5
#define A2 PORTA.RB6
#define A3 PORTA.RB7
/*****************************************************************************/
void main()
{
initSettings();
while (1)
{
Lettura();
}
}
/*****************************************************************************/
void initSettings ()
{
OSCCON = 0b01111000;                             //Frequenza interna 16MHz
//Disabilito i moduli che non mi occorrono
FVRCON.FVREN = 0;                                //Fixed Voltage Reference
DACCON0.DACEN = 0;                               //Modulo DAC
CM1CON0.C1ON = 0;                                //Comparatore 1
CM2CON0.C2ON = 0;                                //Comparatore 2
PWM1CON.PWM1EN = 0;                              //PWM 1
PWM2CON.PWM2EN = 0;                              //PWM 2
PWM3CON.PWM3EN = 0;                              //PWM 3
PWM4CON.PWM4EN = 0;                              //PWM 4
CLC1CON.LC1EN = 0;                               //Logic Cell 1
CLC2CON.LC2EN = 0;                               //Logic Cell 2
NCO1CON.N1EN = 0;                                 //Numerically Controlled Oscillator
CWG1CON0.G1EN = 0;                                //Complementary Waveform Generator
/*****************************************************************************/
OPTION_REG = 0b11011000 ;                         //no prescaler
INTCON = 0b10110000 ;                             //T0IF, INTF and GIE enabled
ADCON0 = 0b00000100;                              //Abilito convertitore ADC e rendo RA0 Analogico
ADCON1 = 0b00000010;                              //Vss and Vdd as voltage references
ADCON2 = 0b10110000;                              //Configure the ADC acquisition time according to the datasheet
ANSELA = 0b00000100;                              //RA2 Analogico
ANSELC = 0b00000000;                              //Tutti digitali
TRISA = 0b00000100;                               //RA2 ingresso il resto uscite.
PORTA = 0b00000100;                               //RA2 ingresso il resto uscite.
TRISC = 0b00000000;                               //Uscite
PORTC = 0b00000000;                               //Uscite
}

void Lettura() {
if ((X >= 312) && (X < 1388)) {                   //1 : 312 - 1388
B1 = 1;
}
else if ((X >= 1388) && (X < 2464)) {             //2 : 1388 - 2464
B2 = 1;
}
else if ((X >= 2464) && (X < 3540)) {             //3 : 2464 - 3540
B3 = 1;
}
else if ((X >= 3540) && (X < 4616)) {             //4 : 3540 - 4616
M1 = 1;
}
else if ((X >= 4616) && (X < 5692)) {             //5 : 4616 - 5692
M2 = 1;
}
else if ((X >= 5692) && (X < 6768)) {             //6 : 5692 - 6768
M3 = 1;
}
else if ((X >= 6768) && (X < 7844)) {             //7 : 6768 - 7844
A1 = 1;
}
else if ((X >= 7844) && (X < 8920)) {             //8 : 7844 - 8920
A2 = 1;
}
else if ((X >= 8920) && (X < 10000)) {            //9 : 8920 - 10000
A3 = 1;
}
else {
OFF;
}
}

void OFF(){
B1=0;
B2=0;
B3=0;
M1=0;
M2=0;
M3=0;
A1=0;
A2=0;
A3=0;
}
To read the frequency from the RA2 pin how do I do it?

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: Led and Frequencies

#4 Post by Sparky1039 » 23 Nov 2018 20:05

To read the frequency from the RA2 pin how do I do it?
This is a complex and advanced topic question not answered easily on a forum.

If the frequency signal is a single pure tone then use the PIC CCP module in capture mode (search web for how to do this. As a start look for Microchip tips & tricks application note DS41214A). If the signal is sinusoidal you will have to convert it to a square wave requiring hardware squaring circuits (comparator ect...) in order to interface it to the PIC.
>>> EDIT: After looking at the datasheet for this PIC, it doesn't have a CCP module so you are out of luck using this method. There is the possibility of using a timer to measure the period of your input square wave, but this will require some efficient code writing using interrupts (i.e. basically constructing a "soft" CCP hardware module in code).

If your frequency signal is audio or multi-tone then you will have to use DSP techniques (i.e. FFT processing) and perhaps use a dsPIC micro-controller for this depending upon your requirements. This technique will not be possible using such a small and under powered PIC due to speed and memory limitations.

From what I could see in your code comments the PIC16F1508/9 looks to be a poor choice for what you want to do.

Lastly

Code: Select all

else {
OFF;
} 
should be

Code: Select all

else {
OFF();
}

mastertecnology76
Posts: 46
Joined: 13 Dec 2013 10:25
Contact:

Re: Led and Frequencies

#5 Post by mastertecnology76 » 23 Nov 2018 20:34

if instead I replace this pic with one with CCP module I should succeed in my intent? Can you suggest one?

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: Led and Frequencies

#6 Post by Sparky1039 » 23 Nov 2018 20:55

if instead I replace this pic with one with CCP module I should succeed in my intent?
If the signal to be measured is reasonably clean (low jitter), is a square wave, and is a single frequency, then yes (using techniques from Microchip tips & tricks application note DS41214A)
Can you suggest one?
Not knowing all the requirements of your application besides measuring frequency with a CCP module this is a difficult question to answer. However you might consider a PIC18F26K22 as a start. PIC18F family of processors are far more efficient for a multitude of reasons when using C compilers.

mastertecnology76
Posts: 46
Joined: 13 Dec 2013 10:25
Contact:

Re: Led and Frequencies

#7 Post by mastertecnology76 » 23 Nov 2018 22:44

Code: Select all

/* Project name:
     Spectrum con Cubo 3X3
 * Description:
     Spectrum con Cubo 3X3
 * Configuration:
     MCU:             PIC16F1829
     Oscillator:      32.0000 MHz
  * SW:
     mikroC PRO for PIC */
/*****************************************************************************/
void initSettings();
void OFF();
void Lettura();
/*****************************************************************************/
#define X PORTA.RA2
#define B1 PORTA.RA0
#define B2 PORTA.RA1
#define B3 PORTA.RA3
#define M1 PORTA.RA4
#define M2 PORTA.RA5
#define M3 PORTA.RB4
#define A1 PORTA.RB5
#define A2 PORTA.RB6
#define A3 PORTA.RB7
/*****************************************************************************/
void main()
{
initSettings();
while (1)
{
Lettura();
}
}
/*****************************************************************************/
void initSettings ()
{
OSCCON = 0b11110000;                             //Frequenza interna 32MHz
//Disabilito i moduli che non mi occorrono
FVRCON.FVREN = 0;                                //Fixed Voltage Reference
DACCON0.DACEN = 0;                               //Modulo DAC
CM1CON0.C1ON = 0;                                //Comparatore 1
CM2CON0.C2ON = 0;                                //Comparatore 2
MDSRC = 0;                                       //PWM
MDCARH = 0;                                      //PWM CCP
/*****************************************************************************/
OPTION_REG = 0b11011000 ;                         //no prescaler
INTCON = 0b10110000 ;                             //T0IF, INTF and GIE enabled
ADCON0 = 0b00000100;                              //Abilito convertitore ADC e rendo RA2 Analogico
ADCON1 = 0b00000010;                              //Vss and Vdd as voltage references
ANSELA = 0b00000100;                              //RA2 Analogico
ANSELC = 0b00000000;                              //Tutti digitali
TRISA = 0b00000100;                               //RA2 ingresso il resto uscite.
PORTA = 0b00000100;                               //RA2 ingresso il resto uscite.
TRISC = 0b00000000;                               //Uscite
PORTC = 0b00000000;                               //Uscite
}

void Lettura() {
if ((X >= 312) && (X < 1388)) {                   //1 : 312 - 1388
B1 = 1;
}
else if ((X >= 1388) && (X < 2464)) {             //2 : 1388 - 2464
B2 = 1;
}
else if ((X >= 2464) && (X < 3540)) {             //3 : 2464 - 3540
B3 = 1;
}
else if ((X >= 3540) && (X < 4616)) {             //4 : 3540 - 4616
M1 = 1;
}
else if ((X >= 4616) && (X < 5692)) {             //5 : 4616 - 5692
M2 = 1;
}
else if ((X >= 5692) && (X < 6768)) {             //6 : 5692 - 6768
M3 = 1;
}
else if ((X >= 6768) && (X < 7844)) {             //7 : 6768 - 7844
A1 = 1;
}
else if ((X >= 7844) && (X < 8920)) {             //8 : 7844 - 8920
A2 = 1;
}
else if ((X >= 8920) && (X < 10000)) {            //9 : 8920 - 10000
A3 = 1;
}
else {
OFF;
}
}

void OFF(){
B1=0;
B2=0;
B3=0;
M1=0;
M2=0;
M3=0;
A1=0;
A2=0;
A3=0;
}
I changed Pic now how can I use the ccp module? I'm not clear what information I found on the net.

Post Reply

Return to “mikroC PRO for PIC General”