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 support@ccsinfo.com

protocol nec

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



Joined: 16 Oct 2015
Posts: 3

View user's profile Send private message

protocol nec
PostPosted: Fri Oct 16, 2015 3:20 pm     Reply with quote

hi all

The frequency is not getting stabilized in output pin_c2, 38Khz. Does anyone know what's wrong with my code ?

sorry very bad my english !
Code:

//#include "emisorNEC.h"
#include <18f4550.h>
#fuses nomclr
#fuses hs // LP,XT,HS,RC
#fuses put
#fuses wdt // NOWDT,WDT
#fuses nolvp // NOLVP,LVP
#fuses nobrownout // NOBROWNOUT,BROWNOUT
#fuses noput // NOPUT,PUT
#fuses nodebug // NODEBUG,DEBUG
#fuses protect // NOPROTECT,PROTECT
#fuses nowrt // NOWRT,WRT_50%,WRT_25%,WRT_5%
#use delay(clock=4Mhz)
#define PIN_PWM PIN_d0
#use rs232 (baud =9600, parity=n,xmit=pin_b2,rcv=pin_b1,bits=8,stream=blue)
#define IR_CODE_TV_ON 0x20DF10Ef//L

void ir_send(int32 code);
boolean first;

void main()
{

setup_ccp1(CCP_PWM);
set_pwm1_duty(0);
setup_timer_2(T2_DIV_BY_1,25,1);//131


for (;;) {

delay_ms(500);
set_pwm1_duty(13);
output_high(pin_D0);
ir_send(IR_CODE_TV_ON);
}
}


void ir_send(unsigned int32 code)
{
unsigned char i = 0;
disable_interrupts(GLOBAL);

// Envío el START
set_pwm1_duty(13);
delay_us(9000);

set_pwm1_duty(0);
delay_us(4500);

// Voy desgranando el código
while (i < 32) {
//#bit first = code.32;

first = bit_test(code,i);
//first = ~first;

fprintf(blue,"%d\n",first);

// Transmitimos un 1
if (first) {
set_pwm1_duty(13);
delay_us(560);
set_pwm1_duty(0);
delay_us(1690);
}

// Transmitimos un 0
else {
set_pwm1_duty(13);
delay_us(560);
set_pwm1_duty(0);
delay_us(560);
}

{
if (first) {
set_pwm1_duty(13);
delay_us(560);
set_pwm1_duty(0);
delay_us(1690);
}

// Transmitimos un 0
else {
set_pwm1_duty(13);
delay_us(560);
set_pwm1_duty(0);
delay_us(560);
}

}
//code <<= 1;
i++;

}

// Bit de parada
set_pwm1_duty(13);
delay_us(560);
set_pwm1_duty(0);

enable_interrupts(GLOBAL);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 16, 2015 11:00 pm     Reply with quote

Quote:
The frequency is not getting stabilized in output pin_c2, 38Khz

The compiler defaults to CPUDIV4 for the oscillator, so your PIC is running
at 1 MHz, and not at 4 MHz. To fix this, add the following fuse:
Code:
#fuses CPUDIV1


Also, you are running the Watchdog Timer, but you never restart it. So,
your PIC will do a watchdog reset about every 2 minutes.

Also, you are enabling Global interrupts, but you don't have any specific
interrupts enabled. So why enable global interrupts ?

There are a few other other things that are a little weird in the fuses,
such as protect, noput, nobrownout. I assume you are running at +5v.
I would have set all those fuses as the opposite of what you have.

Also, I want a 10K MCLR resistor, and without the NOMCLR fuse, so I
can make the board run or not run, with a mouse-click on the Release-
from-Reset button in MPLAB vs. 9.82.
ramos_luna



Joined: 16 Oct 2015
Posts: 3

View user's profile Send private message

PostPosted: Sat Oct 17, 2015 3:21 pm     Reply with quote

Obrigado pela dica (PCM programmer)!! estava uma bagunça meus fuses Embarassed

más o meu receptor IR ainda não esta recebendo os bytes do meu codigo !, acho que tem a ver com a modulação de 38kHZ que não esta gerando certo no pin c2
Obs: O meu recptor esta funcionando normal pois testei com um controle remoto ! porem o emissor que quero ainda não !

translate :

Thanks for the tip !! tava a mess my fuses Embarassed

bad my IR receiver is not yet receiving the bytes of my code! I think that has to do with the modulation of 38kHz that is not generating the right pin c2
Note: My recptor this normal functioning as tested with a remote control ! My recptor this normal functioning as tested with a remote control! but the sender that I want not !

Code:

#include <18f4550.h>

#fuses mclr
#fuses CPUDIV1
#fuses xt // LP,XT,HS,RC
#fuses put
#use delay(clock=4Mhz)
#define PIN_PWM   PIN_c1
#use rs232 (baud =9600, parity=n,xmit=pin_b2,rcv=pin_b1,bits=8,stream=blue)
#define IR_CODE_TV_ON   0x20DF10EF
#define IR_CODE_TV_Off  0x21DF20bF

#use fast_io(c)
boolean first;
void ir_send(int32 code);


void main()
{
set_tris_c(0xf9);
output_low(pin_c2);
   
   
    SETUP_CCP1(CCP_PWM);               
    SETUP_TIMER_2(T2_DIV_BY_1, 25,1);
    set_pwm1_duty(0);
   

   for (;;) {
   
       
         delay_ms(2000);     
         ir_send(IR_CODE_TV_ON);
   }
}

void ir_send(unsigned int32 code)
{
   unsigned char i = 0;
   
   // Envío el START
           
    set_pwm1_duty(13);
    delay_us(9000);
   
   set_pwm1_duty(0);
   delay_us(4500);

   // bit a bit código
   while (i < 32) {
     // #bit first = code.31
       first = bit_test(code,i);
       fprintf(blue,"%d\n",first);
       
      // Transmitir 1
      if (first) {
         
         set_pwm1_duty(13);
         delay_us(560);
       
         set_pwm1_duty(0);
         delay_us(1690);   
      }
     
      // Transmitir  0
      else {
     
         set_pwm1_duty(13);
         delay_us(560);   
         

         set_pwm1_duty(0);
         delay_us(560);   
      }
     
       
      i++;
   }
   
   // Bit de parada
     
   set_pwm1_duty(13);
   delay_us(560);     
   set_pwm1_duty(0);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 17, 2015 6:46 pm     Reply with quote

I don't think your IR sending function is correct. I think you are sending
the bits in the wrong order.

See this code:
https://github.com/z3t0/Arduino-IRremote/blob/master/ir_NEC.cpp
Their for() loop starts with the bitmask set to 0x80000000. Then they
shift the bitmask right by 1 bit, after each pass through the for() loop.
So they start at bit 31, and continue down to bit 0.

But your code starts with bit 0. I think that's wrong. You need to fix it.
ramos_luna



Joined: 16 Oct 2015
Posts: 3

View user's profile Send private message

PostPosted: Sat Oct 17, 2015 11:26 pm     Reply with quote

Quote:
I don't think your IR sending function is correct

I had already noticed that. Too bad my biggest problem is to send the bit to my receiver. Follows the receiver code

it's OK:
Code:

#include <18f4550.h>
#fuses HS
#use delay(clock=20Mhz)
#define LCD_DB4   PIN_B4
#define LCD_DB5   PIN_B5
#define LCD_DB6   PIN_B6
#define LCD_DB7   PIN_B7
#define LCD_RS    PIN_B2
#define LCD_E     PIN_B3
#include <lcd1.c>

int1 BITS[32];
int16 Ta,Tb,TICK;
int8 n;
Int1 BIT_START, NUEVO_DATO;
int BYTE_IR[4];
int8 BIT_BYTE(int1 D7, int1 D6, int1 D5, int1 D4, int1 D3, int1 D2, int1 D1, int1 D0);

#int_EXT
void RB0(){
   Ta=get_timer1();
   TICK = Ta-tb;
   Tb = Ta;
 
   if(BIT_START==1){
       //BIT 1
       if(TICK>1350 && TICK < 1450)  // 1406.25
      {
         BITS[n]=1; n++;
      }
      //BIT 0
       if(TICK>650 && TICK < 750)  // TICK=700
      {
         BITS[n]=0; n++;
      }
      if(n==32){
         NUEVO_DATO=1;
      }
   
   }
   //BIT STAR
   if(TICK>8200 && TICK < 8600)  //8437 
   {
      BIT_START=1;
      n=0;
   }
}


void main(){
   
   lcd_init();
   SETUP_TIMER_1(T1_INTERNAL|T1_DIV_BY_8);
   EXT_INT_EDGE(0,H_TO_L); //
   enable_interrupts(INT_EXT);
   enable_interrupts(GLOBAL);
   lcd_gotoxy(1,1);
   lcd_putc(" Control IRr");
   
   while(TRUE){
     
      if(NUEVO_DATO==1){
         NUEVO_DATO=0;
         BYTE_IR[0]=BIT_BYTE(BITS[0],BITS[1],BITS[2],BITS[3],BITS[4],BITS[5],BITS[6],BITS[7]);
         BYTE_IR[1]=BIT_BYTE(BITS[8],BITS[9],BITS[10],BITS[11],BITS[12],BITS[13],BITS[14],BITS[15]);
         BYTE_IR[2]=BIT_BYTE(BITS[16],BITS[17],BITS[18],BITS[19],BITS[20],BITS[21],BITS[22],BITS[23]);
         BYTE_IR[3]=BIT_BYTE(BITS[24],BITS[25],BITS[26],BITS[27],BITS[28],BITS[29],BITS[30],BITS[31]);
         lcd_gotoxy(1,1);
         printf(lcd_putc,"B0=%X   B1=%X",BYTE_IR[0],BYTE_IR[1]);
         lcd_gotoxy(1,2);
         printf(lcd_putc,"B2=%X   B3=%X",BYTE_IR[2],BYTE_IR[3]);   
         //if(BYTE_IR[2]==0x45) output_d(0xFF);
         //if(BYTE_IR[2]==0x46) output_d(0x00);
      }
   }
}

int8 BIT_BYTE(int1 D0, int1 D1, int1 D2, int1 D3, int1 D4, int1 D5, int1 D6, int1 D7){
   int8 dato;
   dato= D7*128 + D6*64 + D5*32 + D4*16 + D3*8 + D2*4 + D1*2 +D0*1; // 0 al 255
   return dato;
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 18, 2015 5:53 am     Reply with quote

Code:
#include <18f4550.h>
#fuses HS
#use delay(clock=20Mhz)
#define LCD_DB4   PIN_B4
#define LCD_DB5   PIN_B5
#define LCD_DB6   PIN_B6
#define LCD_DB7   PIN_B7
#define LCD_RS    PIN_B2
#define LCD_E     PIN_B3

You are missing CPUDIV1 again. The compiler defaults to using CPUDIV4.
So your PIC is really running at 5 MHz, not 20 MHz as you think.
Attention to detail is one of the most important things in electronics.
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