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

PIC16f18324 I2C, INT_RDA not WORKING!
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
eeeahmet



Joined: 06 Jul 2013
Posts: 18
Location: Turkey

View user's profile Send private message

PIC16f18324 I2C, INT_RDA not WORKING!
PostPosted: Mon Sep 05, 2016 5:43 pm     Reply with quote

Hi,

I am using pcwhd v5.059 with 16f18324 in my project.
Software i2c not working. Also int_rda never fire.

I used 16f1824 in this project last time and worked very well. But this microcontroller not working. Sad

Please help me to solve these problems.

edit:(Adc problem solved)

main.c
Code:

#include <16F18324.h>
#device *=16 ADC=10
#device WRITE_EEPROM = NOINT
//#device icd=true

#fuses MCLR, NOCLKOUT, PUT, BROWNOUT, FCMEN
#fuses LPBOR, BORV27, NOLVP
#fuses NOEXTOSC, RSTOSC_HFINTRC_PLL, WDT
#fuses NOPPS1WAY
#fuses NODEBUG

#use delay(internal = 16M, restart_wdt)
//#pin_select U1TX=PIN_C4
//#pin_select U1RX=PIN_C5

#use rs232(baud=9600, XMIT=pin_c4, RCV=pin_c5, bits=8, stop=1, parity=N, ERRORS)
#use i2c(Master, fast, sda=PIN_C2, scl=PIN_C1, force_sw)

#zero_ram

#include <__hfiles\variables.h>
#include <__hfiles\isr.h>

#INT_RDA
void RDA_isr(){ k_line(); }//////////////////////////////////////////////////////

#INT_TIMER2
void TIMER2_isr()
{
   if(interrupt_active(int_rda))
      return;                 
   read_adc(adc_start_only);                                                   //tamamland?
   counters(counters_enable);                                                  //tamamland?
   Tank_value=read_adc(adc_read_only);                                         //tamamland?
}

#INT_EXT
void EXT_L2H_isr(){return;}///////////////////////////////////////////////////////////


static void main(void)
{
   cpu_setup();                           
   first_conditions();                         
   while(true)                                     
   {
      restart_wdt();                //tamamland?
      load_datas(load_datas_enable, eepromx, restart, delete);                 //tamamland?
      setup_datas(setup_datas_enable, eepromx);                                //tamamland?
      inputs(inputs_enable);                                                   //tamamland?
      conditions(conditions_enable);                                           //tamamland?
      while(wait==true)
         delay_us(100);
      switch_state(refresh[0], refresh[2], mode_flt);                          //tamamland?
      dac_bit_calc(dac_bit_calc_enable);                                       //tamamland?
      dacx_write(dacx_write_enable, dac_bit_calc_enable);                      //tamamland?
      setup_send_data(setup_send_data_enable);           
      //algoritma_cont(algoritmastart[2]);                                       //
      //quick_kline(quick_kline_enable);                                        //tamamland?
   }
}

void Cpu_Setup()////////////////////////////////////////////////////////////////
{
   if(read_eeprom(29)=='N')
      output_a(0b00101101);
   else
      output_a(0b00001101);
   //output_a(0b00101101);
   set_tris_a(0b00000101);
   set_tris_c(0b00101001);
   
   
   setup_timer_0(t0_internal);
   setup_timer_1(t1_disabled);
   setup_timer_3(t3_disabled);
   setup_timer_4(t4_disabled,0,1);
   setup_timer_5(t5_disabled);
   setup_timer_6(t6_disabled,0,1);
   setup_ccp1(ccp_off);
   setup_pwm5(pwm_disabled);
   setup_pwm6(pwm_disabled);
   setup_spi(spi_disabled);
   setup_comparator(nc_nc_nc_nc);
   setup_vref(vref_off);
   setup_dac(dac_off);
   setup_cwg(cwg_disabled,0,0,0);
   setup_cwg2(cwg_disabled,0,0,0);
   setup_nco(nco_disabled,0);
   setup_clc1(clc_disabled);
   setup_clc2(clc_disabled);
   setup_clc3(clc_disabled);
   setup_clc4(clc_disabled);
   setup_dsm(dsm_disabled);
   
   setup_adc(adc_clock_internal);
   setup_adc_ports(sAN0 | VSS_VDD);
   setup_wdt(wdt_256ms);
   setup_timer_2(T2_DIV_BY_64,155,4);
   enable_interrupts(INT_TIMER2);
   enable_interrupts(INT_RDA);
   enable_interrupts(INT_EXT);
}

void first_conditions()/////////////////////////////////////////////////////////
{
   set_adc_channel(0);
   TXEN=0;
   OUTPUT_LOW(TX);
   ilkkontak[2]=true;
   lpg[0]=true;
   benzin[0]=true;
   mode_flt='f';
   refresh[2]=true;
   //algoritma_calc_enable=true;
   counters_enable=true;
   load_datas_enable=true;
   inputs_enable=true;
   conditions_enable=true;
   setup_send_data_enable=true;
   wait=false;
   enable_interrupts(GLOBAL);
}

void load_datas(aktif, durum1, durum2, durum3)//////////////////////////////////
{
   if(!aktif)
      return;
   static int ej;
   disable_interrupts(global);
   if(durum3)
   {
      for(ej=0;ej<72;++ej)
            Lpg_Current_Controll_Data[ej]=0xFF;
   }
   for(ej=0;ej<72;++ej)
   {
      if(durum1)
         write_eeprom(ej,Lpg_Current_Controll_Data[ej]);
      else
         Lpg_Current_Controll_Data[ej]=read_eeprom(ej);
   }
   if(durum2)
   {
      reset_cpu();
   }
   enable_interrupts(global);
   load_datas_enable=false;
   setup_datas_enable=true;
}

void setup_datas(aktif, durum1)////////////////////////////////////////////////
{
   if(!aktif)
      return;
   if(durum1==read && Lpg_Current_Controll_Data[0]!='F')
   {
       Lpg_Current_Controll_Data[0] = 'E';                         //data dolu 'F'
       Lpg_Current_Controll_Data[1] = 0;                           //Min. Dac Bit yüksek 8 biti
       Lpg_Current_Controll_Data[2] = 0;                           //Min. Dac Bit dü?ük 8 biti
       Lpg_Current_Controll_Data[3] = 15;                          //Max. Dac Bit yüksek 8 biti
       Lpg_Current_Controll_Data[4] = 255;                         //Max. Dac Bit dü?ük 8 biti
       Lpg_Current_Controll_Data[5] = 0;                           //%20 Faktörü
       Lpg_Current_Controll_Data[6] = 50;                          //Geçi? süresi
       Lpg_Current_Controll_Data[7] = 0;                           //L0 H 8 bit
       Lpg_Current_Controll_Data[8] = 20;                          //L0 L 8 bit
       Lpg_Current_Controll_Data[9] = 0;                           //L20 H 8 bit
       Lpg_Current_Controll_Data[10] = 57;                         //L20 L 8 bit
       Lpg_Current_Controll_Data[11] = 0;                          //L21 H 8 bit
       Lpg_Current_Controll_Data[12] = 58;                         //L21 L 8 bit
       Lpg_Current_Controll_Data[13] = 0;                          //L40 H 8 bit
       Lpg_Current_Controll_Data[14] = 95;                         //L40 L 8 bit
       Lpg_Current_Controll_Data[15] = 0;                          //L41 H 8 bit
       Lpg_Current_Controll_Data[16] = 96;                         //L41 L 8 bit
       Lpg_Current_Controll_Data[17] = 0;                          //L60 H 8 bit
       Lpg_Current_Controll_Data[18] = 131;                        //L60 L 8 bit
       Lpg_Current_Controll_Data[19] = 0;                          //L61 H 8 bit
       Lpg_Current_Controll_Data[20] = 132;                        //L61 L 8 bit
       Lpg_Current_Controll_Data[21] = 0;                          //L80 H 8 bit
       Lpg_Current_Controll_Data[22] = 168;                        //L80 L 8 bit
       Lpg_Current_Controll_Data[23] = 0;                          //L81 H 8 bit
       Lpg_Current_Controll_Data[24] = 169;                        //L81 L 8 bit
       Lpg_Current_Controll_Data[25] = 0;                          //L100 H 8 bit
       Lpg_Current_Controll_Data[26] = 206;                        //L100 L 8 bit
       Lpg_Current_Controll_Data[27] = 'A';                        //Algoritma Aktif - Pasif               
       Lpg_Current_Controll_Data[28] = 'E';                        //Araç Yak?t Sistemi
       Lpg_Current_Controll_Data[29] = 'N';                        //BRC Tank Modeli Normal - LDI
   }             
   Bdac_min=(unsigned int16)Lpg_Current_Controll_Data[1]<<8 | Lpg_Current_Controll_Data[2];
   Bdac_max=(unsigned int16)Lpg_Current_Controll_Data[3]<<8 | Lpg_Current_Controll_Data[4];     
   refresh_time=Lpg_Current_Controll_Data[6];
   L0=(unsigned int16)Lpg_Current_Controll_Data[7]<<8 | Lpg_Current_Controll_Data[8];
   L20=(unsigned int16)Lpg_Current_Controll_Data[9]<<8 | Lpg_Current_Controll_Data[10];
   L21=(unsigned int16)Lpg_Current_Controll_Data[11]<<8 | Lpg_Current_Controll_Data[12];
   L40=(unsigned int16)Lpg_Current_Controll_Data[13]<<8 | Lpg_Current_Controll_Data[14];
   L41=(unsigned int16)Lpg_Current_Controll_Data[15]<<8 | Lpg_Current_Controll_Data[16];
   L60=(unsigned int16)Lpg_Current_Controll_Data[17]<<8 | Lpg_Current_Controll_Data[18];
   L61=(unsigned int16)Lpg_Current_Controll_Data[19]<<8 | Lpg_Current_Controll_Data[20];
   L80=(unsigned int16)Lpg_Current_Controll_Data[21]<<8 | Lpg_Current_Controll_Data[22];
   L81=(unsigned int16)Lpg_Current_Controll_Data[23]<<8 | Lpg_Current_Controll_Data[24];
   L100=(unsigned int16)Lpg_Current_Controll_Data[25]<<8 | Lpg_Current_Controll_Data[26];
   eepromx=write;
   setup_datas_enable=false;
}

void inputs(aktif)/////////////////////////////////////////////////////
{
   if(!aktif)
      return;
   if(kontak_input)
      kontak=true;
   else
      kontak=false;
   if(lb_input)
   {
      if(lpg[0])
      {
         lpg[0]=false;
         lpg[1]=true;
         benzin[0]=true;
      }
   }
   else
   {
      if(benzin[0])
      {
         benzin[0]=false;
         benzin[1]=true;
         lpg[0]=true;
      }
   }
}

void conditions(aktif)
{
   if(!aktif)
      return;
   switch(inputx_state)
   {
      case hardware:
      {
         if(kontak)//////////////////////////////////////////////////////////
         {
            bit_set(send_data[3],0);
            if(Lpg_Current_Controll_Data[0]=='F')
            {
               //algoritma_calc_enable=true;
               if(sleepx[0])
                  ilkkontak[0]=true;
               sleepx[0]=false;
               sleepx[2]=false;
               counter_sleepx=0;
               if(ilkkontak[2])
               {
                  if(benzin[1])/////////////////////////////////////////////////
                  {
                     if(mode_flt!='f')
                     {
                        mode_flt='f';
                        refresh[0]=true;
                     }
                     else
                        refresh[2]=true;
                     bit_clear(send_data[3],1);
                     benzin[1]=false;
                  }
                  if(lpg[1])////////////////////////////////////////////////////
                  {
                     if(mode_flt!='l')
                     {
                        mode_flt='l';
                        refresh[0]=true;
                     }
                     else
                        refresh[2]=true;
                     bit_set(send_data[3],1);
                     lpg[1]=false;
                  }
               }
            }
            else
            {
               bit_clear(send_data[3],1);
               //algoritma_calc_enable=false;
               mode_flt='f';
               refresh[0]=false;
               refresh[2]=true;
               benzin[0]=true;
               benzin[1]=false;
               lpg[0]=true;
               lpg[1]=false;
            }
         }
         else
         {
            bit_clear(send_data[3],0);
            //algoritma_calc_enable=false;
            refresh[2]=false;
            if(!sleepx[0])
            {
               sleepx[0]=true;
               counter_sleepx=0;
            }
           
            if(Lpg_Current_Controll_Data[29]=='N' && Lpg_Current_Controll_Data[28]=='E')
            {
               mode_flt='f';
               refresh[2]=true;
            }
            else if(refresh[0])
               refresh[2]=true;
               
            ilkkontak[2]=false;
            refresh[0]=false;
            benzin[0]=true;
            benzin[1]=false;
            lpg[0]=true;
            lpg[1]=false;
             
            if(sleepx[2])// || Lpg_Current_Controll_Data[0]=='E')
            {
               disable_interrupts(int_rda);
               disable_interrupts(int_timer2);
               restart_wdt();
               SETUP_WDT(WDT_OFF);
               sleep();
               SETUP_WDT(WDT_256MS);
               sleepx[0]=false;
               sleepx[2]=false;
               enable_interrupts(int_timer2);
               enable_interrupts(int_rda);
               ilkkontak[0]=true;
            }
            if(mode_flt=='l')
            {
               wait=true;
               counter_wait=0;
            }
         }
      }
      break;
     
      case software:
      {
         if(kontak)//////////////////////////////////////////////////////////
         {
            benzin[0]=true;
            benzin[1]=false;
            lpg[0]=true;
            lpg[1]=false;
            bit_set(send_data[3],0);
            V_Dac_Bit=0;
            if(mode_flt=='t')
               V_Dac_Bit=make16(dac_bit[0],dac_bit[1]);
            if(kline[2])
            {
               kline[2]=false;
               inputx_state=hardware;
            }
         }
         else////////////////////////////////////////////////////////////////
            inputx_state=hardware;
      }
      break;
   }
}

void switch_state(durum1, durum2, durum3)////////////////////////////////////////
{
   if(durum1 || durum2)
   {
      if(durum1)
         durum3='e';
      if(durum2)
         refresh[2]=0;
      if(durum3!='l')
      {
         i2c_start();
         i2c_write(0b11000000);
         i2c_write(0b01000000);
         i2c_write(0);
         i2c_write(0);
         i2c_stop();
      }
      switch(durum3)
      {
         case 'e':
            output_low(in1);
            output_low(in2);
            dac_bit_calc_enable=false;
            dacx_write_enable=false;
         break;
         
         case 'f':
            output_low(in2);
            output_high(in1);
            dac_bit_calc_enable=false;
            dacx_write_enable=false;
         break;
         
         case 'l':
            output_low(in1);
            output_high(in2);
            dac_bit_calc_enable=true;
            counter_ripple=0;
            ripple[0]=false;
            ripple[2]=true;
         break;
         
         case 't':
            output_low(in1);
            output_high(in2);
            dac_bit_calc_enable=false;
            dacx_write_enable=true;
         break;
      }
   }
}

void dac_bit_calc(aktif)//////////////////////////////////////////////
{
   if(!aktif)
      return;
   float inducator_binde;
   if(Tank_value<L0 || Tank_value==L0)
   {
      inducator_binde=0;
   }
   else
   {
      if(Tank_value<L20 || Tank_value==L20)
         inducator_binde=((Tank_value-L0)*20)/(L20-L0);
      else if(Tank_value<L40 || Tank_value==L40)
         inducator_binde=20+(((Tank_value-L21)*20)/(L40-L21));
      else if(Tank_value<L60 || Tank_value==L60)
         inducator_binde=40+(((Tank_value-L41)*20)/(L60-L41));
      else if(Tank_value<L80 || Tank_value==L80)
         inducator_binde=60+(((Tank_value-L61)*20)/(L80-L61));
      else if(Tank_value<L100 || Tank_value==L100)
         inducator_binde=80+(((Tank_value-L81)*20)/(L100-L81));
      else
         inducator_binde=100;
   }
   if(Lpg_Current_Controll_Data[5]>inducator_binde && f20[0]) 
   {
      V_Dac_Bit=(unsigned int16)( ((float)(Bdac_max-Bdac_min)*Lpg_Current_Controll_Data[5]) / 100 ) + Bdac_min;
      dacx_write_enable=true;
   }
   else//else if(ripple[2])
   {
      V_Dac_Bit=(unsigned int16)( ((float)(Bdac_max-Bdac_min)*inducator_binde) / 100 ) + Bdac_min;
      ripple[2]=false;
      ripple[0]=true;
      dacx_write_enable=true;
   }
}

void dacx_write(aktif, durum1)///////////////////////////////////////////////////////////
{
   if(!aktif)
      return;
   i2c_start();
   i2c_write(0b11000000);
   i2c_write(0b01000000);
   i2c_write((unsigned int8)((V_Dac_Bit>>4)&0x00ff));
   i2c_write((unsigned int8)((V_Dac_Bit<<4)&0x00f0));
   i2c_stop();
   if(durum1)
      dacx_write_enable=false;
}

void setup_send_data(aktif)/////////////////////////////////////////////////////
{
   if(!aktif)
      return;
   Send_Data[0]=(unsigned int8)(Tank_value>>8)&0xff;
   Send_Data[1]=(unsigned int8)Tank_value&0xff;
   Send_Data[2]=mode_flt;
}


variables.h
Code:


/*DEĞİŞKENLERİ TANIMLA*/

#bit TXEN = 0x19E.5
#define lb_input input(pin_c0)
#define kontak_input input(pin_a2)
#define select pin_c3
#define Tx pin_c4
#define in1 pin_a5
#define in2 pin_a4
#define read 0
#define write 1
#define hardware 0
#define software 1
#define sleepx_time 600          //10dk = 10*60sn 
#define ilkkontak_time 12        //sn
#define f20_time 35              //sn
#define kline_time 1             //sn
#define ripple_time 35           //sn
//#define algoritmastart_time 500  //5s = 10ms*500

int1 eepromx=read;
int1 load_datas_enable;
int1 setup_datas_enable;
int1 conditions_enable;
int1 dac_bit_calc_enable;
int1 dacx_write_enable;
int1 setup_send_data_enable;
//int1 algoritma_calc_enable;
//int1 algoritma_enable;
//int1 quick_kline_enable;
int1 inputx_state;
int1 restart; 
int1 delete;
int1 kontak;
int1 inputs_enable;
int1 counters_enable;
int1 lpg[2];
int1 benzin[2];
int1 refresh[3];
int1 sleepx[3];
int1 ilkkontak[3];
int1 f20[3];
int1 kline[3];
int1 ripple[3];
//int1 algoritmastart[3];
//int1 algoritma[2];
//int1 algoritmafinish[2];
int1 wait=true;
 
unsigned int8 refresh_time;       
unsigned int8 counter_refresh;
unsigned int8 counter_ilkkontak;
unsigned int8 counter_f20;   
unsigned int8 counter_kline;
unsigned int8 counter_ripple;
unsigned int8 mode_flt;     
unsigned int8 sn;             
unsigned int8 Null_Data=0x55;             
unsigned int8 Dac_Bit[2];
unsigned int8 Send_Data[4];
unsigned int8 Lpg_Current_Controll_Data[72];
unsigned int8 counter_wait;

unsigned int16 V_Dac_Bit;     
unsigned int16 Bdac_max,Bdac_min;       
unsigned int16 L0,L20,L21,L40,L41,L60,L61,L80,L81,L100;
unsigned int16 Tank_value;
unsigned int16 counter_sleepx;
//unsigned int16 counter_algoritmastart;
//unsigned int16 counter_algoritma;
//unsigned int16 algoritmastart_value;
//unsigned int16 algoritmafinish_time;



//#separate
//ISR İÇİNDEKİLER
void k_line();
void counters(int1);

//MAIN İÇİNDEKİLER
void Cpu_Setup();
void first_conditions();

//WHILE(TRUE) İÇİNDEKİLER
void load_datas(int1,int1,int1,int1);
void setup_datas(int1,int1);
void inputs(int1);
void conditions(int1);
void switch_state(int1,int1,char);
void dac_bit_calc(int1);
void dacx_write(int1,int1);
void setup_send_data(int1);
//void algoritma_calc(int1);
//void algoritma_cont(int1);
//void quick_kline(int1);



isr.h
Code:

void k_line()
{   
   volatile unsigned char K_Line_Commands[3];
   volatile int j=0;
   volatile int xt=0;
   volatile short KSend=0;
   disable_interrupts(int_rda);
   for(j=0;j<3;++j)
   {     K_Line_Commands[j]=getc();   }
   
   kline[0]=true;
   kline[2]=false;
   counter_kline=0;
   if(K_Line_Commands[0]=='C')
   {
      switch(K_Line_Commands[2])
      {
         case '1':
         {
            //veri oku komutu                      72 byte
            if(K_Line_Commands[1]=='r')
            {
               output_high(select);
               delay_us(500);
               delay_us(500);
               delay_us(500);
               delay_us(500);
               //disable_interrupts(int_timer2);
               for(j=0;j<72;++j)
               {
                  output_high(TX);
                  delay_us(104);
                  for(xt=0;xt<8;++xt)
                  {
                     if(bit_test(Lpg_Current_Controll_Data[j],xt))
                        output_low(TX);
                     else
                        output_high(TX);
                     delay_us(92);
                  }
                  output_low(TX);
                  delay_us(104);
               }
               //enable_interrupts(int_timer2);
               output_FLOAT(select);
            }
            else if(K_Line_Commands[1]=='m')
            {
               output_high(select);
               delay_us(500);
               delay_us(500);
               delay_us(500);
               delay_us(500);
               //disable_interrupts(int_timer2);
               for(j=0;j<4;++j)
               {
                  output_high(TX);
                  delay_us(104);
                  for(xt=0;xt<8;++xt)
                  {
                     if(bit_test(Send_Data[j],xt))
                        output_low(TX);
                     else
                        output_high(TX);
                     delay_us(92);
                  }
                  output_low(TX);
                  delay_us(104);
               }
               //enable_interrupts(int_timer2);
               output_FLOAT(select);
            }
         }
         break;
         
         case '2':
         {
            //Verileri Yükle                        72 byte --> 0x55
            if(K_Line_Commands[1]=='w')
            {
               for(j=0;j<72;++j)
                  Lpg_Current_Controll_Data[j]=getc();
               Lpg_Current_Controll_Data[0]='F';
               load_datas_enable=true;
               restart=true;
            }
            //verileri sil komutu                   
            else if(K_Line_Commands[1]=='d')
            {
               load_datas_enable=true;
               delete=true;
               restart=true;
            }
            //geçiş süresi yaz                      1 byte
            else if(K_Line_Commands[1]=='g')
            {
               Lpg_Current_Controll_Data[6]=getc();
               load_datas_enable=true;
            }
            //test modunda dac veri yaz             2 byte
            else if(K_Line_Commands[1]=='c')
            {
               for(j=0;j<2;++j)
                  Dac_Bit[j]=getc();
            }
            //lpg modunda lpg max-min veri yaz      20 byte
            else //if(K_Line_Commands[1]=='a')
            {
               for(j=0;j<23;++j)
                  Lpg_Current_Controll_Data[7+j]=getc();
               load_datas_enable=true;
            }
            KSend=1;
         }
         break;
         
         case '3':
         {
            if(kontak)
            {
               //Benzin Modu                         
               if(K_Line_Commands[1]=='f')
                  mode_flt='f';
               //Lpg Modu                             
               else if(K_Line_Commands[1]=='l')
                  mode_flt='l';
               //Test Modu                           
               else//if(K_Line_Commands[1]=='t')
                  mode_flt='t';
               inputx_state=software;
               refresh[0]=true;
            }
            KSend=1;
         }
         break;
      }
   }
   if(KSend)
   {
      KSend=0;
      output_high(select);
         delay_us(500);
         delay_us(500);
         delay_us(500);
         delay_us(500);
         //disable_interrupts(int_timer2);
         output_high(TX);
         delay_us(104);
         for(xt=0;xt<8;++xt)
         {
            if(bit_test(Null_Data,xt))
               output_low(TX);
            else
               output_high(TX);
            delay_us(92);
         }
         output_low(TX);
         //enable_interrupts(int_timer2);
      output_FLOAT(select);
   }
   enable_interrupts(int_rda);
}

void counters(aktif)///////////////////////////////////////////////////
{
   //refresh counter      1-255sn
   //sleep counter        600sn
   //first_kontak counter 15sn
   //F20 counter          35sn
   if(!aktif)
      return;
   if(sn)
      sn--;
   else
   {
      sn=99;
      if(refresh[0])////////////////////////////////////////////////////////////
      {
         if(counter_refresh==refresh_time)
         {
            refresh[0]=false;
            refresh[1]=true;
         }
         counter_refresh++;
      }
      if(refresh[1])
      {
         counter_refresh=0;
         refresh[1]=false;
         refresh[2]=true;
         f20[0]=true;///////////////////////////////////////////////////////////ekran tazelendikten hemen sonra f20 saymaya başlar
      }/////////////////////////////////////////////////////////////////////////
      if(f20[0])////////////////////////////////////////////////////////////////
      {
         if(counter_f20==f20_time)
         {
            f20[0]=false;
            f20[1]=true;
         }
         counter_f20++;
      }
      else if(f20[1])
      {
         counter_f20=0;
         f20[1]=false;
         //f20[2]=true;
      }/////////////////////////////////////////////////////////////////////////
      if(sleepx[0])/////////////////////////////////////////////////////////////
      {
         if(counter_sleepx==sleepx_time)
         {
            sleepx[0]=false;
            sleepx[1]=true;
         }
         counter_sleepx++;
      }
      if(sleepx[1])
      {
         counter_sleepx=0;
         sleepx[1]=false;
         sleepx[2]=true;
      }/////////////////////////////////////////////////////////////////////////
      if(ilkkontak[0])//////////////////////////////////////////////////////////
      {
         if(counter_ilkkontak==ilkkontak_time)
         {
            ilkkontak[0]=false;
            ilkkontak[1]=true;
         }
         counter_ilkkontak++;
      }
      if(ilkkontak[1])
      {
         counter_ilkkontak=0;
         ilkkontak[1]=false;
         ilkkontak[2]=true;
      }/////////////////////////////////////////////////////////////////////////
      if(kline[0])//////////////////////////////////////////////////////////////
      {
         if(counter_kline==kline_time)
         {
            kline[0]=false;
            kline[1]=true;
         }
         counter_kline++;
      }
      if(kline[1])
      {
         counter_kline=0;
         kline[1]=false;
         kline[2]=true;
      }/////////////////////////////////////////////////////////////////////////
      if(ripple[0])//////////////////////////////////////////////////////////////
      {
         if(counter_ripple==ripple_time)
         {
            ripple[0]=false;
            ripple[1]=true;
         }
         counter_ripple++;
      }
      if(ripple[1])
      {
         counter_ripple=0;
         ripple[1]=false;
         ripple[2]=true;
      }/////////////////////////////////////////////////////////////////////////
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Sep 05, 2016 7:31 pm     Reply with quote

Look at the .LST file. What type of code is generated for #use rs232() ?
Quote:
........... #use rs232(baud=9600, XMIT=pin_c4, RCV=pin_c5, bits=8, stop=1, parity=N, ERRORS)
*
0033: MOVLB 01
0034: BSF TRISC.5
0035: MOVLB 00
0036: BTFSC PORTC.5
0037: GOTO 036
0038: MOVLW 08
0039: MOVWF @77
003A: MOVLB 02
003B: CLRF @@30
003C: BSF @77.7
003D: GOTO 04E
003E: BCF @77.7
003F: GOTO 04E
0040: BCF STATUS.C
0041: MOVLB 00
....

It sets up a loop count for 8 bits. It's bit-banging code. It doesn't use the
hardware UART.

You have the #pin_select lines commented out:
Quote:
//#pin_select U1TX=PIN_C4
//#pin_select U1RX=PIN_C5

If you un-comment those lines and re-compile, then what do you get ?
Quote:
...... #use rs232(baud=9600, XMIT=pin_c4, RCV=pin_c5, bits=8, stop=1, parity=N, ERRORS)
*
0033: BTFSS PIR1.RCIF
0034: GOTO 033
0035: MOVLB 03
0036: MOVF RC1STA,W
0037: MOVLB 00
0038: MOVWF rs232_errors
0039: MOVLB 03
003A: MOVF RC1REG,W
003B: MOVWF @78
003C: MOVLB 00
003D: BTFSS rs232_errors.1
003E: GOTO 042
003F: MOVLB 03
0040: BCF RC1STA.CREN
0041: BSF RC1STA.CREN
0042: MOVLB 00
0043: RETURN

It talks to the hardware UART registers. It's using the hardware UART.
eeeahmet



Joined: 06 Jul 2013
Posts: 18
Location: Turkey

View user's profile Send private message

PostPosted: Mon Sep 05, 2016 7:46 pm     Reply with quote

Hi PCM programmer,

Thank you for your answer.

Iam looking at the .LST file and code like this
Code:

.................... #use rs232(baud=9600, XMIT=pin_c4, RCV=pin_c5, bits=8, stop=1, parity=N, ERRORS)
*
0033:  MOVLB  01
0034:  BSF    0E.5
0035:  MOVLB  00
0036:  BTFSC  0E.5
0037:  GOTO   036
0038:  MOVLW  08
0039:  MOVWF  77
003A:  MOVLB  02
003B:  CLRF   30
003C:  BSF    77.7
003D:  GOTO   04E
003E:  BCF    77.7
003F:  GOTO   04E
0040:  BCF    03.0
0041:  MOVLB  00
0042:  BTFSC  0E.5
0043:  BSF    03.0
0044:  MOVLB  02
0045:  RRF    30,F
0046:  BSF    77.6
0047:  GOTO   04E
0048:  BCF    77.6
0049:  DECFSZ 77,F
004A:  GOTO   040
004B:  MOVF   30,W
004C:  MOVWF  78
004D:  GOTO   05A
004E:  MOVLW  84
004F:  BTFSC  77.7
0050:  MOVLW  24
0051:  MOVWF  78
0052:  DECFSZ 78,F
0053:  GOTO   052
0054:  GOTO   055
0055:  BTFSC  77.7
0056:  GOTO   03E
0057:  BTFSC  77.6
0058:  GOTO   048
0059:  GOTO   040
005A:  MOVLB  00


After that un-comment #pin_select lines and regenerated code like this
Code:

.................... #pin_select U1TX=PIN_C4 
.................... #pin_select U1RX=PIN_C5
.................... 
.................... #use rs232(baud=9600, XMIT=pin_c4, RCV=pin_c5, bits=8, stop=1, parity=N, ERRORS)
*
0033:  BTFSS  11.5
0034:  GOTO   033
0035:  MOVLB  03
0036:  MOVF   1D,W
0037:  MOVLB  00
0038:  MOVWF  24
0039:  MOVLB  03
003A:  MOVF   19,W
003B:  MOVWF  78
003C:  MOVLB  00
003D:  BTFSS  24.1
003E:  GOTO   042
003F:  MOVLB  03
0040:  BCF    1D.4
0041:  BSF    1D.4
0042:  MOVLB  00
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Sep 05, 2016 7:51 pm     Reply with quote

Put the .LST file in Symbolic mode. It's in the compiler settings window.
eeeahmet



Joined: 06 Jul 2013
Posts: 18
Location: Turkey

View user's profile Send private message

PostPosted: Mon Sep 05, 2016 8:01 pm     Reply with quote

Thanks. I dont know that and still learning Smile


I have comment #pin_select code lines
Code:

.................... //#pin_select U1TX=PIN_C4 
.................... //#pin_select U1RX=PIN_C5
.................... 
.................... #use rs232(baud=9600, XMIT=pin_c4, RCV=pin_c5, bits=8, stop=1, parity=N, ERRORS)
*
0033:  MOVLB  01
0034:  BSF    TRISC.5
0035:  MOVLB  00
0036:  BTFSC  PORTC.5
0037:  GOTO   036
0038:  MOVLW  08
0039:  MOVWF  @77
003A:  MOVLB  02
003B:  CLRF   @@30
003C:  BSF    @77.7
003D:  GOTO   04E
003E:  BCF    @77.7
003F:  GOTO   04E
0040:  BCF    STATUS.C
0041:  MOVLB  00
0042:  BTFSC  PORTC.5
0043:  BSF    STATUS.C
0044:  MOVLB  02
0045:  RRF    @@30,F
0046:  BSF    @77.6
0047:  GOTO   04E
0048:  BCF    @77.6
0049:  DECFSZ @77,F
004A:  GOTO   040
004B:  MOVF   @@30,W
004C:  MOVWF  @78
004D:  GOTO   05A
004E:  MOVLW  84
004F:  BTFSC  @77.7
0050:  MOVLW  24
0051:  MOVWF  @78
0052:  DECFSZ @78,F
0053:  GOTO   052
0054:  GOTO   055
0055:  BTFSC  @77.7
0056:  GOTO   03E
0057:  BTFSC  @77.6
0058:  GOTO   048
0059:  GOTO   040
005A:  MOVLB  00


and un-commented code
Code:

.................... #pin_select U1TX=PIN_C4 
.................... #pin_select U1RX=PIN_C5
.................... 
.................... #use rs232(baud=9600, XMIT=pin_c4, RCV=pin_c5, bits=8, stop=1, parity=N, ERRORS)
*
0033:  BTFSS  PIR1.RCIF
0034:  GOTO   033
0035:  MOVLB  03
0036:  MOVF   RC1STA,W
0037:  MOVLB  00
0038:  MOVWF  rs232_errors
0039:  MOVLB  03
003A:  MOVF   RC1REG,W
003B:  MOVWF  @78
003C:  MOVLB  00
003D:  BTFSS  rs232_errors.1
003E:  GOTO   042
003F:  MOVLB  03
0040:  BCF    RC1STA.CREN
0041:  BSF    RC1STA.CREN
0042:  MOVLB  00
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Tue Sep 06, 2016 12:35 am     Reply with quote

Don't use the pin numbers in the #use.

The way to use #pin_select, is this:
Code:

   
#pin_select U1TX=PIN_C4
#pin_select U1RX=PIN_C5
#use rs232(baud=9600, UART1, bits=8, stop=1, parity=N, ERRORS)


This selects the pins you require, and then tells the #use to use the hardware UART.

Using the pin numbers, 'sometimes works', but on most chips and pin combinations, doesn't. The compiler doesn't realise that the pins have been 'selected' to the hardware, so sets up a software UART instead.....

I've posted this way of working here many times. It really ought to be a 'sticky', or in the manual, but it isn't. I worked it out when #pin_select first appeared, and it has worked ever since.
Very Happy


Last edited by Ttelmah on Tue Sep 06, 2016 1:20 am; edited 1 time in total
eeeahmet



Joined: 06 Jul 2013
Posts: 18
Location: Turkey

View user's profile Send private message

PostPosted: Tue Sep 06, 2016 1:15 am     Reply with quote

Hi Ttelmah,

Thanks for your answer.

I changed that code line like your post.

But still doesn't work :( INT_RDA interrupt doesn't fire on pcb or pickit3 debug.
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Tue Sep 06, 2016 1:32 am     Reply with quote

The first thing to do, is go after one problem at a time.
First look at this thread:
<http://www.ccsinfo.com/forum/viewtopic.php?t=55456>
See how my 'test' program, posted Mon Sep 05, 2016 4:12 am, _just_ tests the problem being talked about. Nothing else.
Do the same with the serial.
Then once this is working, try the I2C.

'Think again' about how you are using the interrupt. INT_RDA, says that _one_ character is waiting. Just one. In general interrupt handlers should just handle the hardware task that is being signalled. So in this case, just receive one character. Your handler breaks hundreds of 'rules of thumb' about how to use interrupts. You are waiting for the serial, so the main code will completely stop working once it triggers. Then you have delays in the interrupt handler, so interrupts will be disabled in delays in the external code (which affects things like I2C)....

Look at ex_sisr.c This shows how a serial interrupt ought to be handled. Just retrieve the character, store it in a buffer, flag that data is available, and exit. Your main code, wants to be in the main. If you want to stop and do something triggered by the interrupt, then set a flag, and have the main code respond to this.

Then there are so many other problems.

For instance, you start the ADC, then immediately read it, without allowing the conversion to complete. If you are using a tick, the trick is to read the ADC at the start of the tick, and then start it. By the time you come 'back' (on the second tick), the ADC has then completed it's read, and has a value waiting. In future then the ADC carries out it's conversion in the time between ticks.

My guess is that INT_RDA is firing, and then the code is doing a watchdog restart, while waiting for characters to arrive.
eeeahmet



Joined: 06 Jul 2013
Posts: 18
Location: Turkey

View user's profile Send private message

PostPosted: Tue Sep 06, 2016 4:34 am     Reply with quote

Hi Ttelmah,

I used the code you wrote before you
<https://www.ccsinfo.com/forum/viewtopic.php?t=55295&highlight=16f18325>

and its works. Rda fired one time not many. When i have comment printf line the code doesn't work.

Computer still sending data every 100ms but rda not fire.

Code:

#include <16F18324.h>
#fuses MCLR, NOCLKOUT, PUT, BROWNOUT
#fuses LPBOR, BORV27, NOLVP
#fuses NOEXTOSC, RSTOSC_HFINTRC_PLL, NOWDT
#fuses NOPPS1WAY

#use delay(internal = 8M)
#pin_select U1TX=PIN_C4
#pin_select U1RX=PIN_C5

#use RS232(UART1, baud=9600, bits=8, stop=1, parity=N)
#zero_ram


#INT_RDA
void uart_rx_isr(void)
{
   char c;
   c = getc();
}

void main(void)
{
   set_tris_c(0b00101000);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   while(1)
  {
     delay_ms(1000);
     printf("hello\r");
  }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Tue Sep 06, 2016 4:47 am     Reply with quote

Are you saying if you remove the printf line, it does nothing?. You do realise that this is all the code will do, without the printf. There will be no sign at all that the code is working.
temtronic



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

View user's profile Send private message

PostPosted: Tue Sep 06, 2016 5:13 am     Reply with quote

also...
unless you use 'use_fast_io(xx);
this..

set_tris_c(0b00101000);

really isn't needed as the compiler is smart enough to handle the icorrect setings for the I/O pins.

however when you do use set_tris..... you should add a comment as to how the pins are configured.As written C3 and C5 are inputs and rest outputs but 'we' don't know if that's what you mean to do.

using fast _IO does create cosde that does run faster but in 99.9999% of applications presented here, is not necssary and in a lot of programs the coder makes a 1 a 0 so the 'input' pin has become an OUTPUT pin and naturally 'it doesn't work'....


Jay
eeeahmet



Joined: 06 Jul 2013
Posts: 18
Location: Turkey

View user's profile Send private message

PostPosted: Tue Sep 06, 2016 6:28 am     Reply with quote

First, Thank you very much for your answers.

Ok. I found my problem rs232 line and solved.

The problem is about one wire communication(k line). I use standart one wire usb serial port. I need to turn off the TX PIN when receiving data. Didn't have this problem when using 16f1824. When using the hardware UART tx pin, I could turn on and off.(TXEN=0; output_low(tx))

The hardware UART TX PIN when using this Processor I realized I didn't turn on and off.(TXEN=0; output_low(tx) dosnt work tx pin level is still high)

While designing a different pin(pin_c3) than I tied the RX pin. In this way, I made sure not to read the data I sent to PC.

Now that I use it as interrupt PIN(#INT_IOC enable_interrupts(INT_RC3_H2L)) when receiving data. Software UART Works perfect in my project.

here new code
Code:

#include <16F18324.h>
#device ADC=10
#device WRITE_EEPROM = NOINT
//#device icd=true

#fuses MCLR, NOCLKOUT, PUT, BROWNOUT, FCMEN
#fuses LPBOR, BORV27, NOLVP
#fuses NOEXTOSC, RSTOSC_HFINTRC_PLL, WDT
#fuses NOPPS1WAY
#fuses NODEBUG

#use delay(internal = 16M, restart_wdt)

//#pin_select U1TX=PIN_C4
//#pin_select U1RX=PIN_C5
#use rs232(baud=9600, XMIT=pin_c4, RCV=pin_c5, bits=8, stop=1, parity=N, ERRORS)
#use i2c(Master,slow, sda=PIN_C2, scl=PIN_C1, force_sw)

#zero_ram

#include <__hfiles\variables.h>
#include <__hfiles\isr.h>

#INT_IOC
void RDA_isr(){ k_line(); }//////////////////////////////////////////////////////

#INT_TIMER2
void TIMER2_isr()
{
   if(interrupt_active(INT_RC3_H2L))
      return;                 
   read_adc(adc_start_only);                                                   //tamamland?
   counters(counters_enable);                                                  //tamamland?
   Tank_value=read_adc(adc_read_only);                                         //tamamland?
}

#INT_EXT
void EXT_L2H_isr(){return;}///////////////////////////////////////////////////////////



static void main(void)
{
   cpu_setup();                           
   first_conditions();                         
   while(true)                                     
   {
      restart_wdt();                //tamamland?
      load_datas(load_datas_enable, eepromx, restart, delete);                 //tamamland?
      setup_datas(setup_datas_enable, eepromx);                                //tamamland?
      inputs(inputs_enable);                                                   //tamamland?
      conditions(conditions_enable);                                           //tamamland?
      while(wait==true)
         delay_us(100);
      switch_state(refresh[0], refresh[2], mode_flt);                          //tamamland?
      dac_bit_calc(dac_bit_calc_enable);                                       //tamamland?
      dacx_write(dacx_write_enable, dac_bit_calc_enable);                      //tamamland?
      setup_send_data(setup_send_data_enable);           
      //algoritma_cont(algoritmastart[2]);                                       //
      //quick_kline(quick_kline_enable);                                        //tamamland?
   }
}

void Cpu_Setup()////////////////////////////////////////////////////////////////
{
   if(read_eeprom(29)=='N')
      output_a(0b00101101);
   else
      output_a(0b00001101);
   //output_a(0b00101101);
   set_tris_a(0b00000101);
   set_tris_c(0b00101001);
   
   
   setup_timer_0(t0_internal);
   setup_timer_1(t1_disabled);
   setup_timer_3(t3_disabled);
   setup_timer_4(t4_disabled,0,1);
   setup_timer_5(t5_disabled);
   setup_timer_6(t6_disabled,0,1);
   setup_ccp1(ccp_off);
   setup_pwm5(pwm_disabled);
   setup_pwm6(pwm_disabled);
   setup_spi(spi_disabled);
   setup_comparator(nc_nc_nc_nc);
   setup_vref(vref_off);
   setup_dac(dac_off);
   setup_cwg(cwg_disabled,0,0,0);
   setup_cwg2(cwg_disabled,0,0,0);
   setup_nco(nco_disabled,0);
   setup_clc1(clc_disabled);
   setup_clc2(clc_disabled);
   setup_clc3(clc_disabled);
   setup_clc4(clc_disabled);
   setup_dsm(dsm_disabled);
   
   setup_adc(adc_clock_internal);
   setup_adc_ports(sAN0 | VSS_VDD);
   setup_wdt(wdt_2s);
   setup_timer_2(T2_DIV_BY_64,155,4);
   enable_interrupts(INT_TIMER2);
   enable_interrupts(INT_RC3_H2L);
   enable_interrupts(INT_EXT);
}

void first_conditions()/////////////////////////////////////////////////////////
{
   set_adc_channel(0);
   TXEN=0;
   OUTPUT_LOW(TX);
   ilkkontak[2]=true;
   lpg[0]=true;
   benzin[0]=true;
   mode_flt='f';
   refresh[2]=true;
   //algoritma_calc_enable=true;
   counters_enable=true;
   load_datas_enable=true;
   inputs_enable=true;
   conditions_enable=true;
   setup_send_data_enable=true;
   wait=false;
   enable_interrupts(GLOBAL);
}

void load_datas(aktif, durum1, durum2, durum3)//////////////////////////////////
{
   if(!aktif)
      return;
   static int ej;
   disable_interrupts(global);
   if(durum3)
   {
      for(ej=0;ej<72;++ej)
            Lpg_Current_Controll_Data[ej]=0xFF;
   }
   for(ej=0;ej<72;++ej)
   {
      if(durum1)
         write_eeprom(ej,Lpg_Current_Controll_Data[ej]);
      else
         Lpg_Current_Controll_Data[ej]=read_eeprom(ej);
   }
   if(durum2)
   {
      reset_cpu();
   }
   enable_interrupts(global);
   load_datas_enable=false;
   setup_datas_enable=true;
}

void setup_datas(aktif, durum1)////////////////////////////////////////////////
{
   if(!aktif)
      return;
   if(durum1==read && Lpg_Current_Controll_Data[0]!='F')
   {
       Lpg_Current_Controll_Data[0] = 'E';                         //data dolu 'F'
       Lpg_Current_Controll_Data[1] = 0;                           //Min. Dac Bit yüksek 8 biti
       Lpg_Current_Controll_Data[2] = 0;                           //Min. Dac Bit dü?ük 8 biti
       Lpg_Current_Controll_Data[3] = 15;                          //Max. Dac Bit yüksek 8 biti
       Lpg_Current_Controll_Data[4] = 255;                         //Max. Dac Bit dü?ük 8 biti
       Lpg_Current_Controll_Data[5] = 0;                           //%20 Faktörü
       Lpg_Current_Controll_Data[6] = 50;                          //Geçi? süresi
       Lpg_Current_Controll_Data[7] = 0;                           //L0 H 8 bit
       Lpg_Current_Controll_Data[8] = 20;                          //L0 L 8 bit
       Lpg_Current_Controll_Data[9] = 0;                           //L20 H 8 bit
       Lpg_Current_Controll_Data[10] = 57;                         //L20 L 8 bit
       Lpg_Current_Controll_Data[11] = 0;                          //L21 H 8 bit
       Lpg_Current_Controll_Data[12] = 58;                         //L21 L 8 bit
       Lpg_Current_Controll_Data[13] = 0;                          //L40 H 8 bit
       Lpg_Current_Controll_Data[14] = 95;                         //L40 L 8 bit
       Lpg_Current_Controll_Data[15] = 0;                          //L41 H 8 bit
       Lpg_Current_Controll_Data[16] = 96;                         //L41 L 8 bit
       Lpg_Current_Controll_Data[17] = 0;                          //L60 H 8 bit
       Lpg_Current_Controll_Data[18] = 131;                        //L60 L 8 bit
       Lpg_Current_Controll_Data[19] = 0;                          //L61 H 8 bit
       Lpg_Current_Controll_Data[20] = 132;                        //L61 L 8 bit
       Lpg_Current_Controll_Data[21] = 0;                          //L80 H 8 bit
       Lpg_Current_Controll_Data[22] = 168;                        //L80 L 8 bit
       Lpg_Current_Controll_Data[23] = 0;                          //L81 H 8 bit
       Lpg_Current_Controll_Data[24] = 169;                        //L81 L 8 bit
       Lpg_Current_Controll_Data[25] = 0;                          //L100 H 8 bit
       Lpg_Current_Controll_Data[26] = 206;                        //L100 L 8 bit
       Lpg_Current_Controll_Data[27] = 'A';                        //Algoritma Aktif - Pasif               
       Lpg_Current_Controll_Data[28] = 'E';                        //Araç Yak?t Sistemi
       Lpg_Current_Controll_Data[29] = 'N';                        //BRC Tank Modeli Normal - LDI
   }             
   Bdac_min=(unsigned int16)Lpg_Current_Controll_Data[1]<<8 | Lpg_Current_Controll_Data[2];
   Bdac_max=(unsigned int16)Lpg_Current_Controll_Data[3]<<8 | Lpg_Current_Controll_Data[4];     
   refresh_time=Lpg_Current_Controll_Data[6];
   L0=(unsigned int16)Lpg_Current_Controll_Data[7]<<8 | Lpg_Current_Controll_Data[8];
   L20=(unsigned int16)Lpg_Current_Controll_Data[9]<<8 | Lpg_Current_Controll_Data[10];
   L21=(unsigned int16)Lpg_Current_Controll_Data[11]<<8 | Lpg_Current_Controll_Data[12];
   L40=(unsigned int16)Lpg_Current_Controll_Data[13]<<8 | Lpg_Current_Controll_Data[14];
   L41=(unsigned int16)Lpg_Current_Controll_Data[15]<<8 | Lpg_Current_Controll_Data[16];
   L60=(unsigned int16)Lpg_Current_Controll_Data[17]<<8 | Lpg_Current_Controll_Data[18];
   L61=(unsigned int16)Lpg_Current_Controll_Data[19]<<8 | Lpg_Current_Controll_Data[20];
   L80=(unsigned int16)Lpg_Current_Controll_Data[21]<<8 | Lpg_Current_Controll_Data[22];
   L81=(unsigned int16)Lpg_Current_Controll_Data[23]<<8 | Lpg_Current_Controll_Data[24];
   L100=(unsigned int16)Lpg_Current_Controll_Data[25]<<8 | Lpg_Current_Controll_Data[26];
   eepromx=write;
   setup_datas_enable=false;
}

void inputs(aktif)/////////////////////////////////////////////////////
{
   if(!aktif)
      return;
   if(kontak_input)
      kontak=true;
   else
      kontak=false;
   if(lb_input)
   {
      if(lpg[0])
      {
         lpg[0]=false;
         lpg[1]=true;
         benzin[0]=true;
      }
   }
   else
   {
      if(benzin[0])
      {
         benzin[0]=false;
         benzin[1]=true;
         lpg[0]=true;
      }
   }
}

void conditions(aktif)
{
   if(!aktif)
      return;
   switch(inputx_state)
   {
      case hardware:
      {
         if(kontak)//////////////////////////////////////////////////////////
         {
            bit_set(send_data[3],0);
            if(Lpg_Current_Controll_Data[0]=='F')
            {
               //algoritma_calc_enable=true;
               if(sleepx[0])
                  ilkkontak[0]=true;
               sleepx[0]=false;
               sleepx[2]=false;
               counter_sleepx=0;
               if(ilkkontak[2])
               {
                  if(benzin[1])/////////////////////////////////////////////////
                  {
                     if(mode_flt!='f')
                     {
                        mode_flt='f';
                        refresh[0]=true;
                     }
                     else
                        refresh[2]=true;
                     bit_clear(send_data[3],1);
                     benzin[1]=false;
                  }
                  if(lpg[1])////////////////////////////////////////////////////
                  {
                     if(mode_flt!='l')
                     {
                        mode_flt='l';
                        refresh[0]=true;
                     }
                     else
                        refresh[2]=true;
                     bit_set(send_data[3],1);
                     lpg[1]=false;
                  }
               }
            }
            else
            {
               bit_clear(send_data[3],1);
               //algoritma_calc_enable=false;
               mode_flt='f';
               refresh[0]=false;
               refresh[2]=true;
               benzin[0]=true;
               benzin[1]=false;
               lpg[0]=true;
               lpg[1]=false;
            }
         }
         else
         {
            bit_clear(send_data[3],0);
            //algoritma_calc_enable=false;
            refresh[2]=false;
            if(!sleepx[0])
            {
               sleepx[0]=true;
               counter_sleepx=0;
            }
           
            if(Lpg_Current_Controll_Data[29]=='N' && Lpg_Current_Controll_Data[28]=='E')
            {
               mode_flt='f';
               refresh[2]=true;
            }
            else if(refresh[0])
               refresh[2]=true;
               
            ilkkontak[2]=false;
            refresh[0]=false;
            benzin[0]=true;
            benzin[1]=false;
            lpg[0]=true;
            lpg[1]=false;
             
            if(sleepx[2])// || Lpg_Current_Controll_Data[0]=='E')
            {
               disable_interrupts(INT_RC3_H2L);
               disable_interrupts(int_timer2);
               restart_wdt();
               SETUP_WDT(WDT_OFF);
               sleep();
               SETUP_WDT(WDT_256MS);
               sleepx[0]=false;
               sleepx[2]=false;
               enable_interrupts(int_timer2);
               enable_interrupts(INT_RC3_H2L);
               ilkkontak[0]=true;
            }
            if(mode_flt=='l')
            {
               wait=true;
               counter_wait=0;
            }
         }
      }
      break;
     
      case software:
      {
         if(kontak)//////////////////////////////////////////////////////////
         {
            benzin[0]=true;
            benzin[1]=false;
            lpg[0]=true;
            lpg[1]=false;
            bit_set(send_data[3],0);
            V_Dac_Bit=0;
            if(mode_flt=='t')
               V_Dac_Bit=make16(dac_bit[0],dac_bit[1]);
            if(kline[2])
            {
               kline[2]=false;
               inputx_state=hardware;
            }
         }
         else////////////////////////////////////////////////////////////////
            inputx_state=hardware;
      }
      break;
   }
}

void switch_state(durum1, durum2, durum3)////////////////////////////////////////
{
   if(durum1 || durum2)
   {
      if(durum1)
         durum3='e';
      if(durum2)
         refresh[2]=0;
      if(durum3!='l')
      {
         i2c_start();
         i2c_write(0b11000000);
         i2c_write(0b01000000);
         i2c_write(0);
         i2c_write(0);
         i2c_stop();
      }
      switch(durum3)
      {
         case 'e':
            output_low(in1);
            output_low(in2);
            dac_bit_calc_enable=false;
            dacx_write_enable=false;
         break;
         
         case 'f':
            output_low(in2);
            output_high(in1);
            dac_bit_calc_enable=false;
            dacx_write_enable=false;
         break;
         
         case 'l':
            output_low(in1);
            output_high(in2);
            dac_bit_calc_enable=true;
            counter_ripple=0;
            ripple[0]=false;
            ripple[2]=true;
         break;
         
         case 't':
            output_low(in1);
            output_high(in2);
            dac_bit_calc_enable=false;
            dacx_write_enable=true;
         break;
      }
   }
}

void dac_bit_calc(aktif)//////////////////////////////////////////////
{
   if(!aktif)
      return;
   float inducator_binde;
   if(Tank_value<L0 || Tank_value==L0)
   {
      inducator_binde=0;
   }
   else
   {
      if(Tank_value<L20 || Tank_value==L20)
         inducator_binde=((Tank_value-L0)*20)/(L20-L0);
      else if(Tank_value<L40 || Tank_value==L40)
         inducator_binde=20+(((Tank_value-L21)*20)/(L40-L21));
      else if(Tank_value<L60 || Tank_value==L60)
         inducator_binde=40+(((Tank_value-L41)*20)/(L60-L41));
      else if(Tank_value<L80 || Tank_value==L80)
         inducator_binde=60+(((Tank_value-L61)*20)/(L80-L61));
      else if(Tank_value<L100 || Tank_value==L100)
         inducator_binde=80+(((Tank_value-L81)*20)/(L100-L81));
      else
         inducator_binde=100;
   }
   if(Lpg_Current_Controll_Data[5]>inducator_binde && f20[0]) 
   {
      V_Dac_Bit=(unsigned int16)( ((float)(Bdac_max-Bdac_min)*Lpg_Current_Controll_Data[5]) / 100 ) + Bdac_min;
      dacx_write_enable=true;
   }
   else//else if(ripple[2])
   {
      V_Dac_Bit=(unsigned int16)( ((float)(Bdac_max-Bdac_min)*inducator_binde) / 100 ) + Bdac_min;
      ripple[2]=false;
      ripple[0]=true;
      dacx_write_enable=true;
   }
}

void dacx_write(aktif, durum1)///////////////////////////////////////////////////////////
{
   if(!aktif)
      return;
   i2c_start();
   i2c_write(0b11000000);
   i2c_write(0b01000000);
   i2c_write((unsigned int8)((V_Dac_Bit>>4)&0x00ff));
   i2c_write((unsigned int8)((V_Dac_Bit<<4)&0x00f0));
   i2c_stop();
   if(durum1)
      dacx_write_enable=false;
}

void setup_send_data(aktif)/////////////////////////////////////////////////////
{
   if(!aktif)
      return;
   Send_Data[0]=(unsigned int8)(Tank_value>>8)&0xff;
   Send_Data[1]=(unsigned int8)Tank_value&0xff;
   Send_Data[2]=mode_flt;
}


But I haven't figured out software I2C problem. (The pins SCL and SDA connected to 5V with 4k7 resistors)
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Tue Sep 06, 2016 6:44 am     Reply with quote

General thing.
If you are having problems with a particular operation on a pin, make sure you have turned off any peripheral that also uses these pins . This chip has a lot more peripherals that could be interfering. CCP4, MDC, comparator etc.. Some of these you have turned off, but not others. Comparator and CCP4.

Also, ADC_CLOCK_INTERNAL, is not recommended above 1MHz. Read the data sheet. This has been covered here hundreds of times.
eeeahmet



Joined: 06 Jul 2013
Posts: 18
Location: Turkey

View user's profile Send private message

PostPosted: Tue Sep 06, 2016 6:53 am     Reply with quote

Ttelmah,

I've done this before what you said but it still doesn't work.

Code:


void Cpu_Setup()////////////////////////////////////////////////////////////////
{
   if(read_eeprom(29)=='N')
      output_a(0b00101101);
   else
      output_a(0b00001101);
   //output_a(0b00101101);
   set_tris_a(0b00000101);
   set_tris_c(0b00101001);
   
   ////////////////////////////////unnecessary functions are closed

   setup_timer_0(t0_internal);
   setup_timer_1(t1_disabled);
   setup_timer_3(t3_disabled);
   setup_timer_4(t4_disabled,0,1);
   setup_timer_5(t5_disabled);
   setup_timer_6(t6_disabled,0,1);
   setup_ccp1(ccp_off);
   setup_pwm5(pwm_disabled);
   setup_pwm6(pwm_disabled);
   setup_spi(spi_disabled);
   setup_comparator(nc_nc_nc_nc);
   setup_vref(vref_off);
   setup_dac(dac_off);
   setup_cwg(cwg_disabled,0,0,0);
   setup_cwg2(cwg_disabled,0,0,0);
   setup_nco(nco_disabled,0);
   setup_clc1(clc_disabled);
   setup_clc2(clc_disabled);
   setup_clc3(clc_disabled);
   setup_clc4(clc_disabled);
   setup_dsm(dsm_disabled);

   ///////////////////////////////////////////////////////////////////////
   
   setup_adc(adc_clock_internal);
   setup_adc_ports(sAN0 | VSS_VDD);
   setup_wdt(wdt_2s);
   setup_timer_2(T2_DIV_BY_64,155,4);
   enable_interrupts(INT_TIMER2);
   enable_interrupts(INT_RC3_H2L);
   enable_interrupts(INT_EXT);
}
eeeahmet



Joined: 06 Jul 2013
Posts: 18
Location: Turkey

View user's profile Send private message

PostPosted: Tue Sep 06, 2016 7:49 am     Reply with quote

Hello friends,

First, Thank you to everyone for their help.

Secondly, I'm sorry to have wasted your time. Because the I2C problem, it was my mistake.

The integrated DAC in my project as MCP4725A0T-E/C I was using. This equipment began to sell. I bought this chip and a 600 piece. They accidentally put me MCP4725A1T-E/C sent.

A0 the address starts with 00, while the A1 address starts with 01.

I fixed part of the address when the I2C function, it worked flawlessly.

old code
Code:

   i2c_start();
   i2c_write(0b11000000);
   i2c_write(0b01000000);
   i2c_write((unsigned int8)((V_Dac_Bit>>4)&0x00ff));
   i2c_write((unsigned int8)((V_Dac_Bit<<4)&0x00f0));
   i2c_stop();


new code
Code:

   i2c_start();
   i2c_write(0b11000100); //address line
   i2c_write(0b01000000);
   i2c_write((unsigned int8)((V_Dac_Bit>>4)&0x00ff));
   i2c_write((unsigned int8)((V_Dac_Bit<<4)&0x00f0));
   i2c_stop();


Again, thank you so much everybody.

Take care of yourself.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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