Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

ADC interrupt every 10ms , 18F452 to MMC using mikroC

Status
Not open for further replies.

kar2on

Member level 1
Joined
Jul 30, 2005
Messages
32
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,728
mikroc timer

I'm trying to write data to an MMC based on values obtained by the ADC. The ADC should sample data and save the value at a frequency of 200HZ, based on a 10MHz crystal running on HS mode. I've pretty much figured out the MMC write and read using the mikroC on a PIC 18f452. The problem is, how do I write an interrupt subroutine for it .

I need the ADC to sample at 200HZ, and then when it has sampled 512 times (only planning to use 8-bits per sample) write that data to the MMC card. How do I write the interrupt for for the ADC to sample at 200Hz, using mikroC? I want to have the interrupt sample the ADC input, save it to an array, then increment a counter. Once the counter reaches 512, reset the counter. In the main function a infinite loop using while(1), will have if statement to test the counter. If the counter is 0 then it will write that the sampled ADC data that was saved into an array to the MMC card.

I just want to know how to write the interrupt, any help will be much appreciated. I don't even know if this is possible, I only took one microcontroller class about 2 years ago, can't really remember it. Plus i did the class using 8051 architecture, now i'm using a PIC. Thanx in advanced.
 

mikroc adc

HI

Use timer inetrrupt insted of ADC interrupt

Set the timer to inerrupt every 10msec and when it enter the ISR call the ADC to convert data wait in a polling loop until the ADC compleate conversation and return to regular operation

P/s search google for inerrupt routine there are many .....

All the best

Bobi
 

mikroc interrupt example

I use CCS C Compiler, it's has more resource and wizard that make this work more easy. The manual of Mikro C informs that SFR are defined automatic and you can use this, without you to specify the memory address of them.

First you have to configure your timer (TIMER 1 for example, to generate a 200Hz frequency) as below:

void main(void)
{
INTCON = 0x80; // This will set GIE (Global interrupts)
T1CON = 0x00; // Timer1 Stopped and preescale 1:1

// when you want to begin to sample start this registers
// look, with a 10Mhz oscillator, an 16bit timer will overflow with 26.2ms (1:1)
// then you have to initialize the timer counter with the value that lacks for the
// overflow -> 1/200Hz = 5ms -> 26.2ms - 5ms = 21.2 ms (value initialized)
// 26.2 ms = 65535 and 21.2 = 53028 = 0xCF24 (will have to initialize registers
// below with this value that represent 21.2ms)
{
TMR1L = 0x24;
TMR1H = 0xCF;
T1CON |= 0x01 // Enable counter
PIE1 = 0x01; // Enable Timer1 interrupt
}
}

// Interrupt service routine
// don't call any function inside of void interrupt(), compiler don't support
void interrupt()
{
// If the interrupt was generated by timer1 overflow
if (PIR & 0x01)
{
// Interrupt enter every 5ms
PIR &= ~0x01; // Clear flag
T1CON &= ~0x01; // Disable counter to avoid errors

// Place ADC code here

TMR1L = 0x24;
TMR1H = 0xCF;
T1CON |= 0x01 // Enable counter
return;
}
}

You can use COMPARE MODE too, this works as same.

Remember: for 200 samples per second you have to sample every 5ms.
 

adc interrupt

hi, why do I need to disable the timer once I enter the interrupt. I intend to save the data into an array not just into a variable. If i disable the timer , then the timing mechanism won't work properly, when I lookk at the assembly code I see that the Timer values don't take into considerations the delay generated by the interrupt. Normally in assembly we take the base value of the timer minus the machine cycles during the ISR to determine the time between 2 instructions inside a timer interrupt. Can start the timer while at the beginning of the interrupt for better accuracy and timing.

Added after 3 minutes:

Sorry, I forgot to mention that if you save data to an array inside an interrupt the code in the Interrupt becomes very very large. SInce the array is 512 bytes long, which is required for an MMC write operation.

Either I do that or,
I write more cmoplex code inside the main program. Making the interrupt simpler at the cost of a more complicated main() function. What I intend to do is, save the ADC to a separate variable in the interrupt, then in the main function enter that variable into the array.

What do you guys think?
 

timer as a counter mikroc

You don't need to disable timer1 when you enter in interrupt, I put it because some time ago my compiler only worked if I don't disable the timer, but in theory it isn't necessary ... a more easy mode is use compare mode of MCU, that form the code is more simpler ... look it mode on specific MCU datasheet. For you write 512 in ISR the timer lost is very long, then you have to set a flag to write it in main(), but your tick rate aren't exactly 200Hz as expected. It depends only if you have more work to do in main that is can't wait your write in ISR, you are true isn't a good idea write a big code on ISR, but it depends of your specific application and how work your system has to perform.

leomecma
 

    kar2on

    Points: 2
    Helpful Answer Positive Rating
mikroc interrupt

The timing and big code in the interrupt is due to polling of ADC, look at the follwing mechanism.

In main (during initialization)
start ADC, Start timer for 5 m sec
In every timer interrupt
read and store the converted digitial value in array
start ADC again

I am sure that ADC conversion will be completed in 5 ms. If the interrupt count reaches 512, Write to MMC in main.

Cheers
idlebrain
 

pic adc interrupt

"In main (during initialization)
start ADC, Start timer for 5 m sec
In every timer interrupt
read and store the converted digitial value in array
start ADC again"

Yeah that's what i plan to do, however I'm afraid that if the interrupt takes(for example) 10 cycles, which 2.5uS(at a 4Mhz freq) then the interval between the interrupts would be 5ms + 2.5uS, which definitely isn't much but still wouldn't be accurate. Does anyone have any ideas? Is this correct? Or does the Timer run even while I'm in the interrupt??
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top