HI, I have a problem.The code brought following should insert, with an interval of time defined in "T3", in a vector, Result [NUM], 100 values from the channel 0 of the converter A/D. Subsequently he should calculate the average of these 100 values and then to send them to the serial port to stamp them to monitor.
#pragma large /*#include <c167cs.h> /*#include <intrins.h>*/ #include <REG167.H> #include <stdio.h> #define NUM 100 unsigned int Result[NUM], media, somma; unsigned char count; void main (void) { /* init serial0 port: */ P3 |= 0x0400; /* set port 3.10 output latch (TXD) */ DP3 |= 0x0400; /* configure port 3.10 for output */ /* operation. ( TXD output) */ DP3 &= 0xF7FF; /* configure port 3.11 for input */ /* operation. ( RXD input) */ S0TIC = 0x80; /* set transmit interrupt flag */ S0RIC = 0x00; /* delete receive interrupt flag */ S0BG = 0x40; /* set baudrate to 9600 baud */ S0CON = 0x8011; /* set serial mode */ putchar(' '); /* send dummy-Byte for compatibility */ /* end of init serial0 port: /* use GPT1 */ T3CON = 0x0007; T3 = 0x000A; /* T3UD=0; T3IC = 0x004B; /* set up the interrupt level and enable it */ T3R = 1; /* start timer 3 running */ ADCON = 0x0000; while (1) { if (T3 >= 0x00AA) { /*ADCON = 0x0000; /* A/D-Converter: Channel0, Single */ ADST = 1; /* Conversion start */ while(ADBSY); /* wait of end of conversion */ Result[count] = (ADDAT & 0x03FF); if (count >= 100) { somma=0; for(count = 1; count <= 100; count++) { somma = somma + Result[count] ; } media = (somma/100) ; printf ("%d\n" , media) ; count = 1 ; } else { count++; } } } }
When I send the code in execution it arrives to me the following sequence that results to be constant in the time:
578 578 578 578 578 578 578 578 578 578 578 578 578 578 578 578 578 578 578 578 578 578 . . . . . .
I just press the key of reset of the card to video it appears me the following endless sequence:
578 -fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff.................
Would someone know, please, to help me? Thanks
Well unless im wrong are you not stuck in the loop?
Did you intend to escape the loop when T3 >= 0x00AA ?
If so then :-
while (1) { if (T3 >= 0x00AA) break; {
This breaks the loop to continue execution when T3 >= 0xAA.
But if you wanted your routine to continually take place after a time interval set with timer 3 then you either need to put the result getting code into the interrut routine for timer3, or use the ADC End Of conversion to do the stuff. At the moment your while (1) loop which reads the ADC will be so fast that all 100 values will probably be the same. I think you may need to totally rethink your code and what it need to do.
Hope this helps.