• AVR Freaks

PIC18F2620 with SD-CARD and ADC

Author
lissonpsantos2
New Member
  • Total Posts : 1
  • Reward points : 0
  • Joined: 2015/08/18 07:36:38
  • Location: 0
  • Status: offline
2016/02/08 15:16:34 (permalink)
0

PIC18F2620 with SD-CARD and ADC

Hi guys
I'm trying to write in my sdcard the values from the ADC at AN0 and AN1, but i can't figure out why it doesn't work. I've tested only with the SDCard and it works properly, it seems that the problem is with the ADC. I'm using the C18 compiler, could you please help me?!
 
 
 
 
#include <p18f2620.h>
#include <stdlib.h>
#include <timers.h>
#include <string.h>
#include <adc.h>

#include "FSIO.h"

#pragma config WDT = OFF, OSC = HS, MCLRE = ON, PWRT = ON

#pragma interrupt interrupcaoT0
#pragma code high_vector=0x08
 void interrupt_at_high_vector(void) {
  _asm GOTO interrupcaoT0 _endasm
 }
#pragma code

#define tempoDoDelaySegundos 5
#define LED PORTCbits.RC1
#define STRT PORTCbits.RC0

unsigned int contagem;

void interrupcaoT0 (void);

void setupT0 (void);

void clock (int segInc,
   unsigned short int *SEG,
   unsigned short int *MIN,
   unsigned short int *HR,
   unsigned short int *DIA,
   unsigned short int *MES,
   unsigned short int *ANO
   );

void picsetup (void);

int input1 (void);

int input2 (void);

void zigbee (void);

void main (void) {

 FSFILE *BKP;

 int AN0 = 0;
 int AN1 = 0;
 unsigned short int SEG = 0;
 unsigned short int MIN = 0;
 unsigned short int HR = 0;
 unsigned short int DIA = 1;
 unsigned short int MES = 1;
 unsigned short int ANO = 2015;

 setupT0();
 picsetup();

 while(STRT);
 while(!STRT);

 LED = 1;

 FSInit();
 BKP = FSfopenpgm("BACKUP.TXT", "w+");
 FSfprintf(BKP, "HORA \t DATA \t AN0 \t AN1 \n");

 while (1) {
  
  if(contagem>=2000*tempoDoDelaySegundos) {
   contagem = 0;
   clock (tempoDoDelaySegundos, &SEG, &MIN, &HR, &DIA, &MES, &ANO);
   AN0 = input1();
   AN1 = input2();
   FSfprintf(BKP, "%02d:%02d:%02d \t %02d/%02d/%d \t %d \t %d \n",HR, MIN, SEG,DIA, MES, ANO, AN0, AN1);

  }
  

  if(STRT == 0) {
    FSfclose(BKP);
   CloseADC();
   LED = 0;
    while(1);
  }
 }
}

void interrupcaoT0 (void) {
 contagem++;
 INTCONbits.TMR0IF = 0;
  WriteTimer0(3);
}

void setupT0 (void) {
 contagem = 0;
 T0CON = 0xC1;
 INTCON = 0xE0;
 WriteTimer0(3);
}

void clock (int segInc, unsigned short int *SEG, unsigned short int *MIN, unsigned short int *HR, unsigned short int *DIA, unsigned short int *MES, unsigned short int *ANO) {
 *SEG += segInc;
 if (*SEG == 60) {
  *SEG = 0;
  *MIN++;
 }
 if (*MIN == 60) {
  *MIN = 0;
  *HR++;
 }
 if (*HR == 24) {
  *HR = 0;
  *DIA++;
 }
 if ((*MES == 1 || *MES == 3 || *MES == 5 || *MES == 7 || *MES == 8 || *MES == 10 || *MES == 12) && *DIA == 32) {
  *DIA = 1;
  *MES++;
 }
 if ((*MES == 4 || *MES == 6 || *MES == 9 || *MES == 11) && *DIA == 31) {
  *DIA = 1;
  *MES++;
 }
 if (*MES == 2 && *ANO%4 == 0 && *DIA == 30) {
  *DIA = 1;
  *MES++;
 }
 if (*MES == 2 && *ANO%4 != 0 && *DIA == 29) {
  *DIA = 1;
  *MES++;
 }
 if (*MES == 13) {
  *MES = 1;
  *ANO++;
 }
}

void picsetup (void) {
 TRISAbits.RA0 = 1;
 TRISAbits.RA1 = 1;
 TRISCbits.RC0 = 1;
 TRISCbits.RC1 = 0;
 TRISCbits.RC6 = 0;
 TRISCbits.RC7 = 0;
}

int input1 (void) {
 int result;
 ADCON1 = 0x0D;
 ADCON0 = 0x00;
 ADCON2 = 0xA1;
 ADCON0bits.ADON = 1;


 while(ADCON0bits.GO_DONE);

 result = ReadADC();
 return result;

}

int input2 (void) {
 int result;
 ADCON1 = 0x0D;
 ADCON0 = 0x00;
 ADCON2 = 0xA1;
 ADCON0bits.ADON = 1;


 while(ADCON0bits.GO_DONE);

 result = ReadADC();
 return result;
}

void zigbee (void) {
}

#1
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: PIC18F2620 with SD-CARD and ADC 2016/02/08 16:18:41 (permalink)
0
What it not working, exactly?
You should not turn on and off the ADC module at every iteration, rather leave it on and just change channel. Then you must also allow for settling time after selecting the channel, before converting.

GENOVA :D :D ! GODO
#2
Jump to:
© 2019 APG vNext Commercial Version 4.5