Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By Cat

need help programing PIC18F14K22

This is a discussion on need help programing PIC18F14K22 within the C Programming forums, part of the General Programming Boards category; hello to all I have a problem over here :/ here is the point. I have a thermometer connected to ...

  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
    333
    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.
    http://www.malcolmmclean.site11.com/www

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    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.
    stahta01 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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).
    Salem likes this.
    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, 09:13 PM
  5. C++ Programing
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-06-2001, 04:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21