Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 #include<htc.h> #include<pic.h> __CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & BOREN_OFF & CP_OFF & CPD_OFF); void main() { unsigned int x=0; GIE=0; PEIE=0; TRISA0=1; TRISC4=0; TRISC5=0; RC4=1; RC5=1; ANSEL=0; ADCON1=0x01<<4; ADCON0=(0x80)+1; ADIE=0; ADIF=0; //while(1) { ADRESH=0; ADRESL=0; x=0; GO_DONE=1; while(GO_DONE==1) {} x=(ADRESH<<8)+ADRESL; x&=0x03FF; if( x < 270) { RC4=1; } if(x>270 ) { if(x<=370) { RC5=0; } } if(x > 370) { RC4=0; RC5=0; } } while(1) {} }
#include<htc.h>
#include<pic.h>
__CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & BOREN_OFF & CP_OFF & CPD_OFF);
void main()
{
unsigned int x = 0;
GIE = 0;
PEIE = 0;
TRISA0 = 1;
TRISC4 = 0;
TRISC5 = 0;
RC4 = 1;
RC5 = 1;
ANSEL = 0;
ADCON1 = 0x01 << 4;
ADCON0 = 0x80 + 1;
ADIE = 0;
ADIF = 0;
while(1) {
ADRESH = 0;
ADRESL = 0;
x = 0;
GO_DONE = 1;
while(GO_DONE == 1) {
}
x = (ADRESH << 8) + ADRESL;
x &= 0x03FF;
if (x < 270) {
RC4 = 1;
}
if (x > 270) {
if (x <= 370) {
RC5 = 0;
}
}
if(x > 370) {
RC4 = 0;
RC5 = 0;
}
}
} /* main */
/* EOF */
#include<htc.h>
#include<pic.h>
__CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & BOREN_OFF & CP_OFF & CPD_OFF);
void main()
{
unsigned int x = 0;
GIE = 0;
PEIE = 0;
TRISA0 = 1;
TRISC4 = 0;
TRISC5 = 0;
RC4 = 1;
RC5 = 1;
ANSEL = 0;
ADCON1 = 0x01 << 4;
ADCON0 = 0x80 + 1;
ADIE = 0;
ADIF = 0;
while(1) {
/* start ADC */
GO_DONE = 1;
/* wait for ADC ready */
while(GO_DONE == 1) {
}
/* get result */
x = (ADRESH << 8) + ADRESL;
x &= 0x03FF;
/* set LEDs according to result */
if (x <= 270) { /* I assume 270 should be included */
RC4 = 1;
} else if (x <= 370) {
RC5 = 1; /* I assume RC5 should be set sometime */
} else {
RC4 = 0;
RC5 = 0;
}
}
} /* main */
/* EOF */
If we need to add some delay in code.
e.g. __delay_ms(100);
Then what changes should be in code.
I try many times but massage with error(undefined XTAL_ FREQ).build failed.
I also down load delay.h file from microchip web site.
Though we are using internal clock.
#define _XTAL_FREQ 4000000
#include <htc.h> // Required to interface with delay routines
#ifndef _XTAL_FREQ
// Unless already defined assume 4MHz system frequency
// This definition is required to calibrate __delay_us() and __delay_ms()
#define _XTAL_FREQ 4000000
#endif
/** demonstration of various built-in delay mechanisms */
void main(void){
while(1){
NOP();
_delay(1000); // delay for 1000 instruction cycles
CLRWDT();
__delay_us(400); // delay for 400 microseconds
CLRWDT();
__delay_ms(2); // delay for 2 milliseconds
CLRWDT();
}
}
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include<htc.h> #include<pic.h> __CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & BOREN_OFF & CP_OFF & CPD_OFF); void main() { unsigned int x = 0; GIE = 0; PEIE = 0; TRISA0 = 1; TRISC4 = 0; TRISC5 = 0; RC4 = 1; RC5 = 1; ANSEL = 0; ADCON1 = 0x01 << 4; ADCON0 = 0x80 + 1; ADIE = 0; ADIF = 0; while(1) { ADRESH = 0; ADRESL = 0; x = 0; GO_DONE = 1; while(GO_DONE == 1) { } x = (ADRESH << 8) + ADRESL; x &= 0x03FF; if (x < 270) { RC4 = 1; } if (x > 270) { if (x <= 370) { RC5 = 0; } } if(x > 370) { RC4 = 0; RC5 = 0; } } } /* main */ /* EOF */
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 #include<htc.h> #include<pic.h> #include "delay.h" __CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & BOREN_OFF & CP_OFF & CPD_OFF); int Adc; // Save analog Data void Read_Adc() { GO_DONE=1; // Start Conversion while(GO_DONE==1); // Wait Until Conversion Complete Adc = (ADRESH << 8) + ADRESL; // get 10 bit result 8+2=10 } void main() { TRISA=0b000111; TRISC = 0b000000; //SET WHOLE PORT C AS OUTPUT PORTC=255; __delay_ms(5); RA5=1; if (RA3==1){ __delay_ms(2000); RA5=0; } else (RA5=0); while(1) { ANSEL = 0b00000111; ADCON0=0b10000001;//SELSECT AN0 and ADON Read_Adc(); // Read Analog AN0 if (Adc > 614) { RC0 = 1; //1st led RC1 = 1; //2nd led } if (Adc<512) // if adc is less than 2.498v { RC0 = 0; //led at RC0 is off __delay_ms(5); } if (Adc>532) //if adc more than 2.596v, { RC0 = 1; //RC0 is high } if (Adc < 426 ) { RC1 = 0; } if (Adc > 441 ) { RC1= 1; } ADCON0=0b10000101; //2nd channel selection Read_Adc(); if (Adc > 814) { __delay_ms(10); RA5=1; } if (Adc<650) { __delay_ms(10); RA5=0; } __delay_ms(10); ADCON0=0b10001001; //3rd channel selection Read_Adc(); if (Adc>950) { __delay_ms(200); RA4=1; } if (Adc < 925){ __delay_ms(10); RA4=0; } } } *//end of file
Thanks,skogsjanne and big guru for your suggestions.
In datasheet i found many answers.
Main issue for me was, i found many examples of ADC at internet pages only with 1 adc channel.
And i was in search of example with 2 adc channels.
In ADCON0 register i found,how we can select next channel?
In ADCCON1 we can set adc osc(Ad conversion clock).
In ANSEL register we select which bit is for analog input,and others are for digital I/O.
And in ADRESH,ADRESL we see result of adc (8bit in ADRESH + 2bits in ADRESL).
Now i urge to use Atmel,s AVR.
Which compiler IDE you suggest for AVR?
Best regards,
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?