ще дадем и пълният програмен код от примера. в предния урок има линк от където можете да си го изтеглите, но все пак ще дадем кофа и като текст тука за по прегледно съдържание. готовият компилиран *.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
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;
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
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
// 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; */
}
// 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