CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PIC18F45K22 configuration

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

PIC18F45K22 configuration
PostPosted: Thu Feb 23, 2017 3:03 pm     Reply with quote

Hi, this code is for generate tone DTMF, the code working fine with the pic 16F887, but with PIC18F45K22 not, what configuration I need for see the pwm signal on CCP1?

Code:

/*************************************************************************/
/* DTMF.C */
/* Programa para generar tonos DTMF a partir de un teclado 4x4. */
/* Usa el módulo de Captura CCP1 en modo PWM */
/* */
/* Autor: Carlos A. Narváez */
/* Fecha: Septiembre, 2005 */
/*************************************************************************/

//!       1209Hz  1336Hz  1477Hz 1633 Hz
//!697Hz  1       2       3      A
//!770Hz  4       5       6      B
//!852Hz  7       8       9      C
//!941Hz  *       0       #      D

#include <18F45K22.h>
#device adc=10                                        // Resolucion de 1024 valores
#Device PASS_STRINGS=IN_RAM                           // #ZERO_RAM                   
#define FASTER_BUT_MORE_ROM                           // Variables en memoria
#fuses HSH, NOWDT, BROWNOUT, PUT, NOPBADEN, NOHFOFST  //
#fuses NOPLLEN                                        // HW PLL disabled, PLL enabled in software
#fuses MCLR                                           // Master Clear pin enabled
#use delay(clock=20MHz,crystal=20MHz)
#use standard_io(A)
#use standard_io(B)
#use standard_io(C)
#use rs232(baud=9600,bits=8,parity=N,xmit=PIN_C6,rcv=PIN_C7,ERRORS,stream=debug)

CONST unsigned int SINE_WAVE[256] = {
128,131,134,137,140,144,147,150,153,156,159,162,165,168,171,174,
177,179,182,185,187,191,193,196,199,201,204,206,209,211,213,216,
218,220,222,224,226,228,230,232,233,235,237,239,240,241,243,244,
245,246,248,249,250,250,251,252,253,253,254,254,254,255,255,255,
255,255,255,255,254,254,254,253,253,252,251,250,250,249,248,246,
245,244,243,241,240,239,237,235,234,232,230,228,226,224,222,220,
218,216,213,211,209,206,203,201,199,196,193,191,188,185,182,179,
177,174,171,168,165,162,159,156,153,150,147,144,140,137,134,131,
128,125,122,119,116,112,109,106,103,100,97,94,91,88,85,82,79,76,
74,71,68,65,63,60,57,54,52,49,47,45,43,40,38,36,34,32,30,28,26,
24,22,21,19,17,16,15,13,12,11,10,8,7,6,6,5,4,3,3,2,1,1,1,1,1,1,
1,1,1,2,2,2,3,3,4,5,6,6,7,8,10,11,12,13,15,16,17,19,21,24,26,28,
30,32,34,36,38,40,43,45,47,50,52,55,57,60,63,65,68,71,74,77,79,
82,85,88,91,94,97,100,103,106,109,112,116,119,122,125,128};

unsigned long index1,index2;
int inc1,inc2;

#INT_TIMER2
void timer2_isr(void){
    int wave = 0;         
    wave = ((long)SINE_WAVE[index1]+(long)SINE_WAVE[index2])/2;
    set_pwm1_duty(wave);
    index1 += inc1;
    index2 += inc2;
   
    if(index1 >= 256)        index1 -= 256;
    if(index2 >= 256)        index2 -= 256;
}

#define DTMF_ROW1 23 // incremento para 700 Hz, cada 128us
#define DTMF_ROW2 25 // incremento para 750 Hz, cada 128us
#define DTMF_ROW3 28 // incremento para 850 Hz, cada 128us
#define DTMF_ROW4 31 // incremento para 950 Hz, cada 128us
#define DTMF_COLA 40 // incremento para 1200 Hz, cada 128us
#define DTMF_COLB 44 // incremento para 1350 Hz, cada 128us
#define DTMF_COLC 48 // incremento para 1500 Hz, cada 128us
#define DTMF_COLD 54 // incremento para 1650 Hz, cada 128us

void generate_dtmf_tone(char keypad, long duration)  {
 
   index1=0;   index2=0;
   inc1=0;     inc2=0;
   
 if((keypad=='1')||(keypad=='2')||(keypad=='3')||(keypad=='A'))       inc1=DTMF_ROW1;
 else if((keypad=='4')||(keypad=='5')||(keypad=='6')||(keypad=='B'))  inc1=DTMF_ROW2;
 else if((keypad=='7')||(keypad=='8')||(keypad=='9')||(keypad=='C'))  inc1=DTMF_ROW3;
 else if((keypad=='*')||(keypad=='0')||(keypad=='#')||(keypad=='D'))  inc1=DTMF_ROW4;
 
 if((keypad=='1')||(keypad=='4')||(keypad=='7')||(keypad=='*'))       inc2=DTMF_COLA;
 else if((keypad=='2')||(keypad=='5')||(keypad=='8')||(keypad=='0'))  inc2=DTMF_COLB;
 else if((keypad=='3')||(keypad=='6')||(keypad=='9')||(keypad=='#'))  inc2=DTMF_COLC;
 else if((keypad=='A')||(keypad=='B')||(keypad=='C')||(keypad=='D'))  inc2=DTMF_COLD;
 
   enable_interrupts(INT_TIMER2);
   while(duration-- > 0)delay_ms(1);
   disable_interrupts(INT_TIMER2);
   set_pwm1_duty(0x80);
}


void main() {                                                                     
   char k;
set_tris_c(0x00);
     
   setup_timer_2(T2_DIV_BY_1, 127, 5);   
 // Timer2 se incrementa cada 200nseg  overflow cada 25.6 useg 
//y genera una interrupción cada  25.6 * 5 = 128; Fs = 7812.5 Hz
   setup_ccp1(CCP_PWM);                    // Configure CCP1 as a PWM

 
// El tiempo que dura un ciclo de PWM es:(1/clock)*4*t2div*(periodo+1)
// En este programa: clock=20000000 y periodo=127,  entonces el periodo PWM es: (1/20000000)*4*1*128 = 25.6 us ó 39.06 khz
// Fs = 25.6 * 5 = 7812.5 Hz.
   set_pwm1_duty(0x80);
   
   fprintf(debug,"Hello!!\r\n");                      // Imprimir mensaje por el UART1

   while (TRUE) {                             
      k='1';
       fprintf(debug,"%d\r\n",k);
      generate_dtmf_tone(k, 200);
      delay_ms(2000);
     
       k='2';fprintf(debug,"%d\r\n",k);
      generate_dtmf_tone(k, 200);
      delay_ms(2000);
     
       k='3';fprintf(debug,"%d\r\n",k);
      generate_dtmf_tone(k, 200);
      delay_ms(2000);
     
       k='4';fprintf(debug,"%d\r\n",k);
      generate_dtmf_tone(k, 200);
      delay_ms(2000);
   }                                       
}
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Feb 23, 2017 4:11 pm     Reply with quote

depending on your compiler version, you may need to add a'patch' to allow the compiler to correctly access the timer.

this....
Code:

//timerPWM.c
//patch to get PWM running

#byte T2CON = 0xFBA
#byte PR2 = 0xFBB

#define setup_timer_2(prescaler, PR2val, postscaler) \
if(prescaler)   \
  {             \
   PR2 = PR2val;  \
   T2CON =  prescaler | ((postscaler -1) << 3);  \
  }             \
else            \
  T2CON = 0;
//
//end of patch


is what I use when coding the PIC18F46K22 a 'relative' of your PIC.

I haven't looked at any of your code( you said it works...)

Jay
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

PostPosted: Thu Feb 23, 2017 4:34 pm     Reply with quote

temtronic wrote:
depending on your compiler version, you may need to add a'patch' to allow the compiler to correctly access the timer.

this....
Code:

//timerPWM.c
//patch to get PWM running

#byte T2CON = 0xFBA
#byte PR2 = 0xFBB

#define setup_timer_2(prescaler, PR2val, postscaler) \
if(prescaler)   \
  {             \
   PR2 = PR2val;  \
   T2CON =  prescaler | ((postscaler -1) << 3);  \
  }             \
else            \
  T2CON = 0;
//
//end of patch


is what I use when coding the PIC18F46K22 a 'relative' of your PIC.

I haven't looked at any of your code (you said it works...)

Jay


Thanks for answer, it's the first time that I listen about this. I thought only need configure the timer2 and ccp1 as I want. mmm :/
I simulate this with the 887 and not problem... but with this pic I see none on CCP1.

I added your code, ... but still not work :/ I compile with 5.066
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Feb 23, 2017 5:48 pm     Reply with quote

I simulate this with the 887 and not problem... but with this pic I see none on CCP1.

SIMULATE ???

If this is 'Proteus' and not real hardware, then that is your problem....

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 23, 2017 6:40 pm     Reply with quote

The code obviously comes from this PDF:
http://microinternet.com.ve/wp-content/uploads/2012/06/tonos.pdf
It's the 2nd section of code, starting on page 13.

But you have changed it. You have this:
Code:
enable_interrupts(INT_TIMER2);
while(duration-- > 0)delay_ms(1);
disable_interrupts(INT_TIMER2);
set_pwm1_duty(0x80);

But the code in the PDF has this:
Code:

enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while(duration-- > 0)
{
delay_ms(1);
}
disable_interrupts(GLOBAL);
disable_interrupts(INT_TIMER2);

You removed the enable/disable for GLOBAL interrupts. Why ?
If you remove it, the #int_timer2 interrupt routine will never execute.
Put that code back in.

I don't guarantee that the PDF code works. I didn't test it very much.
But if you put the GLOBAL interrupts back in, you should get something.

Also, get rid of the patch posted by temtronic. It's for CCS vs. 4.114.
You have vs. 5.066. You don't need it.
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

PostPosted: Thu Feb 23, 2017 6:50 pm     Reply with quote

temtronic wrote:
I simulate this with the 887 and not problem... but with this pic I see none on CCP1.

SIMULATE ???

If this is 'Proteus' and not real hardware, then that is your problem....

Jay


mmmmm I will check in real, but is curious that if sound the tone (and I can decode the tone with app android since mic of phone across of speaker of pc) with one PIC and not with other in Proteus..
Ok temtronic, thanks you so much. Smile
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

PostPosted: Thu Feb 23, 2017 7:20 pm     Reply with quote

PCM programmer wrote:
The code obviously comes from this PDF:
http://microinternet.com.ve/wp-content/uploads/2012/06/tonos.pdf
It's the 2nd section of code, starting on page 13.

But you have changed it. You have this:
Code:
enable_interrupts(INT_TIMER2);
while(duration-- > 0)delay_ms(1);
disable_interrupts(INT_TIMER2);
set_pwm1_duty(0x80);

But the code in the PDF has this:
Code:

enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while(duration-- > 0)
{
delay_ms(1);
}
disable_interrupts(GLOBAL);
disable_interrupts(INT_TIMER2);

You removed the enable/disable for GLOBAL interrupts. Why ?
If you remove it, the #int_timer2 interrupt routine will never execute.
Put that code back in.

I don't guarantee that the PDF code works. I didn't test it very much.
But if you put the GLOBAL interrupts back in, you should get something.

Also, get rid of the patch posted by temtronic. It's for CCS vs. 4.114.
You have vs. 5.066. You don't need it.

Hi, thank

Yes I delete because it "global" this line is not necesary if is in "main". but the code working very fine in proteus, I listen the sound and decode with the cellphone.
I thinking, the pic is not running fine the timer2, maybe running this pic is hard for proteus... so I will check in real...maybe yes working... but really I see if working in proteus, working in real



But I search use this code in one RTOS with the PIC18F45K22. Smile


Last edited by cvargcal on Thu Feb 23, 2017 7:27 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 23, 2017 7:25 pm     Reply with quote

I don't have Proteus so I can't help you anymore.
Ttelmah



Joined: 11 Mar 2010
Posts: 19548

View user's profile Send private message

PostPosted: Fri Feb 24, 2017 1:39 am     Reply with quote

Read the sticky at the top of the forum about Proteus.

The problem is that Proteus (actually 'Isis' the simulator, Proteus is the PCB design package), will first of all not be aware of things that are necessary to make a chip work. So you tell it 'I have this clock', and it'll accept this, even if the real chip has no clock. Then it doesn't understand supply noise (so chips will run without decoupling, while real chips fail), and it also has so many errors in it's internal 'models' of PIC parts - unfortunately, the I2C module for example has different errata on different PIC chips, but the Isis 'model' is of a single generic part.
Isis is a good way of working out things like analog filter designs, but for a PIC, time spent using the simulator is generally wasted, and in some cases it may actually 'cost' you time, making changes that then have to be completely re-done when you get to a real PIC...

You should start with a real chip, and then start simple (1 Hz flash an LED program), and verify you have got the chip working properly, before moving on to more complex tests.
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Feb 24, 2017 6:48 am     Reply with quote

Mr T is 100% about proteus/Isis....It IS a terrible waste of time !! A 'simulation' will work 100% ,so you configure a PIC by the schematic and it will NOT work. You could spend hours(days ?) on the bench trouble shooting onlt yo find out that while Isis doesn't need a xtal and 2 caps to run the PIC, the Real World does !! Something so basic is,well, unforgiveable. Frankly I wouldn't trust the 'analog' side of it,maybe that really does work but do NOT trust it for any PIC projects! Think.. it can't get a simple, low level function correct so HOW could it possibly get 'complicated' things like timers, ECCP, etc. correct!

Please get a couple of PICs, some perf board, learn to solder stitch and work in the real World.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19548

View user's profile Send private message

PostPosted: Fri Feb 24, 2017 9:18 am     Reply with quote

Very Happy

I was talking about 'pure analog'. Not stuff involving the PIC at all. Things like simple tuned circuits or solid state filters, it seems to be using a 'core' that is almost certainly based on something like Spice. The problem with the PIC is that the models just aren't complete, and too many 'assumptions' are made....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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