ще дадем и пълният програмен код от примера. в предния урок има линк от където можете да си го изтеглите, но все пак ще дадем кофа и като текст тука за по прегледно съдържание. готовият компилиран *.hex файл можете да си изтеглите от рар архива от този линк
http://karadev.net/basto/adc_leson/adc_leson.rar

отново ще покажем схемата според която е написан софтуера и неговата функционалност




 
 

файл >>> main.c съдържание

#include <xc.h> // this is global for all *****.c files
#include "initcpu.h" //header file this must be second in order !!!
#include "main.h"    // header file    
#include "fuse.h"  // include like a code file
                    //must be last in order !!!
#include "adc_trimer.h"                    

void main(void) // entry point function call just one !!!
{
    init_cpu(); // initialize cpu hardware blocks
                // using before main just once
                // code is in initcpu.c file
    
    while(1)    // loop using condition
                //until is 1 = FOREVER
    {
        if(jumper==1) // make a chek for value
        {trimer();}    // if is 1 call function
        else            // orde
        {trimer2();}    // if is 0 call other function
        
        // THIS CODE HERE IS FOR START TESTING OF PROJECT
        // YOU CAN YOU TOO FOR FUN OR FOR WHAT YOU WANT :)
        // BLINK LEDS FOR DISCO PARTY :)
        /*led_1 = 1; __delay_ms(200); led_1 = 0; __delay_ms(200);
        led_2 = 1; __delay_ms(200); led_2 = 0; __delay_ms(200);
        led_3 = 1; __delay_ms(200); led_3 = 0; __delay_ms(200);
        led_4 = 1; __delay_ms(200); led_4 = 0; __delay_ms(200);
        led_5 = 1; __delay_ms(200); led_5 = 0; __delay_ms(200);
        led_6 = 1; __delay_ms(200); led_6 = 0; __delay_ms(200);
        led_7 = 1; __delay_ms(200); led_7 = 0; __delay_ms(200);
        led_8 = 1; __delay_ms(200); led_8 = 0; __delay_ms(200);
        // end efect 1
        
        led_1 = 1; __delay_ms(200); led_2 = 1; __delay_ms(200);
        led_3 = 1; __delay_ms(200); led_4 = 1; __delay_ms(200);
        led_5 = 1; __delay_ms(200); led_6 = 1; __delay_ms(200);
        led_7 = 1; __delay_ms(200); led_8 = 1; __delay_ms(200);
        
        led_1 = 0; __delay_ms(200); led_2 = 0; __delay_ms(200);
        led_3 = 0; __delay_ms(200); led_4 = 0; __delay_ms(200);
        led_5 = 0; __delay_ms(200); led_6 = 0; __delay_ms(200);
        led_7 = 0; __delay_ms(200); led_8 = 0; __delay_ms(200);
        // end efect 2
        
        led_8 = 1; __delay_ms(200); led_8 = 0; __delay_ms(200);
        led_7 = 1; __delay_ms(200); led_7 = 0; __delay_ms(200);
        led_6 = 1; __delay_ms(200); led_6 = 0; __delay_ms(200);
        led_5 = 1; __delay_ms(200); led_5 = 0; __delay_ms(200);
        led_4 = 1; __delay_ms(200); led_4 = 0; __delay_ms(200);
        led_3 = 1; __delay_ms(200); led_3 = 0; __delay_ms(200);
        led_2 = 1; __delay_ms(200); led_2 = 0; __delay_ms(200);
        led_1 = 1; __delay_ms(200); led_1 = 0; __delay_ms(200);
        // end efect 3
        
        led_8 = 1; __delay_ms(200); led_7 = 1; __delay_ms(200);
        led_6 = 1; __delay_ms(200); led_5 = 1; __delay_ms(200);
        led_4 = 1; __delay_ms(200); led_3 = 1; __delay_ms(200);
        led_2 = 1; __delay_ms(200); led_1 = 1; __delay_ms(200);
        
        led_8 = 0; __delay_ms(200); led_7 = 0; __delay_ms(200);
        led_6 = 0; __delay_ms(200); led_5 = 0; __delay_ms(200);
        led_4 = 0; __delay_ms(200); led_3 = 0; __delay_ms(200);
        led_2 = 0; __delay_ms(200); led_1 = 0; __delay_ms(200);
        // end efect 2
        */
        
    }//end while DON LOSE THE BRACKET :)
    
} //end main DON LOSE THIS BRACKET TOO :)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - -- - - - - - - - - - -

файл >>> initcpu.c съдържание

#include <xc.h>
#include "main.h"

void init_cpu(void)
{                // set cloc of one of this and check in main.h
                // this value for corect timing _XTAL_FREQ 4000000
OSCCON = 0x6A; // 4F - 1Mhz // 6A - 4Mhz // 0x38 - 8Mhz // 0x1E - 16Mhz

ADON = 1; // ADC block is ON
ANSELA = 0b00000001;  // set RA0 for analog pin others digitals
TRISA = 0b00000011;    // set RA0 and RA1 for inpits
LATA = 0b00000000;
PORTA = 0x00;    

ANSELB = 0b00000000;    // set all pins for digital
TRISB = 0b00000000;    // set all pins for outputs
LATB = 0b00000000;
PORTB = 0x00;

}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - -- - - - - - - - - - -

файл >>> adc_trimer.c съдържание

#include <xc.h>
#include "main.h"
#include "adc_trimer.h"

volatile char trimer1;
// volatile not using from other functions

void trimer(void)
{    trimer1 = 0;    // set variable to zero = 0
    ADFM = 0;         // left arenged result in ADRESH:ADRESL
    CHS4=0;CHS3=0;CHS2=0;CHS1=0;CHS0=0; // select ADC channel
    __delay_ms(10); // IMPORTANT THIS IS AQUISITION TIME !!!!
    GO = 1;            // start mesuring temp ADC value
    trimer1 = ADRESH;
    // save in ADRESH and using only ADRESH value to variable
    
    if(trimer1 == 0)    // make checkout of variable
    { led_1 = 0; led_2 = 0; led_3 = 0; led_4 = 0;
      led_5 = 0; led_6 = 0; led_7 = 0; led_8 = 0;}
    else
        if(trimer1 > 1 && trimer1 <= 31) // if value is between 1 AND 31
    { led_1 = 1; led_2 = 0; led_3 = 0; led_4 = 0; // set led_1 to light 1
      led_5 = 0; led_6 = 0; led_7 = 0; led_8 = 0;}// others stay dark 0
    else
        if(trimer1 > 32 && trimer1 <= 63)
    { led_1 = 0; led_2 = 1; led_3 = 0; led_4 = 0;
      led_5 = 0; led_6 = 0; led_7 = 0; led_8 = 0;}
    else
        if(trimer1 >= 64 && trimer1 <= 95)
    { led_1 = 0; led_2 = 0; led_3 = 1; led_4 = 0;
      led_5 = 0; led_6 = 0; led_7 = 0; led_8 = 0;}
    else
        if(trimer1 >= 96 && trimer1 <= 127)
    { led_1 = 0; led_2 = 0; led_3 = 0; led_4 = 1;
      led_5 = 0; led_6 = 0; led_7 = 0; led_8 = 0;}
    else
        if(trimer1 >= 128 && trimer1 <= 159)
    { led_1 = 0; led_2 = 0; led_3 = 0; led_4 = 0;
      led_5 = 1; led_6 = 0; led_7 = 0; led_8 = 0;}
    else
        if(trimer1 >= 160 && trimer1 <= 191)
    { led_1 = 0; led_2 = 0; led_3 = 0; led_4 = 0;
      led_5 = 0; led_6 = 1; led_7 = 0; led_8 = 0;}
    else
        if(trimer1 >= 192 && trimer1 <= 223)
    { led_1 = 0; led_2 = 0; led_3 = 0; led_4 = 0;
      led_5 = 0; led_6 = 0; led_7 = 1; led_8 = 0;}
    else
        if(trimer1 >= 224 && trimer1 <= 255)
    { led_1 = 0; led_2 = 0; led_3 = 0; led_4 = 0;
      led_5 = 0; led_6 = 0; led_7 = 0; led_8 = 1;}
    
}

void trimer2(void)    // same function but lights are different
{    trimer1 = 0;    // IMPORTANT !!! clear value before USE IT !!!!
    ADFM = 0; // left arenged result in ADRESH:ADRESL
    CHS4=0;CHS3=0;CHS2=0;CHS1=0;CHS0=0;
    __delay_ms(10);
    GO = 1;
    trimer1 = ADRESH; // using only result in ADRESH
    
    if(trimer1 == 0)
    { led_1 = 0; led_2 = 0; led_3 = 0; led_4 = 0;
      led_5 = 0; led_6 = 0; led_7 = 0; led_8 = 0;}
      
    else
        if(trimer1 > 1 && trimer1 <= 31)
    { led_1 = 1; led_2 = 0; led_3 = 0; led_4 = 0;
      led_5 = 0; led_6 = 0; led_7 = 0; led_8 = 0;}
      
    else
        if(trimer1 > 32 && trimer1 <= 63)
    { led_1 = 1; led_2 = 1; led_3 = 0; led_4 = 0;
      led_5 = 0; led_6 = 0; led_7 = 0; led_8 = 0;}
      
    else
        if(trimer1 >= 64 && trimer1 <= 95)
    { led_1 = 1; led_2 = 1; led_3 = 1; led_4 = 0;
      led_5 = 0; led_6 = 0; led_7 = 0; led_8 = 0;}
      
    else
        if(trimer1 >= 96 && trimer1 <= 127)
    { led_1 = 1; led_2 = 1; led_3 = 1; led_4 = 1;
      led_5 = 0; led_6 = 0; led_7 = 0; led_8 = 0;}
      
    else
        if(trimer1 >= 128 && trimer1 <= 159)
    { led_1 = 1; led_2 = 1; led_3 = 1; led_4 = 1;
      led_5 = 1; led_6 = 0; led_7 = 0; led_8 = 0;}
      
    else
        if(trimer1 >= 160 && trimer1 <= 191)
    { led_1 = 1; led_2 = 1; led_3 = 1; led_4 = 1;
      led_5 = 1; led_6 = 1; led_7 = 0; led_8 = 0;}
      
    else
        if(trimer1 >= 192 && trimer1 <= 223)
    { led_1 = 1; led_2 = 1; led_3 = 1; led_4 = 1;
      led_5 = 1; led_6 = 1; led_7 = 1; led_8 = 0;}
      
    else
        if(trimer1 >= 224 && trimer1 <= 255)
    { led_1 = 1; led_2 = 1; led_3 = 1; led_4 = 1;
      led_5 = 1; led_6 = 1; led_7 = 1; led_8 = 1;}
    
    // this code here is from another program
    // check the code and search in inet for see what
    // a command are use here
    // while(GO)continue;
    // TIME DELAY OFF - 1min-10min
    // trimer1 = (trimer1 >> 5); // bit moving rigth with 5
                    // from 8 bits after moving they are only 3 = 111
    // decimal 7 max = 1800 sec = 3 minute
    // delayOFF = trimer1 + 1; // 7 + 1 = 8 // if + 1 = 1800 sec max
    // delayOFF = delayOFF * 600; // 8 * 600 = 4800 = 10 minuti
    
    /* ADFM = 0;
    CHS4=0;CHS3=0;CHS2=0;CHS1=0;CHS0=1;
    __delay_ms(20);
    GO = 1;
    while(GO)continue;
    trimer2 = ADRESH; // TIME DELAY ON - 3s-10s
    trimer2 = (trimer2 >> 4); // bit moving rigth with 4
                    // from 8 after moving they are only 4 = 1111
    delayON = trimer2; */
}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - -- - - - - - - - - - -

файл >>> adc_trimer.h съдържание

#ifndef _TRIMER_
#define _TRIMER_

extern volatile char trimer1 ;
void trimer(void);
void trimer2(void);
void init_cpu(void);

#endif

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - -- - - - - - - - - - -

файл >>> fuse.h съдържание

// system file configuration
// for setup hardware
//modules >> read pdf !!!

#pragma config FOSC   = INTOSC
#pragma config WDTE   = OFF
#pragma config PWRTE  = OFF
#pragma config MCLRE  = OFF
#pragma config CP     = OFF
#pragma config CPD    = OFF
#pragma config BOREN  = OFF
#pragma config IESO   = OFF
#pragma config FCMEN  = OFF
#pragma config WRT    = OFF
#pragma config PLLEN  = OFF
#pragma config STVREN = OFF
#pragma config BORV   = LO
#pragma config LVP    = OFF

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - -- - - - - - - - - - -

файл >>> initcpu.h съдържание

#ifndef _INITCPU_
#define _INITCPU_

void init_cpu(void);
#endif

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - -- - - - - - - - - - -

файл >>> main.h съдържание

#ifndef _MAIN_
#define _MAIN_

#define trimer10k    PORTAbits.RA0 // pin 17 input
#define jumper  PORTAbits.RA1 // pin 18 input
#define notused3  PORTAbits.RA2 // pin 1
#define notused4  PORTAbits.RA3 // pin 2
#define notused5  PORTAbits.RA4 // pin 3
#define notused6  PORTAbits.RA5 // pin 4 RA5 - input4 only
#define notused7  PORTAbits.RA6 // pin 15
#define notused8  PORTAbits.RA7 // pin 16

#define led_1 PORTBbits.RB0 // pin 6
#define led_2 PORTBbits.RB1 // pin 7
#define led_3 PORTBbits.RB2 // pin 8
#define led_4 PORTBbits.RB3 // pin 9
#define led_5 PORTBbits.RB4 // pin 10
#define led_6 PORTBbits.RB5 // pin 11
#define led_7 PORTBbits.RB6 // pin 12
#define led_8 PORTBbits.RB7 // pin 13

#define _XTAL_FREQ 4000000 // this value must set
            // corect depend from OSCCON for corect timing
            // for __delay_ms(500) function
#endif