Hi
I have a simple problem is driving me crazy:
Let's start "for dummies", just increasing one value of +1 every time I send an input High to RA1:
What 's missing ? Do I need something particular like a timer ? The datasheet is very unclear to me and the example are in assembry (that I don't know).
For some strange reason the number on the segment is not updated, but if I put straightforward
THe display will show me 5 .... strange question
I have a simple problem is driving me crazy:
Let's start "for dummies", just increasing one value of +1 every time I send an input High to RA1:
What 's missing ? Do I need something particular like a timer ? The datasheet is very unclear to me and the example are in assembry (that I don't know).
Code:
#include <htc.h>
/***** CONFIGURATION *****/
__CONFIG(MCLREN & UNPROTECT & WDTDIS & INTIO);
/***** PROTOTYPES *****/
void set7segC(char digit); // display digit on 7-segment display
void set7segAB(char digit); // display digit on 7-segment display
//Global variable, dealt by interrupts
unsigned int adc_dec; // scaled ADC output (0-99)
/***** MAIN PROGRAM *****/
void main()
{
adc_dec = 0; // initialize global value
// configure ports
TRISA = 0b11101011;
ANSEL = 0b00000001;
ANSELH = 0b00000001;
TRISB = 0b00000000; // configure PORTB and PORTC as all outputs
TRISC = 0b00000000;
CM1CON0 = 0; // disable comparator 1 (RB0, RB1, RB2 usable)
CM2CON0 = 0; // disable comparator 2 (RC0, RC1, RC4 usable)
VRCON = 0; // disable CVref (RC2 usable)
// configure ADC
ADCON0 = 0b10000001;
//1------ Right justified
//-0------ VDD
//--0000-- Set AN0 as the input !!!!! AN0 = RA0
//-------00 turn ADC off
ADCON1 = 0b00010000; //Before turning on the ADC
//Interrupts setting
GIE=1;
INTEDG=1;
PEIE=1;
RABIE=1;
IOCA = 0b00000010; //Enable only RA1
ADON=1; // turn on the A2D conversion module
// Main loop
for (;;)
{
// sample input
GODONE = 1; // start conversion
while (GODONE); // wait until done
/*
// scale result to 0-99
//adc_dec =(((ADRESL + (ADRESH << 8)) * 3) / 31);
if(RA1) // Read the volt value on the input while the redled is on
{
red_led = (((ADRESL + (ADRESH << 8)) * 3) / 31);
}
if(RA5)// Read the volt value on the input while the irled is on and update the output
{
ir_led = (((ADRESL + (ADRESH << 8)) * 3) / 31);
adc_dec = ir_led;//red_led / ir_led;
}
*/
set7segC(adc_dec%10); // output ones digit of result on segment bus
set7segAB(adc_dec/10); // output tens digit of result on segment bus
}
}
//Interrupt Handler
void interrupt ISR( void )
{
/* Is this a port change interrupt and is it enabled? */
if( RABIF && RABIE )
{
RABIF = 0;
adc_dec= adc_dec +1;
}
}
For some strange reason the number on the segment is not updated, but if I put straightforward
Code:
void interrupt ISR( void )
{
/* Is this a port change interrupt and is it enabled? */
if( RABIF && RABIE )
{
RABIF = 0;
adc_dec= 5;
}
}
THe display will show me 5 .... strange question