Thread: need help programing PIC18F14K22

  1. #1
    Registered User
    Join Date
    May 2013
    Location
    Kaunas, Lithuania
    Posts
    2

    need help programing PIC18F14K22

    hello to all
    I have a problem over here :/ here is the point.

    I have a thermometer connected to battery (12v)
    and I need that: when my thermometer reaches 35 celsius it should open the transistor.
    then from the other battery energy flow to the fan

    sorry for my poor eng. here is the scheme of my idea if u dont get it:
    http://ikelk.lt/i/134796/o_5a9423df82.jpg

    thermometer principal scheme:
    http://ikelk.lt/i/134788/o_24a178da5a.jpg

    thermometer PCB scheme:
    http://ikelk.lt/i/134789/o_37e587e6c2.jpg

    and if you dont know how transistors working:
    How Transistors Work - YouTube

    thermometer code:
    Code:
    #include <p18f14k22.h>
    #pragma config FOSC  = IRC        
    #pragma config WDTEN = OFF  
    #pragma config LVP   = OFF        
    
    const rom unsigned char table_7_segm[]=       
     {
      0x5F, //0
      0x06, //1
      0x3B, //2
      0x2F, //3
      0x66, //4
      0x6D, //5
      0x7D, //6
      0x07, //7
      0x7F, //8
      0x6F, //9
      0x01,      
    };
    
    #pragma code
    
    void delay (unsigned int ms)
    {
    unsigned int i;
    for(i =0 ; i < 332; i++);    
    }
    
    void main (void)
    {
    float skaicius = 0;
    char indikatorius = 0;
    char i = 0;
        
    
    OSCCONbits.IRCF2 = 1;
    OSCCONbits.IRCF1 = 1;
    OSCCONbits.IRCF0 = 1;
    
    ANSEL = 0b0000100;
    ANSELH = 0b0000;
                      
    PORTA = 0;
    TRISA = 0b00100;
    PORTB = 0;
    TRISB = 0;
    PORTC = 0;
    TRISC = 0;
    
    ADCON1 = 0x00;
    ADCON2 = 0b10111101;
    ADCON0 = 0b00001001;
    while (1)
    {
    
    ADCON0bits.GO = 1;
    while (ADCON0bits.GO == 1);
    skaicius = ((unsigned int)ADRESH << 8) |  (unsigned int)ADRESL;
           
    skaicius = skaicius*40.88;
            
    skaicius +=0.5;               
    skaicius = skaicius/1000;
    indikatorius = 0x80;
    for (i=0; i<4; i++) 
    {
    LATC = table_7_segm [(int) (skaicius)]; 
    skaicius = skaicius - (int)skaicius;
    skaicius = skaicius * 10;
    LATB = indikatorius; 
    if(i==1)
    PORTCbits.RC7=1;
                 
    delay(5);
    LATB = 0;
    indikatorius = indikatorius >> 1; 
    }
    }
    
    }
    than you for any help and comments

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    The delay function is wrong. You need to loop on ms. Then it's a good idea to make i volatile.

    Generally, however, you need to wrap the port-level operations into meaningful functions, like "open_transistor" or "wait_for_input". Then test these separately. As it is, it's very hard to know what the operations are supposed to do, so hard to see if they are correct.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    May 2013
    Location
    Kaunas, Lithuania
    Posts
    2
    well ........ then... cuz i'm noob at C

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Some points.
    1. Indentation is vital.
    Code:
    #include <p18f14k22.h>
    #pragma config FOSC  = IRC
    #pragma config WDTEN = OFF
    #pragma config LVP   = OFF
    
    const rom unsigned char table_7_segm[] = {
      0x5F,                         //0
      0x06,                         //1
      0x3B,                         //2
      0x2F,                         //3
      0x66,                         //4
      0x6D,                         //5
      0x7D,                         //6
      0x07,                         //7
      0x7F,                         //8
      0x6F,                         //9
      0x01,
    };
    
    #pragma code
    
    void delay(unsigned int ms)
    {
      unsigned int i;
      for (i = 0; i < 332; i++);
    }
    
    void main(void)
    {
      float skaicius = 0;
      char indikatorius = 0;
      char i = 0;
    
      OSCCONbits.IRCF2 = 1;
      OSCCONbits.IRCF1 = 1;
      OSCCONbits.IRCF0 = 1;
    
      ANSEL = 0b0000100;
      ANSELH = 0b0000;
    
      PORTA = 0;
      TRISA = 0b00100;
      PORTB = 0;
      TRISB = 0;
      PORTC = 0;
      TRISC = 0;
    
      ADCON1 = 0x00;
      ADCON2 = 0b10111101;
      ADCON0 = 0b00001001;
    
      while (1) {
        ADCON0bits.GO = 1;
        while (ADCON0bits.GO == 1);
    
        skaicius = ((unsigned int) ADRESH << 8) | (unsigned int) ADRESL;
        skaicius = skaicius * 40.88;
        skaicius += 0.5;
        skaicius = skaicius / 1000;
        indikatorius = 0x80;
    
        for (i = 0; i < 4; i++) {
          LATC = table_7_segm[(int) (skaicius)];
          skaicius = skaicius - (int) skaicius;
          skaicius = skaicius * 10;
          LATB = indikatorius;
          if (i == 1)
            PORTCbits.RC7 = 1;
          delay(5);
          LATB = 0;
          indikatorius = indikatorius >> 1;
        }
      }
    }
    2. Your schematics don't show where the thermometer or fan are connected.

    3. When posting technical drawings (as images), use PNG format rather than JPG.
    JPG is for natural images. If you compress digital drawings with JPG, then all the fine detail gets fuzzed to the point of being unreadable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Also a comment on the electrical side of things, I'd generally add a snubber to the circuit any time I'm driving a load that might be inductive. That way you're not at the mercy of the fan's control circuitry to safely dissipate any inductive voltage spikes (I'm assuming this fan is brushless - if it were brushed you absolutely need the snubber).

    If you did drive a high-L load and didn't have a snubber, you would greatly reduce your transistor lifespan as it would be subject to very large transient voltage spikes when current is interrupted (I've seen transients as high as 30,000V from a 10V DC brushed motor without a proper snubber).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programing for DNA...
    By S16 in forum C Programming
    Replies: 9
    Last Post: 04-29-2009, 09:25 AM
  2. C programing
    By flame82 in forum C Programming
    Replies: 2
    Last Post: 05-07-2008, 02:35 PM
  3. I do not do a lot of programing but ...
    By dcsmithCB in forum C Programming
    Replies: 3
    Last Post: 10-07-2006, 07:11 AM
  4. C Programing
    By iBiZa in forum C Programming
    Replies: 4
    Last Post: 03-04-2004, 10:13 PM
  5. C++ Programing
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-06-2001, 04:13 PM