Hi guys, firstly, great forum. I can't find a more active microcontroller forum on Google!
I've been toying around with the 16F877 and I've had some problems, pertaining to Timer0 interrupt and ADC.
The problem: Enabling timer0 interrupt, affects my ADC values. Specifically when timer0 interrupt is disabled, my ADC values range properly from 0 -> 1024. But with timer0 interrupt enabled, ADC values range from 512 -> 1024. Weird problem! Do allow me to share the code:
I'm thinking it's some problem with the way OPTION_REG is reacting with my ADC clock or something, but I've totally no idea after 2 days spent meddling and Googling. It's seriously killing me!
Any help is appreciated guys! Thanks!
I've been toying around with the 16F877 and I've had some problems, pertaining to Timer0 interrupt and ADC.
The problem: Enabling timer0 interrupt, affects my ADC values. Specifically when timer0 interrupt is disabled, my ADC values range properly from 0 -> 1024. But with timer0 interrupt enabled, ADC values range from 512 -> 1024. Weird problem! Do allow me to share the code:
Code:
void timer0_enable (void) {
/* Initialise Timer 0 */
OPTION = 0b11010111; /* Set TMR0 to Internal CLk, 1:256 */
T0IF = 0; /* Clear TMR0 Flag, ready for use */
T0IE = 1; /* Enable Timer Overflow Interrupt */
}
void initialize_IO_ports (void) {
//set the digital IO ports as per requirement
TRISA = 0xFF ; //portA as input
TRISB = 0B10000000 ; //portB as input
TRISC = 0B11110000; //portC as output
TRISD = 0B10000000; //portD as output
ADCON1 = 0x82; //set PortE as digital io and portA as analog, and VDD and VSS; ADC output is right-justified.
//clear the output ports at the beginning
//PORTD = 0x00 ; //clear portD
// PORTC = 0x00 ; //clear portC
GIE=1; //global interrupt enabled
PEIE=1; //peripheral interrupt enabled
}
void initialize_ADC (void) {
ADON=1; //enable the ADC
ADCS1=0; //set clock source for the ADC
ADCS0=0;
}
int read_ADC_channel (unsigned int channel_number) {
int value;
switch (channel_number) {
case 0:
CHS2=0;
CHS1=0;
CHS0=0;
case 1:
CHS2=0;
CHS1=0;
CHS0=1;
case 2:
CHS2=0;
CHS1=1;
CHS0=0;
case 3:
CHS2=0;
CHS1=1;
CHS0=1;
default:;
}
ADGO=1; //start AD conversion
while(ADGO) {}; //wait for conversion to finish
value=(ADRESH<<8)+ADRESL; //read the values in the registers
return value;
}
void interrupt isr0 (void) {
// for timer0
if (T0IF == 1) {
luggage_present = luggage_ispresent();
T0IF = 0; // timer0 reload
}
}
void main() {
int myInt;
timer0_enable();
initialize_IO_ports();
lcd_init();
initialize_ADC();
// below is just a debugging tool to print ADC values
I'm thinking it's some problem with the way OPTION_REG is reacting with my ADC clock or something, but I've totally no idea after 2 days spent meddling and Googling. It's seriously killing me!
Any help is appreciated guys! Thanks!