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

16F690 issues

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



Joined: 25 Jun 2010
Posts: 3

View user's profile Send private message

16F690 issues
PostPosted: Fri Dec 10, 2010 5:14 am     Reply with quote

Hi there,
I wrote a sample code four switches Forwad(Fw), Start , Reverse(Rs) and Stop. I used C0,C1 ,A3 and A4 as switch inputs.
This code works fine in MPLAB IDE simulator, and ISIS simulator as well but not in real time, I mean bread board.

In this When I just tested with Start and Stop PIC woks fine Smile , When I added either Fw or Rs code, it's mis triggering. Sad
I pressed the start button, then it should run all the time unless if press other buttons. But due to mistriggering in start mode as Fw or Rs after 2s or 10s.. Don't know why? Confused I checked config bits.. and disbled most of them.
OSC :internal RC NO clock, WDT:off,PUT:off,MCLRE internal,CP:off,CPD:off, BODEN:BODand SBOREN disabled,IESO:Enabled,FCMEN: Enabled
But no luck..... I almost wated 2 day's for this. simple code...
Of course it's worth if I figured it out.
It either my software(i checked my code line by line may be I missed the buis gger..can any one fix this please.)or
CCS mistriggering (or) MICROCHIP PIC hardware issue ?

Any suggestions welcome:
Thanks in advance

Code:
[/#include"16F690.h"
#device ADC=8       //8-bit conversion
#use delay(internal=4000000)   //8MHz internal c
#fuses INTRC_IO,NOWDT,NOPUT,NOBROWNOUT,NOMCLR
#use rs232(baud=9600,xmit=PIN_B7,rcv=PIN_B5,bits=8,timeout=250, stream=NORM,ERRORS)
#byte TXREG  =0X19
#byte RCREG  =0X1A
#byte PIE1   =0X8C
#byte RCSTA  =0X18
#byte TXSTA  =0X98
#byte SPBRG  =0X99
#byte SPBRGH =0X9A
#byte BAUDCTL=0X9B
#byte PORTA  =0x05
#byte PORTC  =0x07
#bit TXIF = 0x0C.4
#bit RCIF = 0x0C.5
#bit TOIF = 0x0B.2
///////////////////////////////////////////////////////////////
#define fw_on PIN_A3   //if PIN set as INPUT, then no conflict but
#define rs_on PIN_A4
#define STOP_Z PIN_C0
#define START_Z PIN_C1
#define INA PIN_C2
#define INB PIN_C3
///////////////////////////////////////////////////////////////

int x,spcount,mscount,fwlogic1,fwlogic0,rslogic1, rslogic0;
int fcount,rm,rcount,r_count,scount,rs_count1,rscount1,test;
//////////////////////////////////
int bits=0;
#bit fw_set=bits.0
#bit rs_set=bits.1     //bit
#bit readytostartY = bits.2
#bit readytostopY  = bits.3
#bit readytostartZ = bits.4
#bit forward_logicset = bits.5
#bit reverse_logicset = bits.6

//////////////////////////////////
int xbits=0;
#bit Y_on =  xbits.0//
#bit Y_off = xbits.1//
#bit bat_set = xbits.2
#bit sound_set = xbits.3
#bit count_set= xbits.4
//////////////////////////////////
#bit SPEAKER1 = PORTC.4
#bit SPEAKER2 = PORTC.5
/////////////////////////////////
int Zbits=0;
#bit stop_set = Zbits.0
#bit start_set= Zbits.1
#bit fwZ_set = Zbits.2
#bit rsZ_set = Zbits.3
#bit startZ_set = Zbits.4
#bit stopZ_set = Zbits.5

//#bit r_sonder_on = r_sounder_bit.0 // bit set if the radiation on.
/////////////////////////////////////////////////////////////
void setup();
void sendmaster(unsigned char data);
void timer0_isr();
void setup();
void startpulse();
void stoppulse();
void Y_oncheck();
void batlevel_check();
void startZ_check();
void stopZ_check();
void fwrs_check();
void mins_check();
void rs_pulse01();
void rs_pulse10();
///////////////////////////////////////////////////////////////
void startZ_check()
{
if((input(START_Z)|| (start_set == true)) &&((fwZ_set ==FALSE) && rsZ_set == FALSE))
 { start_set = true;
   stop_set = false;
   rs_set=false;
   fw_set=false;
   startpulse();
if(forward_logicset == TRUE ||reverse_logicset== TRUE)
  {
   readytostartY = TRUE;
   forward_logicset = FALSE;
   reverse_logicset= FALSE;
 }}
}
/////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
void stopZ_check()
{
if((input(STOP_Z)) || (stop_set == true))//Y-on pulse to logic board after Fw and RS
 {                                                       
   stop_set = true;
   rs_set=false;
   fw_set=false;
   start_set = false;
   readytostartZ = FALSE;
   stoppulse();
  } 
}
//////////////////////////////////////////////////////////////////////////
void fwrs_check() 
{
 if((input(fw_on)|| fw_set==true) && rsZ_set == FALSE)           
 { readytostartZ=FALSE;readytostartY= FALSE;forward_logicset = FALSE; 
   rs_set=false;
   fw_set=true;
   start_set = false;
   stop_set = false;
   fcount++;
   scount++;   
 if(fcount>0 && scount <= 23 )               
  {spcount++;
   stoppulse();
   fwZ_set = TRUE;             
  }
 if(scount > 23)
  {startpulse();       
   fcount=0;
   fwZ_set = TRUE;
 if(scount >= 46)
  {scount=0;
   fwlogic1++;
   fwlogic0 = fwlogic1 +1;
  }}
 if(spcount>110)
  { forward_logicset = TRUE;
   reverse_logicset= FALSE;
   fwZ_set = FALSE;   
   fw_set= false;
   spcount=0;fcount=0;scount=0;
   fwlogic1=0;
   fwlogic0=0;
  }
  } //end of forward routine//
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
if((input(rs_on)||rs_set==true) && (fwZ_set ==FALSE))
 {readytostartZ =FALSE;readytostartY= FALSE;reverse_logicset= FALSE;
  fw_set=false;
  rs_set=true;
  start_set = false;
  stop_set = false;
  rcount++;
  r_count++;
  x++;
if(x >0 && x <= 46 )
 {test++;
  rs_pulse01();
  rsZ_set =TRUE;
 }
if(x >46 && x<= 184)
 {rscount1++;
  rs_count1++;
  rs_pulse10();
  rsZ_set =TRUE;
  test++;
 }
if(x>184)
 {  reverse_logicset= TRUE;
  rsZ_set =FALSE;
  rs_set=false;
  rm=0;rcount=0;r_count=0;
  x=0;rscount1=0;rs_count1=0;
 }}   //end of reverse routine//////
}
///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
void rs_pulse01()
{
if(rcount>0 && r_count<= 23)
{rm++;
 stoppulse();  }
if(r_count>23)
{rcount=0;
 startpulse();
if(r_count >46)
{r_count=0;  }}
}
void rs_pulse10()
{if(rscount1>0 && rs_count1<=23)
{rm++;                               
 startpulse(); }
if(rs_count1>23)
{rscount1=0;
 stoppulse();
if(rs_count1>=46)
{rs_count1=0;  }}
}
////////////////////////////////////////////////
void startpulse()
{
if (input(INB)&input(INA))
 {output_low(INB);     
  output_low(INA);  }       
else
 {output_high(INA);
  output_high(INB); }
}
////////////////////////////////////////
void stoppulse()
 {output_low(INB);
  output_high(INA); }
/////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
#int_rtcc
void clock_isr()
{SET_RTCC(250);
 startZ_check();
if(Y_off == TRUE ||Y_on == FALSE)
{stopZ_check();
 fwrs_check();   
}       //end of check routine//
 
}
//****************************************///////////////////////////////////////////
void setup()    //enable intterrupts
{ SET_TRIS_A(0X3F);SET_TRIS_B(0XF0);SET_TRIS_C(0X03);
  setup_comparator (nc_nc_nc_nc);enable_interrupts(GLOBAL); enable_interrupts(INT_RTCC);
  setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128|RTCC_8_BIT); delay_us(5);set_rtcc(250);
  setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);set_timer1(0); 
  spcount=0;
  fcount=0;rcount=0;scount=0;spcount=0;
  r_count=0;mscount=0;rm=0;test=0;rs_count1=0;rscount1=0;
  x=0;rslogic1=0; rslogic0=0;fwlogic1=0;fwlogic0=0;
  output_low(PIN_C2); output_low(PIN_C3);output_low(PIN_C4);
  output_low(PIN_C5); output_low(PIN_C6); output_low(PIN_C7);
  setup_adc(ADC_CLOCK_INTERNAL); //ADC clock
  setup_adc_ports(sAN0|VSS_VDD);   //Input combination
  set_adc_channel(0);
}
//////////////////////////////////////////////////////////////////////////////////////
void main()
{setup();
do
{delay_us(5) ;
 // read_adc(ADC_START_ONLY);         //starts the conversion and returns
 delay_us(5);
 }
  while(TRUE);
 }      //end of main loop
/////////////////////////////////////////////

CODE EDITED now it compiles and run fine....

Last edited by Humanityrulz on Fri Dec 10, 2010 8:33 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Fri Dec 10, 2010 7:44 am     Reply with quote

There seems to be an enormous amount of code, that is either pointless, or wrong....

You setup, and start an ADC conversion, but nowhere are you reading an ADC conversion.
You have an interrupt handler, and a timer setup, but nowhere are you enabling the timer interrupt, so this will never be called. Most of your code will never actually be reached...
You configure the ADC to use 'ADC_CLOCK_INTERNAL'. Check the data sheet. Is this legitimate for a CPU running at 8MHz, if you are not sleeping during the ADC conversion (doesn't matter at present since you don't read the ADC....).
You are both reading, and writing the same pins. You do not have 'fast_io' selected, so as soon as you output, the pins will be switched to being outputs. Will this work?.

Best Wishes
Humanityrulz



Joined: 25 Jun 2010
Posts: 3

View user's profile Send private message

PostPosted: Fri Dec 10, 2010 8:03 am     Reply with quote

Ttelmah wrote:
There seems to be an enormous amount of code, that is either pointless, or wrong....

You setup, and start an ADC conversion, but nowhere are you reading an ADC conversion.
You have an interrupt handler, and a timer setup, but nowhere are you enabling the timer interrupt, so this will never be called. Most of your code will never actually be reached...
You configure the ADC to use 'ADC_CLOCK_INTERNAL'. Check the data sheet. Is this legitimate for a CPU running at 8MHz, if you are not sleeping during the ADC conversion (doesn't matter at present since you don't read the ADC....).


Hi there,
Thank you reply. My full version code do the A/D conversion, TIMER1.
Code which I posted was small or half of the original, and it runs with timer0 interrupt. I compiled it, showed build succeeded. But sorry I haven't run it. It's my fault ...........now I posted proper code just edited it.
About the clock, that is good point actually. My initial code was wrote to run at 4MHz of course but changed to 8MHz later.
Anyhow I changed to 4MHz but no change my start pulse still mistriggering.
Ttelmah wrote:

You are both reading, and writing the same pins. You do not have 'fast_io' selected, so as soon as you output, the pins will be switched to being outputs. Will this work?.

Which pins you referred to... you mean A/D inputs or ?

Thanks for comments
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Fri Dec 10, 2010 9:46 am     Reply with quote

ADC_CLOCK_INTERNAL, is still not recommended for 4MHz. _read_ the data sheet. It is _vital_. Nobody can design with any chip like this, without repeatedly reading this....

You are making the big/possibly wrong assumption, that switches are perfect. research 'debounce'.

You are doing too much in your interrupt. Have this just read the switch value, apply the debounce, and generate a 'switches' value reflecting what is really set/clear. Do the rest of your processing in the main.

Search here for why setting a timer 'to' a value, will almost certainly not function quite as you expect (in your case, with the 256 prescaler, the result will only be a little slow, but if timing is critical, it won't be what you expect).

You are setting TRIS, but are not selecting fast_io. As such, these settings _will_ be overridden. Same comment then still applies about the outputs/inputs.

A lot still to do....

Smile

Best Wishes
Humanityrulz



Joined: 25 Jun 2010
Posts: 3

View user's profile Send private message

PostPosted: Fri Dec 10, 2010 10:39 am     Reply with quote

Ttelmah wrote:
ADC_CLOCK_INTERNAL, is still not recommended for 4MHz. _read_ the data sheet. It is _vital_. Nobody can design with any chip like this, without repeatedly reading this....


Are you referring this: In data sheet "When the device frequency is greater than 1MHz, the A/D RC clock source is only recommended if th e conversion will be performed during Sleep."
Do I have to use external clock source ? or any other better way ? Suggest me in this.
Ttelmah wrote:

You are making the big/possibly wrong assumption, that switches are perfect. research 'debounce'.....

Yeah.. I should have done this by now, never mind I start do some research. Again any help or comments Welcome Thank you.
Ttelmah wrote:

You are doing too much in your interrupt. Have this just read the switch value, apply the debounce, and generate a 'switches' value reflecting what is really set/clear. Do the rest of your processing in the main......

You're right , I am doing everything in Interrupt, I don't know it is bad practice...!!! I will change my code as you suggested. thanks for comments.

Ttelmah wrote:

Search here for why setting a timer 'to' a value, will almost certainly not function quite as you expect (in your case, with the 256 prescaler, the result will only be a little slow, but if timing is critical, it won't be what you expect). .....

Honestly, interrupt triggering very well as I expected , the main issue is unexpected triggering in PIC happening don't know why? You might be right about too much going on in interrupt.

Ttelmah wrote:

You are setting TRIS, ......
YES.....!!!
Ttelmah wrote:

but are not selecting fast_io. ......
there is a syntax #use FAST_IO(A)
I got this code from from HELP file in CCS
Code:
void pulse_B0() {
#pragma use fast_io(B)
      output_low(PIN_B0);
      delay_ms(100);
      output_high(PIN_B0);
      delay_ms(100);
#pragma use standard_io(B)
}
Sorry still Don't get it? How it's related to my code. How can use this syntax in my code?
Ttelmah wrote:

A lot still to do..........


You're absolutely right.... LOT TO DO..... much appreciated for your help and comments.
Welcome some more comments .... to Learn...Wink
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Fri Dec 10, 2010 3:40 pm     Reply with quote

Read the manual about fast_io, and standard_io.
With standard_io selected (the default), the _compiler_ controls TRIS. If you read a port bit, that bit is set as an input. If you write a bit, it is set as an output. TRIS statements, with standard_io selected, are basically 'pointless'.
Standard_io, generally works well, and avoids you having to control TRIS at all. It only fails if you want to do something like read an entire port, where some bits are set as an output, or if you want to read the status of an output pin (you can do this by using 'input_state', instead of input, which then doesn't change the TRIS.
The pragma you show, is unnecessarily complex. They are switching to fast_io and then back to standard. If you are controlling the TRIS, you just need to select fast_io at the start of your program.

You can clock the ADC, from it's own internal clock, or the master oscillator divided by a ratio. In the table in the ADC section, there will be details of what division ratios should be used for different clock rates. Select the one to suit your clock rate. No 'external clock' needed.

Best Wishes
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