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

Need programming help of pic16f506
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
nikolas



Joined: 09 Oct 2011
Posts: 32

View user's profile Send private message

PostPosted: Thu Oct 13, 2011 1:28 pm     Reply with quote

Thought also of another version, which may optimize a little bit the code.
Code:

 while(1)
    {
     result = read_adc();
       
  //   printf("%u \n\r", result);
    if ((result>240) &&  (temp != 0 ))  {  //neutral
    temp = 0;
    display_digit(temp);}
    else  if ((result<40) &&  (temp != 1 )){ ///1st gear
    temp = 1;
    display_digit(temp);}
    else  if ((result<80) &&  (temp != 2 )){ //2nd gear
    temp = 2;
    display_digit(temp);}
    else  if ((result<120 ) &&  (temp != 3 )){ //3rd gear
    temp = 3;
    display_digit(temp);}
    else  if ((result< 160 ) &&  (temp != 4 )){ //4th gear
    temp = 4;
    display_digit(temp);}
    else  if ((result< 200 ) &&  (temp != 5 )){ //5th gear
    temp = 5;
    display_digit(temp);}
    else { //6th gear
    temp = 6;
    display_digit(temp);}
     delay_ms(1000); 
     
    }
  }

I changed the check from just
(result < x) to
(result < x) && (temp != 0 ))
This way, i think, if after a loop, there is still the same gear, i guess, there is no point to call the routine, since the gear number remains the same on the display.

I really need your help guys.
I believe the code is close to be regarded as complete with all the information you provided..please don't get back on me now :(

a) if this change in the code an optimization or not necessary?
b) the read_adc takes values from 0 to 255 or 0 to 1023.
c) why the c3 pin remains on, until gear is selected? so according to the gear it either turns off or remains on.

Looking forward to hearing from you.
temtronic



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

View user's profile Send private message

PostPosted: Thu Oct 13, 2011 1:40 pm     Reply with quote

You might like to post your entire program,instead of 'bits and pieces' as it's too hard to follow.

The ADC is a 10 bit device,though 8 are probably fine for your application.

If C3 is also used for any internal peripherals(SPI, UART,etc.), those should be disabled so that C3 will only be used as an I/O pin.
nikolas



Joined: 09 Oct 2011
Posts: 32

View user's profile Send private message

PostPosted: Thu Oct 13, 2011 1:45 pm     Reply with quote

you are right the code so far is

Code:
#include <16F506.h>
 #fuses INTRC_IO, IOSC4, NOWDT
 #use delay(clock = 4M)
 #use rs232(baud=9600, xmit=PIN_C3) 

 //===============================

 void display_digit(val);
 
 void main()
 {
 int8 result,temp;
 setup_adc_ports(AN0_AN2); 
 setup_adc(ADC_CLOCK_DIV_8);
 set_adc_channel(0);
 delay_us(10);   

 while(1)
   {
    result = read_adc();
     
 //   printf("%u \n\r", result);
   if (result>240){
   temp = 0;
   display_digit(temp);}
   else  if (result<40){
   temp = 1;
   display_digit(temp);}
   else  if (result<80){
   temp = 2;
   display_digit(temp);}
   else  if (result<120 ){
   temp = 3;
   display_digit(temp);}
   else  if (result< 160 ){
   temp = 4;
   display_digit(temp);}
   else  if (result< 200 ){
   temp = 5;
   display_digit(temp);}
   else {
   temp = 6;
   display_digit(temp);}
    delay_ms(1000); 
   
   }
 }
 
 
 int8 dbits[7] = {63,12,91,94,108,118,119};

 void display_digit(int8 val) {
    int8 itemp;
    if (val>6) return; //Impossible to display
    itemp=dbits[val];
    if (itemp & 64) {
       //Here extra bit on B1 needs to go on
       output_high(PIN_B1);
    }
    else {
       output_low(PIN_B1); //else off
    }
    output_c(itemp & 63); //send other six bits;
 }


and I'm considering the change that i mentioned above.
nikolas



Joined: 09 Oct 2011
Posts: 32

View user's profile Send private message

PostPosted: Thu Oct 13, 2011 8:05 pm     Reply with quote

just an update with my "amateur" soldered circuit..


nikolas



Joined: 09 Oct 2011
Posts: 32

View user's profile Send private message

PostPosted: Mon Oct 17, 2011 6:11 pm     Reply with quote

If anyone is aware of hardware stuff.

Can the LM324 be omitted? Along with the two resistors?

and lead the signal directly to the pic??
temtronic



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

View user's profile Send private message

PostPosted: Tue Oct 18, 2011 5:19 am     Reply with quote

Since we don't know what the 'speed sensor' is, I'd leave the LM324 in. It's configured as a voltage follower which does severals functions for you.
1) provides a low impedence input to the ADC.
2) offers a layer of protection to the PIC ADC
3) limits the Vin to +5 if the sensor produces more

4) has 3 more opamps available for other 'features' as your project grows(they all do !).

Suggestions regarding your boardlayout
1) use a socket for the LM324,in fact all chips.It doesn't cost much these days and allows you to easily change them if required.Power failure,new PIC,etc.

2) bolt the LM7805 to the perfboard with a nut and bolt,adding a small heatsink.while maybe not required in this circuit,it's good practice to keep the regulator as cool as possible.

3) download one of the free PCB design programs(I use ExpressPCB) do try several 'layouts' of the project.That way you can figure out the best layout for easy 'solder stiching' of traces,jumpers,switches,LEDs,etc.even with simple PIC projects, I use it to get a great layout without a 'mess of wires' underneath.Yes, it takes time to learn,but once you get used to it,you'll actually save time in the final building stage.

4) make a test setup of a couple of those white 'breadboards'.I've used them for decades and know that if the circuit works in them, it'll work in the real world as a 'solder stitched' perfboard or a real PCB.Typically I have the power supply,PIC and LCD on one board with external peripherals(RTC, Vinculum,RS232) on the other.Even an 18F4550 at 24MHz works fine.
nikolas



Joined: 09 Oct 2011
Posts: 32

View user's profile Send private message

PostPosted: Wed Oct 19, 2011 8:38 pm     Reply with quote

Thank you very much for your advice. It is really useful.
I was told to notice the cooling part of 7805 from another party as well. I guess it is quite important.
I've installed express pcb but not yet keen with it. I need further practice.
Also, can you please tell me what a "breadboard" is? I find this term second time within a day searching around forums.
nikolas



Joined: 09 Oct 2011
Posts: 32

View user's profile Send private message

PostPosted: Sun Oct 23, 2011 9:03 am     Reply with quote

can someone explain to me what is the use of mclr,
and if it is necessary or good idea to connect it to the circuit..?
nikolas



Joined: 09 Oct 2011
Posts: 32

View user's profile Send private message

PostPosted: Thu Oct 27, 2011 9:38 am     Reply with quote

got at last a programer,
wrote the program on the pic, all segments come on when they are supposed to apart from segment f...

with the old pic with the preinstalled program segment f comes on as well..

potential pic failure?
nikolas



Joined: 09 Oct 2011
Posts: 32

View user's profile Send private message

PostPosted: Thu Oct 27, 2011 10:02 am     Reply with quote

segment f is powered from pic16f506 pin c5/t0ckl
temtronic



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

View user's profile Send private message

PostPosted: Thu Oct 27, 2011 11:47 am     Reply with quote

Breadboard refers to any of the 'white, solderless' boards that allow you to connect PICs and parts with small jumper wires(22ga solid) without soldering. Great tool for building and testing new circuits. I've found that any circuit that works on them, will work on 'perfboard' (like you have ) or a real PCB. The nice thing is it's very,very easy to change pinouts,add chips, change resistors, etc. Typically I have two,one as a long term test setup, the other for making changes. That way I don't have to rewire one board and mess things up. If you can get 10 conductor, 22ga solid wire as a cable,you'll have all 10 colours, making it easier to wire things up.

If the LED display works with the old PIC and program, then you probably have a code issue. Perhaps the segment table( dbits(....) ) is NOT correct, and so not turning on the 'f' segment. Consult the LEDs datasheet for pinout, compare to the PIC I/O pins you've used. A simple truth table will give you the proper bit setting for whatever 'number' or 'digit' you want.

MCLR is the master clear pin. Usually pulled high with a 10K resistor with a normally open push button to ground. Pressing the button and releasing it will cause the PIC to restart the program. On some newer PICs( NOT all), you can 'reuse' this pin for an extra digital input IF you select the correct FUSES(configuration) option.
nikolas



Joined: 09 Oct 2011
Posts: 32

View user's profile Send private message

PostPosted: Thu Oct 27, 2011 4:33 pm     Reply with quote

i ordered a breadboard from ebay, since it seems a very easy way to start experimenting on a new project without all the soldering part..

i reloaded, two or three different programs,
i also entered a line "output_high(pin_c5)" but no result...
also checked the segment table and it seems to be correct..

is there any way to verify that pic's pins are actually working..???
nikolas



Joined: 09 Oct 2011
Posts: 32

View user's profile Send private message

PostPosted: Thu Oct 27, 2011 5:12 pm     Reply with quote

nikolas wrote:
i ordered a breadboard from ebay, since it seems a very easy way to start experimenting on a new project without all the soldering part..

i reloaded, two or three different programs,
i also entered a line "output_high(pin_c5)" but no result...
also checked the segment table and it seems to be correct..

is there any way to verify that pic's pins are actually working..???


i even erased the code protected pic and loaded my program, but still the same result.. pin_c5 not responding..


Code:
#include <16F506.h>
#device adc=8

#FUSES NOWDT                   
#FUSES INTRC_IO               
#FUSES NOPROTECT               
#FUSES NOMCLR                     
#FUSES IOSC4                   

#use delay(clock=4000000)

void display(int8 gear_out)
{
    const int8 segment[7] = {63,12,91,94,108,118,119};
    int8 show;
    switch (gear_out)
    {
      case 1: show=segment[1];
            break;
      case 2: show=segment[2];
            break;
      case 3: show=segment[3];
            break;
      case 4: show=segment[4];
            break;
      case 5: show=segment[5];
            break;
      case 6: show=segment[6];
            break;
      default : show=segment[0];
            break;
    }
    if (show & 64) output_high(PIN_B1);
    else output_low(PIN_B1); 
    output_c(show & 63);
}

void main()
{
   int8 gear,i,temp;

   setup_adc_ports(AN0_AN2);
   setup_adc(ADC_CLOCK_DIV_16);
   setup_comparator(NC_NC_NC_NC);
   set_adc_channel(0);
   delay_us(50);
   temp=0;
   for (i=0;i<7;i++)
   {
      display(i);
      delay_ms(200);
   }
   output_c(0);
   for (i=0;i<10;i++)
   {
   output_toggle(pin_b1);
   delay_ms(100);
   }
   while(1)
   {
      gear = read_adc();
      if ((gear>245) & (temp!=0)){
      temp=0;
      display(0);}
      else if ((gear>10)&(gear<=97) & (temp!=1)){
      temp=1;
      display(1);}
      else if ((gear>97)&(gear<=122) & (temp!=2)){
      temp=2;
      display(2);}
      else if ((gear>122)&(gear<=163) & (temp!=3)){
      temp=3;
      display(3);}
      else if ((gear>163)&(gear<=204) & (temp!=4)){
      temp=4;
      display(4);}
      else if ((gear>204)&(gear<=227) & (temp!=5)){
      temp=5;
      display(5);}
      else if ((gear>227) & (gear<=245) & (temp!=6)){
      temp=6;
      display(6);}     
      else if (gear<10){
      output_c(0);
      output_toggle(pin_b1);}
      else delay_ms(100);
   }
}
nikolas



Joined: 09 Oct 2011
Posts: 32

View user's profile Send private message

PostPosted: Fri Oct 28, 2011 9:48 am     Reply with quote

searching here,

i found this code on a thread from PCM Programmer

Code:
#define set_options(value)   {#ASM         \
                               MOVLW  value \
                               OPTION       \
                               #ENDASM}

 //================================
 void main()
 {
 setup_comparator(NC_NC);

 // Enable pull-ups, wake-up on change, and Pin B2 for normal i/o.
 set_options(0x1F);


it does the jobs, because it sets TOCS to 0 but i don't know if the rest of the bits have to be 0 or 1 in my case..
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 Previous  1, 2, 3
Page 3 of 3

 
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